forked from pactus-project/pactus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
176 lines (146 loc) · 4.05 KB
/
node.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package node
import (
"time"
"github.com/pactus-project/pactus/config"
"github.com/pactus-project/pactus/consensus"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/network"
"github.com/pactus-project/pactus/state"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/sync"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/txpool"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/logger"
"github.com/pactus-project/pactus/version"
"github.com/pactus-project/pactus/www/grpc"
"github.com/pactus-project/pactus/www/http"
"github.com/pactus-project/pactus/www/nanomsg"
"github.com/pactus-project/pactus/www/nanomsg/event"
"github.com/pkg/errors"
)
type Node struct {
genesisDoc *genesis.Genesis
config *config.Config
state state.Facade
store store.Store
txPool txpool.TxPool
consMgr consensus.Manager
network network.Network
sync sync.Synchronizer
http *http.Server
grpc *grpc.Server
nanomsg *nanomsg.Server
}
func NewNode(genDoc *genesis.Genesis, conf *config.Config,
valKeys []*bls.ValidatorKey, rewardAddrs []crypto.Address,
) (*Node, error) {
// Initialize the logger
logger.InitGlobalLogger(conf.Logger)
logger.Info("You are running a Pactus blockchain",
"version", version.Version(),
"network", genDoc.ChainType())
net, err := network.NewNetwork(conf.Network)
if err != nil {
return nil, err
}
messageCh := make(chan message.Message, 500)
eventCh := make(chan event.Event, 500)
if !conf.Nanomsg.Enable {
eventCh = nil
}
txPool := txpool.NewTxPool(conf.TxPool, messageCh)
// TODO implement dequeue for recent transaction
str, err := store.NewStore(conf.Store)
if err != nil {
return nil, err
}
st, err := state.LoadOrNewState(genDoc, valKeys, str, txPool, eventCh)
if err != nil {
return nil, err
}
consMgr := consensus.NewManager(conf.Consensus, st, valKeys, rewardAddrs, messageCh)
syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh)
if err != nil {
return nil, err
}
httpServer := http.NewServer(conf.HTTP)
grpcServer := grpc.NewServer(conf.GRPC, st, syn, net, consMgr)
nanomsgServer := nanomsg.NewServer(conf.Nanomsg, eventCh)
node := &Node{
config: conf,
genesisDoc: genDoc,
network: net,
state: st,
txPool: txPool,
consMgr: consMgr,
sync: syn,
store: str,
http: httpServer,
grpc: grpcServer,
nanomsg: nanomsgServer,
}
return node, nil
}
func (n *Node) Start() error {
now := util.Now()
genTime := n.genesisDoc.GenesisTime()
if genTime.After(now) {
logger.Info("💤 Genesis time is in the future. Sleeping until then...",
"duration", genTime.Sub(now), "genesis_hash", n.genesisDoc.Hash())
time.Sleep(genTime.Sub(now) - 1*time.Second)
}
if err := n.network.Start(); err != nil {
return err
}
// Wait for network to started
time.Sleep(1 * time.Second)
if err := n.sync.Start(); err != nil {
return err
}
if err := n.consMgr.Start(); err != nil {
return err
}
err := n.grpc.StartServer()
if err != nil {
return errors.Wrap(err, "could not start grpc server")
}
err = n.http.StartServer(n.grpc.Address())
if err != nil {
return errors.Wrap(err, "could not start http server")
}
err = n.nanomsg.StartServer()
if err != nil {
return errors.Wrap(err, "could not start nanomsg server")
}
return nil
}
func (n *Node) Stop() {
logger.Info("stopping Node")
n.consMgr.Stop()
n.network.Stop()
n.sync.Stop()
n.state.Close()
n.store.Close()
n.http.StopServer()
n.grpc.StopServer()
n.nanomsg.StopServer()
}
// these methods are using by GUI.
func (n *Node) ConsManager() consensus.ManagerReader {
return n.consMgr
}
func (n *Node) Sync() sync.Synchronizer {
return n.sync
}
func (n *Node) State() state.Facade {
return n.state
}
func (n *Node) GRPC() *grpc.Server {
return n.grpc
}
func (n *Node) Network() network.Network {
return n.network
}