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: move status checks from connection verify_* functions to 02-client verify_* functions. #7006

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ func (k *Keeper) VerifyMembership(ctx sdk.Context, clientID string, height expor
return err
}

if status := clientModule.Status(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(types.ErrClientNotActive, "cannot call verify membership on client (%s) with status %s", clientID, status)
}

return clientModule.VerifyMembership(ctx, clientID, height, delayTimePeriod, delayBlockPeriod, proof, path, value)
}

Expand All @@ -386,6 +390,10 @@ func (k *Keeper) VerifyNonMembership(ctx sdk.Context, clientID string, height ex
return err
}

if status := clientModule.Status(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(types.ErrClientNotActive, "cannot call verify non membership on client (%s) with status %s", clientID, status)
}

return clientModule.VerifyNonMembership(ctx, clientID, height, delayTimePeriod, delayBlockPeriod, proof, path)
}

Expand Down
128 changes: 128 additions & 0 deletions modules/core/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,134 @@ func (suite *KeeperTestSuite) TestGetTimestampAtHeight() {
}
}

func (suite *KeeperTestSuite) TestVerifyMembership() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these (both for membership and non membership) were missing apparently (probably because it simply routed before?

var path *ibctesting.Path

cases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"invalid client id",
func() {
path.EndpointA.ClientID = ""
},
host.ErrInvalidID,
},
{
"failure: client is frozen",
func() {
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
clientState.FrozenHeight = types.NewHeight(0, 1)
path.EndpointA.SetClientState(clientState)
},
types.ErrClientNotActive,
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.Setup()

// create default proof, merklePath, and value which passes
key := host.FullClientStateKey(path.EndpointB.ClientID)
merklePath := commitmenttypes.NewMerklePath(key)
merklePrefixPath, err := commitmenttypes.ApplyPrefix(suite.chainB.GetPrefix(), merklePath)
suite.Require().NoError(err)

proof, proofHeight := suite.chainB.QueryProof(key)

clientState, ok := path.EndpointB.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
value, err := suite.chainB.Codec.MarshalInterface(clientState)
suite.Require().NoError(err)

tc.malleate()

err = suite.chainA.App.GetIBCKeeper().ClientKeeper.VerifyMembership(suite.chainA.GetContext(), path.EndpointA.ClientID, proofHeight, 0, 0, proof, merklePrefixPath, value)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
} else {
suite.Require().ErrorIs(err, tc.expError)
}
})
}
}

func (suite *KeeperTestSuite) TestVerifyNonMembership() {
var path *ibctesting.Path

cases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"invalid client id",
func() {
path.EndpointA.ClientID = ""
},
host.ErrInvalidID,
},
{
"failure: client is frozen",
func() {
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
clientState.FrozenHeight = types.NewHeight(0, 1)
path.EndpointA.SetClientState(clientState)
},
types.ErrClientNotActive,
},
}

for _, tc := range cases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.Setup()

// create default proof, merklePath, and value which passes
key := host.FullClientStateKey("invalid-client-id")

merklePath := commitmenttypes.NewMerklePath(key)
merklePrefixPath, err := commitmenttypes.ApplyPrefix(suite.chainB.GetPrefix(), merklePath)
suite.Require().NoError(err)

proof, proofHeight := suite.chainB.QueryProof(key)

tc.malleate()

err = suite.chainA.App.GetIBCKeeper().ClientKeeper.VerifyNonMembership(suite.chainA.GetContext(), path.EndpointA.ClientID, proofHeight, 0, 0, proof, merklePrefixPath)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
} else {
suite.Require().ErrorIs(err, tc.expError)
}
})
}
}

// TestDefaultSetParams tests the default params set are what is expected
func (suite *KeeperTestSuite) TestDefaultSetParams() {
expParams := types.DefaultParams()
Expand Down
41 changes: 0 additions & 41 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
Expand All @@ -25,10 +24,6 @@ func (k *Keeper) VerifyClientState(
clientState exported.ClientState,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.FullClientStateKey(connection.Counterparty.ClientId))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down Expand Up @@ -62,10 +57,6 @@ func (k *Keeper) VerifyClientConsensusState(
consensusState exported.ConsensusState,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStateKey(connection.Counterparty.ClientId, consensusHeight))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down Expand Up @@ -99,10 +90,6 @@ func (k *Keeper) VerifyConnectionState(
counterpartyConnection types.ConnectionEnd, // opposite connection
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ConnectionKey(connectionID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down Expand Up @@ -137,10 +124,6 @@ func (k *Keeper) VerifyChannelState(
channel channeltypes.Channel,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down Expand Up @@ -176,10 +159,6 @@ func (k *Keeper) VerifyPacketCommitment(
commitmentBytes []byte,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand Down Expand Up @@ -212,10 +191,6 @@ func (k *Keeper) VerifyPacketAcknowledgement(
acknowledgement []byte,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand Down Expand Up @@ -249,10 +224,6 @@ func (k *Keeper) VerifyPacketReceiptAbsence(
sequence uint64,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand Down Expand Up @@ -284,10 +255,6 @@ func (k *Keeper) VerifyNextSequenceRecv(
nextSequenceRecv uint64,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand Down Expand Up @@ -320,10 +287,6 @@ func (k *Keeper) VerifyChannelUpgradeError(
errorReceipt channeltypes.ErrorReceipt,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down Expand Up @@ -357,10 +320,6 @@ func (k *Keeper) VerifyChannelUpgrade(
upgrade channeltypes.Upgrade,
) error {
clientID := connection.ClientId
if status := k.clientKeeper.GetClientStatus(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand Down
Loading