2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
2019-02-08 18:33:06 -08:00
|
|
|
"bytes"
|
2018-12-11 11:05:49 -08:00
|
|
|
"fmt"
|
2018-09-20 07:53:52 -07:00
|
|
|
"time"
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
2019-02-08 18:33:06 -08:00
|
|
|
const (
|
|
|
|
// Default period for deposits & voting
|
|
|
|
DefaultPeriod time.Duration = 86400 * 2 * time.Second // 2 days
|
|
|
|
)
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// GenesisState - all staking state that must be provided at genesis
|
|
|
|
type GenesisState struct {
|
2019-06-04 11:38:11 -07:00
|
|
|
StartingProposalID uint64 `json:"starting_proposal_id"`
|
|
|
|
Deposits Deposits `json:"deposits"`
|
|
|
|
Votes Votes `json:"votes"`
|
|
|
|
Proposals []Proposal `json:"proposals"`
|
|
|
|
DepositParams DepositParams `json:"deposit_params"`
|
|
|
|
VotingParams VotingParams `json:"voting_params"`
|
|
|
|
TallyParams TallyParams `json:"tally_params"`
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// NewGenesisState creates a new genesis state for the governance module
|
2018-11-06 23:33:18 -08:00
|
|
|
func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) GenesisState {
|
2018-06-21 17:19:14 -07:00
|
|
|
return GenesisState{
|
|
|
|
StartingProposalID: startingProposalID,
|
2018-11-06 23:33:18 -08:00
|
|
|
DepositParams: dp,
|
|
|
|
VotingParams: vp,
|
|
|
|
TallyParams: tp,
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get raw genesis raw message for testing
|
|
|
|
func DefaultGenesisState() GenesisState {
|
2019-02-13 15:01:50 -08:00
|
|
|
minDepositTokens := sdk.TokensFromTendermintPower(10)
|
2018-06-21 17:19:14 -07:00
|
|
|
return GenesisState{
|
|
|
|
StartingProposalID: 1,
|
2018-11-06 23:33:18 -08:00
|
|
|
DepositParams: DepositParams{
|
2019-02-13 15:01:50 -08:00
|
|
|
MinDeposit: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, minDepositTokens)},
|
2019-02-08 18:33:06 -08:00
|
|
|
MaxDepositPeriod: DefaultPeriod,
|
2018-07-26 18:24:41 -07:00
|
|
|
},
|
2018-11-06 23:33:18 -08:00
|
|
|
VotingParams: VotingParams{
|
2019-02-08 18:33:06 -08:00
|
|
|
VotingPeriod: DefaultPeriod,
|
2018-07-26 18:24:41 -07:00
|
|
|
},
|
2018-11-06 23:33:18 -08:00
|
|
|
TallyParams: TallyParams{
|
2019-02-27 08:44:05 -08:00
|
|
|
Quorum: sdk.NewDecWithPrec(334, 3),
|
|
|
|
Threshold: sdk.NewDecWithPrec(5, 1),
|
|
|
|
Veto: sdk.NewDecWithPrec(334, 3),
|
2018-07-26 18:24:41 -07:00
|
|
|
},
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:24:04 -08:00
|
|
|
// Checks whether 2 GenesisState structs are equivalent.
|
|
|
|
func (data GenesisState) Equal(data2 GenesisState) bool {
|
2019-05-16 08:25:32 -07:00
|
|
|
b1 := types.ModuleCdc.MustMarshalBinaryBare(data)
|
|
|
|
b2 := types.ModuleCdc.MustMarshalBinaryBare(data2)
|
2019-02-08 18:33:06 -08:00
|
|
|
return bytes.Equal(b1, b2)
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns if a GenesisState is empty or has data in it
|
|
|
|
func (data GenesisState) IsEmpty() bool {
|
|
|
|
emptyGenState := GenesisState{}
|
|
|
|
return data.Equal(emptyGenState)
|
|
|
|
}
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// ValidateGenesis checks if parameters are within valid ranges
|
2018-12-06 16:22:24 -08:00
|
|
|
func ValidateGenesis(data GenesisState) error {
|
2018-12-11 11:05:49 -08:00
|
|
|
threshold := data.TallyParams.Threshold
|
|
|
|
if threshold.IsNegative() || threshold.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("Governance vote threshold should be positive and less or equal to one, is %s",
|
|
|
|
threshold.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
veto := data.TallyParams.Veto
|
|
|
|
if veto.IsNegative() || veto.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("Governance vote veto threshold should be positive and less or equal to one, is %s",
|
|
|
|
veto.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !data.DepositParams.MinDeposit.IsValid() {
|
|
|
|
return fmt.Errorf("Governance deposit amount must be a valid sdk.Coins amount, is %s",
|
|
|
|
data.DepositParams.MinDeposit.String())
|
|
|
|
}
|
|
|
|
|
2018-12-06 16:22:24 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// InitGenesis - store genesis parameters
|
|
|
|
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
|
2019-06-04 11:38:11 -07:00
|
|
|
|
|
|
|
k.setProposalID(ctx, data.StartingProposalID)
|
2018-11-06 23:33:18 -08:00
|
|
|
k.setDepositParams(ctx, data.DepositParams)
|
|
|
|
k.setVotingParams(ctx, data.VotingParams)
|
|
|
|
k.setTallyParams(ctx, data.TallyParams)
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, deposit := range data.Deposits {
|
2019-06-04 11:38:11 -07:00
|
|
|
k.setDeposit(ctx, deposit.ProposalID, deposit.Depositor, deposit)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
for _, vote := range data.Votes {
|
2019-06-04 11:38:11 -07:00
|
|
|
k.setVote(ctx, vote.ProposalID, vote.Voter, vote)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
for _, proposal := range data.Proposals {
|
2019-03-15 09:47:47 -07:00
|
|
|
switch proposal.Status {
|
2019-01-07 14:24:04 -08:00
|
|
|
case StatusDepositPeriod:
|
2019-06-04 11:38:11 -07:00
|
|
|
k.InsertInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
|
2019-01-07 14:24:04 -08:00
|
|
|
case StatusVotingPeriod:
|
2019-06-04 11:38:11 -07:00
|
|
|
k.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|
2018-11-08 16:28:28 -08:00
|
|
|
k.SetProposal(ctx, proposal)
|
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
// ExportGenesis - output genesis parameters
|
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
|
2019-06-04 11:38:11 -07:00
|
|
|
startingProposalID, _ := k.GetProposalID(ctx)
|
2018-11-06 23:33:18 -08:00
|
|
|
depositParams := k.GetDepositParams(ctx)
|
|
|
|
votingParams := k.GetVotingParams(ctx)
|
|
|
|
tallyParams := k.GetTallyParams(ctx)
|
2019-06-04 11:38:11 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
proposals := k.GetProposalsFiltered(ctx, nil, nil, StatusNil, 0)
|
2019-06-04 11:38:11 -07:00
|
|
|
|
|
|
|
var proposalsDeposits Deposits
|
|
|
|
var proposalsVotes Votes
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, proposal := range proposals {
|
2019-06-04 11:38:11 -07:00
|
|
|
deposits := k.GetDeposits(ctx, proposal.ProposalID)
|
|
|
|
proposalsDeposits = append(proposalsDeposits, deposits...)
|
|
|
|
|
|
|
|
votes := k.GetVotes(ctx, proposal.ProposalID)
|
|
|
|
proposalsVotes = append(proposalsVotes, votes...)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
return GenesisState{
|
2018-07-26 18:24:41 -07:00
|
|
|
StartingProposalID: startingProposalID,
|
2019-06-04 11:38:11 -07:00
|
|
|
Deposits: proposalsDeposits,
|
|
|
|
Votes: proposalsVotes,
|
2018-11-08 16:28:28 -08:00
|
|
|
Proposals: proposals,
|
2018-11-06 23:33:18 -08:00
|
|
|
DepositParams: depositParams,
|
|
|
|
VotingParams: votingParams,
|
|
|
|
TallyParams: tallyParams,
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|