Skip to content

Commit c84a086

Browse files
authored
Merge pull request cosmos#568 from CosmWasm/556-ibc-query-only-open-channels
Ibc query only returns open channels
2 parents a012e00 + 70191a7 commit c84a086

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
**Implemented Enhancements:**
66
- Reject invalid events/attributes returned from contracts [\#560](https://github.com/CosmWasm/wasmd/pull/560)
7+
- IBC Query methods from Wasm contracts only return OPEN channels [\#568](https://github.com/CosmWasm/wasmd/pull/568)
78

89
[Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.17.0...HEAD)
910

x/wasm/keeper/query_plugins.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper)
198198
portID := request.ListChannels.PortID
199199
channels := make(wasmvmtypes.IBCChannels, 0)
200200
channelKeeper.IterateChannels(ctx, func(ch channeltypes.IdentifiedChannel) bool {
201-
if portID == "" || portID == ch.PortId {
201+
// it must match the port and be in open state
202+
if (portID == "" || portID == ch.PortId) && ch.State == channeltypes.OPEN {
202203
newChan := wasmvmtypes.IBCChannel{
203204
Endpoint: wasmvmtypes.IBCEndpoint{
204205
PortID: ch.PortId,
@@ -230,7 +231,8 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper)
230231
}
231232
got, found := channelKeeper.GetChannel(ctx, portID, channelID)
232233
var channel *wasmvmtypes.IBCChannel
233-
if found {
234+
// it must be in open state
235+
if found && got.State == channeltypes.OPEN {
234236
channel = &wasmvmtypes.IBCChannel{
235237
Endpoint: wasmvmtypes.IBCEndpoint{
236238
PortID: portID,

x/wasm/keeper/query_plugins_test.go

+74-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
func TestIBCQuerier(t *testing.T) {
1616
myExampleChannels := []channeltypes.IdentifiedChannel{
17+
// this is returned
1718
{
1819
State: channeltypes.OPEN,
1920
Ordering: channeltypes.ORDERED,
@@ -26,9 +27,22 @@ func TestIBCQuerier(t *testing.T) {
2627
PortId: "myPortID",
2728
ChannelId: "myChannelID",
2829
},
30+
// this is filtered out
2931
{
3032
State: channeltypes.INIT,
3133
Ordering: channeltypes.UNORDERED,
34+
Counterparty: channeltypes.Counterparty{
35+
PortId: "foobar",
36+
},
37+
ConnectionHops: []string{"one"},
38+
Version: "initversion",
39+
PortId: "initPortID",
40+
ChannelId: "initChannelID",
41+
},
42+
// this is returned
43+
{
44+
State: channeltypes.OPEN,
45+
Ordering: channeltypes.UNORDERED,
3246
Counterparty: channeltypes.Counterparty{
3347
PortId: "otherCounterPartyPortID",
3448
ChannelId: "otherCounterPartyChannelID",
@@ -38,6 +52,19 @@ func TestIBCQuerier(t *testing.T) {
3852
PortId: "otherPortID",
3953
ChannelId: "otherChannelID",
4054
},
55+
// this is filtered out
56+
{
57+
State: channeltypes.CLOSED,
58+
Ordering: channeltypes.ORDERED,
59+
Counterparty: channeltypes.Counterparty{
60+
PortId: "super",
61+
ChannelId: "duper",
62+
},
63+
ConnectionHops: []string{"no-more"},
64+
Version: "closedVersion",
65+
PortId: "closedPortID",
66+
ChannelId: "closedChannelID",
67+
},
4168
}
4269
specs := map[string]struct {
4370
srcQuery *wasmvmtypes.IBCQuery
@@ -144,7 +171,7 @@ func TestIBCQuerier(t *testing.T) {
144171
channelKeeper: &wasmtesting.MockChannelKeeper{
145172
GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
146173
return channeltypes.Channel{
147-
State: channeltypes.INIT,
174+
State: channeltypes.OPEN,
148175
Ordering: channeltypes.UNORDERED,
149176
Counterparty: channeltypes.Counterparty{
150177
PortId: "counterPartyPortID",
@@ -183,7 +210,7 @@ func TestIBCQuerier(t *testing.T) {
183210
channelKeeper: &wasmtesting.MockChannelKeeper{
184211
GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
185212
return channeltypes.Channel{
186-
State: channeltypes.INIT,
213+
State: channeltypes.OPEN,
187214
Ordering: channeltypes.UNORDERED,
188215
Counterparty: channeltypes.Counterparty{
189216
PortId: "counterPartyPortID",
@@ -210,6 +237,51 @@ func TestIBCQuerier(t *testing.T) {
210237
}
211238
}`,
212239
},
240+
"query channel in init state": {
241+
srcQuery: &wasmvmtypes.IBCQuery{
242+
Channel: &wasmvmtypes.ChannelQuery{
243+
PortID: "myQueryPortID",
244+
ChannelID: "myQueryChannelID",
245+
},
246+
},
247+
channelKeeper: &wasmtesting.MockChannelKeeper{
248+
GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
249+
return channeltypes.Channel{
250+
State: channeltypes.INIT,
251+
Ordering: channeltypes.UNORDERED,
252+
Counterparty: channeltypes.Counterparty{
253+
PortId: "foobar",
254+
},
255+
ConnectionHops: []string{"one"},
256+
Version: "initversion",
257+
}, true
258+
},
259+
},
260+
expJsonResult: "{}",
261+
},
262+
"query channel in closed state": {
263+
srcQuery: &wasmvmtypes.IBCQuery{
264+
Channel: &wasmvmtypes.ChannelQuery{
265+
PortID: "myQueryPortID",
266+
ChannelID: "myQueryChannelID",
267+
},
268+
},
269+
channelKeeper: &wasmtesting.MockChannelKeeper{
270+
GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) {
271+
return channeltypes.Channel{
272+
State: channeltypes.CLOSED,
273+
Ordering: channeltypes.ORDERED,
274+
Counterparty: channeltypes.Counterparty{
275+
PortId: "super",
276+
ChannelId: "duper",
277+
},
278+
ConnectionHops: []string{"no-more"},
279+
Version: "closedVersion",
280+
}, true
281+
},
282+
},
283+
expJsonResult: "{}",
284+
},
213285
"query channel - empty result": {
214286
srcQuery: &wasmvmtypes.IBCQuery{
215287
Channel: &wasmvmtypes.ChannelQuery{

0 commit comments

Comments
 (0)