From 5fa2aabd0a5a9e1ba38c10c9ce91526369bea405 Mon Sep 17 00:00:00 2001 From: tnasu Date: Mon, 28 Nov 2022 21:07:03 +0900 Subject: [PATCH] fix testcases --- evidence/verify_test.go | 32 ++++++----- light/client_test.go | 38 +++++++++++-- state/execution_test.go | 4 +- types/evidence_test.go | 120 +++++++++------------------------------- types/protobuf_test.go | 6 +- 5 files changed, 82 insertions(+), 118 deletions(-) diff --git a/evidence/verify_test.go b/evidence/verify_test.go index d8a3b5fbc..d054149dc 100644 --- a/evidence/verify_test.go +++ b/evidence/verify_test.go @@ -193,7 +193,7 @@ func TestVerify_LunaticAttackAgainstState(t *testing.T) { // as it was not originally in the pending bucket, it should now have been added pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes) - require.Equal(t, 1, len(pendingEvs)) + assert.Equal(t, 1, len(pendingEvs)) assert.Equal(t, ev, pendingEvs[0]) // if we submit evidence only against a single byzantine validator when we see there are more validators then this @@ -292,9 +292,11 @@ func TestVerifyLightClientAttack_Equivocation(t *testing.T) { trustedHeader := makeHeaderRandom(10) conflictingHeader := makeHeaderRandom(10) + conflictingHeader.ValidatorsHash = conflictingVals.Hash() conflictingHeader.VotersHash = conflictingVoters.Hash() trustedHeader.VotersHash = conflictingHeader.VotersHash + trustedHeader.ValidatorsHash = conflictingHeader.ValidatorsHash trustedHeader.NextValidatorsHash = conflictingHeader.NextValidatorsHash trustedHeader.ConsensusHash = conflictingHeader.ConsensusHash trustedHeader.AppHash = conflictingHeader.AppHash @@ -397,9 +399,11 @@ func TestVerifyLightClientAttack_Amnesia(t *testing.T) { conflictingVals, conflictingVoters, conflictingPrivVals := types.RandVoterSet(5, 10) conflictingHeader := makeHeaderRandom(10) + conflictingHeader.ValidatorsHash = conflictingVals.Hash() conflictingHeader.VotersHash = conflictingVoters.Hash() trustedHeader := makeHeaderRandom(10) trustedHeader.VotersHash = conflictingHeader.VotersHash + trustedHeader.ValidatorsHash = conflictingHeader.ValidatorsHash trustedHeader.NextValidatorsHash = conflictingHeader.NextValidatorsHash trustedHeader.AppHash = conflictingHeader.AppHash trustedHeader.ConsensusHash = conflictingHeader.ConsensusHash @@ -648,6 +652,19 @@ func makeLunaticEvidence( Timestamp: commonTime, } + commonHeader := makeHeaderRandom(commonHeight) + commonHeader.Proof = proof + commonHeader.Time = commonTime + common = &types.LightBlock{ + SignedHeader: &types.SignedHeader{ + Header: commonHeader, + // we can leave this empty because we shouldn't be checking this + Commit: &types.Commit{}, + }, + ValidatorSet: commonValSet, + VoterSet: commonVoterSet, + } + trustedHeader := makeHeaderRandom(height) trustedHeader.Proof = proof trustedBlockID := makeBlockID(trustedHeader.Hash(), 1000, []byte("partshash")) @@ -670,19 +687,6 @@ func makeLunaticEvidence( VoterSet: trustedVoters, } - commonHeader := makeHeaderRandom(commonHeight) - commonHeader.Proof = proof - commonHeader.Time = commonTime - common = &types.LightBlock{ - SignedHeader: &types.SignedHeader{ - Header: commonHeader, - // we can leave this empty because we shouldn't be checking this - Commit: &types.Commit{}, - }, - ValidatorSet: commonValSet, - VoterSet: commonVoterSet, - } - return ev, trusted, common } diff --git a/light/client_test.go b/light/client_test.go index db54bbf2c..32e9136d4 100644 --- a/light/client_test.go +++ b/light/client_test.go @@ -75,8 +75,8 @@ var ( // last header (3/3 signed) 3: h3, } - l1 = &types.LightBlock{SignedHeader: h1, VoterSet: voterSet[1]} - l2 = &types.LightBlock{SignedHeader: h2, VoterSet: voterSet[2]} + l1 = &types.LightBlock{SignedHeader: h1, ValidatorSet: vals, VoterSet: voterSet[1]} + l2 = &types.LightBlock{SignedHeader: h2, ValidatorSet: vals, VoterSet: voterSet[2]} fullNode = mockp.New( chainID, headerSet, @@ -191,13 +191,27 @@ func TestClient_SequentialVerification(t *testing.T) { true, }, { - "bad: different first voter set", + "bad: different first validator set", map[int64]*types.SignedHeader{ 1: h1, }, map[int64]*types.ValidatorSet{ 1: differentVals, }, + map[int64]*types.VoterSet{ + 1: voterSet[1], + }, + true, + true, + }, + { + "bad: different first voter set", + map[int64]*types.SignedHeader{ + 1: h1, + }, + map[int64]*types.ValidatorSet{ + 1: vals, + }, map[int64]*types.VoterSet{ 1: differentVoters, }, @@ -243,13 +257,29 @@ func TestClient_SequentialVerification(t *testing.T) { true, }, { - "bad: different voter set at height 3", + "bad: different validator set at height 3", headerSet, map[int64]*types.ValidatorSet{ 1: vals, 2: vals, 3: newVals, }, + map[int64]*types.VoterSet{ + 1: voterSet[1], + 2: voterSet[2], + 3: voterSet[3], + }, + false, + true, + }, + { + "bad: different voter set at height 3", + headerSet, + map[int64]*types.ValidatorSet{ + 1: vals, + 2: vals, + 3: vals, + }, map[int64]*types.VoterSet{ 1: voterSet[1], 2: voterSet[2], diff --git a/state/execution_test.go b/state/execution_test.go index 3ce00559f..82b6755f8 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -151,6 +151,7 @@ func TestBeginBlockByzantineValidators(t *testing.T) { LastCommitHash: crypto.CRandBytes(tmhash.Size), DataHash: crypto.CRandBytes(tmhash.Size), VotersHash: state.Validators.Hash(), + ValidatorsHash: state.Validators.Hash(), NextValidatorsHash: state.Validators.Hash(), ConsensusHash: crypto.CRandBytes(tmhash.Size), AppHash: crypto.CRandBytes(tmhash.Size), @@ -174,7 +175,8 @@ func TestBeginBlockByzantineValidators(t *testing.T) { Signature: crypto.CRandBytes(types.MaxSignatureSize), }}), }, - VoterSet: state.Voters, + ValidatorSet: state.Validators, + VoterSet: state.Voters, }, CommonHeight: 8, ByzantineValidators: []*types.Validator{state.Validators.Validators[0]}, diff --git a/types/evidence_test.go b/types/evidence_test.go index 73b198386..5acaf35f0 100644 --- a/types/evidence_test.go +++ b/types/evidence_test.go @@ -46,28 +46,8 @@ func TestMaxEvidenceBytes(t *testing.T) { TotalVotingPower: math.MaxInt64, ValidatorPower: math.MaxInt64, Timestamp: timestamp, - VoteA: makeVote( - t, - val, - chainID, - math.MaxInt32, - math.MaxInt64, - math.MaxInt32, - tmproto.ProposalType, - blockID, - timestamp, - ), - VoteB: makeVote( - t, - val, - chainID, - math.MaxInt32, - math.MaxInt64, - math.MaxInt32, - tmproto.ProposalType, - blockID2, - timestamp, - ), + VoteA: makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 32, blockID, timestamp), + VoteB: makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 32, blockID2, timestamp), } bz, err := ev.ToProto().Marshal() @@ -82,28 +62,8 @@ func randomDuplicatedVoteEvidence(keyType PrivKeyType, t *testing.T) *DuplicateV blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash")) const chainID = "mychain" return &DuplicateVoteEvidence{ - VoteA: makeVote( - t, - val, - chainID, - 0, - 10, - 2, - tmproto.PrevoteType, - blockID, - defaultVoteTime, - ), - VoteB: makeVote( - t, - val, - chainID, - 0, - 10, - 2, - tmproto.PrevoteType, - blockID2, - defaultVoteTime.Add(1*time.Minute), - ), + VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultVoteTime), + VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultVoteTime.Add(1*time.Minute)), TotalVotingPower: 30, ValidatorPower: 10, Timestamp: defaultVoteTime, @@ -144,17 +104,7 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) { ev.VoteB = nil }, true}, {"Invalid vote type", func(ev *DuplicateVoteEvidence) { - ev.VoteA = makeVote( - t, - val, - chainID, - math.MaxInt32, - math.MaxInt64, - math.MaxInt32, - tmproto.UnknownType, - blockID2, - defaultVoteTime, - ) + ev.VoteA = makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0, blockID2, defaultVoteTime) }, true}, {"Invalid vote order", func(ev *DuplicateVoteEvidence) { swap := ev.VoteA.Copy() @@ -165,30 +115,10 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) { for _, tc := range testCases { tc := tc t.Run(tc.testName, func(t *testing.T) { - vote1 := makeVote( - t, - val, - chainID, - math.MaxInt32, - math.MaxInt64, - math.MaxInt32, - tmproto.PrecommitType, - blockID, - defaultVoteTime, - ) - vote2 := makeVote( - t, - val, - chainID, - math.MaxInt32, - math.MaxInt64, - math.MaxInt32, - tmproto.PrecommitType, - blockID2, - defaultVoteTime, - ) - valSet := WrapValidatorsToVoterSet([]*Validator{val.ExtractIntoValidator(10)}) - ev := NewDuplicateVoteEvidence(vote1, vote2, defaultVoteTime, valSet) + vote1 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID, defaultVoteTime) + vote2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID2, defaultVoteTime) + voterSet := WrapValidatorsToVoterSet([]*Validator{val.ExtractIntoValidator(10)}) + ev := NewDuplicateVoteEvidence(vote1, vote2, defaultVoteTime, voterSet) tc.malleateEvidence(ev) assert.Equal(t, tc.expectErr, ev.ValidateBasic() != nil, "Validate Basic had an unexpected result") }) @@ -212,7 +142,8 @@ func TestLightClientAttackEvidenceBasic(t *testing.T) { Header: header, Commit: commit, }, - VoterSet: voterSet, + ValidatorSet: valSet, + VoterSet: voterSet, }, CommonHeight: commonHeight, TotalVotingPower: voterSet.TotalVotingWeight(), @@ -262,6 +193,7 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) { voteSet, valSet, voterSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, nValidators, 1) header := makeHeaderRandom() header.Height = height + header.ValidatorsHash = valSet.Hash() header.VotersHash = voterSet.Hash() blockID := makeBlockID(header.Hash(), math.MaxInt32, tmhash.Sum([]byte("partshash"))) commit, err := MakeCommit(blockID, height, 1, voteSet, privVals, time.Now()) @@ -272,7 +204,8 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) { Header: header, Commit: commit, }, - VoterSet: voterSet, + ValidatorSet: valSet, + VoterSet: voterSet, }, CommonHeight: commonHeight, TotalVotingPower: voterSet.TotalVotingWeight(), @@ -297,6 +230,9 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) { {"Nil conflicting header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = nil }, true}, {"Nil conflicting blocl", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock = nil }, true}, {"Nil validator set", func(ev *LightClientAttackEvidence) { + ev.ConflictingBlock.ValidatorSet = &ValidatorSet{} + }, true}, + {"Nil voter set", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.VoterSet = &VoterSet{} }, true}, {"Negative total voting power", func(ev *LightClientAttackEvidence) { @@ -312,7 +248,8 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) { Header: header, Commit: commit, }, - VoterSet: voterSet, + ValidatorSet: valSet, + VoterSet: voterSet, }, CommonHeight: commonHeight, TotalVotingPower: voterSet.TotalVotingWeight(), @@ -336,16 +273,8 @@ func TestMockEvidenceValidateBasic(t *testing.T) { } func makeVote( - t *testing.T, - val PrivValidator, - chainID string, - valIndex int32, - height int64, - round int32, - step tmproto.SignedMsgType, - blockID BlockID, - time time.Time, -) *Vote { + t *testing.T, val PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID BlockID, + time time.Time) *Vote { pubKey, err := val.GetPubKey() require.NoError(t, err) v := &Vote{ @@ -353,7 +282,7 @@ func makeVote( ValidatorIndex: valIndex, Height: height, Round: round, - Type: step, + Type: tmproto.SignedMsgType(step), BlockID: blockID, Timestamp: time, Signature: []byte{}, @@ -378,6 +307,7 @@ func makeHeaderRandom() *Header { LastCommitHash: crypto.CRandBytes(tmhash.Size), DataHash: crypto.CRandBytes(tmhash.Size), VotersHash: crypto.CRandBytes(tmhash.Size), + ValidatorsHash: crypto.CRandBytes(tmhash.Size), NextValidatorsHash: crypto.CRandBytes(tmhash.Size), ConsensusHash: crypto.CRandBytes(tmhash.Size), AppHash: crypto.CRandBytes(tmhash.Size), @@ -394,8 +324,8 @@ func TestEvidenceProto(t *testing.T) { blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash"))) blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash"))) const chainID = "mychain" - v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, tmproto.PrevoteType, blockID, defaultVoteTime) - v2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 2, tmproto.PrevoteType, blockID2, defaultVoteTime) + v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime) + v2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 2, 0x01, blockID2, defaultVoteTime) // -------- SignedHeaders -------- const height int64 = 37 diff --git a/types/protobuf_test.go b/types/protobuf_test.go index 566631384..0a830653f 100644 --- a/types/protobuf_test.go +++ b/types/protobuf_test.go @@ -3,8 +3,6 @@ package types import ( "testing" - tmproto "github.com/line/ostracon/proto/ostracon/types" - "github.com/line/ostracon/crypto/secp256k1" "github.com/golang/protobuf/proto" // nolint: staticcheck // still used by gogoproto @@ -143,8 +141,8 @@ func TestABCIEvidence(t *testing.T) { const chainID = "mychain" now := time.Now() ev := &DuplicateVoteEvidence{ - VoteA: makeVote(t, val, chainID, 0, 10, 2, tmproto.PrevoteType, blockID, now), - VoteB: makeVote(t, val, chainID, 0, 10, 2, tmproto.PrevoteType, blockID2, now), + VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, now), + VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, now), TotalVotingPower: int64(100), ValidatorPower: int64(10), Timestamp: now,