-
Notifications
You must be signed in to change notification settings - Fork 233
/
proposal.go
194 lines (157 loc) · 5.24 KB
/
proposal.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gov
import (
"encoding/hex"
"fmt"
"strconv"
"time"
"github.com/rs/zerolog/log"
modulestypes "github.com/forbole/bdjuno/v4/modules/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
parsecmdtypes "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/parser"
"github.com/forbole/juno/v5/types/config"
"github.com/spf13/cobra"
"github.com/forbole/bdjuno/v4/database"
"github.com/forbole/bdjuno/v4/modules/distribution"
"github.com/forbole/bdjuno/v4/modules/gov"
"github.com/forbole/bdjuno/v4/modules/mint"
"github.com/forbole/bdjuno/v4/modules/slashing"
"github.com/forbole/bdjuno/v4/modules/staking"
"github.com/forbole/bdjuno/v4/utils"
)
// proposalCmd returns the Cobra command allowing to fix all things related to a proposal
func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
return &cobra.Command{
Use: "proposal [id]",
Short: "Get the description, votes and everything related to a proposal given its id",
RunE: func(cmd *cobra.Command, args []string) error {
proposalID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
parseCtx, err := parsecmdtypes.GetParserContext(config.Cfg, parseConfig)
if err != nil {
return err
}
sources, err := modulestypes.BuildSources(config.Cfg.Node, parseCtx.EncodingConfig)
if err != nil {
return err
}
// Get the database
db := database.Cast(parseCtx.Database)
// Build expected modules of gov modules for handleParamChangeProposal
distrModule := distribution.NewModule(sources.DistrSource, parseCtx.EncodingConfig.Codec, db)
mintModule := mint.NewModule(sources.MintSource, parseCtx.EncodingConfig.Codec, db)
slashingModule := slashing.NewModule(sources.SlashingSource, parseCtx.EncodingConfig.Codec, db)
stakingModule := staking.NewModule(sources.StakingSource, parseCtx.EncodingConfig.Codec, db)
// Build the gov module
govModule := gov.NewModule(sources.GovSource, nil, distrModule, mintModule, slashingModule, stakingModule, parseCtx.EncodingConfig.Codec, db)
err = refreshProposalDetails(parseCtx, proposalID, govModule)
if err != nil {
return err
}
err = refreshProposalDeposits(parseCtx, proposalID, govModule)
if err != nil {
return err
}
err = refreshProposalVotes(parseCtx, proposalID, govModule)
if err != nil {
return err
}
// Update the proposal to the latest status
height, err := parseCtx.Node.LatestHeight()
if err != nil {
return fmt.Errorf("error while getting chain latest block height: %s", err)
}
err = govModule.UpdateProposal(height, time.Now(), proposalID)
if err != nil {
return err
}
return nil
},
}
}
func refreshProposalDetails(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal details")
// Get the tx that created the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("submit_proposal.proposal_id=%d", proposalID))
if err != nil {
return err
}
if len(txs) > 1 {
return fmt.Errorf("expecting only one create proposal transaction, found %d", len(txs))
}
if len(txs) == 0 {
fmt.Printf("error: couldn't find submit proposal tx info")
return nil
}
// Get the tx details
tx, err := parseCtx.Node.Tx(hex.EncodeToString(txs[0].Tx.Hash()))
if err != nil {
return err
}
// Handle the MsgSubmitProposal messages
for index, msg := range tx.GetMsgs() {
if _, ok := msg.(*govtypesv1beta1.MsgSubmitProposal); !ok {
continue
}
err = govModule.HandleMsg(index, msg, tx)
if err != nil {
return fmt.Errorf("error while handling MsgSubmitProposal: %s", err)
}
}
return nil
}
func refreshProposalDeposits(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal deposits")
// Get the tx that deposited to the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_deposit.proposal_id=%d", proposalID))
if err != nil {
return err
}
for _, tx := range txs {
// Get the tx details
junoTx, err := parseCtx.Node.Tx(hex.EncodeToString(tx.Tx.Hash()))
if err != nil {
return err
}
// Handle the MsgDeposit messages
for index, msg := range junoTx.GetMsgs() {
if _, ok := msg.(*govtypesv1.MsgDeposit); !ok {
continue
}
err = govModule.HandleMsg(index, msg, junoTx)
if err != nil {
return fmt.Errorf("error while handling MsgDeposit: %s", err)
}
}
}
return nil
}
func refreshProposalVotes(parseCtx *parser.Context, proposalID uint64, govModule *gov.Module) error {
log.Debug().Msg("refreshing proposal votes")
// Get the tx that voted the proposal
txs, err := utils.QueryTxs(parseCtx.Node, fmt.Sprintf("proposal_vote.proposal_id=%d", proposalID))
if err != nil {
return err
}
for _, tx := range txs {
// Get the tx details
junoTx, err := parseCtx.Node.Tx(hex.EncodeToString(tx.Tx.Hash()))
if err != nil {
return err
}
// Handle the MsgVote messages
for index, msg := range junoTx.GetMsgs() {
if _, ok := msg.(*govtypesv1.MsgVote); !ok {
continue
}
err = govModule.HandleMsg(index, msg, junoTx)
if err != nil {
return fmt.Errorf("error while handling MsgVote: %s", err)
}
}
}
return nil
}