-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgenesis.go
128 lines (108 loc) · 4.19 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
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/atomone-hub/atomone/x/gov/keeper"
"github.com/atomone-hub/atomone/x/gov/types"
v1 "github.com/atomone-hub/atomone/x/gov/types/v1"
)
// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, data *v1.GenesisState) {
k.SetProposalID(ctx, data.StartingProposalId)
if err := k.SetParams(ctx, *data.Params); err != nil {
panic(fmt.Sprintf("%s module params has not been set", types.ModuleName))
}
k.SetConstitution(ctx, data.Constitution)
// 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.Coins
for _, deposit := range data.Deposits {
k.SetDeposit(ctx, *deposit)
totalDeposits = totalDeposits.Add(deposit.Amount...)
}
for _, vote := range data.Votes {
k.SetVote(ctx, *vote)
}
for _, proposal := range data.Proposals {
switch proposal.Status {
case v1.StatusDepositPeriod:
k.InsertInactiveProposalQueue(ctx, proposal.Id, *proposal.DepositEndTime)
case v1.StatusVotingPeriod:
k.InsertActiveProposalQueue(ctx, proposal.Id, *proposal.VotingEndTime)
}
k.SetProposal(ctx, *proposal)
if data.Params.QuorumCheckCount > 0 && proposal.Status == v1.StatusVotingPeriod {
quorumTimeoutTime := proposal.VotingStartTime.Add(*data.Params.QuorumTimeout)
quorumCheckEntry := v1.NewQuorumCheckQueueEntry(quorumTimeoutTime, data.Params.QuorumCheckCount)
quorum := false
if ctx.BlockTime().After(quorumTimeoutTime) {
quorum = k.HasReachedQuorum(ctx, *proposal)
if !quorum {
// since we don't export the state of the quorum check queue, we can't know how many checks were actually
// done. However, in order to trigger a vote time extension, it is enough to have QuorumChecksDone > 0 to
// trigger a vote time extension, so we set it to 1
quorumCheckEntry.QuorumChecksDone = 1
}
}
if !quorum {
k.InsertQuorumCheckQueue(ctx, proposal.Id, quorumTimeoutTime, quorumCheckEntry)
}
}
}
// if account has zero balance it probably means it's not set, so we set it
balance := bk.GetAllBalances(ctx, moduleAcc.GetAddress())
if balance.IsZero() {
ak.SetModuleAccount(ctx, moduleAcc)
}
// check if total deposits equals balance, if it doesn't panic because there were export/import errors
if !balance.IsEqual(totalDeposits) {
panic(fmt.Sprintf("expected module account was %s but we got %s", balance.String(), totalDeposits.String()))
}
if data.LastMinDeposit != nil {
k.SetLastMinDeposit(ctx, data.LastMinDeposit.Value, *data.LastMinDeposit.Time)
} else {
k.SetLastMinDeposit(ctx, data.Params.MinDepositThrottler.FloorValue, ctx.BlockTime())
}
if data.LastMinInitialDeposit != nil {
k.SetLastMinInitialDeposit(ctx, data.LastMinInitialDeposit.Value, *data.LastMinInitialDeposit.Time)
} else {
k.SetLastMinInitialDeposit(ctx, data.Params.MinInitialDepositThrottler.FloorValue, ctx.BlockTime())
}
}
// ExportGenesis - output genesis parameters
func ExportGenesis(ctx sdk.Context, k *keeper.Keeper) *v1.GenesisState {
startingProposalID, _ := k.GetProposalID(ctx)
proposals := k.GetProposals(ctx)
params := k.GetParams(ctx)
constitution := k.GetConstitution(ctx)
var proposalsDeposits v1.Deposits
var proposalsVotes v1.Votes
for _, proposal := range proposals {
deposits := k.GetDeposits(ctx, proposal.Id)
proposalsDeposits = append(proposalsDeposits, deposits...)
votes := k.GetVotes(ctx, proposal.Id)
proposalsVotes = append(proposalsVotes, votes...)
}
blockTime := ctx.BlockTime()
lastMinDeposit := v1.LastMinDeposit{
Value: k.GetMinDeposit(ctx),
Time: &blockTime,
}
lastMinInitialDeposit := v1.LastMinDeposit{
Value: k.GetMinInitialDeposit(ctx),
Time: &blockTime,
}
return &v1.GenesisState{
StartingProposalId: startingProposalID,
Deposits: proposalsDeposits,
Votes: proposalsVotes,
Proposals: proposals,
Params: ¶ms,
Constitution: constitution,
LastMinDeposit: &lastMinDeposit,
LastMinInitialDeposit: &lastMinInitialDeposit,
}
}