Browse Source

[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
dont_panic_fileperms
Alessio Treglia 6 years ago
committed by Ethan Buchman
parent
commit
3744e8271d
3 changed files with 34 additions and 12 deletions
  1. +1
    -0
      CHANGELOG_PENDING.md
  2. +20
    -12
      state/state.go
  3. +13
    -0
      state/state_test.go

+ 1
- 0
CHANGELOG_PENDING.md 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

+ 20
- 12
state/state.go View File

@ -198,17 +198,25 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
} }
// Make validators slice // Make validators slice
validators := make([]*types.Validator, len(genDoc.Validators))
for i, val := range genDoc.Validators {
pubKey := val.PubKey
address := pubKey.Address()
// Make validator
validators[i] = &types.Validator{
Address: address,
PubKey: pubKey,
VotingPower: val.Power,
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))
for i, val := range genDoc.Validators {
pubKey := val.PubKey
address := pubKey.Address()
// Make validator
validators[i] = &types.Validator{
Address: address,
PubKey: pubKey,
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),
Validators: types.NewValidatorSet(validators),
NextValidators: nextValidatorSet,
Validators: validatorSet,
LastValidators: types.NewValidatorSet(nil), LastValidators: types.NewValidatorSet(nil),
LastHeightValidatorsChanged: 1, LastHeightValidatorsChanged: 1,


+ 13
- 0
state/state_test.go 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)


Loading…
Cancel
Save