Skip to content

Commit

Permalink
fix testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Nov 28, 2022
1 parent 2fa616f commit 5fa2aab
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 118 deletions.
32 changes: 18 additions & 14 deletions evidence/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand All @@ -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
}

Expand Down
38 changes: 34 additions & 4 deletions light/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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],
Expand Down
4 changes: 3 additions & 1 deletion state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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]},
Expand Down
120 changes: 25 additions & 95 deletions types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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")
})
Expand All @@ -212,7 +142,8 @@ func TestLightClientAttackEvidenceBasic(t *testing.T) {
Header: header,
Commit: commit,
},
VoterSet: voterSet,
ValidatorSet: valSet,
VoterSet: voterSet,
},
CommonHeight: commonHeight,
TotalVotingPower: voterSet.TotalVotingWeight(),
Expand Down Expand Up @@ -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())
Expand All @@ -272,7 +204,8 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) {
Header: header,
Commit: commit,
},
VoterSet: voterSet,
ValidatorSet: valSet,
VoterSet: voterSet,
},
CommonHeight: commonHeight,
TotalVotingPower: voterSet.TotalVotingWeight(),
Expand All @@ -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) {
Expand All @@ -312,7 +248,8 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) {
Header: header,
Commit: commit,
},
VoterSet: voterSet,
ValidatorSet: valSet,
VoterSet: voterSet,
},
CommonHeight: commonHeight,
TotalVotingPower: voterSet.TotalVotingWeight(),
Expand All @@ -336,24 +273,16 @@ 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{
ValidatorAddress: pubKey.Address(),
ValidatorIndex: valIndex,
Height: height,
Round: round,
Type: step,
Type: tmproto.SignedMsgType(step),
BlockID: blockID,
Timestamp: time,
Signature: []byte{},
Expand All @@ -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),
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions types/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5fa2aab

Please sign in to comment.