Browse Source

types: allow genesis file to have 0 validators (#2148)

* fixing issue 2015

* Remove comments for code review

* Update tests
pull/2224/head
b00f 6 years ago
committed by Anton Kaliaev
parent
commit
0f931eeb10
3 changed files with 24 additions and 14 deletions
  1. +2
    -0
      .gitignore
  2. +1
    -5
      types/genesis.go
  3. +21
    -9
      types/genesis_test.go

+ 2
- 0
.gitignore View File

@ -37,3 +37,5 @@ addrbook.json
terraform.tfstate
terraform.tfstate.backup
terraform.tfstate.d
.vscode

+ 1
- 5
types/genesis.go View File

@ -25,7 +25,7 @@ type GenesisDoc struct {
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
Validators []GenesisValidator `json:"validators"`
Validators []GenesisValidator `json:"validators,omitempty"`
AppHash cmn.HexBytes `json:"app_hash"`
AppState json.RawMessage `json:"app_state,omitempty"`
}
@ -65,10 +65,6 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
}
}
if len(genDoc.Validators) == 0 {
return cmn.NewError("The genesis file must have at least one validator")
}
for _, v := range genDoc.Validators {
if v.Power == 0 {
return cmn.NewError("The genesis file cannot contain validators with no voting power: %v", v)


+ 21
- 9
types/genesis_test.go View File

@ -14,15 +14,14 @@ import (
func TestGenesisBad(t *testing.T) {
// test some bad ones from raw json
testCases := [][]byte{
[]byte{}, // empty
[]byte{1, 1, 1, 1, 1}, // junk
[]byte(`{}`), // empty
[]byte(`{"chain_id":"mychain"}`), // missing validators
[]byte(`{"chain_id":"mychain","validators":[]}`), // missing validators
[]byte(`{"chain_id":"mychain","validators":[{}]}`), // missing validators
[]byte(`{"chain_id":"mychain","validators":null}`), // missing validators
[]byte(`{"chain_id":"mychain"}`), // missing validators
[]byte(`{"validators":[{"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`), // missing chain_id
[]byte{}, // empty
[]byte{1, 1, 1, 1, 1}, // junk
[]byte(`{}`), // empty
[]byte(`{"chain_id":"mychain","validators":[{}]}`), // invalid validator
// missing pub_key type
[]byte(`{"validators":[{"pub_key":{"value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`),
// missing chain_id
[]byte(`{"validators":[{"pub_key":{"type":"tendermint/PubKeyEd25519","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="},"power":"10","name":""}]}`),
}
for _, testCase := range testCases {
@ -62,6 +61,19 @@ func TestGenesisGood(t *testing.T) {
assert.NoError(t, err, "error marshalling genDoc")
genDoc, err = GenesisDocFromJSON(genDocBytes)
assert.Error(t, err, "expected error for genDoc json with block size of 0")
// Genesis doc from raw json
missingValidatorsTestCases := [][]byte{
[]byte(`{"chain_id":"mychain"}`), // missing validators
[]byte(`{"chain_id":"mychain","validators":[]}`), // missing validators
[]byte(`{"chain_id":"mychain","validators":null}`), // nil validator
[]byte(`{"chain_id":"mychain"}`), // missing validators
}
for _, tc := range missingValidatorsTestCases {
_, err := GenesisDocFromJSON(tc)
assert.NoError(t, err)
}
}
func TestGenesisSaveAs(t *testing.T) {


Loading…
Cancel
Save