Skip to content

Commit

Permalink
fix: initial deposit for v1 props (#444)
Browse files Browse the repository at this point in the history
Co-authored-by: StrathCole <[email protected]>
  • Loading branch information
fragwuerdig and StrathCole authored Mar 7, 2024
1 parent e979ff9 commit ec9105c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
21 changes: 15 additions & 6 deletions custom/auth/ante/min_initial_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
48 changes: 47 additions & 1 deletion custom/auth/ante/min_initial_deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}

0 comments on commit ec9105c

Please sign in to comment.