Skip to content

Commit

Permalink
feat: expedited deposits (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey authored Jun 21, 2022
1 parent 3abc1c5 commit 8e803c7
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 238 deletions.
1 change: 1 addition & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5087,6 +5087,7 @@ DepositParams defines the params for deposits on governance proposals.
| ----- | ---- | ----- | ----------- |
| `min_deposit` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | Minimum deposit for a proposal to enter voting period. |
| `max_deposit_period` | [google.protobuf.Duration](#google.protobuf.Duration) | | Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months. |
| `min_expedited_deposit` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | Minimum expedited deposit for a proposal to enter voting period. |



Expand Down
9 changes: 8 additions & 1 deletion proto/cosmos/gov/v1beta1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ message DepositParams {
(gogoproto.jsontag) = "max_deposit_period,omitempty",
(gogoproto.moretags) = "yaml:\"max_deposit_period\""
];

// Minimum expedited deposit for a proposal to enter voting period.
repeated cosmos.base.v1beta1.Coin min_expedited_deposit = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.moretags) = "yaml:\"min_expedited_deposit\"",
(gogoproto.jsontag) = "min_expedited_deposit,omitempty"
];
}

// VotingParams defines the params for voting on governance proposals.
Expand Down Expand Up @@ -210,7 +218,6 @@ message TallyParams {
(gogoproto.moretags) = "yaml:\"veto_threshold\""
];


// Minimum proportion of Yes votes for an expedited proposal to pass. Default value: 0.67.
bytes expedited_threshold = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
Expand Down
3 changes: 2 additions & 1 deletion x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
logger.Info(
"proposal did not meet minimum deposit; deleted",
"proposal", proposal.ProposalId,
"is_expedited", proposal.IsExpedited,
"title", proposal.GetTitle(),
"min_deposit", keeper.GetDepositParams(ctx).MinDeposit.String(),
"min_deposit", proposal.GetMinDepositFromParams(keeper.GetDepositParams(ctx)).String(),
"total_deposit", proposal.TotalDeposit.String(),
)

Expand Down
34 changes: 25 additions & 9 deletions x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ func TestTickPassedVotingPeriod(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
testProposal := TestProposal

depositMultiplier := getDepositMultiplier(tc.isExpedited)

app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens)
Expand All @@ -242,7 +244,7 @@ func TestTickPassedVotingPeriod(t *testing.T) {
require.False(t, activeQueue.Valid())
activeQueue.Close()

proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5))}
proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5*depositMultiplier))}
newProposalMsg, err := types.NewMsgSubmitProposal(testProposal, proposalCoins, addrs[0], tc.isExpedited)
require.NoError(t, err)

Expand Down Expand Up @@ -319,25 +321,27 @@ func TestTickPassedVotingPeriod(t *testing.T) {
func TestProposalPassedEndblocker(t *testing.T) {
testcases := []struct {
name string
IsExpedited bool
isExpedited bool
}{
{
name: "regular text",
IsExpedited: false,
isExpedited: false,
},
{
name: "text expedited",
IsExpedited: true,
isExpedited: true,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
testProposal := TestProposal

depositMultiplier := getDepositMultiplier(tc.isExpedited)

app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens)
addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens.Mul(sdk.NewInt(depositMultiplier)))

SortAddresses(addrs)

Expand All @@ -356,10 +360,10 @@ func TestProposalPassedEndblocker(t *testing.T) {
require.NotNil(t, macc)
initialModuleAccCoins := app.BankKeeper.GetAllBalances(ctx, macc.GetAddress())

proposal, err := app.GovKeeper.SubmitProposal(ctx, testProposal, tc.IsExpedited)
proposal, err := app.GovKeeper.SubmitProposal(ctx, testProposal, tc.isExpedited)
require.NoError(t, err)

proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 10))}
proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 10*depositMultiplier))}
newDepositMsg := types.NewMsgDeposit(addrs[0], proposal.ProposalId, proposalCoins)

handleAndCheck(t, handler, ctx, newDepositMsg)
Expand Down Expand Up @@ -415,9 +419,11 @@ func TestExpeditedProposal_PassAndConversionToRegular(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
testProposal := TestProposal

depositMultiplier := getDepositMultiplier(true)

app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens)
addrs := simapp.AddTestAddrs(app, ctx, 10, valTokens.Mul(sdk.NewInt(depositMultiplier)))

SortAddresses(addrs)

Expand Down Expand Up @@ -447,7 +453,7 @@ func TestExpeditedProposal_PassAndConversionToRegular(t *testing.T) {
submitterInitialBalance := app.BankKeeper.GetAllBalances(ctx, addrs[0])
depositorInitialBalance := app.BankKeeper.GetAllBalances(ctx, addrs[1])

proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5))}
proposalCoins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5*depositMultiplier))}
newProposalMsg, err := types.NewMsgSubmitProposal(testProposal, proposalCoins, addrs[0], true)
require.NoError(t, err)

Expand Down Expand Up @@ -639,3 +645,13 @@ func TestEndBlockerProposalHandlerFailed(t *testing.T) {
// validate that the proposal fails/has been rejected
gov.EndBlocker(ctx, app.GovKeeper)
}

// With expedited proposal's minimum deposit set higher than the default deposit, we must
// initialize and deposit an amount depositMultiplier times larger
// than the regular min deposit amount.
func getDepositMultiplier(isExpedited bool) int64 {
if !isExpedited {
return 1
}
return types.DefaultMinExpeditedDepositTokens.Quo(types.DefaultMinDepositTokens).Int64()
}
2 changes: 1 addition & 1 deletion x/gov/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, NewIntegrationTestSuite(cfg))

genesisState := types.DefaultGenesisState()
genesisState.DepositParams = types.NewDepositParams(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, types.DefaultMinDepositTokens)), time.Duration(15)*time.Second)
genesisState.DepositParams = types.NewDepositParams(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, types.DefaultMinDepositTokens)), time.Duration(15)*time.Second, sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, types.DefaultMinExpeditedDepositTokens)))
genesisState.VotingParams = types.NewVotingParams(time.Duration(5)*time.Second, time.Duration(2)*time.Second, []types.ProposalVotingPeriod{})
bz, err := cfg.Codec.MarshalJSON(genesisState)
require.NoError(t, err)
Expand Down
7 changes: 5 additions & 2 deletions x/gov/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *IntegrationTestSuite) TestCmdParams() {
{
"json output",
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"voting_params":{"voting_period":"172800000000000","proposal_voting_periods":null,"expedited_voting_period":"86400000000000"},"tally_params":{"quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","expedited_threshold":"0.667000000000000000"},"deposit_params":{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000"}}`,
`{"voting_params":{"voting_period":"172800000000000","proposal_voting_periods":null,"expedited_voting_period":"86400000000000"},"tally_params":{"quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto_threshold":"0.334000000000000000","expedited_threshold":"0.667000000000000000"},"deposit_params":{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000","min_expedited_deposit":[{"denom":"stake","amount":"50000000"}]}}`,
},
{
"text output",
Expand All @@ -99,6 +99,9 @@ deposit_params:
min_deposit:
- amount: "10000000"
denom: stake
min_expedited_deposit:
- amount: "50000000"
denom: stake
tally_params:
expedited_threshold: "0.667000000000000000"
quorum: "0.334000000000000000"
Expand Down Expand Up @@ -156,7 +159,7 @@ func (s *IntegrationTestSuite) TestCmdParam() {
"deposit",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
`{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000"}`,
`{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000","min_expedited_deposit":[{"denom":"stake","amount":"50000000"}]}`,
},
}

Expand Down
3 changes: 2 additions & 1 deletion x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositorAdd
// Check if deposit has provided sufficient total funds to transition the proposal into the voting period
activatedVotingPeriod := false

if proposal.Status == types.StatusDepositPeriod && proposal.TotalDeposit.IsAllGTE(keeper.GetDepositParams(ctx).MinDeposit) {
minDepositAmount := proposal.GetMinDepositFromParams(keeper.GetDepositParams(ctx))
if proposal.Status == types.StatusDepositPeriod && proposal.TotalDeposit.IsAllGTE(minDepositAmount) {
keeper.ActivateVotingPeriod(ctx, proposal)

activatedVotingPeriod = true
Expand Down
Loading

0 comments on commit 8e803c7

Please sign in to comment.