-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathutils.ts
More file actions
263 lines (227 loc) · 10.5 KB
/
utils.ts
File metadata and controls
263 lines (227 loc) · 10.5 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {BitArray, byteArrayEquals} from "@chainsafe/ssz";
import {
FINALIZED_ROOT_DEPTH,
NEXT_SYNC_COMMITTEE_DEPTH,
ForkSeq,
ForkName,
BLOCK_BODY_EXECUTION_PAYLOAD_DEPTH as EXECUTION_PAYLOAD_DEPTH,
BLOCK_BODY_EXECUTION_PAYLOAD_INDEX as EXECUTION_PAYLOAD_INDEX,
NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA,
isForkPostElectra,
FINALIZED_ROOT_DEPTH_ELECTRA,
} from "@lodestar/params";
import {
ssz,
Slot,
LightClientFinalityUpdate,
LightClientHeader,
LightClientOptimisticUpdate,
LightClientUpdate,
BeaconBlockHeader,
SyncCommittee,
isElectraLightClientUpdate,
} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {isValidMerkleBranch, computeEpochAtSlot, computeSyncPeriodAtSlot} from "../utils/index.js";
import {normalizeMerkleBranch} from "../utils/normalizeMerkleBranch.js";
import {LightClientStore} from "./store.js";
export const GENESIS_SLOT = 0;
export const ZERO_HASH = new Uint8Array(32);
export const ZERO_PUBKEY = new Uint8Array(48);
export const ZERO_SYNC_COMMITTEE = ssz.altair.SyncCommittee.defaultValue();
export const ZERO_HEADER = ssz.phase0.BeaconBlockHeader.defaultValue();
export const ZERO_FINALITY_BRANCH = Array.from({length: FINALIZED_ROOT_DEPTH}, () => ZERO_HASH);
/** From https://notes.ethereum.org/@vbuterin/extended_light_client_protocol#Optimistic-head-determining-function */
const SAFETY_THRESHOLD_FACTOR = 2;
export function sumBits(bits: BitArray): number {
return bits.getTrueBitIndexes().length;
}
export function getSafetyThreshold(maxActiveParticipants: number): number {
return Math.floor(maxActiveParticipants / SAFETY_THRESHOLD_FACTOR);
}
export function getZeroSyncCommitteeBranch(fork: ForkName): Uint8Array[] {
const nextSyncCommitteeDepth = isForkPostElectra(fork)
? NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA
: NEXT_SYNC_COMMITTEE_DEPTH;
return Array.from({length: nextSyncCommitteeDepth}, () => ZERO_HASH);
}
export function isSyncCommitteeUpdate(update: LightClientUpdate): boolean {
return (
// Fast return for when constructing full LightClientUpdate from partial updates
update.nextSyncCommitteeBranch !==
getZeroSyncCommitteeBranch(isElectraLightClientUpdate(update) ? ForkName.electra : ForkName.altair) &&
update.nextSyncCommitteeBranch.some((branch) => !byteArrayEquals(branch, ZERO_HASH))
);
}
export function isFinalityUpdate(update: LightClientUpdate): boolean {
return (
// Fast return for when constructing full LightClientUpdate from partial updates
update.finalityBranch !== ZERO_FINALITY_BRANCH &&
update.finalityBranch.some((branch) => !byteArrayEquals(branch, ZERO_HASH))
);
}
export function isZeroedHeader(header: BeaconBlockHeader): boolean {
// Fast return for when constructing full LightClientUpdate from partial updates
return header === ZERO_HEADER || byteArrayEquals(header.bodyRoot, ZERO_HASH);
}
export function isZeroedSyncCommittee(syncCommittee: SyncCommittee): boolean {
// Fast return for when constructing full LightClientUpdate from partial updates
return syncCommittee === ZERO_SYNC_COMMITTEE || byteArrayEquals(syncCommittee.pubkeys[0], ZERO_PUBKEY);
}
export function upgradeLightClientHeader(
config: ChainForkConfig,
targetFork: ForkName,
header: LightClientHeader
): LightClientHeader {
const headerFork = config.getForkName(header.beacon.slot);
if (ForkSeq[headerFork] >= ForkSeq[targetFork]) {
throw Error(`Invalid upgrade request from headerFork=${headerFork} to targetFork=${targetFork}`);
}
// We are modifying the same header object, may be we could create a copy, but its
// not required as of now
const upgradedHeader = header;
const startUpgradeFromFork = Object.values(ForkName)[ForkSeq[headerFork] + 1];
switch (startUpgradeFromFork) {
default:
throw Error(
`Invalid startUpgradeFromFork=${startUpgradeFromFork} for headerFork=${headerFork} in upgradeLightClientHeader to targetFork=${targetFork}`
);
case ForkName.altair:
case ForkName.bellatrix:
// Break if no further upgradation is required else fall through
if (ForkSeq[targetFork] <= ForkSeq.bellatrix) break;
// eslint-disable-next-line no-fallthrough
case ForkName.capella:
(upgradedHeader as LightClientHeader<ForkName.capella>).execution =
ssz.capella.LightClientHeader.fields.execution.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.capella>).executionBranch =
ssz.capella.LightClientHeader.fields.executionBranch.defaultValue();
// Break if no further upgradation is required else fall through
if (ForkSeq[targetFork] <= ForkSeq.capella) break;
// eslint-disable-next-line no-fallthrough
case ForkName.deneb:
(upgradedHeader as LightClientHeader<ForkName.deneb>).execution.blobGasUsed =
ssz.deneb.LightClientHeader.fields.execution.fields.blobGasUsed.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.deneb>).execution.excessBlobGas =
ssz.deneb.LightClientHeader.fields.execution.fields.excessBlobGas.defaultValue();
// Break if no further upgradation is required else fall through
if (ForkSeq[targetFork] <= ForkSeq.deneb) break;
// eslint-disable-next-line no-fallthrough
case ForkName.electra:
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.depositRequestsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.depositRequestsRoot.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.withdrawalRequestsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.withdrawalRequestsRoot.defaultValue();
(upgradedHeader as LightClientHeader<ForkName.electra>).execution.consolidationRequestsRoot =
ssz.electra.LightClientHeader.fields.execution.fields.consolidationRequestsRoot.defaultValue();
// Break if no further upgrades is required else fall through
if (ForkSeq[targetFork] <= ForkSeq.electra) break;
}
return upgradedHeader;
}
export function isValidLightClientHeader(config: ChainForkConfig, header: LightClientHeader): boolean {
const epoch = computeEpochAtSlot(header.beacon.slot);
if (epoch < config.CAPELLA_FORK_EPOCH) {
return (
((header as LightClientHeader<ForkName.capella>).execution === undefined ||
ssz.capella.ExecutionPayloadHeader.equals(
(header as LightClientHeader<ForkName.capella>).execution,
ssz.capella.LightClientHeader.fields.execution.defaultValue()
)) &&
((header as LightClientHeader<ForkName.capella>).executionBranch === undefined ||
ssz.capella.LightClientHeader.fields.executionBranch.equals(
ssz.capella.LightClientHeader.fields.executionBranch.defaultValue(),
(header as LightClientHeader<ForkName.capella>).executionBranch
))
);
}
if (epoch < config.DENEB_FORK_EPOCH) {
if (
((header as LightClientHeader<ForkName.deneb>).execution.blobGasUsed &&
(header as LightClientHeader<ForkName.deneb>).execution.blobGasUsed !== BigInt(0)) ||
((header as LightClientHeader<ForkName.deneb>).execution.excessBlobGas &&
(header as LightClientHeader<ForkName.deneb>).execution.excessBlobGas !== BigInt(0))
) {
return false;
}
}
if (epoch < config.ELECTRA_FORK_EPOCH) {
if (
(header as LightClientHeader<ForkName.electra>).execution.depositRequestsRoot !== undefined ||
(header as LightClientHeader<ForkName.electra>).execution.withdrawalRequestsRoot !== undefined ||
(header as LightClientHeader<ForkName.electra>).execution.consolidationRequestsRoot !== undefined
) {
return false;
}
}
return isValidMerkleBranch(
config
.getExecutionForkTypes(header.beacon.slot)
.ExecutionPayloadHeader.hashTreeRoot((header as LightClientHeader<ForkName.capella>).execution),
(header as LightClientHeader<ForkName.capella>).executionBranch,
EXECUTION_PAYLOAD_DEPTH,
EXECUTION_PAYLOAD_INDEX,
header.beacon.bodyRoot
);
}
export function upgradeLightClientUpdate(
config: ChainForkConfig,
targetFork: ForkName,
update: LightClientUpdate
): LightClientUpdate {
update.attestedHeader = upgradeLightClientHeader(config, targetFork, update.attestedHeader);
update.finalizedHeader = upgradeLightClientHeader(config, targetFork, update.finalizedHeader);
update.nextSyncCommitteeBranch = normalizeMerkleBranch(
update.nextSyncCommitteeBranch,
isForkPostElectra(targetFork) ? NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA : NEXT_SYNC_COMMITTEE_DEPTH
);
update.finalityBranch = normalizeMerkleBranch(
update.finalityBranch,
isForkPostElectra(targetFork) ? FINALIZED_ROOT_DEPTH_ELECTRA : FINALIZED_ROOT_DEPTH
);
return update;
}
export function upgradeLightClientFinalityUpdate(
config: ChainForkConfig,
targetFork: ForkName,
finalityUpdate: LightClientFinalityUpdate
): LightClientFinalityUpdate {
finalityUpdate.attestedHeader = upgradeLightClientHeader(config, targetFork, finalityUpdate.attestedHeader);
finalityUpdate.finalizedHeader = upgradeLightClientHeader(config, targetFork, finalityUpdate.finalizedHeader);
finalityUpdate.finalityBranch = normalizeMerkleBranch(
finalityUpdate.finalityBranch,
isForkPostElectra(targetFork) ? FINALIZED_ROOT_DEPTH_ELECTRA : FINALIZED_ROOT_DEPTH
);
return finalityUpdate;
}
export function upgradeLightClientOptimisticUpdate(
config: ChainForkConfig,
targetFork: ForkName,
optimisticUpdate: LightClientOptimisticUpdate
): LightClientOptimisticUpdate {
optimisticUpdate.attestedHeader = upgradeLightClientHeader(config, targetFork, optimisticUpdate.attestedHeader);
return optimisticUpdate;
}
/**
* Currently this upgradation is not required because all processing is done based on the
* summary that the store generates and maintains. In case store needs to be saved to disk,
* this could be required depending on the format the store is saved to the disk
*/
export function upgradeLightClientStore(
config: ChainForkConfig,
targetFork: ForkName,
store: LightClientStore,
signatureSlot: Slot
): LightClientStore {
const updateSignaturePeriod = computeSyncPeriodAtSlot(signatureSlot);
const bestValidUpdate = store.bestValidUpdates.get(updateSignaturePeriod);
if (bestValidUpdate) {
store.bestValidUpdates.set(updateSignaturePeriod, {
update: upgradeLightClientUpdate(config, targetFork, bestValidUpdate.update),
summary: bestValidUpdate.summary,
});
}
store.finalizedHeader = upgradeLightClientHeader(config, targetFork, store.finalizedHeader);
store.optimisticHeader = upgradeLightClientHeader(config, targetFork, store.optimisticHeader);
return store;
}