From 865e43b99a49250934c07d37e32a7c221518871f Mon Sep 17 00:00:00 2001 From: Simon Noetzlin Date: Wed, 12 Jul 2023 10:32:37 +0200 Subject: [PATCH] Add test for MsgSubmitConsumerMisbehaviour parsing --- tests/integration/misbehaviour.go | 99 ++++++++++++++++++++++++++++++ testutil/integration/debug_test.go | 4 ++ 2 files changed, 103 insertions(+) diff --git a/tests/integration/misbehaviour.go b/tests/integration/misbehaviour.go index 9934f1d77b..bb6c16aa98 100644 --- a/tests/integration/misbehaviour.go +++ b/tests/integration/misbehaviour.go @@ -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" @@ -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 @@ -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 { @@ -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) } }) } @@ -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) diff --git a/testutil/integration/debug_test.go b/testutil/integration/debug_test.go index 3bafef8295..37ab9209f5 100644 --- a/testutil/integration/debug_test.go +++ b/testutil/integration/debug_test.go @@ -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") +}