-
Notifications
You must be signed in to change notification settings - Fork 237
/
handle_genesis.go
86 lines (72 loc) · 2.05 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
package gov
import (
"encoding/json"
"fmt"
"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/types"
"github.com/cosmos/cosmos-sdk/codec"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/rs/zerolog/log"
)
func HandleGenesis(appState map[string]json.RawMessage, cdc codec.Marshaler, db *database.Db) error {
log.Debug().Str("module", "gov").Msg("parsing genesis")
// Read the genesis state
var genState govtypes.GenesisState
err := cdc.UnmarshalJSON(appState[govtypes.ModuleName], &genState)
if err != nil {
return fmt.Errorf("error while reading gov genesis data: %s", err)
}
// Save the proposals
err = saveProposals(genState.Proposals, db)
if err != nil {
return fmt.Errorf("error while storing genesis governance proposals: %s", err)
}
return nil
}
// saveProposals save proposals from genesis file
func saveProposals(slice govtypes.Proposals, db *database.Db) 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.Int64(),
proposal.FinalTallyResult.Abstain.Int64(),
proposal.FinalTallyResult.No.Int64(),
proposal.FinalTallyResult.NoWithVeto.Int64(),
1,
)
deposits[index] = types.NewDeposit(
proposal.ProposalId,
"",
proposal.TotalDeposit,
1,
)
}
// Save the proposals
err := db.SaveProposals(proposals)
if err != nil {
return err
}
// Save the deposits
err = db.SaveDeposits(deposits)
if err != nil {
return err
}
// Save the tally results
return db.SaveTallyResults(tallyResults)
}