forked from bnb-chain/oracle-relayer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (115 loc) · 3.44 KB
/
Copy pathmain.go
File metadata and controls
137 lines (115 loc) · 3.44 KB
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
package main
import (
"flag"
"fmt"
"github.com/binance-chain/go-sdk/common/types"
"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/binance-chain/oracle-relayer/admin"
"github.com/binance-chain/oracle-relayer/executor/bbc"
"github.com/binance-chain/oracle-relayer/executor/bsc"
"github.com/binance-chain/oracle-relayer/model"
"github.com/binance-chain/oracle-relayer/observer"
"github.com/binance-chain/oracle-relayer/relayer"
"github.com/binance-chain/oracle-relayer/util"
)
const (
flagConfigType = "config-type"
flagConfigAwsRegion = "aws-region"
flagConfigAwsSecretKey = "aws-secret-key"
flagConfigPath = "config-path"
flagBBCNetwork = "bbc-network"
)
const (
ConfigTypeLocal = "local"
ConfigTypeAws = "aws"
)
func initFlags() {
flag.String(flagConfigPath, "", "config path")
flag.String(flagConfigType, "", "config type, local or aws")
flag.String(flagConfigAwsRegion, "", "aws s3 region")
flag.String(flagConfigAwsSecretKey, "", "aws s3 secret key")
flag.Int(flagBBCNetwork, int(types.TestNetwork), "bbc chain network type")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
panic(fmt.Sprintf("bind flags error, err=%s", err))
}
}
func printUsage() {
fmt.Print("usage: ./relayer --bbc-network [0 for testnet, 1 for mainnet] --config-type [local or aws] --config-path config_file_path\n")
}
func main() {
initFlags()
bbcNetwork := viper.GetInt(flagBBCNetwork)
if bbcNetwork != int(types.TestNetwork) &&
bbcNetwork != int(types.ProdNetwork) &&
bbcNetwork != int(types.TmpTestNetwork) &&
bbcNetwork != int(types.GangesNetwork) {
printUsage()
return
}
types.Network = types.ChainNetwork(bbcNetwork)
configType := viper.GetString(flagConfigType)
if configType == "" {
printUsage()
return
}
if configType != ConfigTypeAws && configType != ConfigTypeLocal {
printUsage()
return
}
var config *util.Config
if configType == ConfigTypeAws {
awsSecretKey := viper.GetString(flagConfigAwsSecretKey)
if awsSecretKey == "" {
printUsage()
return
}
awsRegion := viper.GetString(flagConfigAwsRegion)
if awsRegion == "" {
printUsage()
return
}
configContent, err := util.GetSecret(awsSecretKey, awsRegion)
if err != nil {
fmt.Printf("get aws config error, err=%s", err.Error())
return
}
config = util.ParseConfigFromJson(configContent)
} else {
configFilePath := viper.GetString(flagConfigPath)
if configFilePath == "" {
printUsage()
return
}
config = util.ParseConfigFromFile(configFilePath)
}
config.Validate()
// init logger
util.InitLogger(*config.LogConfig)
util.InitAlert(config.AlertConfig)
db, err := gorm.Open(config.DBConfig.Dialect, config.DBConfig.DBPath)
if err != nil {
panic(fmt.Sprintf("open db error, err=%s", err.Error()))
}
defer db.Close()
model.InitTables(db)
bscExecutor := bsc.NewExecutor(config.ChainConfig.BSCProviders, config)
ob := observer.NewObserver(db, config, bscExecutor)
go ob.Start()
bbcExecutor, err := bbc.NewExecutor(config.ChainConfig.BBCRpcAddrs, types.Network, config)
if err != nil {
fmt.Printf("new bbc executor error, err=%s\n", err.Error())
return
}
oracleRelayer := relayer.NewRelayer(db, bbcExecutor, config)
go oracleRelayer.Main()
adm := admin.NewAdmin(config, bbcExecutor)
go adm.Serve()
select {}
}