2019-05-16 08:25:32 -07:00
|
|
|
package genutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/common"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
)
|
|
|
|
|
|
|
|
// State to Unmarshal
|
|
|
|
type GenesisState struct {
|
|
|
|
GenTxs []json.RawMessage `json:"gentxs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenesisState creates a new GenesisState object
|
|
|
|
func NewGenesisState(genTxs []json.RawMessage) GenesisState {
|
|
|
|
return GenesisState{
|
|
|
|
GenTxs: genTxs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenesisStateFromStdTx creates a new GenesisState object
|
|
|
|
// from auth transactions
|
|
|
|
func NewGenesisStateFromStdTx(genTxs []auth.StdTx) GenesisState {
|
|
|
|
genTxsBz := make([]json.RawMessage, len(genTxs))
|
|
|
|
for i, genTx := range genTxs {
|
|
|
|
genTxsBz[i] = moduleCdc.MustMarshalJSON(genTx)
|
|
|
|
}
|
|
|
|
return GenesisState{
|
|
|
|
GenTxs: genTxsBz,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the genutil genesis state from the expected app state
|
|
|
|
func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawMessage) GenesisState {
|
|
|
|
var genesisState GenesisState
|
|
|
|
if appState[ModuleName] != nil {
|
|
|
|
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
|
|
|
|
}
|
|
|
|
return genesisState
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the genutil genesis state within the expected app state
|
|
|
|
func SetGenesisStateInAppState(cdc *codec.Codec,
|
|
|
|
appState map[string]json.RawMessage, genesisState GenesisState) map[string]json.RawMessage {
|
|
|
|
|
|
|
|
genesisStateBz := cdc.MustMarshalJSON(genesisState)
|
|
|
|
appState[ModuleName] = genesisStateBz
|
|
|
|
return appState
|
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// Create the core parameters for genesis initialization for the application.
|
|
|
|
//
|
|
|
|
// NOTE: The pubkey input is this machines pubkey.
|
2019-05-16 08:25:32 -07:00
|
|
|
func GenesisStateFromGenDoc(cdc *codec.Codec, genDoc tmtypes.GenesisDoc,
|
|
|
|
) (genesisState map[string]json.RawMessage, err error) {
|
|
|
|
|
|
|
|
if err = cdc.UnmarshalJSON(genDoc.AppState, &genesisState); err != nil {
|
|
|
|
return genesisState, err
|
|
|
|
}
|
|
|
|
return genesisState, nil
|
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// Create the core parameters for genesis initialization for the application.
|
|
|
|
//
|
|
|
|
// NOTE: The pubkey input is this machines pubkey.
|
2019-05-16 08:25:32 -07:00
|
|
|
func GenesisStateFromGenFile(cdc *codec.Codec, genFile string,
|
|
|
|
) (genesisState map[string]json.RawMessage, genDoc *tmtypes.GenesisDoc, err error) {
|
|
|
|
|
|
|
|
if !common.FileExists(genFile) {
|
|
|
|
return genesisState, genDoc,
|
2019-05-18 01:42:24 -07:00
|
|
|
fmt.Errorf("%s does not exist, run `init` first", genFile)
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
genDoc, err = tmtypes.GenesisDocFromFile(genFile)
|
|
|
|
if err != nil {
|
|
|
|
return genesisState, genDoc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
genesisState, err = GenesisStateFromGenDoc(cdc, *genDoc)
|
|
|
|
return genesisState, genDoc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate GenTx transactions
|
|
|
|
func ValidateGenesis(genesisState GenesisState) error {
|
|
|
|
for i, genTx := range genesisState.GenTxs {
|
|
|
|
var tx auth.StdTx
|
|
|
|
if err := moduleCdc.UnmarshalJSON(genTx, &tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs := tx.GetMsgs()
|
|
|
|
if len(msgs) != 1 {
|
|
|
|
return errors.New(
|
|
|
|
"must provide genesis StdTx with exactly 1 CreateValidator message")
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
// TODO abstract back to staking
|
2019-05-16 08:25:32 -07:00
|
|
|
if _, ok := msgs[0].(staking.MsgCreateValidator); !ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Genesis transaction %v does not contain a MsgCreateValidator", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|