forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle_genesis.go
98 lines (82 loc) · 2.4 KB
/
handle_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
package gov
import (
"encoding/json"
"fmt"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/forbole/bdjuno/v2/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/rs/zerolog/log"
)
// HandleGenesis implements modules.Module
func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "gov").Msg("parsing genesis")
// Read the genesis state
var genState govtypes.GenesisState
err := m.cdc.UnmarshalJSON(appState[govtypes.ModuleName], &genState)
if err != nil {
return fmt.Errorf("error while reading gov genesis data: %s", err)
}
// Save the proposals
err = m.saveProposals(genState.Proposals)
if err != nil {
return fmt.Errorf("error while storing genesis governance proposals: %s", err)
}
// Save the params
err = m.db.SaveGovParams(types.NewGovParams(
types.NewVotingParams(genState.VotingParams),
types.NewDepositParam(genState.DepositParams),
types.NewTallyParams(genState.TallyParams),
doc.InitialHeight,
))
if err != nil {
return fmt.Errorf("error while storing genesis governance params: %s", err)
}
return nil
}
// saveProposals save proposals from genesis file
func (m *Module) saveProposals(slice govtypes.Proposals) error {
proposals := make([]types.Proposal, len(slice))
tallyResults := make([]types.TallyResult, len(slice))
deposits := make([]types.Deposit, len(slice))
for index, proposal := range slice {
// Since it's not possible to get the proposer, set it to nil
proposals[index] = types.NewProposal(
proposal.ProposalId,
proposal.ProposalRoute(),
proposal.ProposalType(),
proposal.GetContent(),
proposal.Status.String(),
proposal.SubmitTime,
proposal.DepositEndTime,
proposal.VotingStartTime,
proposal.VotingEndTime,
"",
)
tallyResults[index] = types.NewTallyResult(
proposal.ProposalId,
proposal.FinalTallyResult.Yes.String(),
proposal.FinalTallyResult.Abstain.String(),
proposal.FinalTallyResult.No.String(),
proposal.FinalTallyResult.NoWithVeto.String(),
1,
)
deposits[index] = types.NewDeposit(
proposal.ProposalId,
"",
proposal.TotalDeposit,
1,
)
}
// Save the proposals
err := m.db.SaveProposals(proposals)
if err != nil {
return err
}
// Save the deposits
err = m.db.SaveDeposits(deposits)
if err != nil {
return err
}
// Save the tally results
return m.db.SaveTallyResults(tallyResults)
}