Skip to content

Commit

Permalink
Merge pull request #1397 from CosmWasm/ext_wasmibctesting
Browse files Browse the repository at this point in the history
Decouple testing from app with an interface
  • Loading branch information
alpe authored May 15, 2023
2 parents 1a1d33e + 0337c35 commit e36611a
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 63 deletions.
40 changes: 40 additions & 0 deletions app/test_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package app

import (
"github.com/cosmos/cosmos-sdk/baseapp"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

"github.com/CosmWasm/wasmd/x/wasm"
)

func (app *WasmApp) GetIBCKeeper() *ibckeeper.Keeper {
return app.IBCKeeper
}

func (app *WasmApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
return app.ScopedIBCKeeper
}

func (app *WasmApp) GetBaseApp() *baseapp.BaseApp {
return app.BaseApp
}

func (app *WasmApp) GetBankKeeper() bankkeeper.Keeper {
return app.BankKeeper
}

func (app *WasmApp) GetStakingKeeper() *stakingkeeper.Keeper {
return app.StakingKeeper
}

func (app *WasmApp) GetAccountKeeper() authkeeper.AccountKeeper {
return app.AccountKeeper
}

func (app *WasmApp) GetWasmKeeper() wasm.Keeper {
return app.WasmKeeper
}
4 changes: 3 additions & 1 deletion tests/e2e/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/tests/e2e"
"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
)
Expand Down Expand Up @@ -41,7 +42,8 @@ func TestGovVoteByContract(t *testing.T) {
e2e.MustExecViaReflectContract(t, chain, contractAddr, delegateMsg)

signer := chain.SenderAccount.GetAddress().String()
govKeeper, accountKeeper := chain.App.GovKeeper, chain.App.AccountKeeper
app := chain.App.(*app.WasmApp)
govKeeper, accountKeeper := app.GovKeeper, app.AccountKeeper
communityPoolBalance := chain.Balance(accountKeeper.GetModuleAccount(chain.GetContext(), distributiontypes.ModuleName).GetAddress(), sdk.DefaultBondDenom)
require.False(t, communityPoolBalance.IsZero())

Expand Down
24 changes: 14 additions & 10 deletions tests/e2e/ibc_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestIBCFeesTransfer(t *testing.T) {
}
// with an ics-20 transfer channel setup between both chains
coord.Setup(path)
require.True(t, chainA.App.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID))
appA := chainA.App.(*app.WasmApp)
require.True(t, appA.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, actorChainA.String(), payee.String()))
require.NoError(t, err)
Expand All @@ -66,7 +67,7 @@ func TestIBCFeesTransfer(t *testing.T) {
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)
pendingIncentivisedPackages := appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// and packages relayed
Expand All @@ -91,7 +92,8 @@ func TestIBCFeesTransfer(t *testing.T) {
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)
appB := chainB.App.(*app.WasmApp)
pendingIncentivisedPackages = appB.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// when packages relayed
Expand Down Expand Up @@ -145,8 +147,10 @@ func TestIBCFeesWasm(t *testing.T) {
}
// 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))
appA := chainA.App.(*app.WasmApp)
appB := chainB.App.(*app.WasmApp)
require.True(t, appA.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID))
require.True(t, appB.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)
Expand All @@ -165,21 +169,21 @@ func TestIBCFeesWasm(t *testing.T) {
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)
pendingIncentivisedPackages := appA.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())))
gotCW20Balance, err := appA.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)
pendingIncentivisedPackages = appA.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)
Expand All @@ -197,15 +201,15 @@ func TestIBCFeesWasm(t *testing.T) {
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)
pendingIncentivisedPackages = appB.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())))
gotCW20Balance, err = appA.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
Expand Down
8 changes: 6 additions & 2 deletions tests/e2e/ica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/CosmWasm/wasmd/app"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -30,7 +32,8 @@ func TestICA(t *testing.T) {
coord := wasmibctesting.NewCoordinator(t, 2)
hostChain := coord.GetChain(ibctesting.GetChainID(1))
hostParams := hosttypes.NewParams(true, []string{sdk.MsgTypeURL(&banktypes.MsgSend{})})
hostChain.App.ICAHostKeeper.SetParams(hostChain.GetContext(), hostParams)
hostApp := hostChain.App.(*app.WasmApp)
hostApp.ICAHostKeeper.SetParams(hostChain.GetContext(), hostParams)

controllerChain := coord.GetChain(ibctesting.GetChainID(2))

Expand Down Expand Up @@ -58,7 +61,8 @@ func TestICA(t *testing.T) {
coord.CreateChannels(path)

// assert ICA exists on controller
icaRsp, err := controllerChain.App.ICAControllerKeeper.InterchainAccount(sdk.WrapSDKContext(controllerChain.GetContext()), &icacontrollertypes.QueryInterchainAccountRequest{
contApp := controllerChain.App.(*app.WasmApp)
icaRsp, err := contApp.ICAControllerKeeper.InterchainAccount(sdk.WrapSDKContext(controllerChain.GetContext()), &icacontrollertypes.QueryInterchainAccountRequest{
Owner: ownerAddr.String(),
ConnectionId: path.EndpointA.ConnectionID,
})
Expand Down
5 changes: 4 additions & 1 deletion x/wasm/ibc_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package wasm_test
import (
"testing"

"github.com/CosmWasm/wasmd/app"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/CosmWasm/wasmd/x/wasm/types"
Expand Down Expand Up @@ -54,7 +56,8 @@ func TestOnChanOpenInitVersion(t *testing.T) {
chainA = coordinator.GetChain(wasmibctesting.GetChainID(1))
chainB = coordinator.GetChain(wasmibctesting.GetChainID(2))
myContractAddr = chainA.SeedNewContractInstance()
contractInfo = chainA.App.WasmKeeper.GetContractInfo(chainA.GetContext(), myContractAddr)
appA = chainA.App.(*app.WasmApp)
contractInfo = appA.WasmKeeper.GetContractInfo(chainA.GetContext(), myContractAddr)
)

path := wasmibctesting.NewPath(chainA, chainB)
Expand Down
Loading

0 comments on commit e36611a

Please sign in to comment.