Skip to content

Commit

Permalink
chore: Attempt to replace Ibc app with WasmApp
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Jan 16, 2025
1 parent c2aa99b commit d426e6b
Show file tree
Hide file tree
Showing 8 changed files with 504 additions and 130 deletions.
16 changes: 8 additions & 8 deletions tests/e2e/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand All @@ -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())
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
47 changes: 25 additions & 22 deletions tests/e2e/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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))

Expand Down Expand Up @@ -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()))
Expand All @@ -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)
})
}
}
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
28 changes: 15 additions & 13 deletions tests/e2e/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ 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) {
// Given a group with a contract as only member
// 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{
{
Expand All @@ -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))

Expand All @@ -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())
}
Expand All @@ -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{
{
Expand All @@ -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))

Expand All @@ -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())
}
33 changes: 17 additions & 16 deletions tests/e2e/ibc_callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})),
Expand All @@ -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"`
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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})),
Expand All @@ -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)
}
Loading

0 comments on commit d426e6b

Please sign in to comment.