-
Notifications
You must be signed in to change notification settings - Fork 12
/
abci.go
264 lines (229 loc) · 8.58 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
package keeper
import (
"errors"
"fmt"
"strconv"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/tendermint/tendermint/abci/types"
fxstakingtypes "github.com/functionx/fx-core/v5/x/staking/types"
)
func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
// staking EndBlocker
valUpdates := staking.EndBlocker(ctx, k.Keeper)
// process pk update previous block
k.ConsensusProcess(ctx)
// validators update current block, delayed next block
if len(valUpdates) > 0 {
return valUpdates
}
// update validator consensus pubkey
return k.ConsensusPubKeyUpdate(ctx)
}
func (k Keeper) ConsensusProcess(ctx sdk.Context) {
k.IteratorConsensusProcess(ctx, fxstakingtypes.ProcessEnd, func(valAddr sdk.ValAddress, pkBytes []byte) {
// update signing info and remove old consensus pubkey
if err := k.processEnd(ctx, valAddr, pkBytes); err != nil {
panic(err)
}
})
k.IteratorConsensusProcess(ctx, fxstakingtypes.ProcessStart, func(valAddr sdk.ValAddress, pkBytes []byte) {
// update signing info
if err := k.processStart(ctx, valAddr, pkBytes); err != nil {
panic(err)
}
})
}
func (k Keeper) processEnd(ctx sdk.Context, valAddr sdk.ValAddress, pkBytes []byte) error {
k.DeleteConsensusProcess(ctx, valAddr, fxstakingtypes.ProcessEnd)
var oldPubKey cryptotypes.PubKey
if err := k.cdc.UnmarshalInterfaceJSON(pkBytes, &oldPubKey); err != nil {
return fmt.Errorf("invalid pubkey")
}
oldConsAddr := sdk.ConsAddress(oldPubKey.Address())
// old pk signing info
oldSigningInfo, found := k.slashingKeeper.GetValidatorSigningInfo(ctx, oldConsAddr)
if !found {
return fmt.Errorf("validator %s not found signing info", oldConsAddr.String())
}
// remove old consensus pubkey
k.RemoveValidatorConsAddr(ctx, oldConsAddr)
k.slashingKeeper.DeleteConsensusPubKey(ctx, oldConsAddr)
k.slashingKeeper.DeleteValidatorSigningInfo(ctx, oldConsAddr)
// update validator new pk signing info
validator, found := k.GetValidator(ctx, valAddr)
if !found {
// NOTE: validator not found, because it deleted in below case
// 1. validator unbonded and undelegate all (tx)
// 2. unbonding to unbonded and validator share is zero (end block)
k.slashingKeeper.DeleteConsensusPubKey(ctx, oldConsAddr)
return nil
}
newConsAddr, err := validator.GetConsAddr()
if err != nil {
return err
}
if err = k.updateSigningInfo(ctx, newConsAddr, oldSigningInfo); err != nil {
return err
}
// success event
ctx.EventManager().EmitEvent(sdk.NewEvent(
fxstakingtypes.EventTypeEditedConsensusPubKey,
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
))
return nil
}
func (k Keeper) processStart(ctx sdk.Context, valAddr sdk.ValAddress, pkBytes []byte) error {
k.DeleteConsensusProcess(ctx, valAddr, fxstakingtypes.ProcessStart)
var oldPubKey cryptotypes.PubKey
if err := k.cdc.UnmarshalInterfaceJSON(pkBytes, &oldPubKey); err != nil {
return fmt.Errorf("invalid pubkey")
}
oldConsAddr := sdk.ConsAddress(oldPubKey.Address())
// set process end and delete process start
if err := k.SetConsensusProcess(ctx, valAddr, oldPubKey, fxstakingtypes.ProcessEnd); err != nil {
return err
}
// update validator new pk signing info
validator, found := k.GetValidator(ctx, valAddr)
if !found {
// NOTE: validator not found, because it deleted in below case
// 1. validator unbonded and undelegate all (tx)
// 2. unbonding to unbonded and validator share is zero (end block)
return nil
}
newConsAddr, err := validator.GetConsAddr()
if err != nil {
return err
}
oldSigningInfo, found := k.slashingKeeper.GetValidatorSigningInfo(ctx, oldConsAddr)
if !found {
return fmt.Errorf("validator %s not found signing info", oldConsAddr.String())
}
return k.updateSigningInfo(ctx, newConsAddr, oldSigningInfo)
}
func (k Keeper) updateSigningInfo(ctx sdk.Context, consAddr sdk.ConsAddress, signingInfo slashingtypes.ValidatorSigningInfo) error {
newSigningInfo, found := k.slashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
if !found {
return fmt.Errorf("validator %s not found signing info", consAddr.String())
}
// double sign
if newSigningInfo.Tombstoned {
newSigningInfo.IndexOffset = signingInfo.IndexOffset
newSigningInfo.MissedBlocksCounter = signingInfo.MissedBlocksCounter
} else {
newSigningInfo = signingInfo
newSigningInfo.Address = consAddr.String()
}
k.slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, newSigningInfo)
return nil
}
func (k Keeper) ConsensusPubKeyUpdate(ctx sdk.Context) []abci.ValidatorUpdate {
valUpdates := make([]abci.ValidatorUpdate, 0, 50)
k.IteratorConsensusPubKey(ctx, func(valAddr sdk.ValAddress, pkBytes []byte) bool {
// no matter what happens, clear new consensus pubkey
k.RemoveConsensusPubKey(ctx, valAddr)
cacheCtx, commit := ctx.CacheContext()
pkUpdates, err := k.updateConsensusPubKey(cacheCtx, valAddr, pkBytes)
if err == nil {
valUpdates = append(valUpdates, pkUpdates...)
commit()
}
// event
ctx.EventManager().EmitEvent(sdk.NewEvent(
fxstakingtypes.EventTypeEditingConsensusPubKey,
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
sdk.NewAttribute(fxstakingtypes.AttributeResult, strconv.FormatBool(err == nil)),
))
return false
})
return valUpdates
}
func (k Keeper) updateConsensusPubKey(ctx sdk.Context, valAddr sdk.ValAddress, pkBytes []byte) ([]abci.ValidatorUpdate, error) {
validator, found := k.GetValidator(ctx, valAddr)
if !found {
// NOTE: validator not found, because it deleted in below case
// 1. validator unbonded and undelegate all (tx)
// 2. unbonding to unbonded and validator share is zero (end block)
return nil, errors.New("validator not found")
}
oldPubKey, err := validator.ConsPubKey()
if err != nil {
return nil, fmt.Errorf("invalid old consensus pubkey")
}
oldConsAddr := sdk.ConsAddress(oldPubKey.Address())
var newPubKey cryptotypes.PubKey
if err = k.cdc.UnmarshalInterfaceJSON(pkBytes, &newPubKey); err != nil {
return nil, err
}
// validator pubkey and consaddr
if err = k.updateValidator(ctx, validator, newPubKey); err != nil {
return nil, err
}
// pubkey and signing info
if err = k.updateSlashing(ctx, newPubKey, oldConsAddr); err != nil {
return nil, err
}
// validator unjailed and bonded
var valUpdates []abci.ValidatorUpdate
if validator.IsBonded() {
// new validator updates
valUpdates, err = k.updateABCIValidator(ctx, validator, newPubKey, oldPubKey)
if err != nil {
return nil, err
}
}
// set consensus process, next 2 block will delete old consensus pubkey
if err = k.SetConsensusProcess(ctx, valAddr, oldPubKey, fxstakingtypes.ProcessStart); err != nil {
return nil, err
}
return valUpdates, nil
}
func (k Keeper) updateValidator(ctx sdk.Context, validator types.Validator, newPubKey cryptotypes.PubKey) error {
pkAny, err := codectypes.NewAnyWithValue(newPubKey)
if err != nil {
return err
}
validator.ConsensusPubkey = pkAny
k.SetValidator(ctx, validator)
// set new consensus address with validator
k.SetValidatorConsAddr(ctx, sdk.ConsAddress(newPubKey.Address()), validator.GetOperator())
return nil
}
func (k Keeper) updateSlashing(ctx sdk.Context, newPubKey cryptotypes.PubKey, oldConsAddr sdk.ConsAddress) error {
// add new pubkey
if err := k.slashingKeeper.AddPubkey(ctx, newPubKey); err != nil {
return err
}
newConsAddr := sdk.ConsAddress(newPubKey.Address())
// add signing info
signingInfo, found := k.slashingKeeper.GetValidatorSigningInfo(ctx, oldConsAddr)
if !found {
// NOTE: validator create but not bonded enough token
return fmt.Errorf("consensus address %s not found signing info", oldConsAddr.String())
}
signingInfo.Address = newConsAddr.String()
k.slashingKeeper.SetValidatorSigningInfo(ctx, newConsAddr, signingInfo)
return nil
}
func (k Keeper) updateABCIValidator(ctx sdk.Context, val types.Validator, newPk, oldPk cryptotypes.PubKey) ([]abci.ValidatorUpdate, error) {
oldTmProtoPk, err := cryptocodec.ToTmProtoPublicKey(oldPk)
if err != nil {
return nil, err
}
newTmProtoPk, err := cryptocodec.ToTmProtoPublicKey(newPk)
if err != nil {
return nil, err
}
valPower := val.ConsensusPower(k.PowerReduction(ctx))
// set old pk power to 0
oldPkUpdate := abci.ValidatorUpdate{PubKey: oldTmProtoPk, Power: 0}
// add new pk with power
newPkUpdate := abci.ValidatorUpdate{PubKey: newTmProtoPk, Power: valPower}
return []abci.ValidatorUpdate{oldPkUpdate, newPkUpdate}, nil
}