-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
genesis_state.go
129 lines (111 loc) · 3.94 KB
/
genesis_state.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
package types
import (
"encoding/json"
"fmt"
tmos "github.com/tendermint/tendermint/libs/os"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// NewGenesisState creates a new GenesisState object
func NewGenesisState(genTxs []json.RawMessage) *GenesisState {
// Ensure genTxs is never nil, https://github.com/cosmos/cosmos-sdk/issues/5086
if len(genTxs) == 0 {
genTxs = make([]json.RawMessage, 0)
}
return &GenesisState{
GenTxs: genTxs,
}
}
// DefaultGenesisState returns the genutil module's default genesis state.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
GenTxs: []json.RawMessage{},
}
}
// NewGenesisStateFromTx creates a new GenesisState object
// from auth transactions
func NewGenesisStateFromTx(txJSONEncoder sdk.TxEncoder, genTxs []sdk.Tx) *GenesisState {
genTxsBz := make([]json.RawMessage, len(genTxs))
for i, genTx := range genTxs {
var err error
genTxsBz[i], err = txJSONEncoder(genTx)
if err != nil {
panic(err)
}
}
return NewGenesisState(genTxsBz)
}
// GetGenesisStateFromAppState gets the genutil genesis state from the expected app state
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return &genesisState
}
// SetGenesisStateInAppState sets the genutil genesis state within the expected app state
func SetGenesisStateInAppState(
cdc codec.JSONCodec, appState map[string]json.RawMessage, genesisState *GenesisState,
) map[string]json.RawMessage {
genesisStateBz := cdc.MustMarshalJSON(genesisState)
appState[ModuleName] = genesisStateBz
return appState
}
// GenesisStateFromGenDoc creates the core parameters for genesis initialization
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
func GenesisStateFromGenDoc(genDoc tmtypes.GenesisDoc) (genesisState map[string]json.RawMessage, err error) {
if err = json.Unmarshal(genDoc.AppState, &genesisState); err != nil {
return genesisState, err
}
return genesisState, nil
}
// GenesisStateFromGenFile creates the core parameters for genesis initialization
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMessage, genDoc *tmtypes.GenesisDoc, err error) {
if !tmos.FileExists(genFile) {
return genesisState, genDoc,
fmt.Errorf("%s does not exist, run `init` first", genFile)
}
genDoc, err = tmtypes.GenesisDocFromFile(genFile)
if err != nil {
return genesisState, genDoc, err
}
genesisState, err = GenesisStateFromGenDoc(*genDoc)
return genesisState, genDoc, err
}
// ValidateGenesis validates GenTx transactions
func ValidateGenesis(genesisState *GenesisState, txJSONDecoder sdk.TxDecoder) error {
for _, genTx := range genesisState.GenTxs {
_, err := ValidateAndGetGenTx(genTx, txJSONDecoder)
if err != nil {
return err
}
}
return nil
}
// ValidateAndGetGenTx validates the genesis transaction and returns GenTx if valid
// it cannot verify the signature as it is stateless validation
func ValidateAndGetGenTx(genTx json.RawMessage, txJSONDecoder sdk.TxDecoder) (sdk.Tx, error) {
tx, err := txJSONDecoder(genTx)
if err != nil {
return tx, fmt.Errorf("failed to decode gentx: %s, error: %s", genTx, err)
}
msgs := tx.GetMsgs()
if len(msgs) != 1 {
return tx, fmt.Errorf("unexpected number of GenTx messages; got: %d, expected: 1", len(msgs))
}
// TODO: abstract back to staking
if _, ok := msgs[0].(*stakingtypes.MsgCreateValidator); !ok {
return tx, fmt.Errorf("unexpected GenTx message type; expected: MsgCreateValidator, got: %T", msgs[0])
}
if err := msgs[0].ValidateBasic(); err != nil {
return tx, fmt.Errorf("invalid GenTx '%s': %s", msgs[0], err)
}
return tx, nil
}