From 4598d28b26f9cba61bfeb49e241d6b1ef46ddd1d Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 6 Jan 2023 14:23:45 +0100 Subject: [PATCH] Add group test --- tests/e2e/gov_test.go | 8 ++--- tests/e2e/grants_test.go | 4 +-- tests/e2e/group_test.go | 71 +++++++++++++++++++++++++++++++++++++ tests/e2e/reflect_helper.go | 47 ++++++++++++++++++------ 4 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 tests/e2e/group_test.go diff --git a/tests/e2e/gov_test.go b/tests/e2e/gov_test.go index 7f27032348..75119447da 100644 --- a/tests/e2e/gov_test.go +++ b/tests/e2e/gov_test.go @@ -1,7 +1,6 @@ package e2e_test import ( - "github.com/CosmWasm/wasmd/tests/e2e" "testing" "time" @@ -13,20 +12,19 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/CosmWasm/wasmd/tests/e2e" "github.com/CosmWasm/wasmd/x/wasm/ibctesting" ) func TestGovVoteByContract(t *testing.T) { // Given a contract with delegation // And a gov proposal - // When the contract sends a vote the proposal + // When the contract sends a vote for the proposal // Then the vote is taken into account coord := ibctesting.NewCoordinator(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) - codeID := chain.StoreCodeFile("../../x/wasm/keeper/testdata/reflect_1_1.wasm").CodeID - contractAddr := chain.InstantiateContract(codeID, []byte(`{}`)) - require.NotEmpty(t, contractAddr) + contractAddr := e2e.InstantiateReflectContract(t, chain) chain.Fund(contractAddr, sdk.NewIntFromUint64(1_000_000_000)) // a contract with a high delegation amount delegateMsg := wasmvmtypes.CosmosMsg{ diff --git a/tests/e2e/grants_test.go b/tests/e2e/grants_test.go index b54f89234d..775cd20692 100644 --- a/tests/e2e/grants_test.go +++ b/tests/e2e/grants_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/CosmWasm/wasmd/tests/e2e" "github.com/CosmWasm/wasmd/x/wasm/ibctesting" "github.com/CosmWasm/wasmd/x/wasm/types" ) @@ -28,8 +29,7 @@ func TestGrants(t *testing.T) { coord := ibctesting.NewCoordinator(t, 1) chain := coord.GetChain(ibctesting.GetChainID(1)) - codeID := chain.StoreCodeFile("../../x/wasm/keeper/testdata/reflect_1_1.wasm").CodeID - contractAddr := chain.InstantiateContract(codeID, []byte(`{}`)) + contractAddr := e2e.InstantiateReflectContract(t, chain) require.NotEmpty(t, contractAddr) granterAddr := chain.SenderAccount.GetAddress() diff --git a/tests/e2e/group_test.go b/tests/e2e/group_test.go new file mode 100644 index 0000000000..6b7942b3b8 --- /dev/null +++ b/tests/e2e/group_test.go @@ -0,0 +1,71 @@ +package e2e_test + +import ( + "testing" + "time" + + "github.com/CosmWasm/wasmd/x/wasm/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/cosmos-sdk/x/group" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/rand" + + "github.com/CosmWasm/wasmd/tests/e2e" + "github.com/CosmWasm/wasmd/x/wasm/ibctesting" +) + +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) + chain := coord.GetChain(ibctesting.GetChainID(1)) + contractAddr := e2e.InstantiateReflectContract(t, chain) + chain.Fund(contractAddr, sdk.NewIntFromUint64(1_000_000_000)) + + members := []group.MemberRequest{ + { + Address: contractAddr.String(), + Weight: "1", + Metadata: "my contract", + }, + } + msg, err := group.NewMsgCreateGroupWithPolicy( + chain.SenderAccount.GetAddress().String(), + members, + "my group", + "my metadata", + false, + group.NewPercentageDecisionPolicy("1", time.Second, 0), + ) + require.NoError(t, err) + rsp, err := chain.SendMsgs(msg) + require.NoError(t, err) + + createRsp := rsp.MsgResponses[0].GetCachedValue().(*group.MsgCreateGroupWithPolicyResponse) + groupID, policyAddr := createRsp.GroupId, sdk.MustAccAddressFromBech32(createRsp.GroupPolicyAddress) + require.NotEmpty(t, groupID) + chain.Fund(policyAddr, sdk.NewIntFromUint64(1_000_000_000)) + // and a proposal submitted + recipientAddr := sdk.AccAddress(rand.Bytes(address.Len)) + + payload := []sdk.Msg{banktypes.NewMsgSend(policyAddr, recipientAddr, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())))} + propMsg, err := group.NewMsgSubmitProposal(policyAddr.String(), []string{contractAddr.String()}, payload, "my proposal", group.Exec_EXEC_TRY) + require.NoError(t, err) + + rsp = e2e.MustExecViaStargateReflectContract(t, chain, contractAddr, propMsg) + bz := rsp.MsgResponses[0].GetCachedValue().(*types.MsgExecuteContractResponse).Data + var groupRsp group.MsgSubmitProposalResponse + require.NoError(t, chain.Codec.Unmarshal(bz, &groupRsp)) + // require.NotEmpty(t, groupRsp.ProposalId) + + // and coins received + recipientBalance := chain.Balance(recipientAddr, sdk.DefaultBondDenom) + expBalanceAmount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt()) + assert.Equal(t, expBalanceAmount.String(), recipientBalance.String()) +} diff --git a/tests/e2e/reflect_helper.go b/tests/e2e/reflect_helper.go index 54803dd8be..c2ebcadbf2 100644 --- a/tests/e2e/reflect_helper.go +++ b/tests/e2e/reflect_helper.go @@ -2,17 +2,20 @@ package e2e import ( "encoding/json" + "testing" + + wasmvmtypes "github.com/CosmWasm/wasmvm/types" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "github.com/CosmWasm/wasmd/x/wasm/ibctesting" "github.com/CosmWasm/wasmd/x/wasm/keeper/testdata" - types3 "github.com/CosmWasm/wasmd/x/wasm/types" - types2 "github.com/CosmWasm/wasmvm/types" - "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" - "testing" + "github.com/CosmWasm/wasmd/x/wasm/types" ) // InstantiateReflectContract store and instantiate a reflect contract instance -func InstantiateReflectContract(t *testing.T, chain *ibctesting.TestChain) types.AccAddress { +func InstantiateReflectContract(t *testing.T, chain *ibctesting.TestChain) sdk.AccAddress { codeID := chain.StoreCodeFile("../../x/wasm/keeper/testdata/reflect_1_1.wasm").CodeID contractAddr := chain.InstantiateContract(codeID, []byte(`{}`)) require.NotEmpty(t, contractAddr) @@ -20,19 +23,43 @@ func InstantiateReflectContract(t *testing.T, chain *ibctesting.TestChain) types } // MustExecViaReflectContract submit execute message to send payload to reflect contract -func MustExecViaReflectContract(t *testing.T, chain *ibctesting.TestChain, contractAddr types.AccAddress, msgs ...types2.CosmosMsg) { - _, err := ExecViaReflectContract(t, chain, contractAddr, msgs) +func MustExecViaReflectContract(t *testing.T, chain *ibctesting.TestChain, contractAddr sdk.AccAddress, msgs ...wasmvmtypes.CosmosMsg) *sdk.Result { + rsp, err := ExecViaReflectContract(t, chain, contractAddr, msgs) + require.NoError(t, err) + return rsp +} + +type sdkMessageType interface { + codec.ProtoMarshaler + sdk.Msg +} + +func MustExecViaStargateReflectContract[T sdkMessageType](t *testing.T, chain *ibctesting.TestChain, contractAddr sdk.AccAddress, msgs ...T) *sdk.Result { + vmMsgs := make([]wasmvmtypes.CosmosMsg, len(msgs)) + for i, m := range msgs { + bz, err := chain.Codec.Marshal(m) + require.NoError(t, err) + vmMsgs[i] = wasmvmtypes.CosmosMsg{ + Stargate: &wasmvmtypes.StargateMsg{ + TypeURL: sdk.MsgTypeURL(m), + Value: bz, + }, + } + } + rsp, err := ExecViaReflectContract(t, chain, contractAddr, vmMsgs) require.NoError(t, err) + return rsp } // ExecViaReflectContract submit execute message to send payload to reflect contract -func ExecViaReflectContract(t *testing.T, chain *ibctesting.TestChain, contractAddr types.AccAddress, msgs []types2.CosmosMsg) (*types.Result, error) { +func ExecViaReflectContract(t *testing.T, chain *ibctesting.TestChain, contractAddr sdk.AccAddress, msgs []wasmvmtypes.CosmosMsg) (*sdk.Result, error) { + require.NotEmpty(t, msgs) reflectSend := testdata.ReflectHandleMsg{ Reflect: &testdata.ReflectPayload{Msgs: msgs}, } reflectSendBz, err := json.Marshal(reflectSend) require.NoError(t, err) - execMsg := &types3.MsgExecuteContract{ + execMsg := &types.MsgExecuteContract{ Sender: chain.SenderAccount.GetAddress().String(), Contract: contractAddr.String(), Msg: reflectSendBz,