Browse Source

types: check if nil or empty valset (#5167)

Solves #5138 in the way that if a validatorSet is nil or empty it will not try to transform it to protobug

Co-authored-by: Callum Michael Waters <cmwaters19@gmail.com>
pull/5173/head
Marko 4 years ago
committed by GitHub
parent
commit
dc71f265aa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 11 deletions
  1. +1
    -1
      consensus/byzantine_test.go
  2. +7
    -6
      state/state_test.go
  3. +2
    -2
      types/validator_set.go
  4. +2
    -2
      types/validator_set_test.go

+ 1
- 1
consensus/byzantine_test.go View File

@ -167,7 +167,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
msg := <-blocksSubs[j].Out()
block := msg.Data().(types.EventDataNewBlock).Block
// assert that we have evidence
assert.True(t, len(block.Evidence.Evidence) == 1)
require.True(t, len(block.Evidence.Evidence) == 1)
// and that the evidence is of type DuplicateVoteEvidence
ev, ok := block.Evidence.Evidence[0].(*types.DuplicateVoteEvidence)
assert.True(t, ok)


+ 7
- 6
state/state_test.go View File

@ -1036,24 +1036,25 @@ func TestStateProto(t *testing.T) {
tc := []struct {
testName string
state *sm.State
expPass bool
expPass1 bool
expPass2 bool
}{
{"empty state", &sm.State{}, false},
{"nil failure state", nil, false},
{"success state", &state, true},
{"empty state", &sm.State{}, true, false},
{"nil failure state", nil, false, false},
{"success state", &state, true, true},
}
for _, tt := range tc {
tt := tt
pbs, err := tt.state.ToProto()
if !tt.expPass {
if !tt.expPass1 {
assert.Error(t, err)
} else {
assert.NoError(t, err, tt.testName)
}
smt, err := sm.StateFromProto(pbs)
if tt.expPass {
if tt.expPass2 {
require.NoError(t, err, tt.testName)
require.Equal(t, tt.state, smt, tt.testName)
} else {


+ 2
- 2
types/validator_set.go View File

@ -912,8 +912,8 @@ func (valz ValidatorsByAddress) Swap(i, j int) {
// ToProto converts ValidatorSet to protobuf
func (vals *ValidatorSet) ToProto() (*tmproto.ValidatorSet, error) {
if vals == nil {
return nil, errors.New("nil validator set") // validator set should never be nil
if vals.IsNilOrEmpty() {
return &tmproto.ValidatorSet{}, nil // validator set should never be nil
}
vp := new(tmproto.ValidatorSet)


+ 2
- 2
types/validator_set_test.go View File

@ -1598,8 +1598,8 @@ func TestValidatorSetProtoBuf(t *testing.T) {
{"fail valSet2, pubkey empty", valset2, false, false},
{"fail nil Proposer", valset3, false, false},
{"fail empty Proposer", valset4, false, false},
{"fail empty valSet", &ValidatorSet{}, false, false},
{"false nil", nil, false, false},
{"fail empty valSet", &ValidatorSet{}, true, false},
{"false nil", nil, true, false},
}
for _, tc := range testCases {
protoValSet, err := tc.v1.ToProto()


Loading…
Cancel
Save