Skip to content

Commit

Permalink
feat(lightclient): Add query for getting canonical channel (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt authored Dec 10, 2024
1 parent caca30c commit 58cbe8d
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ proposal.json
.go-version
**/testdata/rapid/
**/__debug_bin*
.aider*
.aider*
repomix.config.json
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func (a *AppKeepers) InitKeepers(
appCodec,
a.keys[lightclientmoduletypes.StoreKey],
a.IBCKeeper.ClientKeeper,
a.IBCKeeper.ChannelKeeper,
a.SequencerKeeper,
a.RollappKeeper,
)
Expand Down
14 changes: 14 additions & 0 deletions proto/dymensionxyz/dymension/lightclient/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ service Query {
rpc ExpectedClientState(QueryExpectedClientStateRequest) returns (QueryExpectedClientStateResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/lightclient/expectedclientstate";
}
rpc RollappCanonChannel(QueryRollappCanonChannelRequest) returns (QueryRollappCanonChannelResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/lightclient/canon_channel/{rollappId}";
}
}

message QueryExpectedClientStateRequest {}
Expand All @@ -32,3 +35,14 @@ message QueryExpectedClientStateResponse {
// client state
google.protobuf.Any client_state = 2 [(gogoproto.moretags) = "yaml:\"client_state\""];
}

message QueryRollappCanonChannelRequest {
string rollappId = 1;
}

message QueryRollappCanonChannelResponse {
// hub side
string hub_channel_id = 1;
// rollapp side ('counterparty')
string rollapp_channel_id = 2;
}
1 change: 1 addition & 0 deletions testutil/keeper/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func LightClientKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
cdc,
storeKey,
mockIBCKeeper,
nil,
mockSequencerKeeper,
mockRollappKeeper,
)
Expand Down
6 changes: 6 additions & 0 deletions x/lightclient/ante/ibc_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"

ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand Down Expand Up @@ -106,6 +107,11 @@ type MockIBCChannelKeeper struct {
channelConnections map[string]ibcconnectiontypes.ConnectionEnd
}

func (m *MockIBCChannelKeeper) GetChannel(ctx sdk.Context, portID, channelID string) (channeltypes.Channel, bool) {
// TODO implement me
panic("implement me")
}

func NewMockIBCChannelKeeper(connections map[string]ibcconnectiontypes.ConnectionEnd) *MockIBCChannelKeeper {
return &MockIBCChannelKeeper{
channelConnections: connections,
Expand Down
24 changes: 24 additions & 0 deletions x/lightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymension/v3/internal/collcompat"
Expand All @@ -36,6 +37,7 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
ibcClientKeeper types.IBCClientKeeperExpected
ibcChannelK types.IBCChannelKeeperExpected
SeqK types.SequencerKeeperExpected
rollappKeeper types.RollappKeeperExpected

Expand All @@ -57,6 +59,7 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ibcKeeper types.IBCClientKeeperExpected,
ibcChannelK types.IBCChannelKeeperExpected,
sequencerKeeper types.SequencerKeeperExpected,
rollappKeeper types.RollappKeeperExpected,
) *Keeper {
Expand All @@ -67,6 +70,7 @@ func NewKeeper(
cdc: cdc,
storeKey: storeKey,
ibcClientKeeper: ibcKeeper,
ibcChannelK: ibcChannelK,
SeqK: sequencerKeeper,
rollappKeeper: rollappKeeper,
headerSigners: collections.NewKeySet(
Expand Down Expand Up @@ -156,6 +160,26 @@ func (k Keeper) ExpectedClientState(context.Context, *types.QueryExpectedClientS
return &types.QueryExpectedClientStateResponse{ClientState: anyClient}, nil
}

// a convenience function to get both hub and rollapp channel ids from just the rollapp id
func (k Keeper) RollappCanonChannel(goCtx context.Context, req *types.QueryRollappCanonChannelRequest) (*types.QueryRollappCanonChannelResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ra, ok := k.rollappKeeper.GetRollapp(ctx, req.GetRollappId())
if !ok {
return nil, rollapptypes.ErrRollappNotFound
}
if ra.ChannelId == "" {
return nil, gerrc.ErrFailedPrecondition.Wrap("canonical channel not set on rollapp")
}
cha, ok := k.ibcChannelK.GetChannel(ctx, "transfer", ra.ChannelId)
if !ok {
return nil, gerrc.ErrInternal.Wrapf("channel: %s", ra.ChannelId)
}
return &types.QueryRollappCanonChannelResponse{
HubChannelId: ra.ChannelId,
RollappChannelId: cha.GetCounterparty().GetChannelID(),
}, nil
}

func (k Keeper) pruneSigners(ctx sdk.Context, client string, h uint64, isAbove bool) error {
var rng *collections.PairRange[string, uint64]
if isAbove {
Expand Down
2 changes: 2 additions & 0 deletions x/lightclient/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"

rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
Expand Down Expand Up @@ -34,5 +35,6 @@ type IBCClientKeeperExpected interface {
}

type IBCChannelKeeperExpected interface {
GetChannel(ctx sdk.Context, portID, channelID string) (types.Channel, bool)
GetChannelConnection(ctx sdk.Context, portID, channelID string) (string, exported.ConnectionI, error)
}
Loading

0 comments on commit 58cbe8d

Please sign in to comment.