Skip to content

Commit e81d655

Browse files
committed
Start rework channel query
1 parent 09b5008 commit e81d655

File tree

4 files changed

+32
-159
lines changed

4 files changed

+32
-159
lines changed

x/wasm/keeper/query_plugins.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,29 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper)
249249
}
250250
if request.ListChannels != nil {
251251
portID := request.ListChannels.PortID
252-
channels := make(wasmvmtypes.IBCChannels, 0)
253-
channelKeeper.IterateChannels(ctx, func(ch channeltypes.IdentifiedChannel) bool {
254-
// it must match the port and be in open state
255-
if (portID == "" || portID == ch.PortId) && ch.State == channeltypes.OPEN {
256-
newChan := wasmvmtypes.IBCChannel{
257-
Endpoint: wasmvmtypes.IBCEndpoint{
258-
PortID: ch.PortId,
259-
ChannelID: ch.ChannelId,
260-
},
261-
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
262-
PortID: ch.Counterparty.PortId,
263-
ChannelID: ch.Counterparty.ChannelId,
264-
},
265-
Order: ch.Ordering.String(),
266-
Version: ch.Version,
267-
ConnectionID: ch.ConnectionHops[0],
268-
}
269-
channels = append(channels, newChan)
252+
if portID == "" {
253+
portID = wasm.GetContractInfo(ctx, caller).IBCPortID
254+
}
255+
gotChannels := channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
256+
channels := make(wasmvmtypes.IBCChannels, 0, len(gotChannels))
257+
for _, ch := range gotChannels {
258+
if ch.State != channeltypes.OPEN {
259+
continue
270260
}
271-
return false
272-
})
261+
channels = append(channels, wasmvmtypes.IBCChannel{
262+
Endpoint: wasmvmtypes.IBCEndpoint{
263+
PortID: ch.PortId,
264+
ChannelID: ch.ChannelId,
265+
},
266+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
267+
PortID: ch.Counterparty.PortId,
268+
ChannelID: ch.Counterparty.ChannelId,
269+
},
270+
Order: ch.Ordering.String(),
271+
Version: ch.Version,
272+
ConnectionID: ch.ConnectionHops[0],
273+
})
274+
}
273275
res := wasmvmtypes.ListChannelsResponse{
274276
Channels: channels,
275277
}

x/wasm/keeper/query_plugins_test.go

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,6 @@ import (
4040
)
4141

4242
func TestIBCQuerier(t *testing.T) {
43-
myExampleChannels := []channeltypes.IdentifiedChannel{
44-
// this is returned
45-
{
46-
State: channeltypes.OPEN,
47-
Ordering: channeltypes.ORDERED,
48-
Counterparty: channeltypes.Counterparty{
49-
PortId: "counterPartyPortID",
50-
ChannelId: "counterPartyChannelID",
51-
},
52-
ConnectionHops: []string{"one"},
53-
Version: "v1",
54-
PortId: "myPortID",
55-
ChannelId: "myChannelID",
56-
},
57-
// this is filtered out
58-
{
59-
State: channeltypes.INIT,
60-
Ordering: channeltypes.UNORDERED,
61-
Counterparty: channeltypes.Counterparty{
62-
PortId: "foobar",
63-
},
64-
ConnectionHops: []string{"one"},
65-
Version: "initversion",
66-
PortId: "initPortID",
67-
ChannelId: "initChannelID",
68-
},
69-
// this is returned
70-
{
71-
State: channeltypes.OPEN,
72-
Ordering: channeltypes.UNORDERED,
73-
Counterparty: channeltypes.Counterparty{
74-
PortId: "otherCounterPartyPortID",
75-
ChannelId: "otherCounterPartyChannelID",
76-
},
77-
ConnectionHops: []string{"other", "second"},
78-
Version: "otherVersion",
79-
PortId: "otherPortID",
80-
ChannelId: "otherChannelID",
81-
},
82-
// this is filtered out
83-
{
84-
State: channeltypes.CLOSED,
85-
Ordering: channeltypes.ORDERED,
86-
Counterparty: channeltypes.Counterparty{
87-
PortId: "super",
88-
ChannelId: "duper",
89-
},
90-
ConnectionHops: []string{"no-more"},
91-
Version: "closedVersion",
92-
PortId: "closedPortID",
93-
ChannelId: "closedChannelID",
94-
},
95-
}
9643
specs := map[string]struct {
9744
srcQuery *wasmvmtypes.IBCQuery
9845
wasmKeeper *mockWasmQueryKeeper
@@ -112,82 +59,6 @@ func TestIBCQuerier(t *testing.T) {
11259
channelKeeper: &wasmtesting.MockChannelKeeper{},
11360
expJSONResult: `{"port_id":"myIBCPortID"}`,
11461
},
115-
"query list channels - all": {
116-
srcQuery: &wasmvmtypes.IBCQuery{
117-
ListChannels: &wasmvmtypes.ListChannelsQuery{},
118-
},
119-
channelKeeper: &wasmtesting.MockChannelKeeper{
120-
IterateChannelsFn: wasmtesting.MockChannelKeeperIterator(myExampleChannels),
121-
},
122-
expJSONResult: `{
123-
"channels": [
124-
{
125-
"endpoint": {
126-
"port_id": "myPortID",
127-
"channel_id": "myChannelID"
128-
},
129-
"counterparty_endpoint": {
130-
"port_id": "counterPartyPortID",
131-
"channel_id": "counterPartyChannelID"
132-
},
133-
"order": "ORDER_ORDERED",
134-
"version": "v1",
135-
"connection_id": "one"
136-
},
137-
{
138-
"endpoint": {
139-
"port_id": "otherPortID",
140-
"channel_id": "otherChannelID"
141-
},
142-
"counterparty_endpoint": {
143-
"port_id": "otherCounterPartyPortID",
144-
"channel_id": "otherCounterPartyChannelID"
145-
},
146-
"order": "ORDER_UNORDERED",
147-
"version": "otherVersion",
148-
"connection_id": "other"
149-
}
150-
]
151-
}`,
152-
},
153-
"query list channels - filtered": {
154-
srcQuery: &wasmvmtypes.IBCQuery{
155-
ListChannels: &wasmvmtypes.ListChannelsQuery{
156-
PortID: "otherPortID",
157-
},
158-
},
159-
channelKeeper: &wasmtesting.MockChannelKeeper{
160-
IterateChannelsFn: wasmtesting.MockChannelKeeperIterator(myExampleChannels),
161-
},
162-
expJSONResult: `{
163-
"channels": [
164-
{
165-
"endpoint": {
166-
"port_id": "otherPortID",
167-
"channel_id": "otherChannelID"
168-
},
169-
"counterparty_endpoint": {
170-
"port_id": "otherCounterPartyPortID",
171-
"channel_id": "otherCounterPartyChannelID"
172-
},
173-
"order": "ORDER_UNORDERED",
174-
"version": "otherVersion",
175-
"connection_id": "other"
176-
}
177-
]
178-
}`,
179-
},
180-
"query list channels - filtered empty": {
181-
srcQuery: &wasmvmtypes.IBCQuery{
182-
ListChannels: &wasmvmtypes.ListChannelsQuery{
183-
PortID: "none-existing",
184-
},
185-
},
186-
channelKeeper: &wasmtesting.MockChannelKeeper{
187-
IterateChannelsFn: wasmtesting.MockChannelKeeperIterator(myExampleChannels),
188-
},
189-
expJSONResult: `{"channels": []}`,
190-
},
19162
"query channel": {
19263
srcQuery: &wasmvmtypes.IBCQuery{
19364
Channel: &wasmvmtypes.ChannelQuery{

x/wasm/keeper/wasmtesting/mock_keepers.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
type MockChannelKeeper struct {
14-
GetChannelFn func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool)
15-
GetNextSequenceSendFn func(ctx sdk.Context, portID, channelID string) (uint64, bool)
16-
ChanCloseInitFn func(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
17-
GetAllChannelsFn func(ctx sdk.Context) []channeltypes.IdentifiedChannel
18-
IterateChannelsFn func(ctx sdk.Context, cb func(channeltypes.IdentifiedChannel) bool)
19-
SetChannelFn func(ctx sdk.Context, portID, channelID string, channel channeltypes.Channel)
14+
GetChannelFn func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool)
15+
GetNextSequenceSendFn func(ctx sdk.Context, portID, channelID string) (uint64, bool)
16+
ChanCloseInitFn func(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
17+
GetAllChannelsFn func(ctx sdk.Context) []channeltypes.IdentifiedChannel
18+
SetChannelFn func(ctx sdk.Context, portID, channelID string, channel channeltypes.Channel)
19+
GetAllChannelsWithPortPrefixFn func(ctx sdk.Context, portPrefix string) []channeltypes.IdentifiedChannel
2020
}
2121

2222
func (m *MockChannelKeeper) GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
@@ -47,11 +47,11 @@ func (m *MockChannelKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID str
4747
return m.ChanCloseInitFn(ctx, portID, channelID, chanCap)
4848
}
4949

50-
func (m *MockChannelKeeper) IterateChannels(ctx sdk.Context, cb func(channeltypes.IdentifiedChannel) bool) {
51-
if m.IterateChannelsFn == nil {
50+
func (m *MockChannelKeeper) GetAllChannelsWithPortPrefix(ctx sdk.Context, portPrefix string) []channeltypes.IdentifiedChannel {
51+
if m.GetAllChannelsWithPortPrefixFn == nil {
5252
panic("not expected to be called")
5353
}
54-
m.IterateChannelsFn(ctx, cb)
54+
return m.GetAllChannelsWithPortPrefixFn(ctx, portPrefix)
5555
}
5656

5757
func (m *MockChannelKeeper) SetChannel(ctx sdk.Context, portID, channelID string, channel channeltypes.Channel) {

x/wasm/types/expected_keepers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ type ChannelKeeper interface {
8282
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
8383
ChanCloseInit(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
8484
GetAllChannels(ctx sdk.Context) (channels []channeltypes.IdentifiedChannel)
85-
IterateChannels(ctx sdk.Context, cb func(channeltypes.IdentifiedChannel) bool)
8685
SetChannel(ctx sdk.Context, portID, channelID string, channel channeltypes.Channel)
86+
GetAllChannelsWithPortPrefix(ctx sdk.Context, portPrefix string) []channeltypes.IdentifiedChannel
8787
}
8888

8989
// ICS4Wrapper defines the method for an IBC data package to be submitted.

0 commit comments

Comments
 (0)