From ec9105c0e79daa11c7a154f59d3dd3e64398a46c Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Thu, 7 Mar 2024 09:58:14 +0100 Subject: [PATCH] fix: initial deposit for v1 props (#444) Co-authored-by: StrathCole <7449529+StrathCole@users.noreply.github.com> --- custom/auth/ante/min_initial_deposit.go | 21 ++++++--- custom/auth/ante/min_initial_deposit_test.go | 48 +++++++++++++++++++- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/custom/auth/ante/min_initial_deposit.go b/custom/auth/ante/min_initial_deposit.go index ef1771314..0804c68e4 100644 --- a/custom/auth/ante/min_initial_deposit.go +++ b/custom/auth/ante/min_initial_deposit.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" + govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" ) @@ -26,24 +27,32 @@ func NewMinInitialDepositDecorator(govKeeper govkeeper.Keeper, treasuryKeeper Tr // IsMsgSubmitProposal checks whether the input msg is a MsgSubmitProposal func IsMsgSubmitProposal(msg sdk.Msg) bool { - _, ok := msg.(*govv1beta1.MsgSubmitProposal) - return ok + switch msg.(type) { + case *govv1beta1.MsgSubmitProposal, *govv1.MsgSubmitProposal: + return true + default: + return false + } } // HandleCheckMinInitialDeposit func HandleCheckMinInitialDeposit(ctx sdk.Context, msg sdk.Msg, govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) (err error) { - submitPropMsg, ok := msg.(*govv1beta1.MsgSubmitProposal) - if !ok { + var initialDepositCoins sdk.Coins + + switch submitPropMsg := msg.(type) { + case *govv1beta1.MsgSubmitProposal: + initialDepositCoins = submitPropMsg.GetInitialDeposit() + case *govv1.MsgSubmitProposal: + initialDepositCoins = submitPropMsg.GetInitialDeposit() + default: return fmt.Errorf("could not dereference msg as MsgSubmitProposal") } - minDeposit := govKeeper.GetDepositParams(ctx).MinDeposit requiredAmount := sdk.NewDecFromInt(minDeposit[0].Amount).Mul(treasuryKeeper.GetMinInitialDepositRatio(ctx)).TruncateInt() requiredDepositCoins := sdk.NewCoins( sdk.NewCoin(core.MicroLunaDenom, requiredAmount), ) - initialDepositCoins := submitPropMsg.GetInitialDeposit() if !initialDepositCoins.IsAllGTE(requiredDepositCoins) { return fmt.Errorf("not enough initial deposit provided. Expected %q; got %q", requiredDepositCoins, initialDepositCoins) diff --git a/custom/auth/ante/min_initial_deposit_test.go b/custom/auth/ante/min_initial_deposit_test.go index 6129934cc..5c67bc1d1 100644 --- a/custom/auth/ante/min_initial_deposit_test.go +++ b/custom/auth/ante/min_initial_deposit_test.go @@ -11,6 +11,7 @@ import ( "github.com/classic-terra/core/v2/custom/auth/ante" core "github.com/classic-terra/core/v2/types" + // core "github.com/terra-money/core/types" // treasury "github.com/terra-money/core/x/treasury/types" @@ -60,6 +61,21 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioDefault() { // antehandler should not error _, err = antehandler(suite.ctx, tx, false) suite.Require().NoError(err, "error: Proposal whithout initial deposit should have gone through") + + // create v1 proposal + msgv1, _ := govv1.NewMsgSubmitProposal([]sdk.Msg{}, depositCoins1, addr1.String(), "metadata") + feeAmountv1 := testdata.NewTestFeeAmount() + gasLimitv1 := testdata.NewTestGasLimit() + suite.Require().NoError(suite.txBuilder.SetMsgs(msgv1)) + suite.txBuilder.SetFeeAmount(feeAmountv1) + suite.txBuilder.SetGasLimit(gasLimitv1) + privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) + suite.Require().NoError(err) + + // ante handler should not error for v1 proposal with sufficient deposit + _, err = antehandler(suite.ctx, txv1, false) + suite.Require().NoError(err, "error: v1 proposal whithout initial deposit should have gone through") } func (suite *AnteTestSuite) TestMinInitialDepositRatioWithSufficientDeposit() { @@ -102,6 +118,21 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithSufficientDeposit() { // antehandler should not error _, err = antehandler(suite.ctx, tx, false) suite.Require().NoError(err, "error: Proposal with sufficient initial deposit should have gone through") + + // create v1 proposal + msgv1, _ := govv1.NewMsgSubmitProposal([]sdk.Msg{}, depositCoins1, addr1.String(), "metadata") + feeAmountv1 := testdata.NewTestFeeAmount() + gasLimitv1 := testdata.NewTestGasLimit() + suite.Require().NoError(suite.txBuilder.SetMsgs(msgv1)) + suite.txBuilder.SetFeeAmount(feeAmountv1) + suite.txBuilder.SetGasLimit(gasLimitv1) + privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) + suite.Require().NoError(err) + + // ante handler should not error for v1 proposal with sufficient deposit + _, err = antehandler(suite.ctx, txv1, false) + suite.Require().NoError(err, "error: v1 proposal with sufficient initial deposit should have gone through") } func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() { @@ -141,7 +172,22 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - // antehandler should not error + // antehandler should error with insufficient deposit _, err = antehandler(suite.ctx, tx, false) suite.Require().Error(err, "error: Proposal with insufficient initial deposit should have failed") + + // create v1 proposal + msgv1, _ := govv1.NewMsgSubmitProposal([]sdk.Msg{}, depositCoins1, addr1.String(), "metadata") + feeAmountv1 := testdata.NewTestFeeAmount() + gasLimitv1 := testdata.NewTestGasLimit() + suite.Require().NoError(suite.txBuilder.SetMsgs(msgv1)) + suite.txBuilder.SetFeeAmount(feeAmountv1) + suite.txBuilder.SetGasLimit(gasLimitv1) + privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} + txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) + suite.Require().NoError(err) + + // ante handler should error for v1 proposal with insufficient deposit + _, err = antehandler(suite.ctx, txv1, false) + suite.Require().Error(err, "error: v1 proposal with insufficient initial deposit should have failed") }