Skip to content

Commit

Permalink
refactor: replace usage of verification funcs in 03-connection (cosmo…
Browse files Browse the repository at this point in the history
…s#1647)

* replacing 03-connection verifications with generic membership methods

* adding inline comments for skipping delay period checks

* chore: removing legacy verify funcs from `ClientState` interface (cosmos#1649)

* removing legacy verify funcs from clientstate interface

* chore: remove legacy verify funcs tm client (cosmos#1651)

* removing legacy verification methods from tendermint client

* removing commented out tests

* fixing interface definition
  • Loading branch information
damiannolan authored and oshorefueled committed Aug 9, 2022
1 parent 9ca378a commit 7a5e68a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1,231 deletions.
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.ErrClientStateNotFound, "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, // skip delay period checks for non-packet processing verification
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.ErrClientStateNotFound, "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, // skip delay period checks for non-packet processing verification
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.ErrClientStateNotFound, "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, // skip delay period checks for non-packet processing verification
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.ErrClientStateNotFound, "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, // skip delay period checks for non-packet processing verification
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
92 changes: 0 additions & 92 deletions modules/core/exported/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,98 +125,6 @@ type ClientState interface {
path []byte,
) error

VerifyClientState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
prefix Prefix,
counterpartyClientIdentifier string,
proof []byte,
clientState ClientState,
) error
VerifyClientConsensusState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
counterpartyClientIdentifier string,
consensusHeight Height,
prefix Prefix,
proof []byte,
consensusState ConsensusState,
) error
VerifyConnectionState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
prefix Prefix,
proof []byte,
connectionID string,
connectionEnd ConnectionI,
) error
VerifyChannelState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
prefix Prefix,
proof []byte,
portID,
channelID string,
channel ChannelI,
) error
VerifyPacketCommitment(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
commitmentBytes []byte,
) error
VerifyPacketAcknowledgement(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
acknowledgement []byte,
) error
VerifyPacketReceiptAbsence(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
) error
VerifyNextSequenceRecv(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix Prefix,
proof []byte,
portID,
channelID string,
nextSequenceRecv uint64,
) error
GetTimestampAtHeight(
ctx sdk.Context,
clientStore sdk.KVStore,
Expand Down
Loading

0 comments on commit 7a5e68a

Please sign in to comment.