Skip to content

Commit d5fa278

Browse files
committed
Handle query for non ibc contracts
1 parent 177c0a9 commit d5fa278

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

x/wasm/keeper/query_plugin_integration_test.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,13 @@ func TestIBCListChannelsQuery(t *testing.T) {
647647
cdc := MakeEncodingConfig(t).Codec
648648
pCtx, keepers := CreateTestInput(t, false, ReflectFeatures, WithMessageEncoders(reflectEncoders(cdc)), WithQueryPlugins(reflectPlugins()))
649649
keeper := keepers.WasmKeeper
650-
example := InstantiateReflectExampleContract(t, pCtx, keepers)
650+
nonIbcExample := InstantiateReflectExampleContract(t, pCtx, keepers)
651+
ibcExample := InstantiateReflectExampleContract(t, pCtx, keepers)
651652
// add an ibc port for testing
652653
myIBCPortID := "myValidPortID"
653-
cInfo := keeper.GetContractInfo(pCtx, example.Contract)
654+
cInfo := keeper.GetContractInfo(pCtx, ibcExample.Contract)
654655
cInfo.IBCPortID = myIBCPortID
655-
keeper.storeContractInfo(pCtx, example.Contract, cInfo)
656+
keeper.storeContractInfo(pCtx, ibcExample.Contract, cInfo)
656657
// store a random channel to be ignored in queries
657658
unusedChan := channeltypes.Channel{
658659
State: channeltypes.OPEN,
@@ -720,14 +721,16 @@ func TestIBCListChannelsQuery(t *testing.T) {
720721
noopSetup := func(t *testing.T, ctx sdk.Context) sdk.Context { return ctx }
721722

722723
specs := map[string]struct {
723-
setup func(t *testing.T, ctx sdk.Context) sdk.Context
724-
query *wasmvmtypes.IBCQuery
725-
expErr bool
726-
assert func(t *testing.T, d []byte)
724+
setup func(t *testing.T, ctx sdk.Context) sdk.Context
725+
contract sdk.AccAddress
726+
query *wasmvmtypes.IBCQuery
727+
expErr bool
728+
assert func(t *testing.T, d []byte)
727729
}{
728-
"only open channels - portID empty": {
729-
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
730-
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
730+
"open channels - with query portID empty": {
731+
contract: ibcExample.Contract,
732+
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
733+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
731734
assert: func(t *testing.T, d []byte) {
732735
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
733736
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
@@ -754,9 +757,10 @@ func TestIBCListChannelsQuery(t *testing.T) {
754757
assert.Equal(t, exp, rsp)
755758
},
756759
},
757-
"open channels - portID set to non contract addr": {
758-
setup: withChannelsStored("OtherPortID", myExampleChannels...),
759-
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{PortID: "OtherPortID"}},
760+
"open channels - with query portID passed": {
761+
contract: ibcExample.Contract,
762+
setup: withChannelsStored("OtherPortID", myExampleChannels...),
763+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{PortID: "OtherPortID"}},
760764
assert: func(t *testing.T, d []byte) {
761765
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
762766
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
@@ -783,9 +787,19 @@ func TestIBCListChannelsQuery(t *testing.T) {
783787
assert.Equal(t, exp, rsp)
784788
},
785789
},
786-
"no channels": {
787-
setup: noopSetup,
788-
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
790+
"non ibc contract - with query portID empty": {
791+
contract: nonIbcExample.Contract,
792+
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
793+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
794+
assert: func(t *testing.T, d []byte) {
795+
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
796+
assert.Nil(t, rsp.Channels)
797+
},
798+
},
799+
"no matching channels": {
800+
contract: ibcExample.Contract,
801+
setup: noopSetup,
802+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
789803
assert: func(t *testing.T, d []byte) {
790804
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
791805
assert.Empty(t, rsp.Channels)
@@ -803,7 +817,7 @@ func TestIBCListChannelsQuery(t *testing.T) {
803817
Request: &wasmvmtypes.QueryRequest{IBC: spec.query},
804818
},
805819
})
806-
simpleRes, gotErr := keeper.QuerySmart(ctx, example.Contract, queryBz)
820+
simpleRes, gotErr := keeper.QuerySmart(ctx, spec.contract, queryBz)
807821
if spec.expErr {
808822
require.Error(t, gotErr)
809823
return

x/wasm/keeper/query_plugins.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,28 +249,31 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper)
249249
}
250250
if request.ListChannels != nil {
251251
portID := request.ListChannels.PortID
252-
if portID == "" {
252+
if portID == "" { // then fallback to contract port address
253253
portID = wasm.GetContractInfo(ctx, caller).IBCPortID
254254
}
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
255+
var channels wasmvmtypes.IBCChannels
256+
if portID != "" { // then return empty list for non ibc contracts; no channels possible
257+
gotChannels := channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
258+
channels = make(wasmvmtypes.IBCChannels, 0, len(gotChannels))
259+
for _, ch := range gotChannels {
260+
if ch.State != channeltypes.OPEN {
261+
continue
262+
}
263+
channels = append(channels, wasmvmtypes.IBCChannel{
264+
Endpoint: wasmvmtypes.IBCEndpoint{
265+
PortID: ch.PortId,
266+
ChannelID: ch.ChannelId,
267+
},
268+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
269+
PortID: ch.Counterparty.PortId,
270+
ChannelID: ch.Counterparty.ChannelId,
271+
},
272+
Order: ch.Ordering.String(),
273+
Version: ch.Version,
274+
ConnectionID: ch.ConnectionHops[0],
275+
})
260276
}
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-
})
274277
}
275278
res := wasmvmtypes.ListChannelsResponse{
276279
Channels: channels,

0 commit comments

Comments
 (0)