forked from polynetwork/heco-relayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (157 loc) · 4.77 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
/*
* Copyright (C) 2020 The poly network Authors
* This file is part of The poly network library.
*
* The poly network 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 poly network 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 poly network . If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/polynetwork/heco_relayer/cmd"
"github.com/polynetwork/heco_relayer/config"
"github.com/polynetwork/heco_relayer/db"
"github.com/polynetwork/heco_relayer/log"
"github.com/polynetwork/heco_relayer/manager"
sdk "github.com/polynetwork/poly-go-sdk"
"github.com/urfave/cli"
"os"
"os/signal"
"runtime"
"syscall"
)
var ConfigPath string
var LogDir string
var StartHeight uint64
var PolyStartHeight uint64
var StartForceHeight uint64
func setupApp() *cli.App {
app := cli.NewApp()
app.Usage = "Huobi eco chain relayer Service"
app.Action = startServer
app.Version = config.Version
app.Copyright = "Copyright in 2020 The poly network Authors"
app.Flags = []cli.Flag{
cmd.LogLevelFlag,
cmd.ConfigPathFlag,
cmd.HecoStartFlag,
cmd.HecoStartForceFlag,
cmd.PolyStartFlag,
cmd.LogDir,
}
app.Commands = []cli.Command{}
app.Before = func(context *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
return nil
}
return app
}
func startServer(ctx *cli.Context) {
// get all cmd flag
logLevel := ctx.GlobalInt(cmd.GetFlagName(cmd.LogLevelFlag))
ld := ctx.GlobalString(cmd.GetFlagName(cmd.LogDir))
log.InitLog(logLevel, ld, log.Stdout)
ConfigPath = ctx.GlobalString(cmd.GetFlagName(cmd.ConfigPathFlag))
hecoStart := ctx.GlobalUint64(cmd.GetFlagName(cmd.HecoStartFlag))
if hecoStart > 0 {
StartHeight = hecoStart
}
StartForceHeight = 0
hecoStartForce := ctx.GlobalUint64(cmd.GetFlagName(cmd.HecoStartForceFlag))
if hecoStartForce > 0 {
StartForceHeight = hecoStartForce
}
polyStart := ctx.GlobalUint64(cmd.GetFlagName(cmd.PolyStartFlag))
if polyStart > 0 {
PolyStartHeight = polyStart
}
// read config
servConfig := config.NewServiceConfig(ConfigPath)
if servConfig == nil {
log.Errorf("startServer - create config failed!")
return
}
// create poly sdk
polySdk := sdk.NewPolySdk()
err := setUpPoly(polySdk, servConfig.PolyConfig.RestURL)
if err != nil {
log.Errorf("startServer - failed to setup poly sdk: %v", err)
return
}
// create heco sdk
ethereumsdk, err := ethclient.Dial(servConfig.HecoConfig.RestURL)
if err != nil {
log.Errorf("startServer - cannot dial sync node, err: %s", err)
return
}
var boltDB *db.BoltDB
if servConfig.BoltDbPath == "" {
boltDB, err = db.NewBoltDB("boltdb")
} else {
boltDB, err = db.NewBoltDB(servConfig.BoltDbPath)
}
if err != nil {
log.Fatalf("db.NewWaitingDB error:%s", err)
return
}
initPolyServer(servConfig, polySdk, ethereumsdk, boltDB)
initHecoServer(servConfig, polySdk, ethereumsdk, boltDB)
waitToExit()
}
func setUpPoly(poly *sdk.PolySdk, RpcAddr string) error {
poly.NewRpcClient().SetAddress(RpcAddr)
hdr, err := poly.GetHeaderByHeight(0)
if err != nil {
return err
}
poly.SetChainId(hdr.ChainID)
return nil
}
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("waitToExit - Heco relayer received exit signal:%v.", sig.String())
close(exit)
break
}
}()
<-exit
}
func initHecoServer(servConfig *config.ServiceConfig, polysdk *sdk.PolySdk, ethereumsdk *ethclient.Client, boltDB *db.BoltDB) {
mgr, err := manager.NewHecoManager(servConfig, StartHeight, StartForceHeight, polysdk, ethereumsdk, boltDB)
if err != nil {
log.Error("initHecoServer - HecoServer start err: %s", err.Error())
return
}
go mgr.MonitorHecoChain()
go mgr.RegularlyTryCommitHecoLockProofToPoly()
go mgr.CheckDeposit()
}
func initPolyServer(servConfig *config.ServiceConfig, polysdk *sdk.PolySdk, ethereumsdk *ethclient.Client, boltDB *db.BoltDB) {
mgr, err := manager.NewPolyManager(servConfig, uint32(PolyStartHeight), polysdk, ethereumsdk, boltDB)
if err != nil {
log.Error("initPolyServer - PolyServer service start failed: %v", err)
return
}
go mgr.MonitorPolyChain()
}
func main() {
log.Infof("main - Heco relayer starting...")
if err := setupApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}