diff --git a/x/wasm/internal/keeper/ibc_mocks_test.go b/x/wasm/internal/keeper/ibc_mocks_test.go deleted file mode 100644 index a4f6653d7..000000000 --- a/x/wasm/internal/keeper/ibc_mocks_test.go +++ /dev/null @@ -1,148 +0,0 @@ -package keeper_test - -import ( - "bytes" - "testing" - - cosmwasmv1 "github.com/CosmWasm/go-cosmwasm" - "github.com/CosmWasm/wasmd/app" - "github.com/CosmWasm/wasmd/x/wasm" - "github.com/CosmWasm/wasmd/x/wasm/internal/keeper" - cosmwasmv2 "github.com/CosmWasm/wasmd/x/wasm/internal/keeper/cosmwasm" - "github.com/CosmWasm/wasmd/x/wasm/internal/types" - "github.com/cosmos/cosmos-sdk/store/prefix" - sdk "github.com/cosmos/cosmos-sdk/types" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported" - channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/libs/rand" -) - -type mockContract struct { - app *app.WasmApp - t *testing.T - contractAddr sdk.AccAddress -} - -func MockContract(t *testing.T, contractAddr sdk.AccAddress, app *app.WasmApp) *mockContract { - c := &mockContract{t: t, - contractAddr: contractAddr, - app: app, - } - keeper.MockContracts[contractAddr.String()] = c - return c -} - -func (c *mockContract) AcceptChannel(ctx sdk.Context, hash []byte, params cosmwasmv2.Env, order channeltypes.Order, version string, connectionHops []string, store prefix.Store, api cosmwasmv1.GoAPI, querier keeper.QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasmv2.AcceptChannelResponse, uint64, error) { - if order != channeltypes.ORDERED { - return &cosmwasmv2.AcceptChannelResponse{ - Result: false, - Reason: "channel type must be ordered", - }, 0, nil - } - return &cosmwasmv2.AcceptChannelResponse{Result: true}, 0, nil -} - -func (s *mockContract) OnConnect(ctx sdk.Context, hash []byte, params cosmwasmv2.Env, counterpartyPortID string, counterpartyChannelID string, store prefix.Store, api cosmwasmv1.GoAPI, querier keeper.QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasmv2.OnConnectIBCResponse, uint64, error) { - return &cosmwasmv2.OnConnectIBCResponse{}, 0, nil -} - -func (c *mockContract) OnReceive(ctx sdk.Context, hash []byte, params cosmwasmv2.Env, msg []byte, store prefix.Store, api cosmwasmv1.GoAPI, querier keeper.QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasmv2.OnReceiveIBCResponse, uint64, error) { - // real contract would do something with incoming msg - // create some random ackknowledgement - myAck := rand.Bytes(25) - store.Set(hash, append(msg, myAck...)) - return &cosmwasmv2.OnReceiveIBCResponse{Acknowledgement: myAck}, 0, nil -} -func (c *mockContract) OnAcknowledgement(ctx sdk.Context, hash []byte, params cosmwasmv2.Env, originalData []byte, acknowledgement []byte, store prefix.Store, api cosmwasmv1.GoAPI, querier keeper.QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasmv2.OnAcknowledgeIBCResponse, uint64, error) { - state := store.Get(hash) - require.NotNil(c.t, state) - assert.Equal(c.t, state, append(originalData, acknowledgement...)) - return &cosmwasmv2.OnAcknowledgeIBCResponse{}, 0, nil -} - -func (c *mockContract) OnTimeout(ctx sdk.Context, hash []byte, params cosmwasmv2.Env, originalData []byte, store prefix.Store, api cosmwasmv1.GoAPI, querier keeper.QueryHandler, meter sdk.GasMeter, gas uint64) (*cosmwasmv2.OnTimeoutIBCResponse, uint64, error) { - state := store.Get(hash) - require.NotNil(c.t, state) - assert.True(c.t, bytes.HasPrefix(state, originalData)) - return &cosmwasmv2.OnTimeoutIBCResponse{}, 0, nil -} - -func (c mockContract) DoIBCCall(ctx sdk.Context, sourceChannelID string, sourcePortID string) { // todo: move somewhere else - // how does contract know where to send package too? channel/portID - // can environment have a list of available channel/portIDs or do we check later? - // alternative: query for ibc channels/ portids? - - msg := types.MsgWasmIBCCall{ - SourcePort: sourcePortID, - SourceChannel: sourceChannelID, - Sender: c.contractAddr, - TimeoutHeight: 110, - TimeoutTimestamp: 0, - Msg: []byte("{}"), - } - handler := wasm.NewHandler(c.app.WasmKeeper) - _, err := handler(ctx, &msg) - require.NoError(c.t, err) -} - -const ( - protocolVersion = "1.0" - counterpartyPortID = "otherPortID" - counterpartyChannelID = "otherChannelID" - counterpartyConnectionID = "otherConnectionID" - counterpartyClientID = "otherClientID" - channelID = "myChannelID" - connectionID = "myConnectionID" - clientID = "myClientID" -) - -type receivedPackets struct { - channelCap *capabilitytypes.Capability - packet channelexported.PacketI -} - -type mockChannelKeeper struct { - seq uint64 - k types.ChannelKeeper - received []receivedPackets -} - -func NewMockChannelKeeper(k types.ChannelKeeper) *mockChannelKeeper { - return &mockChannelKeeper{k: k} -} - -func (m mockChannelKeeper) GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { - counterpartyChannel := channeltypes.NewCounterparty(counterpartyPortID, counterpartyChannelID) - return channeltypes.NewChannel(channeltypes.OPEN, channeltypes.ORDERED, counterpartyChannel, - []string{connectionID}, protocolVersion, - ), true -} - -func (m *mockChannelKeeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) { - m.seq++ - return m.seq, true -} - -func (m *mockChannelKeeper) SendPacket(ctx sdk.Context, channelCap *capabilitytypes.Capability, packet channelexported.PacketI) error { - m.received = append(m.received, receivedPackets{ - channelCap: channelCap, - packet: packet, - }) - return nil -} - -func (m mockChannelKeeper) PacketExecuted(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet channelexported.PacketI, acknowledgement []byte) error { - panic("implement me") -} - -func (m mockChannelKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error { - panic("implement me") -} - -func (m *mockChannelKeeper) Reset() { - m.seq = 0 - m.received = nil -} diff --git a/x/wasm/internal/keeper/ibc_test.go b/x/wasm/internal/keeper/ibc_test.go index 25f8967f8..a136f6c1c 100644 --- a/x/wasm/internal/keeper/ibc_test.go +++ b/x/wasm/internal/keeper/ibc_test.go @@ -8,12 +8,9 @@ import ( "testing" "time" - "github.com/CosmWasm/wasmd/x/wasm" sdk "github.com/cosmos/cosmos-sdk/types" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" - host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" @@ -71,94 +68,6 @@ func TestBindingPortOnInstantiate(t *testing.T) { } -func TestRelay(t *testing.T) { - // a chain with a contract instantiated and a channel - - // when our test-contract sends the MsgWasmIBCCall for an outgoing ibc call - // then a WasmIBCContractPacketData is sent to the channel - // and a WasmIBCContractPacketAcknowledgement stored - - // when the relayer picks up WasmIBCContractPacketData and returns an ack to - // our chain - // then the ack is passed to the contract with the origin WasmIBCContractPacketData - - wasmApp := CreateTestApp(t) - ctx := wasmApp.BaseApp.NewContext(false, header(10)) - contractAddr := withAContractInstance(t, ctx, wasmApp) - portID := wasmApp.WasmKeeper.GetContractInfo(ctx, contractAddr).IBCPortID - - mockChannelKeeper := NewMockChannelKeeper(wasmApp.WasmKeeper.ChannelKeeper) - wasmApp.WasmKeeper.ChannelKeeper = mockChannelKeeper - withCapabilities(t, ctx, channelID, portID, wasmApp) - - myContract := MockContract(t, contractAddr, wasmApp) - myContract.DoIBCCall(ctx, channelID, portID) - // then - require.Len(t, mockChannelKeeper.received, 1) - - // new scenario: - mockChannelKeeper.Reset() - // when we receive an incoming ibc packet - - packet := types.Packet{ - Sequence: 1, - SourcePort: counterpartyPortID, - SourceChannel: counterpartyChannelID, - DestinationPort: portID, - DestinationChannel: channelID, - Data: []byte(`{"my":"data""}`), - TimeoutHeight: 100, - TimeoutTimestamp: 0, - } - - ibcHandler := wasm.NewIBCHandler(wasmApp.WasmKeeper) - _, ack, err := ibcHandler.OnRecvPacket(ctx, packet) - require.NoError(t, err) - require.NotEmpty(t, ack) - // - res, err := ibcHandler.OnAcknowledgementPacket(ctx, packet, ack) - require.NoError(t, err) - _ = res -} - -func withCapabilities(t *testing.T, ctx sdk.Context, channelID, portID string, wasmApp *app.WasmApp) { - capName := host.ChannelCapabilityPath(portID, channelID) - cap, err := wasmApp.ScopedIBCKeeper.NewCapability(ctx, capName) - require.NoError(t, err) - err = wasmApp.ScopedWasmKeeper.ClaimCapability(ctx, cap, capName) - require.NoError(t, err) -} - -func withAContractInstance(t *testing.T, ctx sdk.Context, app *app.WasmApp) sdk.AccAddress { - t.Helper() - accKeeper, keeper, bankKeeper := app.AccountKeeper, app.WasmKeeper, app.BankKeeper - - deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000)) - creator := createFakeFundedAccount(t, ctx, accKeeper, bankKeeper, deposit) - - wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") - require.NoError(t, err) - - contractID, err := keeper.Create(ctx, creator, wasmCode, "https://github.com/CosmWasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "", nil) - require.NoError(t, err) - - _, _, bob := keyPubAddr() - _, _, fred := keyPubAddr() - - initMsg := InitMsg{ - Verifier: fred, - Beneficiary: bob, - } - initMsgBz, err := json.Marshal(initMsg) - require.NoError(t, err) - - // create with no balance is legal - addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 1", nil) - require.NoError(t, err) - - return addr -} - // This should replace CreateTestInput when possible (likely after CosmWasm 1.0 is merged into this branch) func CreateTestApp(t *testing.T) *app.WasmApp { tempDir, err := ioutil.TempDir("", "wasm")