mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-10 06:57:24 +00:00
genDoc.ValidateAndComplete
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -52,6 +52,33 @@ func (genDoc *GenesisDoc) ValidatorHash() []byte {
|
||||
return vset.Hash()
|
||||
}
|
||||
|
||||
// ValidateAndComplete checks that all necessary fields are present
|
||||
// and fills in defaults for optional fields left empty
|
||||
func (genDoc *GenesisDoc) ValidateAndComplete() error {
|
||||
|
||||
if genDoc.ChainID == "" {
|
||||
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 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
|
||||
|
||||
@@ -59,17 +86,12 @@ func (genDoc *GenesisDoc) ValidatorHash() []byte {
|
||||
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
|
||||
genDoc := GenesisDoc{}
|
||||
err := json.Unmarshal(jsonBlob, &genDoc)
|
||||
|
||||
// validate genesis
|
||||
if genDoc.ChainID == "" {
|
||||
return nil, errors.Errorf("Genesis doc must include non-empty chain_id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if genDoc.ConsensusParams == nil {
|
||||
genDoc.ConsensusParams = cfg.DefaultConsensusParams()
|
||||
} else {
|
||||
if err := genDoc.ConsensusParams.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := genDoc.ValidateAndComplete(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &genDoc, err
|
||||
|
||||
Reference in New Issue
Block a user