diff --git a/state/state.go b/state/state.go index 7e22e04d1..4e0fa75b5 100644 --- a/state/state.go +++ b/state/state.go @@ -324,12 +324,9 @@ func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State { // // Used in tests. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State { - if len(genDoc.Validators) == 0 { - cmn.Exit(cmn.Fmt("The genesis file has no validators")) - } - - if genDoc.GenesisTime.IsZero() { - genDoc.GenesisTime = time.Now() + err := genDoc.ValidateAndComplete() + if err != nil { + cmn.Exit(cmn.Fmt("Error in genesis file: %v", err)) } // Make validators slice diff --git a/types/genesis.go b/types/genesis.go index b7762c576..79bf63c8b 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -52,26 +52,48 @@ func (genDoc *GenesisDoc) ValidatorHash() []byte { return vset.Hash() } -//------------------------------------------------------------ -// Make genesis state from file - -// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc. -func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) { - genDoc := GenesisDoc{} - err := json.Unmarshal(jsonBlob, &genDoc) +// ValidateAndComplete checks that all necessary fields are present +// and fills in defaults for optional fields left empty +func (genDoc *GenesisDoc) ValidateAndComplete() error { - // validate genesis if genDoc.ChainID == "" { - return nil, errors.Errorf("Genesis doc must include non-empty chain_id") + return errors.Errorf("Genesis doc must include non-empty chain_id") } + if genDoc.ConsensusParams == nil { genDoc.ConsensusParams = cfg.DefaultConsensusParams() } else { if err := genDoc.ConsensusParams.Validate(); err != nil { - return nil, err + return err } } + if len(genDoc.Validators) == 0 { + return errors.Errorf("The genesis file must have at least one validator") + } + + if genDoc.GenesisTime.IsZero() { + genDoc.GenesisTime = time.Now() + } + + return nil +} + +//------------------------------------------------------------ +// Make genesis state from file + +// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc. +func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) { + genDoc := GenesisDoc{} + err := json.Unmarshal(jsonBlob, &genDoc) + if err != nil { + return nil, err + } + + if err := genDoc.ValidateAndComplete(); err != nil { + return nil, err + } + return &genDoc, err }