Skip to content

Commit

Permalink
Add test for MsgSubmitConsumerMisbehaviour parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Jul 12, 2023
1 parent e734e53 commit 865e43b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
99 changes: 99 additions & 0 deletions tests/integration/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ibc-go/v4/modules/core/exported"
"github.com/cosmos/interchain-security/v2/x/ccv/provider"
"github.com/cosmos/interchain-security/v2/x/ccv/provider/types"

ibcsolotypes "github.com/cosmos/ibc-go/v4/modules/light-clients/06-solomachine/types"
Expand Down Expand Up @@ -100,6 +101,10 @@ func (s *CCVTestSuite) TestHandleConsumerMisbehaviour() {
for _, tc := range testCases {
s.Run(tc.name, func() {
err := s.providerApp.GetProviderKeeper().HandleConsumerMisbehaviour(s.providerCtx(), tc.misbehaviour)
// get clienstate
cs, ok := s.providerApp.GetIBCKeeper().ClientKeeper.GetClientState(s.providerCtx(), s.path.EndpointB.ClientID)
s.Require().True(ok)

if tc.expPass {
s.NoError(err)
// Check that only the validators of the alternate validator set
Expand All @@ -111,11 +116,16 @@ func (s *CCVTestSuite) TestHandleConsumerMisbehaviour() {
if _, ok := altSigners[consuVal.Address.String()]; ok {
s.Require().True(provVal.Jailed)
s.Require().True(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provConsAddr))

} else {
s.Require().False(provVal.Jailed)
s.Require().False(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provConsAddr))
s.Require().NotZero(cs.(*ibctmtypes.ClientState).FrozenHeight)

}
}
// verify that the client was frozen
s.Require().NotZero(cs.(*ibctmtypes.ClientState).FrozenHeight)
} else {
// Check that no validators are jailed or tombstoned on the provider
for _, consuVal := range clientTMValset.Validators {
Expand All @@ -126,6 +136,8 @@ func (s *CCVTestSuite) TestHandleConsumerMisbehaviour() {
s.Require().NoError(err)
s.Require().False(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provConsAddr))
}
// verify that the client wasn't frozen
s.Require().Zero(cs.(*ibctmtypes.ClientState).FrozenHeight)
}
})
}
Expand Down Expand Up @@ -299,6 +311,93 @@ func (s *CCVTestSuite) TestConstructLightClientEvidence() {
}
}

// TestMsgSubmitConsumerClientHandler tests that the provider
// handler can parse MsgSubmitConsumerMisbehaviour
// TODO: move this to x/provider/handler_test.go
func (s *CCVTestSuite) TestMsgSubmitConsumerMisbehaviourHandler() {
s.SetupCCVChannel(s.path)
// required to have the consumer client revision height greater than 0
s.SendEmptyVSCPacket()

// create signing info for all validators
for _, v := range s.providerChain.Vals.Validators {
s.setDefaultValSigningInfo(*v)
}

// create a new header timestamp
headerTs := s.providerCtx().BlockTime().Add(time.Minute)

// get trusted validators and height
clientHeight := s.consumerChain.LastHeader.TrustedHeight
clientTMValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators)
clientSigners := s.consumerChain.Signers

altValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators[0:2])
altSigners := make(map[string]tmtypes.PrivValidator, 1)
altSigners[clientTMValset.Validators[0].Address.String()] = clientSigners[clientTMValset.Validators[0].Address.String()]
altSigners[clientTMValset.Validators[1].Address.String()] = clientSigners[clientTMValset.Validators[1].Address.String()]

testCases := []struct {
name string
misbehaviour *ibctmtypes.Misbehaviour
expPass bool
}{
{
"invalid MsgSubmitMisbehaviour shouldn't pass",
&ibctmtypes.Misbehaviour{},
false,
},
{

"valid MsgSubmitMisbehaviour should pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: s.consumerChain.CreateTMClientHeader(
s.consumerChain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
headerTs,
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
),
// the resulting Header2 will have a different BlockID
// than Header1 since doesn't share the same valset and signers
Header2: s.consumerChain.CreateTMClientHeader(
s.consumerChain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
headerTs,
altValset,
altValset,
clientTMValset,
altSigners,
),
},
true,
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
// build the msg using the misbehaviour data
msg, err := types.NewMsgSubmitConsumerMisbehaviour(s.providerChain.SenderAccount.GetAddress(), tc.misbehaviour)
s.Require().NoError(err)

k := s.providerApp.GetProviderKeeper()

// Try to handle the message
_, err = provider.NewHandler(&k)(s.providerCtx(), msg)
if tc.expPass {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
})
}
}

func (s *CCVTestSuite) getProviderValFromConsumerVal(valAddr tmtypes.Validator) stakingtypes.Validator {
consuAddr := types.NewConsumerConsAddress(sdk.ConsAddress(valAddr.Address.Bytes()))
provAddr := s.providerApp.GetProviderKeeper().GetProviderAddrFromConsumerAddr(s.providerCtx(), s.consumerChain.ChainID, consuAddr)
Expand Down
4 changes: 4 additions & 0 deletions testutil/integration/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,7 @@ func TestHandleConsumerMisbehaviour(t *testing.T) {
func TestConstructLightClientEvidence(t *testing.T) {
runCCVTestByName(t, "TestConstructLightClientEvidence")
}

func TestMsgSubmitConsumerMisbehaviourHandler(t *testing.T) {
runCCVTestByName(t, "TestMsgSubmitConsumerMisbehaviourHandler")
}

0 comments on commit 865e43b

Please sign in to comment.