-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathprocessDeposit.ts
179 lines (162 loc) Β· 6.31 KB
/
processDeposit.ts
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
import {PublicKey, Signature, verify} from "@chainsafe/blst";
import {BLSPubkey, Bytes32, UintNum64, phase0, ssz} from "@lodestar/types";
import {verifyMerkleBranch} from "@lodestar/utils";
import {
DEPOSIT_CONTRACT_TREE_DEPTH,
DOMAIN_DEPOSIT,
EFFECTIVE_BALANCE_INCREMENT,
FAR_FUTURE_EPOCH,
ForkSeq,
MAX_EFFECTIVE_BALANCE,
} from "@lodestar/params";
import {DepositData} from "@lodestar/types/lib/phase0/types.js";
import {DepositRequest} from "@lodestar/types/lib/electra/types.js";
import {BeaconConfig} from "@lodestar/config";
import {ZERO_HASH} from "../constants/index.js";
import {
computeDomain,
computeSigningRoot,
hasCompoundingWithdrawalCredential,
hasEth1WithdrawalCredential,
increaseBalance,
switchToCompoundingValidator,
} from "../util/index.js";
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStateElectra} from "../types.js";
/**
* Process a Deposit operation. Potentially adds a new validator to the registry. Mutates the validators and balances
* trees, pushing contigious values at the end.
*
* PERF: Work depends on number of Deposit per block. On regular networks the average is 0 / block.
*/
export function processDeposit(fork: ForkSeq, state: CachedBeaconStateAllForks, deposit: phase0.Deposit): void {
// verify the merkle branch
if (
!verifyMerkleBranch(
ssz.phase0.DepositData.hashTreeRoot(deposit.data),
deposit.proof,
DEPOSIT_CONTRACT_TREE_DEPTH + 1,
state.eth1DepositIndex,
state.eth1Data.depositRoot
)
) {
throw new Error("Deposit has invalid merkle proof");
}
// deposits must be processed in order
state.eth1DepositIndex += 1;
applyDeposit(fork, state, deposit.data);
}
/**
* Adds a new validator into the registry. Or increase balance if already exist.
* Follows applyDeposit() in consensus spec. Will be used by processDeposit() and processDepositRequest()
*
*/
export function applyDeposit(
fork: ForkSeq,
state: CachedBeaconStateAllForks,
deposit: DepositData | DepositRequest
): void {
const {config, validators, epochCtx} = state;
const {pubkey, withdrawalCredentials, amount} = deposit;
const cachedIndex = epochCtx.getValidatorIndex(pubkey);
if (cachedIndex === undefined || !Number.isSafeInteger(cachedIndex) || cachedIndex >= validators.length) {
if (isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, deposit.signature)) {
addValidatorToRegistry(fork, state, pubkey, withdrawalCredentials, amount);
}
} else {
if (fork < ForkSeq.electra) {
// increase balance by deposit amount right away pre-electra
increaseBalance(state, cachedIndex, amount);
} else if (fork >= ForkSeq.electra) {
const stateElectra = state as CachedBeaconStateElectra;
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index: cachedIndex,
amount: BigInt(amount),
});
stateElectra.pendingBalanceDeposits.push(pendingBalanceDeposit);
if (
hasCompoundingWithdrawalCredential(withdrawalCredentials) &&
hasEth1WithdrawalCredential(validators.getReadonly(cachedIndex).withdrawalCredentials) &&
isValidDepositSignature(config, pubkey, withdrawalCredentials, amount, deposit.signature)
) {
switchToCompoundingValidator(stateElectra, cachedIndex);
}
}
}
}
function addValidatorToRegistry(
fork: ForkSeq,
state: CachedBeaconStateAllForks,
pubkey: BLSPubkey,
withdrawalCredentials: Bytes32,
amount: UintNum64
): void {
const {validators, epochCtx} = state;
// add validator and balance entries
const effectiveBalance =
fork < ForkSeq.electra ? Math.min(amount - (amount % EFFECTIVE_BALANCE_INCREMENT), MAX_EFFECTIVE_BALANCE) : 0;
validators.push(
ssz.phase0.Validator.toViewDU({
pubkey,
withdrawalCredentials,
activationEligibilityEpoch: FAR_FUTURE_EPOCH,
activationEpoch: FAR_FUTURE_EPOCH,
exitEpoch: FAR_FUTURE_EPOCH,
withdrawableEpoch: FAR_FUTURE_EPOCH,
effectiveBalance,
slashed: false,
})
);
const validatorIndex = validators.length - 1;
// TODO Electra: Review this
// Updating here is better than updating at once on epoch transition
// - Simplify genesis fn applyDeposits(): effectiveBalanceIncrements is populated immediately
// - Keep related code together to reduce risk of breaking this cache
// - Should have equal performance since it sets a value in a flat array
epochCtx.effectiveBalanceIncrementsSet(validatorIndex, effectiveBalance);
// now that there is a new validator, update the epoch context with the new pubkey
epochCtx.addPubkey(validatorIndex, pubkey);
// Only after altair:
if (fork >= ForkSeq.altair) {
const stateAltair = state as CachedBeaconStateAltair;
stateAltair.inactivityScores.push(0);
// add participation caches
stateAltair.previousEpochParticipation.push(0);
stateAltair.currentEpochParticipation.push(0);
}
if (fork < ForkSeq.electra) {
state.balances.push(amount);
} else if (fork >= ForkSeq.electra) {
state.balances.push(0);
const stateElectra = state as CachedBeaconStateElectra;
const pendingBalanceDeposit = ssz.electra.PendingBalanceDeposit.toViewDU({
index: validatorIndex,
amount: BigInt(amount),
});
stateElectra.pendingBalanceDeposits.push(pendingBalanceDeposit);
}
}
function isValidDepositSignature(
config: BeaconConfig,
pubkey: Uint8Array,
withdrawalCredentials: Uint8Array,
amount: number,
depositSignature: Uint8Array
): boolean {
// verify the deposit signature (proof of posession) which is not checked by the deposit contract
const depositMessage = {
pubkey,
withdrawalCredentials,
amount,
};
// fork-agnostic domain since deposits are valid across forks
const domain = computeDomain(DOMAIN_DEPOSIT, config.GENESIS_FORK_VERSION, ZERO_HASH);
const signingRoot = computeSigningRoot(ssz.phase0.DepositMessage, depositMessage, domain);
try {
// Pubkeys must be checked for group + inf. This must be done only once when the validator deposit is processed
const publicKey = PublicKey.fromBytes(pubkey, true);
const signature = Signature.fromBytes(depositSignature, true);
return verify(signingRoot, publicKey, signature);
} catch (e) {
return false; // Catch all BLS errors: failed key validation, failed signature validation, invalid signature
}
}