-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
356 lines (302 loc) · 8.99 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"context"
"flag"
"fmt"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/ethclient"
oksdk "github.com/okex/exchain-go-sdk"
"github.com/oklog/run"
poly_bridge_sdk "github.com/polynetwork/poly-bridge/bridgesdk"
sdk "github.com/polynetwork/poly-go-sdk"
"github.com/zhiqiangxu/relay-patch/config"
"github.com/zhiqiangxu/relay-patch/pkg/log"
"github.com/zhiqiangxu/relay-patch/pkg/relay"
"github.com/zhiqiangxu/relay-patch/pkg/storage"
"github.com/zhiqiangxu/relay-patch/pkg/tools"
txPkg "github.com/zhiqiangxu/relay-patch/pkg/tx"
)
var confFile string
var tx string
var chain uint64
var force bool
var price string
var print bool
var mr bool
func init() {
flag.StringVar(&confFile, "conf", "./config.json", "configuration file path")
flag.StringVar(&tx, "tx", "", "specify tx hash")
flag.Uint64Var(&chain, "chain", 0, "specify chain ID")
flag.BoolVar(&force, "force", false, "force transaction")
flag.StringVar(&price, "price", "", "gas price")
flag.BoolVar(&print, "print", false, "print merkle value")
flag.BoolVar(&mr, "mr", false, "check merkle root")
flag.Parse()
}
func setUpEthClientAndKeyStore(ethConfig *config.EthConfig) (clients []*ethclient.Client, tmClients []*oksdk.Client, ks *tools.EthKeyStore) {
for _, node := range ethConfig.RestURL {
client, err := ethclient.Dial(node)
if err != nil {
log.Fatalf("ethclient.Dial failed:%v", err)
}
clients = append(clients, client)
}
for _, tmNode := range ethConfig.TMRestURL {
config, _ := oksdk.NewClientConfig(tmNode, "okexchain-65", oksdk.BroadcastBlock, "0.01okt", 200000, 0, "")
client := oksdk.NewClient(config)
tmClients = append(tmClients, &client)
}
start := time.Now()
chainID, err := clients[0].ChainID(context.Background())
if err != nil {
log.Fatalf("clients[0].ChainID failed:%v", err)
}
log.Infof("SideChain %d ChainID() took %v", ethConfig.SideChainId, time.Now().Sub(start).String())
ks = tools.NewEthKeyStore(ethConfig.KeyStorePath, ethConfig.KeyStorePwdSet, chainID)
return
}
func setUpEthToPoly(ethToPolyCh chan string, polySdk *sdk.PolySdk,
signer *sdk.Account,
clients []*ethclient.Client,
tmClients []*oksdk.Client,
ethConfig *config.EthConfig,
conf *config.Config) []*relay.EthToPoly {
var workers []*relay.EthToPoly
for i := 0; i < 5; i++ {
workers = append(workers, relay.NewEthToPoly(ethToPolyCh, polySdk, signer, clients, tmClients, ethConfig, conf))
}
return workers
}
func setUpPolyToEth(clients []*ethclient.Client, ks *tools.EthKeyStore, polyToEthWorkCh chan string, polySdk *sdk.PolySdk, bridgeSdk *poly_bridge_sdk.BridgeSdk, ethConfig *config.EthConfig, polyConfig *config.PolyConfig, conf *config.Config) []*relay.PolyToEth {
var workers []*relay.PolyToEth
for _, account := range ks.GetAccounts() {
worker := relay.NewPolyToEth(polyToEthWorkCh, polySdk, bridgeSdk, clients, ethConfig, polyConfig, conf, account, ks)
workers = append(workers, worker)
}
return workers
}
func idToEthConf(id uint64, conf *config.Config) *config.EthConfig {
if id == 0 {
return nil
}
switch id {
case conf.BSCConfig.SideChainId:
return &conf.BSCConfig
case conf.HecoConfig.SideChainId:
return &conf.HecoConfig
case conf.CurveConfig.SideChainId:
return &conf.CurveConfig
case conf.EthConfig.SideChainId:
return &conf.EthConfig
case conf.OKConfig.SideChainId:
return &conf.OKConfig
case conf.BorConfig.SideChainId:
return &conf.BorConfig
case conf.ArbConfig.SideChainId:
return &conf.ArbConfig
default:
panic(fmt.Sprintf("unkown chain id:%d", id))
}
}
func main() {
log.InitLog(log.InfoLog, "./Log/", log.Stdout)
conf, err := config.LoadConfig(confFile)
if err != nil {
log.Fatalf("LoadConfig fail:%v", err)
}
if force {
conf.Force = true
}
if print {
conf.Print = true
}
if mr {
conf.CheckMerkleRoot = true
}
if price != "" {
gasPrice := big.NewInt(0)
gasPrice, ok := gasPrice.SetString(price, 10)
if !ok {
log.Fatalf("invalid gas price:%v", price)
}
conf.GasPrice = gasPrice
}
// {
// confBytes, _ := json.MarshalIndent(conf, "", " ")
// fmt.Println("conf", string(confBytes))
// }
polySdk := sdk.NewPolySdk()
err = setUpPoly(polySdk, conf.PolyConfig.RestURL)
if err != nil {
log.Fatalf("setUpPoly failed: %v", err)
}
wallet, err := polySdk.OpenWallet(conf.PolyConfig.WalletFile)
if err != nil {
log.Fatalf("polySdk.OpenWallet failed: %v", err)
}
signer, err := wallet.GetDefaultAccount([]byte(conf.PolyConfig.WalletPwd))
if err != nil {
log.Fatalf("wallet.GetDefaultAccount failed: %v", err)
}
chainIDs := []uint64{
conf.BSCConfig.SideChainId,
conf.HecoConfig.SideChainId,
conf.CurveConfig.SideChainId,
conf.EthConfig.SideChainId,
conf.OKConfig.SideChainId,
conf.BorConfig.SideChainId,
conf.ArbConfig.SideChainId,
}
ethToPolyChs := make(map[uint64]chan string)
polyToEthChs := make(map[uint64]chan string)
eth2PolyWorkers := make(map[uint64][]*relay.EthToPoly)
polyToEthWorkers := make(map[uint64][]*relay.PolyToEth)
bridgeSdk := poly_bridge_sdk.NewBridgeSdk(conf.BridgeConfig.RestURL[0][0])
for _, chainID := range chainIDs {
polyToEthChs[chainID] = make(chan string)
ethConf := idToEthConf(chainID, conf)
if ethConf == nil {
continue
}
if !ethConf.OnlyDst {
ethToPolyChs[chainID] = make(chan string)
}
clients, tmClients, ks := setUpEthClientAndKeyStore(ethConf)
if ethToPolyChs[chainID] != nil {
eth2PolyWorkers[chainID] = setUpEthToPoly(ethToPolyChs[chainID], polySdk, signer, clients, tmClients, ethConf, conf)
}
polyToEthWorkers[chainID] = setUpPolyToEth(clients, ks, polyToEthChs[chainID], polySdk, bridgeSdk, ethConf, &conf.PolyConfig, conf)
}
if tx != "" {
txes := strings.Split(tx, ",")
for _, txHash := range txes {
var (
targetWorkersForEthToPoly []*relay.EthToPoly
targetWorkersForPolyToEth []*relay.PolyToEth
toChainID uint64
polyTxHash string
)
switch chain {
case 0:
// handle poly tx hash
merkleValue, _, _ := relay.GetKeyParams(polySdk, &conf.PolyConfig, txHash, 0)
if merkleValue == nil {
log.Errorf("merkle value empty for poly hash %s", tx)
}
toChainID = merkleValue.MakeTxParam.ToChainID
polyTxHash = txHash
goto HANDLE_POLY_TX
default:
targetWorkersForEthToPoly = eth2PolyWorkers[chain]
if len(targetWorkersForEthToPoly) == 0 {
log.Fatalf("eth2poly unsupported chainID:%d", chain)
}
}
toChainID, polyTxHash = targetWorkersForEthToPoly[0].MonitorTx(txHash)
if polyTxHash == "" {
return
}
HANDLE_POLY_TX:
log.Infof("toChainID:%d poly hash:%s", toChainID, polyTxHash)
polyTxHeight, err := polySdk.GetBlockHeightByTxHash(polyTxHash)
if err != nil {
log.Warnf("polySdk.GetBlockHeightByTxHash failed:%v", err)
polyTxHeight, err = polySdk.GetCurrentBlockHeight()
if err != nil {
log.Fatalf("poly2eth calc polyTxHeight by GetCurrentBlockHeight failed:%v", err)
}
}
waitPolyHeight(polySdk, polyTxHeight+1)
targetWorkersForPolyToEth = polyToEthWorkers[toChainID]
if len(targetWorkersForPolyToEth) == 0 {
log.Fatalf("poly2eth unsupported chainID:%d", toChainID)
}
targetWorkersForPolyToEth[0].SendTx(polyTxHash)
}
return
}
go func() {
timer := time.NewTimer(time.Minute * 15)
<-timer.C
log.Fatal("max time exceeded, auto restart")
}()
mysql, err := storage.NewMySQL(conf.MySQLConfig)
if err != nil {
log.Fatalf("storage.NewMySQL failed:%v", err)
}
filter := txPkg.NewFilter(mysql, ethToPolyChs, polyToEthChs, conf)
var g run.Group
g.Add(func() error {
filter.Start()
return nil
}, func(error) {
filter.Stop()
})
var toPolyWorkers []*relay.EthToPoly
for _, worker := range eth2PolyWorkers {
toPolyWorkers = append(toPolyWorkers, worker...)
}
for i := range toPolyWorkers {
worker := toPolyWorkers[i]
g.Add(func() error {
worker.Start()
return nil
}, func(error) {
worker.Stop()
})
}
var fromPolyWorkers []*relay.PolyToEth
for _, worker := range polyToEthWorkers {
fromPolyWorkers = append(fromPolyWorkers, worker...)
}
for i := range fromPolyWorkers {
worker := fromPolyWorkers[i]
g.Add(func() error {
worker.Start()
return nil
}, func(error) {
worker.Stop()
})
}
go func() {
err := g.Run()
log.Fatalf("run.Group finished:%v", err)
for _, workCh := range ethToPolyChs {
close(workCh)
}
for _, workCh := range polyToEthChs {
close(workCh)
}
}()
for _, workCh := range ethToPolyChs {
<-workCh
}
for _, workCh := range polyToEthChs {
<-workCh
}
return
}
func setUpPoly(polySdk *sdk.PolySdk, rpcAddr string) error {
polySdk.NewRpcClient().SetAddress(rpcAddr)
hdr, err := polySdk.GetHeaderByHeight(0)
if err != nil {
return err
}
polySdk.SetChainId(hdr.ChainID)
return nil
}
func waitPolyHeight(polySdk *sdk.PolySdk, height uint32) {
for {
currentHeight, err := polySdk.GetCurrentBlockHeight()
if err != nil {
log.Fatalf("polySdk.GetCurrentBlockHeight failed:%v", err)
}
if currentHeight >= height {
break
}
log.Infof("wait poly height:%d, current height:%d", height, currentHeight)
time.Sleep(time.Second)
}
}