Skip to content

Commit

Permalink
feat: include a percentage of deposit when submitting a proposal (#7)
Browse files Browse the repository at this point in the history
* feat: include a percentage of deposit when submitting a proposal

* chore: correctly check protobuf breaking changes

* fix: throw a custom error

* fix: correctly adjust min deposit
  • Loading branch information
johnletey committed Jul 4, 2022
1 parent 09f3b24 commit 6627df7
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 124 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BINDIR ?= $(GOPATH)/bin
BUILDDIR ?= $(CURDIR)/build
SIMAPP = ./simapp
MOCKS_DIR = $(CURDIR)/tests/mocks
HTTPS_GIT := https://github.com/cosmos/cosmos-sdk.git
HTTPS_GIT := https://github.com/KYVENetwork/cosmos-sdk.git
DOCKER := $(shell which docker)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf

Expand Down Expand Up @@ -396,7 +396,7 @@ proto-lint:
@$(DOCKER_BUF) lint --error-format=json

proto-check-breaking:
@$(DOCKER_BUF) breaking --against $(HTTPS_GIT)#branch=master
@$(DOCKER_BUF) breaking --against $(HTTPS_GIT)#branch=kyve-main


TM_URL = https://raw.githubusercontent.com/tendermint/tendermint/v0.34.0-rc6/proto/tendermint
Expand Down
1 change: 1 addition & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5088,6 +5088,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. |
| `min_deposit_percentage` | [bytes](#bytes) | | TODO |



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

// TODO
bytes min_deposit_percentage = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "min_deposit_percentage,omitempty",
(gogoproto.moretags) = "yaml:\"min_deposit_percentage\""
];
}

// VotingParams defines the params for voting on governance proposals.
Expand Down
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, sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, types.DefaultMinExpeditedDepositTokens)))
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)), types.DefaultMinDepositPercentage)
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
5 changes: 3 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","min_expedited_deposit":[{"denom":"stake","amount":"50000000"}]}}`,
`{"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"}],"min_deposit_percentage":"0.000000000000000000"}}`,
},
{
"text output",
Expand All @@ -99,6 +99,7 @@ deposit_params:
min_deposit:
- amount: "10000000"
denom: stake
min_deposit_percentage: "0.000000000000000000"
min_expedited_deposit:
- amount: "50000000"
denom: stake
Expand Down Expand Up @@ -159,7 +160,7 @@ func (s *IntegrationTestSuite) TestCmdParam() {
"deposit",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
`{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000","min_expedited_deposit":[{"denom":"stake","amount":"50000000"}]}`,
`{"min_deposit":[{"denom":"stake","amount":"10000000"}],"max_deposit_period":"172800000000000","min_expedited_deposit":[{"denom":"stake","amount":"50000000"}],"min_deposit_percentage":"0.000000000000000000"}`,
},
}

Expand Down
15 changes: 14 additions & 1 deletion x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositorAdd
return false, sdkerrors.Wrapf(types.ErrInactiveProposal, "%d", proposalID)
}

// Check if deposit is enough.
depositParams := keeper.GetDepositParams(ctx)
minDepositAmount := proposal.GetMinDepositFromParams(depositParams)

adjustedMinDeposit := sdk.NewCoins()
for _, coin := range minDepositAmount {
amount := coin.Amount.ToDec().Mul(depositParams.MinDepositPercentage).RoundInt()
adjustedMinDeposit = adjustedMinDeposit.Add(sdk.NewCoin(coin.Denom, amount))
}

if !depositAmount.IsAllGTE(adjustedMinDeposit) {
return false, sdkerrors.Wrapf(types.ErrInvalidDeposit, "%s", depositAmount.String())
}

// update the governance module's account coins pool
err := keeper.bankKeeper.SendCoinsFromAccountToModule(ctx, depositorAddr, types.ModuleName, depositAmount)
if err != nil {
Expand All @@ -130,7 +144,6 @@ 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

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

Expand Down
11 changes: 7 additions & 4 deletions x/gov/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
gocontext "context"
"fmt"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -483,8 +484,9 @@ func (suite *KeeperTestSuite) TestGRPCQueryParams() {
func() {
req = &types.QueryParamsRequest{ParamsType: types.ParamVoting}
expRes = &types.QueryParamsResponse{
VotingParams: types.DefaultVotingParams(),
TallyParams: types.NewTallyParams(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0)),
VotingParams: types.DefaultVotingParams(),
DepositParams: types.NewDepositParams(sdk.NewCoins(), time.Second*0, sdk.NewCoins(), sdk.ZeroDec()),
TallyParams: types.NewTallyParams(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0)),
}
},
true,
Expand All @@ -494,7 +496,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryParams() {
func() {
req = &types.QueryParamsRequest{ParamsType: types.ParamTallying}
expRes = &types.QueryParamsResponse{
TallyParams: types.DefaultTallyParams(),
DepositParams: types.NewDepositParams(sdk.NewCoins(), time.Second*0, sdk.NewCoins(), sdk.ZeroDec()),
TallyParams: types.DefaultTallyParams(),
}
},
true,
Expand All @@ -517,7 +520,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryParams() {

if testCase.expPass {
suite.Require().NoError(err)
suite.Require().Equal(expRes.GetDepositParams(), params.GetDepositParams())
suite.Require().True(expRes.GetDepositParams().Equal(params.GetDepositParams()))
suite.Require().True(expRes.GetVotingParams().Equal(params.GetVotingParams()))
suite.Require().Equal(expRes.GetTallyParams(), params.GetTallyParams())
} else {
Expand Down
1 change: 1 addition & 0 deletions x/gov/legacy/v040/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestMigrate(t *testing.T) {
"deposit_params": {
"max_deposit_period": "0s",
"min_deposit": [],
"min_deposit_percentage": "0",
"min_expedited_deposit": []
},
"deposits": [],
Expand Down
1 change: 1 addition & 0 deletions x/gov/legacy/v043/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestMigrateJSON(t *testing.T) {
"deposit_params": {
"max_deposit_period": "0s",
"min_deposit": [],
"min_deposit_percentage": "0",
"min_expedited_deposit": []
},
"deposits": [],
Expand Down
2 changes: 1 addition & 1 deletion x/gov/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func RandomizedGenState(simState *module.SimulationState) {

govGenesis := types.NewGenesisState(
startingProposalID,
types.NewDepositParams(minDeposit, depositPeriod, minExpeditedDeposit),
types.NewDepositParams(minDeposit, depositPeriod, minExpeditedDeposit, sdk.ZeroDec()),
types.NewVotingParams(votingPeriod, expeditedVotingPeriod, proposalVotingPeriods),
types.NewTallyParams(quorum, threshold, expeditedThreshold, veto),
)
Expand Down
1 change: 1 addition & 0 deletions x/gov/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ var (
ErrInvalidVote = sdkerrors.Register(ModuleName, 7, "invalid vote option")
ErrInvalidGenesis = sdkerrors.Register(ModuleName, 8, "invalid genesis state")
ErrNoProposalHandlerExists = sdkerrors.Register(ModuleName, 9, "no handler exists for proposal type")
ErrInvalidDeposit = sdkerrors.Register(ModuleName, 1000, "invalid deposit amount")
)
6 changes: 6 additions & 0 deletions x/gov/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func ValidateGenesis(data *GenesisState) error {
minDeposit.String())
}

minDepositPercentage := data.DepositParams.MinDepositPercentage
if minDepositPercentage.IsNegative() || minDepositPercentage.GT(sdk.OneDec()) {
return fmt.Errorf("governance min deposit percentage should be positive and less or equal to one, is %s",
minDepositPercentage.String())
}

return nil
}

Expand Down
Loading

0 comments on commit 6627df7

Please sign in to comment.