Skip to content

Commit

Permalink
Merge PR cosmos#3567: x/gov peer review
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored Feb 9, 2019
1 parent 996ab2a commit 02e9dcb
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 209 deletions.
8 changes: 4 additions & 4 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ func TestVote(t *testing.T) {
require.Equal(t, gov.OptionYes, vote.Option)

tally := getTally(t, port, proposalID)
require.Equal(t, sdk.ZeroDec(), tally.Yes, "tally should be 0 as the address is not bonded")
require.Equal(t, sdk.ZeroInt(), tally.Yes, "tally should be 0 as the address is not bonded")

// create bond TX
delTokens := staking.TokensFromTendermintPower(60)
Expand All @@ -885,7 +885,7 @@ func TestVote(t *testing.T) {
expectedBalance = coins[0]

tally = getTally(t, port, proposalID)
require.Equal(t, sdk.NewDecFromInt(delTokens), tally.Yes, "tally should be equal to the amount delegated")
require.Equal(t, delTokens, tally.Yes, "tally should be equal to the amount delegated")

// change vote option
resultTx = doVote(t, port, seed, name1, pw, addr, proposalID, "No", fees)
Expand All @@ -897,8 +897,8 @@ func TestVote(t *testing.T) {
require.Equal(t, expectedBalance.Amount, acc.GetCoins().AmountOf(staking.DefaultBondDenom))

tally = getTally(t, port, proposalID)
require.Equal(t, sdk.ZeroDec(), tally.Yes, "tally should be 0 the user changed the option")
require.Equal(t, sdk.NewDecFromInt(delTokens), tally.No, "tally should be equal to the amount delegated")
require.Equal(t, sdk.ZeroInt(), tally.Yes, "tally should be 0 the user changed the option")
require.Equal(t, delTokens, tally.No, "tally should be equal to the amount delegated")
}

func TestUnjail(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions x/gov/client/module_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
amino "github.com/tendermint/go-amino"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/gov"
govCli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
)

Expand All @@ -22,7 +23,7 @@ func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient {
func (mc ModuleClient) GetQueryCmd() *cobra.Command {
// Group gov queries under a subcommand
govQueryCmd := &cobra.Command{
Use: "gov",
Use: gov.ModuleName,
Short: "Querying commands for the governance module",
}

Expand All @@ -44,7 +45,7 @@ func (mc ModuleClient) GetQueryCmd() *cobra.Command {
// GetTxCmd returns the transaction commands for this module
func (mc ModuleClient) GetTxCmd() *cobra.Command {
govTxCmd := &cobra.Command{
Use: "gov",
Use: gov.ModuleName,
Short: "Governance transactions subcommands",
}

Expand Down
12 changes: 6 additions & 6 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)

type postProposalReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
InitialDeposit sdk.Coins `json:"initial_deposit"` // Coins to add to the proposal's deposit
}

Expand All @@ -71,8 +71,8 @@ type depositReq struct {

type voteReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
}

func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
Expand Down
3 changes: 3 additions & 0 deletions x/gov/depositsvotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (d Deposit) String() string {
type Deposits []Deposit

func (d Deposits) String() string {
if len(d) == 0 {
return "[]"
}
out := fmt.Sprintf("Deposits for Proposal %d:", d[0].ProposalID)
for _, dep := range d {
out += fmt.Sprintf("\n %s: %s", dep.Depositor, dep.Amount)
Expand Down
76 changes: 76 additions & 0 deletions x/gov/endblocker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gov

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/tags"
)

// Called every block, process inflation, update validator set
func EndBlocker(ctx sdk.Context, keeper Keeper) sdk.Tags {
logger := ctx.Logger().With("module", "x/gov")
resTags := sdk.NewTags()

inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time)
defer inactiveIterator.Close()
for ; inactiveIterator.Valid(); inactiveIterator.Next() {
var proposalID uint64

keeper.cdc.MustUnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID)
inactiveProposal := keeper.GetProposal(ctx, proposalID)

keeper.DeleteProposal(ctx, proposalID)
keeper.DeleteDeposits(ctx, proposalID) // delete any associated deposits (burned)

resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposalID))
resTags = resTags.AppendTag(tags.ProposalResult, tags.ActionProposalDropped)

logger.Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted",
inactiveProposal.GetProposalID(),
inactiveProposal.GetTitle(),
keeper.GetDepositParams(ctx).MinDeposit,
inactiveProposal.GetTotalDeposit(),
),
)
}

// fetch active proposals whose voting periods have ended (are passed the block time)
activeIterator := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time)
defer activeIterator.Close()
for ; activeIterator.Valid(); activeIterator.Next() {
var proposalID uint64

keeper.cdc.MustUnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID)
activeProposal := keeper.GetProposal(ctx, proposalID)
passes, tallyResults := tally(ctx, keeper, activeProposal)

var tagValue string
if passes {
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusPassed)
tagValue = tags.ActionProposalPassed
} else {
keeper.DeleteDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusRejected)
tagValue = tags.ActionProposalRejected
}

activeProposal.SetFinalTallyResult(tallyResults)
keeper.SetProposal(ctx, activeProposal)
keeper.RemoveFromActiveProposalQueue(ctx, activeProposal.GetVotingEndTime(), activeProposal.GetProposalID())

logger.Info(
fmt.Sprintf(
"proposal %d (%s) tallied; passed: %v",
activeProposal.GetProposalID(), activeProposal.GetTitle(), passes,
),
)

resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposalID))
resTags = resTags.AppendTag(tags.ProposalResult, tagValue)
}

return resTags
}
3 changes: 1 addition & 2 deletions x/gov/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
DefaultCodespace sdk.CodespaceType = "GOV"
DefaultCodespace sdk.CodespaceType = ModuleName

CodeUnknownProposal sdk.CodeType = 1
CodeInactiveProposal sdk.CodeType = 2
Expand All @@ -23,7 +23,6 @@ const (
CodeInvalidProposalStatus sdk.CodeType = 11
)

//----------------------------------------
// Error constructors

func ErrUnknownProposal(codespace sdk.CodespaceType, proposalID uint64) sdk.Error {
Expand Down
57 changes: 12 additions & 45 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package gov

import (
"bytes"
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
)

const (
// Default period for deposits & voting
DefaultPeriod time.Duration = 86400 * 2 * time.Second // 2 days
)

// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
StartingProposalID uint64 `json:"starting_proposal_id"`
Expand Down Expand Up @@ -47,10 +53,10 @@ func DefaultGenesisState() GenesisState {
StartingProposalID: 1,
DepositParams: DepositParams{
MinDeposit: sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, minDepositTokens)},
MaxDepositPeriod: time.Duration(172800) * time.Second,
MaxDepositPeriod: DefaultPeriod,
},
VotingParams: VotingParams{
VotingPeriod: time.Duration(172800) * time.Second,
VotingPeriod: DefaultPeriod,
},
TallyParams: TallyParams{
Quorum: sdk.NewDecWithPrec(334, 3),
Expand All @@ -63,48 +69,9 @@ func DefaultGenesisState() GenesisState {

// Checks whether 2 GenesisState structs are equivalent.
func (data GenesisState) Equal(data2 GenesisState) bool {
if data.StartingProposalID != data2.StartingProposalID ||
!data.DepositParams.Equal(data2.DepositParams) ||
data.VotingParams != data2.VotingParams ||
data.TallyParams != data2.TallyParams {
return false
}

if len(data.Deposits) != len(data2.Deposits) {
return false
}
for i := range data.Deposits {
deposit1 := data.Deposits[i]
deposit2 := data2.Deposits[i]
if deposit1.ProposalID != deposit2.ProposalID ||
!deposit1.Deposit.Equals(deposit2.Deposit) {
return false
}
}

if len(data.Votes) != len(data2.Votes) {
return false
}
for i := range data.Votes {
vote1 := data.Votes[i]
vote2 := data2.Votes[i]
if vote1.ProposalID != vote2.ProposalID ||
!vote1.Vote.Equals(vote2.Vote) {
return false
}
}

if len(data.Proposals) != len(data2.Proposals) {
return false
}
for i := range data.Proposals {
if !ProposalEqual(data.Proposals[i], data2.Proposals[i]) {
return false
}
}

return true

b1 := msgCdc.MustMarshalBinaryBare(data)
b2 := msgCdc.MustMarshalBinaryBare(data2)
return bytes.Equal(b1, b2)
}

// Returns if a GenesisState is empty or has data in it
Expand All @@ -113,7 +80,7 @@ func (data GenesisState) IsEmpty() bool {
return data.Equal(emptyGenState)
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3007
// ValidateGenesis
func ValidateGenesis(data GenesisState) error {
threshold := data.TallyParams.Threshold
if threshold.IsNegative() || threshold.GT(sdk.OneDec()) {
Expand Down
69 changes: 0 additions & 69 deletions x/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,72 +83,3 @@ func handleMsgVote(ctx sdk.Context, keeper Keeper, msg MsgVote) sdk.Result {
),
}
}

// Called every block, process inflation, update validator set
func EndBlocker(ctx sdk.Context, keeper Keeper) sdk.Tags {
logger := ctx.Logger().With("module", "x/gov")
resTags := sdk.NewTags()

inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time)
for ; inactiveIterator.Valid(); inactiveIterator.Next() {
var proposalID uint64

keeper.cdc.MustUnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID)
inactiveProposal := keeper.GetProposal(ctx, proposalID)

keeper.DeleteProposal(ctx, proposalID)
keeper.DeleteDeposits(ctx, proposalID) // delete any associated deposits (burned)

resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposalID))
resTags = resTags.AppendTag(tags.ProposalResult, tags.ActionProposalDropped)

logger.Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted",
inactiveProposal.GetProposalID(),
inactiveProposal.GetTitle(),
keeper.GetDepositParams(ctx).MinDeposit,
inactiveProposal.GetTotalDeposit(),
),
)
}

inactiveIterator.Close()

activeIterator := keeper.ActiveProposalQueueIterator(ctx, ctx.BlockHeader().Time)
for ; activeIterator.Valid(); activeIterator.Next() {
var proposalID uint64

keeper.cdc.MustUnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID)
activeProposal := keeper.GetProposal(ctx, proposalID)
passes, tallyResults := tally(ctx, keeper, activeProposal)

var tagValue string
if passes {
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusPassed)
tagValue = tags.ActionProposalPassed
} else {
keeper.DeleteDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusRejected)
tagValue = tags.ActionProposalRejected
}

activeProposal.SetFinalTallyResult(tallyResults)
keeper.SetProposal(ctx, activeProposal)
keeper.RemoveFromActiveProposalQueue(ctx, activeProposal.GetVotingEndTime(), activeProposal.GetProposalID())

logger.Info(
fmt.Sprintf(
"proposal %d (%s) tallied; passed: %v",
activeProposal.GetProposalID(), activeProposal.GetTitle(), passes,
),
)

resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposalID))
resTags = resTags.AppendTag(tags.ProposalResult, tagValue)
}

activeIterator.Close()

return resTags
}
Loading

0 comments on commit 02e9dcb

Please sign in to comment.