From 7e0b64e8b0842d2f13c2b24feed13ec187be10f5 Mon Sep 17 00:00:00 2001 From: Tess Rinearson Date: Thu, 12 Sep 2019 14:59:41 -0700 Subject: [PATCH] types: add test for block commits with votes for the wrong blockID (#3936) * types: add test for block commits with votes for the wrong blockID * remove * respond to feedback * verify the commits as well * testing table * Update types/block_test.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com> * Update types/block_test.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com> * gofmt * test panic case --- types/block_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/types/block_test.go b/types/block_test.go index 71c6e9ea..1bf2a15f 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -15,6 +15,7 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/tmhash" cmn "github.com/tendermint/tendermint/libs/common" + tmtime "github.com/tendermint/tendermint/types/time" "github.com/tendermint/tendermint/version" ) @@ -372,6 +373,63 @@ func TestCommitToVoteSet(t *testing.T) { } } +func TestCommitToVoteSetWithVotesForAnotherBlockOrNilBlock(t *testing.T) { + blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash")) + blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash")) + blockID3 := makeBlockID([]byte("blockhash3"), 10000, []byte("partshash")) + + height := int64(3) + round := 1 + + type commitVoteTest struct { + blockIDs []BlockID + numVotes []int // must sum to numValidators + numValidators int + valid bool + } + + testCases := []commitVoteTest{ + {[]BlockID{blockID, blockID2, blockID3}, []int{8, 1, 1}, 10, true}, + {[]BlockID{blockID, blockID2, blockID3}, []int{67, 20, 13}, 100, true}, + {[]BlockID{blockID, blockID2, blockID3}, []int{1, 1, 1}, 3, false}, + {[]BlockID{blockID, blockID2, blockID3}, []int{3, 1, 1}, 5, false}, + {[]BlockID{blockID, {}}, []int{67, 33}, 100, true}, + {[]BlockID{blockID, blockID2, {}}, []int{10, 5, 5}, 20, false}, + } + + for _, tc := range testCases { + voteSet, valSet, vals := randVoteSet(height-1, 1, PrecommitType, tc.numValidators, 1) + + vi := 0 + for n := range tc.blockIDs { + for i := 0; i < tc.numVotes[n]; i++ { + addr := vals[vi].GetPubKey().Address() + vote := &Vote{ + ValidatorAddress: addr, + ValidatorIndex: vi, + Height: height - 1, + Round: round, + Type: PrecommitType, + BlockID: tc.blockIDs[n], + Timestamp: tmtime.Now(), + } + + _, err := signAddVote(vals[vi], vote, voteSet) + assert.NoError(t, err) + vi++ + } + } + if tc.valid { + commit := voteSet.MakeCommit() // panics without > 2/3 valid votes + assert.NotNil(t, commit) + err := valSet.VerifyCommit(voteSet.ChainID(), blockID, height-1, commit) + assert.Nil(t, err) + } else { + assert.Panics(t, func() { voteSet.MakeCommit() }) + } + } +} + func TestSignedHeaderValidateBasic(t *testing.T) { commit := randCommit() chainID := "𠜎"