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) 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 9baa9aa428..e9727b588d 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 @@ -144,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", @@ -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{