cosmos-sdk/x/gov/genesis.go

146 lines
4.5 KiB
Go
Raw Normal View History

2018-06-21 17:19:14 -07:00
package gov
import (
2019-02-08 18:33:06 -08:00
"bytes"
"fmt"
"time"
2018-06-21 17:19:14 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
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 {
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
}
// NewGenesisState creates a new genesis state for the governance module
func NewGenesisState(startingProposalID uint64, dp DepositParams, vp VotingParams, tp TallyParams) GenesisState {
2018-06-21 17:19:14 -07:00
return GenesisState{
StartingProposalID: startingProposalID,
DepositParams: dp,
VotingParams: vp,
TallyParams: tp,
2018-06-21 17:19:14 -07:00
}
}
// get raw genesis raw message for testing
func DefaultGenesisState() GenesisState {
minDepositTokens := sdk.TokensFromConsensusPower(10)
2018-06-21 17:19:14 -07:00
return GenesisState{
StartingProposalID: 1,
DepositParams: DepositParams{
MinDeposit: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, minDepositTokens)},
2019-02-08 18:33:06 -08:00
MaxDepositPeriod: DefaultPeriod,
},
VotingParams: VotingParams{
2019-02-08 18:33:06 -08:00
VotingPeriod: DefaultPeriod,
},
TallyParams: TallyParams{
Quorum: sdk.NewDecWithPrec(334, 3),
Threshold: sdk.NewDecWithPrec(5, 1),
Veto: sdk.NewDecWithPrec(334, 3),
},
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 {
Merge PR #4159: Module/Genesis Generalization * first commit * gaia cleanup * ... * staking multihooks * missing module function return args * bank module name constant * working, module interface for x/ * got this thing compiling * make test compiles and passes * remove expanded simulation invariants * genesis issue * continued * continued * register crisis routes thought mm * begin blocker to mm * end blocker to mm * empty routes not initialized * move gaia initChainer sanity check to baseapp * remove codecs from module manager * reorging genesis stuff * module manager passed by reference/bugfixes from working last commit int int * move invariant checks from gaia to crisis * typo * basic refactors cmd/gaia/init * working * MultiStakingHooks from types to x/staking/types int * default module manager order of operations from input modules * working * typo * add AppModuleBasic * moduleBasicManager / non-test code compiles * working attempting to get tests passing * make test passes * sim random genesis fix * export bug * ... * genutil module * genutil working * refactored - happy with non-testing code in cmd/ * ... * lint fixes * comment improvement * cli test fix * compile housing * working through compile errors * working gettin' compilin' * non-test code compiles * move testnet to its own module * reworking tests int * bez staging PR 1 comments * concise module function-of names * moved all tests from genesis_test.go to other genutil tests * genaccounts package, add genutil and genaccounts to app.go * docs for genutil genaccounts * genaccounts iterate fn * non-test code with genaccounts/ now compiles * working test compiling * debugging tests * resolved all make test compile errors * test debuggin * resolved all unit tests, introduced param module * cli-test compile fixes * staking initialization bug * code comment improvements, changelog entries * BasicGaiaApp -> ModuleBasics * highlevel explanation in types/module.go * @alexanderbez comment revisions * @fedekunze PR comments * @alexanderbez PR comments (x2) * @cwgoes comments (minor updates) * @fedekunze suggestions * panic on init with multiple validator updates from different modules * initchain panic makes validate genesis fail int * AppModuleGenesis seperation int * test * remove init panic logic in validate genesis replaced with TODO * set maxprocs to match system's GOMAXPROCS * Update circleci * Cap maxprocs in CI to 4 * @alexanderbez recent comments addressed * less blocks in twouble sims int * runsim error output flag * -e on import_export as well * error out int * Try to fix failures * runsim
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)
}
// ValidateGenesis checks if parameters are within valid ranges
func ValidateGenesis(data GenesisState) error {
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())
}
return nil
}
2018-06-21 17:19:14 -07:00
// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
k.setProposalID(ctx, data.StartingProposalID)
k.setDepositParams(ctx, data.DepositParams)
k.setVotingParams(ctx, data.VotingParams)
k.setTallyParams(ctx, data.TallyParams)
for _, deposit := range data.Deposits {
k.setDeposit(ctx, deposit.ProposalID, deposit.Depositor, deposit)
}
for _, vote := range data.Votes {
k.setVote(ctx, vote.ProposalID, vote.Voter, vote)
}
for _, proposal := range data.Proposals {
switch proposal.Status {
2019-01-07 14:24:04 -08:00
case StatusDepositPeriod:
k.InsertInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
2019-01-07 14:24:04 -08:00
case StatusVotingPeriod:
k.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
2019-01-07 14:24:04 -08:00
}
k.SetProposal(ctx, proposal)
}
2018-06-21 17:19:14 -07:00
}
// ExportGenesis - output genesis parameters
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
startingProposalID, _ := k.GetProposalID(ctx)
depositParams := k.GetDepositParams(ctx)
votingParams := k.GetVotingParams(ctx)
tallyParams := k.GetTallyParams(ctx)
proposals := k.GetProposalsFiltered(ctx, nil, nil, StatusNil, 0)
var proposalsDeposits Deposits
var proposalsVotes Votes
for _, proposal := range proposals {
deposits := k.GetDeposits(ctx, proposal.ProposalID)
proposalsDeposits = append(proposalsDeposits, deposits...)
votes := k.GetVotes(ctx, proposal.ProposalID)
proposalsVotes = append(proposalsVotes, votes...)
}
2018-06-21 17:19:14 -07:00
return GenesisState{
StartingProposalID: startingProposalID,
Deposits: proposalsDeposits,
Votes: proposalsVotes,
Proposals: proposals,
DepositParams: depositParams,
VotingParams: votingParams,
TallyParams: tallyParams,
2018-06-21 17:19:14 -07:00
}
}