-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathprocessPendingConsolidations.ts
More file actions
49 lines (43 loc) · 1.86 KB
/
processPendingConsolidations.ts
File metadata and controls
49 lines (43 loc) · 1.86 KB
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
import {CachedBeaconStateElectra, EpochTransitionCache} from "../types.js";
import {decreaseBalance, increaseBalance} from "../util/balance.js";
import {getActiveBalance} from "../util/validator.js";
/**
* Starting from Electra:
* Process every `pendingConsolidation` in `state.pendingConsolidations`.
* Churn limit was applied when enqueueing so we don't care about the limit here
* However we only process consolidations up to current epoch
*
* For each valid `pendingConsolidation`, update withdrawal credential of target
* validator to compounding, decrease balance of source validator and increase balance
* of target validator.
*
* Dequeue all processed consolidations from `state.pendingConsolidation`
*
*/
export function processPendingConsolidations(state: CachedBeaconStateElectra, cache: EpochTransitionCache): void {
const nextEpoch = state.epochCtx.epoch + 1;
let nextPendingConsolidation = 0;
const validators = state.validators;
const cachedBalances = cache.balances;
for (const pendingConsolidation of state.pendingConsolidations.getAllReadonly()) {
const {sourceIndex, targetIndex} = pendingConsolidation;
const sourceValidator = validators.getReadonly(sourceIndex);
if (sourceValidator.slashed) {
nextPendingConsolidation++;
continue;
}
if (sourceValidator.withdrawableEpoch > nextEpoch) {
break;
}
// Move active balance to target. Excess balance is withdrawable.
const activeBalance = getActiveBalance(state, sourceIndex);
decreaseBalance(state, sourceIndex, activeBalance);
increaseBalance(state, targetIndex, activeBalance);
if (cachedBalances) {
cachedBalances[sourceIndex] -= activeBalance;
cachedBalances[targetIndex] += activeBalance;
}
nextPendingConsolidation++;
}
state.pendingConsolidations = state.pendingConsolidations.sliceFrom(nextPendingConsolidation);
}