Skip to content

Commit

Permalink
check evidence age duration
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Aug 24, 2023
1 parent 1e301d5 commit 0b2665e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
58 changes: 50 additions & 8 deletions tests/integration/double_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package integration
import (
"time"

abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/interchain-security/v2/x/ccv/provider/types"
"github.com/tendermint/tendermint/crypto/tmhash"
Expand Down Expand Up @@ -100,25 +103,26 @@ func (s *CCVTestSuite) TestHandleConsumerDoubleVoting() {
provAddr := s.providerApp.GetProviderKeeper().GetProviderAddrFromConsumerAddr(s.providerCtx(), s.consumerChain.ChainID, consuAddr)

for _, tc := range testCases {
ctx := setDefaultConsensusEvidenceParams(s.providerCtx())
s.Run(tc.name, func() {
err = s.providerApp.GetProviderKeeper().HandleConsumerDoubleVoting(
s.providerCtx(),
ctx,
tc.ev,
tc.chainID,
)
if tc.expPass {
s.Require().NoError(err)

// verifies that the jailing and tombstoning has occurred
s.Require().True(s.providerApp.GetTestStakingKeeper().IsValidatorJailed(s.providerCtx(), provAddr.ToSdkConsAddr()))
s.Require().True(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provAddr.ToSdkConsAddr()))
s.Require().True(s.providerApp.GetTestStakingKeeper().IsValidatorJailed(ctx, provAddr.ToSdkConsAddr()))
s.Require().True(s.providerApp.GetTestSlashingKeeper().IsTombstoned(ctx, provAddr.ToSdkConsAddr()))
} else {
s.Require().Error(err)

// verifies that no jailing and tombstoning has occurred

s.Require().False(s.providerApp.GetTestStakingKeeper().IsValidatorJailed(s.providerCtx(), provAddr.ToSdkConsAddr()))
s.Require().False(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provAddr.ToSdkConsAddr()))
s.Require().False(s.providerApp.GetTestStakingKeeper().IsValidatorJailed(ctx, provAddr.ToSdkConsAddr()))
s.Require().False(s.providerApp.GetTestSlashingKeeper().IsTombstoned(ctx, provAddr.ToSdkConsAddr()))
}
})
}
Expand All @@ -140,6 +144,8 @@ func (s *CCVTestSuite) TestVerifyDoubleVotingEvidence() {
blockID1 := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))

oldTime := s.consumerCtx().BlockTime().Add(-505 * time.Hour)

consuAddr := types.NewConsumerConsAddress(sdk.ConsAddress(val.Address.Bytes()))
provAddr := s.providerApp.GetProviderKeeper().GetProviderAddrFromConsumerAddr(s.providerCtx(), s.consumerChain.ChainID, consuAddr)

Expand All @@ -149,6 +155,29 @@ func (s *CCVTestSuite) TestVerifyDoubleVotingEvidence() {
chainID string
expPass bool
}{
{
"evidence is too old - shouldn't pass",
[]*tmtypes.Vote{
makeAndSignVote(
blockID1,
s.consumerCtx().BlockHeight(),
oldTime,
valSet,
signer,
s.consumerChain.ChainID,
),
makeAndSignVote(
blockID2,
s.consumerCtx().BlockHeight(),
oldTime,
valSet,
signer,
s.consumerChain.ChainID,
),
},
s.consumerChain.ChainID,
false,
},
{
"evidence has votes with different block height - shouldn't pass",
[]*tmtypes.Vote{
Expand Down Expand Up @@ -313,23 +342,24 @@ func (s *CCVTestSuite) TestVerifyDoubleVotingEvidence() {
}

for _, tc := range testCases {
ctx := setDefaultConsensusEvidenceParams(s.providerCtx())
s.Run(tc.name, func() {
// get the consumer chain public key assigned to the validator
consuPubkey, ok := s.providerApp.GetProviderKeeper().GetValidatorConsumerPubKey(
s.providerCtx(),
ctx,
s.consumerChain.ChainID,
provAddr,
)
s.Require().True(ok)

err := s.providerApp.GetProviderKeeper().VerifyDoubleVotingEvidence(
s.providerCtx(),
ctx,
tmtypes.DuplicateVoteEvidence{
VoteA: tc.votes[0],
VoteB: tc.votes[1],
ValidatorPower: val.VotingPower,
TotalVotingPower: val.VotingPower,
Timestamp: s.consumerCtx().BlockTime(),
Timestamp: tc.votes[0].Timestamp,
},
tc.chainID,
consuPubkey,
Expand Down Expand Up @@ -390,3 +420,15 @@ func makeAndSignVote(
vote.Signature = v.Signature
return vote
}

func setDefaultConsensusEvidenceParams(ctx sdk.Context) sdk.Context {
return ctx.WithConsensusParams(
&abci.ConsensusParams{
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
},
)
}
12 changes: 11 additions & 1 deletion x/ccv/provider/keeper/double_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ func (k Keeper) VerifyDoubleVotingEvidence(
chainID string,
pubkey tmprotocrypto.PublicKey,
) error {
// TODO: check the evidence age once we agreed on the infraction height mapping
// check evidence age duration
maxAgeDuration := ctx.ConsensusParams().Evidence.MaxAgeDuration
ageDuration := ctx.BlockHeader().Time.Sub(evidence.Time())

if ageDuration > maxAgeDuration {
return fmt.Errorf(
"evidence from created at: %v is too old; evidence can not be older than %v",
evidence.Time(),
ctx.BlockHeader().Time.Add(maxAgeDuration),
)
}

// H/R/S must be the same
if evidence.VoteA.Height != evidence.VoteB.Height ||
Expand Down

0 comments on commit 0b2665e

Please sign in to comment.