Skip to content

Commit ea817ac

Browse files
committed
add tests for rotaiton functinos
1 parent 323350d commit ea817ac

3 files changed

Lines changed: 424 additions & 22 deletions

File tree

x/staking/keeper/rotation.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,31 +192,38 @@ func (k Keeper) ApplyConsKeyRotation(ctx context.Context, valAddr sdk.ValAddress
192192
// PendingConsKeyRotationUpdate stores the rotation metadata needed to align
193193
// staking-side validator updates with CometBFT's delayed key-rotation view.
194194
type PendingConsKeyRotationUpdate struct {
195-
oldPubKey cryptotypes.PubKey
196-
newPubKey cryptotypes.PubKey
197-
emitHeight int64
198-
lastPower int64
195+
// OldPubKey is the consensus key currently stored in SDK validator state.
196+
OldPubKey cryptotypes.PubKey
197+
198+
// NewPubKey is the pending consensus key CometBFT should track for the
199+
// validator once the rotation update is emitted.
200+
NewPubKey cryptotypes.PubKey
201+
202+
// EmitHeight is the EndBlock height that should emit the old@0,new@power
203+
// rotation pair.
204+
EmitHeight int64
205+
206+
// LastPower is the validator power from the last Comet-visible validator
207+
// set, keyed by operator address.
208+
LastPower int64
199209
}
200210

201211
// oldAddr returns the Comet validator address for the pre-rotation key.
202212
func (r PendingConsKeyRotationUpdate) oldAddr() string {
203-
return string(r.oldPubKey.Address())
213+
return string(r.OldPubKey.Address())
204214
}
205215

206216
// shouldEmitPowerUpdates returns whether the rotation should emit
207217
// old@0,new@power at height.
208218
func (r PendingConsKeyRotationUpdate) shouldEmitPowerUpdates(height int64) bool {
209-
return r.emitHeight == height && r.lastPower > 0
219+
return r.EmitHeight == height && r.LastPower > 0
210220
}
211221

212222
// PendingConsKeyRotationUpdates returns rotation metadata for in-flight
213223
// rotations whose CometBFT update has been or should be emitted by the current
214224
// EndBlock. It must be called after ApplyConsKeyRotations so matured entries
215225
// have already been drained.
216-
func (k Keeper) PendingConsKeyRotationUpdates(
217-
ctx context.Context,
218-
last map[string]int64,
219-
) (updates []PendingConsKeyRotationUpdate, err error) {
226+
func (k Keeper) PendingConsKeyRotationUpdates(ctx context.Context, last map[string]int64) (updates []PendingConsKeyRotationUpdate, err error) {
220227
store := k.storeService.OpenKVStore(ctx)
221228
currentHeight := sdk.UnwrapSDKContext(ctx).BlockHeight()
222229

@@ -262,19 +269,19 @@ func (k Keeper) PendingConsKeyRotationUpdates(
262269

263270
lastPower := last[string(valAddr)]
264271
updates = append(updates, PendingConsKeyRotationUpdate{
265-
oldPubKey: oldPubKey,
266-
newPubKey: newPubKey,
267-
emitHeight: emitHeight,
268-
lastPower: lastPower,
272+
OldPubKey: oldPubKey,
273+
NewPubKey: newPubKey,
274+
EmitHeight: emitHeight,
275+
LastPower: lastPower,
269276
})
270277
}
271278
return updates, nil
272279
}
273280

274-
// rewriteValidatorUpdatesForConsKeyRotations rewrites validator updates that
281+
// ProcessValidatorUpdatesForConsKeyRotations rewrites validator updates that
275282
// reference old consensus keys so the returned batch matches CometBFT's
276283
// key-rotation timeline.
277-
func (k Keeper) rewriteValidatorUpdatesForConsKeyRotations(
284+
func (k Keeper) ProcessValidatorUpdatesForConsKeyRotations(
278285
ctx context.Context,
279286
rotations []PendingConsKeyRotationUpdate,
280287
updates []abci.ValidatorUpdate,
@@ -319,14 +326,14 @@ func (k Keeper) rewriteValidatorUpdatesForConsKeyRotations(
319326
// we should emit power updates for this validator at this height
320327

321328
// set the key they are rotating away from to 0
322-
oldUpdate, err := validatorUpdateForPubKey(rotation.oldPubKey, 0)
329+
oldUpdate, err := validatorUpdateForPubKey(rotation.OldPubKey, 0)
323330
if err != nil {
324331
return nil, err
325332
}
326333

327334
// and set the new key they are rotating to to the validators last seen
328335
// power
329-
newUpdate, err := validatorUpdateForPubKey(rotation.newPubKey, rotation.lastPower)
336+
newUpdate, err := validatorUpdateForPubKey(rotation.NewPubKey, rotation.LastPower)
330337
if err != nil {
331338
return nil, err
332339
}
@@ -352,15 +359,15 @@ func rotatedValidatorUpdates(
352359
case update.Power == 0:
353360
// The rotation was already emitted in an earlier EndBlock, so Comet now
354361
// tracks the new key even though SDK state still stores the old key.
355-
newUpdate, err := validatorUpdateForPubKey(rotation.newPubKey, 0)
362+
newUpdate, err := validatorUpdateForPubKey(rotation.NewPubKey, 0)
356363
if err != nil {
357364
return nil, err
358365
}
359366
return []abci.ValidatorUpdate{newUpdate}, nil
360367
case rotation.shouldEmitPowerUpdates(currentHeight):
361368
// Normal staking emitted old@power at the same height the rotation pair
362369
// is due. Convert it into the Comet key swap old@0, new@power.
363-
newUpdate, err := validatorUpdateForPubKey(rotation.newPubKey, update.Power)
370+
newUpdate, err := validatorUpdateForPubKey(rotation.NewPubKey, update.Power)
364371
if err != nil {
365372
return nil, err
366373
}
@@ -371,7 +378,7 @@ func rotatedValidatorUpdates(
371378
default:
372379
// The key swap was already emitted to Comet, but the SDK-side key swap
373380
// has not reached applyHeight. Translate old@power to new@power.
374-
newUpdate, err := validatorUpdateForPubKey(rotation.newPubKey, update.Power)
381+
newUpdate, err := validatorUpdateForPubKey(rotation.NewPubKey, update.Power)
375382
if err != nil {
376383
return nil, err
377384
}

0 commit comments

Comments
 (0)