Skip to content

Commit

Permalink
add IBCClientHeader func for endpoint and update tests (cosmos#5493)
Browse files Browse the repository at this point in the history
* add PopulateClientHeader func for endpoint and update tests

* minor fix

* lint

* lint

* rename function to IBCClientHeader and move it to chain struct

* add migration entry

* apply review suggestions

---------

Co-authored-by: Carlos Rodriguez <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
Co-authored-by: Colin Axnér <[email protected]>
  • Loading branch information
4 people authored Feb 6, 2024
1 parent fc0bfab commit 99202e6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 51 deletions.
1 change: 1 addition & 0 deletions docs/docs/05-migrations/13-v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Please use the new functions `path.Setup`, `path.SetupClients`, `path.SetupConne
### IBC testing package

- The `mock.PV` type has been removed in favour of [`cmttypes.MockPV`](https://github.com/cometbft/cometbft/blob/v0.38.5/types/priv_validator.go#L50) ([#5709](https://github.com/cosmos/ibc-go/pull/5709)).
- Functions `ConstructUpdateTMClientHeader` and `ConstructUpdateTMClientHeaderWithTrustedHeight` of `TestChain` type have been replaced with `IBCClientHeader`. This function will construct a `07-tendermint` header to update the light client on the counterparty chain. The trusted height must be passed in as a non-zero height.

## Relayers

Expand Down
5 changes: 3 additions & 2 deletions modules/core/02-client/keeper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {

// Must create header creation functions since suite.header gets recreated on each test case
createFutureUpdateFn := func(trustedHeight clienttypes.Height) *ibctm.Header {
header, err := suite.chainA.ConstructUpdateTMClientHeaderWithTrustedHeight(path.EndpointB.Chain, path.EndpointA.ClientID, trustedHeight)
header, err := path.EndpointB.Chain.IBCClientHeader(path.EndpointB.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
return header
}
Expand Down Expand Up @@ -507,7 +507,8 @@ func (suite *KeeperTestSuite) TestUpdateClientEventEmission() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()

header, err := suite.chainA.ConstructUpdateTMClientHeader(suite.chainB, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
header, err := path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

msg, err := clienttypes.NewMsgUpdateClient(
Expand Down
3 changes: 2 additions & 1 deletion modules/core/02-client/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func (suite *KeeperTestSuite) TestMsgUpdateClientEvents() {

suite.chainB.Coordinator.CommitBlock(suite.chainB)

header, err := suite.chainA.ConstructUpdateTMClientHeader(suite.chainB, ibctesting.FirstClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
header, err := suite.chainB.IBCClientHeader(suite.chainB.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
suite.Require().NotNil(header)

Expand Down
3 changes: 2 additions & 1 deletion modules/core/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func (suite *AnteTestSuite) createUpdateClientMessage() sdk.Msg {

switch endpoint.ClientConfig.GetClientType() {
case exported.Tendermint:
header, _ = endpoint.Chain.ConstructUpdateTMClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID)
trustedHeight := endpoint.GetClientState().GetLatestHeight().(clienttypes.Height)
header, _ = endpoint.Counterparty.Chain.IBCClientHeader(endpoint.Counterparty.Chain.LatestCommittedHeader, trustedHeight)

default:
}
Expand Down
24 changes: 16 additions & 8 deletions modules/light-clients/07-tendermint/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() {

// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
header, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
header, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

tc.malleate()
Expand Down Expand Up @@ -364,7 +365,8 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().NoError(err)

// use the same header which just updated the client
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
clientMessage, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

tmHeader, ok := clientMessage.(*ibctm.Header)
Expand Down Expand Up @@ -406,7 +408,8 @@ func (suite *TendermintTestSuite) TestUpdateState() {

// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
clientMessage, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
},
func() {
Expand Down Expand Up @@ -448,7 +451,8 @@ func (suite *TendermintTestSuite) TestUpdateState() {
suite.Require().NoError(err)

// use the same header which just updated the client
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
clientMessage, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
},
func() {
Expand Down Expand Up @@ -484,7 +488,8 @@ func (suite *TendermintTestSuite) TestUpdateState() {

// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
clientMessage, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

tc.malleate()
Expand Down Expand Up @@ -717,15 +722,17 @@ func (suite *TendermintTestSuite) TestCheckForMisbehaviour() {
{
"valid fork misbehaviour returns true",
func() {
header1, err := path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
header1, err := path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

// commit block and update client
suite.coordinator.CommitBlock(suite.chainB)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)

header2, err := path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight = path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
header2, err := path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

// assign the same height, each header will have a different commit hash
Expand Down Expand Up @@ -766,7 +773,8 @@ func (suite *TendermintTestSuite) TestCheckForMisbehaviour() {

// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
clientMessage, err = path.EndpointA.Counterparty.Chain.IBCClientHeader(path.EndpointA.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)

tc.malleate()
Expand Down
62 changes: 24 additions & 38 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,44 +446,6 @@ func (chain *TestChain) GetPrefix() commitmenttypes.MerklePrefix {
return commitmenttypes.NewMerklePrefix(chain.App.GetIBCKeeper().ConnectionKeeper.GetCommitmentPrefix().Bytes())
}

// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the
// light client on the source chain.
func (chain *TestChain) ConstructUpdateTMClientHeader(counterparty *TestChain, clientID string) (*ibctm.Header, error) {
return chain.ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty, clientID, clienttypes.ZeroHeight())
}

// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the
// light client on the source chain.
func (chain *TestChain) ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty *TestChain, clientID string, trustedHeight clienttypes.Height) (*ibctm.Header, error) {
header := counterparty.LatestCommittedHeader
// Relayer must query for LatestHeight on client to get TrustedHeight if the trusted height is not set
if trustedHeight.IsZero() {
trustedHeight = chain.GetClientState(clientID).GetLatestHeight().(clienttypes.Height)
}
var (
cmtTrustedVals *cmttypes.ValidatorSet
ok bool
)

cmtTrustedVals, ok = counterparty.GetValsAtHeight(int64(trustedHeight.RevisionHeight))
if !ok {
return nil, errorsmod.Wrapf(ibctm.ErrInvalidHeaderHeight, "could not retrieve trusted validators at trustedHeight: %d", trustedHeight)
}

// inject trusted fields into last header
// for now assume revision number is 0
header.TrustedHeight = trustedHeight

trustedVals, err := cmtTrustedVals.ToProto()
if err != nil {
return nil, err
}
trustedVals.TotalVotingPower = cmtTrustedVals.TotalVotingPower()
header.TrustedValidators = trustedVals

return header, nil
}

// ExpireClient fast forwards the chain's block time by the provided amount of time which will
// expire any clients with a trusting period less than or equal to this amount of time.
func (chain *TestChain) ExpireClient(amount time.Duration) {
Expand Down Expand Up @@ -663,3 +625,27 @@ func (chain *TestChain) DeleteKey(key []byte) {
kvStore := chain.GetContext().KVStore(storeKey)
kvStore.Delete(key)
}

// IBCClientHeader will construct a 07-tendermint Header to update the light client
// on the counterparty chain. The trustedHeight must be passed in as a non-zero height.
func (chain *TestChain) IBCClientHeader(header *ibctm.Header, trustedHeight clienttypes.Height) (*ibctm.Header, error) {
if trustedHeight.IsZero() {
return nil, errorsmod.Wrap(ibctm.ErrInvalidHeaderHeight, "trustedHeight must be a non-zero height")
}

cmtTrustedVals, ok := chain.GetValsAtHeight(int64(trustedHeight.RevisionHeight))
if !ok {
return nil, errorsmod.Wrapf(ibctm.ErrInvalidHeaderHeight, "could not retrieve trusted validators at trustedHeight: %d", trustedHeight)
}

trustedVals, err := cmtTrustedVals.ToProto()
if err != nil {
return nil, err
}

header.TrustedHeight = trustedHeight
trustedVals.TotalVotingPower = cmtTrustedVals.TotalVotingPower()
header.TrustedValidators = trustedVals

return header, nil
}
3 changes: 2 additions & 1 deletion testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ func (endpoint *Endpoint) UpdateClient() (err error) {

switch endpoint.ClientConfig.GetClientType() {
case exported.Tendermint:
header, err = endpoint.Chain.ConstructUpdateTMClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID)
trustedHeight := endpoint.GetClientState().GetLatestHeight().(clienttypes.Height)
header, err = endpoint.Counterparty.Chain.IBCClientHeader(endpoint.Counterparty.Chain.LatestCommittedHeader, trustedHeight)
default:
err = fmt.Errorf("client type %s is not supported", endpoint.ClientConfig.GetClientType())
}
Expand Down

0 comments on commit 99202e6

Please sign in to comment.