forked from BooniesFX/ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (224 loc) · 6.73 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
/*
* 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 (
"errors"
"fmt"
"os"
"os/signal"
"runtime"
"sort"
"strings"
"syscall"
"time"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/account"
"github.com/ontio/ontology/cmd"
"github.com/ontio/ontology/cmd/utils"
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/consensus"
"github.com/ontio/ontology/core/ledger"
ldgactor "github.com/ontio/ontology/core/ledger/actor"
"github.com/ontio/ontology/events"
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/net"
"github.com/ontio/ontology/net/protocol"
"github.com/ontio/ontology/txnpool"
tc "github.com/ontio/ontology/txnpool/common"
"github.com/ontio/ontology/validator/statefull"
"github.com/ontio/ontology/validator/stateless"
"github.com/urfave/cli"
)
const (
DefaultMultiCoreNum = 4
)
func init() {
log.Init(log.PATH, log.Stdout)
cmd.HelpUsage()
// Todo: If the actor bus uses a different log lib, remove it
var coreNum int
if config.Parameters.MultiCoreNum > DefaultMultiCoreNum {
coreNum = int(config.Parameters.MultiCoreNum)
} else {
coreNum = DefaultMultiCoreNum
}
log.Debug("The Core number is ", coreNum)
runtime.GOMAXPROCS(coreNum)
}
func setupAPP() *cli.App {
app := cli.NewApp()
app.Action = ontMain
app.Version = "0.6.0"
app.Copyright = "Copyright in 2018 The Ontology Authors"
app.Commands = []cli.Command{
cmd.WalletCommand,
cmd.InfoCommand,
cmd.AssetCommand,
cmd.SettingCommand,
cmd.ContractCommand,
}
startFlags := []cli.Flag{
utils.WalletUsedFlag,
utils.ConfigUsedFlag,
}
app.Flags = append(append(append(app.Flags, cmd.NodeFlags...), cmd.ContractFlags...), cmd.InfoFlags...)
app.Flags = append(app.Flags, startFlags...)
return app
}
func main() {
defer func() {
if p := recover(); p != nil {
if str, ok := p.(string); ok {
log.Warn("Leave gracefully. ", errors.New(str))
}
}
}()
if err := setupAPP().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func ontMain(ctx *cli.Context) {
var acct *account.Account
var err error
var noder protocol.Noder
log.Trace("Node version: ", config.Version)
consensusType := strings.ToLower(config.Parameters.ConsensusType)
if consensusType == "dbft" && len(config.Parameters.Bookkeepers) < account.DEFAULT_BOOKKEEPER_COUNT {
log.Fatal("With dbft consensus type, at least ", account.DEFAULT_BOOKKEEPER_COUNT, " Bookkeepers should be set in config.json")
os.Exit(1)
}
log.Info("0. Open the account")
client := account.GetClient(ctx)
if client == nil {
log.Fatal("Can't get local account.")
os.Exit(1)
}
acct = client.GetDefaultAccount()
if acct == nil {
log.Fatal("can not get default account")
os.Exit(1)
}
log.Debug("The Node's PublicKey ", acct.PublicKey)
defBookkeepers, err := client.GetBookkeepers()
sort.Sort(keypair.NewPublicList(defBookkeepers))
if err != nil {
log.Fatalf("GetBookkeepers error:%s", err)
os.Exit(1)
}
//Init event hub
events.Init()
log.Info("1. Loading the Ledger")
ledger.DefLedger, err = ledger.NewLedger()
if err != nil {
log.Fatalf("NewLedger error %s", err)
os.Exit(1)
}
err = ledger.DefLedger.Init(defBookkeepers)
if err != nil {
log.Fatalf("DefLedger.Init error %s", err)
os.Exit(1)
}
ldgerActor := ldgactor.NewLedgerActor()
ledgerPID := ldgerActor.Start()
log.Info("3. Start the transaction pool server")
// Start the transaction pool server
txPoolServer := txnpool.StartTxnPoolServer()
if txPoolServer == nil {
log.Fatalf("failed to start txn pool server")
os.Exit(1)
}
stlValidator, _ := stateless.NewValidator("stateless_validator")
stlValidator.Register(txPoolServer.GetPID(tc.VerifyRspActor))
stfValidator, _ := statefull.NewValidator("statefull_validator")
stfValidator.Register(txPoolServer.GetPID(tc.VerifyRspActor))
log.Info("4. Start the P2P networks")
net.SetLedgerPid(ledgerPID)
net.SetTxnPoolPid(txPoolServer.GetPID(tc.TxActor))
noder = net.StartProtocol(acct.PublicKey)
if err != nil {
log.Fatalf("Net StartProtocol error %s", err)
os.Exit(1)
}
p2pActor, err := net.InitNetServerActor(noder)
if err != nil {
log.Fatalf("Net InitNetServerActor error %s", err)
os.Exit(1)
}
txPoolServer.RegisterActor(tc.NetActor, p2pActor)
hserver.SetNetServerPid(p2pActor)
hserver.SetLedgerPid(ledgerPID)
hserver.SetTxnPoolPid(txPoolServer.GetPID(tc.TxPoolActor))
hserver.SetTxPid(txPoolServer.GetPID(tc.TxActor))
go restful.StartServer()
noder.SyncNodeHeight()
noder.WaitForPeersStart()
noder.WaitForSyncBlkFinish()
if protocol.SERVICE_NODE_NAME != config.Parameters.NodeType {
log.Info("5. Start Consensus Services")
pool := txPoolServer.GetPID(tc.TxPoolActor)
consensusService, _ := consensus.NewConsensusService(acct, pool, nil, p2pActor)
net.SetConsensusPid(consensusService.GetPID())
go consensusService.Start()
time.Sleep(5 * time.Second)
hserver.SetConsensusPid(consensusService.GetPID())
go localrpc.StartLocalServer()
}
log.Info("--Start the RPC interface")
go jsonrpc.StartRPCServer()
go websocket.StartServer()
if config.Parameters.HttpInfoPort > 0 {
go nodeinfo.StartServer(noder)
}
go logCurrBlockHeight()
//等待退出信号
waitToExit()
}
func logCurrBlockHeight() {
ticker := time.NewTicker(config.DEFAULT_GEN_BLOCK_TIME * time.Second)
for {
select {
case <-ticker.C:
log.Infof("BlockHeight = %d", ledger.DefLedger.GetCurrentBlockHeight())
isNeedNewFile := log.CheckIfNeedNewFile()
if isNeedNewFile {
log.ClosePrintLog()
log.Init(log.PATH, os.Stdout)
}
}
}
}
func waitToExit() {
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())
close(exit)
break
}
}()
<-exit
}