-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
genesis.go
80 lines (67 loc) · 2.25 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
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k Keeper, supplyKeeper types.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.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 StatusDepositPeriod:
k.InsertInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
case StatusVotingPeriod:
k.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
}
k.SetProposal(ctx, proposal)
}
// 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) GenesisState {
startingProposalID, _ := k.GetProposalID(ctx)
depositParams := k.GetDepositParams(ctx)
votingParams := k.GetVotingParams(ctx)
tallyParams := k.GetTallyParams(ctx)
proposals := k.GetProposals(ctx)
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...)
}
return GenesisState{
StartingProposalID: startingProposalID,
Deposits: proposalsDeposits,
Votes: proposalsVotes,
Proposals: proposals,
DepositParams: depositParams,
VotingParams: votingParams,
TallyParams: tallyParams,
}
}