Skip to content

Commit 177c0a9

Browse files
committed
Test channels query
1 parent e81d655 commit 177c0a9

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

x/wasm/keeper/query_plugin_integration_test.go

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
1111
"github.com/cosmos/gogoproto/proto"
12+
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415

@@ -642,6 +643,180 @@ func TestDistributionQuery(t *testing.T) {
642643
}
643644
}
644645

646+
func TestIBCListChannelsQuery(t *testing.T) {
647+
cdc := MakeEncodingConfig(t).Codec
648+
pCtx, keepers := CreateTestInput(t, false, ReflectFeatures, WithMessageEncoders(reflectEncoders(cdc)), WithQueryPlugins(reflectPlugins()))
649+
keeper := keepers.WasmKeeper
650+
example := InstantiateReflectExampleContract(t, pCtx, keepers)
651+
// add an ibc port for testing
652+
myIBCPortID := "myValidPortID"
653+
cInfo := keeper.GetContractInfo(pCtx, example.Contract)
654+
cInfo.IBCPortID = myIBCPortID
655+
keeper.storeContractInfo(pCtx, example.Contract, cInfo)
656+
// store a random channel to be ignored in queries
657+
unusedChan := channeltypes.Channel{
658+
State: channeltypes.OPEN,
659+
Ordering: channeltypes.UNORDERED,
660+
Counterparty: channeltypes.Counterparty{
661+
PortId: "counterPartyPortID",
662+
ChannelId: "counterPartyChannelID",
663+
},
664+
ConnectionHops: []string{"any"},
665+
Version: "any",
666+
}
667+
keepers.IBCKeeper.ChannelKeeper.SetChannel(pCtx, "nonContractPortID", "channel-99", unusedChan)
668+
669+
// mixed channel examples for testing
670+
myExampleChannels := []channeltypes.Channel{
671+
{
672+
State: channeltypes.OPEN,
673+
Ordering: channeltypes.ORDERED,
674+
Counterparty: channeltypes.Counterparty{
675+
PortId: "counterPartyPortID",
676+
ChannelId: "counterPartyChannelID",
677+
},
678+
ConnectionHops: []string{"one"},
679+
Version: "v1",
680+
},
681+
{
682+
State: channeltypes.INIT,
683+
Ordering: channeltypes.UNORDERED,
684+
Counterparty: channeltypes.Counterparty{
685+
PortId: "foobar",
686+
},
687+
ConnectionHops: []string{"one"},
688+
Version: "initversion",
689+
},
690+
{
691+
State: channeltypes.OPEN,
692+
Ordering: channeltypes.UNORDERED,
693+
Counterparty: channeltypes.Counterparty{
694+
PortId: "otherCounterPartyPortID",
695+
ChannelId: "otherCounterPartyChannelID",
696+
},
697+
ConnectionHops: []string{"other", "second"},
698+
Version: "otherVersion",
699+
},
700+
{
701+
State: channeltypes.CLOSED,
702+
Ordering: channeltypes.ORDERED,
703+
Counterparty: channeltypes.Counterparty{
704+
PortId: "super",
705+
ChannelId: "duper",
706+
},
707+
ConnectionHops: []string{"no-more"},
708+
Version: "closedVersion",
709+
},
710+
}
711+
712+
withChannelsStored := func(portID string, channels ...channeltypes.Channel) func(t *testing.T, ctx sdk.Context) sdk.Context {
713+
return func(t *testing.T, ctx sdk.Context) sdk.Context {
714+
for i, v := range channels {
715+
keepers.IBCKeeper.ChannelKeeper.SetChannel(ctx, portID, fmt.Sprintf("channel-%d", i), v)
716+
}
717+
return ctx
718+
}
719+
}
720+
noopSetup := func(t *testing.T, ctx sdk.Context) sdk.Context { return ctx }
721+
722+
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)
727+
}{
728+
"only open channels - portID empty": {
729+
setup: withChannelsStored(myIBCPortID, myExampleChannels...),
730+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
731+
assert: func(t *testing.T, d []byte) {
732+
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
733+
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
734+
{
735+
Endpoint: wasmvmtypes.IBCEndpoint{PortID: myIBCPortID, ChannelID: "channel-0"},
736+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
737+
PortID: "counterPartyPortID",
738+
ChannelID: "counterPartyChannelID",
739+
},
740+
Order: channeltypes.ORDERED.String(),
741+
Version: "v1",
742+
ConnectionID: "one",
743+
}, {
744+
Endpoint: wasmvmtypes.IBCEndpoint{PortID: myIBCPortID, ChannelID: "channel-2"},
745+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
746+
PortID: "otherCounterPartyPortID",
747+
ChannelID: "otherCounterPartyChannelID",
748+
},
749+
Order: channeltypes.UNORDERED.String(),
750+
Version: "otherVersion",
751+
ConnectionID: "other",
752+
},
753+
}}
754+
assert.Equal(t, exp, rsp)
755+
},
756+
},
757+
"open channels - portID set to non contract addr": {
758+
setup: withChannelsStored("OtherPortID", myExampleChannels...),
759+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{PortID: "OtherPortID"}},
760+
assert: func(t *testing.T, d []byte) {
761+
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
762+
exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{
763+
{
764+
Endpoint: wasmvmtypes.IBCEndpoint{PortID: "OtherPortID", ChannelID: "channel-0"},
765+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
766+
PortID: "counterPartyPortID",
767+
ChannelID: "counterPartyChannelID",
768+
},
769+
Order: channeltypes.ORDERED.String(),
770+
Version: "v1",
771+
ConnectionID: "one",
772+
}, {
773+
Endpoint: wasmvmtypes.IBCEndpoint{PortID: "OtherPortID", ChannelID: "channel-2"},
774+
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{
775+
PortID: "otherCounterPartyPortID",
776+
ChannelID: "otherCounterPartyChannelID",
777+
},
778+
Order: channeltypes.UNORDERED.String(),
779+
Version: "otherVersion",
780+
ConnectionID: "other",
781+
},
782+
}}
783+
assert.Equal(t, exp, rsp)
784+
},
785+
},
786+
"no channels": {
787+
setup: noopSetup,
788+
query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}},
789+
assert: func(t *testing.T, d []byte) {
790+
rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d)
791+
assert.Empty(t, rsp.Channels)
792+
},
793+
},
794+
}
795+
for name, spec := range specs {
796+
t.Run(name, func(t *testing.T) {
797+
ctx, _ := pCtx.CacheContext()
798+
ctx = spec.setup(t, ctx)
799+
800+
// when
801+
queryBz := mustMarshal(t, testdata.ReflectQueryMsg{
802+
Chain: &testdata.ChainQuery{
803+
Request: &wasmvmtypes.QueryRequest{IBC: spec.query},
804+
},
805+
})
806+
simpleRes, gotErr := keeper.QuerySmart(ctx, example.Contract, queryBz)
807+
if spec.expErr {
808+
require.Error(t, gotErr)
809+
return
810+
}
811+
// then
812+
require.NoError(t, gotErr)
813+
var rsp testdata.ChainResponse
814+
mustUnmarshal(t, simpleRes, &rsp)
815+
spec.assert(t, rsp.Data)
816+
})
817+
}
818+
}
819+
645820
func unmarshalReflect[T any](t *testing.T, d []byte) T {
646821
var v T
647822
mustUnmarshal(t, d, &v)

0 commit comments

Comments
 (0)