diff --git a/tests/e2e/gov_test.go b/tests/e2e/gov_test.go index ac57b5fbd..5f3c0c55c 100644 --- a/tests/e2e/gov_test.go +++ b/tests/e2e/gov_test.go @@ -15,9 +15,9 @@ import ( distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" - "github.com/CosmWasm/wasmd/app" "github.com/CosmWasm/wasmd/tests/e2e" - "github.com/CosmWasm/wasmd/tests/ibctesting" + wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" + ibctesting "github.com/cosmos/ibc-go/v9/testing" ) func TestGovVoteByContract(t *testing.T) { @@ -26,10 +26,10 @@ func TestGovVoteByContract(t *testing.T) { // When the contract sends a vote for the proposal // Then the vote is taken into account - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) contractAddr := e2e.InstantiateReflectContract(t, chain) - chain.Fund(contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) + wasmibctesting.Fund(t, chain, contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) // a contract with a high delegation amount delegateMsg := wasmvmtypes.CosmosMsg{ Staking: &wasmvmtypes.StakingMsg{ @@ -45,9 +45,9 @@ func TestGovVoteByContract(t *testing.T) { e2e.MustExecViaReflectContract(t, chain, contractAddr, delegateMsg) signer := chain.SenderAccount.GetAddress().String() - app := chain.App.(*app.WasmApp) + app := chain.App.(*wasmibctesting.Dupa).App govKeeper, accountKeeper := app.GovKeeper, app.AccountKeeper - communityPoolBalance := chain.Balance(accountKeeper.GetModuleAccount(chain.GetContext(), distributiontypes.ModuleName).GetAddress(), sdk.DefaultBondDenom) + communityPoolBalance := app.GetBankKeeper().GetBalance(chain.GetContext(), accountKeeper.GetModuleAccount(chain.GetContext(), distributiontypes.ModuleName).GetAddress(), sdk.DefaultBondDenom) require.False(t, communityPoolBalance.IsZero()) gParams, err := govKeeper.Params.Get(chain.GetContext()) @@ -107,7 +107,7 @@ func TestGovVoteByContract(t *testing.T) { rsp, gotErr := chain.SendMsgs(msg) require.NoError(t, gotErr) var got v1.MsgSubmitProposalResponse - chain.UnwrapExecTXResult(rsp, &got) + wasmibctesting.UnwrapExecTXResult(t, chain, rsp, &got) propID := got.ProposalId @@ -131,7 +131,7 @@ func TestGovVoteByContract(t *testing.T) { coord.CommitBlock(chain) // and recipient balance updated - recipientBalance := chain.Balance(recipientAddr, sdk.DefaultBondDenom) + recipientBalance := app.GetBankKeeper().GetBalance(chain.GetContext(), recipientAddr, sdk.DefaultBondDenom) if !spec.expPass { assert.True(t, recipientBalance.IsZero()) return diff --git a/tests/e2e/grants_test.go b/tests/e2e/grants_test.go index 12b7e9e16..4e32b430f 100644 --- a/tests/e2e/grants_test.go +++ b/tests/e2e/grants_test.go @@ -20,8 +20,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/authz" "github.com/CosmWasm/wasmd/tests/e2e" - "github.com/CosmWasm/wasmd/tests/ibctesting" "github.com/CosmWasm/wasmd/x/wasm/types" + + wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" + ibctesting "github.com/cosmos/ibc-go/v9/testing" ) func TestGrants(t *testing.T) { @@ -33,7 +35,7 @@ func TestGrants(t *testing.T) { // - balance A reduced (on success) // - balance B not touched - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) contractAddr := e2e.InstantiateReflectContract(t, chain) require.NotEmpty(t, contractAddr) @@ -44,9 +46,9 @@ func TestGrants(t *testing.T) { otherPrivKey := secp256k1.GenPrivKey() otherAddr := sdk.AccAddress(otherPrivKey.PubKey().Address().Bytes()) - chain.Fund(granteeAddr, sdkmath.NewInt(1_000_000)) - chain.Fund(otherAddr, sdkmath.NewInt(1_000_000)) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) + wasmibctesting.Fund(t, chain, granteeAddr, sdkmath.NewInt(1_000_000)) + wasmibctesting.Fund(t, chain, otherAddr, sdkmath.NewInt(1_000_000)) + assert.Equal(t, sdkmath.NewInt(1_000_000), chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) myAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2_000_000)) @@ -97,7 +99,7 @@ func TestGrants(t *testing.T) { _, err = chain.SendMsgs(grantMsg) require.NoError(t, err) - granterStartBalance := chain.Balance(granterAddr, sdk.DefaultBondDenom).Amount + granterStartBalance := chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), granterAddr, sdk.DefaultBondDenom).Amount // when anyValidReflectMsg := []byte(fmt.Sprintf(`{"reflect_msg": {"msgs": [{"bank":{"burn":{"amount":[{"denom":%q, "amount": %q}]}}}]}}`, sdk.DefaultBondDenom, myAmount.Amount.String())) @@ -110,15 +112,16 @@ func TestGrants(t *testing.T) { _, gotErr := chain.SendNonDefaultSenderMsgs(spec.senderKey, &execMsg) // then + keeper := chain.App.(wasmibctesting.Dupa).GetBankKeeper() if spec.expErr != nil { require.ErrorContains(t, gotErr, fmt.Sprintf("%s/%d:", spec.expErr.Codespace(), spec.expErr.ABCICode())) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) - assert.Equal(t, granterStartBalance, chain.Balance(granterAddr, sdk.DefaultBondDenom).Amount) + assert.Equal(t, sdkmath.NewInt(1_000_000), keeper.GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) + assert.Equal(t, granterStartBalance, keeper.GetBalance(chain.GetContext(), granterAddr, sdk.DefaultBondDenom).Amount) return } require.NoError(t, gotErr) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) - assert.Equal(t, granterStartBalance.Sub(spec.transferAmount.Amount), chain.Balance(granterAddr, sdk.DefaultBondDenom).Amount) + assert.Equal(t, sdkmath.NewInt(1_000_000), keeper.GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) + assert.Equal(t, granterStartBalance.Sub(spec.transferAmount.Amount), keeper.GetBalance(chain.GetContext(), granterAddr, sdk.DefaultBondDenom).Amount) }) } } @@ -130,7 +133,7 @@ func TestStoreCodeGrant(t *testing.T) { reflectCodeChecksum, err := wasmvm.CreateChecksum(reflectWasmCode) require.NoError(t, err) - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) granterAddr := chain.SenderAccount.GetAddress() @@ -139,9 +142,9 @@ func TestStoreCodeGrant(t *testing.T) { otherPrivKey := secp256k1.GenPrivKey() otherAddr := sdk.AccAddress(otherPrivKey.PubKey().Address().Bytes()) - chain.Fund(granteeAddr, sdkmath.NewInt(1_000_000)) - chain.Fund(otherAddr, sdkmath.NewInt(1_000_000)) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) + wasmibctesting.Fund(t, chain, granteeAddr, sdkmath.NewInt(1_000_000)) + wasmibctesting.Fund(t, chain, otherAddr, sdkmath.NewInt(1_000_000)) + assert.Equal(t, sdkmath.NewInt(1_000_000), chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) specs := map[string]struct { codeHash []byte @@ -218,7 +221,7 @@ func TestGzipStoreCodeGrant(t *testing.T) { hackatomCodeChecksum, err := wasmvm.CreateChecksum(hackatomWasmCode) require.NoError(t, err) - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) granterAddr := chain.SenderAccount.GetAddress() @@ -227,9 +230,9 @@ func TestGzipStoreCodeGrant(t *testing.T) { otherPrivKey := secp256k1.GenPrivKey() otherAddr := sdk.AccAddress(otherPrivKey.PubKey().Address().Bytes()) - chain.Fund(granteeAddr, sdkmath.NewInt(1_000_000)) - chain.Fund(otherAddr, sdkmath.NewInt(1_000_000)) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) + wasmibctesting.Fund(t, chain, granteeAddr, sdkmath.NewInt(1_000_000)) + wasmibctesting.Fund(t, chain, otherAddr, sdkmath.NewInt(1_000_000)) + assert.Equal(t, sdkmath.NewInt(1_000_000), chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) specs := map[string]struct { codeHash []byte @@ -300,7 +303,7 @@ func TestBrokenGzipStoreCodeGrant(t *testing.T) { brokenGzipWasmCode, err := os.ReadFile("../../x/wasm/keeper/testdata/broken_crc.gzip") require.NoError(t, err) - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) granterAddr := chain.SenderAccount.GetAddress() @@ -309,9 +312,9 @@ func TestBrokenGzipStoreCodeGrant(t *testing.T) { otherPrivKey := secp256k1.GenPrivKey() otherAddr := sdk.AccAddress(otherPrivKey.PubKey().Address().Bytes()) - chain.Fund(granteeAddr, sdkmath.NewInt(1_000_000)) - chain.Fund(otherAddr, sdkmath.NewInt(1_000_000)) - assert.Equal(t, sdkmath.NewInt(1_000_000), chain.Balance(granteeAddr, sdk.DefaultBondDenom).Amount) + wasmibctesting.Fund(t, chain, granteeAddr, sdkmath.NewInt(1_000_000)) + wasmibctesting.Fund(t, chain, otherAddr, sdkmath.NewInt(1_000_000)) + assert.Equal(t, sdkmath.NewInt(1_000_000), chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), granteeAddr, sdk.DefaultBondDenom).Amount) codeHash := []byte("*") instantiatePermission := types.AllowEverybody diff --git a/tests/e2e/group_test.go b/tests/e2e/group_test.go index 288a296be..b097c2c2b 100644 --- a/tests/e2e/group_test.go +++ b/tests/e2e/group_test.go @@ -16,8 +16,10 @@ import ( "github.com/cosmos/cosmos-sdk/x/group" "github.com/CosmWasm/wasmd/tests/e2e" - "github.com/CosmWasm/wasmd/tests/ibctesting" "github.com/CosmWasm/wasmd/x/wasm/types" + + wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" + ibctesting "github.com/cosmos/ibc-go/v9/testing" ) func TestGroupWithContract(t *testing.T) { @@ -25,10 +27,10 @@ func TestGroupWithContract(t *testing.T) { // When contract submits a proposal with try_execute // Then the payload msg is executed - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) contractAddr := e2e.InstantiateStargateReflectContract(t, chain) - chain.Fund(contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) + wasmibctesting.Fund(t, chain, contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) members := []group.MemberRequest{ { @@ -50,10 +52,10 @@ func TestGroupWithContract(t *testing.T) { require.NoError(t, err) var createRsp group.MsgCreateGroupWithPolicyResponse - chain.UnwrapExecTXResult(rsp, &createRsp) + wasmibctesting.UnwrapExecTXResult(t, chain, rsp, &createRsp) groupID, policyAddr := createRsp.GroupId, sdk.MustAccAddressFromBech32(createRsp.GroupPolicyAddress) require.NotEmpty(t, groupID) - chain.Fund(policyAddr, sdkmath.NewIntFromUint64(1_000_000_000)) + wasmibctesting.Fund(t, chain, policyAddr, sdkmath.NewIntFromUint64(1_000_000_000)) // and a proposal submitted recipientAddr := sdk.AccAddress(rand.Bytes(address.Len)) @@ -63,14 +65,14 @@ func TestGroupWithContract(t *testing.T) { rsp = e2e.MustExecViaStargateReflectContract(t, chain, contractAddr, propMsg) var execRsp types.MsgExecuteContractResponse - chain.UnwrapExecTXResult(rsp, &execRsp) + wasmibctesting.UnwrapExecTXResult(t, chain, rsp, &execRsp) var groupRsp group.MsgSubmitProposalResponse require.NoError(t, chain.Codec.Unmarshal(execRsp.Data, &groupRsp)) // require.NotEmpty(t, groupRsp.ProposalId) // and coins received - recipientBalance := chain.Balance(recipientAddr, sdk.DefaultBondDenom) + recipientBalance := chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), recipientAddr, sdk.DefaultBondDenom) expBalanceAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.OneInt()) assert.Equal(t, expBalanceAmount.String(), recipientBalance.String()) } @@ -80,10 +82,10 @@ func TestGroupWithNewReflectContract(t *testing.T) { // When contract submits a proposal with try_execute // Then the payload msg is executed - coord := ibctesting.NewCoordinator(t, 1) + coord := wasmibctesting.NewCoordinator2(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) contractAddr := e2e.InstantiateReflectContract(t, chain) - chain.Fund(contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) + wasmibctesting.Fund(t, chain, contractAddr, sdkmath.NewIntFromUint64(1_000_000_000)) members := []group.MemberRequest{ { @@ -105,10 +107,10 @@ func TestGroupWithNewReflectContract(t *testing.T) { require.NoError(t, err) var createRsp group.MsgCreateGroupWithPolicyResponse - chain.UnwrapExecTXResult(rsp, &createRsp) + wasmibctesting.UnwrapExecTXResult(t, chain, rsp, &createRsp) groupID, policyAddr := createRsp.GroupId, sdk.MustAccAddressFromBech32(createRsp.GroupPolicyAddress) require.NotEmpty(t, groupID) - chain.Fund(policyAddr, sdkmath.NewIntFromUint64(1_000_000_000)) + wasmibctesting.Fund(t, chain, policyAddr, sdkmath.NewIntFromUint64(1_000_000_000)) // and a proposal submitted recipientAddr := sdk.AccAddress(rand.Bytes(address.Len)) @@ -118,13 +120,13 @@ func TestGroupWithNewReflectContract(t *testing.T) { rsp = e2e.MustExecViaAnyReflectContract(t, chain, contractAddr, propMsg) var execRsp types.MsgExecuteContractResponse - chain.UnwrapExecTXResult(rsp, &execRsp) + wasmibctesting.UnwrapExecTXResult(t, chain, rsp, &execRsp) var groupRsp group.MsgSubmitProposalResponse require.NoError(t, chain.Codec.Unmarshal(execRsp.Data, &groupRsp)) // and coins received - recipientBalance := chain.Balance(recipientAddr, sdk.DefaultBondDenom) + recipientBalance := chain.App.(wasmibctesting.Dupa).GetBankKeeper().GetBalance(chain.GetContext(), recipientAddr, sdk.DefaultBondDenom) expBalanceAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.OneInt()) assert.Equal(t, expBalanceAmount.String(), recipientBalance.String()) } diff --git a/tests/e2e/ibc_callbacks_test.go b/tests/e2e/ibc_callbacks_test.go index bd85f6d3b..3bcb88b45 100644 --- a/tests/e2e/ibc_callbacks_test.go +++ b/tests/e2e/ibc_callbacks_test.go @@ -33,14 +33,14 @@ func TestIBCCallbacks(t *testing.T) { // then the contract on B should receive a destination chain callback // and the contract on A should receive a source chain callback with the result (ack or timeout) marshaler := app.MakeEncodingConfig(t).Codec - coord := wasmibctesting.NewCoordinator(t, 2) + coord := wasmibctesting.NewCoordinator2(t, 2) chainA := coord.GetChain(wasmibctesting.GetChainID(1)) chainB := coord.GetChain(wasmibctesting.GetChainID(2)) actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address()) oneToken := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1))) - path := wasmibctesting.NewPath(chainA, chainB) + path := ibctesting.NewPath(chainA, chainB) path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ PortID: ibctransfertypes.PortID, Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), @@ -55,10 +55,10 @@ func TestIBCCallbacks(t *testing.T) { coord.Setup(path) // with an ibc-callbacks contract deployed on chain A - codeIDonA := chainA.StoreCodeFile("./testdata/ibc_callbacks.wasm").CodeID + codeIDonA := wasmibctesting.StoreCodeFile(t, chainA, "./testdata/ibc_callbacks.wasm").CodeID // and on chain B - codeIDonB := chainB.StoreCodeFile("./testdata/ibc_callbacks.wasm").CodeID + codeIDonB := wasmibctesting.StoreCodeFile(t, chainB, "./testdata/ibc_callbacks.wasm").CodeID type TransferExecMsg struct { ToAddress string `json:"to_address"` @@ -105,9 +105,9 @@ func TestIBCCallbacks(t *testing.T) { for name, spec := range specs { t.Run(name, func(t *testing.T) { - contractAddrA := chainA.InstantiateContract(codeIDonA, []byte(`{}`)) + contractAddrA := wasmibctesting.InstantiateContract(t, chainA, codeIDonA, []byte(`{}`)) require.NotEmpty(t, contractAddrA) - contractAddrB := chainB.InstantiateContract(codeIDonB, []byte(`{}`)) + contractAddrB := wasmibctesting.InstantiateContract(t, chainB, codeIDonB, []byte(`{}`)) require.NotEmpty(t, contractAddrB) if spec.contractMsg.Transfer != nil && spec.contractMsg.Transfer.ToAddress == "" { @@ -178,13 +178,13 @@ func TestIBCCallbacksWithoutEntrypoints(t *testing.T) { // then the VM should try to call the callback on B and fail gracefully // and should try to call the callback on A and fail gracefully marshaler := app.MakeEncodingConfig(t).Codec - coord := wasmibctesting.NewCoordinator(t, 2) - chainA := coord.GetChain(wasmibctesting.GetChainID(1)) - chainB := coord.GetChain(wasmibctesting.GetChainID(2)) + coord := wasmibctesting.NewCoordinator2(t, 2) + chainA := coord.GetChain(ibctesting.GetChainID(1)) + chainB := coord.GetChain(ibctesting.GetChainID(2)) oneToken := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1)) - path := wasmibctesting.NewPath(chainA, chainB) + path := ibctesting.NewPath(chainA, chainB) path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ PortID: ibctransfertypes.PortID, Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), @@ -200,26 +200,27 @@ func TestIBCCallbacksWithoutEntrypoints(t *testing.T) { // with a reflect contract deployed on chain A and B contractAddrA := e2e.InstantiateReflectContract(t, chainA) - chainA.Fund(contractAddrA, oneToken.Amount) + wasmibctesting.Fund(t, chainA, contractAddrA, oneToken.Amount) contractAddrB := e2e.InstantiateReflectContract(t, chainA) // when the contract on A sends an IBCMsg::Transfer to the contract on B memo := fmt.Sprintf(`{"src_callback":{"address":"%v"},"dest_callback":{"address":"%v"}}`, contractAddrA.String(), contractAddrB.String()) - e2e.MustExecViaReflectContract(t, chainA, contractAddrA, wasmvmtypes.CosmosMsg{ + result := e2e.MustExecViaReflectContract(t, chainA, contractAddrA, wasmvmtypes.CosmosMsg{ IBC: &wasmvmtypes.IBCMsg{ Transfer: &wasmvmtypes.TransferMsg{ ToAddress: contractAddrB.String(), ChannelID: path.EndpointA.ChannelID, Amount: wasmvmtypes.NewCoin(oneToken.Amount.Uint64(), oneToken.Denom), Timeout: wasmvmtypes.IBCTimeout{ - Timestamp: uint64(chainA.LastHeader.GetTime().Add(time.Second * 100).UnixNano()), + Timestamp: uint64(chainA.ProposedHeader.GetTime().Add(time.Second * 100).UnixNano()), }, Memo: memo, }, }, }) - // and the packet is relayed without problems - require.NoError(t, coord.RelayAndAckPendingPackets(path)) - assert.Empty(t, chainA.PendingSendPackets) + packet, err := ibctesting.ParsePacketFromEvents(result.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) } diff --git a/tests/e2e/ibc_fees_test.go b/tests/e2e/ibc_fees_test.go index 9df428dcf..ee56e72c8 100644 --- a/tests/e2e/ibc_fees_test.go +++ b/tests/e2e/ibc_fees_test.go @@ -9,10 +9,10 @@ import ( "testing" "time" + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types" ibcfee "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types" - ibctransfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types" - clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" //nolint:staticcheck + ibctransfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types" //nolint:staticcheck channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" ibctesting "github.com/cosmos/ibc-go/v9/testing" "github.com/stretchr/testify/assert" @@ -22,12 +22,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" + clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" "strings" "github.com/CosmWasm/wasmd/app" wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" - wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" ) // GetTransferCoin creates a transfer coin with the port ID and channel ID @@ -50,9 +50,9 @@ func TestIBCFeesTransfer(t *testing.T) { // 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(t).Codec - coord := wasmibctesting.NewCoordinator(t, 2) - chainA := coord.GetChain(wasmibctesting.GetChainID(1)) - chainB := coord.GetChain(wasmibctesting.GetChainID(2)) + coord := wasmibctesting.NewCoordinator2(t, 2) + chainA := coord.GetChain(ibctesting.GetChainID(1)) + chainB := coord.GetChain(ibctesting.GetChainID(2)) actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address()) actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address()) @@ -60,7 +60,7 @@ func TestIBCFeesTransfer(t *testing.T) { payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len)) oneToken := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1))) - path := wasmibctesting.NewPath(chainA, chainB) + path := ibctesting.NewPath(chainA, chainB) path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ PortID: ibctransfertypes.PortID, Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), @@ -73,8 +73,8 @@ func TestIBCFeesTransfer(t *testing.T) { } // with an ics-20 transfer channel setup between both chains coord.Setup(path) - appA := chainA.App.(*app.WasmApp) - require.True(t, appA.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)) + appA := chainA.App.(wasmibctesting.Dupa) + require.True(t, appA.GetIBCFeeKeeper().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) @@ -86,20 +86,24 @@ func TestIBCFeesTransfer(t *testing.T) { ibcPayloadMsg := ibctransfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, []sdk.Coin{transferCoin}, actorChainA.String(), receiver.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()), "testing", &ibctransfertypes.Forwarding{Unwind: false, Hops: []ibctransfertypes.Hop{}}) ibcPackageFee := ibcfee.NewFee(oneToken, oneToken, sdk.Coins{}) feeMsg := ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), nil) - _, err = chainA.SendMsgs(feeMsg, ibcPayloadMsg) + result, err := chainA.SendMsgs(feeMsg, ibcPayloadMsg) require.NoError(t, err) - pendingIncentivisedPackages := appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) + pendingIncentivisedPackages := appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) assert.Len(t, pendingIncentivisedPackages, 1) require.NoError(t, err) // message committed - require.NoError(t, coord.RelayAndAckPendingPackets(path)) + packet, err := ibctesting.ParsePacketFromEvents(result.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) // then + appB := chainB.App.(wasmibctesting.Dupa) expBalance := GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, transferCoin.Denom, transferCoin.Amount) - gotBalance := chainB.Balance(receiver, expBalance.Denom) + gotBalance := appB.GetBankKeeper().GetBalance(chainB.GetContext(), receiver, expBalance.Denom) assert.Equal(t, expBalance.String(), gotBalance.String()) - payeeBalance := chainA.AllBalances(payee) + payeeBalance := appA.GetBankKeeper().GetAllBalances(chainA.GetContext(), payee) assert.Equal(t, oneToken.Add(oneToken...).String(), payeeBalance.String()) // and with a payee registered for chain B to A @@ -112,20 +116,22 @@ func TestIBCFeesTransfer(t *testing.T) { ibcPayloadMsg = ibctransfertypes.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, []sdk.Coin{transferCoin}, actorChainB.String(), receiver.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()), "more testing", &ibctransfertypes.Forwarding{Unwind: false, Hops: []ibctransfertypes.Hop{}}) ibcPackageFee = ibcfee.NewFee(oneToken, oneToken, sdk.Coins{}) feeMsg = ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), nil) - _, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg) + result, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg) require.NoError(t, err) - appB := chainB.App.(*app.WasmApp) - pendingIncentivisedPackages = appB.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID) + pendingIncentivisedPackages = appB.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID) assert.Len(t, pendingIncentivisedPackages, 1) // when packages relayed - require.NoError(t, coord.RelayAndAckPendingPackets(path)) + packet, err = ibctesting.ParsePacketFromEvents(result.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) // then expBalance = GetTransferCoin(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, transferCoin.Denom, transferCoin.Amount) - gotBalance = chainA.Balance(receiver, expBalance.Denom) + gotBalance = appA.GetBankKeeper().GetBalance(chainA.GetContext(), receiver, expBalance.Denom) assert.Equal(t, expBalance.String(), gotBalance.String()) - payeeBalance = chainB.AllBalances(payee) + payeeBalance = appB.GetBankKeeper().GetAllBalances(chainB.GetContext(), payee) assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2)).String(), payeeBalance.String()) } @@ -136,27 +142,28 @@ func TestIBCFeesWasm(t *testing.T) { // 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(t).Codec - coord := wasmibctesting.NewCoordinator(t, 2) + coord := wasmibctesting.NewCoordinator2(t, 2) + chainA := coord.GetChain(ibctesting.GetChainID(1)) chainB := coord.GetChain(ibctesting.GetChainID(2)) 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 + codeID := wasmibctesting.StoreCodeFile(t, chainA, "./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) + cw20ContractAddr := wasmibctesting.InstantiateContract(t, chainA, 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 + codeID = wasmibctesting.StoreCodeFile(t, chainA, "./testdata/cw20_ics20.wasm.gz").CodeID + ibcContractAddr := wasmibctesting.InstantiateContract(t, chainA, codeID, initMsg) + ibcContractPortID := wasmibctesting.ContractInfo(chainA, ibcContractAddr).IBCPortID payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len)) oneToken := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(1))) - path := wasmibctesting.NewPath(chainA, chainB) + path := ibctesting.NewPath(chainA, chainB) path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ PortID: ibcContractPortID, Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V1})), @@ -169,10 +176,10 @@ func TestIBCFeesWasm(t *testing.T) { } // with an ics-29 fee enabled channel setup between both chains coord.Setup(path) - 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)) + appA := chainA.App.(wasmibctesting.Dupa) + appB := chainB.App.(wasmibctesting.Dupa) + require.True(t, appA.GetIBCFeeKeeper().IsFeeEnabled(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID)) + require.True(t, appB.GetIBCFeeKeeper().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) @@ -189,27 +196,30 @@ func TestIBCFeesWasm(t *testing.T) { } ibcPackageFee := ibcfee.NewFee(oneToken, oneToken, sdk.Coins{}) feeMsg := ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibcContractPortID, path.EndpointA.ChannelID, actorChainA.String(), nil) - _, err = chainA.SendMsgs(feeMsg, &execMsg) + result, err := chainA.SendMsgs(feeMsg, &execMsg) require.NoError(t, err) - pendingIncentivisedPackages := appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID) + pendingIncentivisedPackages := appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID) assert.Len(t, pendingIncentivisedPackages, 1) // and packages relayed - require.NoError(t, coord.RelayAndAckPendingPackets(path)) + packet, err := ibctesting.ParsePacketFromEvents(result.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) // then // on chain A - gotCW20Balance, err := appA.WasmKeeper.QuerySmart(chainA.GetContext(), cw20ContractAddr, []byte(fmt.Sprintf(`{"balance":{"address": %q}}`, actorChainA.String()))) + gotCW20Balance, err := appA.GetWasmKeeper().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) + payeeBalance := appA.GetBankKeeper().GetAllBalances(chainA.GetContext(), payee) assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2)).String(), payeeBalance.String()) // and on chain B - pendingIncentivisedPackages = appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID) + pendingIncentivisedPackages = appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID) assert.Len(t, pendingIncentivisedPackages, 0) expBalance := GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, "cw20:"+cw20ContractAddr.String(), sdkmath.NewInt(100)) - gotBalance := chainB.Balance(actorChainB, expBalance.Denom) - assert.Equal(t, expBalance.String(), gotBalance.String(), chainB.AllBalances(actorChainB)) + gotBalance := appB.GetBankKeeper().GetBalance(chainB.GetContext(), actorChainB, expBalance.Denom) + assert.Equal(t, expBalance.String(), gotBalance.String(), appB.GetBankKeeper().GetAllBalances(chainB.GetContext(), actorChainB)) // and with a payee registered for chain B to A _, err = chainA.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibcContractPortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String())) @@ -221,21 +231,24 @@ func TestIBCFeesWasm(t *testing.T) { ibcPayloadMsg := ibctransfertypes.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, []sdk.Coin{gotBalance}, actorChainB.String(), actorChainA.String(), clienttypes.Height{}, uint64(time.Now().Add(time.Minute).UnixNano()), "even more tests", &ibctransfertypes.Forwarding{Unwind: false, Hops: []ibctransfertypes.Hop{}}) ibcPackageFee = ibcfee.NewFee(oneToken, oneToken, sdk.Coins{}) feeMsg = ibcfee.NewMsgPayPacketFee(ibcPackageFee, ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), nil) - _, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg) + result, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg) require.NoError(t, err) - pendingIncentivisedPackages = appB.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID) + pendingIncentivisedPackages = appB.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID) assert.Len(t, pendingIncentivisedPackages, 1) // when packages relayed - require.NoError(t, coord.RelayAndAckPendingPackets(path)) + packet, err = ibctesting.ParsePacketFromEvents(result.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) // then // on chain A - gotCW20Balance, err = appA.WasmKeeper.QuerySmart(chainA.GetContext(), cw20ContractAddr, []byte(fmt.Sprintf(`{"balance":{"address": %q}}`, actorChainA.String()))) + gotCW20Balance, err = appA.GetWasmKeeper().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) + payeeBalance = appB.GetBankKeeper().GetAllBalances(chainB.GetContext(), payee) assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2)).String(), payeeBalance.String()) } @@ -247,22 +260,22 @@ func TestIBCFeesReflect(t *testing.T) { // then the relayer's payee is receiving the fee(s) on success marshaler := app.MakeEncodingConfig(t).Codec - coord := wasmibctesting.NewCoordinator(t, 2) + coord := wasmibctesting.NewCoordinator2(t, 2) chainA := coord.GetChain(wasmibctesting.GetChainID(1)) chainB := coord.GetChain(ibctesting.GetChainID(2)) actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address()) actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address()) // setup chain A - codeID := chainA.StoreCodeFile("./testdata/reflect_2_2.wasm").CodeID + codeID := wasmibctesting.StoreCodeFile(t, chainA, "./testdata/reflect_2_2.wasm").CodeID initMsg := []byte("{}") - reflectContractAddr := chainA.InstantiateContract(codeID, initMsg) + reflectContractAddr := wasmibctesting.InstantiateContract(t, chainA, codeID, initMsg) payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len)) oneToken := []wasmvmtypes.Coin{wasmvmtypes.NewCoin(1, sdk.DefaultBondDenom)} - path := wasmibctesting.NewPath(chainA, chainB) + path := ibctesting.NewPath(chainA, chainB) path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ PortID: ibctransfertypes.PortID, Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), @@ -275,10 +288,10 @@ func TestIBCFeesReflect(t *testing.T) { } // with an ics-29 fee enabled channel setup between both chains coord.Setup(path) - appA := chainA.App.(*app.WasmApp) - appB := chainB.App.(*app.WasmApp) - require.True(t, appA.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)) - require.True(t, appB.IBCFeeKeeper.IsFeeEnabled(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID)) + appA := chainA.App.(wasmibctesting.Dupa) + appB := chainB.App.(wasmibctesting.Dupa) + require.True(t, appA.GetIBCFeeKeeper().IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)) + require.True(t, appB.GetIBCFeeKeeper().IsFeeEnabled(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID)) // and with a payee registered for A -> B _, err := chainA.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String())) require.NoError(t, err) @@ -286,7 +299,7 @@ func TestIBCFeesReflect(t *testing.T) { require.NoError(t, err) // when reflect contract on A sends a PayPacketFee msg, followed by a transfer - _, err = ExecViaReflectContract(t, chainA, reflectContractAddr, []wasmvmtypes.CosmosMsg{ + result1, err := ExecViaReflectContract(t, chainA, reflectContractAddr, []wasmvmtypes.CosmosMsg{ { IBC: &wasmvmtypes.IBCMsg{ PayPacketFee: &wasmvmtypes.PayPacketFeeMsg{ @@ -316,7 +329,7 @@ func TestIBCFeesReflect(t *testing.T) { }) require.NoError(t, err) - pendingIncentivisedPackages := appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) + pendingIncentivisedPackages := appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) assert.Len(t, pendingIncentivisedPackages, 1) // and sends an PayPacketFeeAsync msg @@ -340,17 +353,20 @@ func TestIBCFeesReflect(t *testing.T) { require.NoError(t, err) // and packages relayed - require.NoError(t, coord.RelayAndAckPendingPackets(path)) + packet, err := ibctesting.ParsePacketFromEvents(result1.Events) + require.NoError(t, err) + err = path.RelayPacket(packet) + require.NoError(t, err) // then // on chain A - payeeBalance := chainA.AllBalances(payee) + payeeBalance := appA.GetBankKeeper().GetAllBalances(chainA.GetContext(), payee) // 2 tokens from the PayPacketFee and 1 token from the PayPacketFeeAsync assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(3)).String(), payeeBalance.String()) // and on chain B - pendingIncentivisedPackages = appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) + pendingIncentivisedPackages = appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID) assert.Len(t, pendingIncentivisedPackages, 0) expBalance := GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sdk.DefaultBondDenom, sdkmath.NewInt(10)) - gotBalance := chainB.Balance(actorChainB, expBalance.Denom) - assert.Equal(t, expBalance.String(), gotBalance.String(), chainB.AllBalances(actorChainB)) + gotBalance := appB.GetBankKeeper().GetBalance(chainB.GetContext(), actorChainB, expBalance.Denom) + assert.Equal(t, expBalance.String(), gotBalance.String(), appB.GetBankKeeper().GetAllBalances(chainB.GetContext(), actorChainB)) } diff --git a/tests/e2e/ica_test.go b/tests/e2e/ica_test.go index 6904e1600..432aefdd6 100644 --- a/tests/e2e/ica_test.go +++ b/tests/e2e/ica_test.go @@ -22,7 +22,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/address" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/CosmWasm/wasmd/app" wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" ) @@ -33,15 +32,15 @@ func TestICA(t *testing.T) { // and the channel is established to the host chain // then the ICA owner can submit a message via IBC // to control their account on the host chain - coord := wasmibctesting.NewCoordinator(t, 2) + coord := wasmibctesting.NewCoordinator2(t, 2) hostChain := coord.GetChain(ibctesting.GetChainID(1)) hostParams := hosttypes.NewParams(true, []string{sdk.MsgTypeURL(&banktypes.MsgSend{})}) - hostApp := hostChain.App.(*app.WasmApp) + hostApp := hostChain.App.(*wasmibctesting.Dupa) hostApp.ICAHostKeeper.SetParams(hostChain.GetContext(), hostParams) controllerChain := coord.GetChain(ibctesting.GetChainID(2)) - path := wasmibctesting.NewPath(controllerChain, hostChain) + path := ibctesting.NewPath(controllerChain, hostChain) coord.SetupConnections(path) specs := map[string]struct { @@ -67,7 +66,7 @@ func TestICA(t *testing.T) { t.Run(name, func(t *testing.T) { icaControllerKey := secp256k1.GenPrivKey() icaControllerAddr := sdk.AccAddress(icaControllerKey.PubKey().Address().Bytes()) - controllerChain.Fund(icaControllerAddr, sdkmath.NewInt(1_000)) + wasmibctesting.Fund(t, controllerChain, icaControllerAddr, sdkmath.NewInt(1_000)) msg := icacontrollertypes.NewMsgRegisterInterchainAccount(path.EndpointA.ConnectionID, icaControllerAddr.String(), spec.icaVersion, channeltypes.NONE) res, err := controllerChain.SendNonDefaultSenderMsgs(icaControllerKey, msg) @@ -90,14 +89,14 @@ func TestICA(t *testing.T) { coord.CreateChannels(path) // assert ICA exists on controller - contApp := controllerChain.App.(*app.WasmApp) + contApp := controllerChain.App.(*wasmibctesting.Dupa) icaRsp, err := contApp.ICAControllerKeeper.InterchainAccount(controllerChain.GetContext(), &icacontrollertypes.QueryInterchainAccountRequest{ Owner: icaControllerAddr.String(), ConnectionId: path.EndpointA.ConnectionID, }) require.NoError(t, err) icaAddr := sdk.MustAccAddressFromBech32(icaRsp.GetAddress()) - hostChain.Fund(icaAddr, sdkmath.NewInt(1_000)) + wasmibctesting.Fund(t, hostChain, icaAddr, sdkmath.NewInt(1_000)) // submit a tx targetAddr := sdk.AccAddress(rand.Bytes(address.Len)) diff --git a/tests/e2e/reflect_helper.go b/tests/e2e/reflect_helper.go index 08a8fe2c0..f2be0986e 100644 --- a/tests/e2e/reflect_helper.go +++ b/tests/e2e/reflect_helper.go @@ -13,24 +13,25 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/CosmWasm/wasmd/tests/ibctesting" + wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting" "github.com/CosmWasm/wasmd/x/wasm/keeper/testdata" "github.com/CosmWasm/wasmd/x/wasm/types" + ibctesting "github.com/cosmos/ibc-go/v9/testing" ) // InstantiateStargateReflectContract stores and instantiates the reflect contract shipped with CosmWasm 1.5.3. // This instance still expects the old CosmosMsg.Stargate variant instead of the new CosmosMsg.Any. func InstantiateStargateReflectContract(t *testing.T, chain *ibctesting.TestChain) sdk.AccAddress { - codeID := chain.StoreCodeFile("../../x/wasm/keeper/testdata/reflect_1_5.wasm").CodeID - contractAddr := chain.InstantiateContract(codeID, []byte(`{}`)) + codeID := wasmibctesting.StoreCodeFile(t, chain, "../../x/wasm/keeper/testdata/reflect_1_5.wasm").CodeID + contractAddr := wasmibctesting.InstantiateContract(t, chain, codeID, []byte(`{}`)) require.NotEmpty(t, contractAddr) return contractAddr } // InstantiateReflectContract stores and instantiates a 2.0 reflect contract instance. func InstantiateReflectContract(t *testing.T, chain *ibctesting.TestChain) sdk.AccAddress { - codeID := chain.StoreCodeFile("../../x/wasm/keeper/testdata/reflect_2_0.wasm").CodeID - contractAddr := chain.InstantiateContract(codeID, []byte(`{}`)) + codeID := wasmibctesting.StoreCodeFile(t, chain, "../../x/wasm/keeper/testdata/reflect_2_0.wasm").CodeID + contractAddr := wasmibctesting.InstantiateContract(t, chain, codeID, []byte(`{}`)) require.NotEmpty(t, contractAddr) return contractAddr } diff --git a/tests/ibctesting/chain2.go b/tests/ibctesting/chain2.go new file mode 100644 index 000000000..3b95a5a7c --- /dev/null +++ b/tests/ibctesting/chain2.go @@ -0,0 +1,352 @@ +package ibctesting + +import ( + "bytes" + "compress/gzip" + "context" + "os" + "strings" + "testing" + + "cosmossdk.io/math" + storetypes "cosmossdk.io/store/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + cmttypes "github.com/cometbft/cometbft/types" + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/gogoproto/proto" + ibcfeekeeper "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/keeper" + + "github.com/CosmWasm/wasmd/app" + wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" + "github.com/CosmWasm/wasmd/x/wasm/types" + abci "github.com/cometbft/cometbft/abci/types" + capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" + ibckeeper "github.com/cosmos/ibc-go/v9/modules/core/keeper" + ibctesting "github.com/cosmos/ibc-go/v9/testing" + ibctestingtypes "github.com/cosmos/ibc-go/v9/testing/types" +) + +type Dupa struct { + App *app.WasmApp +} + +// ibc-go additions +func (app Dupa) GetBaseApp() *baseapp.BaseApp { + return app.App.BaseApp +} + +func (app Dupa) GetStakingKeeper() ibctestingtypes.StakingKeeper { + return app.App.GetStakingKeeper() +} + +func (app Dupa) GetIBCKeeper() *ibckeeper.Keeper { + return app.App.GetIBCKeeper() +} + +func (app Dupa) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper { + return app.App.GetScopedIBCKeeper() +} + +func (app Dupa) GetTxConfig() client.TxConfig { + return app.App.TxConfig() +} + +func (app Dupa) AppCodec() codec.Codec { + return app.App.AppCodec() +} + +func (app Dupa) LastCommitID() storetypes.CommitID { + return app.App.LastCommitID() +} + +func (app Dupa) LastBlockHeight() int64 { + return app.App.LastBlockHeight() +} + +func (app Dupa) Info(a *abci.RequestInfo) (*abci.ResponseInfo, error) { + return app.App.Info(a) +} + +func (app Dupa) Query(c context.Context, a *abci.RequestQuery) (*abci.ResponseQuery, error) { + return app.App.Query(c, a) +} + +func (app Dupa) CheckTx(a *abci.RequestCheckTx) (*abci.ResponseCheckTx, error) { + return app.App.CheckTx(a) +} + +func (app Dupa) InitChain(a *abci.RequestInitChain) (*abci.ResponseInitChain, error) { + return app.App.InitChain(a) +} + +func (app Dupa) PrepareProposal(a *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) { + return app.App.PrepareProposal(a) +} + +func (app Dupa) ProcessProposal(a *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) { + return app.App.ProcessProposal(a) +} + +func (app Dupa) FinalizeBlock(a *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) { + return app.App.FinalizeBlock(a) +} + +func (app Dupa) ExtendVote(c context.Context, a *abci.RequestExtendVote) (*abci.ResponseExtendVote, error) { + return app.App.ExtendVote(c, a) +} + +func (app Dupa) VerifyVoteExtension(a *abci.RequestVerifyVoteExtension) (*abci.ResponseVerifyVoteExtension, error) { + return app.App.VerifyVoteExtension(a) +} + +func (app Dupa) Commit() (*abci.ResponseCommit, error) { + return app.App.Commit() +} + +func (app Dupa) ListSnapshots(a *abci.RequestListSnapshots) (*abci.ResponseListSnapshots, error) { + return app.App.ListSnapshots(a) +} + +func (app Dupa) OfferSnapshot(a *abci.RequestOfferSnapshot) (*abci.ResponseOfferSnapshot, error) { + return app.App.OfferSnapshot(a) +} + +func (app Dupa) LoadSnapshotChunk(a *abci.RequestLoadSnapshotChunk) (*abci.ResponseLoadSnapshotChunk, error) { + return app.App.LoadSnapshotChunk(a) +} + +func (app Dupa) ApplySnapshotChunk(a *abci.RequestApplySnapshotChunk) (*abci.ResponseApplySnapshotChunk, error) { + return app.App.ApplySnapshotChunk(a) +} + +func (app Dupa) GetIBCFeeKeeper() ibcfeekeeper.Keeper { + return app.App.IBCFeeKeeper +} + +func (app Dupa) GetBankKeeper() bankkeeper.Keeper { + return app.App.GetBankKeeper() +} + +func (app Dupa) GetWasmKeeper() wasmkeeper.Keeper { + return app.App.GetWasmKeeper() +} + +func StoreCodeFile(t *testing.T, chain *ibctesting.TestChain, filename string) types.MsgStoreCodeResponse { + wasmCode, err := os.ReadFile(filename) + require.NoError(t, err) + if strings.HasSuffix(filename, "wasm") { // compress for gas limit + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + _, err := gz.Write(wasmCode) + require.NoError(t, err) + err = gz.Close() + require.NoError(t, err) + wasmCode = buf.Bytes() + } + return StoreCode(t, chain, wasmCode) +} + +func StoreCode(t *testing.T, chain *ibctesting.TestChain, byteCode []byte) types.MsgStoreCodeResponse { + storeMsg := &types.MsgStoreCode{ + Sender: chain.SenderAccount.GetAddress().String(), + WASMByteCode: byteCode, + } + r, err := chain.SendMsgs(storeMsg) + require.NoError(t, err) + + var pInstResp types.MsgStoreCodeResponse + UnwrapExecTXResult(t, chain, r, &pInstResp) + + require.NotEmpty(t, pInstResp.CodeID) + require.NotEmpty(t, pInstResp.Checksum) + return pInstResp +} + +// UnwrapExecTXResult is a helper to unpack execution result from proto any type +func UnwrapExecTXResult(t *testing.T, chain *ibctesting.TestChain, r *abci.ExecTxResult, target proto.Message) { + var wrappedRsp sdk.TxMsgData + require.NoError(t, chain.App.AppCodec().Unmarshal(r.Data, &wrappedRsp)) + + // unmarshal protobuf response from data + require.Len(t, wrappedRsp.MsgResponses, 1) + require.NoError(t, proto.Unmarshal(wrappedRsp.MsgResponses[0].Value, target)) +} + +func InstantiateContract(t *testing.T, chain *ibctesting.TestChain, codeID uint64, initMsg []byte) sdk.AccAddress { + instantiateMsg := &types.MsgInstantiateContract{ + Sender: chain.SenderAccount.GetAddress().String(), + Admin: chain.SenderAccount.GetAddress().String(), + CodeID: codeID, + Label: "ibc-test", + Msg: initMsg, + Funds: sdk.Coins{ibctesting.TestCoin}, + } + + r, err := chain.SendMsgs(instantiateMsg) + require.NoError(t, err) + + var pExecResp types.MsgInstantiateContractResponse + UnwrapExecTXResult(t, chain, r, &pExecResp) + + a, err := sdk.AccAddressFromBech32(pExecResp.Address) + require.NoError(t, err) + return a +} + +func ContractInfo(chain *ibctesting.TestChain, contractAddr sdk.AccAddress) *types.ContractInfo { + return chain.App.(Dupa).GetWasmKeeper().GetContractInfo(chain.GetContext(), contractAddr) +} + +// Fund an address with the given amount in default denom +func Fund(t *testing.T, chain *ibctesting.TestChain, addr sdk.AccAddress, amount math.Int) { + _, err := chain.SendMsgs(&banktypes.MsgSend{ + FromAddress: chain.SenderAccount.GetAddress().String(), + ToAddress: addr.String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), + }) + require.NoError(t, err) +} + +// ChainAppFactory abstract factory method that usually implemented by app.SetupWithGenesisValSet +type ChainAppFactory2 func(t *testing.T, valSet *cmttypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, opts []wasmkeeper.Option, balances ...banktypes.Balance) Dupa + +// DefaultWasmAppFactory instantiates and sets up the default wasmd app +func DefaultWasmAppFactory2(t *testing.T, valSet *cmttypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, opts []wasmkeeper.Option, balances ...banktypes.Balance) Dupa { + return Dupa{App: app.SetupWithGenesisValSet(t, valSet, genAccs, chainID, opts, balances...)} +} + +// NewDefaultTestChain initializes a new test chain with a default of 4 validators +// Use this function if the tests do not need custom control over the validator set +func NewDefaultTestChain2(t *testing.T, coord *ibctesting.Coordinator, chainID string, opts ...wasmkeeper.Option) *ibctesting.TestChain { + return NewTestChain2(t, coord, DefaultWasmAppFactory2, chainID, opts...) +} + +// NewTestChain initializes a new test chain with a default of 4 validators +// Use this function if the tests do not need custom control over the validator set +func NewTestChain2(t *testing.T, coord *ibctesting.Coordinator, appFactory ChainAppFactory2, chainID string, opts ...wasmkeeper.Option) *ibctesting.TestChain { + // generate validators private/public key + var ( + validatorsPerChain = 4 + validators = make([]*cmttypes.Validator, 0, validatorsPerChain) + signersByAddress = make(map[string]cmttypes.PrivValidator, validatorsPerChain) + ) + + for i := 0; i < validatorsPerChain; i++ { + _, privVal := cmttypes.RandValidator(false, 100) + pubKey, err := privVal.GetPubKey() + require.NoError(t, err) + validators = append(validators, cmttypes.NewValidator(pubKey, 1)) + signersByAddress[pubKey.Address().String()] = privVal + } + + // construct validator set; + // Note that the validators are sorted by voting power + // or, if equal, by address lexical order + valSet := cmttypes.NewValidatorSet(validators) + + return NewTestChainWithValSet2(t, coord, appFactory, chainID, valSet, signersByAddress, opts...) +} + +// NewTestChainWithValSet initializes a new TestChain instance with the given validator set +// and signer array. It also initializes 10 Sender accounts with a balance of 10000000000000000000 coins of +// bond denom to use for tests. +// +// The first block height is committed to state in order to allow for client creations on +// counterparty chains. The TestChain will return with a block height starting at 2. +// +// Time management is handled by the Coordinator in order to ensure synchrony between chains. +// Each update of any chain increments the block header time for all chains by 5 seconds. +// +// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this +// constructor function. +// +// CONTRACT: Validator array must be provided in the order expected by Tendermint. +// i.e. sorted first by power and then lexicographically by address. +func NewTestChainWithValSet2(t *testing.T, coord *ibctesting.Coordinator, appFactory ChainAppFactory2, chainID string, valSet *cmttypes.ValidatorSet, signers map[string]cmttypes.PrivValidator, opts ...wasmkeeper.Option) *ibctesting.TestChain { + genAccs := []authtypes.GenesisAccount{} + genBals := []banktypes.Balance{} + senderAccs := []ibctesting.SenderAccount{} + + // generate genesis accounts + for i := 0; i < MaxAccounts; i++ { + senderPrivKey := secp256k1.GenPrivKey() + acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) + amount, ok := math.NewIntFromString("10000000000000000000") + require.True(t, ok) + + // add sender account + balance := banktypes.Balance{ + Address: acc.GetAddress().String(), + Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), + } + + genAccs = append(genAccs, acc) + genBals = append(genBals, balance) + + senderAcc := ibctesting.SenderAccount{ + SenderAccount: acc, + SenderPrivKey: senderPrivKey, + } + + senderAccs = append(senderAccs, senderAcc) + } + + wasmApp := appFactory(t, valSet, genAccs, chainID, opts, genBals...) + + // create current header and call begin block + header := cmtproto.Header{ + ChainID: chainID, + Height: 1, + Time: coord.CurrentTime.UTC(), + } + + txConfig := wasmApp.GetTxConfig() + + // create an account to send transactions from + chain := &ibctesting.TestChain{ + TB: t, + Coordinator: coord, + ChainID: chainID, + App: wasmApp, + ProposedHeader: header, + TxConfig: txConfig, + Codec: wasmApp.AppCodec(), + Vals: valSet, + NextVals: valSet, + Signers: signers, + SenderPrivKey: senderAccs[0].SenderPrivKey, + SenderAccount: senderAccs[0].SenderAccount, + SenderAccounts: senderAccs, + } + + coord.CommitBlock(chain) + + return chain +} + +// NewCoordinator initializes Coordinator with N TestChain's +func NewCoordinator2(t *testing.T, n int) *ibctesting.Coordinator { + t.Helper() + chains := make(map[string]*ibctesting.TestChain) + coord := &ibctesting.Coordinator{ + T: t, + CurrentTime: globalStartTime, + } + + for i := 1; i <= n; i++ { + chainID := ibctesting.GetChainID(i) + chains[chainID] = NewDefaultTestChain2(t, coord, chainID) + } + coord.Chains = chains + + return coord +}