From 3016668d29e34368479547f25a26eff02c1eecb6 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 10:14:06 +0200 Subject: [PATCH 1/3] Add failing tests showing nothing filtered now --- x/wasm/keeper/query_plugins_test.go | 74 ++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/x/wasm/keeper/query_plugins_test.go b/x/wasm/keeper/query_plugins_test.go index 9baa9aa428..57ac72f3aa 100644 --- a/x/wasm/keeper/query_plugins_test.go +++ b/x/wasm/keeper/query_plugins_test.go @@ -14,6 +14,7 @@ import ( func TestIBCQuerier(t *testing.T) { myExampleChannels := []channeltypes.IdentifiedChannel{ + // this is returned { State: channeltypes.OPEN, Ordering: channeltypes.ORDERED, @@ -26,9 +27,22 @@ func TestIBCQuerier(t *testing.T) { PortId: "myPortID", ChannelId: "myChannelID", }, + // this is filtered out { State: channeltypes.INIT, Ordering: channeltypes.UNORDERED, + Counterparty: channeltypes.Counterparty{ + PortId: "foobar", + }, + ConnectionHops: []string{"one"}, + Version: "initversion", + PortId: "initPortID", + ChannelId: "initChannelID", + }, + // this is returned + { + State: channeltypes.OPEN, + Ordering: channeltypes.UNORDERED, Counterparty: channeltypes.Counterparty{ PortId: "otherCounterPartyPortID", ChannelId: "otherCounterPartyChannelID", @@ -38,6 +52,19 @@ func TestIBCQuerier(t *testing.T) { PortId: "otherPortID", ChannelId: "otherChannelID", }, + // this is filtered out + { + State: channeltypes.CLOSED, + Ordering: channeltypes.ORDERED, + Counterparty: channeltypes.Counterparty{ + PortId: "super", + ChannelId: "duper", + }, + ConnectionHops: []string{"no-more"}, + Version: "closedVersion", + PortId: "closedPortID", + ChannelId: "closedChannelID", + }, } specs := map[string]struct { srcQuery *wasmvmtypes.IBCQuery @@ -183,7 +210,7 @@ func TestIBCQuerier(t *testing.T) { channelKeeper: &wasmtesting.MockChannelKeeper{ GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { return channeltypes.Channel{ - State: channeltypes.INIT, + State: channeltypes.OPEN, Ordering: channeltypes.UNORDERED, Counterparty: channeltypes.Counterparty{ PortId: "counterPartyPortID", @@ -210,6 +237,51 @@ func TestIBCQuerier(t *testing.T) { } }`, }, + "query channel in init state": { + srcQuery: &wasmvmtypes.IBCQuery{ + Channel: &wasmvmtypes.ChannelQuery{ + PortID: "myQueryPortID", + ChannelID: "myQueryChannelID", + }, + }, + channelKeeper: &wasmtesting.MockChannelKeeper{ + GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { + return channeltypes.Channel{ + State: channeltypes.INIT, + Ordering: channeltypes.UNORDERED, + Counterparty: channeltypes.Counterparty{ + PortId: "foobar", + }, + ConnectionHops: []string{"one"}, + Version: "initversion", + }, true + }, + }, + expJsonResult: "{}", + }, + "query channel in closed state": { + srcQuery: &wasmvmtypes.IBCQuery{ + Channel: &wasmvmtypes.ChannelQuery{ + PortID: "myQueryPortID", + ChannelID: "myQueryChannelID", + }, + }, + channelKeeper: &wasmtesting.MockChannelKeeper{ + GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { + return channeltypes.Channel{ + State: channeltypes.CLOSED, + Ordering: channeltypes.ORDERED, + Counterparty: channeltypes.Counterparty{ + PortId: "super", + ChannelId: "duper", + }, + ConnectionHops: []string{"no-more"}, + Version: "closedVersion", + }, true + }, + }, + expJsonResult: "{}", + }, "query channel - empty result": { srcQuery: &wasmvmtypes.IBCQuery{ Channel: &wasmvmtypes.ChannelQuery{ From a428d5c6054a4dac4929ce48e4e0ac409856b18b Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 10:17:24 +0200 Subject: [PATCH 2/3] Add filtering to logic, tests pass --- x/wasm/keeper/query_plugins.go | 6 ++++-- x/wasm/keeper/query_plugins_test.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/x/wasm/keeper/query_plugins.go b/x/wasm/keeper/query_plugins.go index fff7953dc3..cba21cdfc7 100644 --- a/x/wasm/keeper/query_plugins.go +++ b/x/wasm/keeper/query_plugins.go @@ -198,7 +198,8 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper) portID := request.ListChannels.PortID channels := make(wasmvmtypes.IBCChannels, 0) channelKeeper.IterateChannels(ctx, func(ch channeltypes.IdentifiedChannel) bool { - if portID == "" || portID == ch.PortId { + // it must match the port and be in open state + if (portID == "" || portID == ch.PortId) && ch.State == channeltypes.OPEN { newChan := wasmvmtypes.IBCChannel{ Endpoint: wasmvmtypes.IBCEndpoint{ PortID: ch.PortId, @@ -230,7 +231,8 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper) } got, found := channelKeeper.GetChannel(ctx, portID, channelID) var channel *wasmvmtypes.IBCChannel - if found { + // it must be in open state + if found && got.State == channeltypes.OPEN { channel = &wasmvmtypes.IBCChannel{ Endpoint: wasmvmtypes.IBCEndpoint{ PortID: portID, diff --git a/x/wasm/keeper/query_plugins_test.go b/x/wasm/keeper/query_plugins_test.go index 57ac72f3aa..e9727b588d 100644 --- a/x/wasm/keeper/query_plugins_test.go +++ b/x/wasm/keeper/query_plugins_test.go @@ -171,7 +171,7 @@ func TestIBCQuerier(t *testing.T) { channelKeeper: &wasmtesting.MockChannelKeeper{ GetChannelFn: func(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { return channeltypes.Channel{ - State: channeltypes.INIT, + State: channeltypes.OPEN, Ordering: channeltypes.UNORDERED, Counterparty: channeltypes.Counterparty{ PortId: "counterPartyPortID", From 70191a73dc8e8b407f3345eaea8a73970b40cebd Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 28 Jul 2021 10:20:47 +0200 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7f26cc4bb..a899fcf031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ **Implemented Enhancements:** - Reject invalid events/attributes returned from contracts [\#560](https://github.com/CosmWasm/wasmd/pull/560) +- IBC Query methods from Wasm contracts only return OPEN channels [\#568](https://github.com/CosmWasm/wasmd/pull/568) [Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.17.0...HEAD)