Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/gov): add autocli options for tx #18036

Merged
merged 11 commits into from
Oct 12, 2023
Prev Previous commit
Next Next commit
updates
  • Loading branch information
julienrbrt committed Oct 10, 2023
commit 8190c8c61db9996169d967fc65e112b8fb7a3d77
9 changes: 5 additions & 4 deletions tests/e2e/gov/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cosmossdk.io/math"

"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"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
Expand Down Expand Up @@ -73,19 +74,19 @@ func (s *DepositTestSuite) TearDownSuite() {

func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx

// submit proposal without initial deposit
id := s.submitProposal(val, sdk.NewCoin(s.cfg.BondDenom, math.NewInt(0)), "TestQueryDepositsWithoutInitialDeposit")
proposalID := strconv.FormatUint(id, 10)

// deposit amount
depositAmount := sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens.Add(math.NewInt(50))).String()
_, err := govclitestutil.MsgDeposit(clientCtx, val.Address.String(), proposalID, depositAmount)
depositAmount := sdk.NewCoin(s.cfg.BondDenom, v1.DefaultMinDepositTokens.Add(math.NewInt(50)))
msg := v1.NewDeposit(id, val.Address, sdk.NewCoins(depositAmount))
_, err := clitestutil.SubmitTestTx(val.ClientCtx, &msg, val.Address, clitestutil.TestTxConfig{})
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())

// query deposit
proposalID := strconv.FormatUint(id, 10)
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(depositAmount, sdk.Coins(deposit.Deposit.Amount).String())
Expand Down
190 changes: 0 additions & 190 deletions tests/e2e/gov/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govclitestutil "github.com/cosmos/cosmos-sdk/x/gov/client/testutil"
"github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -277,195 +276,6 @@ func (s *E2ETestSuite) TestNewCmdSubmitLegacyProposal() {
}
}

func (s *E2ETestSuite) TestNewCmdCancelProposal() {
val := s.network.Validators[0]
val2 := sdk.AccAddress("invalid_acc_addr")

testCases := []struct {
name string
args []string
expectErr bool
expectedCode uint32
}{
{
"without proposal id",
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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,
},
{
"invalid proposal id",
[]string{
"asdasd",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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 proposal-id but invalid proposer",
[]string{
"4",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val2),
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 proposer",
[]string{
"4",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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,
},
{
"proposal not exists after cancel",
[]string{
"4",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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, 1,
},
}

for _, tc := range testCases {
var resp sdk.TxResponse

s.Run(tc.name, func() {
cmd := cli.NewCmdCancelProposal()
clientCtx := val.ClientCtx
var balRes banktypes.QueryAllBalancesResponse
var newBalance banktypes.QueryAllBalancesResponse
if !tc.expectErr && tc.expectedCode == 0 {
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val.APIAddress, val.Address.String()))
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
}

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(), &resp), out.String())
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, tc.expectedCode))

if !tc.expectErr && tc.expectedCode == 0 {
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val.APIAddress, val.Address.String()))
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &newBalance)
s.Require().NoError(err)
remainingAmount := v1.DefaultMinDepositTokens.Mul(
v1.DefaultProposalCancelRatio.Mul(math.LegacyMustNewDecFromStr("100")).TruncateInt(),
).Quo(math.NewIntFromUint64(100))

// new balance = old balance + remaining amount from proposal deposit - txFee (cancel proposal)
txFee := math.NewInt(10)
s.Require().True(
newBalance.Balances.AmountOf(s.network.Config.BondDenom).Equal(
balRes.Balances.AmountOf(s.network.Config.BondDenom).Add(remainingAmount).Sub(txFee),
),
)
}
}
})
}
}

func (s *E2ETestSuite) TestNewCmdDeposit() {
val := s.network.Validators[0]

testCases := []struct {
name string
args []string
expectErr bool
expectedCode uint32
}{
{
"without proposal id",
[]string{
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)).String(), // 10stake
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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,
},
{
"without deposit amount",
[]string{
"1",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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,
},
{
"deposit on non existing proposal",
[]string{
"10",
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)).String(), // 10stake
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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, 1,
},
{
"deposit on existing proposal",
[]string{
"1",
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10)).String(), // 10stake
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.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
var resp sdk.TxResponse

s.Run(tc.name, func() {
cmd := cli.NewCmdDeposit()
clientCtx := val.ClientCtx

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(), &resp), out.String())
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, tc.expectedCode))
}
})
}
}

func (s *E2ETestSuite) TestNewCmdVote() {
val := s.network.Validators[0]

Expand Down
4 changes: 4 additions & 0 deletions x/gov/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "Deposit tokens for an active proposal",
Long: fmt.Sprintf(`Submit a deposit for an active proposal. You can find the proposal-id by running "%s query gov proposals"`, version.AppName),
Example: fmt.Sprintf(`$ %s tx gov deposit 1 10stake --from mykey`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "proposal_id"},
{ProtoField: "amount", Varargs: true},
},
},
{
RpcMethod: "MsgCancelProposal",
Expand Down
10 changes: 5 additions & 5 deletions x/gov/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

var longAddressError = "address max length is 255"

func (suite *KeeperTestSuite) TestSubmitProposalReq() {
func (suite *KeeperTestSuite) TestMsgSubmitProposal() {
suite.reset()
govAcct := suite.govKeeper.GetGovernanceAccount(suite.ctx).GetAddress()
addrs := suite.addrs
Expand Down Expand Up @@ -253,7 +253,7 @@ func (suite *KeeperTestSuite) TestSubmitProposalReq() {
}
}

func (suite *KeeperTestSuite) TestCancelProposalReq() {
func (suite *KeeperTestSuite) TestMsgCancelProposal() {
govAcct := suite.govKeeper.GetGovernanceAccount(suite.ctx).GetAddress()
addrs := suite.addrs
proposer := addrs[0]
Expand Down Expand Up @@ -348,7 +348,7 @@ func (suite *KeeperTestSuite) TestCancelProposalReq() {
}
}

func (suite *KeeperTestSuite) TestVoteReq() {
func (suite *KeeperTestSuite) TestMsgVote() {
suite.reset()
govAcct := suite.govKeeper.GetGovernanceAccount(suite.ctx).GetAddress()
addrs := suite.addrs
Expand Down Expand Up @@ -491,7 +491,7 @@ func (suite *KeeperTestSuite) TestVoteReq() {
}
}

func (suite *KeeperTestSuite) TestVoteWeightedReq() {
func (suite *KeeperTestSuite) TestMsgVoteWeighted() {
suite.reset()
govAcct := suite.govKeeper.GetGovernanceAccount(suite.ctx).GetAddress()

Expand Down Expand Up @@ -735,7 +735,7 @@ func (suite *KeeperTestSuite) TestVoteWeightedReq() {
}
}

func (suite *KeeperTestSuite) TestDepositReq() {
func (suite *KeeperTestSuite) TestMsgDeposit() {
govAcct := suite.govKeeper.GetGovernanceAccount(suite.ctx).GetAddress()
addrs := suite.addrs
proposer := addrs[0]
Expand Down
Loading