[R4R] Pass nil to NewValidatorSet() when genesis file's Validators field is nil (#2617)

* Pass nil to NewValidatorSet() when genesis file's Validators field is nil

Closes: #2616

* Update CHANGELOG_PENDING.md
This commit is contained in:
Alessio Treglia 2018-10-11 20:37:21 -07:00 committed by Ethan Buchman
parent 7b48ea1788
commit 3744e8271d
3 changed files with 33 additions and 11 deletions

View File

@ -57,3 +57,4 @@ timeoutPrecommit before starting next round
block block
- [p2p] \#2555 fix p2p switch FlushThrottle value (@goolAdapter) - [p2p] \#2555 fix p2p switch FlushThrottle value (@goolAdapter)
- [libs/event] \#2518 fix event concurrency flaw (@goolAdapter) - [libs/event] \#2518 fix event concurrency flaw (@goolAdapter)
- [state] \#2616 Pass nil to NewValidatorSet() when genesis file's Validators field is nil

View File

@ -198,6 +198,11 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
} }
// Make validators slice // Make validators slice
var validatorSet, nextValidatorSet *types.ValidatorSet
if genDoc.Validators == nil {
validatorSet = types.NewValidatorSet(nil)
nextValidatorSet = types.NewValidatorSet(nil)
} else {
validators := make([]*types.Validator, len(genDoc.Validators)) validators := make([]*types.Validator, len(genDoc.Validators))
for i, val := range genDoc.Validators { for i, val := range genDoc.Validators {
pubKey := val.PubKey pubKey := val.PubKey
@ -210,6 +215,9 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
VotingPower: val.Power, VotingPower: val.Power,
} }
} }
validatorSet = types.NewValidatorSet(validators)
nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementAccum(1)
}
return State{ return State{
@ -219,8 +227,8 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
LastBlockID: types.BlockID{}, LastBlockID: types.BlockID{},
LastBlockTime: genDoc.GenesisTime, LastBlockTime: genDoc.GenesisTime,
NextValidators: types.NewValidatorSet(validators).CopyIncrementAccum(1), NextValidators: nextValidatorSet,
Validators: types.NewValidatorSet(validators), Validators: validatorSet,
LastValidators: types.NewValidatorSet(nil), LastValidators: types.NewValidatorSet(nil),
LastHeightValidatorsChanged: 1, LastHeightValidatorsChanged: 1,

View File

@ -48,6 +48,19 @@ func TestStateCopy(t *testing.T) {
%v`, state)) %v`, state))
} }
//TestMakeGenesisStateNilValidators tests state's consistency when genesis file's validators field is nil.
func TestMakeGenesisStateNilValidators(t *testing.T) {
doc := types.GenesisDoc{
ChainID: "dummy",
Validators: nil,
}
require.Nil(t, doc.ValidateAndComplete())
state, err := MakeGenesisState(&doc)
require.Nil(t, err)
require.Equal(t, 0, len(state.Validators.Validators))
require.Equal(t, 0, len(state.NextValidators.Validators))
}
// TestStateSaveLoad tests saving and loading State from a db. // TestStateSaveLoad tests saving and loading State from a db.
func TestStateSaveLoad(t *testing.T) { func TestStateSaveLoad(t *testing.T) {
tearDown, stateDB, state := setupTestCase(t) tearDown, stateDB, state := setupTestCase(t)