From 21dbf56bd12745cf131a6df249b368b414169c3d Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 24 Mar 2022 10:53:32 +0100 Subject: [PATCH 1/4] adding UpdateStateOnMisbehaviour with basic test --- .../07-tendermint/types/update.go | 11 ++++ .../07-tendermint/types/upgrade_test.go | 54 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/modules/light-clients/07-tendermint/types/update.go b/modules/light-clients/07-tendermint/types/update.go index 2699ebc2cc9..32428f482fc 100644 --- a/modules/light-clients/07-tendermint/types/update.go +++ b/modules/light-clients/07-tendermint/types/update.go @@ -13,6 +13,7 @@ import ( clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" + host "github.com/cosmos/ibc-go/v3/modules/core/24-host" "github.com/cosmos/ibc-go/v3/modules/core/exported" ) @@ -261,3 +262,13 @@ func update(ctx sdk.Context, clientStore sdk.KVStore, clientState *ClientState, return clientState, consensusState } + +// UpdateStateOnMisbehaviour updates state upon misbehaviour, freezing the ClientState. This method should only be called on misbehaviour +// as it does not perform any misbehaviour checks. +func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore) (*ClientState, exported.ConsensusState, error) { + cs.FrozenHeight = FrozenHeight + + clientStore.Set(host.ClientStateKey(), clienttypes.MustMarshalClientState(cdc, &cs)) + + return nil, nil, nil +} diff --git a/modules/light-clients/07-tendermint/types/upgrade_test.go b/modules/light-clients/07-tendermint/types/upgrade_test.go index 112d3366cda..cc3b4f6f914 100644 --- a/modules/light-clients/07-tendermint/types/upgrade_test.go +++ b/modules/light-clients/07-tendermint/types/upgrade_test.go @@ -5,6 +5,7 @@ import ( clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" + host "github.com/cosmos/ibc-go/v3/modules/core/24-host" "github.com/cosmos/ibc-go/v3/modules/core/exported" "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types" ibctesting "github.com/cosmos/ibc-go/v3/testing" @@ -481,3 +482,56 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { } } } + +func (suite *TendermintTestSuite) TestUpdateStateOnMisbehaviour() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + // reset suite to create fresh application state + suite.SetupTest() + path = ibctesting.NewPath(suite.chainA, suite.chainB) + + err := path.EndpointA.CreateClient() + suite.Require().NoError(err) + + // ensure counterparty state is committed + suite.coordinator.CommitBlock(suite.chainB) + + tc.malleate() + + clientState := path.EndpointA.GetClientState() + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) + + // TODO: remove casting when 'UpdateState' is an interface function. + tmClientState, ok := clientState.(*types.ClientState) + suite.Require().True(ok) + + tmClientState.UpdateStateOnMisbehaviour(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore) + + if tc.expPass { + clientStateBz := clientStore.Get(host.ClientStateKey()) + suite.Require().NotEmpty(clientStateBz) + + newClientState := clienttypes.MustUnmarshalClientState(suite.chainA.Codec, clientStateBz) + suite.Require().Equal(frozenHeight, newClientState.(*types.ClientState).FrozenHeight) + } + }) + } +} From 523b72ce6e39a3e9f0478fb27b1e826ec1b0a10c Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 24 Mar 2022 10:57:26 +0100 Subject: [PATCH 2/4] removing redundant return values --- modules/light-clients/07-tendermint/types/update.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/light-clients/07-tendermint/types/update.go b/modules/light-clients/07-tendermint/types/update.go index 32428f482fc..bc321e515f3 100644 --- a/modules/light-clients/07-tendermint/types/update.go +++ b/modules/light-clients/07-tendermint/types/update.go @@ -265,10 +265,8 @@ func update(ctx sdk.Context, clientStore sdk.KVStore, clientState *ClientState, // UpdateStateOnMisbehaviour updates state upon misbehaviour, freezing the ClientState. This method should only be called on misbehaviour // as it does not perform any misbehaviour checks. -func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore) (*ClientState, exported.ConsensusState, error) { +func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore) { cs.FrozenHeight = FrozenHeight clientStore.Set(host.ClientStateKey(), clienttypes.MustMarshalClientState(cdc, &cs)) - - return nil, nil, nil } From 79c2f3cd48acf1056bb609151046e48988a2668a Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 24 Mar 2022 15:46:46 +0100 Subject: [PATCH 3/4] removing unnecessary CommitBlock in test func --- modules/light-clients/07-tendermint/types/upgrade_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/light-clients/07-tendermint/types/upgrade_test.go b/modules/light-clients/07-tendermint/types/upgrade_test.go index cc3b4f6f914..aaa8289f864 100644 --- a/modules/light-clients/07-tendermint/types/upgrade_test.go +++ b/modules/light-clients/07-tendermint/types/upgrade_test.go @@ -511,9 +511,6 @@ func (suite *TendermintTestSuite) TestUpdateStateOnMisbehaviour() { err := path.EndpointA.CreateClient() suite.Require().NoError(err) - // ensure counterparty state is committed - suite.coordinator.CommitBlock(suite.chainB) - tc.malleate() clientState := path.EndpointA.GetClientState() From 6dcab3e1a45aeb9a177359b60de6de2a28da1e74 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 24 Mar 2022 15:48:21 +0100 Subject: [PATCH 4/4] Update modules/light-clients/07-tendermint/types/update.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- modules/light-clients/07-tendermint/types/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/light-clients/07-tendermint/types/update.go b/modules/light-clients/07-tendermint/types/update.go index bc321e515f3..3f796cb90ed 100644 --- a/modules/light-clients/07-tendermint/types/update.go +++ b/modules/light-clients/07-tendermint/types/update.go @@ -263,7 +263,7 @@ func update(ctx sdk.Context, clientStore sdk.KVStore, clientState *ClientState, return clientState, consensusState } -// UpdateStateOnMisbehaviour updates state upon misbehaviour, freezing the ClientState. This method should only be called on misbehaviour +// UpdateStateOnMisbehaviour updates state upon misbehaviour, freezing the ClientState. This method should only be called when misbehaviour is detected // as it does not perform any misbehaviour checks. func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore) { cs.FrozenHeight = FrozenHeight