-
Notifications
You must be signed in to change notification settings - Fork 182
/
genesis.go
183 lines (158 loc) · 5.87 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gov
import (
"bytes"
"fmt"
"strconv"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/exchain/x/gov/keeper"
"github.com/okex/exchain/x/gov/types"
)
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
StartingProposalID uint64 `json:"starting_proposal_id" yaml:"starting_proposal_id"`
Deposits Deposits `json:"deposits" yaml:"deposits"`
Votes Votes `json:"votes" yaml:"votes"`
Proposals []Proposal `json:"proposals" yaml:"proposals"`
WaitingProposals map[string]uint64 `json:"waiting_proposals" yaml:"waiting_proposals"`
DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"`
VotingParams VotingParams `json:"voting_params" yaml:"voting_params"`
TallyParams TallyParams `json:"tally_params" yaml:"tally_params"`
}
// DefaultGenesisState get raw genesis raw message for testing
func DefaultGenesisState() GenesisState {
var minDeposit = sdk.SysCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(100))}
return GenesisState{
StartingProposalID: 1,
Proposals: []types.Proposal{},
DepositParams: DepositParams{
MinDeposit: minDeposit,
MaxDepositPeriod: time.Hour * 24,
},
VotingParams: VotingParams{
VotingPeriod: time.Hour * 72,
},
TallyParams: TallyParams{
Quorum: sdk.NewDecWithPrec(334, 3),
Threshold: sdk.NewDecWithPrec(5, 1),
Veto: sdk.NewDecWithPrec(334, 3),
YesInVotePeriod: sdk.NewDecWithPrec(667, 3),
},
}
}
// Checks whether 2 GenesisState structs are equivalent.
func (data GenesisState) equal(data2 GenesisState) bool {
b1 := types.ModuleCdc.MustMarshalBinaryBare(data)
b2 := types.ModuleCdc.MustMarshalBinaryBare(data2)
return bytes.Equal(b1, b2)
}
// 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())
}
quorum := data.TallyParams.Quorum
if quorum.IsNegative() || quorum.GT(sdk.OneDec()) {
return fmt.Errorf("governance vote Quorum should be positive and less or equal to one, is %s",
threshold.String())
}
yesInVotePeriod := data.TallyParams.YesInVotePeriod
if yesInVotePeriod.IsNegative() || yesInVotePeriod.GT(sdk.OneDec()) {
return fmt.Errorf("governance vote YesInVotePeriod should be positive and less or equal to one, is %s",
threshold.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
}
// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k keeper.Keeper, supplyKeeper keeper.SupplyKeeper, data GenesisState) {
k.SetProposalID(ctx, data.StartingProposalID)
k.SetDepositParams(ctx, data.DepositParams)
k.SetVotingParams(ctx, data.VotingParams)
k.SetTallyParams(ctx, data.TallyParams)
// check if the deposits pool account exists
moduleAcc := k.GetGovernanceAccount(ctx)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
var totalDeposits sdk.SysCoins
for _, deposit := range data.Deposits {
k.SetDeposit(ctx, deposit)
totalDeposits = totalDeposits.Add(deposit.Amount...)
}
for _, vote := range data.Votes {
k.SetVote(ctx, vote.ProposalID, vote)
}
for _, proposal := range data.Proposals {
switch proposal.Status {
case StatusDepositPeriod:
k.InsertInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
case StatusVotingPeriod:
k.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
}
k.SetProposal(ctx, proposal)
}
for proposalIDStr, height := range data.WaitingProposals {
proposalID, err := strconv.ParseUint(proposalIDStr, 10, 64)
if err != nil {
panic(err)
}
k.InsertWaitingProposalQueue(ctx, height, proposalID)
}
// add coins if not provided on genesis
if moduleAcc.GetCoins().IsZero() {
if err := moduleAcc.SetCoins(totalDeposits); err != nil {
panic(err)
}
supplyKeeper.SetModuleAccount(ctx, moduleAcc)
}
}
// ExportGenesis - output genesis parameters
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) GenesisState {
startingProposalID, err := k.GetProposalID(ctx)
if err != nil {
panic(err)
}
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...)
}
waitingProposals := make(map[string]uint64)
k.IterateAllWaitingProposals(ctx, func(proposal types.Proposal, proposalID, height uint64) (stop bool) {
waitingProposals[strconv.FormatUint(proposalID, 10)] = height
return false
})
return GenesisState{
StartingProposalID: startingProposalID,
Deposits: proposalsDeposits,
Votes: proposalsVotes,
Proposals: proposals,
WaitingProposals: waitingProposals,
DepositParams: depositParams,
VotingParams: votingParams,
TallyParams: tallyParams,
}
}