@lodestar/light-client 1.35.0-dev.fcf8d024ea → 1.35.0-dev.feed916580

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/package.json +18 -16
  2. package/lib/events.d.ts.map +0 -1
  3. package/lib/index.d.ts.map +0 -1
  4. package/lib/spec/index.d.ts.map +0 -1
  5. package/lib/spec/isBetterUpdate.d.ts.map +0 -1
  6. package/lib/spec/processLightClientUpdate.d.ts.map +0 -1
  7. package/lib/spec/store.d.ts.map +0 -1
  8. package/lib/spec/utils.d.ts.map +0 -1
  9. package/lib/spec/validateLightClientBootstrap.d.ts.map +0 -1
  10. package/lib/spec/validateLightClientUpdate.d.ts.map +0 -1
  11. package/lib/transport/index.d.ts.map +0 -1
  12. package/lib/transport/interface.d.ts.map +0 -1
  13. package/lib/transport/rest.d.ts.map +0 -1
  14. package/lib/transport.d.ts.map +0 -1
  15. package/lib/types.d.ts.map +0 -1
  16. package/lib/utils/api.d.ts.map +0 -1
  17. package/lib/utils/chunkify.d.ts.map +0 -1
  18. package/lib/utils/clock.d.ts.map +0 -1
  19. package/lib/utils/domain.d.ts.map +0 -1
  20. package/lib/utils/index.d.ts.map +0 -1
  21. package/lib/utils/logger.d.ts.map +0 -1
  22. package/lib/utils/map.d.ts.map +0 -1
  23. package/lib/utils/normalizeMerkleBranch.d.ts.map +0 -1
  24. package/lib/utils/update.d.ts.map +0 -1
  25. package/lib/utils/utils.d.ts.map +0 -1
  26. package/lib/utils/verifyMerkleBranch.d.ts.map +0 -1
  27. package/lib/utils.d.ts.map +0 -1
  28. package/lib/validation.d.ts.map +0 -1
  29. package/src/events.ts +0 -17
  30. package/src/index.ts +0 -340
  31. package/src/spec/index.ts +0 -71
  32. package/src/spec/isBetterUpdate.ts +0 -94
  33. package/src/spec/processLightClientUpdate.ts +0 -119
  34. package/src/spec/store.ts +0 -105
  35. package/src/spec/utils.ts +0 -266
  36. package/src/spec/validateLightClientBootstrap.ts +0 -41
  37. package/src/spec/validateLightClientUpdate.ts +0 -154
  38. package/src/transport/index.ts +0 -2
  39. package/src/transport/interface.ts +0 -37
  40. package/src/transport/rest.ts +0 -89
  41. package/src/transport.ts +0 -2
  42. package/src/types.ts +0 -20
  43. package/src/utils/api.ts +0 -19
  44. package/src/utils/chunkify.ts +0 -26
  45. package/src/utils/clock.ts +0 -45
  46. package/src/utils/domain.ts +0 -44
  47. package/src/utils/index.ts +0 -8
  48. package/src/utils/logger.ts +0 -29
  49. package/src/utils/map.ts +0 -20
  50. package/src/utils/normalizeMerkleBranch.ts +0 -15
  51. package/src/utils/update.ts +0 -30
  52. package/src/utils/utils.ts +0 -95
  53. package/src/utils/verifyMerkleBranch.ts +0 -29
  54. package/src/utils.ts +0 -2
  55. package/src/validation.ts +0 -201
@@ -1,94 +0,0 @@
1
- import {SYNC_COMMITTEE_SIZE} from "@lodestar/params";
2
- import {LightClientUpdate, Slot} from "@lodestar/types";
3
- import {computeSyncPeriodAtSlot} from "../utils/index.js";
4
- import {isFinalityUpdate, isSyncCommitteeUpdate, sumBits} from "./utils.js";
5
-
6
- /**
7
- * Wrapper type for `isBetterUpdate()` so we can apply its logic without requiring the full LightClientUpdate type.
8
- */
9
- export type LightClientUpdateSummary = {
10
- activeParticipants: number;
11
- attestedHeaderSlot: Slot;
12
- signatureSlot: Slot;
13
- finalizedHeaderSlot: Slot;
14
- /** `if update.next_sync_committee_branch != [Bytes32() for _ in range(floorlog2(NEXT_SYNC_COMMITTEE_INDEX))]` */
15
- isSyncCommitteeUpdate: boolean;
16
- /** `if update.finality_branch != [Bytes32() for _ in range(floorlog2(FINALIZED_ROOT_INDEX))]` */
17
- isFinalityUpdate: boolean;
18
- };
19
-
20
- /**
21
- * Returns the update with more bits. On ties, prevUpdate is the better
22
- *
23
- * https://github.com/ethereum/consensus-specs/blob/be3c774069e16e89145660be511c1b183056017e/specs/altair/light-client/sync-protocol.md#is_better_update
24
- */
25
- export function isBetterUpdate(newUpdate: LightClientUpdateSummary, oldUpdate: LightClientUpdateSummary): boolean {
26
- // Compare supermajority (> 2/3) sync committee participation
27
- const newNumActiveParticipants = newUpdate.activeParticipants;
28
- const oldNumActiveParticipants = oldUpdate.activeParticipants;
29
- const newHasSupermajority = newNumActiveParticipants * 3 >= SYNC_COMMITTEE_SIZE * 2;
30
- const oldHasSupermajority = oldNumActiveParticipants * 3 >= SYNC_COMMITTEE_SIZE * 2;
31
- if (newHasSupermajority !== oldHasSupermajority) {
32
- return newHasSupermajority;
33
- }
34
- if (!newHasSupermajority && newNumActiveParticipants !== oldNumActiveParticipants) {
35
- return newNumActiveParticipants > oldNumActiveParticipants;
36
- }
37
-
38
- // Compare presence of relevant sync committee
39
- const newHasRelevantSyncCommittee =
40
- newUpdate.isSyncCommitteeUpdate &&
41
- computeSyncPeriodAtSlot(newUpdate.attestedHeaderSlot) === computeSyncPeriodAtSlot(newUpdate.signatureSlot);
42
- const oldHasRelevantSyncCommittee =
43
- oldUpdate.isSyncCommitteeUpdate &&
44
- computeSyncPeriodAtSlot(oldUpdate.attestedHeaderSlot) === computeSyncPeriodAtSlot(oldUpdate.signatureSlot);
45
- if (newHasRelevantSyncCommittee !== oldHasRelevantSyncCommittee) {
46
- return newHasRelevantSyncCommittee;
47
- }
48
-
49
- // Compare indication of any finality
50
- const newHasFinality = newUpdate.isFinalityUpdate;
51
- const oldHasFinality = oldUpdate.isFinalityUpdate;
52
- if (newHasFinality !== oldHasFinality) {
53
- return newHasFinality;
54
- }
55
-
56
- // Compare sync committee finality
57
- if (newHasFinality) {
58
- const newHasSyncCommitteeFinality =
59
- computeSyncPeriodAtSlot(newUpdate.finalizedHeaderSlot) === computeSyncPeriodAtSlot(newUpdate.attestedHeaderSlot);
60
- const oldHasSyncCommitteeFinality =
61
- computeSyncPeriodAtSlot(oldUpdate.finalizedHeaderSlot) === computeSyncPeriodAtSlot(oldUpdate.attestedHeaderSlot);
62
- if (newHasSyncCommitteeFinality !== oldHasSyncCommitteeFinality) {
63
- return newHasSyncCommitteeFinality;
64
- }
65
- }
66
-
67
- // Tiebreaker 1: Sync committee participation beyond supermajority
68
- if (newNumActiveParticipants !== oldNumActiveParticipants) {
69
- return newNumActiveParticipants > oldNumActiveParticipants;
70
- }
71
-
72
- // Tiebreaker 2: Prefer older data (fewer changes to best)
73
- if (newUpdate.attestedHeaderSlot !== oldUpdate.attestedHeaderSlot) {
74
- return newUpdate.attestedHeaderSlot < oldUpdate.attestedHeaderSlot;
75
- }
76
- return newUpdate.signatureSlot < oldUpdate.signatureSlot;
77
- }
78
-
79
- export function isSafeLightClientUpdate(update: LightClientUpdateSummary): boolean {
80
- return (
81
- update.activeParticipants * 3 >= SYNC_COMMITTEE_SIZE * 2 && update.isFinalityUpdate && update.isSyncCommitteeUpdate
82
- );
83
- }
84
-
85
- export function toLightClientUpdateSummary(update: LightClientUpdate): LightClientUpdateSummary {
86
- return {
87
- activeParticipants: sumBits(update.syncAggregate.syncCommitteeBits),
88
- attestedHeaderSlot: update.attestedHeader.beacon.slot,
89
- signatureSlot: update.signatureSlot,
90
- finalizedHeaderSlot: update.finalizedHeader.beacon.slot,
91
- isSyncCommitteeUpdate: isSyncCommitteeUpdate(update),
92
- isFinalityUpdate: isFinalityUpdate(update),
93
- };
94
- }
@@ -1,119 +0,0 @@
1
- import {ChainForkConfig} from "@lodestar/config";
2
- import {SYNC_COMMITTEE_SIZE} from "@lodestar/params";
3
- import {LightClientUpdate, Slot, SyncPeriod} from "@lodestar/types";
4
- import {pruneSetToMax} from "@lodestar/utils";
5
- import {computeSyncPeriodAtSlot, deserializeSyncCommittee, sumBits} from "../utils/index.js";
6
- import {LightClientUpdateSummary, isBetterUpdate, toLightClientUpdateSummary} from "./isBetterUpdate.js";
7
- import {ILightClientStore, MAX_SYNC_PERIODS_CACHE, SyncCommitteeFast} from "./store.js";
8
- import {getSafetyThreshold, isSyncCommitteeUpdate} from "./utils.js";
9
- import {validateLightClientUpdate} from "./validateLightClientUpdate.js";
10
-
11
- export interface ProcessUpdateOpts {
12
- allowForcedUpdates?: boolean;
13
- updateHeadersOnForcedUpdate?: boolean;
14
- }
15
-
16
- export function processLightClientUpdate(
17
- config: ChainForkConfig,
18
- store: ILightClientStore,
19
- currentSlot: Slot,
20
- opts: ProcessUpdateOpts,
21
- update: LightClientUpdate
22
- ): void {
23
- if (update.signatureSlot > currentSlot) {
24
- throw Error(`update slot ${update.signatureSlot} must not be in the future, current slot ${currentSlot}`);
25
- }
26
-
27
- const updateSignaturePeriod = computeSyncPeriodAtSlot(update.signatureSlot);
28
- // TODO: Consider attempting to retrieve LightClientUpdate from transport if missing
29
- // Note: store.getSyncCommitteeAtPeriod() may advance store
30
- const syncCommittee = getSyncCommitteeAtPeriod(store, updateSignaturePeriod, opts);
31
-
32
- validateLightClientUpdate(config, store, update, syncCommittee);
33
-
34
- // Track the maximum number of active participants in the committee signatures
35
- const syncCommitteeTrueBits = sumBits(update.syncAggregate.syncCommitteeBits);
36
- store.setActiveParticipants(updateSignaturePeriod, syncCommitteeTrueBits);
37
-
38
- // Update the optimistic header
39
- if (
40
- syncCommitteeTrueBits > getSafetyThreshold(store.getMaxActiveParticipants(updateSignaturePeriod)) &&
41
- update.attestedHeader.beacon.slot > store.optimisticHeader.beacon.slot
42
- ) {
43
- store.optimisticHeader = update.attestedHeader;
44
- }
45
-
46
- // Update finalized header
47
- if (
48
- syncCommitteeTrueBits * 3 >= SYNC_COMMITTEE_SIZE * 2 &&
49
- update.finalizedHeader.beacon.slot > store.finalizedHeader.beacon.slot
50
- ) {
51
- store.finalizedHeader = update.finalizedHeader;
52
- if (store.finalizedHeader.beacon.slot > store.optimisticHeader.beacon.slot) {
53
- store.optimisticHeader = store.finalizedHeader;
54
- }
55
- }
56
-
57
- if (isSyncCommitteeUpdate(update)) {
58
- // Update the best update in case we have to force-update to it if the timeout elapses
59
- const bestValidUpdate = store.bestValidUpdates.get(updateSignaturePeriod);
60
- const updateSummary = toLightClientUpdateSummary(update);
61
- if (!bestValidUpdate || isBetterUpdate(updateSummary, bestValidUpdate.summary)) {
62
- store.bestValidUpdates.set(updateSignaturePeriod, {update, summary: updateSummary});
63
- pruneSetToMax(store.bestValidUpdates, MAX_SYNC_PERIODS_CACHE);
64
- }
65
-
66
- // Note: defer update next sync committee to a future getSyncCommitteeAtPeriod() call
67
- }
68
- }
69
-
70
- export function getSyncCommitteeAtPeriod(
71
- store: ILightClientStore,
72
- period: SyncPeriod,
73
- opts: ProcessUpdateOpts
74
- ): SyncCommitteeFast {
75
- const syncCommittee = store.syncCommittees.get(period);
76
- if (syncCommittee) {
77
- return syncCommittee;
78
- }
79
-
80
- const bestValidUpdate = store.bestValidUpdates.get(period - 1);
81
- if (bestValidUpdate && (isSafeLightClientUpdate(bestValidUpdate.summary) || opts.allowForcedUpdates)) {
82
- const {update} = bestValidUpdate;
83
- const syncCommittee = deserializeSyncCommittee(update.nextSyncCommittee);
84
- store.syncCommittees.set(period, syncCommittee);
85
- pruneSetToMax(store.syncCommittees, MAX_SYNC_PERIODS_CACHE);
86
- store.bestValidUpdates.delete(period - 1);
87
-
88
- if (opts.updateHeadersOnForcedUpdate) {
89
- // From https://github.com/ethereum/consensus-specs/blob/a57e15636013eeba3610ff3ade41781dba1bb0cd/specs/altair/light-client/sync-protocol.md?plain=1#L403
90
- if (update.finalizedHeader.beacon.slot <= store.finalizedHeader.beacon.slot) {
91
- update.finalizedHeader = update.attestedHeader;
92
- }
93
-
94
- // From https://github.com/ethereum/consensus-specs/blob/a57e15636013eeba3610ff3ade41781dba1bb0cd/specs/altair/light-client/sync-protocol.md?plain=1#L374
95
- if (update.finalizedHeader.beacon.slot > store.finalizedHeader.beacon.slot) {
96
- store.finalizedHeader = update.finalizedHeader;
97
- }
98
- if (store.finalizedHeader.beacon.slot > store.optimisticHeader.beacon.slot) {
99
- store.optimisticHeader = store.finalizedHeader;
100
- }
101
- }
102
-
103
- return syncCommittee;
104
- }
105
-
106
- const availableSyncCommittees = Array.from(store.syncCommittees.keys());
107
- const availableBestValidUpdates = Array.from(store.bestValidUpdates.keys());
108
- throw Error(
109
- `No SyncCommittee for period ${period}` +
110
- ` available syncCommittees ${JSON.stringify(availableSyncCommittees)}` +
111
- ` available bestValidUpdates ${JSON.stringify(availableBestValidUpdates)}`
112
- );
113
- }
114
-
115
- export function isSafeLightClientUpdate(update: LightClientUpdateSummary): boolean {
116
- return (
117
- update.activeParticipants * 3 >= SYNC_COMMITTEE_SIZE * 2 && update.isFinalityUpdate && update.isSyncCommitteeUpdate
118
- );
119
- }
package/src/spec/store.ts DELETED
@@ -1,105 +0,0 @@
1
- import type {PublicKey} from "@chainsafe/bls/types";
2
- import {BeaconConfig} from "@lodestar/config";
3
- import {LightClientBootstrap, LightClientHeader, LightClientUpdate, SyncPeriod} from "@lodestar/types";
4
- import {computeSyncPeriodAtSlot, deserializeSyncCommittee} from "../utils/index.js";
5
- import {LightClientUpdateSummary} from "./isBetterUpdate.js";
6
-
7
- export const MAX_SYNC_PERIODS_CACHE = 2;
8
-
9
- export interface ILightClientStore {
10
- readonly config: BeaconConfig;
11
-
12
- /** Map of trusted SyncCommittee to be used for sig validation */
13
- readonly syncCommittees: Map<SyncPeriod, SyncCommitteeFast>;
14
- /** Map of best valid updates */
15
- readonly bestValidUpdates: Map<SyncPeriod, LightClientUpdateWithSummary>;
16
-
17
- getMaxActiveParticipants(period: SyncPeriod): number;
18
- setActiveParticipants(period: SyncPeriod, activeParticipants: number): void;
19
-
20
- // Header that is finalized
21
- finalizedHeader: LightClientHeader;
22
-
23
- // Most recent available reasonably-safe header
24
- optimisticHeader: LightClientHeader;
25
- }
26
-
27
- export interface LightClientStoreEvents {
28
- onSetFinalizedHeader?: (header: LightClientHeader) => void;
29
- onSetOptimisticHeader?: (header: LightClientHeader) => void;
30
- }
31
-
32
- export class LightClientStore implements ILightClientStore {
33
- readonly syncCommittees = new Map<SyncPeriod, SyncCommitteeFast>();
34
- readonly bestValidUpdates = new Map<SyncPeriod, LightClientUpdateWithSummary>();
35
-
36
- private _finalizedHeader: LightClientHeader;
37
- private _optimisticHeader: LightClientHeader;
38
-
39
- private readonly maxActiveParticipants = new Map<SyncPeriod, number>();
40
-
41
- constructor(
42
- readonly config: BeaconConfig,
43
- bootstrap: LightClientBootstrap,
44
- private readonly events: LightClientStoreEvents
45
- ) {
46
- const bootstrapPeriod = computeSyncPeriodAtSlot(bootstrap.header.beacon.slot);
47
- this.syncCommittees.set(bootstrapPeriod, deserializeSyncCommittee(bootstrap.currentSyncCommittee));
48
- this._finalizedHeader = bootstrap.header;
49
- this._optimisticHeader = bootstrap.header;
50
- }
51
-
52
- get finalizedHeader(): LightClientHeader {
53
- return this._finalizedHeader;
54
- }
55
-
56
- set finalizedHeader(value: LightClientHeader) {
57
- this._finalizedHeader = value;
58
- this.events.onSetFinalizedHeader?.(value);
59
- }
60
-
61
- get optimisticHeader(): LightClientHeader {
62
- return this._optimisticHeader;
63
- }
64
-
65
- set optimisticHeader(value: LightClientHeader) {
66
- this._optimisticHeader = value;
67
- this.events.onSetOptimisticHeader?.(value);
68
- }
69
-
70
- getMaxActiveParticipants(period: SyncPeriod): number {
71
- const currMaxParticipants = this.maxActiveParticipants.get(period) ?? 0;
72
- const prevMaxParticipants = this.maxActiveParticipants.get(period - 1) ?? 0;
73
-
74
- return Math.max(currMaxParticipants, prevMaxParticipants);
75
- }
76
-
77
- setActiveParticipants(period: SyncPeriod, activeParticipants: number): void {
78
- const maxActiveParticipants = this.maxActiveParticipants.get(period) ?? 0;
79
- if (activeParticipants > maxActiveParticipants) {
80
- this.maxActiveParticipants.set(period, activeParticipants);
81
- }
82
-
83
- // Prune old entries
84
- for (const key of this.maxActiveParticipants.keys()) {
85
- if (key < period - MAX_SYNC_PERIODS_CACHE) {
86
- this.maxActiveParticipants.delete(key);
87
- }
88
- }
89
- }
90
- }
91
-
92
- export type SyncCommitteeFast = {
93
- pubkeys: PublicKey[];
94
- aggregatePubkey: PublicKey;
95
- };
96
-
97
- export type LightClientUpdateWithSummary = {
98
- update: LightClientUpdate;
99
- summary: LightClientUpdateSummary;
100
- };
101
-
102
- // === storePeriod ? store.currentSyncCommittee : store.nextSyncCommittee;
103
- // if (!syncCommittee) {
104
- // throw Error(`syncCommittee not available for signature period ${updateSignaturePeriod}`);
105
- // }
package/src/spec/utils.ts DELETED
@@ -1,266 +0,0 @@
1
- import {BitArray, byteArrayEquals} from "@chainsafe/ssz";
2
- import {ChainForkConfig} from "@lodestar/config";
3
- import {
4
- BLOCK_BODY_EXECUTION_PAYLOAD_DEPTH as EXECUTION_PAYLOAD_DEPTH,
5
- BLOCK_BODY_EXECUTION_PAYLOAD_INDEX as EXECUTION_PAYLOAD_INDEX,
6
- FINALIZED_ROOT_DEPTH,
7
- FINALIZED_ROOT_DEPTH_ELECTRA,
8
- ForkName,
9
- ForkSeq,
10
- NEXT_SYNC_COMMITTEE_DEPTH,
11
- NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA,
12
- isForkPostElectra,
13
- } from "@lodestar/params";
14
- import {
15
- BeaconBlockHeader,
16
- LightClientFinalityUpdate,
17
- LightClientHeader,
18
- LightClientOptimisticUpdate,
19
- LightClientUpdate,
20
- Slot,
21
- SyncCommittee,
22
- isElectraLightClientUpdate,
23
- ssz,
24
- } from "@lodestar/types";
25
- import {computeEpochAtSlot, computeSyncPeriodAtSlot, isValidMerkleBranch} from "../utils/index.js";
26
- import {normalizeMerkleBranch} from "../utils/normalizeMerkleBranch.js";
27
- import {LightClientStore} from "./store.js";
28
-
29
- export const GENESIS_SLOT = 0;
30
- export const ZERO_HASH = new Uint8Array(32);
31
- export const ZERO_PUBKEY = new Uint8Array(48);
32
- export const ZERO_SYNC_COMMITTEE = ssz.altair.SyncCommittee.defaultValue();
33
- export const ZERO_HEADER = ssz.phase0.BeaconBlockHeader.defaultValue();
34
- /** From https://notes.ethereum.org/@vbuterin/extended_light_client_protocol#Optimistic-head-determining-function */
35
- const SAFETY_THRESHOLD_FACTOR = 2;
36
-
37
- export function sumBits(bits: BitArray): number {
38
- return bits.getTrueBitIndexes().length;
39
- }
40
-
41
- export function getSafetyThreshold(maxActiveParticipants: number): number {
42
- return Math.floor(maxActiveParticipants / SAFETY_THRESHOLD_FACTOR);
43
- }
44
-
45
- export function getZeroSyncCommitteeBranch(fork: ForkName): Uint8Array[] {
46
- const nextSyncCommitteeDepth = isForkPostElectra(fork)
47
- ? NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA
48
- : NEXT_SYNC_COMMITTEE_DEPTH;
49
-
50
- return Array.from({length: nextSyncCommitteeDepth}, () => ZERO_HASH);
51
- }
52
-
53
- export function getZeroFinalityBranch(fork: ForkName): Uint8Array[] {
54
- const finalizedRootDepth = isForkPostElectra(fork) ? FINALIZED_ROOT_DEPTH_ELECTRA : FINALIZED_ROOT_DEPTH;
55
-
56
- return Array.from({length: finalizedRootDepth}, () => ZERO_HASH);
57
- }
58
-
59
- export function isSyncCommitteeUpdate(update: LightClientUpdate): boolean {
60
- return (
61
- // Fast return for when constructing full LightClientUpdate from partial updates
62
- update.nextSyncCommitteeBranch !==
63
- getZeroSyncCommitteeBranch(isElectraLightClientUpdate(update) ? ForkName.electra : ForkName.altair) &&
64
- update.nextSyncCommitteeBranch.some((branch) => !byteArrayEquals(branch, ZERO_HASH))
65
- );
66
- }
67
-
68
- export function isFinalityUpdate(update: LightClientUpdate): boolean {
69
- return (
70
- // Fast return for when constructing full LightClientUpdate from partial updates
71
- update.finalityBranch !==
72
- getZeroFinalityBranch(isElectraLightClientUpdate(update) ? ForkName.electra : ForkName.altair) &&
73
- update.finalityBranch.some((branch) => !byteArrayEquals(branch, ZERO_HASH))
74
- );
75
- }
76
-
77
- export function isZeroedHeader(header: BeaconBlockHeader): boolean {
78
- // Fast return for when constructing full LightClientUpdate from partial updates
79
- return header === ZERO_HEADER || byteArrayEquals(header.bodyRoot, ZERO_HASH);
80
- }
81
-
82
- export function isZeroedSyncCommittee(syncCommittee: SyncCommittee): boolean {
83
- // Fast return for when constructing full LightClientUpdate from partial updates
84
- return syncCommittee === ZERO_SYNC_COMMITTEE || byteArrayEquals(syncCommittee.pubkeys[0], ZERO_PUBKEY);
85
- }
86
-
87
- export function upgradeLightClientHeader(
88
- config: ChainForkConfig,
89
- targetFork: ForkName,
90
- header: LightClientHeader
91
- ): LightClientHeader {
92
- const headerFork = config.getForkName(header.beacon.slot);
93
- if (ForkSeq[headerFork] >= ForkSeq[targetFork]) {
94
- throw Error(`Invalid upgrade request from headerFork=${headerFork} to targetFork=${targetFork}`);
95
- }
96
-
97
- // We are modifying the same header object, may be we could create a copy, but its
98
- // not required as of now
99
- const upgradedHeader = header;
100
- const startUpgradeFromFork = Object.values(ForkName)[ForkSeq[headerFork] + 1];
101
-
102
- switch (startUpgradeFromFork) {
103
- // biome-ignore lint/suspicious/useDefaultSwitchClauseLast: We want default to evaluate at first to throw error early
104
- default:
105
- throw Error(
106
- `Invalid startUpgradeFromFork=${startUpgradeFromFork} for headerFork=${headerFork} in upgradeLightClientHeader to targetFork=${targetFork}`
107
- );
108
-
109
- case ForkName.altair:
110
- // biome-ignore lint/suspicious/noFallthroughSwitchClause: We need fall-through behavior here
111
- case ForkName.bellatrix:
112
- // Break if no further upgradation is required else fall through
113
- if (ForkSeq[targetFork] <= ForkSeq.bellatrix) break;
114
-
115
- // biome-ignore lint/suspicious/noFallthroughSwitchClause: We need fall-through behavior here
116
- case ForkName.capella:
117
- (upgradedHeader as LightClientHeader<ForkName.capella>).execution =
118
- ssz.capella.LightClientHeader.fields.execution.defaultValue();
119
- (upgradedHeader as LightClientHeader<ForkName.capella>).executionBranch =
120
- ssz.capella.LightClientHeader.fields.executionBranch.defaultValue();
121
-
122
- // Break if no further upgradation is required else fall through
123
- if (ForkSeq[targetFork] <= ForkSeq.capella) break;
124
-
125
- // biome-ignore lint/suspicious/noFallthroughSwitchClause: We need fall-through behavior here
126
- case ForkName.deneb:
127
- (upgradedHeader as LightClientHeader<ForkName.deneb>).execution.blobGasUsed =
128
- ssz.deneb.LightClientHeader.fields.execution.fields.blobGasUsed.defaultValue();
129
- (upgradedHeader as LightClientHeader<ForkName.deneb>).execution.excessBlobGas =
130
- ssz.deneb.LightClientHeader.fields.execution.fields.excessBlobGas.defaultValue();
131
-
132
- // Break if no further upgradation is required else fall through
133
- if (ForkSeq[targetFork] <= ForkSeq.deneb) break;
134
-
135
- // biome-ignore lint/suspicious/noFallthroughSwitchClause: We need fall-through behavior here
136
- case ForkName.electra:
137
- // No changes to LightClientHeader in Electra
138
-
139
- // Break if no further upgrades is required else fall through
140
- if (ForkSeq[targetFork] <= ForkSeq.electra) break;
141
-
142
- // biome-ignore lint/suspicious/noFallthroughSwitchClause: We need fall-through behavior here
143
- case ForkName.fulu:
144
- // No changes to LightClientHeader in Fulu
145
-
146
- // Break if no further upgrades is required else fall through
147
- if (ForkSeq[targetFork] <= ForkSeq.fulu) break;
148
-
149
- case ForkName.gloas:
150
- // No changes to LightClientHeader in Gloas
151
-
152
- // Break if no further upgrades is required else fall through
153
- if (ForkSeq[targetFork] <= ForkSeq.gloas) break;
154
- }
155
- return upgradedHeader;
156
- }
157
-
158
- export function isValidLightClientHeader(config: ChainForkConfig, header: LightClientHeader): boolean {
159
- const epoch = computeEpochAtSlot(header.beacon.slot);
160
-
161
- if (epoch < config.CAPELLA_FORK_EPOCH) {
162
- return (
163
- ((header as LightClientHeader<ForkName.capella>).execution === undefined ||
164
- ssz.capella.ExecutionPayloadHeader.equals(
165
- (header as LightClientHeader<ForkName.capella>).execution,
166
- ssz.capella.LightClientHeader.fields.execution.defaultValue()
167
- )) &&
168
- ((header as LightClientHeader<ForkName.capella>).executionBranch === undefined ||
169
- ssz.capella.LightClientHeader.fields.executionBranch.equals(
170
- ssz.capella.LightClientHeader.fields.executionBranch.defaultValue(),
171
- (header as LightClientHeader<ForkName.capella>).executionBranch
172
- ))
173
- );
174
- }
175
-
176
- if (
177
- epoch < config.DENEB_FORK_EPOCH &&
178
- (((header as LightClientHeader<ForkName.deneb>).execution.blobGasUsed &&
179
- (header as LightClientHeader<ForkName.deneb>).execution.blobGasUsed !== BigInt(0)) ||
180
- ((header as LightClientHeader<ForkName.deneb>).execution.excessBlobGas &&
181
- (header as LightClientHeader<ForkName.deneb>).execution.excessBlobGas !== BigInt(0)))
182
- ) {
183
- return false;
184
- }
185
-
186
- return isValidMerkleBranch(
187
- config
188
- .getPostBellatrixForkTypes(header.beacon.slot)
189
- .ExecutionPayloadHeader.hashTreeRoot((header as LightClientHeader<ForkName.capella>).execution),
190
- (header as LightClientHeader<ForkName.capella>).executionBranch,
191
- EXECUTION_PAYLOAD_DEPTH,
192
- EXECUTION_PAYLOAD_INDEX,
193
- header.beacon.bodyRoot
194
- );
195
- }
196
-
197
- export function upgradeLightClientUpdate(
198
- config: ChainForkConfig,
199
- targetFork: ForkName,
200
- update: LightClientUpdate
201
- ): LightClientUpdate {
202
- update.attestedHeader = upgradeLightClientHeader(config, targetFork, update.attestedHeader);
203
- update.finalizedHeader = upgradeLightClientHeader(config, targetFork, update.finalizedHeader);
204
- update.nextSyncCommitteeBranch = normalizeMerkleBranch(
205
- update.nextSyncCommitteeBranch,
206
- isForkPostElectra(targetFork) ? NEXT_SYNC_COMMITTEE_DEPTH_ELECTRA : NEXT_SYNC_COMMITTEE_DEPTH
207
- );
208
- update.finalityBranch = normalizeMerkleBranch(
209
- update.finalityBranch,
210
- isForkPostElectra(targetFork) ? FINALIZED_ROOT_DEPTH_ELECTRA : FINALIZED_ROOT_DEPTH
211
- );
212
-
213
- return update;
214
- }
215
-
216
- export function upgradeLightClientFinalityUpdate(
217
- config: ChainForkConfig,
218
- targetFork: ForkName,
219
- finalityUpdate: LightClientFinalityUpdate
220
- ): LightClientFinalityUpdate {
221
- finalityUpdate.attestedHeader = upgradeLightClientHeader(config, targetFork, finalityUpdate.attestedHeader);
222
- finalityUpdate.finalizedHeader = upgradeLightClientHeader(config, targetFork, finalityUpdate.finalizedHeader);
223
- finalityUpdate.finalityBranch = normalizeMerkleBranch(
224
- finalityUpdate.finalityBranch,
225
- isForkPostElectra(targetFork) ? FINALIZED_ROOT_DEPTH_ELECTRA : FINALIZED_ROOT_DEPTH
226
- );
227
-
228
- return finalityUpdate;
229
- }
230
-
231
- export function upgradeLightClientOptimisticUpdate(
232
- config: ChainForkConfig,
233
- targetFork: ForkName,
234
- optimisticUpdate: LightClientOptimisticUpdate
235
- ): LightClientOptimisticUpdate {
236
- optimisticUpdate.attestedHeader = upgradeLightClientHeader(config, targetFork, optimisticUpdate.attestedHeader);
237
-
238
- return optimisticUpdate;
239
- }
240
-
241
- /**
242
- * Currently this upgradation is not required because all processing is done based on the
243
- * summary that the store generates and maintains. In case store needs to be saved to disk,
244
- * this could be required depending on the format the store is saved to the disk
245
- */
246
- export function upgradeLightClientStore(
247
- config: ChainForkConfig,
248
- targetFork: ForkName,
249
- store: LightClientStore,
250
- signatureSlot: Slot
251
- ): LightClientStore {
252
- const updateSignaturePeriod = computeSyncPeriodAtSlot(signatureSlot);
253
- const bestValidUpdate = store.bestValidUpdates.get(updateSignaturePeriod);
254
-
255
- if (bestValidUpdate) {
256
- store.bestValidUpdates.set(updateSignaturePeriod, {
257
- update: upgradeLightClientUpdate(config, targetFork, bestValidUpdate.update),
258
- summary: bestValidUpdate.summary,
259
- });
260
- }
261
-
262
- store.finalizedHeader = upgradeLightClientHeader(config, targetFork, store.finalizedHeader);
263
- store.optimisticHeader = upgradeLightClientHeader(config, targetFork, store.optimisticHeader);
264
-
265
- return store;
266
- }
@@ -1,41 +0,0 @@
1
- import {byteArrayEquals} from "@chainsafe/ssz";
2
- import {ChainForkConfig} from "@lodestar/config";
3
- import {isForkPostElectra} from "@lodestar/params";
4
- import {LightClientBootstrap, Root, ssz} from "@lodestar/types";
5
- import {toHex} from "@lodestar/utils";
6
- import {isValidMerkleBranch} from "../utils/verifyMerkleBranch.js";
7
- import {isValidLightClientHeader} from "./utils.js";
8
-
9
- const CURRENT_SYNC_COMMITTEE_INDEX = 22;
10
- const CURRENT_SYNC_COMMITTEE_DEPTH = 5;
11
- const CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA = 22;
12
- const CURRENT_SYNC_COMMITTEE_DEPTH_ELECTRA = 6;
13
-
14
- export function validateLightClientBootstrap(
15
- config: ChainForkConfig,
16
- trustedBlockRoot: Root,
17
- bootstrap: LightClientBootstrap
18
- ): void {
19
- const headerRoot = ssz.phase0.BeaconBlockHeader.hashTreeRoot(bootstrap.header.beacon);
20
- const fork = config.getForkName(bootstrap.header.beacon.slot);
21
-
22
- if (!isValidLightClientHeader(config, bootstrap.header)) {
23
- throw Error("Bootstrap Header is not Valid Light Client Header");
24
- }
25
-
26
- if (!byteArrayEquals(headerRoot, trustedBlockRoot)) {
27
- throw Error(`bootstrap header root ${toHex(headerRoot)} != trusted root ${toHex(trustedBlockRoot)}`);
28
- }
29
-
30
- if (
31
- !isValidMerkleBranch(
32
- ssz.altair.SyncCommittee.hashTreeRoot(bootstrap.currentSyncCommittee),
33
- bootstrap.currentSyncCommitteeBranch,
34
- isForkPostElectra(fork) ? CURRENT_SYNC_COMMITTEE_DEPTH_ELECTRA : CURRENT_SYNC_COMMITTEE_DEPTH,
35
- isForkPostElectra(fork) ? CURRENT_SYNC_COMMITTEE_INDEX_ELECTRA : CURRENT_SYNC_COMMITTEE_INDEX,
36
- bootstrap.header.beacon.stateRoot
37
- )
38
- ) {
39
- throw Error("Invalid currentSyncCommittee merkle branch");
40
- }
41
- }