-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Min Initial Deposit (Recreated using main as base branch) (#138)
- Loading branch information
1 parent
0f8b747
commit 58f2c52
Showing
15 changed files
with
407 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package v3 | ||
|
||
const UpgradeName = "v3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package v3 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
func CreateV3UpgradeHandler( | ||
mm *module.Manager, | ||
cfg module.Configurator, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { | ||
// treasury store migration | ||
return mm.RunMigrations(ctx, cfg, fromVM) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
core "github.com/classic-terra/core/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
// MinInitialDeposit Decorator will check Initial Deposits for MsgSubmitProposal | ||
type MinInitialDepositDecorator struct { | ||
govKeeper govkeeper.Keeper | ||
treasuryKeeper TreasuryKeeper | ||
} | ||
|
||
// NewMinInitialDeposit returns new min initial deposit decorator instance | ||
func NewMinInitialDepositDecorator(govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) MinInitialDepositDecorator { | ||
return MinInitialDepositDecorator{ | ||
govKeeper: govKeeper, | ||
treasuryKeeper: treasuryKeeper, | ||
} | ||
} | ||
|
||
// IsMsgSubmitProposal checks whether the input msg is a MsgSubmitProposal | ||
func IsMsgSubmitProposal(msg sdk.Msg) bool { | ||
_, ok := msg.(*govtypes.MsgSubmitProposal) | ||
return ok | ||
} | ||
|
||
// HandleCheckMinInitialDeposit | ||
func HandleCheckMinInitialDeposit(ctx sdk.Context, msg sdk.Msg, govKeeper govkeeper.Keeper, treasuryKeeper TreasuryKeeper) (err error) { | ||
submitPropMsg, ok := msg.(*govtypes.MsgSubmitProposal) | ||
if !ok { | ||
return fmt.Errorf("Could not dereference msg as MsgSubmitProposal") | ||
} | ||
|
||
minDeposit := govKeeper.GetDepositParams(ctx).MinDeposit | ||
requiredAmount := sdk.NewDecFromInt(minDeposit.AmountOf(core.MicroLunaDenom)).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) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// AnteHandle handles checking MsgSubmitProposal | ||
func (midd MinInitialDepositDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
if simulate { | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
msgs := tx.GetMsgs() | ||
for _, msg := range msgs { | ||
|
||
if !IsMsgSubmitProposal(msg) { | ||
continue | ||
} | ||
|
||
err := HandleCheckMinInitialDeposit(ctx, msg, midd.govKeeper, midd.treasuryKeeper) | ||
if err != nil { | ||
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, err.Error()) | ||
} | ||
|
||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package ante_test | ||
|
||
import ( | ||
//"fmt" | ||
|
||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
// banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
|
||
"github.com/classic-terra/core/custom/auth/ante" | ||
// core "github.com/terra-money/core/types" | ||
// treasury "github.com/terra-money/core/x/treasury/types" | ||
|
||
//"github.com/cosmos/cosmos-sdk/types/query" | ||
//cosmosante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
//"github.com/cosmos/cosmos-sdk/x/auth/types" | ||
//minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
func (suite *AnteTestSuite) TestMinInitialDepositRatioDefault() { | ||
suite.SetupTest(true) // setup | ||
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() | ||
|
||
midd := ante.NewMinInitialDepositDecorator(suite.app.GovKeeper, suite.app.TreasuryKeeper) | ||
antehandler := sdk.ChainAnteDecorators(midd) | ||
|
||
// set required deposit to uluna | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govtypes.DefaultDepositParams()) | ||
govparams := suite.app.GovKeeper.GetDepositParams(suite.ctx) | ||
govparams.MinDeposit = sdk.NewCoins( | ||
sdk.NewCoin("uluna", sdk.NewInt(1_000_000)), | ||
) | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govparams) | ||
|
||
// set initial deposit ratio to 0.0 | ||
ratio := sdk.ZeroDec() | ||
suite.app.TreasuryKeeper.SetMinInitialDepositRatio(suite.ctx, ratio) | ||
|
||
// keys and addresses | ||
priv1, _, addr1 := testdata.KeyTestPubAddr() | ||
prop1 := govtypes.NewTextProposal("prop1", "prop1") | ||
depositCoins1 := sdk.NewCoins() | ||
|
||
// create prop tx | ||
msg, _ := govtypes.NewMsgSubmitProposal(prop1, depositCoins1, addr1) | ||
feeAmount := testdata.NewTestFeeAmount() | ||
gasLimit := testdata.NewTestGasLimit() | ||
suite.Require().NoError(suite.txBuilder.SetMsgs(msg)) | ||
suite.txBuilder.SetFeeAmount(feeAmount) | ||
suite.txBuilder.SetGasLimit(gasLimit) | ||
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} | ||
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) | ||
suite.Require().NoError(err) | ||
|
||
// antehandler should not error | ||
_, err = antehandler(suite.ctx, tx, false) | ||
suite.Require().NoError(err, "error: Proposal whithout initial deposit should have gone through") | ||
} | ||
|
||
func (suite *AnteTestSuite) TestMinInitialDepositRatioWithSufficientDeposit() { | ||
suite.SetupTest(true) // setup | ||
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() | ||
|
||
midd := ante.NewMinInitialDepositDecorator(suite.app.GovKeeper, suite.app.TreasuryKeeper) | ||
antehandler := sdk.ChainAnteDecorators(midd) | ||
|
||
// set required deposit to uluna | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govtypes.DefaultDepositParams()) | ||
govparams := suite.app.GovKeeper.GetDepositParams(suite.ctx) | ||
govparams.MinDeposit = sdk.NewCoins( | ||
sdk.NewCoin("uluna", sdk.NewInt(1_000_000)), | ||
) | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govparams) | ||
|
||
// set initial deposit ratio to 0.2 | ||
ratio := sdk.NewDecWithPrec(2, 1) | ||
suite.app.TreasuryKeeper.SetMinInitialDepositRatio(suite.ctx, ratio) | ||
|
||
// keys and addresses | ||
priv1, _, addr1 := testdata.KeyTestPubAddr() | ||
prop1 := govtypes.NewTextProposal("prop1", "prop1") | ||
depositCoins1 := sdk.NewCoins( | ||
sdk.NewCoin("uluna", sdk.NewInt(200_000)), | ||
) | ||
|
||
// create prop tx | ||
msg, _ := govtypes.NewMsgSubmitProposal(prop1, depositCoins1, addr1) | ||
feeAmount := testdata.NewTestFeeAmount() | ||
gasLimit := testdata.NewTestGasLimit() | ||
suite.Require().NoError(suite.txBuilder.SetMsgs(msg)) | ||
suite.txBuilder.SetFeeAmount(feeAmount) | ||
suite.txBuilder.SetGasLimit(gasLimit) | ||
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} | ||
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) | ||
suite.Require().NoError(err) | ||
|
||
// antehandler should not error | ||
_, err = antehandler(suite.ctx, tx, false) | ||
suite.Require().NoError(err, "error: Proposal with sufficient initial deposit should have gone through") | ||
} | ||
|
||
func (suite *AnteTestSuite) TestMinInitialDepositRatioWithInsufficientDeposit() { | ||
suite.SetupTest(true) // setup | ||
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() | ||
|
||
midd := ante.NewMinInitialDepositDecorator(suite.app.GovKeeper, suite.app.TreasuryKeeper) | ||
antehandler := sdk.ChainAnteDecorators(midd) | ||
|
||
// set required deposit to uluna | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govtypes.DefaultDepositParams()) | ||
govparams := suite.app.GovKeeper.GetDepositParams(suite.ctx) | ||
govparams.MinDeposit = sdk.NewCoins( | ||
sdk.NewCoin("uluna", sdk.NewInt(1_000_000)), | ||
) | ||
suite.app.GovKeeper.SetDepositParams(suite.ctx, govparams) | ||
|
||
// set initial deposit ratio to 0.2 | ||
ratio := sdk.NewDecWithPrec(2, 1) | ||
suite.app.TreasuryKeeper.SetMinInitialDepositRatio(suite.ctx, ratio) | ||
|
||
// keys and addresses | ||
priv1, _, addr1 := testdata.KeyTestPubAddr() | ||
prop1 := govtypes.NewTextProposal("prop1", "prop1") | ||
depositCoins1 := sdk.NewCoins( | ||
sdk.NewCoin("uluna", sdk.NewInt(100_000)), | ||
) | ||
|
||
// create prop tx | ||
msg, _ := govtypes.NewMsgSubmitProposal(prop1, depositCoins1, addr1) | ||
feeAmount := testdata.NewTestFeeAmount() | ||
gasLimit := testdata.NewTestGasLimit() | ||
suite.Require().NoError(suite.txBuilder.SetMsgs(msg)) | ||
suite.txBuilder.SetFeeAmount(feeAmount) | ||
suite.txBuilder.SetGasLimit(gasLimit) | ||
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0} | ||
tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) | ||
suite.Require().NoError(err) | ||
|
||
// antehandler should not error | ||
_, err = antehandler(suite.ctx, tx, false) | ||
suite.Require().Error(err, "error: Proposal with insufficient initial deposit should have failed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.