@lodestar/beacon-node 1.36.0-dev.6f46a8bd20 → 1.36.0-dev.797fa46c1b

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 (98) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +41 -22
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/api/impl/lodestar/index.d.ts +5 -0
  5. package/lib/api/impl/lodestar/index.d.ts.map +1 -1
  6. package/lib/api/impl/lodestar/index.js +29 -1
  7. package/lib/api/impl/lodestar/index.js.map +1 -1
  8. package/lib/api/impl/node/utils.js +1 -1
  9. package/lib/api/impl/node/utils.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 +32 -16
  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/options.d.ts +0 -2
  21. package/lib/chain/options.d.ts.map +1 -1
  22. package/lib/chain/options.js +0 -1
  23. package/lib/chain/options.js.map +1 -1
  24. package/lib/chain/stateCache/datastore/db.d.ts +12 -0
  25. package/lib/chain/stateCache/datastore/db.d.ts.map +1 -1
  26. package/lib/chain/stateCache/datastore/db.js +70 -0
  27. package/lib/chain/stateCache/datastore/db.js.map +1 -1
  28. package/lib/chain/stateCache/datastore/file.d.ts +1 -0
  29. package/lib/chain/stateCache/datastore/file.d.ts.map +1 -1
  30. package/lib/chain/stateCache/datastore/file.js +7 -0
  31. package/lib/chain/stateCache/datastore/file.js.map +1 -1
  32. package/lib/chain/stateCache/datastore/types.d.ts +1 -0
  33. package/lib/chain/stateCache/datastore/types.d.ts.map +1 -1
  34. package/lib/index.d.ts +2 -0
  35. package/lib/index.d.ts.map +1 -1
  36. package/lib/index.js +2 -0
  37. package/lib/index.js.map +1 -1
  38. package/lib/network/core/networkCore.d.ts.map +1 -1
  39. package/lib/network/core/networkCore.js +5 -1
  40. package/lib/network/core/networkCore.js.map +1 -1
  41. package/lib/network/gossip/encoding.js +1 -1
  42. package/lib/network/gossip/encoding.js.map +1 -1
  43. package/lib/network/gossip/snappy_bun.d.ts +3 -0
  44. package/lib/network/gossip/snappy_bun.d.ts.map +1 -0
  45. package/lib/network/gossip/snappy_bun.js +3 -0
  46. package/lib/network/gossip/snappy_bun.js.map +1 -0
  47. package/lib/network/metadata.d.ts +1 -1
  48. package/lib/network/metadata.d.ts.map +1 -1
  49. package/lib/network/metadata.js +1 -0
  50. package/lib/network/metadata.js.map +1 -1
  51. package/lib/network/options.d.ts +0 -1
  52. package/lib/network/options.d.ts.map +1 -1
  53. package/lib/network/options.js.map +1 -1
  54. package/lib/network/reqresp/handlers/beaconBlocksByRange.d.ts.map +1 -1
  55. package/lib/network/reqresp/handlers/beaconBlocksByRange.js +3 -1
  56. package/lib/network/reqresp/handlers/beaconBlocksByRange.js.map +1 -1
  57. package/lib/node/nodejs.d.ts +2 -1
  58. package/lib/node/nodejs.d.ts.map +1 -1
  59. package/lib/node/nodejs.js +2 -1
  60. package/lib/node/nodejs.js.map +1 -1
  61. package/lib/sync/range/range.d.ts.map +1 -1
  62. package/lib/sync/range/range.js +2 -1
  63. package/lib/sync/range/range.js.map +1 -1
  64. package/lib/sync/utils/remoteSyncType.d.ts +2 -1
  65. package/lib/sync/utils/remoteSyncType.d.ts.map +1 -1
  66. package/lib/sync/utils/remoteSyncType.js +19 -4
  67. package/lib/sync/utils/remoteSyncType.js.map +1 -1
  68. package/lib/util/blobs.d.ts +1 -1
  69. package/lib/util/blobs.d.ts.map +1 -1
  70. package/lib/util/blobs.js +53 -20
  71. package/lib/util/blobs.js.map +1 -1
  72. package/lib/util/sszBytes.d.ts +2 -0
  73. package/lib/util/sszBytes.d.ts.map +1 -1
  74. package/lib/util/sszBytes.js +25 -0
  75. package/lib/util/sszBytes.js.map +1 -1
  76. package/package.json +19 -14
  77. package/src/api/impl/beacon/blocks/index.ts +47 -25
  78. package/src/api/impl/lodestar/index.ts +36 -1
  79. package/src/api/impl/node/utils.ts +1 -1
  80. package/src/chain/chain.ts +48 -23
  81. package/src/chain/forkChoice/index.ts +178 -2
  82. package/src/chain/interface.ts +2 -0
  83. package/src/chain/options.ts +0 -3
  84. package/src/chain/stateCache/datastore/db.ts +89 -1
  85. package/src/chain/stateCache/datastore/file.ts +8 -0
  86. package/src/chain/stateCache/datastore/types.ts +1 -0
  87. package/src/index.ts +2 -0
  88. package/src/network/core/networkCore.ts +5 -1
  89. package/src/network/gossip/encoding.ts +1 -1
  90. package/src/network/gossip/snappy_bun.ts +2 -0
  91. package/src/network/metadata.ts +3 -1
  92. package/src/network/options.ts +0 -1
  93. package/src/network/reqresp/handlers/beaconBlocksByRange.ts +3 -1
  94. package/src/node/nodejs.ts +3 -0
  95. package/src/sync/range/range.ts +2 -1
  96. package/src/sync/utils/remoteSyncType.ts +23 -4
  97. package/src/util/blobs.ts +64 -20
  98. package/src/util/sszBytes.ts +30 -0
@@ -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,
package/src/util/blobs.ts CHANGED
@@ -149,41 +149,85 @@ export async function dataColumnMatrixRecovery(
149
149
  * Reconstruct blobs from a set of data columns, at least 50%+ of all the columns
150
150
  * must be provided to allow to reconstruct the full data matrix
151
151
  */
152
- export async function reconstructBlobs(sidecars: fulu.DataColumnSidecars): Promise<deneb.Blobs> {
152
+ export async function reconstructBlobs(sidecars: fulu.DataColumnSidecars, indices?: number[]): Promise<deneb.Blobs> {
153
153
  if (sidecars.length < NUMBER_OF_COLUMNS / 2) {
154
154
  throw Error(
155
155
  `Expected at least ${NUMBER_OF_COLUMNS / 2} data columns to reconstruct blobs, received ${sidecars.length}`
156
156
  );
157
157
  }
158
+ const blobCount = sidecars[0].column.length;
158
159
 
159
- let fullSidecars: fulu.DataColumnSidecars;
160
-
161
- if (sidecars.length === NUMBER_OF_COLUMNS) {
162
- // Full columns, no need to recover
163
- fullSidecars = sidecars;
164
- } else {
165
- const sidecarsByIndex = new Map<number, fulu.DataColumnSidecar>(sidecars.map((sc) => [sc.index, sc]));
166
- const recoveredSidecars = await dataColumnMatrixRecovery(sidecarsByIndex);
167
- if (recoveredSidecars === null) {
168
- // Should not happen because we check the column count above
169
- throw Error("Failed to reconstruct the full data matrix");
160
+ for (const index of indices ?? []) {
161
+ if (index < 0 || index >= blobCount) {
162
+ throw Error(`Invalid blob index ${index}, must be between 0 and ${blobCount - 1}`);
170
163
  }
171
- fullSidecars = recoveredSidecars;
164
+ }
165
+ const indicesToReconstruct = indices ?? Array.from({length: blobCount}, (_, i) => i);
166
+
167
+ const recoveredCells = await recoverBlobCells(sidecars, indicesToReconstruct);
168
+ if (recoveredCells === null) {
169
+ // Should not happen because we check the column count above
170
+ throw Error("Failed to recover cells to reconstruct blobs");
172
171
  }
173
172
 
174
- const blobCount = fullSidecars[0].column.length;
175
- const blobs: deneb.Blobs = new Array(blobCount);
173
+ const blobs: deneb.Blobs = new Array(indicesToReconstruct.length);
176
174
 
177
- const ordered = fullSidecars.slice().sort((a, b) => a.index - b.index);
178
- for (let row = 0; row < blobCount; row++) {
179
- // 128 cells that make up one "extended blob" row
180
- const cells = ordered.map((col) => col.column[row]);
181
- blobs[row] = cellsToBlob(cells);
175
+ for (let i = 0; i < indicesToReconstruct.length; i++) {
176
+ const blobIndex = indicesToReconstruct[i];
177
+ const cells = recoveredCells.get(blobIndex);
178
+ if (!cells) {
179
+ throw Error(`Failed to get recovered cells for blob index ${blobIndex}`);
180
+ }
181
+ blobs[i] = cellsToBlob(cells);
182
182
  }
183
183
 
184
184
  return blobs;
185
185
  }
186
186
 
187
+ /**
188
+ * Recover cells for specific blob indices from a set of data columns
189
+ */
190
+ async function recoverBlobCells(
191
+ partialSidecars: fulu.DataColumnSidecar[],
192
+ blobIndices: number[]
193
+ ): Promise<Map<number, fulu.Cell[]> | null> {
194
+ const columnCount = partialSidecars.length;
195
+ if (columnCount < NUMBER_OF_COLUMNS / 2) {
196
+ // We don't have enough columns to recover
197
+ return null;
198
+ }
199
+
200
+ const recoveredCells = new Map<number, fulu.Cell[]>();
201
+ // Sort data columns by index in ascending order
202
+ const partialSidecarsSorted = partialSidecars.slice().sort((a, b) => a.index - b.index);
203
+
204
+ if (columnCount === NUMBER_OF_COLUMNS) {
205
+ // Full columns, no need to recover
206
+ for (const blobIndex of blobIndices) {
207
+ // 128 cells that make up one "extended blob" row
208
+ const cells = partialSidecarsSorted.map((col) => col.column[blobIndex]);
209
+ recoveredCells.set(blobIndex, cells);
210
+ }
211
+ return recoveredCells;
212
+ }
213
+
214
+ await Promise.all(
215
+ blobIndices.map(async (blobIndex) => {
216
+ const cellIndices: number[] = [];
217
+ const cells: fulu.Cell[] = [];
218
+ for (const dataColumn of partialSidecarsSorted) {
219
+ cellIndices.push(dataColumn.index);
220
+ cells.push(dataColumn.column[blobIndex]);
221
+ }
222
+ // Recover cells for this specific blob row
223
+ const recovered = await kzg.asyncRecoverCellsAndKzgProofs(cellIndices, cells);
224
+ recoveredCells.set(blobIndex, recovered.cells);
225
+ })
226
+ );
227
+
228
+ return recoveredCells;
229
+ }
230
+
187
231
  /**
188
232
  * Concatenate the systematic half (columns 0‑63) of a row of cells into
189
233
  * the original 131072 byte blob. The parity half (64‑127) is ignored as
@@ -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
  *