Skip to content

Commit

Permalink
imp: remove Route method in favour of Verify(Non)Membership on cl…
Browse files Browse the repository at this point in the history
…ient keeper (#6760)

* chore: pull out get light client module to helper methods (#6712)

* chore: pull out get light client module to helper methods

* another place to use getLightClientModuleRoute

* use getLightClientModule in other 02-client functions

* lint

---------

Co-authored-by: Carlos Rodriguez <[email protected]>

* add verify(non)membership functions to client keeper and removed route from interface

* delete file

* use VerifyMembershipProof function name until #6775

* Update modules/core/02-client/keeper/keeper.go

Co-authored-by: DimitrisJim <[email protected]>

---------

Co-authored-by: Gjermund Garaba <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent 4499cd8 commit b62e7d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 117 deletions.
26 changes: 9 additions & 17 deletions modules/core/02-client/keeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ func (k *Keeper) CreateClient(ctx sdk.Context, clientType string, clientState, c
return "", errorsmod.Wrapf(types.ErrInvalidClientType, "cannot create client of type: %s", clientType)
}

params := k.GetParams(ctx)
if !params.IsAllowedClient(clientType) {
return "", errorsmod.Wrapf(
types.ErrInvalidClientType,
"client state type %s is not registered in the allowlist", clientType,
)
}

clientID := k.GenerateClientIdentifier(ctx, clientType)

clientModule, found := k.router.GetRoute(clientID)
if !found {
return "", errorsmod.Wrap(types.ErrRouteNotFound, clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return "", err
}

if err := clientModule.Initialize(ctx, clientID, clientState, consensusState); err != nil {
Expand Down Expand Up @@ -70,9 +62,9 @@ func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg export
return errorsmod.Wrapf(err, "unable to parse client identifier %s", clientID)
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return errorsmod.Wrap(types.ErrRouteNotFound, clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return err
}

if err := clientModule.VerifyClientMessage(ctx, clientID, clientMsg); err != nil {
Expand Down Expand Up @@ -136,9 +128,9 @@ func (k *Keeper) UpgradeClient(
return errorsmod.Wrapf(err, "unable to parse client identifier %s", clientID)
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return errorsmod.Wrap(types.ErrRouteNotFound, clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return err
}

if err := clientModule.VerifyUpgradeAndUpdateState(ctx, clientID, upgradedClient, upgradedConsState, upgradeClientProof, upgradeConsensusStateProof); err != nil {
Expand Down
81 changes: 48 additions & 33 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ func (k *Keeper) HasClientConsensusState(ctx sdk.Context, clientID string, heigh

// GetLatestClientConsensusState gets the latest ConsensusState stored for a given client
func (k *Keeper) GetLatestClientConsensusState(ctx sdk.Context, clientID string) (exported.ConsensusState, bool) {
clientModule, found := k.router.GetRoute(clientID)
if !found {
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return nil, false
}

Expand All @@ -318,6 +318,26 @@ func (k *Keeper) ValidateSelfClient(ctx sdk.Context, clientState exported.Client
return k.consensusHost.ValidateSelfClient(ctx, clientState)
}

// VerifyMembershipProof retrieves the light client module for the clientID and verifies the proof of the existence of a key-value pair at a specified height.
func (k *Keeper) VerifyMembershipProof(ctx sdk.Context, clientID string, height exported.Height, delayTimePeriod uint64, delayBlockPeriod uint64, proof []byte, path exported.Path, value []byte) error {
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return err
}

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

// VerifyNonMembership retrieves the light client module for the clientID and verifies the absence of a given key at a specified height.
func (k *Keeper) VerifyNonMembership(ctx sdk.Context, clientID string, height exported.Height, delayTimePeriod uint64, delayBlockPeriod uint64, proof []byte, path exported.Path) error {
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return err
}

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

// GetUpgradePlan executes the upgrade keeper GetUpgradePlan function.
func (k *Keeper) GetUpgradePlan(ctx sdk.Context) (upgradetypes.Plan, error) {
return k.upgradeKeeper.GetUpgradePlan(ctx)
Expand Down Expand Up @@ -383,40 +403,22 @@ func (k *Keeper) ClientStore(ctx sdk.Context, clientID string) storetypes.KVStor
// GetClientStatus returns the status for a client state given a client identifier. If the client type is not in the allowed
// clients param field, Unauthorized is returned, otherwise the client state status is returned.
func (k *Keeper) GetClientStatus(ctx sdk.Context, clientID string) exported.Status {
clientType, _, err := types.ParseClientIdentifier(clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return exported.Unauthorized
}

if !k.GetParams(ctx).IsAllowedClient(clientType) {
return exported.Unauthorized
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return exported.Unauthorized
}

return clientModule.Status(ctx, clientID)
}

// GetClientLatestHeight returns the latest height of a client state for a given client identifier. If the client type is not in the allowed
// clients param field, a zero value height is returned, otherwise the client state latest height is returned.
func (k *Keeper) GetClientLatestHeight(ctx sdk.Context, clientID string) types.Height {
clientType, _, err := types.ParseClientIdentifier(clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return types.ZeroHeight()
}

if !k.GetParams(ctx).IsAllowedClient(clientType) {
return types.ZeroHeight()
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return types.ZeroHeight()
}

var latestHeight types.Height
latestHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
if !ok {
Expand All @@ -427,18 +429,9 @@ func (k *Keeper) GetClientLatestHeight(ctx sdk.Context, clientID string) types.H

// GetClientTimestampAtHeight returns the timestamp in nanoseconds of the consensus state at the given height.
func (k *Keeper) GetClientTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error) {
clientType, _, err := types.ParseClientIdentifier(clientID)
clientModule, err := k.getLightClientModule(ctx, clientID)
if err != nil {
return 0, errorsmod.Wrapf(err, "clientID (%s)", clientID)
}

if !k.GetParams(ctx).IsAllowedClient(clientType) {
return 0, errorsmod.Wrapf(types.ErrInvalidClientType, "client state type %s is not registered in the allowlist", clientType)
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return 0, errorsmod.Wrap(types.ErrRouteNotFound, clientType)
return 0, err
}

return clientModule.TimestampAtHeight(ctx, clientID, height)
Expand Down Expand Up @@ -493,3 +486,25 @@ func (k *Keeper) ScheduleIBCSoftwareUpgrade(ctx sdk.Context, plan upgradetypes.P

return nil
}

// getLightClientModule checks that the clientType of clientID is allowed and returns the corresponding light client module
func (k *Keeper) getLightClientModule(ctx sdk.Context, clientID string) (exported.LightClientModule, error) {
clientType, _, err := types.ParseClientIdentifier(clientID)
if err != nil {
return nil, errorsmod.Wrapf(err, "unable to parse client identifier %s", clientID)
}

if !k.GetParams(ctx).IsAllowedClient(clientType) {
return nil, errorsmod.Wrapf(
types.ErrInvalidClientType,
"client (%s) type %s is not in the allowed client list", clientID, clientType,
)
}

clientModule, found := k.router.GetRoute(clientID)
if !found {
return nil, errorsmod.Wrap(types.ErrRouteNotFound, clientID)
}

return clientModule, nil
}
77 changes: 11 additions & 66 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func (k *Keeper) VerifyClientState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.FullClientStateKey(connection.Counterparty.ClientId))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -45,7 +40,7 @@ func (k *Keeper) VerifyClientState(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
Expand All @@ -71,11 +66,6 @@ func (k *Keeper) VerifyClientConsensusState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStateKey(connection.Counterparty.ClientId, consensusHeight))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -87,7 +77,7 @@ func (k *Keeper) VerifyClientConsensusState(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
Expand All @@ -113,11 +103,6 @@ func (k *Keeper) VerifyConnectionState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ConnectionKey(connectionID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -129,7 +114,7 @@ func (k *Keeper) VerifyConnectionState(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
Expand All @@ -156,11 +141,6 @@ func (k *Keeper) VerifyChannelState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -172,7 +152,7 @@ func (k *Keeper) VerifyChannelState(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
Expand Down Expand Up @@ -200,11 +180,6 @@ func (k *Keeper) VerifyPacketCommitment(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand All @@ -215,7 +190,7 @@ func (k *Keeper) VerifyPacketCommitment(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height, timeDelay, blockDelay, proof, merklePath, commitmentBytes,
); err != nil {
return errorsmod.Wrapf(err, "failed packet commitment verification for client (%s)", clientID)
Expand All @@ -241,11 +216,6 @@ func (k *Keeper) VerifyPacketAcknowledgement(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand All @@ -256,7 +226,7 @@ func (k *Keeper) VerifyPacketAcknowledgement(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height, timeDelay, blockDelay,
proof, merklePath, channeltypes.CommitAcknowledgement(acknowledgement),
); err != nil {
Expand All @@ -283,27 +253,17 @@ func (k *Keeper) VerifyPacketReceiptAbsence(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientType, _, err := clienttypes.ParseClientIdentifier(clientID)
if err != nil {
return err
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientType)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketReceiptKey(portID, channelID, sequence))
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
return err
}

if err := clientModule.VerifyNonMembership(
if err := k.clientKeeper.VerifyNonMembership(
ctx, clientID, height, timeDelay, blockDelay, proof, merklePath,
); err != nil {
return errorsmod.Wrapf(err, "failed packet receipt absence verification for client (%s)", clientID)
Expand All @@ -328,11 +288,6 @@ func (k *Keeper) VerifyNextSequenceRecv(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

// get time and block delays
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)
Expand All @@ -343,7 +298,7 @@ func (k *Keeper) VerifyNextSequenceRecv(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
timeDelay, blockDelay,
proof, merklePath, sdk.Uint64ToBigEndian(nextSequenceRecv),
Expand All @@ -369,11 +324,6 @@ func (k *Keeper) VerifyChannelUpgradeError(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -385,7 +335,7 @@ func (k *Keeper) VerifyChannelUpgradeError(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
Expand All @@ -411,11 +361,6 @@ func (k *Keeper) VerifyChannelUpgrade(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

clientModule, found := k.clientKeeper.Route(clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrRouteNotFound, clientID)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeKey(portID, channelID))
merklePath, err := commitmenttypes.ApplyPrefix(connection.Counterparty.Prefix, merklePath)
if err != nil {
Expand All @@ -427,7 +372,7 @@ func (k *Keeper) VerifyChannelUpgrade(
return err
}

if err := clientModule.VerifyMembership(
if err := k.clientKeeper.VerifyMembershipProof(
ctx, clientID, proofHeight,
0, 0, // skip delay period checks for non-packet processing verification
upgradeProof, merklePath, bz,
Expand Down
Loading

0 comments on commit b62e7d4

Please sign in to comment.