Skip to content

Commit

Permalink
Min Initial Deposit (Recreated using main as base branch) (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
fragwuerdig authored Mar 16, 2023
1 parent 0f8b747 commit 58f2c52
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 49 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/classic-terra/core/app/keepers"
terraappparams "github.com/classic-terra/core/app/params"
v2 "github.com/classic-terra/core/app/upgrades/v2"
v3 "github.com/classic-terra/core/app/upgrades/v3"

customante "github.com/classic-terra/core/custom/auth/ante"
customauthrest "github.com/classic-terra/core/custom/auth/client/rest"
Expand Down Expand Up @@ -193,6 +194,7 @@ func NewTerraApp(
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
IBCChannelKeeper: app.IBCKeeper.ChannelKeeper,
DistributionKeeper: app.DistrKeeper,
GovKeeper: app.GovKeeper,
},
)
if err != nil {
Expand Down Expand Up @@ -402,4 +404,8 @@ func (app *TerraApp) setupUpgradeHandlers() {
v2.UpgradeName,
v2.CreateV2UpgradeHandler(app.mm, app.configurator),
)
app.UpgradeKeeper.SetUpgradeHandler(
v3.UpgradeName,
v3.CreateV3UpgradeHandler(app.mm, app.configurator),
)
}
3 changes: 3 additions & 0 deletions app/upgrades/v3/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v3

const UpgradeName = "v3"
17 changes: 17 additions & 0 deletions app/upgrades/v3/upgrades.go
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)
}
}
3 changes: 3 additions & 0 deletions custom/auth/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cosmosante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
Expand All @@ -22,6 +23,7 @@ type HandlerOptions struct {
SigGasConsumer cosmosante.SignatureVerificationGasConsumer
IBCChannelKeeper channelkeeper.Keeper
DistributionKeeper distributionkeeper.Keeper
GovKeeper govkeeper.Keeper
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
Expand Down Expand Up @@ -70,5 +72,6 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
cosmosante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewAnteDecorator(options.IBCChannelKeeper),
NewMinInitialDepositDecorator(options.GovKeeper, options.TreasuryKeeper),
), nil
}
6 changes: 6 additions & 0 deletions custom/auth/ante/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante
import (
sdk "github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

// TreasuryKeeper for tax charging & recording
Expand All @@ -12,6 +13,7 @@ type TreasuryKeeper interface {
GetTaxCap(ctx sdk.Context, denom string) (taxCap sdk.Int)
GetBurnSplitRate(ctx sdk.Context) sdk.Dec
HasBurnTaxExemptionAddress(ctx sdk.Context, addresses ...string) bool
GetMinInitialDepositRatio(ctx sdk.Context) sdk.Dec
}

// OracleKeeper for feeder validation
Expand All @@ -29,3 +31,7 @@ type DistrKeeper interface {
FundCommunityPool(ctx sdk.Context, amount sdk.Coins, sender sdk.AccAddress) error
GetFeePool(ctx sdk.Context) distributiontypes.FeePool
}

type GovKeeper interface {
GetDepositParams(ctx sdk.Context) govtypes.DepositParams
}
75 changes: 75 additions & 0 deletions custom/auth/ante/min_initial_deposit.go
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)
}
144 changes: 144 additions & 0 deletions custom/auth/ante/min_initial_deposit_test.go
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")
}
5 changes: 5 additions & 0 deletions proto/terra/treasury/v1beta1/treasury.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string min_initial_deposit_ratio = 9 [
(gogoproto.moretags) = "yaml:\"min_initial_deposit_ratio\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// PolicyConstraints - defines policy constraints can be applied in tax & reward policies
Expand Down
7 changes: 7 additions & 0 deletions x/treasury/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {

return nil
}

// Migrate2to3 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
m.keeper.SetMinInitialDepositRatio(ctx, types.DefaultMinInitialDepositRatio)

return nil
}
9 changes: 9 additions & 0 deletions x/treasury/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func (k Keeper) SetBurnSplitRate(ctx sdk.Context, burnTaxSplit sdk.Dec) {
k.paramSpace.Set(ctx, types.KeyBurnTaxSplit, burnTaxSplit)
}

func (k Keeper) GetMinInitialDepositRatio(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.KeyMinInitialDepositRatio, &res)
return
}

func (k Keeper) SetMinInitialDepositRatio(ctx sdk.Context, minInitialDepositRatio sdk.Dec) {
k.paramSpace.Set(ctx, types.KeyMinInitialDepositRatio, minInitialDepositRatio)
}

// GetParams returns the total set of treasury parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
Expand Down
3 changes: 2 additions & 1 deletion x/treasury/legacy/v05/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func TestMigrate(t *testing.T) {
},
"window_long": "52",
"window_probation": "18",
"window_short": "4"
"window_short": "4",
"min_initial_deposit_ratio": "0"
},
"reward_weight": "1.000000000000000000",
"tax_caps": [
Expand Down
7 changes: 6 additions & 1 deletion x/treasury/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err != nil {
panic(err)
}

err = cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the treasury module. It returns
Expand All @@ -158,7 +163,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock returns the begin blocker for the treasury module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {}
Expand Down
Loading

0 comments on commit 58f2c52

Please sign in to comment.