Skip to content

Commit

Permalink
Handle query for non ibc contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Sep 13, 2023
1 parent 177c0a9 commit d5fa278
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
48 changes: 31 additions & 17 deletions x/wasm/keeper/query_plugin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,13 @@ func TestIBCListChannelsQuery(t *testing.T) {
cdc := MakeEncodingConfig(t).Codec
pCtx, keepers := CreateTestInput(t, false, ReflectFeatures, WithMessageEncoders(reflectEncoders(cdc)), WithQueryPlugins(reflectPlugins()))
keeper := keepers.WasmKeeper
example := InstantiateReflectExampleContract(t, pCtx, keepers)
nonIbcExample := InstantiateReflectExampleContract(t, pCtx, keepers)
ibcExample := InstantiateReflectExampleContract(t, pCtx, keepers)
// add an ibc port for testing
myIBCPortID := "myValidPortID"
cInfo := keeper.GetContractInfo(pCtx, example.Contract)
cInfo := keeper.GetContractInfo(pCtx, ibcExample.Contract)
cInfo.IBCPortID = myIBCPortID
keeper.storeContractInfo(pCtx, example.Contract, cInfo)
keeper.storeContractInfo(pCtx, ibcExample.Contract, cInfo)
// store a random channel to be ignored in queries
unusedChan := channeltypes.Channel{
State: channeltypes.OPEN,
Expand Down Expand Up @@ -720,14 +721,16 @@ func TestIBCListChannelsQuery(t *testing.T) {
noopSetup := func(t *testing.T, ctx sdk.Context) sdk.Context { return ctx }

specs := map[string]struct {
setup func(t *testing.T, ctx sdk.Context) sdk.Context
query *wasmvmtypes.IBCQuery
expErr bool
assert func(t *testing.T, d []byte)
setup func(t *testing.T, ctx sdk.Context) sdk.Context
contract sdk.AccAddress
query *wasmvmtypes.IBCQuery
expErr bool
assert func(t *testing.T, d []byte)
}{
"only open channels - portID empty": {
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
"open channels - with query portID empty": {
contract: ibcExample.Contract,
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
assert: func(t *testing.T, d []byte) {
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
Expand All @@ -754,9 +757,10 @@ func TestIBCListChannelsQuery(t *testing.T) {
assert.Equal(t, exp, rsp)
},
},
"open channels - portID set to non contract addr": {
setup: withChannelsStored("OtherPortID", myExampleChannels...),
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{PortID: "OtherPortID"}},
"open channels - with query portID passed": {
contract: ibcExample.Contract,
setup: withChannelsStored("OtherPortID", myExampleChannels...),
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{PortID: "OtherPortID"}},
assert: func(t *testing.T, d []byte) {
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
Expand All @@ -783,9 +787,19 @@ func TestIBCListChannelsQuery(t *testing.T) {
assert.Equal(t, exp, rsp)
},
},
"no channels": {
setup: noopSetup,
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
"non ibc contract - with query portID empty": {
contract: nonIbcExample.Contract,
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
assert: func(t *testing.T, d []byte) {
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
assert.Nil(t, rsp.Channels)
},
},
"no matching channels": {
contract: ibcExample.Contract,
setup: noopSetup,
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
assert: func(t *testing.T, d []byte) {
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
assert.Empty(t, rsp.Channels)
Expand All @@ -803,7 +817,7 @@ func TestIBCListChannelsQuery(t *testing.T) {
Request: &wasmvmtypes.QueryRequest{IBC: spec.query},
},
})
simpleRes, gotErr := keeper.QuerySmart(ctx, example.Contract, queryBz)
simpleRes, gotErr := keeper.QuerySmart(ctx, spec.contract, queryBz)
if spec.expErr {
require.Error(t, gotErr)
return
Expand Down
41 changes: 22 additions & 19 deletions x/wasm/keeper/query_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,31 @@ func IBCQuerier(wasm contractMetaDataSource, channelKeeper types.ChannelKeeper)
}
if request.ListChannels != nil {
portID := request.ListChannels.PortID
if portID == "" {
if portID == "" { // then fallback to contract port address
portID = wasm.GetContractInfo(ctx, caller).IBCPortID
}
gotChannels := channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
channels := make(wasmvmtypes.IBCChannels, 0, len(gotChannels))
for _, ch := range gotChannels {
if ch.State != channeltypes.OPEN {
continue
var channels wasmvmtypes.IBCChannels
if portID != "" { // then return empty list for non ibc contracts; no channels possible
gotChannels := channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
channels = make(wasmvmtypes.IBCChannels, 0, len(gotChannels))
for _, ch := range gotChannels {
if ch.State != channeltypes.OPEN {
continue
}
channels = append(channels, wasmvmtypes.IBCChannel{
Endpoint: wasmvmtypes.IBCEndpoint{
PortID: ch.PortId,
ChannelID: ch.ChannelId,
},
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
PortID: ch.Counterparty.PortId,
ChannelID: ch.Counterparty.ChannelId,
},
Order: ch.Ordering.String(),
Version: ch.Version,
ConnectionID: ch.ConnectionHops[0],
})
}
channels = append(channels, wasmvmtypes.IBCChannel{
Endpoint: wasmvmtypes.IBCEndpoint{
PortID: ch.PortId,
ChannelID: ch.ChannelId,
},
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
PortID: ch.Counterparty.PortId,
ChannelID: ch.Counterparty.ChannelId,
},
Order: ch.Ordering.String(),
Version: ch.Version,
ConnectionID: ch.ConnectionHops[0],
})
}
res := wasmvmtypes.ListChannelsResponse{
Channels: channels,
Expand Down

0 comments on commit d5fa278

Please sign in to comment.