-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
149 lines (128 loc) · 3.98 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
package main
import (
"flag"
"fmt"
"github.com/binance-chain/go-sdk/common/types"
"github.com/ethereum/go-ethereum/common"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/bnb-chain/bsc-relayer/admin"
relayercommon "github.com/bnb-chain/bsc-relayer/common"
config "github.com/bnb-chain/bsc-relayer/config"
"github.com/bnb-chain/bsc-relayer/executor"
"github.com/bnb-chain/bsc-relayer/model"
"github.com/bnb-chain/bsc-relayer/relayer"
)
const (
flagConfigPath = "config-path"
flagConfigType = "config-type"
flagBBCNetworkType = "bbc-network-type"
flagConfigAwsRegion = "aws-region"
flagConfigAwsSecretKey = "aws-secret-key"
flagManager = "manager"
)
func initFlags() {
flag.String(flagConfigPath, "", "config file path")
flag.String(flagConfigType, "local_private_key", "config type, local_private_key or aws_private_key")
flag.Int(flagBBCNetworkType, int(types.TmpTestNetwork), "BNB Beacon Chain network type")
flag.String(flagConfigAwsRegion, "", "aws region")
flag.String(flagConfigAwsSecretKey, "", "aws secret key")
flag.String(flagManager, "", "manager")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
panic(err)
}
}
func printUsage() {
fmt.Print("usage: ./bsc-relayer --config-type local --config-path configFile\n")
fmt.Print("usage: ./bsc-relayer --config-type aws --aws-region awsRegin --aws-secret-key awsSecretKey\n")
}
func main() {
initFlags()
bbcNetworkType := viper.GetInt(flagBBCNetworkType)
if bbcNetworkType != int(types.TestNetwork) && bbcNetworkType != int(types.TmpTestNetwork) && bbcNetworkType != int(types.ProdNetwork) {
printUsage()
return
}
types.Network = types.ChainNetwork(bbcNetworkType)
configType := viper.GetString(flagConfigType)
if configType != config.AWSConfig && configType != config.LocalConfig {
printUsage()
return
}
var cfg *config.Config
if configType == config.AWSConfig {
awsSecretKey := viper.GetString(flagConfigAwsSecretKey)
if awsSecretKey == "" {
printUsage()
return
}
awsRegion := viper.GetString(flagConfigAwsRegion)
if awsRegion == "" {
printUsage()
return
}
configContent, err := config.GetSecret(awsSecretKey, awsRegion)
if err != nil {
fmt.Printf("get aws config error, err=%s", err.Error())
return
}
cfg = config.ParseConfigFromJson(configContent)
} else {
configFilePath := viper.GetString(flagConfigPath)
if configFilePath == "" {
printUsage()
return
}
cfg = config.ParseConfigFromFile(configFilePath)
}
if cfg == nil {
fmt.Println("failed to get configuration")
return
}
relayercommon.InitLogger(&cfg.LogConfig)
var db *gorm.DB
if cfg.DBConfig.DBPath != "" {
var err error
db, err = gorm.Open(cfg.DBConfig.Dialect, cfg.DBConfig.DBPath)
if err != nil {
panic(fmt.Sprintf("open db error, err=%s", err.Error()))
}
defer db.Close()
model.InitTables(db)
}
adm := admin.NewAdmin(db, cfg)
go adm.Serve()
bbcExecutor, err := executor.NewBBCExecutor(cfg, types.ChainNetwork(bbcNetworkType))
if err != nil {
relayercommon.Logger.Error(err.Error())
return
}
bscExecutor, err := executor.NewBSCExecutor(db, bbcExecutor, cfg)
if err != nil {
relayercommon.Logger.Error(err.Error())
return
}
abciInfo, err := bbcExecutor.GetClient().ABCIInfo()
if err != nil {
relayercommon.Logger.Error(err.Error())
return
}
startHeight := abciInfo.Response.LastBlockHeight - 1
block, err := bbcExecutor.GetClient().Block(&(startHeight))
if err != nil {
relayercommon.Logger.Error(err.Error())
return
}
curValidatorsHash := block.BlockMeta.Header.ValidatorsHash
manager := viper.GetString(flagManager)
relayerInstance := relayer.NewRelayer(db, cfg, bbcExecutor, bscExecutor, common.HexToAddress(manager))
relayercommon.Logger.Info("Starting relayer")
relayerInstance.Start(uint64(startHeight), curValidatorsHash)
select {}
}