Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 07-tendermint set client/consensus states in UpdateState #1199

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (cs ClientState) CheckSubstituteAndUpdateState(
return nil, sdkerrors.Wrap(err, "unable to retrieve latest consensus state for substitute client")
}

SetConsensusState(subjectClientStore, cdc, consensusState, height)
setConsensusState(subjectClientStore, cdc, consensusState, height)

// set metadata stored for the substitute consensus state
processedHeight, found := GetProcessedHeight(substituteClientStore, height)
Expand Down
4 changes: 2 additions & 2 deletions modules/light-clients/07-tendermint/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func setClientState(clientStore sdk.KVStore, cdc codec.BinaryCodec, clientState
clientStore.Set(key, val)
}

// SetConsensusState stores the consensus state at the given height.
func SetConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensusState *ConsensusState, height exported.Height) {
// setConsensusState stores the consensus state at the given height.
func setConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensusState *ConsensusState, height exported.Height) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to make private / unexported

key := host.ConsensusStateKey(height)
val := clienttypes.MustMarshalConsensusState(cdc, consensusState)
clientStore.Set(key, val)
Expand Down
4 changes: 3 additions & 1 deletion modules/light-clients/07-tendermint/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, client
NextValidatorsHash: header.Header.NextValidatorsHash,
}

// set metadata for this consensus state
// set client state, consensus state and asssociated metadata
setClientState(clientStore, cdc, &cs)
setConsensusState(clientStore, cdc, consensusState, header.GetHeight())
setConsensusMetadata(ctx, clientStore, header.GetHeight())

return &cs, consensusState, nil
Expand Down
40 changes: 28 additions & 12 deletions modules/light-clients/07-tendermint/types/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
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"
Expand Down Expand Up @@ -397,11 +398,10 @@ func (suite *TendermintTestSuite) TestVerifyHeader() {

func (suite *TendermintTestSuite) TestUpdateState() {
var (
path *ibctesting.Path
clientMessage exported.ClientMessage
pruneHeight clienttypes.Height
updatedClientState *types.ClientState // TODO: retrieve from state after 'UpdateState' call
updatedConsensusState *types.ConsensusState // TODO: retrieve from state after 'UpdateState' call
path *ibctesting.Path
clientMessage exported.ClientMessage
clientStore sdk.KVStore
pruneHeight clienttypes.Height
)

testCases := []struct {
Expand All @@ -415,7 +415,10 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().LT(clientMessage.GetHeight()))
},
func() {
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().LT(updatedClientState.GetLatestHeight())) // new update, updated client state should have changed
bz := clientStore.Get(host.ClientStateKey())
updatedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), bz)

suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().EQ(updatedClientState.GetLatestHeight())) // new update, updated client state should have changed
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}, true,
},
{
Expand All @@ -429,6 +432,9 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().GT(clientMessage.GetHeight()))
},
func() {
bz := clientStore.Get(host.ClientStateKey())
updatedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), bz)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this go in malleate? I'm not sure these checks perform any actual checks now since GetClientState() performs the same code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, yeah I get you! GetClientState is essentially just getting from the store and unmarshalling as well. So this is basically asserting true is true.

Could you expand on what you're suggesting for moving to malleate? I'm not sure I follow, malleate will be called before UpdateState right, so the client state retrieved is not yet updated. Are you suggesting to retrieve the not yet updated state in malleate and then in expResult() assert that they are not equal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting to retrieve the not yet updated state in malleate and then in expResult() assert that they are not equal?

These test cases are asserting the client state didn't get updated (since we updated to a previous height). So yes, I am suggesting we construct the expected client state in malleate and then ensure the client state doesn't change after calling UpdateState

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, sorry overlooked that when I wrote the comment. I've updated as you suggested to retrieve the "previous" states before update in malleate and check for equality in expResult! :))

suite.Require().Equal(path.EndpointA.GetClientState(), updatedClientState) // fill in height, no change to client state
}, true,
},
Expand All @@ -444,6 +450,12 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().Equal(path.EndpointA.GetClientState().GetLatestHeight(), clientMessage.GetHeight())
},
func() {
bz := clientStore.Get(host.ClientStateKey())
updatedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), bz)

bz = clientStore.Get(host.ConsensusStateKey(clientMessage.GetHeight()))
updatedConsensusState := clienttypes.MustUnmarshalConsensusState(suite.chainA.App.AppCodec(), bz)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on moving into malleate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍


suite.Require().Equal(path.EndpointA.GetClientState(), updatedClientState)
suite.Require().Equal(path.EndpointA.GetConsensusState(clientMessage.GetHeight()), updatedConsensusState)
}, true,
Expand Down Expand Up @@ -474,7 +486,10 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().NoError(err)
},
func() {
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().LT(updatedClientState.GetLatestHeight())) // new update, updated client state should have changed
bz := clientStore.Get(host.ClientStateKey())
updatedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), bz)

suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().EQ(updatedClientState.GetLatestHeight())) // new update, updated client state should have changed
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

// ensure consensus state was pruned
_, found := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
Expand Down Expand Up @@ -511,8 +526,8 @@ func (suite *TendermintTestSuite) TestUpdateState() {
tmClientState, ok := clientState.(*types.ClientState)
suite.Require().True(ok)

clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
updatedClientState, updatedConsensusState, err = tmClientState.UpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, clientMessage)
clientStore = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
_, _, err = tmClientState.UpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, clientMessage)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -523,13 +538,14 @@ func (suite *TendermintTestSuite) TestUpdateState() {
Root: commitmenttypes.NewMerkleRoot(header.Header.GetAppHash()),
NextValidatorsHash: header.Header.NextValidatorsHash,
}

bz := clientStore.Get(host.ConsensusStateKey(header.GetHeight()))
updatedConsensusState := clienttypes.MustUnmarshalConsensusState(suite.chainA.App.AppCodec(), bz)

suite.Require().Equal(expConsensusState, updatedConsensusState)

} else {
suite.Require().Error(err)
suite.Require().Nil(updatedClientState)
suite.Require().Nil(updatedConsensusState)

}

// perform custom checks
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/07-tendermint/types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (cs ClientState) VerifyUpgradeAndUpdateState(
)

setClientState(clientStore, cdc, newClientState)
SetConsensusState(clientStore, cdc, newConsState, newClientState.LatestHeight)
setConsensusState(clientStore, cdc, newConsState, newClientState.LatestHeight)
setConsensusMetadata(ctx, clientStore, tmUpgradeClient.LatestHeight)

return nil
Expand Down