forked from bottos-project/bottos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (192 loc) · 5.34 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
package main
import (
cactor "github.com/bottos-project/bottos/action/actor"
caapi "github.com/bottos-project/bottos/action/actor/api"
"github.com/bottos-project/bottos/action/actor/transaction"
actionenv "github.com/bottos-project/bottos/action/env"
"github.com/bottos-project/bottos/api"
"github.com/bottos-project/bottos/chain"
"github.com/bottos-project/bottos/chain/extra"
"github.com/bottos-project/bottos/cmd"
"github.com/bottos-project/bottos/config"
"github.com/bottos-project/bottos/contract"
"github.com/bottos-project/bottos/db"
"github.com/bottos-project/bottos/restful/handler"
"github.com/bottos-project/bottos/role"
"github.com/bottos-project/bottos/transaction"
"github.com/bottos-project/bottos/action/actor/transaction/trxprehandleactor"
log "github.com/cihub/seelog"
"github.com/micro/go-micro"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"fmt"
"gopkg.in/urfave/cli.v1"
"runtime"
"strconv"
"github.com/bottos-project/bottos/plugin/mongodb"
"github.com/bottos-project/bottos/common/types"
"github.com/AsynkronIT/protoactor-go/actor"
)
var (
app = cli.NewApp()
)
func init() {
app.Usage = "the bottos command line interface"
app.Version = "3.2.0"
app.Copyright = "Copyright 2017~2022 The Bottos Authors"
app.Flags = []cli.Flag{
cmd.ConfigFileFlag,
cmd.GenesisFileFlag,
cmd.DataDirFlag,
cmd.DisableRESTFlag,
cmd.RESTPortFlag,
cmd.RESTServerAddrFlag,
cmd.DisableRPCFlag,
cmd.RPCPortFlag,
cmd.P2PPortFlag,
cmd.P2PServerAddrFlag,
cmd.PeerListFlag,
cmd.DelegateSignkeyFlag,
cmd.DelegateFlag,
cmd.EnableStaleReportFlag,
cmd.EnableMongoDBFlag,
cmd.MongoDBFlag,
cmd.LogConfigFlag,
cmd.WalletDirFlag,
cmd.EnableWalletFlag,
}
app.Action = startBottos
app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
return nil
}
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func loadConfig(ctx *cli.Context) {
config.InitConfig()
if err := config.InitLogConfig(ctx); err != nil {
os.Exit(1)
}
if err := config.LoadConfig(ctx); err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
log.Infof("Bottos ChainID: %x", config.GetChainID())
}
func startBottos(ctx *cli.Context) error {
loadConfig(ctx)
blockDBPath := filepath.Join(config.Param.DataDir, "block/")
stateDBPath := filepath.Join(config.Param.DataDir, "state.db")
dbInst := db.NewDbService(blockDBPath, stateDBPath)
if dbInst == nil {
log.Critical("Create DB service fail")
os.Exit(1)
}
roleIntf := role.NewRole(dbInst)
var mdbActor *actor.PID = nil
if ctx.GlobalBool(cmd.EnableMongoDBFlag.Name) {
mdbActor = startMangoDB(roleIntf)
if mdbActor == nil {
log.Critical("Start MongoDB service fail")
os.Exit(1)
}
}
nc, err := contract.NewNativeContract(roleIntf)
if err != nil {
log.Critical("Create Native Contract error: ", err)
os.Exit(1)
}
chain, err := chain.CreateBlockChain(dbInst, roleIntf, nc)
if err != nil {
log.Critical("Create BlockChain error: ", err)
os.Exit(1)
}
if ctx.GlobalBool(cmd.EnableMongoDBFlag.Name) {
chain.RegisterHandledBlockCallback(func (block *types.Block) {
mdbActor.Tell(block)
})
}
if err := chain.Init(); err != nil {
log.Critical("Initialize BlockChain error: ", err)
os.Exit(1)
}
txStore := txstore.NewTransactionStore(chain, roleIntf)
actorenv := &actionenv.ActorEnv{
RoleIntf: roleIntf,
Chain: chain,
TxStore: txStore,
NcIntf: nc,
}
multiActors := cactor.InitActors(actorenv)
var trxPool = transaction.InitTrxPool(dbInst, roleIntf, nc, multiActors.GetNetActor())
trxactor.SetTrxPool(trxPool)
trxprehandleactor.SetTrxPool(trxPool)
trxprehandleactor.SetTrxActor(multiActors.GetTrxActor())
//start RESTful Api
if !ctx.GlobalBool(cmd.DisableRESTFlag.Name) {
go startRestApi(roleIntf)
}
//enable Rpc Api
if ctx.GlobalBool(cmd.EnableRPCFlag.Name) {
go startRPCService(actorenv)
}
WaitSystemDown(chain, multiActors)
return nil
}
//WaitSystemDown is to handle ctrl+C
func WaitSystemDown(chain chain.BlockChainInterface, actors *cactor.MultiActor) {
exit := make(chan bool, 0)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGKILL)
go func() {
for sig := range sigc {
actors.ActorsStop()
chain.Close()
log.Infof("System shutdown, signal: %v", sig.String())
log.Flush()
close(exit)
}
}()
<-exit
}
func startRestApi(roleIntf role.RoleInterface) {
router := handler.NewRouter()
//transfer to restful handler
handler.SetRoleIntf(roleIntf)
err := http.ListenAndServe(config.Param.RESTServAddr+":"+strconv.Itoa(config.Param.RESTPort), router)
if err != nil {
log.Critical("RESTful server fail: ", err)
os.Exit(1)
}
}
func startRPCService(actorenv *actionenv.ActorEnv) {
repo := caapi.NewApiService(actorenv)
service := micro.NewService(
micro.Name(config.Param.RpcServiceName),
micro.Version(config.Param.RpcServiceVersion),
)
api.RegisterChainHandler(service.Server(), repo)
if err := service.Run(); err != nil {
log.Critical("RPC server fail: ", err)
}
}
func startMangoDB(roleIntf role.RoleInterface) *actor.PID {
optiondb := db.NewOptionDbService(config.Param.OptionDb)
if optiondb == nil {
log.Errorf("Start optional db fail")
return nil
}
pid := mongodb.NewMdbActor(roleIntf, optiondb)
if pid == nil {
log.Errorf("Start mongodb fail")
}
return pid
}