378 lines
13 KiB
Go
378 lines
13 KiB
Go
package gov
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
|
|
"github.com/cosmos/gogoproto/proto"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"cosmossdk.io/math"
|
|
authtypes "cosmossdk.io/x/auth/types"
|
|
"cosmossdk.io/x/gov/client/cli"
|
|
govclitestutil "cosmossdk.io/x/gov/client/testutil"
|
|
"cosmossdk.io/x/gov/types"
|
|
v1 "cosmossdk.io/x/gov/types/v1"
|
|
"cosmossdk.io/x/gov/types/v1beta1"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
type E2ETestSuite struct {
|
|
suite.Suite
|
|
|
|
cfg network.Config
|
|
network network.NetworkI
|
|
}
|
|
|
|
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
|
|
return &E2ETestSuite{cfg: cfg}
|
|
}
|
|
|
|
func (s *E2ETestSuite) SetupSuite() {
|
|
s.T().Log("setting up e2e test suite")
|
|
|
|
var err error
|
|
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(s.network.WaitForNextBlock())
|
|
|
|
val := s.network.GetValidators()[0]
|
|
clientCtx := val.GetClientCtx()
|
|
var resp sdk.TxResponse
|
|
|
|
// create a proposal with deposit
|
|
out, err := govclitestutil.MsgSubmitLegacyProposal(val.GetClientCtx(), val.GetAddress().String(),
|
|
"Text Proposal 1", "Where is the title!?", v1beta1.ProposalTypeText,
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens).String()))
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
|
|
// vote for proposal
|
|
out, err = govclitestutil.MsgVote(val.GetClientCtx(), val.GetAddress().String(), "1", "yes")
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
|
|
// create a proposal with a small deposit
|
|
minimumAcceptedDep := v1.DefaultMinDepositTokens.ToLegacyDec().Mul(v1.DefaultMinDepositRatio).Ceil().TruncateInt()
|
|
out, err = govclitestutil.MsgSubmitLegacyProposal(val.GetClientCtx(), val.GetAddress().String(),
|
|
"Text Proposal 2", "Where is the title!?", v1beta1.ProposalTypeText,
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, minimumAcceptedDep).String()))
|
|
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
|
|
// create a proposal3 with deposit
|
|
out, err = govclitestutil.MsgSubmitLegacyProposal(val.GetClientCtx(), val.GetAddress().String(),
|
|
"Text Proposal 3", "Where is the title!?", v1beta1.ProposalTypeText,
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens).String()))
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
|
|
// create a proposal4 with deposit to check the cancel proposal cli tx
|
|
out, err = govclitestutil.MsgSubmitLegacyProposal(val.GetClientCtx(), val.GetAddress().String(),
|
|
"Text Proposal 4", "Where is the title!?", v1beta1.ProposalTypeText,
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens).String()))
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
|
|
// vote for proposal3 as val
|
|
out, err = govclitestutil.MsgVote(val.GetClientCtx(), val.GetAddress().String(), "3", "yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05")
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, 0))
|
|
}
|
|
|
|
func (s *E2ETestSuite) TearDownSuite() {
|
|
s.T().Log("tearing down e2e test suite")
|
|
s.network.Cleanup()
|
|
}
|
|
|
|
func (s *E2ETestSuite) TestNewCmdSubmitProposal() {
|
|
val := s.network.GetValidators()[0]
|
|
|
|
// Create a legacy proposal JSON, make sure it doesn't pass this new CLI
|
|
// command.
|
|
invalidProp := `{
|
|
"title": "",
|
|
"description": "Where is the title!?",
|
|
"type": "Text",
|
|
"deposit": "-324foocoin"
|
|
}`
|
|
invalidPropFile := testutil.WriteToNewTempFile(s.T(), invalidProp)
|
|
defer invalidPropFile.Close()
|
|
|
|
// Create a valid new proposal JSON.
|
|
propMetadata := []byte{42}
|
|
validProp := fmt.Sprintf(`
|
|
{
|
|
"messages": [
|
|
{
|
|
"@type": "/cosmos.gov.v1.MsgExecLegacyContent",
|
|
"authority": "%s",
|
|
"content": {
|
|
"@type": "/cosmos.gov.v1beta1.TextProposal",
|
|
"title": "My awesome title",
|
|
"description": "My awesome description"
|
|
}
|
|
}
|
|
],
|
|
"title": "My awesome title",
|
|
"summary": "My awesome description",
|
|
"metadata": "%s",
|
|
"deposit": "%s"
|
|
}`, authtypes.NewModuleAddress(types.ModuleName), base64.StdEncoding.EncodeToString(propMetadata), sdk.NewCoin(s.cfg.BondDenom, math.NewInt(100000)))
|
|
validPropFile := testutil.WriteToNewTempFile(s.T(), validProp)
|
|
defer validPropFile.Close()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedCode uint32
|
|
respType proto.Message
|
|
}{
|
|
{
|
|
"invalid proposal",
|
|
[]string{
|
|
invalidPropFile.Name(),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
true, 0, nil,
|
|
},
|
|
{
|
|
"valid proposal",
|
|
[]string{
|
|
validPropFile.Name(),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0, &sdk.TxResponse{},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.NewCmdSubmitProposal()
|
|
clientCtx := val.GetClientCtx()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
|
|
txResp := tc.respType.(*sdk.TxResponse)
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, txResp.TxHash, tc.expectedCode))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *E2ETestSuite) TestNewCmdSubmitLegacyProposal() {
|
|
val := s.network.GetValidators()[0]
|
|
invalidProp := `{
|
|
"title": "",
|
|
"description": "Where is the title!?",
|
|
"type": "Text",
|
|
"deposit": "-324foocoin"
|
|
}`
|
|
invalidPropFile := testutil.WriteToNewTempFile(s.T(), invalidProp)
|
|
defer invalidPropFile.Close()
|
|
validProp := fmt.Sprintf(`{
|
|
"title": "Text Proposal",
|
|
"description": "Hello, World!",
|
|
"type": "Text",
|
|
"deposit": "%s"
|
|
}`, sdk.NewCoin(s.cfg.BondDenom, math.NewInt(154310)))
|
|
validPropFile := testutil.WriteToNewTempFile(s.T(), validProp)
|
|
defer validPropFile.Close()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedCode uint32
|
|
respType proto.Message
|
|
}{
|
|
{
|
|
"invalid proposal (file)",
|
|
[]string{
|
|
fmt.Sprintf("--%s=%s", cli.FlagProposal, invalidPropFile.Name()), //nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
true, 0, nil,
|
|
},
|
|
{
|
|
"invalid proposal",
|
|
[]string{
|
|
fmt.Sprintf("--%s='Where is the title!?'", cli.FlagDescription), //nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
fmt.Sprintf("--%s=%s", cli.FlagProposalType, v1beta1.ProposalTypeText), //nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10000)).String()),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
true, 0, nil,
|
|
},
|
|
{
|
|
"valid transaction (file)",
|
|
//nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
[]string{
|
|
fmt.Sprintf("--%s=%s", cli.FlagProposal, validPropFile.Name()),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0, &sdk.TxResponse{},
|
|
},
|
|
{
|
|
"valid transaction",
|
|
[]string{
|
|
fmt.Sprintf("--%s='Text Proposal'", cli.FlagTitle),
|
|
fmt.Sprintf("--%s='Where is the title!?'", cli.FlagDescription), //nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
fmt.Sprintf("--%s=%s", cli.FlagProposalType, v1beta1.ProposalTypeText), //nolint:staticcheck // we are intentionally using a deprecated flag here.
|
|
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, math.NewInt(100000)).String()),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0, &sdk.TxResponse{},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.NewCmdSubmitLegacyProposal()
|
|
clientCtx := val.GetClientCtx()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
|
|
txResp := tc.respType.(*sdk.TxResponse)
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, txResp.TxHash, tc.expectedCode))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *E2ETestSuite) TestNewCmdWeightedVote() {
|
|
val := s.network.GetValidators()[0]
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedCode uint32
|
|
}{
|
|
{
|
|
"invalid vote",
|
|
[]string{},
|
|
true, 0,
|
|
},
|
|
{
|
|
"vote for invalid proposal",
|
|
[]string{
|
|
"10",
|
|
"yes",
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 3,
|
|
},
|
|
{
|
|
"valid vote",
|
|
[]string{
|
|
"1",
|
|
"yes",
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0,
|
|
},
|
|
{
|
|
"valid vote with metadata",
|
|
[]string{
|
|
"1",
|
|
"yes",
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--metadata=%s", "AQ=="),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0,
|
|
},
|
|
{
|
|
"invalid valid split vote string",
|
|
[]string{
|
|
"1",
|
|
"yes/0.6,no/0.3,abstain/0.05,no_with_veto/0.05",
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
true, 0,
|
|
},
|
|
{
|
|
"valid split vote",
|
|
[]string{
|
|
"1",
|
|
"yes=0.6,no=0.3,abstain=0.05,no_with_veto=0.05",
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.GetAddress().String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
|
|
},
|
|
false, 0,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.NewCmdWeightedVote()
|
|
clientCtx := val.GetClientCtx()
|
|
var txResp sdk.TxResponse
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
|
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &txResp), out.String())
|
|
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, txResp.TxHash, tc.expectedCode))
|
|
}
|
|
})
|
|
}
|
|
}
|