forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_handler.go
98 lines (83 loc) · 2.55 KB
/
genesis_handler.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"
"time"
"github.com/cosmos/cosmos-sdk/codec"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/rs/zerolog/log"
"github.com/faddat/bdjuno/database"
"github.com/faddat/bdjuno/x/gov/types"
)
func HandleGenesis(
appState map[string]json.RawMessage, cdc codec.Marshaler, govClient govtypes.QueryClient, db *database.BigDipperDb,
) 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, govClient, 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(p govtypes.Proposals, govClient govtypes.QueryClient, db *database.BigDipperDb) error {
proposals := make([]types.Proposal, len(p))
tallyResults := make([]types.TallyResult, len(p))
deposits := make([]types.Deposit, len(p))
for index, proposal := range p {
// Since it's not possible to get the proposer, set it to nil
proposals[index] = types.NewProposal(
proposal.GetTitle(),
proposal.GetContent().GetDescription(),
proposal.ProposalRoute(),
proposal.ProposalType(),
proposal.ProposalId,
proposal.Status,
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,
)
// Update the proposal status when the voting period or deposit period ends
update := UpdateProposal(proposal.ProposalId, govClient, db)
if proposal.Status == govtypes.StatusVotingPeriod {
time.AfterFunc(time.Since(proposal.VotingEndTime), update)
} else if proposal.Status == govtypes.StatusDepositPeriod {
time.AfterFunc(time.Since(proposal.DepositEndTime), update)
}
}
// Save the proposals
err := db.SaveProposals(proposals)
if err != nil {
return nil
}
// Save the deposits
err = db.SaveDeposits(deposits)
if err != nil {
return nil
}
// Save the tally results
return db.SaveTallyResults(tallyResults)
}