Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/gov): add max cancel voting period param #18856

Merged
merged 9 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 193 additions & 103 deletions api/cosmos/gov/v1/gov.pulsar.go

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions proto/cosmos/gov/v1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,18 @@
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true];

// burn deposits if a proposal does not meet quorum
//
// Since: cosmos-sdk 0.47
bool burn_vote_quorum = 13;

// burn deposits if the proposal does not enter voting period
//
// Since: cosmos-sdk 0.47
bool burn_proposal_deposit_prevote = 14;

// burn deposits if quorum with vote type no_veto is met
//
// Since: cosmos-sdk 0.47
bool burn_vote_veto = 15;

// The ratio representing the proportion of the deposit value minimum that must be met when making a deposit.
Expand All @@ -304,15 +310,21 @@
// Since: cosmos-sdk 0.50
string min_deposit_ratio = 16 [(cosmos_proto.scalar) = "cosmos.Dec"];

// proposal_cancel_max_cancel_period defines how far in the voting period a proposer can cancel a proposal.
// If the proposal is cancelled before the max cancel period, the deposit will be returned/burn to the
// depositors, according to the proposal_cancel_ratio and proposal_cancel_dest parameters.
// After the max cancel period, the proposal cannot be cancelled anymore.
string proposal_cancel_max_cancel_period = 17 [(cosmos_proto.scalar) = "cosmos.Dec"];

Check failure on line 317 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "17" with name "proposal_cancel_max_cancel_period" on message "Params" changed option "json_name" from "optimisticAuthorizedAddresses" to "proposalCancelMaxCancelPeriod".

Check failure on line 317 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "17" on message "Params" changed label from "repeated" to "optional".

Check failure on line 317 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "17" on message "Params" changed name from "optimistic_authorized_addresses" to "proposal_cancel_max_cancel_period".
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved

// optimistic_authorized_addresses is an optional governance parameter that limits the authorized accounts than can
// submit optimistic proposals
//
// Since: x/gov v1.0.0
repeated string optimistic_authorized_addresses = 17 [(cosmos_proto.scalar) = "cosmos.AddressString"];
repeated string optimistic_authorized_addresses = 18 [(cosmos_proto.scalar) = "cosmos.AddressString"];

Check failure on line 323 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "18" with name "optimistic_authorized_addresses" on message "Params" changed option "json_name" from "optimisticRejectedThreshold" to "optimisticAuthorizedAddresses".

Check failure on line 323 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "18" on message "Params" changed label from "optional" to "repeated".

Check failure on line 323 in proto/cosmos/gov/v1/gov.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "18" on message "Params" changed name from "optimistic_rejected_threshold" to "optimistic_authorized_addresses".

// optimistic rejected threshold defines at which percentage of NO votes, the optimistic proposal should fail and be
// converted to a standard proposal. The threshold is expressed as a percentage of the total bonded tokens.
//
// Since: x/gov v1.0.0
string optimistic_rejected_threshold = 18 [(cosmos_proto.scalar) = "cosmos.Dec"];
string optimistic_rejected_threshold = 19 [(cosmos_proto.scalar) = "cosmos.Dec"];
}
3 changes: 2 additions & 1 deletion x/gov/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#18445](https://github.com/cosmos/cosmos-sdk/pull/18445) Extend gov config
* [#18856](https://github.com/cosmos/cosmos-sdk/pull/18856) Add `ProposalCancelMaxCancelPeriod` for modifying how long a proposal can be cancelled after it has been submitted.
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
* [#18445](https://github.com/cosmos/cosmos-sdk/pull/18445) Extend gov config.
* [#18532](https://github.com/cosmos/cosmos-sdk/pull/18532) Repurpose `govcliutils.NormalizeProposalType` to work for gov v1 proposal types.

### API Breaking Changes
Expand Down
91 changes: 47 additions & 44 deletions x/gov/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ can be adapted to any Proof-Of-Stake blockchain by replacing *ATOM* with the nat
staking token of the chain.

* [Concepts](#concepts)
* [Proposal submission](#proposal-submission)
* [Deposit](#deposit)
* [Vote](#vote)
* [Proposal submission](#proposal-submission)
* [Deposit](#deposit)
* [Vote](#vote)
* [State](#state)
* [Proposals](#proposals)
* [Parameters and base types](#parameters-and-base-types)
* [Deposit](#deposit-1)
* [ValidatorGovInfo](#validatorgovinfo)
* [Stores](#stores)
* [Proposal Processing Queue](#proposal-processing-queue)
* [Legacy Proposal](#legacy-proposal)
* [Proposals](#proposals)
* [Parameters and base types](#parameters-and-base-types)
* [Deposit](#deposit-1)
* [ValidatorGovInfo](#validatorgovinfo)
* [Stores](#stores)
* [Proposal Processing Queue](#proposal-processing-queue)
* [Legacy Proposal](#legacy-proposal)
* [Messages](#messages)
* [Proposal Submission](#proposal-submission-1)
* [Deposit](#deposit-2)
* [Vote](#vote-1)
* [Proposal Submission](#proposal-submission-1)
* [Deposit](#deposit-2)
* [Vote](#vote-1)
* [Events](#events)
* [EndBlocker](#endblocker)
* [Handlers](#handlers)
* [EndBlocker](#endblocker)
* [Handlers](#handlers)
* [Parameters](#parameters)
* [Client](#client)
* [CLI](#cli)
* [gRPC](#grpc)
* [REST](#rest)
* [CLI](#cli)
* [gRPC](#grpc)
* [REST](#rest)
* [Metadata](#metadata)
* [Proposal](#proposal-3)
* [Vote](#vote-5)
* [Proposal](#proposal-3)
* [Vote](#vote-5)
* [Future Improvements](#future-improvements)

## Concepts
Expand Down Expand Up @@ -273,12 +273,12 @@ There are three parameters that define if the deposit of a proposal should be bu
Since this is more of a social feature than a technical feature, we'll now get into some items that may have been useful to have in a genesis constitution:

* What limitations on governance exist, if any?
* is it okay for the community to slash the wallet of a whale that they no longer feel that they want around? (viz: Juno Proposal 4 and 16)
* can governance "socially slash" a validator who is using unapproved MEV? (viz: commonwealth.im/osmosis)
* In the event of an economic emergency, what should validators do?
* Terra crash of May, 2022, saw validators choose to run a new binary with code that had not been approved by governance, because the governance token had been inflated to nothing.
* is it okay for the community to slash the wallet of a whale that they no longer feel that they want around? (viz: Juno Proposal 4 and 16)
* can governance "socially slash" a validator who is using unapproved MEV? (viz: commonwealth.im/osmosis)
* In the event of an economic emergency, what should validators do?
* Terra crash of May, 2022, saw validators choose to run a new binary with code that had not been approved by governance, because the governance token had been inflated to nothing.
* What is the purpose of the chain, specifically?
* best example of this is the Cosmos hub, where different founding groups, have different interpretations of the purpose of the network.
* best example of this is the Cosmos hub, where different founding groups, have different interpretations of the purpose of the network.

This genesis entry, "constitution" hasn't been designed for existing chains, who should likely just ratify a constitution using their governance system. Instead, this is for new chains. It will allow for validators to have a much clearer idea of purpose and the expecations placed on them while operating their nodes. Likewise, for community members, the constitution will give them some idea of what to expect from both the "chain team" and the validators, respectively.

Expand Down Expand Up @@ -480,7 +480,7 @@ The `initialDeposit` must be strictly positive and conform to the accepted denom
* Initialise `Proposal`'s attributes
* Decrease balance of sender by `InitialDeposit`
* If `MinDeposit` is reached:
* Push `proposalID` in `ProposalProcessingQueue`
* Push `proposalID` in `ProposalProcessingQueue`
* Transfer `InitialDeposit` from the `Proposer` to the governance `ModuleAccount`

### Deposit
Expand All @@ -504,7 +504,7 @@ https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/gov/v1/tx.pro
* Add `deposit` of sender in `proposal.Deposits`
* Increase `proposal.TotalDeposit` by sender's `deposit`
* If `MinDeposit` is reached:
* Push `proposalID` in `ProposalProcessingQueueEnd`
* Push `proposalID` in `ProposalProcessingQueueEnd`
* Transfer `Deposit` from the `proposer` to the governance `ModuleAccount`

### Vote
Expand Down Expand Up @@ -591,23 +591,26 @@ The governance module emits the following events:

The governance module contains the following parameters:

| Key | Type | Example |
| ------------------------------- | ---------------------- | --------------------------------------- |
| min_deposit | array (coins) | [{"denom":"uatom","amount":"10000000"}] |
| max_deposit_period | string (time ns) | "172800000000000" (17280s) |
| voting_period | string (time ns) | "172800000000000" (17280s) |
| quorum | string (dec) | "0.334000000000000000" |
| threshold | string (dec) | "0.500000000000000000" |
| veto | string (dec) | "0.334000000000000000" |
| expedited_threshold | string (time ns) | "0.667000000000000000" |
| expedited_voting_period | string (time ns) | "86400000000000" (8600s) |
| expedited_min_deposit | array (coins) | [{"denom":"uatom","amount":"50000000"}] |
| burn_proposal_deposit_prevote | bool | false |
| burn_vote_quorum | bool | false |
| burn_vote_veto | bool | true |
| min_initial_deposit_ratio | string | "0.1" |
| optimistic_rejected_threshold | string (dec) | "0.1" |
| optimistic_authorized_addresses | bytes array (addresses) | [][] |
| Key | Type | Example |
| --------------------------------- | ----------------- | --------------------------------------- |
| min_deposit | array (coins) | [{"denom":"uatom","amount":"10000000"}] |
| max_deposit_period | string (time ns) | "172800000000000" (17280s) |
| voting_period | string (time ns) | "172800000000000" (17280s) |
| quorum | string (dec) | "0.334000000000000000" |
| threshold | string (dec) | "0.500000000000000000" |
| veto | string (dec) | "0.334000000000000000" |
| expedited_threshold | string (time ns) | "0.667000000000000000" |
| expedited_voting_period | string (time ns) | "86400000000000" (8600s) |
| expedited_min_deposit | array (coins) | [{"denom":"uatom","amount":"50000000"}] |
| burn_proposal_deposit_prevote | bool | false |
| burn_vote_quorum | bool | false |
| burn_vote_veto | bool | true |
| min_initial_deposit_ratio | string | "0.1" |
| proposal_cancel_ratio | string (dec) | "0.5" |
| proposal_cancel_dest | string (address) | "cosmos1.." or empty for burn |
| proposal_cancel_max_cancel_period | string (dec) | "0.5" |
| optimistic_rejected_threshold | string (dec) | "0.1" |
| optimistic_authorized_addresses | array (addresses) | [] |

**NOTE**: The governance module contains parameters that are objects unlike other
modules. If only a subset of parameters are desired to be changed, only they need
Expand Down
4 changes: 2 additions & 2 deletions x/gov/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ func (suite *KeeperTestSuite) TestMsgCancelProposal() {
},
depositor: proposer,
expErr: true,
expErrMsg: "not found",
expErrMsg: "proposal 0 doesn't exist",
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
},
"valid proposal but invalid proposer": {
preRun: func() uint64 {
return proposalID
return res.ProposalId
},
depositor: addrs[1],
expErr: true,
Expand Down
29 changes: 21 additions & 8 deletions x/gov/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/gov/types"
v1 "cosmossdk.io/x/gov/types/v1"

Expand Down Expand Up @@ -126,6 +127,14 @@ func (keeper Keeper) SubmitProposal(ctx context.Context, messages []sdk.Msg, met
func (keeper Keeper) CancelProposal(ctx context.Context, proposalID uint64, proposer string) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
proposal, err := keeper.Proposals.Get(ctx, proposalID)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.ErrInvalidProposal.Wrapf("proposal %d doesn't exist", proposalID)
}
return err
}

params, err := keeper.Params.Get(ctx)
if err != nil {
return err
}
Expand All @@ -146,18 +155,22 @@ func (keeper Keeper) CancelProposal(ctx context.Context, proposalID uint64, prop
return types.ErrInvalidProposal.Wrap("proposal should be in the deposit or voting period")
}

// Check proposal voting period is ended.
if proposal.VotingEndTime != nil && proposal.VotingEndTime.Before(sdkCtx.HeaderInfo().Time) {
return types.ErrVotingPeriodEnded.Wrapf("voting period is already ended for this proposal %d", proposalID)
// Check proposal is not too far in voting period to be canceled
if proposal.VotingEndTime != nil {
currentTime := sdkCtx.HeaderInfo().Time

maxCancelPeriodRate := sdkmath.LegacyMustNewDecFromStr(params.ProposalCancelMaxCancelPeriod)
maxCancelPeriod := time.Duration(float64(proposal.VotingEndTime.Sub(*proposal.VotingStartTime)) * maxCancelPeriodRate.MustFloat64()).Round(time.Second)

if proposal.VotingEndTime.Before(currentTime) {
return types.ErrVotingPeriodEnded.Wrapf("voting period is already ended for this proposal %d", proposalID)
} else if proposal.VotingEndTime.Add(-maxCancelPeriod).Before(currentTime) {
return types.ErrTooLateToCancel.Wrapf("proposal %d is too late to cancel", proposalID)
}
}

// burn the (deposits * proposal_cancel_rate) amount or sent to cancellation destination address.
// and deposits * (1 - proposal_cancel_rate) will be sent to depositors.
params, err := keeper.Params.Get(ctx)
if err != nil {
return err
}

err = keeper.ChargeDeposit(ctx, proposal.Id, params.ProposalCancelDest, params.ProposalCancelRatio)
if err != nil {
return err
Expand Down
48 changes: 33 additions & 15 deletions x/gov/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -216,32 +217,32 @@ func (suite *KeeperTestSuite) TestCancelProposal() {
suite.Require().NoError(err)

testCases := []struct {
name string
malleate func() (proposalID uint64, proposer string)
proposalID uint64
proposer string
expectedErr bool
name string
malleate func() (proposalID uint64, proposer string)
proposalID uint64
proposer string
expectedErrMsg string
}{
{
name: "without proposer",
malleate: func() (uint64, string) {
return 1, ""
return proposalID, ""
},
expectedErr: true,
expectedErrMsg: "invalid proposer",
},
{
name: "invalid proposal id",
malleate: func() (uint64, string) {
return 1, suite.addrs[1].String()
return 10, suite.addrs[1].String()
},
expectedErr: true,
expectedErrMsg: "proposal 10 doesn't exist",
},
{
name: "valid proposalID but invalid proposer",
malleate: func() (uint64, string) {
return proposalID, suite.addrs[1].String()
},
expectedErr: true,
expectedErrMsg: "invalid proposer",
},
{
name: "valid proposalID but invalid proposal which has already passed",
Expand All @@ -255,14 +256,32 @@ func (suite *KeeperTestSuite) TestCancelProposal() {
suite.Require().NoError(err)
return proposal2ID, suite.addrs[1].String()
},
expectedErr: true,
expectedErrMsg: "proposal should be in the deposit or voting period",
},
{
name: "proposal canceled too late",
malleate: func() (uint64, string) {
suite.Require().NoError(suite.govKeeper.ActivateVotingPeriod(suite.ctx, proposal2))

proposal2, err = suite.govKeeper.Proposals.Get(suite.ctx, proposal2.Id)
suite.Require().Nil(err)

headerInfo := suite.ctx.HeaderInfo()
// try to cancel 1min before the end of the voting period
// this should fail, as we allow to cancel proposal (by default) up to 1/2 of the voting period
headerInfo.Time = proposal2.VotingEndTime.Add(-1 * time.Minute)
suite.ctx = suite.ctx.WithHeaderInfo(headerInfo)

suite.Require().NoError(err)
return proposal2ID, suite.addrs[1].String()
},
expectedErrMsg: "too late",
},
{
name: "valid proposer and proposal id",
malleate: func() (uint64, string) {
return proposalID, suite.addrs[0].String()
},
expectedErr: false,
},
{
name: "valid case with deletion of votes",
Expand All @@ -283,16 +302,15 @@ func (suite *KeeperTestSuite) TestCancelProposal() {

return proposalID, suite.addrs[0].String()
},
expectedErr: false,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
pID, proposer := tc.malleate()
err = suite.govKeeper.CancelProposal(suite.ctx, pID, proposer)
if tc.expectedErr {
suite.Require().Error(err)
if tc.expectedErrMsg != "" {
suite.Require().ErrorContains(err, tc.expectedErrMsg)
} else {
suite.Require().NoError(err)
}
Expand Down
1 change: 1 addition & 0 deletions x/gov/migrations/v6/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func MigrateStore(ctx sdk.Context, paramsCollection collections.Item[v1.Params],
defaultParams := v1.DefaultParams()
govParams.OptimisticAuthorizedAddresses = defaultParams.OptimisticAuthorizedAddresses
govParams.OptimisticRejectedThreshold = defaultParams.OptimisticRejectedThreshold
govParams.ProposalCancelMaxCancelPeriod = defaultParams.ProposalCancelMaxCancelPeriod

return paramsCollection.Set(ctx, govParams)
}
Loading
Loading