-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (63 loc) · 2.09 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
package main
import (
"context"
"fmt"
"github.com/block-listener/conf"
service "github.com/block-listener/services"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
go chainService(conf.GetConfig().EthData)
go chainService(conf.GetConfig().BscData)
chainService(conf.GetConfig().CronosData)
}
func chainService(chainInfo conf.ChainData) {
fmt.Println("service started, chain ID: ", chainInfo.ChainId)
client, err := ethclient.Dial(chainInfo.WsRpc)
if err != nil {
chainService(chainInfo)
return
}
service.OpenTx(client, chainInfo.ChainId)
headers := make(chan *types.Header)
sub, err := client.SubscribeNewHead(context.Background(), headers)
if err != nil {
chainService(chainInfo)
return
}
fmt.Println("ChainId:", chainInfo.ChainId)
lastConfimedBlock, err := service.GetBlockInfobyChainId(chainInfo.ChainId)
if err != nil {
chainService(chainInfo)
return
}
firstRun := true
ctx := context.Background()
for {
select {
case err := <-sub.Err():
fmt.Println("restarting service of chain id", chainInfo.ChainId, "and error is:", err)
chainService(chainInfo)
case header := <-headers:
block, err := client.BlockByNumber(context.Background(), header.Number)
if err != nil {
fmt.Println("error in GetBlockByNumber:", err)
continue
}
// fmt.Println("New block :", block.Number().Uint64())
if firstRun && lastConfimedBlock != nil {
syncStartNum := uint64(lastConfimedBlock.Backupsyncnum)
if lastConfimedBlock.Syncstatus == 1 {
syncStartNum = uint64(lastConfimedBlock.Blocksyncnum)
}
go service.SyncBlocks(syncStartNum, block.Number().Uint64(), chainInfo.ContractAddress, chainInfo.WsRpc, chainInfo.ChainId, client)
} else if firstRun && lastConfimedBlock == nil {
syncStartNum := chainInfo.BackupSyncnum
go service.SyncBlocks(syncStartNum, block.Number().Uint64(), chainInfo.ContractAddress, chainInfo.WsRpc, chainInfo.ChainId, client)
}
go service.FindTx(ctx, header.Number, false, chainInfo.ContractAddress, chainInfo.ChainId, client)
firstRun = false
}
}
}