@lodestar/beacon-node 1.36.0-dev.43f2c7cbbd → 1.36.0-dev.52be14b24f

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 (93) hide show
  1. package/lib/api/impl/lodestar/index.d.ts +5 -0
  2. package/lib/api/impl/lodestar/index.d.ts.map +1 -1
  3. package/lib/api/impl/lodestar/index.js +35 -10
  4. package/lib/api/impl/lodestar/index.js.map +1 -1
  5. package/lib/api/impl/node/utils.js +1 -1
  6. package/lib/api/impl/node/utils.js.map +1 -1
  7. package/lib/chain/archiveStore/strategies/frequencyStateArchiveStrategy.d.ts.map +1 -1
  8. package/lib/chain/archiveStore/strategies/frequencyStateArchiveStrategy.js +6 -1
  9. package/lib/chain/archiveStore/strategies/frequencyStateArchiveStrategy.js.map +1 -1
  10. package/lib/chain/chain.d.ts +5 -2
  11. package/lib/chain/chain.d.ts.map +1 -1
  12. package/lib/chain/chain.js +27 -12
  13. package/lib/chain/chain.js.map +1 -1
  14. package/lib/chain/forkChoice/index.d.ts +9 -1
  15. package/lib/chain/forkChoice/index.d.ts.map +1 -1
  16. package/lib/chain/forkChoice/index.js +109 -4
  17. package/lib/chain/forkChoice/index.js.map +1 -1
  18. package/lib/chain/interface.d.ts +2 -0
  19. package/lib/chain/interface.d.ts.map +1 -1
  20. package/lib/chain/stateCache/datastore/db.d.ts +12 -0
  21. package/lib/chain/stateCache/datastore/db.d.ts.map +1 -1
  22. package/lib/chain/stateCache/datastore/db.js +70 -0
  23. package/lib/chain/stateCache/datastore/db.js.map +1 -1
  24. package/lib/chain/stateCache/datastore/file.d.ts +1 -0
  25. package/lib/chain/stateCache/datastore/file.d.ts.map +1 -1
  26. package/lib/chain/stateCache/datastore/file.js +7 -0
  27. package/lib/chain/stateCache/datastore/file.js.map +1 -1
  28. package/lib/chain/stateCache/datastore/types.d.ts +1 -0
  29. package/lib/chain/stateCache/datastore/types.d.ts.map +1 -1
  30. package/lib/index.d.ts +2 -0
  31. package/lib/index.d.ts.map +1 -1
  32. package/lib/index.js +2 -0
  33. package/lib/index.js.map +1 -1
  34. package/lib/metrics/metrics/lodestar.js +1 -1
  35. package/lib/metrics/metrics/lodestar.js.map +1 -1
  36. package/lib/network/core/networkCoreWorker.js +2 -7
  37. package/lib/network/core/networkCoreWorker.js.map +1 -1
  38. package/lib/network/discv5/worker.js +2 -7
  39. package/lib/network/discv5/worker.js.map +1 -1
  40. package/lib/network/gossip/encoding.js +1 -1
  41. package/lib/network/gossip/encoding.js.map +1 -1
  42. package/lib/network/gossip/snappy_bun.d.ts +3 -0
  43. package/lib/network/gossip/snappy_bun.d.ts.map +1 -0
  44. package/lib/network/gossip/snappy_bun.js +3 -0
  45. package/lib/network/gossip/snappy_bun.js.map +1 -0
  46. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  47. package/lib/network/processor/gossipHandlers.js +6 -2
  48. package/lib/network/processor/gossipHandlers.js.map +1 -1
  49. package/lib/network/reqresp/handlers/beaconBlocksByRange.d.ts.map +1 -1
  50. package/lib/network/reqresp/handlers/beaconBlocksByRange.js +3 -1
  51. package/lib/network/reqresp/handlers/beaconBlocksByRange.js.map +1 -1
  52. package/lib/node/nodejs.d.ts +2 -1
  53. package/lib/node/nodejs.d.ts.map +1 -1
  54. package/lib/node/nodejs.js +2 -1
  55. package/lib/node/nodejs.js.map +1 -1
  56. package/lib/sync/range/range.d.ts.map +1 -1
  57. package/lib/sync/range/range.js +2 -1
  58. package/lib/sync/range/range.js.map +1 -1
  59. package/lib/sync/utils/remoteSyncType.d.ts +2 -1
  60. package/lib/sync/utils/remoteSyncType.d.ts.map +1 -1
  61. package/lib/sync/utils/remoteSyncType.js +19 -4
  62. package/lib/sync/utils/remoteSyncType.js.map +1 -1
  63. package/lib/util/profile.d.ts +6 -4
  64. package/lib/util/profile.d.ts.map +1 -1
  65. package/lib/util/profile.js +40 -3
  66. package/lib/util/profile.js.map +1 -1
  67. package/lib/util/sszBytes.d.ts +2 -0
  68. package/lib/util/sszBytes.d.ts.map +1 -1
  69. package/lib/util/sszBytes.js +25 -0
  70. package/lib/util/sszBytes.js.map +1 -1
  71. package/package.json +30 -24
  72. package/src/api/impl/lodestar/index.ts +42 -10
  73. package/src/api/impl/node/utils.ts +1 -1
  74. package/src/chain/archiveStore/strategies/frequencyStateArchiveStrategy.ts +5 -1
  75. package/src/chain/chain.ts +43 -18
  76. package/src/chain/forkChoice/index.ts +178 -2
  77. package/src/chain/interface.ts +2 -0
  78. package/src/chain/stateCache/datastore/db.ts +89 -1
  79. package/src/chain/stateCache/datastore/file.ts +8 -0
  80. package/src/chain/stateCache/datastore/types.ts +1 -0
  81. package/src/index.ts +2 -0
  82. package/src/metrics/metrics/lodestar.ts +1 -1
  83. package/src/network/core/networkCoreWorker.ts +2 -7
  84. package/src/network/discv5/worker.ts +2 -7
  85. package/src/network/gossip/encoding.ts +1 -1
  86. package/src/network/gossip/snappy_bun.ts +2 -0
  87. package/src/network/processor/gossipHandlers.ts +6 -1
  88. package/src/network/reqresp/handlers/beaconBlocksByRange.ts +3 -1
  89. package/src/node/nodejs.ts +3 -0
  90. package/src/sync/range/range.ts +2 -1
  91. package/src/sync/utils/remoteSyncType.ts +23 -4
  92. package/src/util/profile.ts +45 -3
  93. package/src/util/sszBytes.ts +30 -0
@@ -1,5 +1,3 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
1
  import worker from "node:worker_threads";
4
2
  import {privateKeyFromProtobuf} from "@libp2p/crypto/keys";
5
3
  import {peerIdFromPrivateKey} from "@libp2p/peer-id";
@@ -14,7 +12,7 @@ import {Gauge} from "@lodestar/utils";
14
12
  import {RegistryMetricCreator} from "../../metrics/index.js";
15
13
  import {collectNodeJSMetrics} from "../../metrics/nodeJsMetrics.js";
16
14
  import {Clock} from "../../util/clock.js";
17
- import {profileNodeJS, writeHeapSnapshot} from "../../util/profile.js";
15
+ import {ProfileThread, profileThread, writeHeapSnapshot} from "../../util/profile.js";
18
16
  import {Discv5WorkerApi, Discv5WorkerData} from "./types.js";
19
17
  import {ENRRelevance, enrRelevance} from "./utils.js";
20
18
 
@@ -108,10 +106,7 @@ const module: Discv5WorkerApi = {
108
106
  return (await metricsRegistry?.metrics()) ?? "";
109
107
  },
110
108
  writeProfile: async (durationMs: number, dirpath: string) => {
111
- const profile = await profileNodeJS(durationMs);
112
- const filePath = path.join(dirpath, `discv5_thread_${new Date().toISOString()}.cpuprofile`);
113
- fs.writeFileSync(filePath, profile);
114
- return filePath;
109
+ return profileThread(ProfileThread.DISC5, durationMs, dirpath);
115
110
  },
116
111
  writeHeapSnapshot: async (prefix: string, dirpath: string) => {
117
112
  return writeHeapSnapshot(prefix, dirpath);
@@ -1,6 +1,6 @@
1
1
  import {Message} from "@libp2p/interface";
2
- import {compress, uncompress} from "snappyjs";
3
2
  import xxhashFactory from "xxhash-wasm";
3
+ import {compress, uncompress} from "#snappy";
4
4
  import {digest} from "@chainsafe/as-sha256";
5
5
  import {RPC} from "@chainsafe/libp2p-gossipsub/message";
6
6
  import {DataTransform} from "@chainsafe/libp2p-gossipsub/types";
@@ -0,0 +1,2 @@
1
+ import {snappy} from "@lodestar/bun";
2
+ export const {compress, uncompress} = snappy;
@@ -39,6 +39,7 @@ import {
39
39
  BlockError,
40
40
  BlockErrorCode,
41
41
  BlockGossipError,
42
+ DataColumnSidecarErrorCode,
42
43
  DataColumnSidecarGossipError,
43
44
  GossipAction,
44
45
  GossipActionError,
@@ -304,7 +305,11 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand
304
305
  ...blockInput.getLogMeta(),
305
306
  index: dataColumnSidecar.index,
306
307
  });
307
- return blockInput;
308
+ throw new DataColumnSidecarGossipError(GossipAction.IGNORE, {
309
+ code: DataColumnSidecarErrorCode.ALREADY_KNOWN,
310
+ columnIdx: dataColumnSidecar.index,
311
+ slot,
312
+ });
308
313
  }
309
314
  }
310
315
 
@@ -19,7 +19,9 @@ export async function* onBeaconBlocksByRange(
19
19
 
20
20
  const finalized = db.blockArchive;
21
21
  const unfinalized = db.block;
22
- const finalizedSlot = chain.forkChoice.getFinalizedBlock().slot;
22
+ // in the case of initializing from a non-finalized state, we don't have the finalized block so this api does not work
23
+ // chain.forkChoice.getFinalizeBlock().slot
24
+ const finalizedSlot = chain.forkChoice.getFinalizedCheckpointSlot();
23
25
 
24
26
  // Finalized range of blocks
25
27
  if (startSlot <= finalizedSlot) {
@@ -53,6 +53,7 @@ export type BeaconNodeInitModules = {
53
53
  dataDir: string;
54
54
  peerStoreDir?: string;
55
55
  anchorState: BeaconStateAllForks;
56
+ isAnchorStateFinalized: boolean;
56
57
  wsCheckpoint?: phase0.Checkpoint;
57
58
  metricsRegistries?: Registry[];
58
59
  };
@@ -154,6 +155,7 @@ export class BeaconNode {
154
155
  dataDir,
155
156
  peerStoreDir,
156
157
  anchorState,
158
+ isAnchorStateFinalized,
157
159
  wsCheckpoint,
158
160
  metricsRegistries = [],
159
161
  }: BeaconNodeInitModules): Promise<T> {
@@ -217,6 +219,7 @@ export class BeaconNode {
217
219
  metrics,
218
220
  validatorMonitor,
219
221
  anchorState,
222
+ isAnchorStateFinalized,
220
223
  eth1: initializeEth1ForBlockProduction(opts.eth1, {
221
224
  config,
222
225
  db,
@@ -114,13 +114,14 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
114
114
  */
115
115
  addPeer(peerId: PeerIdStr, localStatus: Status, peerStatus: Status): void {
116
116
  // Compute if we should do a Finalized or Head sync with this peer
117
- const {syncType, startEpoch, target} = getRangeSyncTarget(localStatus, peerStatus, this.chain.forkChoice);
117
+ const {syncType, startEpoch, target} = getRangeSyncTarget(localStatus, peerStatus, this.chain);
118
118
  this.logger.debug("Sync peer joined", {
119
119
  peer: peerId,
120
120
  syncType,
121
121
  startEpoch,
122
122
  targetSlot: target.slot,
123
123
  targetRoot: toRootHex(target.root),
124
+ localHeadSlot: localStatus.headSlot,
124
125
  earliestAvailableSlot: (peerStatus as fulu.Status).earliestAvailableSlot ?? Infinity,
125
126
  });
126
127
 
@@ -1,6 +1,7 @@
1
1
  import {IForkChoice} from "@lodestar/fork-choice";
2
2
  import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
3
3
  import {Slot, Status} from "@lodestar/types";
4
+ import {IBeaconChain} from "../../chain/interface.ts";
4
5
  import {ChainTarget} from "../range/utils/index.js";
5
6
 
6
7
  /** The type of peer relative to our current state */
@@ -103,8 +104,11 @@ export function getRangeSyncType(local: Status, remote: Status, forkChoice: IFor
103
104
  export function getRangeSyncTarget(
104
105
  local: Status,
105
106
  remote: Status,
106
- forkChoice: IForkChoice
107
+ chain: IBeaconChain
107
108
  ): {syncType: RangeSyncType; startEpoch: Slot; target: ChainTarget} {
109
+ const forkChoice = chain.forkChoice;
110
+
111
+ // finalized sync
108
112
  if (remote.finalizedEpoch > local.finalizedEpoch && !forkChoice.hasBlock(remote.finalizedRoot)) {
109
113
  return {
110
114
  // If RangeSyncType.Finalized, the range of blocks fetchable from startEpoch and target must allow to switch
@@ -131,11 +135,26 @@ export function getRangeSyncTarget(
131
135
  },
132
136
  };
133
137
  }
138
+
139
+ // we don't want to sync from epoch < minEpoch
140
+ // if we boot from an unfinalized checkpoint state, we don't want to sync before anchorStateLatestBlockSlot
141
+ // if we boot from a finalized checkpoint state, anchorStateLatestBlockSlot is trusted and we also don't want to sync before it
142
+ const minEpoch = Math.max(remote.finalizedEpoch, computeEpochAtSlot(chain.anchorStateLatestBlockSlot));
143
+
144
+ // head sync
134
145
  return {
135
146
  syncType: RangeSyncType.Head,
136
- // The new peer has the same finalized (earlier filters should prevent a peer with an
137
- // earlier finalized chain from reaching here) and local head will always be >= local finalized.
138
- startEpoch: computeEpochAtSlot(local.headSlot),
147
+ // The new peer has the same finalized `remote.finalizedEpoch == local.finalizedEpoch` since
148
+ // previous filters should prevent a peer with an earlier finalized chain from reaching here.
149
+ //
150
+ // By default and during stable network conditions, the head sync always starts from
151
+ // the finalized epoch (even though it's the head sync) because finalized epoch is < local head.
152
+ // This is to prevent the issue noted here https://github.com/ChainSafe/lodestar/pull/7509#discussion_r1984353063.
153
+ //
154
+ // During non-finality of the network, when starting from an unfinalized checkpoint state, we don't want
155
+ // to sync before anchorStateLatestBlockSlot as finalized epoch is too far away. Local head will also be
156
+ // the same to that value at startup, the head sync always starts from anchorStateLatestBlockSlot in this case.
157
+ startEpoch: Math.min(computeEpochAtSlot(local.headSlot), minEpoch),
139
158
  target: {
140
159
  slot: remote.headSlot,
141
160
  root: remote.headRoot,
@@ -1,13 +1,32 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
1
3
  import {sleep} from "@lodestar/utils";
2
4
 
5
+ export enum ProfileThread {
6
+ MAIN = "main",
7
+ NETWORK = "network",
8
+ DISC5 = "discv5",
9
+ }
10
+
11
+ /**
12
+ * The time to take a Bun profile.
13
+ * If we increase this time it'll potentiall cause the app to crash.
14
+ * If we decrease this time, profile recorded will be fragmented and hard to analyze.
15
+ */
16
+ const BUN_PROFILE_MS = 3 * 1000;
17
+
18
+ export async function profileThread(thread: ProfileThread, durationMs: number, dirpath: string): Promise<string> {
19
+ return globalThis.Bun ? profileBun(thread, durationMs) : profileNodeJS(thread, durationMs, dirpath);
20
+ }
21
+
3
22
  /**
4
- * Take 10m profile of the current thread without promise tracking.
23
+ * Take `durationMs` profile of the current thread and return the persisted file path.
5
24
  */
6
- export async function profileNodeJS(durationMs: number): Promise<string> {
25
+ async function profileNodeJS(thread: ProfileThread, durationMs: number, dirpath: string): Promise<string> {
7
26
  const inspector = await import("node:inspector");
8
27
 
9
28
  // due to some typing issues, not able to use promisify here
10
- return new Promise<string>((resolve, reject) => {
29
+ const profile = await new Promise<string>((resolve, reject) => {
11
30
  // Start the inspector and connect to it
12
31
  const session = new inspector.Session();
13
32
  session.connect();
@@ -29,6 +48,29 @@ export async function profileNodeJS(durationMs: number): Promise<string> {
29
48
  });
30
49
  });
31
50
  });
51
+
52
+ const filePath = path.join(dirpath, `${thread}_thread_${new Date().toISOString()}.cpuprofile`);
53
+ fs.writeFileSync(filePath, profile);
54
+ return filePath;
55
+ }
56
+
57
+ /**
58
+ * Unlike NodeJS, Bun console.profile() api flush data to the inspector,
59
+ * so this api returns ms taken of this profile instead of file path.
60
+ */
61
+ async function profileBun(thread: ProfileThread, durationMs: number): Promise<string> {
62
+ const start = Date.now();
63
+ let now = Date.now();
64
+ while (now - start < durationMs) {
65
+ // biome-ignore lint/suspicious/noConsole: need to use console api to profile in Bun
66
+ console.profile(String(now));
67
+ await sleep(BUN_PROFILE_MS);
68
+ // biome-ignore lint/suspicious/noConsole: need to use console api to profile in Bun
69
+ console.profileEnd(String(now));
70
+ now = Date.now();
71
+ }
72
+
73
+ return `Successfully take Bun ${thread} thread profile in ${now - start}ms. Check your inspector to see the profile.`;
32
74
  }
33
75
 
34
76
  /**
@@ -417,6 +417,36 @@ export function getSlotFromDataColumnSidecarSerialized(data: Uint8Array): Slot |
417
417
  return getSlotFromOffset(data, SLOT_BYTES_POSITION_IN_SIGNED_DATA_COLUMN_SIDECAR);
418
418
  }
419
419
 
420
+ /**
421
+ * BeaconState of all forks (up until Electra, check with new forks)
422
+ * class BeaconState(Container):
423
+ * genesis_time: uint64 - 8 bytes
424
+ * genesis_validators_root: Root - 32 bytes
425
+ * slot: Slot - 8 bytes
426
+ * fork: Fork - 16 bytes
427
+ * latest_block_header: BeaconBlockHeader - fixed size
428
+ * slot: Slot - 8 bytes
429
+ *
430
+ */
431
+
432
+ const BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE = 8 + 32 + 8 + 16;
433
+ export function getLastProcessedSlotFromBeaconStateSerialized(data: Uint8Array): Slot | null {
434
+ if (data.length < BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE + SLOT_SIZE) {
435
+ return null;
436
+ }
437
+
438
+ return getSlotFromOffset(data, BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE);
439
+ }
440
+
441
+ const SLOT_BYTES_POSITION_IN_BEACON_STATE = 8 + 32;
442
+ export function getSlotFromBeaconStateSerialized(data: Uint8Array): Slot | null {
443
+ if (data.length < SLOT_BYTES_POSITION_IN_BEACON_STATE) {
444
+ return null;
445
+ }
446
+
447
+ return getSlotFromOffset(data, SLOT_BYTES_POSITION_IN_BEACON_STATE);
448
+ }
449
+
420
450
  /**
421
451
  * Read only the first 4 bytes of Slot, max value is 4,294,967,295 will be reached 1634 years after genesis
422
452
  *