Skip to content

Commit

Permalink
Implement AllChannels Query
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Mar 4, 2021
1 parent 9bf2206 commit 4e2c4cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
21 changes: 15 additions & 6 deletions x/wasm/internal/keeper/query_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,24 @@ func IBCQuerier(wasm *Keeper, channelKeeper types.ChannelKeeper) func(ctx sdk.Co
}
return json.Marshal(res)
}
// I just discovered we cannot query by port (how odd since they store it like this)
// Anyway, I will allow Port as an optional filter
// TODO: let's revisit this query (if needed, if we should change it)
if request.ListChannels != nil {
portID := request.ListChannels.PortID
if portID == "" {
contractInfo := wasm.GetContractInfo(ctx, caller)
portID = contractInfo.IBCPortID
}
// TODO: query the channels for this port
var channels wasmvmtypes.IBCEndpoints
channelKeeper.IterateChannels(ctx, func(ch types.IdentifiedChannel) bool {
if portID == "" || portID == ch.PortId {
newChan := wasmvmtypes.IBCEndpoint{
PortID: ch.PortId,
ChannelID: ch.ChannelId,
}
channels = append(channels, newChan)
}
return false
})
res := wasmvmtypes.ListChannelsResponse{
Channels: wasmvmtypes.IBCEndpoints{},
Channels: channels,
}
return json.Marshal(res)
}
Expand Down
19 changes: 19 additions & 0 deletions x/wasm/internal/keeper/wasmtesting/mock_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type MockChannelKeeper struct {
GetNextSequenceSendFn func(ctx sdk.Context, portID, channelID string) (uint64, bool)
SendPacketFn func(ctx sdk.Context, channelCap *capabilitytypes.Capability, packet ibcexported.PacketI) error
ChanCloseInitFn func(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
GetAllChannelsFn func(ctx sdk.Context) []channeltypes.IdentifiedChannel
}

func (m *MockChannelKeeper) GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
Expand All @@ -21,6 +22,24 @@ func (m *MockChannelKeeper) GetChannel(ctx sdk.Context, srcPort, srcChan string)
return m.GetChannelFn(ctx, srcPort, srcChan)
}

func (m *MockChannelKeeper) GetAllChannels(ctx sdk.Context) []channeltypes.IdentifiedChannel {
if m.GetAllChannelsFn == nil {
panic("not supposed to be called!")
}
return m.GetAllChannelsFn(ctx)
}

// Auto-implemented from GetAllChannels data
func (m *MockChannelKeeper) IterateChannels(ctx sdk.Context, cb func(channeltypes.IdentifiedChannel) bool) {
channels := m.GetAllChannels(ctx)
for _, channel := range channels {
stop := cb(channel)
if stop {
break
}
}
}

func (m *MockChannelKeeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) {
if m.GetNextSequenceSendFn == nil {
panic("not supposed to be called!")
Expand Down
4 changes: 4 additions & 0 deletions x/wasm/internal/types/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ type ChannelKeeper interface {
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
SendPacket(ctx sdk.Context, channelCap *capabilitytypes.Capability, packet ibcexported.PacketI) error
ChanCloseInit(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
GetAllChannels(ctx sdk.Context) (channels []channeltypes.IdentifiedChannel)
IterateChannels(ctx sdk.Context, cb func(channeltypes.IdentifiedChannel) bool)
}

type IdentifiedChannel = channeltypes.IdentifiedChannel

// ClientKeeper defines the expected IBC client keeper
type ClientKeeper interface {
GetClientConsensusState(ctx sdk.Context, clientID string) (connection ibcexported.ConsensusState, found bool)
Expand Down

0 comments on commit 4e2c4cb

Please sign in to comment.