/
utils_proposal.go
297 lines (251 loc) · 8.99 KB
/
utils_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package gov
import (
"fmt"
"strings"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
proposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
"google.golang.org/grpc/codes"
"github.com/rarimo/bdjuno/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1beta "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
func (m *Module) UpdateProposal(height int64, id uint64) error {
// Get the proposal
proposal, err := m.source.Proposal(height, id)
if err != nil {
if strings.Contains(err.Error(), codes.NotFound.String()) {
// Handle case when a proposal is deleted from the chain (did not pass deposit period)
return m.updateDeletedProposalStatus(id)
}
return fmt.Errorf("error while getting proposal: %s", err)
}
err = m.handleBridgeProposal(height, proposal)
if err != nil {
return fmt.Errorf("error while handling bridge proposal: %s", err)
}
err = m.handleOracleManagerProposal(height, proposal)
if err != nil {
return fmt.Errorf("error while handling oraclemanger proposal: %s", err)
}
err = m.handleTokenManagerProposal(height, proposal)
if err != nil {
return fmt.Errorf("error while handling tokenmanager proposal: %s", err)
}
err = m.handleRarimoCoreProposal(height, proposal)
if err != nil {
return fmt.Errorf("error while handling rarimocore proposal: %s", err)
}
err = m.handleParamChangeProposal(height, proposal)
if err != nil {
return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err)
}
err = m.updateProposalStatus(proposal)
if err != nil {
return fmt.Errorf("error while updating proposal status: %s", err)
}
err = m.updateProposalTallyResult(proposal)
if err != nil {
return fmt.Errorf("error while updating proposal tally result: %s", err)
}
err = m.updateAccounts(proposal)
if err != nil {
return fmt.Errorf("error while updating account: %s", err)
}
return nil
}
func (m *Module) UpdateProposalSnapshots(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
err := m.updateProposalStakingPoolSnapshot(height, id)
if err != nil {
return fmt.Errorf("error while updating proposal staking pool snapshot: %s", err)
}
err = m.updateProposalValidatorStatusesSnapshot(height, id, blockVals)
if err != nil {
return fmt.Errorf("error while updating proposal validator statuses snapshot: %s", err)
}
return nil
}
// updateDeletedProposalStatus updates the proposal having the given id by setting its status
// to the one that represents a deleted proposal
func (m *Module) updateDeletedProposalStatus(id uint64) error {
stored, err := m.db.GetProposal(id)
if err != nil {
return err
}
return m.db.UpdateProposal(
types.NewProposalUpdate(
stored.ProposalID,
types.ProposalStatusInvalid,
stored.VotingStartBlock,
stored.VotingEndBlock,
),
)
}
// handleParamChangeProposal updates params to the corresponding modules if a ParamChangeProposal has passed
func (m *Module) handleParamChangeProposal(height int64, proposal govtypesv1beta.Proposal) error {
if proposal.Status != govtypesv1beta.StatusPassed {
// If the status of ParamChangeProposal is not passed, do nothing
return nil
}
var content govtypesv1beta.Content
err := m.db.EncodingConfig.Codec.UnpackAny(proposal.Content, &content)
if err != nil {
return fmt.Errorf("error while handling ParamChangeProposal: %s", err)
}
paramChangeProposal, ok := content.(*proposaltypes.ParameterChangeProposal)
if !ok {
return nil
}
for _, change := range paramChangeProposal.Changes {
// Update the params for corresponding modules
switch change.Subspace {
case distrtypes.ModuleName:
err = m.distrModule.UpdateParams(height)
if err != nil {
return fmt.Errorf("error while updating ParamChangeProposal %s params : %s", distrtypes.ModuleName, err)
}
case govtypes.ModuleName:
err = m.UpdateParams(height)
if err != nil {
return fmt.Errorf("error while updating ParamChangeProposal %s params : %s", govtypes.ModuleName, err)
}
case minttypes.ModuleName:
err = m.mintModule.UpdateParams(height)
if err != nil {
return fmt.Errorf("error while updating ParamChangeProposal %s params : %s", minttypes.ModuleName, err)
}
case slashingtypes.ModuleName:
err = m.slashingModule.UpdateParams(height)
if err != nil {
return fmt.Errorf("error while updating ParamChangeProposal %s params : %s", slashingtypes.ModuleName, err)
}
case stakingtypes.ModuleName:
err = m.stakingModule.UpdateParams(height)
if err != nil {
return fmt.Errorf("error while updating ParamChangeProposal %s params : %s", stakingtypes.ModuleName, err)
}
}
}
return nil
}
// updateProposalStatus updates the given proposal status
func (m *Module) updateProposalStatus(proposal govtypesv1beta.Proposal) error {
return m.db.UpdateProposal(
types.NewProposalUpdate(
proposal.ProposalId,
proposal.Status.String(),
proposal.VotingStartBlock,
proposal.VotingEndBlock,
),
)
}
// updateProposalTallyResult updates the tally result associated with the given proposal
func (m *Module) updateProposalTallyResult(proposal govtypesv1beta.Proposal) error {
height, err := m.db.GetLastBlockHeight()
if err != nil {
return err
}
result, err := m.source.TallyResult(height, proposal.ProposalId)
if err != nil {
return fmt.Errorf("error while getting tally result: %s", err)
}
return m.db.SaveTallyResults([]types.TallyResult{
types.NewTallyResult(
proposal.ProposalId,
result.YesCount,
result.AbstainCount,
result.NoCount,
result.NoWithVetoCount,
height,
),
})
}
// updateAccounts updates any account that might be involved in the proposal (eg. fund community recipient)
func (m *Module) updateAccounts(proposal govtypesv1beta.Proposal) error {
content, ok := proposal.Content.GetCachedValue().(*distrtypes.CommunityPoolSpendProposal)
if ok {
height, err := m.db.GetLastBlockHeight()
if err != nil {
return fmt.Errorf("error while getting last block height: %s", err)
}
addresses := []string{content.Recipient}
err = m.authModule.RefreshAccounts(height, addresses)
if err != nil {
return err
}
return nil
}
return nil
}
// updateProposalStakingPoolSnapshot updates the staking pool snapshot associated with the gov
// proposal having the provided id
func (m *Module) updateProposalStakingPoolSnapshot(height int64, proposalID uint64) error {
pool, err := m.stakingModule.GetStakingPoolSnapshot(height)
if err != nil {
return fmt.Errorf("error while getting staking pool: %s", err)
}
return m.db.SaveProposalStakingPoolSnapshot(
types.NewProposalStakingPoolSnapshot(proposalID, pool),
)
}
// updateProposalValidatorStatusesSnapshot updates the snapshots of the various validators for
// the proposal having the given id
func (m *Module) updateProposalValidatorStatusesSnapshot(
height int64, proposalID uint64, blockVals *tmctypes.ResultValidators,
) error {
validators, _, err := m.stakingModule.GetValidatorsWithStatus(height, stakingtypes.Bonded.String())
if err != nil {
return fmt.Errorf("error while getting validators with bonded status: %s", err)
}
votingPowers, err := m.stakingModule.GetValidatorsVotingPowers(height, blockVals)
if err != nil {
return fmt.Errorf("error while getting validators voting powers: %s", err)
}
statuses, err := m.stakingModule.GetValidatorsStatuses(height, validators)
if err != nil {
return fmt.Errorf("error while getting validator statuses: %s", err)
}
var snapshots = make([]types.ProposalValidatorStatusSnapshot, len(validators))
for index, validator := range validators {
consAddr, err := validator.GetConsAddr()
if err != nil {
return err
}
status, err := findStatus(consAddr.String(), statuses)
if err != nil {
return fmt.Errorf("error while searching for status: %s", err)
}
votingPower, err := findVotingPower(consAddr.String(), votingPowers)
if err != nil {
return fmt.Errorf("error while searching for voting power: %s", err)
}
snapshots[index] = types.NewProposalValidatorStatusSnapshot(
proposalID,
consAddr.String(),
votingPower.VotingPower,
status.Status,
status.Jailed,
height,
)
}
return m.db.SaveProposalValidatorsStatusesSnapshots(snapshots)
}
func findVotingPower(consAddr string, powers []types.ValidatorVotingPower) (types.ValidatorVotingPower, error) {
for _, votingPower := range powers {
if votingPower.ConsensusAddress == consAddr {
return votingPower, nil
}
}
return types.ValidatorVotingPower{}, fmt.Errorf("voting power not found for validator with consensus address %s", consAddr)
}
func findStatus(consAddr string, statuses []types.ValidatorStatus) (types.ValidatorStatus, error) {
for _, status := range statuses {
if status.ConsensusAddress == consAddr {
return status, nil
}
}
return types.ValidatorStatus{}, fmt.Errorf("cannot find status for validator with consensus address %s", consAddr)
}