Skip to content

Commit

Permalink
Add group test
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 6, 2023
1 parent b74d5c1 commit 4598d28
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 17 deletions.
8 changes: 3 additions & 5 deletions tests/e2e/gov_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package e2e_test

import (
"github.com/CosmWasm/wasmd/tests/e2e"
"testing"
"time"

Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand Down
71 changes: 71 additions & 0 deletions tests/e2e/group_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
47 changes: 37 additions & 10 deletions tests/e2e/reflect_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,64 @@ 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)
return contractAddr
}

// 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,
Expand Down

0 comments on commit 4598d28

Please sign in to comment.