-
Notifications
You must be signed in to change notification settings - Fork 292
/
main.go
460 lines (418 loc) · 12.2 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/hex"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology-eventbus/actor"
alog "github.com/ontio/ontology-eventbus/log"
"github.com/ontio/ontology/account"
"github.com/ontio/ontology/cmd"
cmdcom "github.com/ontio/ontology/cmd/common"
"github.com/ontio/ontology/cmd/utils"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/consensus"
"github.com/ontio/ontology/core/genesis"
"github.com/ontio/ontology/core/ledger"
"github.com/ontio/ontology/events"
bactor "github.com/ontio/ontology/http/base/actor"
hserver "github.com/ontio/ontology/http/base/actor"
"github.com/ontio/ontology/http/jsonrpc"
"github.com/ontio/ontology/http/localrpc"
"github.com/ontio/ontology/http/nodeinfo"
"github.com/ontio/ontology/http/restful"
"github.com/ontio/ontology/http/websocket"
"github.com/ontio/ontology/p2pserver"
netreqactor "github.com/ontio/ontology/p2pserver/actor/req"
p2pactor "github.com/ontio/ontology/p2pserver/actor/server"
"github.com/ontio/ontology/txnpool"
tc "github.com/ontio/ontology/txnpool/common"
"github.com/ontio/ontology/txnpool/proc"
"github.com/ontio/ontology/validator/stateful"
"github.com/ontio/ontology/validator/stateless"
"github.com/urfave/cli"
)
func setupAPP() *cli.App {
app := cli.NewApp()
app.Usage = "Ontology CLI"
app.Action = startOntology
app.Version = config.Version
app.Copyright = "Copyright in 2018 The Ontology Authors"
app.Commands = []cli.Command{
cmd.AccountCommand,
cmd.InfoCommand,
cmd.AssetCommand,
cmd.ContractCommand,
cmd.ImportCommand,
cmd.ExportCommand,
cmd.TxCommond,
cmd.SigTxCommand,
cmd.MultiSigAddrCommand,
cmd.MultiSigTxCommand,
cmd.SendTxCommand,
cmd.ShowTxCommand,
}
app.Flags = []cli.Flag{
//common setting
utils.ConfigFlag,
utils.LogLevelFlag,
utils.DisableLogFileFlag,
utils.DisableEventLogFlag,
utils.DataDirFlag,
//account setting
utils.WalletFileFlag,
utils.AccountAddressFlag,
utils.AccountPassFlag,
//consensus setting
utils.EnableConsensusFlag,
utils.MaxTxInBlockFlag,
//txpool setting
utils.GasPriceFlag,
utils.GasLimitFlag,
utils.TxpoolPreExecDisableFlag,
utils.DisableSyncVerifyTxFlag,
utils.DisableBroadcastNetTxFlag,
//p2p setting
utils.ReservedPeersOnlyFlag,
utils.ReservedPeersFileFlag,
utils.NetworkIdFlag,
utils.NodePortFlag,
utils.HttpInfoPortFlag,
utils.MaxConnInBoundFlag,
utils.MaxConnOutBoundFlag,
utils.MaxConnInBoundForSingleIPFlag,
//test mode setting
utils.EnableTestModeFlag,
utils.TestModeGenBlockTimeFlag,
//rpc setting
utils.RPCDisabledFlag,
utils.RPCPortFlag,
utils.RPCLocalEnableFlag,
utils.RPCLocalProtFlag,
//rest setting
utils.RestfulEnableFlag,
utils.RestfulPortFlag,
utils.RestfulMaxConnsFlag,
//ws setting
utils.WsEnabledFlag,
utils.WsPortFlag,
}
app.Before = func(context *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
return nil
}
return app
}
func main() {
if err := setupAPP().Run(os.Args); err != nil {
cmd.PrintErrorMsg(err.Error())
os.Exit(1)
}
}
func startOntology(ctx *cli.Context) {
initLog(ctx)
log.Infof("ontology version %s", config.Version)
setMaxOpenFiles()
cfg, err := initConfig(ctx)
if err != nil {
log.Errorf("initConfig error: %s", err)
return
}
acc, err := initAccount(ctx)
if err != nil {
log.Errorf("initWallet error: %s", err)
return
}
stateHashHeight := config.GetStateHashCheckHeight(cfg.P2PNode.NetworkId)
ldg, err := initLedger(ctx, stateHashHeight)
if err != nil {
log.Errorf("%s", err)
return
}
txpool, err := initTxPool(ctx)
if err != nil {
log.Errorf("initTxPool error: %s", err)
return
}
p2pSvr, p2pPid, err := initP2PNode(ctx, txpool)
if err != nil {
log.Errorf("initP2PNode error: %s", err)
return
}
_, err = initConsensus(ctx, p2pPid, txpool, acc)
if err != nil {
log.Errorf("initConsensus error: %s", err)
return
}
err = initRpc(ctx)
if err != nil {
log.Errorf("initRpc error: %s", err)
return
}
err = initLocalRpc(ctx)
if err != nil {
log.Errorf("initLocalRpc error: %s", err)
return
}
initRestful(ctx)
initWs(ctx)
initNodeInfo(ctx, p2pSvr)
go logCurrBlockHeight()
waitToExit(ldg)
}
func initLog(ctx *cli.Context) {
//init log module
logLevel := ctx.GlobalInt(utils.GetFlagName(utils.LogLevelFlag))
//if true, the log will not be output to the file
disableLogFile := ctx.GlobalBool(utils.GetFlagName(utils.DisableLogFileFlag))
if disableLogFile {
log.InitLog(logLevel, log.Stdout)
} else {
alog.InitLog(log.PATH)
log.InitLog(logLevel, log.PATH, log.Stdout)
}
}
func initConfig(ctx *cli.Context) (*config.OntologyConfig, error) {
//init ontology config from cli
cfg, err := cmd.SetOntologyConfig(ctx)
if err != nil {
return nil, err
}
log.Infof("Config init success")
return cfg, nil
}
func initAccount(ctx *cli.Context) (*account.Account, error) {
if !config.DefConfig.Consensus.EnableConsensus {
return nil, nil
}
walletFile := ctx.GlobalString(utils.GetFlagName(utils.WalletFileFlag))
if walletFile == "" {
return nil, fmt.Errorf("Please config wallet file using --wallet flag")
}
if !common.FileExisted(walletFile) {
return nil, fmt.Errorf("Cannot find wallet file: %s. Please create a wallet first", walletFile)
}
acc, err := cmdcom.GetAccount(ctx)
if err != nil {
return nil, fmt.Errorf("get account error: %s", err)
}
log.Infof("Using account: %s", acc.Address.ToBase58())
if config.DefConfig.Genesis.ConsensusType == config.CONSENSUS_TYPE_SOLO {
curPk := hex.EncodeToString(keypair.SerializePublicKey(acc.PublicKey))
config.DefConfig.Genesis.SOLO.Bookkeepers = []string{curPk}
}
log.Infof("Account init success")
return acc, nil
}
func initLedger(ctx *cli.Context, stateHashHeight uint32) (*ledger.Ledger, error) {
events.Init() //Init event hub
var err error
dbDir := utils.GetStoreDirPath(config.DefConfig.Common.DataDir, config.DefConfig.P2PNode.NetworkName)
ledger.DefLedger, err = ledger.NewLedger(dbDir, stateHashHeight)
if err != nil {
return nil, fmt.Errorf("NewLedger error: %s", err)
}
bookKeepers, err := config.DefConfig.GetBookkeepers()
if err != nil {
return nil, fmt.Errorf("GetBookkeepers error: %s", err)
}
genesisConfig := config.DefConfig.Genesis
genesisBlock, err := genesis.BuildGenesisBlock(bookKeepers, genesisConfig)
if err != nil {
return nil, fmt.Errorf("genesisBlock error %s", err)
}
err = ledger.DefLedger.Init(bookKeepers, genesisBlock)
if err != nil {
return nil, fmt.Errorf("Init ledger error: %s", err)
}
log.Infof("Ledger init success")
return ledger.DefLedger, nil
}
func initTxPool(ctx *cli.Context) (*proc.TXPoolServer, error) {
disablePreExec := ctx.GlobalBool(utils.GetFlagName(utils.TxpoolPreExecDisableFlag))
bactor.DisableSyncVerifyTx = ctx.GlobalBool(utils.GetFlagName(utils.DisableSyncVerifyTxFlag))
disableBroadcastNetTx := ctx.GlobalBool(utils.GetFlagName(utils.DisableBroadcastNetTxFlag))
txPoolServer, err := txnpool.StartTxnPoolServer(disablePreExec, disableBroadcastNetTx)
if err != nil {
return nil, fmt.Errorf("Init txpool error: %s", err)
}
stlValidator, _ := stateless.NewValidator("stateless_validator")
stlValidator.Register(txPoolServer.GetPID(tc.VerifyRspActor))
stlValidator2, _ := stateless.NewValidator("stateless_validator2")
stlValidator2.Register(txPoolServer.GetPID(tc.VerifyRspActor))
stfValidator, _ := stateful.NewValidator("stateful_validator")
stfValidator.Register(txPoolServer.GetPID(tc.VerifyRspActor))
hserver.SetTxnPoolPid(txPoolServer.GetPID(tc.TxPoolActor))
hserver.SetTxPid(txPoolServer.GetPID(tc.TxActor))
log.Infof("TxPool init success")
return txPoolServer, nil
}
func initP2PNode(ctx *cli.Context, txpoolSvr *proc.TXPoolServer) (*p2pserver.P2PServer, *actor.PID, error) {
if config.DefConfig.Genesis.ConsensusType == config.CONSENSUS_TYPE_SOLO {
return nil, nil, nil
}
p2p := p2pserver.NewServer()
p2pActor := p2pactor.NewP2PActor(p2p)
p2pPID, err := p2pActor.Start()
if err != nil {
return nil, nil, fmt.Errorf("p2pActor init error %s", err)
}
p2p.SetPID(p2pPID)
err = p2p.Start()
if err != nil {
return nil, nil, fmt.Errorf("p2p service start error %s", err)
}
netreqactor.SetTxnPoolPid(txpoolSvr.GetPID(tc.TxActor))
txpoolSvr.RegisterActor(tc.NetActor, p2pPID)
hserver.SetNetServerPID(p2pPID)
p2p.WaitForPeersStart()
log.Infof("P2P init success")
return p2p, p2pPID, nil
}
func initConsensus(ctx *cli.Context, p2pPid *actor.PID, txpoolSvr *proc.TXPoolServer, acc *account.Account) (consensus.ConsensusService, error) {
if !config.DefConfig.Consensus.EnableConsensus {
return nil, nil
}
pool := txpoolSvr.GetPID(tc.TxPoolActor)
consensusType := strings.ToLower(config.DefConfig.Genesis.ConsensusType)
consensusService, err := consensus.NewConsensusService(consensusType, acc, pool, nil, p2pPid)
if err != nil {
return nil, fmt.Errorf("NewConsensusService %s error: %s", consensusType, err)
}
consensusService.Start()
netreqactor.SetConsensusPid(consensusService.GetPID())
hserver.SetConsensusPid(consensusService.GetPID())
log.Infof("Consensus init success")
return consensusService, nil
}
func initRpc(ctx *cli.Context) error {
if !config.DefConfig.Rpc.EnableHttpJsonRpc {
return nil
}
var err error
exitCh := make(chan interface{}, 0)
go func() {
err = jsonrpc.StartRPCServer()
close(exitCh)
}()
flag := false
select {
case <-exitCh:
if !flag {
return err
}
case <-time.After(time.Millisecond * 5):
flag = true
}
log.Infof("Rpc init success")
return nil
}
func initLocalRpc(ctx *cli.Context) error {
if !ctx.GlobalBool(utils.GetFlagName(utils.RPCLocalEnableFlag)) {
return nil
}
var err error
exitCh := make(chan interface{}, 0)
go func() {
err = localrpc.StartLocalServer()
close(exitCh)
}()
flag := false
select {
case <-exitCh:
if !flag {
return err
}
case <-time.After(time.Millisecond * 5):
flag = true
}
log.Infof("Local rpc init success")
return nil
}
func initRestful(ctx *cli.Context) {
if !config.DefConfig.Restful.EnableHttpRestful {
return
}
go restful.StartServer()
log.Infof("Restful init success")
}
func initWs(ctx *cli.Context) {
if !config.DefConfig.Ws.EnableHttpWs {
return
}
websocket.StartServer()
log.Infof("Ws init success")
}
func initNodeInfo(ctx *cli.Context, p2pSvr *p2pserver.P2PServer) {
if config.DefConfig.P2PNode.HttpInfoPort == 0 {
return
}
go nodeinfo.StartServer(p2pSvr.GetNetWork())
log.Infof("Nodeinfo init success")
}
func logCurrBlockHeight() {
ticker := time.NewTicker(config.DEFAULT_GEN_BLOCK_TIME * time.Second)
for {
select {
case <-ticker.C:
log.Infof("CurrentBlockHeight = %d", ledger.DefLedger.GetCurrentBlockHeight())
isNeedNewFile := log.CheckIfNeedNewFile()
if isNeedNewFile {
log.ClosePrintLog()
log.InitLog(int(config.DefConfig.Common.LogLevel), log.PATH, log.Stdout)
}
}
}
}
func setMaxOpenFiles() {
max, err := fdlimit.Maximum()
if err != nil {
log.Errorf("failed to get maximum open files: %v", err)
return
}
_, err = fdlimit.Raise(uint64(max))
if err != nil {
log.Errorf("failed to set maximum open files: %v", err)
return
}
}
func waitToExit(db *ledger.Ledger) {
exit := make(chan bool, 0)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for sig := range sc {
log.Infof("Ontology received exit signal: %v.", sig.String())
log.Infof("closing ledger...")
db.Close()
close(exit)
break
}
}()
<-exit
}