-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (102 loc) · 3.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/base64"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/go-grpc-middleware/validator"
"github.com/satellitex/bbft/config"
"github.com/satellitex/bbft/controller"
"github.com/satellitex/bbft/convertor"
"github.com/satellitex/bbft/dba"
. "github.com/satellitex/bbft/grpc"
"github.com/satellitex/bbft/model"
"github.com/satellitex/bbft/proto"
"github.com/satellitex/bbft/usecase"
"google.golang.org/grpc"
"log"
"net"
"os"
"time"
)
func DecodeString64(s string) []byte {
ret, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err.Error())
}
return ret
}
func DemoGenesisCommit(conf *config.BBFTConfig, factory model.ModelFactory, bc dba.BlockChain, ps dba.PeerService) {
// myself addPeer
conf.PublicKey = DecodeString64(conf.Demo.PublicKey)
conf.SecretKey = DecodeString64(conf.Demo.SecretKey)
ps.AddPeer(factory.NewPeer(conf.Demo.Host1+":"+conf.Demo.Port1, DecodeString64(conf.Demo.Pubkey1)))
ps.AddPeer(factory.NewPeer(conf.Demo.Host2+":"+conf.Demo.Port2, DecodeString64(conf.Demo.Pubkey2)))
ps.AddPeer(factory.NewPeer(conf.Demo.Host3+":"+conf.Demo.Port3, DecodeString64(conf.Demo.Pubkey3)))
ps.AddPeer(factory.NewPeer(conf.Demo.Host4+":"+conf.Demo.Port4, DecodeString64(conf.Demo.Pubkey4)))
genesisBlock, err := factory.NewBlock(0, nil, 0, nil)
if err != nil {
panic("DemoGenesisCommit: " + err.Error())
}
bc.Commit(genesisBlock)
}
func OnceNodeGenesis(conf *config.BBFTConfig, factory model.ModelFactory, bc dba.BlockChain, ps dba.PeerService) {
// myself addPeer
conf.PublicKey, conf.SecretKey = convertor.NewKeyPair()
ps.AddPeer(factory.NewPeer(conf.Host+":"+conf.Port, conf.PublicKey))
genesisBlock, err := factory.NewBlock(0, nil, 0, nil)
if err != nil {
panic("DemoGenesisCommit: " + err.Error())
}
bc.Commit(genesisBlock)
}
func main() {
log.Println("=========================== boot bbft ===========================")
config.Init()
conf := config.GetConfig()
l, err := net.Listen("tcp", ":"+conf.Port)
if err != nil {
panic(err.Error())
}
log.Println("Succcess New Listen")
ps := dba.NewPeerServiceOnMemory()
author := convertor.NewAuthor(ps)
queue := dba.NewProposalTxQueueOnMemory(conf)
lock := dba.NewLockOnMemory(ps, conf)
pool := dba.NewReceiverPoolOnMemory(conf)
bc := dba.NewBlockChainOnMemory()
slv := convertor.NewStatelessValidator()
sender := NewGrpcConsensusSender(conf, ps)
receivChan := usecase.NewReceiveChannel(conf)
consensusReceiver := usecase.NewConsensusReceiverUsecase(queue, ps, lock, pool, bc, slv, sender, receivChan)
clientRceiver := usecase.NewClientGateReceiverUsecase(slv, sender)
log.Println("Success New Receivers")
s := grpc.NewServer([]grpc.ServerOption{
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_validator.UnaryServerInterceptor(),
grpc_ctxtags.UnaryServerInterceptor(),
grpc_recovery.UnaryServerInterceptor(),
)),
}...)
log.Println("Success New Server")
bbft.RegisterConsensusGateServer(s, controller.NewConsensusController(consensusReceiver, author))
bbft.RegisterTxGateServer(s, controller.NewClientGateController(clientRceiver, author))
log.Println("Success New Register Endpoint")
log.Println("Set Up!!")
sfv := convertor.NewStatefulValidator(bc)
factory := convertor.NewModelFactory()
consensus := usecase.NewConsensusStepUsecase(conf, bc, ps, lock, queue, sender, slv, sfv, factory, receivChan)
if os.Getenv("DEMO") != "" {
time.Sleep(time.Second * 2)
DemoGenesisCommit(conf, factory, bc, ps)
} else {
OnceNodeGenesis(conf, factory, bc, ps)
}
// Consensus Run!!
go func() {
consensus.Run()
}()
if err := s.Serve(l); err != nil {
log.Println("Failed to server grpc: ", err.Error())
}
}