|
9 | 9 |
|
10 | 10 | wasmvmtypes "github.com/CosmWasm/wasmvm/types"
|
11 | 11 | "github.com/cosmos/gogoproto/proto"
|
| 12 | + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" |
12 | 13 | "github.com/stretchr/testify/assert"
|
13 | 14 | "github.com/stretchr/testify/require"
|
14 | 15 |
|
@@ -642,6 +643,194 @@ func TestDistributionQuery(t *testing.T) {
|
642 | 643 | }
|
643 | 644 | }
|
644 | 645 |
|
| 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 | + nonIbcExample := InstantiateReflectExampleContract(t, pCtx, keepers) |
| 651 | + ibcExample := InstantiateReflectExampleContract(t, pCtx, keepers) |
| 652 | + // add an ibc port for testing |
| 653 | + myIBCPortID := "myValidPortID" |
| 654 | + cInfo := keeper.GetContractInfo(pCtx, ibcExample.Contract) |
| 655 | + cInfo.IBCPortID = myIBCPortID |
| 656 | + keeper.storeContractInfo(pCtx, ibcExample.Contract, cInfo) |
| 657 | + // store a random channel to be ignored in queries |
| 658 | + unusedChan := channeltypes.Channel{ |
| 659 | + State: channeltypes.OPEN, |
| 660 | + Ordering: channeltypes.UNORDERED, |
| 661 | + Counterparty: channeltypes.Counterparty{ |
| 662 | + PortId: "counterPartyPortID", |
| 663 | + ChannelId: "counterPartyChannelID", |
| 664 | + }, |
| 665 | + ConnectionHops: []string{"any"}, |
| 666 | + Version: "any", |
| 667 | + } |
| 668 | + keepers.IBCKeeper.ChannelKeeper.SetChannel(pCtx, "nonContractPortID", "channel-99", unusedChan) |
| 669 | + |
| 670 | + // mixed channel examples for testing |
| 671 | + myExampleChannels := []channeltypes.Channel{ |
| 672 | + { |
| 673 | + State: channeltypes.OPEN, |
| 674 | + Ordering: channeltypes.ORDERED, |
| 675 | + Counterparty: channeltypes.Counterparty{ |
| 676 | + PortId: "counterPartyPortID", |
| 677 | + ChannelId: "counterPartyChannelID", |
| 678 | + }, |
| 679 | + ConnectionHops: []string{"one"}, |
| 680 | + Version: "v1", |
| 681 | + }, |
| 682 | + { |
| 683 | + State: channeltypes.INIT, |
| 684 | + Ordering: channeltypes.UNORDERED, |
| 685 | + Counterparty: channeltypes.Counterparty{ |
| 686 | + PortId: "foobar", |
| 687 | + }, |
| 688 | + ConnectionHops: []string{"one"}, |
| 689 | + Version: "initversion", |
| 690 | + }, |
| 691 | + { |
| 692 | + State: channeltypes.OPEN, |
| 693 | + Ordering: channeltypes.UNORDERED, |
| 694 | + Counterparty: channeltypes.Counterparty{ |
| 695 | + PortId: "otherCounterPartyPortID", |
| 696 | + ChannelId: "otherCounterPartyChannelID", |
| 697 | + }, |
| 698 | + ConnectionHops: []string{"other", "second"}, |
| 699 | + Version: "otherVersion", |
| 700 | + }, |
| 701 | + { |
| 702 | + State: channeltypes.CLOSED, |
| 703 | + Ordering: channeltypes.ORDERED, |
| 704 | + Counterparty: channeltypes.Counterparty{ |
| 705 | + PortId: "super", |
| 706 | + ChannelId: "duper", |
| 707 | + }, |
| 708 | + ConnectionHops: []string{"no-more"}, |
| 709 | + Version: "closedVersion", |
| 710 | + }, |
| 711 | + } |
| 712 | + |
| 713 | + withChannelsStored := func(portID string, channels ...channeltypes.Channel) func(t *testing.T, ctx sdk.Context) sdk.Context { |
| 714 | + return func(t *testing.T, ctx sdk.Context) sdk.Context { |
| 715 | + for i, v := range channels { |
| 716 | + keepers.IBCKeeper.ChannelKeeper.SetChannel(ctx, portID, fmt.Sprintf("channel-%d", i), v) |
| 717 | + } |
| 718 | + return ctx |
| 719 | + } |
| 720 | + } |
| 721 | + noopSetup := func(t *testing.T, ctx sdk.Context) sdk.Context { return ctx } |
| 722 | + |
| 723 | + specs := map[string]struct { |
| 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) |
| 729 | + }{ |
| 730 | + "open channels - with query portID empty": { |
| 731 | + contract: ibcExample.Contract, |
| 732 | + setup: withChannelsStored(myIBCPortID, myExampleChannels...), |
| 733 | + query: &wasmvmtypes.IBCQuery{ListChannels: &wasmvmtypes.ListChannelsQuery{}}, |
| 734 | + assert: func(t *testing.T, d []byte) { |
| 735 | + rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d) |
| 736 | + exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{ |
| 737 | + { |
| 738 | + Endpoint: wasmvmtypes.IBCEndpoint{PortID: myIBCPortID, ChannelID: "channel-0"}, |
| 739 | + CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{ |
| 740 | + PortID: "counterPartyPortID", |
| 741 | + ChannelID: "counterPartyChannelID", |
| 742 | + }, |
| 743 | + Order: channeltypes.ORDERED.String(), |
| 744 | + Version: "v1", |
| 745 | + ConnectionID: "one", |
| 746 | + }, { |
| 747 | + Endpoint: wasmvmtypes.IBCEndpoint{PortID: myIBCPortID, ChannelID: "channel-2"}, |
| 748 | + CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{ |
| 749 | + PortID: "otherCounterPartyPortID", |
| 750 | + ChannelID: "otherCounterPartyChannelID", |
| 751 | + }, |
| 752 | + Order: channeltypes.UNORDERED.String(), |
| 753 | + Version: "otherVersion", |
| 754 | + ConnectionID: "other", |
| 755 | + }, |
| 756 | + }} |
| 757 | + assert.Equal(t, exp, rsp) |
| 758 | + }, |
| 759 | + }, |
| 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"}}, |
| 764 | + assert: func(t *testing.T, d []byte) { |
| 765 | + rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d) |
| 766 | + exp := wasmvmtypes.ListChannelsResponse{Channels: []wasmvmtypes.IBCChannel{ |
| 767 | + { |
| 768 | + Endpoint: wasmvmtypes.IBCEndpoint{PortID: "OtherPortID", ChannelID: "channel-0"}, |
| 769 | + CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{ |
| 770 | + PortID: "counterPartyPortID", |
| 771 | + ChannelID: "counterPartyChannelID", |
| 772 | + }, |
| 773 | + Order: channeltypes.ORDERED.String(), |
| 774 | + Version: "v1", |
| 775 | + ConnectionID: "one", |
| 776 | + }, { |
| 777 | + Endpoint: wasmvmtypes.IBCEndpoint{PortID: "OtherPortID", ChannelID: "channel-2"}, |
| 778 | + CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{ |
| 779 | + PortID: "otherCounterPartyPortID", |
| 780 | + ChannelID: "otherCounterPartyChannelID", |
| 781 | + }, |
| 782 | + Order: channeltypes.UNORDERED.String(), |
| 783 | + Version: "otherVersion", |
| 784 | + ConnectionID: "other", |
| 785 | + }, |
| 786 | + }} |
| 787 | + assert.Equal(t, exp, rsp) |
| 788 | + }, |
| 789 | + }, |
| 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{}}, |
| 803 | + assert: func(t *testing.T, d []byte) { |
| 804 | + rsp := unmarshalReflect[wasmvmtypes.ListChannelsResponse](t, d) |
| 805 | + assert.Empty(t, rsp.Channels) |
| 806 | + }, |
| 807 | + }, |
| 808 | + } |
| 809 | + for name, spec := range specs { |
| 810 | + t.Run(name, func(t *testing.T) { |
| 811 | + ctx, _ := pCtx.CacheContext() |
| 812 | + ctx = spec.setup(t, ctx) |
| 813 | + |
| 814 | + // when |
| 815 | + queryBz := mustMarshal(t, testdata.ReflectQueryMsg{ |
| 816 | + Chain: &testdata.ChainQuery{ |
| 817 | + Request: &wasmvmtypes.QueryRequest{IBC: spec.query}, |
| 818 | + }, |
| 819 | + }) |
| 820 | + simpleRes, gotErr := keeper.QuerySmart(ctx, spec.contract, queryBz) |
| 821 | + if spec.expErr { |
| 822 | + require.Error(t, gotErr) |
| 823 | + return |
| 824 | + } |
| 825 | + // then |
| 826 | + require.NoError(t, gotErr) |
| 827 | + var rsp testdata.ChainResponse |
| 828 | + mustUnmarshal(t, simpleRes, &rsp) |
| 829 | + spec.assert(t, rsp.Data) |
| 830 | + }) |
| 831 | + } |
| 832 | +} |
| 833 | + |
645 | 834 | func unmarshalReflect[T any](t *testing.T, d []byte) T {
|
646 | 835 | var v T
|
647 | 836 | mustUnmarshal(t, d, &v)
|
|
0 commit comments