Skip to content

Commit 804799f

Browse files
committed
go/consensus/cometbft/apps/scheduler: Move validator updates to a method
1 parent 84f6f57 commit 804799f

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

.changelog/6431.trivial.md

Whitespace-only changes.

go/consensus/cometbft/apps/scheduler/scheduler.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,39 +317,49 @@ func (app *Application) ExecuteTx(*api.Context, *transaction.Transaction) error
317317
func (app *Application) EndBlock(ctx *api.Context) (types.ResponseEndBlock, error) {
318318
var resp types.ResponseEndBlock
319319

320+
validatorUpdates, err := updateValidators(ctx, resp)
321+
if err != nil {
322+
return resp, err
323+
}
324+
325+
resp.ValidatorUpdates = validatorUpdates
326+
327+
return resp, nil
328+
}
329+
330+
func updateValidators(ctx *api.Context, resp types.ResponseEndBlock) ([]types.ValidatorUpdate, error) {
320331
state := schedulerState.NewMutableState(ctx.State())
321332
pendingValidators, err := state.PendingValidators(ctx)
322333
if err != nil {
323-
return resp, fmt.Errorf("cometbft/scheduler: failed to query pending validators: %w", err)
334+
return nil, fmt.Errorf("cometbft/scheduler: failed to query pending validators: %w", err)
324335
}
325336
if pendingValidators == nil {
326337
// No validator updates to apply.
327-
return resp, nil
338+
return nil, nil
328339
}
329340

330341
currentValidators, err := state.CurrentValidators(ctx)
331342
if err != nil {
332-
return resp, fmt.Errorf("cometbft/scheduler: failed to query current validators: %w", err)
343+
return nil, fmt.Errorf("cometbft/scheduler: failed to query current validators: %w", err)
333344
}
334345

335346
// Clear out the pending validator update.
336347
if err = state.PutPendingValidators(ctx, nil); err != nil {
337-
return resp, fmt.Errorf("cometbft/scheduler: failed to clear validators: %w", err)
348+
return nil, fmt.Errorf("cometbft/scheduler: failed to clear validators: %w", err)
338349
}
339350

340351
// CometBFT expects a vector of ValidatorUpdate that expresses
341352
// the difference between the current validator set (tracked manually
342353
// from InitChain), and the new validator set, which is a huge pain
343354
// in the ass.
344355

345-
resp.ValidatorUpdates = diffValidators(ctx.Logger(), currentValidators, pendingValidators)
356+
validatorUpdates := diffValidators(ctx.Logger(), currentValidators, pendingValidators)
346357

347358
// Stash the updated validator set.
348359
if err = state.PutCurrentValidators(ctx, pendingValidators); err != nil {
349-
return resp, fmt.Errorf("cometbft/scheduler: failed to set validators: %w", err)
360+
return nil, fmt.Errorf("cometbft/scheduler: failed to set validators: %w", err)
350361
}
351-
352-
return resp, nil
362+
return validatorUpdates, nil
353363
}
354364

355365
func diffValidators(logger *logging.Logger, current, pending map[signature.PublicKey]*scheduler.Validator) []types.ValidatorUpdate {

0 commit comments

Comments
 (0)