Skip to content

Commit

Permalink
chore: remove legacy verify funcs tm client (#1651)
Browse files Browse the repository at this point in the history
* removing legacy verification methods from tendermint client

* removing commented out tests
  • Loading branch information
damiannolan committed Jul 7, 2022
1 parent ab8d977 commit a9fbba8
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 1,066 deletions.
374 changes: 0 additions & 374 deletions modules/light-clients/07-tendermint/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

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 Down Expand Up @@ -190,333 +187,6 @@ func (cs ClientState) Initialize(ctx sdk.Context, _ codec.BinaryCodec, clientSto
return nil
}

// VerifyClientState verifies a proof of the client state of the running chain
// stored on the target machine
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyClientState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
counterpartyClientIdentifier string,
proof []byte,
clientState exported.ClientState,
) error {
merkleProof, provingConsensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

clientPrefixedPath := commitmenttypes.NewMerklePath(host.FullClientStatePath(counterpartyClientIdentifier))
path, err := commitmenttypes.ApplyPrefix(prefix, clientPrefixedPath)
if err != nil {
return err
}

if clientState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "client state cannot be empty")
}

_, ok := clientState.(*ClientState)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "invalid client type %T, expected %T", clientState, &ClientState{})
}

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

return merkleProof.VerifyMembership(cs.ProofSpecs, provingConsensusState.GetRoot(), path, bz)
}

// VerifyClientConsensusState verifies a proof of the consensus state of the
// Tendermint client stored on the target machine.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyClientConsensusState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
counterpartyClientIdentifier string,
consensusHeight exported.Height,
prefix exported.Prefix,
proof []byte,
consensusState exported.ConsensusState,
) error {
merkleProof, provingConsensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

clientPrefixedPath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(counterpartyClientIdentifier, consensusHeight))
path, err := commitmenttypes.ApplyPrefix(prefix, clientPrefixedPath)
if err != nil {
return err
}

if consensusState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be empty")
}

_, ok := consensusState.(*ConsensusState)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, &ConsensusState{})
}

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

if err := merkleProof.VerifyMembership(cs.ProofSpecs, provingConsensusState.GetRoot(), path, bz); err != nil {
return err
}

return nil
}

// VerifyConnectionState verifies a proof of the connection state of the
// specified connection end stored on the target machine.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyConnectionState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
proof []byte,
connectionID string,
connectionEnd exported.ConnectionI,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

connectionPath := commitmenttypes.NewMerklePath(host.ConnectionPath(connectionID))
path, err := commitmenttypes.ApplyPrefix(prefix, connectionPath)
if err != nil {
return err
}

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

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

if err := merkleProof.VerifyMembership(cs.ProofSpecs, consensusState.GetRoot(), path, bz); err != nil {
return err
}

return nil
}

// VerifyChannelState verifies a proof of the channel state of the specified
// channel end, under the specified port, stored on the target machine.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyChannelState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
channel exported.ChannelI,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

channelPath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, channelPath)
if err != nil {
return err
}

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

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

if err := merkleProof.VerifyMembership(cs.ProofSpecs, consensusState.GetRoot(), path, bz); err != nil {
return err
}

return nil
}

// VerifyPacketCommitment verifies a proof of an outgoing packet commitment at
// the specified port, specified channel, and specified sequence.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyPacketCommitment(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
commitmentBytes []byte,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

// check delay period has passed
if err := verifyDelayPeriodPassed(ctx, store, height, delayTimePeriod, delayBlockPeriod); err != nil {
return err
}

commitmentPath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, commitmentPath)
if err != nil {
return err
}

if err := merkleProof.VerifyMembership(cs.ProofSpecs, consensusState.GetRoot(), path, commitmentBytes); err != nil {
return err
}

return nil
}

// VerifyPacketAcknowledgement verifies a proof of an incoming packet
// acknowledgement at the specified port, specified channel, and specified sequence.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyPacketAcknowledgement(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
acknowledgement []byte,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

// check delay period has passed
if err := verifyDelayPeriodPassed(ctx, store, height, delayTimePeriod, delayBlockPeriod); err != nil {
return err
}

ackPath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, ackPath)
if err != nil {
return err
}

if err := merkleProof.VerifyMembership(cs.ProofSpecs, consensusState.GetRoot(), path, channeltypes.CommitAcknowledgement(acknowledgement)); err != nil {
return err
}

return nil
}

// VerifyPacketReceiptAbsence verifies a proof of the absence of an
// incoming packet receipt at the specified port, specified channel, and
// specified sequence.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyPacketReceiptAbsence(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
sequence uint64,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

// check delay period has passed
if err := verifyDelayPeriodPassed(ctx, store, height, delayTimePeriod, delayBlockPeriod); err != nil {
return err
}

receiptPath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, receiptPath)
if err != nil {
return err
}

if err := merkleProof.VerifyNonMembership(cs.ProofSpecs, consensusState.GetRoot(), path); err != nil {
return err
}

return nil
}

// VerifyNextSequenceRecv verifies a proof of the next sequence number to be
// received of the specified channel at the specified port.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func (cs ClientState) VerifyNextSequenceRecv(
ctx sdk.Context,
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
delayTimePeriod uint64,
delayBlockPeriod uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
nextSequenceRecv uint64,
) error {
merkleProof, consensusState, err := produceVerificationArgs(store, cdc, cs, height, prefix, proof)
if err != nil {
return err
}

// check delay period has passed
if err := verifyDelayPeriodPassed(ctx, store, height, delayTimePeriod, delayBlockPeriod); err != nil {
return err
}

nextSequenceRecvPath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, nextSequenceRecvPath)
if err != nil {
return err
}

bz := sdk.Uint64ToBigEndian(nextSequenceRecv)

if err := merkleProof.VerifyMembership(cs.ProofSpecs, consensusState.GetRoot(), path, bz); err != nil {
return err
}

return nil
}

// VerifyMembership is a generic proof verification method which verifies a proof of the existence of a value at a given CommitmentPath at the specified height.
// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
func (cs ClientState) VerifyMembership(
Expand Down Expand Up @@ -648,47 +318,3 @@ func verifyDelayPeriodPassed(ctx sdk.Context, store sdk.KVStore, proofHeight exp

return nil
}

// produceVerificationArgs perfoms the basic checks on the arguments that are
// shared between the verification functions and returns the unmarshalled
// merkle proof, the consensus state and an error if one occurred.
// TODO: remove, https://github.com/cosmos/ibc-go/issues/1294
func produceVerificationArgs(
store sdk.KVStore,
cdc codec.BinaryCodec,
cs ClientState,
height exported.Height,
prefix exported.Prefix,
proof []byte,
) (merkleProof commitmenttypes.MerkleProof, consensusState *ConsensusState, err error) {
if cs.GetLatestHeight().LT(height) {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"client state height < proof height (%d < %d), please ensure the client has been updated", cs.GetLatestHeight(), height,
)
}

if prefix == nil {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrap(commitmenttypes.ErrInvalidPrefix, "prefix cannot be empty")
}

_, ok := prefix.(*commitmenttypes.MerklePrefix)
if !ok {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrapf(commitmenttypes.ErrInvalidPrefix, "invalid prefix type %T, expected *MerklePrefix", prefix)
}

if proof == nil {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "proof cannot be empty")
}

if err = cdc.Unmarshal(proof, &merkleProof); err != nil {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "failed to unmarshal proof into commitment merkle proof")
}

consensusState, found := GetConsensusState(store, cdc, height)
if !found {
return commitmenttypes.MerkleProof{}, nil, sdkerrors.Wrap(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client")
}

return merkleProof, consensusState, nil
}
Loading

0 comments on commit a9fbba8

Please sign in to comment.