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..ed0f49692 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 @@ -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 @@ -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 ff29167cd..ee56e72c8 100644 --- a/tests/e2e/ibc_fees_test.go +++ b/tests/e2e/ibc_fees_test.go @@ -3,11 +3,13 @@ package e2e import ( "bytes" "crypto/sha256" + "encoding/base64" "encoding/hex" "fmt" "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" //nolint:staticcheck @@ -133,140 +135,122 @@ func TestIBCFeesTransfer(t *testing.T) { assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.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(t).Codec -// coord := ibctesting.NewCoordinator(t, 2) - -// genAccs := []authtypes.GenesisAccount{} -// genBals := []banktypes.Balance{} - -// // generate genesis accounts -// for i := 0; i < ibctesting.MaxAccounts; i++ { -// senderPrivKey := secp256k1.GenPrivKey() -// acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) -// amount, ok := sdkmath.NewIntFromString(ibctesting.DefaultGenesisAccBalance) -// 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) -// } - -// chainA := coord.GetChain(ibctesting.GetChainID(1)) -// chainB := coord.GetChain(ibctesting.GetChainID(2)) -// chainA.App = wasmibctesting.NewWasmApp(t, chainA.Vals, genAccs, chainA.ChainID, nil, genBals) -// chainB.App = wasmibctesting.NewWasmApp(t, chainB.Vals, genAccs, chainB.ChainID, nil, genBals) -// actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address()) -// actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address()) - -// // setup chain A -// codeID := chainA.App.(*wasmibctesting.Dupa).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, sdkmath.NewInt(1))) - -// // path := ibctesting.NewPath(chainA, chainB) -// // path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ -// // PortID: ibcContractPortID, -// // Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), -// // Order: channeltypes.UNORDERED, -// // } -// // path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{ -// // PortID: ibctransfertypes.PortID, -// // Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V2})), -// // Order: channeltypes.UNORDERED, -// // } -// // // with an ics-29 fee enabled channel setup between both chains -// // coord.Setup(path) -// // appA := chainA.GetSimApp() -// // appB := chainB.GetSimApp() -// // 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) -// // _, 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 := 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 := 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.GetSimApp().BankKeeper.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) -// // assert.Len(t, pendingIncentivisedPackages, 0) -// // // expBalance := ibctransfertypes.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)) - -// // // 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, []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) -// // require.NoError(t, err) -// // 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 = 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 -// // payeeBalance = chainB.GetSimApp().BankKeeper.GetAllBalances(chainB.GetContext(), payee) -// // assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.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(t).Codec + 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 := 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 := wasmibctesting.InstantiateContract(t, chainA, codeID, initMsg) + + initMsg = []byte(fmt.Sprintf(`{"default_timeout": 360, "gov_contract": %q, "allowlist":[{"contract":%q}]}`, actorChainA.String(), cw20ContractAddr.String())) + 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 := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{ + PortID: ibcContractPortID, + Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V1})), + Order: channeltypes.UNORDERED, + } + path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{ + PortID: ibctransfertypes.PortID, + Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.V1})), + Order: channeltypes.UNORDERED, + } + // with an ics-29 fee enabled channel setup between both chains + coord.Setup(path) + 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) + _, 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) + result, err := chainA.SendMsgs(feeMsg, &execMsg) + require.NoError(t, err) + pendingIncentivisedPackages := appA.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibcContractPortID, path.EndpointA.ChannelID) + assert.Len(t, pendingIncentivisedPackages, 1) + + // and packages relayed + 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.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 := 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.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 := 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())) + 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, []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) + result, err = chainB.SendMsgs(feeMsg, ibcPayloadMsg) + require.NoError(t, err) + pendingIncentivisedPackages = appB.GetIBCFeeKeeper().GetIdentifiedPacketFeesForChannel(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID) + assert.Len(t, pendingIncentivisedPackages, 1) + + // when packages relayed + 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.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 = appB.GetBankKeeper().GetAllBalances(chainB.GetContext(), payee) + assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2)).String(), payeeBalance.String()) +} func TestIBCFeesReflect(t *testing.T) { // scenario: @@ -276,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})), @@ -304,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) @@ -315,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{ @@ -345,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 @@ -369,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 := ibctransfertypes.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)) + expBalance := GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sdk.DefaultBondDenom, sdkmath.NewInt(10)) + 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 index 1c9e5aed2..3b95a5a7c 100644 --- a/tests/ibctesting/chain2.go +++ b/tests/ibctesting/chain2.go @@ -1,12 +1,14 @@ package ibctesting import ( + "bytes" + "compress/gzip" "context" + "os" + "strings" "testing" - // simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - - sdkmath "cosmossdk.io/math" + "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttypes "github.com/cometbft/cometbft/types" @@ -20,10 +22,12 @@ import ( 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" @@ -32,7 +36,6 @@ import ( ) type Dupa struct { - // t *testing.T App *app.WasmApp } @@ -133,155 +136,85 @@ func (app Dupa) GetBankKeeper() bankkeeper.Keeper { return app.App.GetBankKeeper() } -// func (chain Dupa) StoreCodeFile(filename string) types.MsgStoreCodeResponse { -// wasmCode, err := os.ReadFile(filename) -// require.NoError(chain.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(chain.t, err) -// err = gz.Close() -// require.NoError(chain.t, err) -// wasmCode = buf.Bytes() -// } -// return chain.StoreCode(wasmCode) -// } - -// func (chain Dupa) StoreCode(byteCode []byte) types.MsgStoreCodeResponse { -// storeMsg := &types.MsgStoreCode{ -// Sender: chain.SenderAccount.GetAddress().String(), -// WASMByteCode: byteCode, -// } -// r, err := chain.SendMsgs(storeMsg) -// require.NoError(chain.t, err) - -// var pInstResp types.MsgStoreCodeResponse -// chain.UnwrapExecTXResult(r, &pInstResp) - -// require.NotEmpty(chain.t, pInstResp.CodeID) -// require.NotEmpty(chain.t, pInstResp.Checksum) -// return pInstResp -// } - -// // UnwrapExecTXResult is a helper to unpack execution result from proto any type -// func (chain Dupa) UnwrapExecTXResult(r *abci.ExecTxResult, target proto.Message) { -// var wrappedRsp sdk.TxMsgData -// require.NoError(chain.t, chain.AppCodec().Unmarshal(r.Data, &wrappedRsp)) - -// // unmarshal protobuf response from data -// require.Len(chain.t, wrappedRsp.MsgResponses, 1) -// require.NoError(chain.t, proto.Unmarshal(wrappedRsp.MsgResponses[0].Value, target)) -// } - -// // sendMsgs delivers a transaction through the application without returning the result. -// func (chain Dupa) sendMsgs(msgs ...sdk.Msg) error { -// _, err := chain.SendMsgs(msgs...) -// return err -// } - -// // SendMsgs delivers a transaction through the application. It updates the senders sequence -// // number and updates the TestChain's headers. It returns the result and error if one -// // occurred. -// func (chain Dupa) SendMsgs(msgs ...sdk.Msg) (*abci.ExecTxResult, error) { -// rsp, gotErr := chain.sendWithSigner(chain.SenderPrivKey, chain.SenderAccount, msgs...) -// require.NoError(chain.t, chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence()+1)) -// return rsp, gotErr -// } - -// // // SendNonDefaultSenderMsgs is the same as SendMsgs but with a custom signer/account -// // func (chain Dupa) SendNonDefaultSenderMsgs(senderPrivKey cryptotypes.PrivKey, msgs ...sdk.Msg) (*abci.ExecTxResult, error) { -// // require.NotEqual(chain.t, chain.SenderPrivKey, senderPrivKey, "use SendMsgs method") - -// // addr := sdk.AccAddress(senderPrivKey.PubKey().Address().Bytes()) -// // account := chain.App.GetAccountKeeper().GetAccount(chain.GetContext(), addr) -// // require.NotNil(chain.t, account) -// // return chain.sendWithSigner(senderPrivKey, account, msgs...) -// // } - -// // sendWithSigner is a generic helper to send messages -// func (chain Dupa) sendWithSigner( -// senderPrivKey cryptotypes.PrivKey, -// senderAccount sdk.AccountI, -// msgs ...sdk.Msg, -// ) (*abci.ExecTxResult, error) { -// // ensure the chain has the latest time -// chain.Coordinator.UpdateTimeForChain(chain) - -// blockResp, gotErr := app.SignAndDeliverWithoutCommit( -// chain.t, -// chain.TxConfig, -// chain.App.GetBaseApp(), -// msgs, -// chain.DefaultMsgFees, -// chain.ChainID, -// []uint64{senderAccount.GetAccountNumber()}, -// []uint64{senderAccount.GetSequence()}, -// chain.CurrentHeader.GetTime(), -// senderPrivKey, -// ) -// if gotErr != nil { -// return nil, gotErr -// } - -// chain.commitBlock(blockResp) -// chain.Coordinator.IncrementTime() - -// require.Len(chain.t, blockResp.TxResults, 1) -// txResult := blockResp.TxResults[0] -// if txResult.Code != 0 { -// return txResult, fmt.Errorf("%s/%d: %q", txResult.Codespace, txResult.Code, txResult.Log) -// } - -// chain.CaptureIBCEvents(txResult) -// return txResult, nil -// } - -// func (chain Dupa) CaptureIBCEvents(r *abci.ExecTxResult) { -// toSend := GetSendPackets(r.Events) -// if len(toSend) > 0 { -// // Keep a queue on the chain that we can relay in tests -// chain.PendingSendPackets = append(chain.PendingSendPackets, toSend...) -// } -// } - -// func NewWasmApp(t *testing.T, valSet *cmttypes.ValidatorSet, chainID string, opts ...wasmkeeper.Option) ibctesting.TestingApp { -// genAccs := []authtypes.GenesisAccount{} -// genBals := []banktypes.Balance{} - -// // generate genesis accounts -// for i := 0; i < ibctesting.MaxAccounts; i++ { -// senderPrivKey := secp256k1.GenPrivKey() -// acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) -// amount, ok := sdkmath.NewIntFromString(ibctesting.DefaultGenesisAccBalance) -// 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) -// } -// return Dupa{App: app.SetupWithGenesisValSet(t, valSet, genAccs, chainID, opts, genBals...)} -// app, _ := app.Setup2(t, chainID, false, 0, opts...) -// return Dupa{App: app} -// } - -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// -/////////////// +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 @@ -347,7 +280,7 @@ func NewTestChainWithValSet2(t *testing.T, coord *ibctesting.Coordinator, appFac for i := 0; i < MaxAccounts; i++ { senderPrivKey := secp256k1.GenPrivKey() acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) - amount, ok := sdkmath.NewIntFromString("10000000000000000000") + amount, ok := math.NewIntFromString("10000000000000000000") require.True(t, ok) // add sender account