forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_verify.go
79 lines (65 loc) · 1.78 KB
/
genesis_verify.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
)
var errInvalidGenesisState = errors.New("invalid genesis state type")
type verifyCfg struct {
commonCfg
}
// newVerifyCmd creates the genesis verify subcommand
func newVerifyCmd(io commands.IO) *commands.Command {
cfg := &verifyCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "verify",
ShortUsage: "verify [flags]",
ShortHelp: "verifies a genesis.json",
LongHelp: "Verifies a node's genesis.json",
},
cfg,
func(_ context.Context, _ []string) error {
return execVerify(cfg, io)
},
)
}
func (c *verifyCfg) RegisterFlags(fs *flag.FlagSet) {
c.commonCfg.RegisterFlags(fs)
}
func execVerify(cfg *verifyCfg, io commands.IO) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
// Verify it
if validateErr := genesis.Validate(); validateErr != nil {
return fmt.Errorf("unable to verify genesis, %w", validateErr)
}
// Validate the genesis state
if genesis.AppState != nil {
state, ok := genesis.AppState.(gnoland.GnoGenesisState)
if !ok {
return errInvalidGenesisState
}
// Validate the initial transactions
for _, tx := range state.Txs {
if validateErr := tx.ValidateBasic(); validateErr != nil {
return fmt.Errorf("invalid transacton, %w", validateErr)
}
}
// Validate the initial balances
for _, balance := range state.Balances {
if err := balance.Verify(); err != nil {
return fmt.Errorf("invalid balance: %w", err)
}
}
}
io.Printfln("Genesis at %s is valid", cfg.genesisPath)
return nil
}