forked from ChainSafe/gossamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
438 lines (374 loc) · 13 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package main
import (
"errors"
"fmt"
"os"
"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/urfave/cli"
)
const (
accountCommandName = "account"
exportCommandName = "export"
initCommandName = "init"
buildSpecCommandName = "build-spec"
importRuntimeCommandName = "import-runtime"
importStateCommandName = "import-state"
pruningStateCommandName = "prune-state"
)
// app is the cli application
var app = cli.NewApp()
var logger log.LeveledLogger = log.NewFromGlobal(log.AddContext("pkg", "cmd"))
var (
// exportCommand defines the "export" subcommand (ie, `gossamer export`)
exportCommand = cli.Command{
Action: FixFlagOrder(exportAction),
Name: exportCommandName,
Usage: "Export configuration values to TOML configuration file",
ArgsUsage: "",
Flags: ExportFlags,
Category: "EXPORT",
Description: "The export command exports configuration values from the command flags to a TOML configuration file.\n" +
"\tUsage: gossamer export --config chain/test/config.toml --basepath ~/.gossamer/test",
}
// initCommand defines the "init" subcommand (ie, `gossamer init`)
initCommand = cli.Command{
Action: FixFlagOrder(initAction),
Name: initCommandName,
Usage: "Initialise node databases and load genesis data to state",
ArgsUsage: "",
Flags: InitFlags,
Category: "INIT",
Description: "The init command initialises the node databases and loads the genesis data from the genesis file to state.\n" +
"\tUsage: gossamer init --genesis genesis.json",
}
// accountCommand defines the "account" subcommand (ie, `gossamer account`)
accountCommand = cli.Command{
Action: FixFlagOrder(accountAction),
Name: accountCommandName,
Usage: "Create and manage node keystore accounts",
Flags: AccountFlags,
Category: "ACCOUNT",
Description: "The account command is used to manage the gossamer keystore.\n" +
"\tTo generate a new sr25519 account: gossamer account --generate\n" +
"\tTo generate a new ed25519 account: gossamer account --generate --ed25519\n" +
"\tTo generate a new secp256k1 account: gossamer account --generate --secp256k1\n" +
"\tTo import a keystore file: gossamer account --import=path/to/file\n" +
"\tTo list keys: gossamer account --list",
}
// buildSpecCommand creates a raw genesis file from a human readable genesis file.
buildSpecCommand = cli.Command{
Action: FixFlagOrder(buildSpecAction),
Name: buildSpecCommandName,
Usage: "Generates genesis JSON data, and can convert to raw genesis data",
ArgsUsage: "",
Flags: BuildSpecFlags,
Category: "BUILD-SPEC",
Description: "The build-spec command outputs current genesis JSON data.\n" +
"\tUsage: gossamer build-spec\n" +
"\tTo generate raw genesis file from default: gossamer build-spec --raw > genesis.json" +
"\tTo generate raw genesis file from specific genesis file: gossamer build-spec --raw --genesis genesis-spec.json > genesis.json",
}
// importRuntime generates a genesis file given a .wasm runtime binary.
importRuntimeCommand = cli.Command{
Action: FixFlagOrder(importRuntimeAction),
Name: importRuntimeCommandName,
Usage: "Generates a genesis file given a .wasm runtime binary",
ArgsUsage: "",
Flags: RootFlags,
Category: "IMPORT-RUNTIME",
Description: "The import-runtime command generates a genesis file given a .wasm runtime binary.\n" +
"\tUsage: gossamer import-runtime runtime.wasm > genesis.json\n",
}
importStateCommand = cli.Command{
Action: FixFlagOrder(importStateAction),
Name: importStateCommandName,
Usage: "Import state from a JSON file and set it as the chain head state",
ArgsUsage: "",
Flags: ImportStateFlags,
Category: "IMPORT-STATE",
Description: "The import-state command allows a JSON file containing a given state in the form of key-value pairs to be imported.\n" +
"Input can be generated by using the RPC function state_getPairs.\n" +
"\tUsage: gossamer import-state --state state.json --header header.json --first-slot <first slot of network>\n",
}
pruningCommand = cli.Command{
Action: FixFlagOrder(pruneState),
Name: pruningStateCommandName,
Usage: "Prune state will prune the state trie",
ArgsUsage: "",
Flags: PruningFlags,
Description: `prune-state <retain-blocks> will prune historical state data.
All trie nodes that do not belong to the specified version state will be deleted from the database.
The default pruning target is the HEAD-256 state`,
}
)
// init initialises the cli application
func init() {
app.Action = gossamerAction
app.Copyright = "Copyright 2019 ChainSafe Systems Authors"
app.Name = "gossamer"
app.Usage = "Official gossamer command-line interface"
app.Author = "ChainSafe Systems 2019"
app.Version = "0.3.2"
app.Commands = []cli.Command{
exportCommand,
initCommand,
accountCommand,
buildSpecCommand,
importRuntimeCommand,
importStateCommand,
pruningCommand,
}
app.Flags = RootFlags
}
// main runs the cli application
func main() {
if err := app.Run(os.Args); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func importStateAction(ctx *cli.Context) error {
var (
stateFP, headerFP string
firstSlot int
)
if stateFP = ctx.String(StateFlag.Name); stateFP == "" {
return errors.New("must provide argument to --state")
}
if headerFP = ctx.String(HeaderFlag.Name); headerFP == "" {
return errors.New("must provide argument to --header")
}
if firstSlot = ctx.Int(FirstSlotFlag.Name); firstSlot == 0 {
return errors.New("must provide argument to --first-slot")
}
cfg, err := createImportStateConfig(ctx)
if err != nil {
logger.Errorf("failed to create node configuration: %s", err)
return err
}
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)
return dot.ImportState(cfg.Global.BasePath, stateFP, headerFP, uint64(firstSlot))
}
// importRuntimeAction generates a genesis file given a .wasm runtime binary.
func importRuntimeAction(ctx *cli.Context) error {
arguments := ctx.Args()
if len(arguments) == 0 {
return fmt.Errorf("no args provided, please provide wasm file")
}
fp := arguments[0]
out, err := createGenesisWithRuntime(fp)
if err != nil {
return err
}
fmt.Println(out)
return nil
}
// gossamerAction is the root action for the gossamer command, creates a node
// configuration, loads the keystore, initialises the node if not initialised,
// then creates and starts the node and node services
func gossamerAction(ctx *cli.Context) error {
// check for unknown command arguments
if arguments := ctx.Args(); len(arguments) > 0 {
return fmt.Errorf("failed to read command argument: %q", arguments[0])
}
// setup gossamer logger
lvl, err := setupLogger(ctx)
if err != nil {
logger.Errorf("failed to setup logger: %s", err)
return err
}
// create new dot configuration (the dot configuration is created within the
// cli application from the flag values provided)
cfg, err := createDotConfig(ctx)
if err != nil {
logger.Errorf("failed to create node configuration: %s", err)
return err
}
cfg.Global.LogLvl = lvl
// expand data directory and update node configuration (performed separately
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)
if !dot.NodeInitialized(cfg.Global.BasePath) {
// initialise node (initialise state database and load genesis data)
err = dot.InitNode(cfg)
if err != nil {
logger.Errorf("failed to initialise node: %s", err)
return err
}
}
// ensure configuration matches genesis data stored during node initialization
// but do not overwrite configuration if the corresponding flag value is set
err = updateDotConfigFromGenesisData(ctx, cfg)
if err != nil {
logger.Errorf("failed to update config from genesis data: %s", err)
return err
}
ks := keystore.NewGlobalKeystore()
// load built-in test keys if specified by `cfg.Account.Key`
err = keystore.LoadKeystore(cfg.Account.Key, ks.Acco)
if err != nil {
logger.Errorf("failed to load account keystore: %s", err)
return err
}
err = keystore.LoadKeystore(cfg.Account.Key, ks.Babe)
if err != nil {
logger.Errorf("failed to load BABE keystore: %s", err)
return err
}
err = keystore.LoadKeystore(cfg.Account.Key, ks.Gran)
if err != nil {
logger.Errorf("failed to load grandpa keystore: %s", err)
return err
}
// load user keys if specified
err = unlockKeystore(ks.Acco, cfg.Global.BasePath, cfg.Account.Unlock, ctx.String(PasswordFlag.Name))
if err != nil {
logger.Errorf("failed to unlock keystore: %s", err)
return err
}
err = unlockKeystore(ks.Babe, cfg.Global.BasePath, cfg.Account.Unlock, ctx.String(PasswordFlag.Name))
if err != nil {
logger.Errorf("failed to unlock keystore: %s", err)
return err
}
err = unlockKeystore(ks.Gran, cfg.Global.BasePath, cfg.Account.Unlock, ctx.String(PasswordFlag.Name))
if err != nil {
logger.Errorf("failed to unlock keystore: %s", err)
return err
}
node, err := dot.NewNode(cfg, ks)
if err != nil {
logger.Errorf("failed to create node services: %s", err)
return err
}
logger.Info("starting node " + node.Name + "...")
// start node
err = node.Start()
if err != nil {
return err
}
return nil
}
// initAction is the action for the "init" subcommand, initialises the trie and
// state databases and loads initial state from the configured genesis file
func initAction(ctx *cli.Context) error {
lvl, err := setupLogger(ctx)
if err != nil {
logger.Errorf("failed to setup logger: %s", err)
return err
}
cfg, err := createInitConfig(ctx)
if err != nil {
logger.Errorf("failed to create node configuration: %s", err)
return err
}
cfg.Global.LogLvl = lvl
// expand data directory and update node configuration (performed separately
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)
// check if node has been initialised (expected false - no warning log)
if dot.NodeInitialized(cfg.Global.BasePath) {
// use --force value to force initialise the node
force := ctx.Bool(ForceFlag.Name)
// prompt user to confirm reinitialization
if force || confirmMessage("Are you sure you want to reinitialise the node? [Y/n]") {
logger.Info("reinitialising node at base path " + cfg.Global.BasePath + "...")
} else {
logger.Warn("exiting without reinitialising the node at base path " + cfg.Global.BasePath + "...")
return nil // exit if reinitialization is not confirmed
}
}
// initialise node (initialise state database and load genesis data)
err = dot.InitNode(cfg)
if err != nil {
logger.Errorf("failed to initialise node: %s", err)
return err
}
return nil
}
func buildSpecAction(ctx *cli.Context) error {
// set logger to critical, so output only contains genesis data
err := ctx.Set("log", "crit")
if err != nil {
return err
}
_, err = setupLogger(ctx)
if err != nil {
return err
}
var bs *dot.BuildSpec
if genesis := ctx.String(GenesisSpecFlag.Name); genesis != "" {
bspec, e := dot.BuildFromGenesis(genesis, 0)
if e != nil {
return e
}
bs = bspec
} else {
cfg, e := createBuildSpecConfig(ctx)
if e != nil {
return e
}
// expand data directory and update node configuration (performed separately
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)
bspec, e := dot.BuildFromDB(cfg.Global.BasePath)
if e != nil {
return fmt.Errorf("error building spec from database, init must be run before build-spec or run build-spec with --genesis flag Error %s", e)
}
bs = bspec
}
if bs == nil {
return fmt.Errorf("error building genesis")
}
var res []byte
if ctx.Bool(RawFlag.Name) {
res, err = bs.ToJSONRaw()
} else {
res, err = bs.ToJSON()
}
if err != nil {
return err
}
if outputPath := ctx.String(OutputSpecFlag.Name); outputPath != "" {
if err = dot.WriteGenesisSpecFile(res, outputPath); err != nil {
return err
}
} else {
fmt.Printf("%s", res)
}
return nil
}
func pruneState(ctx *cli.Context) error {
tomlCfg, _, err := setupConfigFromChain(ctx)
if err != nil {
logger.Errorf("failed to load chain configuration: %s", err)
return err
}
inputDBPath := tomlCfg.Global.BasePath
prunedDBPath := ctx.GlobalString(DBPathFlag.Name)
if prunedDBPath == "" {
return fmt.Errorf("path not specified for badger db")
}
bloomSize := ctx.GlobalUint64(BloomFilterSizeFlag.Name)
retainBlocks := ctx.GlobalInt64(RetainBlockNumberFlag.Name)
pruner, err := state.NewOfflinePruner(inputDBPath, prunedDBPath, bloomSize, retainBlocks)
if err != nil {
return err
}
logger.Info("Offline pruner initialised")
err = pruner.SetBloomFilter()
if err != nil {
return fmt.Errorf("failed to set keys into bloom filter: %w", err)
}
err = pruner.Prune()
if err != nil {
return fmt.Errorf("failed to prune: %w", err)
}
return nil
}