/
validate_genesis.go
54 lines (43 loc) · 1.58 KB
/
validate_genesis.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
package cli
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/netcloth/netcloth-chain/codec"
"github.com/netcloth/netcloth-chain/server"
"github.com/netcloth/netcloth-chain/types/module"
)
// Validate genesis command takes
func ValidateGenesisCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager) *cobra.Command {
return &cobra.Command{
Use: "validate-genesis [file]",
Args: cobra.RangeArgs(0, 1),
Short: "validates the genesis file at the default location or at the location passed as an arg",
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Load default if passed no args, otherwise load passed file
var genesis string
if len(args) == 0 {
genesis = ctx.Config.GenesisFile()
} else {
genesis = args[0]
}
fmt.Fprintf(os.Stderr, "validating genesis file at %s\n", genesis)
var genDoc *tmtypes.GenesisDoc
if genDoc, err = tmtypes.GenesisDocFromFile(genesis); err != nil {
return fmt.Errorf("error loading genesis doc from %s: %s", genesis, err.Error())
}
var genState map[string]json.RawMessage
if err = cdc.UnmarshalJSON(genDoc.AppState, &genState); err != nil {
return fmt.Errorf("error unmarshaling genesis doc %s: %s", genesis, err.Error())
}
if err = mbm.ValidateGenesis(genState); err != nil {
return fmt.Errorf("error validating genesis file %s: %s", genesis, err.Error())
}
// TODO test to make sure initchain doesn't panic
fmt.Printf("File at %s is a valid genesis file\n", genesis)
return nil
},
}
}