-
Notifications
You must be signed in to change notification settings - Fork 20
/
abci.go
294 lines (244 loc) · 10.3 KB
/
abci.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
package gov
import (
"fmt"
"github.com/QOSGroup/qos/module/distribution"
"github.com/QOSGroup/qos/module/gov/mapper"
"github.com/QOSGroup/qos/module/gov/types"
"github.com/QOSGroup/qos/module/mint"
"github.com/QOSGroup/qos/module/params"
"github.com/QOSGroup/qos/module/stake"
"github.com/QOSGroup/qos/version"
"github.com/QOSGroup/qbase/account"
"github.com/QOSGroup/qbase/context"
btypes "github.com/QOSGroup/qbase/types"
qtypes "github.com/QOSGroup/qos/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
)
func BeginBlocker(ctx context.Context, req abci.RequestBeginBlock) {
logger := ctx.Logger().With("module", "module/gov")
if ctx.BlockHeight() > 1 {
// software upgrade
proposal := types.Proposal{}
exists := GetMapper(ctx).Get(types.KeySoftUpgradeProposal, &proposal)
if exists {
proposalContent := proposal.ProposalContent.(*types.SoftwareUpgradeProposal)
// start network from zero height
if proposalContent.ForZeroHeight {
panic(fmt.Sprintf("PLEASE INSTALL VERSION: %s, THEN SET THE CORRECT `genesis.json`(DataHeight:%d, MD5:%s) FOR UPGRADING TO %s, YOU CAN DOWNLOAD THE `genesis.json` FROM %s",
proposalContent.Version, proposalContent.DataHeight, proposalContent.GenesisMD5, proposalContent.Version, proposalContent.GenesisFile))
}
// need the new software version
if version.Version != proposalContent.Version {
panic(fmt.Sprintf("PLEASE INSTALL VERSION: %s , THEN RESTART YOUR NODE FOR THE SOFTWARE UPGRADING", proposalContent.Version))
}
GetMapper(ctx).Del(types.KeySoftUpgradeProposal)
logger.Info("software upgrade completed", "proposal", proposal.ProposalID)
}
}
}
// Process proposals
func EndBlocker(ctx context.Context) {
logger := ctx.Logger().With("module", "module/gov")
gm := mapper.GetMapper(ctx)
inactiveIterator := gm.InactiveProposalQueueIterator(ctx.BlockHeader().Time)
defer inactiveIterator.Close()
for ; inactiveIterator.Valid(); inactiveIterator.Next() {
var proposalID int64
gm.GetCodec().UnmarshalBinaryBare(inactiveIterator.Value(), &proposalID)
inactiveProposal, ok := gm.GetProposal(proposalID)
if !ok {
panic(fmt.Sprintf("proposal %d does not exist", proposalID))
}
gm.DeleteProposal(proposalID)
gm.DeleteDeposits(ctx, proposalID) // delete any associated deposits (burned)
gm.Metrics.ProposalStatus.With(mapper.ProposalIdLabel, fmt.Sprintf("%d", proposalID)).Set(float64(types.StatusNil))
ctx.EventManager().EmitEvent(
btypes.NewEvent(
types.EventTypeInactiveProposal,
btypes.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)),
btypes.NewAttribute(types.AttributeKeyProposalResult, types.AttributeKeyDropped),
),
)
logger.Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %d (had only %d); deleted",
inactiveProposal.ProposalID,
inactiveProposal.GetTitle(),
gm.GetLevelParams(ctx, inactiveProposal.GetProposalLevel()).MinDeposit,
inactiveProposal.TotalDeposit,
),
)
}
// fetch active proposals whose voting periods have ended (are passed the block time)
activeIterator := gm.ActiveProposalQueueIterator(ctx.BlockHeader().Time)
defer activeIterator.Close()
for ; activeIterator.Valid(); activeIterator.Next() {
var proposalID int64
gm.GetCodec().UnmarshalBinaryBare(activeIterator.Value(), &proposalID)
activeProposal, ok := gm.GetProposal(proposalID)
if !ok {
panic(fmt.Sprintf("proposal %d does not exist", proposalID))
}
proposalResult, tallyResults, votingValidators, deductOption := mapper.Tally(ctx, gm, activeProposal)
switch deductOption {
case types.DepositDeductNone:
gm.RefundDeposits(ctx, activeProposal.ProposalID, activeProposal.GetProposalLevel(), false)
break
case types.DepositDeductPart:
gm.RefundDeposits(ctx, activeProposal.ProposalID, activeProposal.GetProposalLevel(), true)
break
case types.DepositDeductAll:
gm.DeleteDeposits(ctx, activeProposal.ProposalID)
break
}
var tagValue string
switch proposalResult {
case types.PASS:
activeProposal.Status = types.StatusPassed
tagValue = types.AttributeKeyResultPassed
Execute(ctx, activeProposal, logger)
break
case types.REJECT:
activeProposal.Status = types.StatusRejected
tagValue = types.AttributeKeyResultRejected
break
case types.REJECTVETO:
activeProposal.Status = types.StatusRejected
tagValue = types.AttributeKeyResultVetoRejected
break
}
activeProposal.FinalTallyResult = tallyResults
gm.SetProposal(activeProposal)
gm.RemoveFromActiveProposalQueue(activeProposal.VotingEndTime, activeProposal.ProposalID)
gm.Metrics.ProposalStatus.With(mapper.ProposalIdLabel, fmt.Sprintf("%d", proposalID)).Set(float64(activeProposal.Status))
logger.Info(
fmt.Sprintf(
"proposal %d (%s) tallied; result: %v",
activeProposal.ProposalID, activeProposal.GetTitle(), proposalResult,
),
)
ctx.EventManager().EmitEvent(
btypes.NewEvent(
types.EventTypeActiveProposal,
btypes.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)),
btypes.NewAttribute(types.AttributeKeyProposalResult, tagValue),
),
)
// slash not voted validators
penalty := gm.GetLevelParams(ctx, activeProposal.GetProposalLevel()).Penalty
if penalty.GT(qtypes.ZeroDec()) {
sm := stake.GetMapper(ctx)
var validators []stake.Validator
sm.Get(stake.BuildCurrentValidatorsAddressKey(), &validators)
for _, val := range validators {
if _, ok := votingValidators[val.GetValidatorAddress().String()]; !ok {
if validator, exists := sm.GetValidator(val.GetValidatorAddress()); exists && validator.BondHeight < activeProposal.VotingStartHeight {
e := slash(ctx, validator, penalty, activeProposal.ProposalID)
if e != nil {
logger.Error("slash validator error", "e", e, "validator", validator.GetValidatorAddress().String())
}
}
}
}
}
gm.DeleteValidatorSet(proposalID)
}
}
func slash(ctx context.Context, validator stake.Validator, penalty qtypes.Dec, proposalID int64) error {
log := ctx.Logger().With("module", "module/gov")
log.Debug("slash validator", "proposalId", proposalID, "validator", validator.GetValidatorAddress().String())
sm := stake.GetMapper(ctx)
var delegations []stake.Delegation
sm.IterateDelegationsValDeleAddr(validator.GetValidatorAddress(), func(valAddr btypes.ValAddress, delAddr btypes.AccAddress) {
if delegation, exists := sm.GetDelegationInfo(delAddr, valAddr); exists {
delegations = append(delegations, delegation)
}
})
totalSlashTokens := btypes.ZeroInt()
log.Debug("slash delegations", "delegations", delegations)
for _, delegation := range delegations {
bondTokens := delegation.Amount
// 计算惩罚
tokenSlashed := qtypes.NewDecFromInt(bondTokens).Mul(penalty).TruncateInt()
if !tokenSlashed.LT(bondTokens) {
tokenSlashed = bondTokens
}
totalSlashTokens = totalSlashTokens.Add(tokenSlashed)
// 修改delegator绑定收益
remainTokens := bondTokens.Sub(tokenSlashed)
delegation.Amount = remainTokens
sm.BeforeDelegationModified(ctx, validator.GetValidatorAddress(), delegation.DelegatorAddr, delegation.Amount)
// 更新delegation
sm.SetDelegationInfo(delegation)
log.Debug("slash validator's delegators", "delegator", delegation.DelegatorAddr.String(), "preToken", bondTokens, "slashToken", tokenSlashed, "remainTokens", remainTokens)
}
if totalSlashTokens.GT(validator.BondTokens) {
panic("slash token is greater then validator bondTokens")
}
// 更新validator
sm.ChangeValidatorBondTokens(validator, validator.BondTokens.Sub(totalSlashTokens))
log.Debug("slash validator bond tokens", "validator", validator.GetValidatorAddress().String(), "preTokens", validator.BondTokens, "slashTokens", totalSlashTokens, "afterTokens", validator.BondTokens.Sub(totalSlashTokens))
// slash放入社区费池
sm.AfterValidatorSlashed(ctx, totalSlashTokens)
return nil
}
func Execute(ctx context.Context, proposal types.Proposal, logger log.Logger) error {
switch proposal.GetProposalType() {
case types.ProposalTypeParameterChange:
return executeParameterChange(ctx, proposal, logger)
case types.ProposalTypeTaxUsage:
return executeTaxUsage(ctx, proposal, logger)
case types.ProposalTypeModifyInflation:
return executeModifyInflation(ctx, proposal, logger)
case types.ProposalTypeSoftwareUpgrade:
GetMapper(ctx).Set(types.KeySoftUpgradeProposal, proposal)
return nil
}
return nil
}
func executeParameterChange(ctx context.Context, proposal types.Proposal, logger log.Logger) error {
proposalContent := proposal.ProposalContent.(*types.ParameterProposal)
paramMapper := params.GetMapper(ctx)
for _, param := range proposalContent.Params {
paramSet, exists := paramMapper.GetModuleParamSet(param.Module)
if !exists {
panic(fmt.Sprintf("%s should exists", param.Module))
}
v, _ := paramSet.ValidateKeyValue(param.Key, param.Value)
paramMapper.SetParam(param.Module, param.Key, v)
}
logger.Info("execute parameterChange", "proposal", proposal.ProposalID)
return nil
}
func executeTaxUsage(ctx context.Context, proposal types.Proposal, logger log.Logger) error {
proposalContent := proposal.ProposalContent.(*types.TaxUsageProposal)
dm := distribution.GetMapper(ctx)
accountMapper := ctx.Mapper(account.AccountMapperName).(*account.AccountMapper)
acc := accountMapper.GetAccount(proposalContent.DestAddress)
var account *qtypes.QOSAccount
if acc == nil {
account = qtypes.NewQOSAccountWithAddress(proposalContent.DestAddress)
} else {
account = acc.(*qtypes.QOSAccount)
}
feePool := dm.GetCommunityFeePool()
qos := proposalContent.Percent.MulInt(feePool).TruncateInt()
if feePool.LT(qos) {
return fmt.Errorf("Percent %s may be too bigger. feePoo %s, usage %s", proposalContent.Percent, feePool, qos)
}
account.MustPlusQOS(qos)
accountMapper.SetAccount(account)
dm.SetCommunityFeePool(feePool.Sub(qos))
logger.Info("execute taxUsage", "proposal", proposal.ProposalID)
return nil
}
func executeModifyInflation(ctx context.Context, proposal types.Proposal, logger log.Logger) error {
proposalContent := proposal.ProposalContent.(*types.ModifyInflationProposal)
mintMapper := mint.GetMapper(ctx)
oldPhrases := mintMapper.MustGetInflationPhrases()
phrases := proposalContent.InflationPhrases.Adapt(oldPhrases)
mintMapper.SetInflationPhrases(phrases)
mintMapper.SetTotalQOSAmount(proposalContent.TotalAmount)
logger.Info("execute modifyInflation, proposal: %d", proposal.ProposalID)
return nil
}