-
Notifications
You must be signed in to change notification settings - Fork 319
/
main.go
187 lines (164 loc) · 5.3 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
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
// Usage:
// make build
// ./bin/server -config-file=./config.yaml
//
package main
import (
"context"
"flag"
"fmt"
glog "log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/iotexproject/go-pkgs/hash"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db/trie/mptrie"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/probe"
"github.com/iotexproject/iotex-core/pkg/recovery"
"github.com/iotexproject/iotex-core/server/itx"
)
/**
* overwritePath is the path to the config file which overwrite default values
* secretPath is the path to the config file store secret values
*/
var (
_genesisPath string
_overwritePath string
_secretPath string
_subChainPath string
_plugins strs
)
type strs []string
func (ss *strs) String() string {
return strings.Join(*ss, ",")
}
func (ss *strs) Set(str string) error {
*ss = append(*ss, str)
return nil
}
func init() {
// set max number of CPUs, disable log printing
maxprocs.Set(maxprocs.Logger(nil))
flag.StringVar(&_genesisPath, "genesis-path", "", "Genesis path")
flag.StringVar(&_overwritePath, "config-path", "", "Config path")
flag.StringVar(&_secretPath, "secret-path", "", "Secret path")
flag.StringVar(&_subChainPath, "sub-config-path", "", "Sub chain Config path")
flag.Var(&_plugins, "plugin", "Plugin of the node")
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr,
"usage: server -config-path=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
}
func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
stopped := make(chan struct{})
livenessCtx, livenessCancel := context.WithCancel(context.Background())
genesisCfg, err := genesis.New(_genesisPath)
if err != nil {
glog.Fatalln("Failed to new genesis config.", zap.Error(err))
}
// set genesis timestamp
genesis.SetGenesisTimestamp(genesisCfg.Timestamp)
if genesis.Timestamp() == 0 {
glog.Fatalln("Genesis timestamp is not set, call genesis.New() first")
}
// load genesis block's hash
block.LoadGenesisHash(&genesisCfg)
if block.GenesisHash() == hash.ZeroHash256 {
glog.Fatalln("Genesis hash is not set, call block.LoadGenesisHash() first")
}
cfg, err := config.New([]string{_overwritePath, _secretPath}, _plugins)
if err != nil {
glog.Fatalln("Failed to new config.", zap.Error(err))
}
if err = initLogger(cfg); err != nil {
glog.Fatalln("Cannot config global logger, use default one: ", zap.Error(err))
}
if err = recovery.SetCrashlogDir(cfg.System.SystemLogDBPath); err != nil {
glog.Fatalln("Failed to set directory of crashlog: ", zap.Error(err))
}
defer recovery.Recover()
// check EVM network ID and chain ID
if cfg.Chain.EVMNetworkID == 0 || cfg.Chain.ID == 0 {
glog.Fatalln("EVM Network ID or Chain ID is not set, call config.New() first")
}
cfg.Genesis = genesisCfg
cfgToLog := cfg
cfgToLog.Chain.ProducerPrivKey = ""
cfgToLog.Network.MasterKey = ""
log.S().Infof("Config in use: %+v", cfgToLog)
log.S().Infof("EVM Network ID: %d, Chain ID: %d", cfg.Chain.EVMNetworkID, cfg.Chain.ID)
log.S().Infof("Genesis timestamp: %d", genesisCfg.Timestamp)
log.S().Infof("Genesis hash: %x", block.GenesisHash())
// liveness start
probeSvr := probe.New(cfg.System.HTTPStatsPort)
if err := probeSvr.Start(ctx); err != nil {
log.L().Fatal("Failed to start probe server.", zap.Error(err))
}
go func() {
<-stop
// start stopping
cancel()
<-stopped
// liveness end
if err := probeSvr.Stop(livenessCtx); err != nil {
log.L().Error("Error when stopping probe server.", zap.Error(err))
}
livenessCancel()
}()
if cfg.System.MptrieLogPath != "" {
if err = mptrie.OpenLogDB(cfg.System.MptrieLogPath); err != nil {
log.L().Fatal("Failed to open mptrie log DB.", zap.Error(err))
}
defer func() {
if err = mptrie.CloseLogDB(); err != nil {
log.L().Error("Failed to close mptrie log DB.", zap.Error(err))
}
}()
}
// create and start the node
svr, err := itx.NewServer(cfg)
if err != nil {
log.L().Fatal("Failed to create server.", zap.Error(err))
}
var cfgsub config.Config
if _subChainPath != "" {
cfgsub, err = config.NewSub([]string{_secretPath, _subChainPath})
if err != nil {
log.L().Fatal("Failed to new sub chain config.", zap.Error(err))
}
} else {
cfgsub = config.Config{}
}
if cfgsub.Chain.ID != 0 {
if err := svr.NewSubChainService(cfgsub); err != nil {
log.L().Fatal("Failed to new sub chain.", zap.Error(err))
}
}
itx.StartServer(ctx, svr, probeSvr, cfg)
close(stopped)
<-livenessCtx.Done()
}
func initLogger(cfg config.Config) error {
addr := cfg.Chain.ProducerAddress()
return log.InitLoggers(cfg.Log, cfg.SubLogs, zap.AddCaller(), zap.Fields(
zap.String("ioAddr", addr.String()),
))
}