From 2705c40dd0ffcd9f87338efe22dcafef9058889f Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Wed, 6 Mar 2024 19:10:46 +0100 Subject: [PATCH 1/5] test: min initial deposit for v1 props --- custom/auth/ante/min_initial_deposit_test.go | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/custom/auth/ante/min_initial_deposit_test.go b/custom/auth/ante/min_initial_deposit_test.go index 6129934cc..dfd743afd 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 + _, 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,22 @@ 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 + _, 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() { @@ -144,4 +176,20 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() // antehandler should not error _, 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 not error for v1 proposal + _, err = antehandler(suite.ctx, txv1, false) + suite.Require().Error(err, "error: v1 proposal with insufficient initial deposit should have failed") + } From 49a2a1c7ecb8e8cbc9f237e930b53b4360751921 Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Wed, 6 Mar 2024 19:18:07 +0100 Subject: [PATCH 2/5] fix: v1 not accounted for in deposit check --- custom/auth/ante/min_initial_deposit.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/custom/auth/ante/min_initial_deposit.go b/custom/auth/ante/min_initial_deposit.go index ef1771314..8ac5cbe19 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,34 @@ 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 + _, okv1beta1 := msg.(*govv1beta1.MsgSubmitProposal) + _, okv1 := msg.(*govv1.MsgSubmitProposal) + return okv1beta1 || okv1 } // HandleCheckMinInitialDeposit func HandleCheckMinInitialDeposit(ctx sdk.Context, msg sdk.Msg, govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) (err error) { - submitPropMsg, ok := msg.(*govv1beta1.MsgSubmitProposal) - if !ok { + submitPropMsgV1Beta1, okV1Beta1 := msg.(*govv1beta1.MsgSubmitProposal) + submitPropMsgV1, okV1 := msg.(*govv1.MsgSubmitProposal) + + if !(okV1Beta1 || okV1) { return fmt.Errorf("could not dereference msg as MsgSubmitProposal") } + var initialDepositCoins sdk.Coins + + if okV1Beta1 { + initialDepositCoins = submitPropMsgV1Beta1.GetInitialDeposit() + } else if okV1 { + initialDepositCoins = submitPropMsgV1.GetInitialDeposit() + } + 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) From ca3fdefc43a22567a58af7cffbf776f6ba496978 Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Wed, 6 Mar 2024 19:25:21 +0100 Subject: [PATCH 3/5] lint: min_initial_deposit_test.go --- custom/auth/ante/min_initial_deposit_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom/auth/ante/min_initial_deposit_test.go b/custom/auth/ante/min_initial_deposit_test.go index dfd743afd..d5cfb927f 100644 --- a/custom/auth/ante/min_initial_deposit_test.go +++ b/custom/auth/ante/min_initial_deposit_test.go @@ -133,7 +133,6 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithSufficientDeposit() { // ante handler should not error for v1 proposal _, 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() { @@ -191,5 +190,4 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() // ante handler should not error for v1 proposal _, err = antehandler(suite.ctx, txv1, false) suite.Require().Error(err, "error: v1 proposal with insufficient initial deposit should have failed") - } From aa87018050cc933c427a180fce821f8ec466e51e Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Thu, 7 Mar 2024 09:04:42 +0100 Subject: [PATCH 4/5] docs: correct comments --- custom/auth/ante/min_initial_deposit_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/custom/auth/ante/min_initial_deposit_test.go b/custom/auth/ante/min_initial_deposit_test.go index d5cfb927f..5c67bc1d1 100644 --- a/custom/auth/ante/min_initial_deposit_test.go +++ b/custom/auth/ante/min_initial_deposit_test.go @@ -73,7 +73,7 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioDefault() { txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - // ante handler should not error for v1 proposal + // 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") } @@ -130,7 +130,7 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithSufficientDeposit() { txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - // ante handler should not error for v1 proposal + // 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") } @@ -172,7 +172,7 @@ 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") @@ -187,7 +187,7 @@ func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() txv1, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) suite.Require().NoError(err) - // ante handler should not error for v1 proposal + // 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") } From de22d6a82da61f1442757736b2c587ae92d7a492 Mon Sep 17 00:00:00 2001 From: Till Ziegler Date: Thu, 7 Mar 2024 09:34:51 +0100 Subject: [PATCH 5/5] fix: apply suggestions from strath Co-authored-by: StrathCole <7449529+StrathCole@users.noreply.github.com> --- custom/auth/ante/min_initial_deposit.go | 28 ++++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/custom/auth/ante/min_initial_deposit.go b/custom/auth/ante/min_initial_deposit.go index 8ac5cbe19..0804c68e4 100644 --- a/custom/auth/ante/min_initial_deposit.go +++ b/custom/auth/ante/min_initial_deposit.go @@ -27,28 +27,26 @@ func NewMinInitialDepositDecorator(govKeeper govkeeper.Keeper, treasuryKeeper Tr // IsMsgSubmitProposal checks whether the input msg is a MsgSubmitProposal func IsMsgSubmitProposal(msg sdk.Msg) bool { - _, okv1beta1 := msg.(*govv1beta1.MsgSubmitProposal) - _, okv1 := msg.(*govv1.MsgSubmitProposal) - return okv1beta1 || okv1 + 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) { - submitPropMsgV1Beta1, okV1Beta1 := msg.(*govv1beta1.MsgSubmitProposal) - submitPropMsgV1, okV1 := msg.(*govv1.MsgSubmitProposal) - - if !(okV1Beta1 || okV1) { - return fmt.Errorf("could not dereference msg as MsgSubmitProposal") - } - var initialDepositCoins sdk.Coins - if okV1Beta1 { - initialDepositCoins = submitPropMsgV1Beta1.GetInitialDeposit() - } else if okV1 { - initialDepositCoins = submitPropMsgV1.GetInitialDeposit() + 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()