-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathelectra.ts
More file actions
56 lines (47 loc) · 2.36 KB
/
electra.ts
File metadata and controls
56 lines (47 loc) · 2.36 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
50
51
52
53
54
55
56
import {COMPOUNDING_WITHDRAWAL_PREFIX, FAR_FUTURE_EPOCH, MIN_ACTIVATION_BALANCE} from "@lodestar/params";
import {ValidatorIndex, ssz} from "@lodestar/types";
import {CachedBeaconStateElectra} from "../types.js";
import {hasEth1WithdrawalCredential} from "./capella.js";
export function hasCompoundingWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
return withdrawalCredentials[0] === COMPOUNDING_WITHDRAWAL_PREFIX;
}
export function hasExecutionWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
return (
hasCompoundingWithdrawalCredential(withdrawalCredentials) || hasEth1WithdrawalCredential(withdrawalCredentials)
);
}
export function switchToCompoundingValidator(state: CachedBeaconStateElectra, index: ValidatorIndex): void {
const validator = state.validators.get(index);
// directly modifying the byte leads to ssz missing the modification resulting into
// wrong root compute, although slicing can be avoided but anyway this is not going
// to be a hot path so its better to clean slice and avoid side effects
const newWithdrawalCredentials = validator.withdrawalCredentials.slice();
newWithdrawalCredentials[0] = COMPOUNDING_WITHDRAWAL_PREFIX;
validator.withdrawalCredentials = newWithdrawalCredentials;
queueExcessActiveBalance(state, index);
}
export function queueExcessActiveBalance(state: CachedBeaconStateElectra, index: ValidatorIndex): void {
const balance = state.balances.get(index);
if (balance > MIN_ACTIVATION_BALANCE) {
const excessBalance = balance - MIN_ACTIVATION_BALANCE;
state.balances.set(index, MIN_ACTIVATION_BALANCE);
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index,
amount: BigInt(excessBalance),
});
state.pendingBalanceDeposits.push(pendingBalanceDeposit);
}
}
export function queueEntireBalanceAndResetValidator(state: CachedBeaconStateElectra, index: ValidatorIndex): void {
const balance = state.balances.get(index);
state.balances.set(index, 0);
const validator = state.validators.get(index);
validator.effectiveBalance = 0;
state.epochCtx.effectiveBalanceIncrementsSet(index, 0);
validator.activationEligibilityEpoch = FAR_FUTURE_EPOCH;
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index,
amount: BigInt(balance),
});
state.pendingBalanceDeposits.push(pendingBalanceDeposit);
}