Browse Source

genDoc.ValidateAndComplete

pull/650/head
Ethan Buchman 7 years ago
parent
commit
14abdd57f3
2 changed files with 35 additions and 16 deletions
  1. +3
    -6
      state/state.go
  2. +32
    -10
      types/genesis.go

+ 3
- 6
state/state.go View File

@ -324,12 +324,9 @@ func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
// //
// Used in tests. // Used in tests.
func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State { 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 // Make validators slice


+ 32
- 10
types/genesis.go View File

@ -52,26 +52,48 @@ func (genDoc *GenesisDoc) ValidatorHash() []byte {
return vset.Hash() 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 == "" { 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 { if genDoc.ConsensusParams == nil {
genDoc.ConsensusParams = cfg.DefaultConsensusParams() genDoc.ConsensusParams = cfg.DefaultConsensusParams()
} else { } else {
if err := genDoc.ConsensusParams.Validate(); err != nil { 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 return &genDoc, err
} }


Loading…
Cancel
Save