forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
357 lines (314 loc) · 8.62 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"time"
"github.com/elastos/Elastos.ELA/blockchain"
cmdcom "github.com/elastos/Elastos.ELA/cmd/common"
"github.com/elastos/Elastos.ELA/common/log"
"github.com/elastos/Elastos.ELA/core/types"
"github.com/elastos/Elastos.ELA/dpos"
"github.com/elastos/Elastos.ELA/dpos/account"
dlog "github.com/elastos/Elastos.ELA/dpos/log"
"github.com/elastos/Elastos.ELA/dpos/state"
"github.com/elastos/Elastos.ELA/dpos/store"
"github.com/elastos/Elastos.ELA/elanet"
"github.com/elastos/Elastos.ELA/elanet/routes"
"github.com/elastos/Elastos.ELA/mempool"
"github.com/elastos/Elastos.ELA/p2p"
"github.com/elastos/Elastos.ELA/p2p/msg"
"github.com/elastos/Elastos.ELA/pow"
"github.com/elastos/Elastos.ELA/servers"
"github.com/elastos/Elastos.ELA/servers/httpjsonrpc"
"github.com/elastos/Elastos.ELA/servers/httpnodeinfo"
"github.com/elastos/Elastos.ELA/servers/httprestful"
"github.com/elastos/Elastos.ELA/servers/httpwebsocket"
"github.com/elastos/Elastos.ELA/utils"
"github.com/elastos/Elastos.ELA/utils/elalog"
"github.com/elastos/Elastos.ELA/utils/signal"
"github.com/urfave/cli"
)
var (
// Build version generated when build program.
Version string
// The go source code version at build.
GoVersion string
// printStateInterval is the interval to print out peer-to-peer network
// state.
printStateInterval = time.Minute
)
func main() {
if err := setupNode().Run(os.Args); err != nil {
cmdcom.PrintErrorMsg(err.Error())
os.Exit(1)
}
}
func setupNode() *cli.App {
app := cli.NewApp()
app.Name = "ela"
app.Version = Version
app.HelpName = "ela"
app.Usage = "ela node for elastos blockchain"
app.UsageText = "ela [options] [args]"
app.Flags = []cli.Flag{
cmdcom.ConfigFileFlag,
cmdcom.DataDirFlag,
cmdcom.AccountPasswordFlag,
}
app.Action = func(c *cli.Context) {
setupConfig(c)
setupLog(c)
startNode(c)
}
app.Before = func(c *cli.Context) error {
// Use all processor cores.
runtime.GOMAXPROCS(runtime.NumCPU())
// Block and transaction processing can cause bursty allocations. This
// limits the garbage collector from excessively overallocating during
// bursts. This value was arrived at with the help of profiling live
// usage.
debug.SetGCPercent(10)
return nil
}
return app
}
func setupConfig(c *cli.Context) {
configPath := c.String("conf")
var err error
file, err := loadConfigFile(configPath)
if err != nil {
if c.IsSet("conf") {
cmdcom.PrintErrorMsg(err.Error())
os.Exit(1)
}
file = &defaultConfig
}
cfg, err = loadConfigParams(file)
if err != nil {
cmdcom.PrintErrorMsg(err.Error())
os.Exit(1)
}
}
func startNode(c *cli.Context) {
// Enable http profiling server if requested.
if cfg.ProfilePort != 0 {
go utils.StartPProf(cfg.ProfilePort)
}
flagDataDir := c.String("datadir")
dataDir := filepath.Join(flagDataDir, dataPath)
var act account.Account
if cfg.DPoSConfiguration.EnableArbiter {
password, err := cmdcom.GetFlagPassword(c)
if err != nil {
printErrorAndExit(err)
}
act, err = account.Open(password)
if err != nil {
printErrorAndExit(err)
}
}
log.Infof("Node version: %s", Version)
log.Info(GoVersion)
var interrupt = signal.NewInterrupt()
// fixme remove singleton Ledger
ledger := blockchain.Ledger{}
// Initializes the foundation address
blockchain.FoundationAddress = activeNetParams.Foundation
var dposStore store.IDposStore
chainStore, err := blockchain.NewChainStore(dataDir, activeNetParams.GenesisBlock)
if err != nil {
printErrorAndExit(err)
}
defer chainStore.Close()
ledger.Store = chainStore // fixme
dposStore, err = store.NewDposStore(dataDir)
if err != nil {
printErrorAndExit(err)
}
defer dposStore.Close()
txMemPool := mempool.NewTxPool(activeNetParams)
blockMemPool := mempool.NewBlockPool(activeNetParams)
blockMemPool.Store = chainStore
blockchain.DefaultLedger = &ledger // fixme
arbiters, err := state.NewArbitrators(activeNetParams, nil,
chainStore.GetHeight, func() (*types.Block, error) {
hash := chainStore.GetCurrentBlockHash()
block, err := chainStore.GetBlock(hash)
if err != nil {
return nil, err
}
blockchain.CalculateTxsFee(block)
return block, nil
}, func(height uint32) (*types.Block, error) {
hash, err := chainStore.GetBlockHash(height)
if err != nil {
return nil, err
}
block, err := chainStore.GetBlock(hash)
if err != nil {
return nil, err
}
blockchain.CalculateTxsFee(block)
return block, nil
})
if err != nil {
printErrorAndExit(err)
}
ledger.Arbitrators = arbiters // fixme
chain, err := blockchain.New(chainStore, activeNetParams, arbiters.State)
if err != nil {
printErrorAndExit(err)
}
ledger.Blockchain = chain // fixme
blockMemPool.Chain = chain
routesCfg := &routes.Config{TimeSource: chain.TimeSource}
if act != nil {
routesCfg.PID = act.PublicKeyBytes()
routesCfg.Addr = fmt.Sprintf("%s:%d",
cfg.DPoSConfiguration.IPAddress,
cfg.DPoSConfiguration.DPoSPort)
routesCfg.Sign = act.Sign
}
route := routes.New(routesCfg)
server, err := elanet.NewServer(dataDir, &elanet.Config{
Chain: chain,
ChainParams: activeNetParams,
PermanentPeers: cfg.PermanentPeers,
TxMemPool: txMemPool,
BlockMemPool: blockMemPool,
Routes: route,
})
if err != nil {
printErrorAndExit(err)
}
routesCfg.IsCurrent = server.IsCurrent
routesCfg.RelayAddr = server.RelayInventory
blockMemPool.IsCurrent = server.IsCurrent
var arbitrator *dpos.Arbitrator
if act != nil {
dcfg := cfg.DPoSConfiguration
dlog.Init(dcfg.PrintLevel, dcfg.MaxPerLogSize, dcfg.MaxLogsSize)
arbitrator, err = dpos.NewArbitrator(act, dpos.Config{
EnableEventLog: true,
EnableEventRecord: false,
Localhost: cfg.DPoSConfiguration.IPAddress,
ChainParams: activeNetParams,
Arbitrators: arbiters,
Store: dposStore,
Server: server,
TxMemPool: txMemPool,
BlockMemPool: blockMemPool,
Broadcast: func(msg p2p.Message) {
server.BroadcastMessage(msg)
},
AnnounceAddr: route.AnnounceAddr,
})
if err != nil {
printErrorAndExit(err)
}
routesCfg.OnCipherAddr = arbitrator.OnCipherAddr
servers.Arbiter = arbitrator
arbitrator.Start()
defer arbitrator.Stop()
}
servers.Compile = Version
servers.Config = cfg
servers.Chain = chain
servers.Store = chainStore
servers.TxMemPool = txMemPool
servers.Server = server
servers.Arbiters = arbiters
servers.Pow = pow.NewService(&pow.Config{
PayToAddr: cfg.PowConfiguration.PayToAddr,
MinerInfo: cfg.PowConfiguration.MinerInfo,
Chain: chain,
ChainParams: activeNetParams,
TxMemPool: txMemPool,
BlkMemPool: blockMemPool,
BroadcastBlock: func(block *types.Block) {
hash := block.Hash()
server.RelayInventory(msg.NewInvVect(msg.InvTypeBlock, &hash), block)
},
Arbitrators: arbiters,
})
// initialize producer state after arbiters has initialized.
if err = chain.InitProducerState(interrupt.C, pgBar.Start,
pgBar.Increase); err != nil {
printErrorAndExit(err)
}
pgBar.Stop()
log.Info("Start the P2P networks")
server.Start()
defer server.Stop()
log.Info("Start services")
if cfg.EnableRPC {
go httpjsonrpc.StartRPCServer()
}
if cfg.HttpRestStart {
go httprestful.StartServer()
}
if cfg.HttpWsStart {
go httpwebsocket.Start()
}
if cfg.HttpInfoStart {
go httpnodeinfo.StartServer()
}
go printSyncState(chainStore, server)
waitForSyncFinish(server, interrupt.C)
if interrupt.Interrupted() {
return
}
log.Info("Start consensus")
if cfg.PowConfiguration.AutoMining {
log.Info("Start POW Services")
go servers.Pow.Start()
}
<-interrupt.C
}
func printErrorAndExit(err error) {
log.Error(err)
os.Exit(-1)
}
func waitForSyncFinish(server elanet.Server, interrupt <-chan struct{}) {
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
out:
for {
select {
case <-ticker.C:
if server.IsCurrent() {
break out
}
case <-interrupt:
break out
}
}
}
func printSyncState(db blockchain.IChainStore, server elanet.Server) {
statlog := elalog.NewBackend(logger.Writer()).Logger("STAT",
elalog.LevelInfo)
ticker := time.NewTicker(printStateInterval)
defer ticker.Stop()
for range ticker.C {
var buf bytes.Buffer
buf.WriteString("-> ")
buf.WriteString(strconv.FormatUint(uint64(db.GetHeight()), 10))
peers := server.ConnectedPeers()
buf.WriteString(" [")
for i, p := range peers {
buf.WriteString(strconv.FormatUint(uint64(p.ToPeer().Height()), 10))
buf.WriteString(" ")
buf.WriteString(p.ToPeer().String())
if i != len(peers)-1 {
buf.WriteString(", ")
}
}
buf.WriteString("]")
statlog.Info(buf.String())
}
}