Browse Source

lite2: Prevent falsely returned double voting error (#4620)

* prevent faulty double voting error

* create test

* clean tests

* clean tests

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
pull/4649/head
Callum Waters 5 years ago
committed by GitHub
parent
commit
379848eab7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 6 deletions
  1. +5
    -5
      types/validator_set.go
  2. +45
    -1
      types/validator_set_test.go

+ 5
- 5
types/validator_set.go View File

@ -778,12 +778,12 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, blockID BlockID,
// check for each vote if its validator is already known. // check for each vote if its validator is already known.
valIdx, val := vals.GetByAddress(commitSig.ValidatorAddress) valIdx, val := vals.GetByAddress(commitSig.ValidatorAddress)
if firstIndex, ok := seenVals[valIdx]; ok { // double vote
secondIndex := idx
return errors.Errorf("double vote from %v (%d and %d)", val, firstIndex, secondIndex)
}
if val != nil { if val != nil {
// check for double vote of validator on the same commit
if firstIndex, ok := seenVals[valIdx]; ok {
secondIndex := idx
return errors.Errorf("double vote from %v (%d and %d)", val, firstIndex, secondIndex)
}
seenVals[valIdx] = idx seenVals[valIdx] = idx
// Validate signature. // Validate signature.


+ 45
- 1
types/validator_set_test.go View File

@ -3,11 +3,13 @@ package types
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/stretchr/testify/require"
"math" "math"
"sort" "sort"
"strings" "strings"
"testing" "testing"
"testing/quick" "testing/quick"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -308,7 +310,7 @@ func randPubKey() crypto.PubKey {
func randValidator(totalVotingPower int64) *Validator { func randValidator(totalVotingPower int64) *Validator {
// this modulo limits the ProposerPriority/VotingPower to stay in the // this modulo limits the ProposerPriority/VotingPower to stay in the
// bounds of MaxTotalVotingPower minus the already existing voting power: // bounds of MaxTotalVotingPower minus the already existing voting power:
val := NewValidator(randPubKey(), int64(tmrand.Uint64()%uint64((MaxTotalVotingPower-totalVotingPower))))
val := NewValidator(randPubKey(), int64(tmrand.Uint64()%uint64(MaxTotalVotingPower-totalVotingPower)))
val.ProposerPriority = tmrand.Int64() % (MaxTotalVotingPower - totalVotingPower) val.ProposerPriority = tmrand.Int64() % (MaxTotalVotingPower - totalVotingPower)
return val return val
} }
@ -1324,6 +1326,48 @@ func TestValSetUpdateOverflowRelated(t *testing.T) {
} }
} }
func TestVerifyCommitTrusting(t *testing.T) {
var (
blockID = makeBlockIDRandom()
voteSet, originalValset, vals = randVoteSet(1, 1, PrecommitType, 6, 1)
commit, err = MakeCommit(blockID, 1, 1, voteSet, vals, time.Now())
newValSet, _ = RandValidatorSet(2, 1)
)
require.NoError(t, err)
testCases := []struct {
valSet *ValidatorSet
err bool
}{
// good
0: {
valSet: originalValset,
err: false,
},
// bad - no overlap between validator sets
1: {
valSet: newValSet,
err: true,
},
// good - first two are different but the rest of the same -> >1/3
2: {
valSet: NewValidatorSet(append(newValSet.Validators, originalValset.Validators...)),
err: false,
},
}
for _, tc := range testCases {
err = tc.valSet.VerifyCommitTrusting("test_chain_id", blockID, commit.Height, commit,
tmmath.Fraction{Numerator: 1, Denominator: 3})
if tc.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}
//--------------------- //---------------------
// Sort validators by priority and address // Sort validators by priority and address
type validatorsByPriority []*Validator type validatorsByPriority []*Validator


Loading…
Cancel
Save