-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
genesis.go
78 lines (65 loc) · 2.35 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
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/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)
k.SetParams(ctx, *data.Params)
// 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 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()))
}
}
// 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)
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...)
}
return &v1.GenesisState{
StartingProposalId: startingProposalID,
Deposits: proposalsDeposits,
Votes: proposalsVotes,
Proposals: proposals,
Params: ¶ms,
}
}