From 0f931eeb10a7f5953e55c05e69335fefae6dfb90 Mon Sep 17 00:00:00 2001 From: b00f Date: Tue, 14 Aug 2018 23:02:53 +0800 Subject: [PATCH] types: allow genesis file to have 0 validators (#2148) * fixing issue 2015 * Remove comments for code review * Update tests --- .gitignore | 2 ++ types/genesis.go | 6 +----- types/genesis_test.go | 30 +++++++++++++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 69196fac5..193a42894 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ addrbook.json terraform.tfstate terraform.tfstate.backup terraform.tfstate.d + +.vscode \ No newline at end of file diff --git a/types/genesis.go b/types/genesis.go index c4f69980e..ccfd019c7 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -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) diff --git a/types/genesis_test.go b/types/genesis_test.go index 925bba790..fb981e9cc 100644 --- a/types/genesis_test.go +++ b/types/genesis_test.go @@ -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) {