forked from ChainSafe/ChainBridge
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
239 lines (207 loc) · 6.26 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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
/*
Provides the command-line interface for the chainbridge application.
For configuration and CLI commands see the README: https://github.com/ChainSafe/ChainBridge.
*/
package main
import (
"errors"
"fmt"
"net/http"
"os"
"strconv"
"github.com/ChainSafe/ChainBridge/chains/ethereum"
"github.com/ChainSafe/ChainBridge/chains/substrate"
"github.com/ChainSafe/ChainBridge/config"
log "github.com/ChainSafe/log15"
"github.com/centrifuge/chainbridge-utils/core"
"github.com/centrifuge/chainbridge-utils/metrics/health"
metrics "github.com/centrifuge/chainbridge-utils/metrics/types"
"github.com/centrifuge/chainbridge-utils/msg"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)
var app = cli.NewApp()
var cliFlags = []cli.Flag{
config.ConfigFileFlag,
config.VerbosityFlag,
config.KeystorePathFlag,
config.BlockstorePathFlag,
config.FreshStartFlag,
config.LatestBlockFlag,
config.MetricsFlag,
config.MetricsPort,
}
var generateFlags = []cli.Flag{
config.PasswordFlag,
config.Sr25519Flag,
config.Secp256k1Flag,
config.SubkeyNetworkFlag,
}
var devFlags = []cli.Flag{
config.TestKeyFlag,
}
var importFlags = []cli.Flag{
config.EthereumImportFlag,
config.PrivateKeyFlag,
config.Sr25519Flag,
config.Secp256k1Flag,
config.PasswordFlag,
config.SubkeyNetworkFlag,
}
var accountCommand = cli.Command{
Name: "accounts",
Usage: "manage bridge keystore",
Description: "The accounts command is used to manage the bridge keystore.\n" +
"\tTo generate a new account (key type generated is determined on the flag passed in): chainbridge accounts generate\n" +
"\tTo import a keystore file: chainbridge accounts import path/to/file\n" +
"\tTo import a geth keystore file: chainbridge accounts import --ethereum path/to/file\n" +
"\tTo import a private key file: chainbridge accounts import --privateKey private_key\n" +
"\tTo list keys: chainbridge accounts list",
Subcommands: []*cli.Command{
{
Action: wrapHandler(handleGenerateCmd),
Name: "generate",
Usage: "generate bridge keystore, key type determined by flag",
Flags: generateFlags,
Description: "The generate subcommand is used to generate the bridge keystore.\n" +
"\tIf no options are specified, a secp256k1 key will be made.",
},
{
Action: wrapHandler(handleImportCmd),
Name: "import",
Usage: "import bridge keystore",
Flags: importFlags,
Description: "The import subcommand is used to import a keystore for the bridge.\n" +
"\tA path to the keystore must be provided\n" +
"\tUse --ethereum to import an ethereum keystore from external sources such as geth\n" +
"\tUse --privateKey to create a keystore from a provided private key.",
},
{
Action: wrapHandler(handleListCmd),
Name: "list",
Usage: "list bridge keystore",
Description: "The list subcommand is used to list all of the bridge keystores.\n",
},
},
}
var (
Version = "0.0.1"
)
// init initializes CLI
func init() {
app.Action = run
app.Copyright = "Copyright 2019 ChainSafe Systems Authors"
app.Name = "chainbridge"
app.Usage = "ChainBridge"
app.Authors = []*cli.Author{{Name: "ChainSafe Systems 2019"}}
app.Version = Version
app.EnableBashCompletion = true
app.Commands = []*cli.Command{
&accountCommand,
}
app.Flags = append(app.Flags, cliFlags...)
app.Flags = append(app.Flags, devFlags...)
}
func main() {
if err := app.Run(os.Args); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
func startLogger(ctx *cli.Context) error {
logger := log.Root()
handler := logger.GetHandler()
var lvl log.Lvl
if lvlToInt, err := strconv.Atoi(ctx.String(config.VerbosityFlag.Name)); err == nil {
lvl = log.Lvl(lvlToInt)
} else if lvl, err = log.LvlFromString(ctx.String(config.VerbosityFlag.Name)); err != nil {
return err
}
log.Root().SetHandler(log.LvlFilterHandler(lvl, handler))
return nil
}
func run(ctx *cli.Context) error {
err := startLogger(ctx)
if err != nil {
return err
}
log.Info("Starting ChainBridge...")
cfg, err := config.GetConfig(ctx)
if err != nil {
return err
}
// Check for test key flag
var ks string
var insecure bool
if key := ctx.String(config.TestKeyFlag.Name); key != "" {
ks = key
insecure = true
} else {
ks = cfg.KeystorePath
}
// Used to signal core shutdown due to fatal error
sysErr := make(chan error)
c := core.NewCore(sysErr)
for _, chain := range cfg.Chains {
chainId, errr := strconv.Atoi(chain.Id)
if errr != nil {
return errr
}
chainConfig := &core.ChainConfig{
Name: chain.Name,
Id: msg.ChainId(chainId),
Endpoint: chain.Endpoint,
From: chain.From,
KeystorePath: ks,
Insecure: insecure,
BlockstorePath: ctx.String(config.BlockstorePathFlag.Name),
FreshStart: ctx.Bool(config.FreshStartFlag.Name),
LatestBlock: ctx.Bool(config.LatestBlockFlag.Name),
Opts: chain.Opts,
}
var newChain core.Chain
var m *metrics.ChainMetrics
logger := log.Root().New("chain", chainConfig.Name)
if ctx.Bool(config.MetricsFlag.Name) {
m = metrics.NewChainMetrics(chain.Name)
}
if chain.Type == "ethereum" {
newChain, err = ethereum.InitializeChain(chainConfig, logger, sysErr, m)
} else if chain.Type == "substrate" {
newChain, err = substrate.InitializeChain(chainConfig, logger, sysErr, m)
} else {
return errors.New("unrecognized Chain Type")
}
if err != nil {
return err
}
c.AddChain(newChain)
}
// Start prometheus and health server
if ctx.Bool(config.MetricsFlag.Name) {
port := ctx.Int(config.MetricsPort.Name)
blockTimeoutStr := os.Getenv(config.HealthBlockTimeout)
blockTimeout := config.DefaultBlockTimeout
if blockTimeoutStr != "" {
blockTimeout, err = strconv.ParseInt(blockTimeoutStr, 10, 0)
if err != nil {
return err
}
}
h := health.NewHealthServer(port, c.Registry, int(blockTimeout))
go func() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/health", h.HealthStatus)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if errors.Is(err, http.ErrServerClosed) {
log.Info("Health status server is shutting down", err)
} else {
log.Error("Error serving metrics", "err", err)
}
}()
}
c.Start()
return nil
}