Skip to content

Commit

Permalink
Add fee middleware to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Nov 17, 2022
1 parent 4566e39 commit 2ce07c9
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 21 deletions.
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,15 @@ func NewWasmApp(
icaHostStack = icahost.NewIBCModule(app.ICAHostKeeper)
icaHostStack = ibcfee.NewIBCMiddleware(icaHostStack, app.IBCFeeKeeper)

// Create fee enabled wasm ibc Stack
var wasmStack porttypes.IBCModule
wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper)
wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper)

// Create static IBC router, add app routes, then set and seal it
ibcRouter := porttypes.NewRouter().
AddRoute(ibctransfertypes.ModuleName, transferStack).
AddRoute(wasm.ModuleName, wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper)).
AddRoute(wasm.ModuleName, wasmStack).
AddRoute(intertxtypes.ModuleName, icaControllerStack).
AddRoute(icacontrollertypes.SubModuleName, icaControllerStack).
AddRoute(icahosttypes.SubModuleName, icaHostStack)
Expand Down
160 changes: 147 additions & 13 deletions tests/e2e/ibc_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package e2e

import (
"bytes"
"encoding/base64"
"fmt"
"testing"
"time"

"github.com/CosmWasm/wasmd/app"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
ibcfee "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v4/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ibcfee "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"

"github.com/CosmWasm/wasmd/app"
wasmibctesting "github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

func TestIBCFees(t *testing.T) {
func TestIBCFeesTransfer(t *testing.T) {
// scenario:
// given 2 chains
// with an ics-20 channel established
Expand All @@ -33,7 +33,8 @@ func TestIBCFees(t *testing.T) {
chainA := coord.GetChain(ibctesting.GetChainID(0))
chainB := coord.GetChain(ibctesting.GetChainID(1))

sender := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address())
actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address())
actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address())
receiver := sdk.AccAddress(bytes.Repeat([]byte{1}, address.Len))
payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len))
oneToken := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)))
Expand All @@ -53,16 +54,16 @@ func TestIBCFees(t *testing.T) {
coord.Setup(path)
require.True(t, chainA.App.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID))
// and with a payee registered on both chains
_, err := chainA.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointA.ChannelID, sender.String(), payee.String()))
_, err := chainA.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String()))
require.NoError(t, err)
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, chainB.SenderAccount.GetAddress().String(), payee.String()))
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), payee.String()))
require.NoError(t, err)

// when a transfer package is sent
transferCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))
ibcPayloadMsg := ibctransfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, transferCoin, sender.String(), receiver.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()))
ibcPayloadMsg := ibctransfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, transferCoin, actorChainA.String(), receiver.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()))
ibcPackageFee := ibcfee.NewFee(oneToken, oneToken, sdk.Coins{})
feeMsg := ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointA.ChannelID, sender.String(), nil)
feeMsg := ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), nil)
_, err = chainA.SendMsgs(feeMsg, ibcPayloadMsg)
require.NoError(t, err)
pendingIncentivisedPackages := chainA.App.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)
Expand All @@ -77,4 +78,137 @@ func TestIBCFees(t *testing.T) {
assert.Equal(t, expBalance.String(), gotBalance.String())
payeeBalance := chainA.AllBalances(payee)
assert.Equal(t, oneToken.Add(oneToken...).String(), payeeBalance.String())

// and with a payee registered for chain B to A
_, err = chainA.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String()))
require.NoError(t, err)
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), payee.String()))
require.NoError(t, err)

// and transfer from B to A
ibcPayloadMsg = ibctransfertypes.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, transferCoin, actorChainB.String(), receiver.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()))
ibcPackageFee = ibcfee.NewFee(oneToken, oneToken, sdk.Coins{})
feeMsg = ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), nil)
_, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg)
require.NoError(t, err)
pendingIncentivisedPackages = chainB.App.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// when packages relayed
require.NoError(t, coord.RelayAndAckPendingPackets(path))

// then
expBalance = ibctransfertypes.GetTransferCoin(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, transferCoin.Denom, transferCoin.Amount)
gotBalance = chainA.Balance(receiver, expBalance.Denom)
assert.Equal(t, expBalance.String(), gotBalance.String())
payeeBalance = chainB.AllBalances(payee)
assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(2)).String(), payeeBalance.String())
}

func TestIBCFeesWasm(t *testing.T) {
// scenario:
// given 2 chains with cw20-ibc on chain A and native ics20 module on B
// and an ibc channel established
// when an ics-29 fee is attached to an ibc package
// then the relayer's payee is receiving the fee(s) on success
marshaler := app.MakeEncodingConfig().Marshaler
coord := wasmibctesting.NewCoordinator(t, 2)
chainA := coord.GetChain(ibctesting.GetChainID(0))
chainB := coord.GetChain(ibctesting.GetChainID(1))
actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address())
actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address())

// setup chain A
codeID := chainA.StoreCodeFile("./testdata/cw20_base.wasm.gz").CodeID

initMsg := []byte(fmt.Sprintf(`{"decimals": 6, "name": "test", "symbol":"ALX", "initial_balances": [{"address": %q,"amount":"100000000"}] }`, actorChainA.String()))
cw20ContractAddr := chainA.InstantiateContract(codeID, initMsg)

initMsg = []byte(fmt.Sprintf(`{"default_timeout": 360, "gov_contract": %q, "allowlist":[{"contract":%q}]}`, actorChainA.String(), cw20ContractAddr.String()))
codeID = chainA.StoreCodeFile("./testdata/cw20_ics20.wasm.gz").CodeID
ibcContractAddr := chainA.InstantiateContract(codeID, initMsg)
ibcContractPortID := chainA.ContractInfo(ibcContractAddr).IBCPortID

payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len))
oneToken := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1)))

path := wasmibctesting.NewPath(chainA, chainB)
path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{
PortID: ibcContractPortID,
Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.Version})),
Order: channeltypes.UNORDERED,
}
path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{
PortID: ibctransfertypes.PortID,
Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.Version})),
Order: channeltypes.UNORDERED,
}
// with an ics-29 fee enabled channel setup between both chains
coord.Setup(path)
require.True(t, chainA.App.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID))
require.True(t, chainB.App.IBCFeeKeeper.IsFeeEnabled(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID))
// and with a payee registered for A -> B
_, err := chainA.SendMsgs(ibcfee.NewMsgRegisterPayee(ibcContractPortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String()))
require.NoError(t, err)
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), payee.String()))
require.NoError(t, err)

// when a transfer package is sent from ics20 contract on A to B
transfer := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(`{"channel": %q, "remote_address": %q}`, path.EndpointA.ChannelID, actorChainB.String())))
exec := []byte(fmt.Sprintf(`{"send":{"contract": %q, "amount": "100", "msg": %q}}`, ibcContractAddr.String(), transfer))
execMsg := wasmtypes.MsgExecuteContract{
Sender: actorChainA.String(),
Contract: cw20ContractAddr.String(),
Msg: exec,
}
ibcPackageFee := ibcfee.NewFee(oneToken, oneToken, sdk.Coins{})
feeMsg := ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibcContractPortID, path.EndpointA.ChannelID, actorChainA.String(), nil)
_, err = chainA.SendMsgs(feeMsg, &execMsg)
require.NoError(t, err)
pendingIncentivisedPackages := chainA.App.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// and packages relayed
require.NoError(t, coord.RelayAndAckPendingPackets(path))

// then
// on chain A
gotCW20Balance, err := chainA.App.WasmKeeper.QuerySmart(chainA.GetContext(), cw20ContractAddr, []byte(fmt.Sprintf(`{"balance":{"address": %q}}`, actorChainA.String())))
require.NoError(t, err)
assert.JSONEq(t, `{"balance":"99999900"}`, string(gotCW20Balance))
payeeBalance := chainA.AllBalances(payee)
assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(2)).String(), payeeBalance.String())
// and on chain B
pendingIncentivisedPackages = chainA.App.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 0)
expBalance := ibctransfertypes.GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, "cw20:"+cw20ContractAddr.String(), sdk.NewInt(100))
gotBalance := chainB.Balance(actorChainB, expBalance.Denom)
assert.Equal(t, expBalance.String(), gotBalance.String(), chainB.AllBalances(actorChainB))

// and with a payee registered for chain B to A
_, err = chainA.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibcContractPortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String()))
require.NoError(t, err)
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), payee.String()))
require.NoError(t, err)

// and when sent back from chain B to A
ibcPayloadMsg := ibctransfertypes.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, gotBalance, actorChainB.String(), actorChainA.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()))
ibcPackageFee = ibcfee.NewFee(oneToken, oneToken, sdk.Coins{})
feeMsg = ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), nil)
_, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg)
require.NoError(t, err)
pendingIncentivisedPackages = chainB.App.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// when packages relayed
require.NoError(t, coord.RelayAndAckPendingPackets(path))

// then
// on chain A
gotCW20Balance, err = chainA.App.WasmKeeper.QuerySmart(chainA.GetContext(), cw20ContractAddr, []byte(fmt.Sprintf(`{"balance":{"address": %q}}`, actorChainA.String())))
require.NoError(t, err)
assert.JSONEq(t, `{"balance":"100000000"}`, string(gotCW20Balance))
// and on chain B
payeeBalance = chainB.AllBalances(payee)
assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(2)).String(), payeeBalance.String())
}
Binary file added tests/e2e/testdata/cw20_base.wasm.gz
Binary file not shown.
Binary file added tests/e2e/testdata/cw20_ics20.wasm.gz
Binary file not shown.
12 changes: 11 additions & 1 deletion x/wasm/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package wasm

import (
"math"
"strings"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
ibcfees "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v4/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v4/modules/core/24-host"
Expand Down Expand Up @@ -228,11 +230,19 @@ func (i IBCHandler) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string
}

func toWasmVMChannel(portID, channelID string, channelInfo channeltypes.Channel) wasmvmtypes.IBCChannel {
version := channelInfo.Version
if strings.TrimSpace(version) != "" {
// check for ics-29 middleware versions
var versionMetadata ibcfees.Metadata
if err := types.ModuleCdc.UnmarshalJSON([]byte(channelInfo.Version), &versionMetadata); err == nil {
version = versionMetadata.AppVersion
}
}
return wasmvmtypes.IBCChannel{
Endpoint: wasmvmtypes.IBCEndpoint{PortID: portID, ChannelID: channelID},
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{PortID: channelInfo.Counterparty.PortId, ChannelID: channelInfo.Counterparty.ChannelId},
Order: channelInfo.Ordering.String(),
Version: channelInfo.Version,
Version: version,
ConnectionID: channelInfo.ConnectionHops[0], // At the moment this list must be of length 1. In the future multi-hop channels may be supported.
}
}
Expand Down
10 changes: 9 additions & 1 deletion x/wasm/ibctesting/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,16 @@ func (coord *Coordinator) ChanOpenInitOnBothChains(path *Path) error {
func (coord *Coordinator) RelayAndAckPendingPackets(path *Path) error {
// get all the packet to relay src->dest
src := path.EndpointA
coord.t.Logf("Relay %d Packets A->B\n", len(src.Chain.PendingSendPackets))
coord.t.Logf("Relay: %d Packets A->B, %d Packets B->A\n", len(src.Chain.PendingSendPackets), len(path.EndpointB.Chain.PendingSendPackets))
for i, v := range src.Chain.PendingSendPackets {
err := path.RelayPacket(v, nil)
if err != nil {
return err
}
src.Chain.PendingSendPackets = append(src.Chain.PendingSendPackets[0:i], src.Chain.PendingSendPackets[i+1:]...)
}

src = path.EndpointB
for i, v := range src.Chain.PendingSendPackets {
err := path.RelayPacket(v, nil)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions x/wasm/relay_pingpong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ func TestPinPong(t *testing.T) {
require.Len(t, chainA.PendingSendPackets, 1)
err := coordinator.RelayAndAckPendingPackets(path)
require.NoError(t, err)

// switch side
require.Len(t, chainB.PendingSendPackets, 1)
err = coordinator.RelayAndAckPendingPackets(path.Invert())
require.NoError(t, err)
}

// then receive/response state is as expected
Expand Down

0 comments on commit 2ce07c9

Please sign in to comment.