-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (141 loc) · 4.02 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
package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/tendermint/tendermint/rpc/client/http"
"github.com/terry108/eth-relayer/cmd"
"github.com/terry108/eth-relayer/config"
"github.com/terry108/eth-relayer/db"
"github.com/terry108/eth-relayer/log"
"github.com/terry108/eth-relayer/manager"
"github.com/urfave/cli"
)
var ConfigPath string
var LogDir string
var StartHeight uint64
var BridgeStartHeight uint64
var StartForceHeight uint64
func setupApp() *cli.App {
app := cli.NewApp()
app.Usage = "ETH relayer Service"
app.Action = startServer
app.Version = config.Version
app.Copyright = "Copyright in 2021 The Pi Bridge Authors"
app.Flags = []cli.Flag{
cmd.LogLevelFlag,
cmd.ConfigPathFlag,
cmd.EthStartFlag,
cmd.EthStartForceFlag,
// 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))
ethstart := ctx.GlobalUint64(cmd.GetFlagName(cmd.EthStartFlag))
if ethstart > 0 {
StartHeight = ethstart
}
StartForceHeight = 0
ethstartforce := ctx.GlobalUint64(cmd.GetFlagName(cmd.EthStartForceFlag))
if ethstartforce > 0 {
StartForceHeight = ethstartforce
}
bridgeStart := ctx.GlobalUint64(cmd.GetFlagName(cmd.BridgeStartFlag))
if bridgeStart > 0 {
BridgeStartHeight = bridgeStart
}
// read config
servConfig := config.NewServiceConfig(ConfigPath)
if servConfig == nil {
log.Errorf("startServer - create config failed!")
return
}
// create pi bridge sdk
bridgeSdk, err := http.New(servConfig.BridgeConfig.BridgeRpcAddr, "/websocket")
if err != nil {
log.Errorf("startServer - failed to new Tendermint Cli: %v", err)
return
}
// create ethereum sdk
ethereumSdk, err := ethclient.Dial(servConfig.ETHConfig.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
}
initBridgeServer(servConfig, bridgeSdk, ethereumSdk, boltDB)
initETHServer(servConfig, bridgeSdk, 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)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for sig := range sc {
log.Infof("waitToExit - ETH relayer received exit signal:%v.", sig.String())
close(exit)
break
}
}()
<-exit
}
func initETHServer(servConfig *config.ServiceConfig, bridgeSDK *http.HTTP, ethereumsdk *ethclient.Client, boltDB *db.BoltDB) {
mgr, err := manager.NewEthereumManager(servConfig, StartHeight, StartForceHeight, bridgeSDK, ethereumsdk, boltDB)
if err != nil {
log.Error("initETHServer - eth service start err: %s", err.Error())
return
}
_ = mgr
go mgr.MonitorChain()
go mgr.MonitorDeposit()
go mgr.CheckDeposit()
}
func initBridgeServer(servConfig *config.ServiceConfig, bridgeSDK *http.HTTP, ethereumsdk *ethclient.Client, boltDB *db.BoltDB) {
mgr, err := manager.NewBridgeManager(servConfig, BridgeStartHeight, bridgeSDK, ethereumsdk, boltDB)
if err != nil {
log.Error("initPolyServer - PolyServer service start failed: %v", err)
return
}
go mgr.MonitorChain()
}
func main() {
log.Infof("main - ETH relayer starting...")
if err := setupApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}