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

refactor: replace usage of verification funcs in 03-connection #1647

Merged
174 changes: 148 additions & 26 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/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"
)

Expand All @@ -31,9 +35,27 @@ func (k Keeper) VerifyClientState(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

if err := targetClient.VerifyClientState(
clientStore, k.cdc, height,
connection.GetCounterparty().GetPrefix(), connection.GetCounterparty().GetClientID(), proof, clientState); err != nil {
merklePath := commitmenttypes.NewMerklePath(host.FullClientStatePath(connection.GetCounterparty().GetClientID()))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

bz, err := k.cdc.MarshalInterface(clientState)
if err != nil {
return err
}

if err := targetClient.VerifyMembership(
ctx, clientStore, k.cdc, height,
0, 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

might be nice to add an inline comment for future readers:

    0, 0, // skip delay period checks for non-packet processing verification

or something along those lines

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should add test cases for each of the verify functions with the time/block delay periods set to 0. That is, add test cases which will normally fail on the delay period not being passed (if 0, 0, isn't passed in). Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great idea, I'll add the inline comment.

add test cases which will normally fail on the delay period not being passed (if 0, 0, isn't passed in)

So if 0, 0, isn't passed in here it won't fail? there's nothing at the lightclient level which would return an error, and it needs to be generic to support both zero values and non zero values for delay periods, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's address this with a follow up if needs be.

proof, path, bz,
); err != nil {
return sdkerrors.Wrapf(err, "failed client state verification for target client: %s", clientID)
}

Expand Down Expand Up @@ -62,9 +84,26 @@ func (k Keeper) VerifyClientConsensusState(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

if err := clientState.VerifyClientConsensusState(
clientStore, k.cdc, height,
connection.GetCounterparty().GetClientID(), consensusHeight, connection.GetCounterparty().GetPrefix(), proof, consensusState,
merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(connection.GetCounterparty().GetClientID(), consensusHeight))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

bz, err := k.cdc.MarshalInterface(consensusState)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
0, 0,
proof, path, bz,
); err != nil {
return sdkerrors.Wrapf(err, "failed consensus state verification for client (%s)", clientID)
}
Expand All @@ -80,7 +119,7 @@ func (k Keeper) VerifyConnectionState(
height exported.Height,
proof []byte,
connectionID string,
connectionEnd exported.ConnectionI, // opposite connection
counterpartyConnection exported.ConnectionI, // opposite connection
) error {
clientID := connection.GetClientID()
clientStore := k.clientKeeper.ClientStore(ctx, clientID)
Expand All @@ -94,9 +133,31 @@ func (k Keeper) VerifyConnectionState(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

if err := clientState.VerifyConnectionState(
clientStore, k.cdc, height,
connection.GetCounterparty().GetPrefix(), proof, connectionID, connectionEnd,
merklePath := commitmenttypes.NewMerklePath(host.ConnectionPath(connectionID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

connectionEnd, ok := counterpartyConnection.(connectiontypes.ConnectionEnd)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "invalid connection type %T", counterpartyConnection)
}

bz, err := k.cdc.Marshal(&connectionEnd)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
0, 0,
proof, path, bz,
); err != nil {
return sdkerrors.Wrapf(err, "failed connection state verification for client (%s)", clientID)
}
Expand Down Expand Up @@ -127,10 +188,31 @@ func (k Keeper) VerifyChannelState(
return sdkerrors.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

if err := clientState.VerifyChannelState(
clientStore, k.cdc, height,
connection.GetCounterparty().GetPrefix(), proof,
portID, channelID, channel,
merklePath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

channelEnd, ok := channel.(channeltypes.Channel)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "invalid channel type %T", channel)
}

bz, err := k.cdc.Marshal(&channelEnd)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
0, 0,
proof, path, bz,
); err != nil {
return sdkerrors.Wrapf(err, "failed channel state verification for client (%s)", clientID)
}
Expand Down Expand Up @@ -166,11 +248,21 @@ func (k Keeper) VerifyPacketCommitment(
timeDelay := connection.GetDelayPeriod()
blockDelay := k.getBlockDelay(ctx, connection)

if err := clientState.VerifyPacketCommitment(
merklePath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, sequence))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence, commitmentBytes,
proof, path, commitmentBytes,
); err != nil {
return sdkerrors.Wrapf(err, "failed packet commitment verification for client (%s)", clientID)
}
Expand Down Expand Up @@ -206,11 +298,21 @@ func (k Keeper) VerifyPacketAcknowledgement(
timeDelay := connection.GetDelayPeriod()
blockDelay := k.getBlockDelay(ctx, connection)

if err := clientState.VerifyPacketAcknowledgement(
merklePath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, sequence))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence, acknowledgement,
proof, path, channeltypes.CommitAcknowledgement(acknowledgement),
); err != nil {
return sdkerrors.Wrapf(err, "failed packet acknowledgement verification for client (%s)", clientID)
}
Expand Down Expand Up @@ -246,11 +348,21 @@ func (k Keeper) VerifyPacketReceiptAbsence(
timeDelay := connection.GetDelayPeriod()
blockDelay := k.getBlockDelay(ctx, connection)

if err := clientState.VerifyPacketReceiptAbsence(
merklePath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, sequence))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

if err := clientState.VerifyNonMembership(
ctx, clientStore, k.cdc, height,
timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
sequence,
proof, path,
); err != nil {
return sdkerrors.Wrapf(err, "failed packet receipt absence verification for client (%s)", clientID)
}
Expand Down Expand Up @@ -285,11 +397,21 @@ func (k Keeper) VerifyNextSequenceRecv(
timeDelay := connection.GetDelayPeriod()
blockDelay := k.getBlockDelay(ctx, connection)

if err := clientState.VerifyNextSequenceRecv(
merklePath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

path, err := k.cdc.Marshal(&merklePath)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
timeDelay, blockDelay,
connection.GetCounterparty().GetPrefix(), proof, portID, channelID,
nextSequenceRecv,
proof, path, sdk.Uint64ToBigEndian(nextSequenceRecv),
); err != nil {
return sdkerrors.Wrapf(err, "failed next sequence receive verification for client (%s)", clientID)
}
Expand Down