@lodestar/beacon-node 1.48.0-dev.a427d3dbb5 → 1.48.0-dev.acdde443d2
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.
- package/lib/chain/archiveStore/archiveStore.d.ts +1 -0
- package/lib/chain/archiveStore/archiveStore.d.ts.map +1 -1
- package/lib/chain/archiveStore/archiveStore.js +1 -0
- package/lib/chain/archiveStore/archiveStore.js.map +1 -1
- package/lib/chain/archiveStore/historicalState/historicalStateRegen.d.ts.map +1 -1
- package/lib/chain/archiveStore/historicalState/historicalStateRegen.js +1 -0
- package/lib/chain/archiveStore/historicalState/historicalStateRegen.js.map +1 -1
- package/lib/chain/archiveStore/historicalState/types.d.ts +2 -0
- package/lib/chain/archiveStore/historicalState/types.d.ts.map +1 -1
- package/lib/chain/archiveStore/historicalState/types.js.map +1 -1
- package/lib/chain/archiveStore/historicalState/worker.js +4 -1
- package/lib/chain/archiveStore/historicalState/worker.js.map +1 -1
- package/lib/chain/archiveStore/utils/archiveBlocks.d.ts.map +1 -1
- package/lib/chain/archiveStore/utils/archiveBlocks.js +31 -34
- package/lib/chain/archiveStore/utils/archiveBlocks.js.map +1 -1
- package/lib/chain/blocks/writeBlockInputToDb.d.ts.map +1 -1
- package/lib/chain/blocks/writeBlockInputToDb.js +9 -16
- package/lib/chain/blocks/writeBlockInputToDb.js.map +1 -1
- package/lib/chain/chain.d.ts +2 -1
- package/lib/chain/chain.d.ts.map +1 -1
- package/lib/chain/chain.js +47 -24
- package/lib/chain/chain.js.map +1 -1
- package/lib/db/beacon.d.ts +14 -5
- package/lib/db/beacon.d.ts.map +1 -1
- package/lib/db/beacon.js +17 -3
- package/lib/db/beacon.js.map +1 -1
- package/lib/db/dataColumnStore.d.ts +39 -0
- package/lib/db/dataColumnStore.d.ts.map +1 -0
- package/lib/db/dataColumnStore.js +81 -0
- package/lib/db/dataColumnStore.js.map +1 -0
- package/lib/db/flatFileStore/atomicWrite.d.ts +8 -0
- package/lib/db/flatFileStore/atomicWrite.d.ts.map +1 -0
- package/lib/db/flatFileStore/atomicWrite.js +61 -0
- package/lib/db/flatFileStore/atomicWrite.js.map +1 -0
- package/lib/db/flatFileStore/dcolFormat.d.ts +90 -0
- package/lib/db/flatFileStore/dcolFormat.d.ts.map +1 -0
- package/lib/db/flatFileStore/dcolFormat.js +286 -0
- package/lib/db/flatFileStore/dcolFormat.js.map +1 -0
- package/lib/db/flatFileStore/errors.d.ts +82 -0
- package/lib/db/flatFileStore/errors.d.ts.map +1 -0
- package/lib/db/flatFileStore/errors.js +32 -0
- package/lib/db/flatFileStore/errors.js.map +1 -0
- package/lib/db/flatFileStore/flatFileStore.d.ts +41 -0
- package/lib/db/flatFileStore/flatFileStore.d.ts.map +1 -0
- package/lib/db/flatFileStore/flatFileStore.js +251 -0
- package/lib/db/flatFileStore/flatFileStore.js.map +1 -0
- package/lib/db/flatFileStore/index.d.ts +4 -0
- package/lib/db/flatFileStore/index.d.ts.map +1 -0
- package/lib/db/flatFileStore/index.js +2 -0
- package/lib/db/flatFileStore/index.js.map +1 -0
- package/lib/db/flatFileStore/interface.d.ts +25 -0
- package/lib/db/flatFileStore/interface.d.ts.map +1 -0
- package/lib/db/flatFileStore/interface.js +2 -0
- package/lib/db/flatFileStore/interface.js.map +1 -0
- package/lib/db/flatFileStore/metrics.d.ts +24 -0
- package/lib/db/flatFileStore/metrics.d.ts.map +1 -0
- package/lib/db/flatFileStore/metrics.js +25 -0
- package/lib/db/flatFileStore/metrics.js.map +1 -0
- package/lib/db/flatFileStore/path.d.ts +9 -0
- package/lib/db/flatFileStore/path.d.ts.map +1 -0
- package/lib/db/flatFileStore/path.js +21 -0
- package/lib/db/flatFileStore/path.js.map +1 -0
- package/lib/db/flatFileStore/slotIndex.d.ts +14 -0
- package/lib/db/flatFileStore/slotIndex.d.ts.map +1 -0
- package/lib/db/flatFileStore/slotIndex.js +46 -0
- package/lib/db/flatFileStore/slotIndex.js.map +1 -0
- package/lib/db/flatFileStore/snappy.d.ts +3 -0
- package/lib/db/flatFileStore/snappy.d.ts.map +1 -0
- package/lib/db/flatFileStore/snappy.js +12 -0
- package/lib/db/flatFileStore/snappy.js.map +1 -0
- package/lib/db/index.d.ts +1 -0
- package/lib/db/index.d.ts.map +1 -1
- package/lib/db/interface.d.ts +8 -1
- package/lib/db/interface.d.ts.map +1 -1
- package/lib/db/repositories/blockArchive.d.ts +8 -1
- package/lib/db/repositories/blockArchive.d.ts.map +1 -1
- package/lib/db/repositories/blockArchive.js +65 -28
- package/lib/db/repositories/blockArchive.js.map +1 -1
- package/lib/db/repositories/blockArchiveIndex.d.ts +1 -2
- package/lib/db/repositories/blockArchiveIndex.d.ts.map +1 -1
- package/lib/db/repositories/blockArchiveIndex.js +3 -7
- package/lib/db/repositories/blockArchiveIndex.js.map +1 -1
- package/lib/metrics/metrics/lodestar.d.ts +18 -0
- package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
- package/lib/metrics/metrics/lodestar.js +55 -1
- package/lib/metrics/metrics/lodestar.js.map +1 -1
- package/lib/monitoring/clientStats.js +1 -1
- package/lib/monitoring/clientStats.js.map +1 -1
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.d.ts.map +1 -1
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js +112 -91
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js.map +1 -1
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRoot.d.ts.map +1 -1
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRoot.js +2 -0
- package/lib/network/reqresp/handlers/dataColumnSidecarsByRoot.js.map +1 -1
- package/lib/network/reqresp/utils/dataColumnResponseValidation.d.ts +2 -1
- package/lib/network/reqresp/utils/dataColumnResponseValidation.d.ts.map +1 -1
- package/lib/network/reqresp/utils/dataColumnResponseValidation.js +14 -9
- package/lib/network/reqresp/utils/dataColumnResponseValidation.js.map +1 -1
- package/lib/node/nodejs.d.ts +2 -1
- package/lib/node/nodejs.d.ts.map +1 -1
- package/lib/node/nodejs.js +4 -2
- package/lib/node/nodejs.js.map +1 -1
- package/lib/util/sszBytes.d.ts +6 -0
- package/lib/util/sszBytes.d.ts.map +1 -1
- package/lib/util/sszBytes.js +1 -1
- package/lib/util/sszBytes.js.map +1 -1
- package/package.json +15 -15
- package/src/chain/archiveStore/archiveStore.ts +6 -1
- package/src/chain/archiveStore/historicalState/historicalStateRegen.ts +1 -0
- package/src/chain/archiveStore/historicalState/types.ts +2 -0
- package/src/chain/archiveStore/historicalState/worker.ts +4 -1
- package/src/chain/archiveStore/utils/archiveBlocks.ts +34 -39
- package/src/chain/blocks/writeBlockInputToDb.ts +10 -17
- package/src/chain/chain.ts +53 -23
- package/src/db/beacon.ts +33 -6
- package/src/db/dataColumnStore.ts +113 -0
- package/src/db/flatFileStore/atomicWrite.ts +65 -0
- package/src/db/flatFileStore/dcolFormat.ts +389 -0
- package/src/db/flatFileStore/errors.ts +53 -0
- package/src/db/flatFileStore/flatFileStore.ts +310 -0
- package/src/db/flatFileStore/index.ts +3 -0
- package/src/db/flatFileStore/interface.ts +21 -0
- package/src/db/flatFileStore/metrics.ts +48 -0
- package/src/db/flatFileStore/path.ts +32 -0
- package/src/db/flatFileStore/slotIndex.ts +56 -0
- package/src/db/flatFileStore/snappy.ts +14 -0
- package/src/db/index.ts +1 -0
- package/src/db/interface.ts +9 -1
- package/src/db/repositories/blockArchive.ts +89 -30
- package/src/db/repositories/blockArchiveIndex.ts +4 -9
- package/src/metrics/metrics/lodestar.ts +57 -1
- package/src/monitoring/clientStats.ts +1 -1
- package/src/network/reqresp/handlers/dataColumnSidecarsByRange.ts +139 -105
- package/src/network/reqresp/handlers/dataColumnSidecarsByRoot.ts +2 -0
- package/src/network/reqresp/utils/dataColumnResponseValidation.ts +15 -7
- package/src/node/nodejs.ts +6 -1
- package/src/util/sszBytes.ts +1 -1
|
@@ -134,6 +134,8 @@ export async function archiveBlocks(
|
|
|
134
134
|
logger.verbose("Migrated blobSidecars from hot DB to cold DB", {...logCtx, migratedEntries});
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
// Keep the normal LevelDB hot-to-cold flow for legacy columns. New columns are written
|
|
138
|
+
// directly to flat files, so this only moves fallback data left by older versions.
|
|
137
139
|
if (finalizedPostFulu) {
|
|
138
140
|
const migratedSlots = await migrateDataColumnSidecarsFromHotToColdDb(
|
|
139
141
|
config,
|
|
@@ -142,7 +144,7 @@ export async function archiveBlocks(
|
|
|
142
144
|
finalizedCanonicalBlocks,
|
|
143
145
|
currentEpoch
|
|
144
146
|
);
|
|
145
|
-
logger.verbose("Migrated dataColumnSidecars from hot DB to cold DB", {
|
|
147
|
+
logger.verbose("Migrated legacy dataColumnSidecars from hot DB to cold DB", {
|
|
146
148
|
...logCtx,
|
|
147
149
|
migratedEntries: migratedSlots.length,
|
|
148
150
|
slotRange: prettyPrintIndices(migratedSlots),
|
|
@@ -195,6 +197,23 @@ export async function archiveBlocks(
|
|
|
195
197
|
slotRange: prettyPrintIndices(nonCanonicalSlots),
|
|
196
198
|
};
|
|
197
199
|
|
|
200
|
+
const columnItems = finalizedNonCanonicalBlocks
|
|
201
|
+
// Gloas EMPTY and FULL variants share a block root. EMPTY has no sidecars, so deleting by its root could
|
|
202
|
+
// remove the canonical FULL variant's columns. Pre-Gloas blocks are always FULL.
|
|
203
|
+
.filter(
|
|
204
|
+
(summary) => config.getForkSeq(summary.slot) >= ForkSeq.fulu && summary.payloadStatus === PayloadStatus.FULL
|
|
205
|
+
)
|
|
206
|
+
.map((summary) => ({slot: summary.slot, blockRoot: summary.blockRoot}));
|
|
207
|
+
if (columnItems.length > 0) {
|
|
208
|
+
// Delete sidecars first so their block roots remain available to retry cleanup after a failure or crash.
|
|
209
|
+
await db.dataColumns.deleteMany(columnItems);
|
|
210
|
+
logger.verbose("Deleted non canonical data columns of blocks", {
|
|
211
|
+
...logCtx,
|
|
212
|
+
blocks: columnItems.length,
|
|
213
|
+
slotRange: prettyPrintIndices(columnItems.map(({slot}) => slot).sort((a, b) => a - b)),
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
198
217
|
await db.block.batchDelete(nonCanonicalBlockRoots);
|
|
199
218
|
logger.verbose("Deleted non canonical blocks from hot DB", nonCanonicalLogCtx);
|
|
200
219
|
|
|
@@ -203,11 +222,6 @@ export async function archiveBlocks(
|
|
|
203
222
|
logger.verbose("Deleted non canonical blobSidecars from hot DB", nonCanonicalLogCtx);
|
|
204
223
|
}
|
|
205
224
|
|
|
206
|
-
if (finalizedPostFulu) {
|
|
207
|
-
await db.dataColumnSidecar.deleteMany(nonCanonicalBlockRoots);
|
|
208
|
-
logger.verbose("Deleted non canonical dataColumnSidecars from hot DB", nonCanonicalLogCtx);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
225
|
if (finalizedPostGloas) {
|
|
212
226
|
await db.executionPayloadEnvelope.batchDelete(nonCanonicalBlockRoots);
|
|
213
227
|
logger.verbose("Deleted non canonical executionPayloadEnvelopes from hot DB", nonCanonicalLogCtx);
|
|
@@ -245,23 +259,16 @@ export async function archiveBlocks(
|
|
|
245
259
|
);
|
|
246
260
|
const dataColumnSidecarsMinEpoch = currentEpoch - dataColumnSidecarsArchiveWindow;
|
|
247
261
|
if (dataColumnSidecarsMinEpoch >= config.FULU_FORK_EPOCH) {
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
});
|
|
252
|
-
// for each slot there could be multiple dataColumnSidecar, so we need to deduplicate it
|
|
253
|
-
const slotsToDelete = [...new Set(prefixedKeys.map(({prefix}) => prefix))].sort((a, b) => a - b);
|
|
254
|
-
|
|
255
|
-
if (slotsToDelete.length > 0) {
|
|
256
|
-
await db.dataColumnSidecarArchive.deleteMany(slotsToDelete);
|
|
262
|
+
const columnsPruneSlot = computeStartSlotAtEpoch(dataColumnSidecarsMinEpoch);
|
|
263
|
+
const prunedColumnSlots = await db.dataColumns.pruneBefore(columnsPruneSlot);
|
|
264
|
+
if (prunedColumnSlots.length > 0) {
|
|
257
265
|
logger.verbose("dataColumnSidecars prune", {
|
|
258
266
|
...logCtx,
|
|
259
|
-
slotRange: prettyPrintIndices(
|
|
260
|
-
numOfSlots:
|
|
261
|
-
totalNumOfSidecars: prefixedKeys.length,
|
|
267
|
+
slotRange: prettyPrintIndices(prunedColumnSlots),
|
|
268
|
+
numOfSlots: prunedColumnSlots.length,
|
|
262
269
|
});
|
|
263
270
|
} else {
|
|
264
|
-
logger.verbose(
|
|
271
|
+
logger.verbose("dataColumnSidecars prune: no entries before slot", {...logCtx, slot: columnsPruneSlot});
|
|
265
272
|
}
|
|
266
273
|
} else {
|
|
267
274
|
logger.verbose(
|
|
@@ -389,7 +396,6 @@ async function migrateBlobSidecarsFromHotToColdDb(
|
|
|
389
396
|
)
|
|
390
397
|
).filter((e): e is KeyValue<Slot, Uint8Array> => e !== null);
|
|
391
398
|
|
|
392
|
-
// put to blockArchive db and delete block db
|
|
393
399
|
await Promise.all([
|
|
394
400
|
db.blobSidecarsArchive.batchPutBinary(canonicalBlobSidecarsEntries),
|
|
395
401
|
db.blobSidecars.batchDelete(canonicalBlocks.map((block) => block.root)),
|
|
@@ -402,11 +408,9 @@ async function migrateBlobSidecarsFromHotToColdDb(
|
|
|
402
408
|
|
|
403
409
|
// TODO: This function can be simplified further by reducing layers of promises in a loop
|
|
404
410
|
/**
|
|
405
|
-
* Post-gloas the data columns of a Gloas block are tied to its execution payload envelope
|
|
406
|
-
*
|
|
407
|
-
* blocks only have a FULL variant, so the
|
|
408
|
-
* Blocks whose canonical variant is PENDING/EMPTY are skipped here — their columns will be picked
|
|
409
|
-
* up on a later run once the FULL variant appears in the ancestor walk.
|
|
411
|
+
* Post-gloas the data columns of a Gloas block are tied to its execution payload envelope.
|
|
412
|
+
* Columns only exist once the FULL variant of the block is in the proto-array. Pre-Gloas
|
|
413
|
+
* blocks only have a FULL variant, so the payload-status filter passes them all.
|
|
410
414
|
*/
|
|
411
415
|
async function migrateDataColumnSidecarsFromHotToColdDb(
|
|
412
416
|
config: ChainForkConfig,
|
|
@@ -428,32 +432,24 @@ async function migrateDataColumnSidecarsFromHotToColdDb(
|
|
|
428
432
|
if (batch.length === 0) break;
|
|
429
433
|
|
|
430
434
|
const promises: Promise<void>[] = [];
|
|
431
|
-
|
|
432
|
-
// load Buffer instead of ssz deserialized to improve performance
|
|
433
435
|
for (const block of batch) {
|
|
434
|
-
const
|
|
435
|
-
const blockEpoch = computeEpochAtSlot(blockSlot);
|
|
436
|
-
|
|
436
|
+
const blockEpoch = computeEpochAtSlot(block.slot);
|
|
437
437
|
if (
|
|
438
|
-
config.getForkSeq(
|
|
439
|
-
// if block is out of ${config.MIN_EPOCHS_FOR_DATA_COLUMN_SIDECARS_REQUESTS}, skip this step
|
|
438
|
+
config.getForkSeq(block.slot) < ForkSeq.fulu ||
|
|
440
439
|
blockEpoch < currentEpoch - config.MIN_EPOCHS_FOR_DATA_COLUMN_SIDECARS_REQUESTS
|
|
441
440
|
) {
|
|
442
441
|
continue;
|
|
443
442
|
}
|
|
444
443
|
|
|
445
|
-
// Here we assume the data column sidecars are already in the hot db
|
|
446
444
|
const dataColumnSidecarBytes = await fromAsync(db.dataColumnSidecar.valuesStreamBinary(block.root));
|
|
447
445
|
if (dataColumnSidecarBytes.length === 0) {
|
|
448
|
-
// Empty stream: either the block has no blobs, or columns were already archived on a
|
|
449
|
-
// previous run (boundary block). Nothing to migrate.
|
|
450
446
|
logger.debug("DataColumnSidecars in forkchoice but missing in hot db, could be already archived", {
|
|
451
447
|
slot: block.slot,
|
|
452
448
|
root: toRootHex(block.root),
|
|
453
449
|
});
|
|
454
450
|
continue;
|
|
455
451
|
}
|
|
456
|
-
logger.verbose("Migrated dataColumnSidecars for block", {
|
|
452
|
+
logger.verbose("Migrated legacy dataColumnSidecars for block", {
|
|
457
453
|
currentEpoch,
|
|
458
454
|
slot: block.slot,
|
|
459
455
|
root: toRootHex(block.root),
|
|
@@ -462,15 +458,14 @@ async function migrateDataColumnSidecarsFromHotToColdDb(
|
|
|
462
458
|
promises.push(
|
|
463
459
|
db.dataColumnSidecarArchive.putManyBinary(
|
|
464
460
|
block.slot,
|
|
465
|
-
dataColumnSidecarBytes.map((
|
|
461
|
+
dataColumnSidecarBytes.map((entry) => ({key: entry.id, value: entry.value}))
|
|
466
462
|
)
|
|
467
463
|
);
|
|
468
464
|
migratedSlots.push(block.slot);
|
|
469
465
|
}
|
|
470
466
|
|
|
471
|
-
promises.push(db.dataColumnSidecar.deleteMany(batch.map((block) => block.root)));
|
|
472
|
-
|
|
473
467
|
await Promise.all(promises);
|
|
468
|
+
await db.dataColumnSidecar.deleteMany(batch.map((block) => block.root));
|
|
474
469
|
}
|
|
475
470
|
|
|
476
471
|
// Ancestor walk is newest to oldest; sort ascending so `prettyPrintIndices` renders cleanly.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {ForkPostDeneb, isForkPostDeneb} from "@lodestar/params";
|
|
1
|
+
import {ForkPostDeneb, ForkPostFulu, isForkPostDeneb} from "@lodestar/params";
|
|
2
2
|
import {SignedBeaconBlock} from "@lodestar/types";
|
|
3
|
-
import {
|
|
3
|
+
import {toRootHex} from "@lodestar/utils";
|
|
4
4
|
import {getBlobKzgCommitments} from "../../util/dataColumns.js";
|
|
5
5
|
import {BeaconChain} from "../chain.js";
|
|
6
6
|
import {IBlockInput, IDataColumnsInput, isBlockInputBlobs, isBlockInputColumns} from "./blockInput/index.js";
|
|
@@ -76,7 +76,6 @@ async function writeBlockAndBlobsToDb(this: BeaconChain, blockInput: IBlockInput
|
|
|
76
76
|
*/
|
|
77
77
|
export async function writeDataColumnsToDb(this: BeaconChain, blockInput: IDataColumnsInput): Promise<void> {
|
|
78
78
|
const {slot, blockRootHex} = blockInput;
|
|
79
|
-
const blockRoot = fromHex(blockRootHex);
|
|
80
79
|
|
|
81
80
|
if (!blockInput.hasComputedAllData()) {
|
|
82
81
|
// Supernodes may only have a subset of the data columns by the time the block begins to be imported
|
|
@@ -89,25 +88,19 @@ export async function writeDataColumnsToDb(this: BeaconChain, blockInput: IDataC
|
|
|
89
88
|
|
|
90
89
|
const {custodyColumns} = this.custodyConfig;
|
|
91
90
|
const dataColumnSidecars = blockInput.getCustodyColumns();
|
|
91
|
+
const dataColumnSidecarType = this.config.getForkTypes<ForkPostFulu>(slot).DataColumnSidecar;
|
|
92
92
|
|
|
93
|
-
const
|
|
94
|
-
const nonbinaryPuts = [];
|
|
93
|
+
const binaryColumns: {index: number; data: Uint8Array}[] = [];
|
|
95
94
|
for (const dataColumnSidecar of dataColumnSidecars) {
|
|
96
|
-
// skip reserializing column if we already have it
|
|
97
95
|
const serialized = this.serializedCache.get(dataColumnSidecar);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
96
|
+
binaryColumns.push({
|
|
97
|
+
index: dataColumnSidecar.index,
|
|
98
|
+
data: serialized ?? dataColumnSidecarType.serialize(dataColumnSidecar),
|
|
99
|
+
});
|
|
103
100
|
}
|
|
101
|
+
await this.db.dataColumns.putManyBinary({slot, blockRoot: blockRootHex}, binaryColumns);
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
this.db.dataColumnSidecar.putManyBinary(blockRoot, binaryPuts),
|
|
107
|
-
this.db.dataColumnSidecar.putMany(blockRoot, nonbinaryPuts),
|
|
108
|
-
]);
|
|
109
|
-
|
|
110
|
-
this.logger.debug("Persisted dataColumnSidecars to hot DB", {
|
|
103
|
+
this.logger.debug("Persisted dataColumnSidecars", {
|
|
111
104
|
slot,
|
|
112
105
|
root: blockRootHex,
|
|
113
106
|
dataColumnSidecars: dataColumnSidecars.length,
|
package/src/chain/chain.ts
CHANGED
|
@@ -268,6 +268,7 @@ export class BeaconChain implements IBeaconChain {
|
|
|
268
268
|
db,
|
|
269
269
|
dbName,
|
|
270
270
|
dataDir,
|
|
271
|
+
dataColumnDir,
|
|
271
272
|
logger,
|
|
272
273
|
processShutdownCallback,
|
|
273
274
|
clock,
|
|
@@ -285,6 +286,7 @@ export class BeaconChain implements IBeaconChain {
|
|
|
285
286
|
db: IBeaconDb;
|
|
286
287
|
dbName: string;
|
|
287
288
|
dataDir: string;
|
|
289
|
+
dataColumnDir?: string;
|
|
288
290
|
logger: Logger;
|
|
289
291
|
processShutdownCallback: ProcessShutdownCallback;
|
|
290
292
|
/** Used for testing to supply fake clock */
|
|
@@ -505,7 +507,12 @@ export class BeaconChain implements IBeaconChain {
|
|
|
505
507
|
|
|
506
508
|
this.archiveStore = new ArchiveStore(
|
|
507
509
|
{db, chain: this, logger: logger as LoggerNode, metrics},
|
|
508
|
-
{
|
|
510
|
+
{
|
|
511
|
+
...opts,
|
|
512
|
+
dbName,
|
|
513
|
+
dataColumnDir: dataColumnDir ?? path.join(dataDir, "data_columns"),
|
|
514
|
+
anchorState: {finalizedCheckpoint: anchorState.finalizedCheckpoint},
|
|
515
|
+
},
|
|
509
516
|
signal
|
|
510
517
|
);
|
|
511
518
|
|
|
@@ -969,12 +976,15 @@ export class BeaconChain implements IBeaconChain {
|
|
|
969
976
|
|
|
970
977
|
async getDataColumnSidecars(blockSlot: Slot, blockRootHex: string): Promise<DataColumnSidecar[]> {
|
|
971
978
|
const fork = this.config.getForkName(blockSlot);
|
|
979
|
+
const sidecarsByIndex = new Map<number, DataColumnSidecar>();
|
|
972
980
|
|
|
973
981
|
if (isForkPostGloas(fork)) {
|
|
974
982
|
// After gloas, columns are tracked in PayloadEnvelopeInput
|
|
975
983
|
const payloadInput = this.seenPayloadEnvelopeInputCache.get(blockRootHex);
|
|
976
984
|
if (payloadInput) {
|
|
977
|
-
|
|
985
|
+
for (const sidecar of payloadInput.getAllColumns()) {
|
|
986
|
+
sidecarsByIndex.set(sidecar.index, sidecar);
|
|
987
|
+
}
|
|
978
988
|
}
|
|
979
989
|
} else {
|
|
980
990
|
// Before gloas, columns are tracked in BlockInput
|
|
@@ -983,16 +993,17 @@ export class BeaconChain implements IBeaconChain {
|
|
|
983
993
|
if (!isBlockInputColumns(blockInput)) {
|
|
984
994
|
throw new Error(`Expected block input to have columns: slot=${blockSlot} root=${blockRootHex}`);
|
|
985
995
|
}
|
|
986
|
-
|
|
996
|
+
for (const sidecar of blockInput.getAllColumns()) {
|
|
997
|
+
sidecarsByIndex.set(sidecar.index, sidecar);
|
|
998
|
+
}
|
|
987
999
|
}
|
|
988
1000
|
}
|
|
989
1001
|
|
|
990
|
-
const
|
|
991
|
-
|
|
992
|
-
return sidecarsUnfinalized;
|
|
1002
|
+
for (const sidecar of await this.db.dataColumns.getAll({slot: blockSlot, blockRoot: blockRootHex})) {
|
|
1003
|
+
if (!sidecarsByIndex.has(sidecar.index)) sidecarsByIndex.set(sidecar.index, sidecar);
|
|
993
1004
|
}
|
|
994
|
-
|
|
995
|
-
return
|
|
1005
|
+
|
|
1006
|
+
return [...sidecarsByIndex.values()].sort((a, b) => a.index - b.index);
|
|
996
1007
|
}
|
|
997
1008
|
|
|
998
1009
|
async getSerializedDataColumnSidecars(
|
|
@@ -1001,22 +1012,25 @@ export class BeaconChain implements IBeaconChain {
|
|
|
1001
1012
|
indices: number[]
|
|
1002
1013
|
): Promise<(Uint8Array | undefined)[]> {
|
|
1003
1014
|
const fork = this.config.getForkName(blockSlot);
|
|
1015
|
+
const dataColumnSidecars: (Uint8Array | undefined)[] = indices.map(() => undefined);
|
|
1004
1016
|
|
|
1005
1017
|
if (isForkPostGloas(fork)) {
|
|
1006
1018
|
// After gloas, columns are tracked in PayloadEnvelopeInput
|
|
1007
1019
|
const payloadInput = this.seenPayloadEnvelopeInputCache.get(blockRootHex);
|
|
1008
1020
|
if (payloadInput) {
|
|
1009
|
-
|
|
1021
|
+
for (let i = 0; i < indices.length; i++) {
|
|
1022
|
+
const index = indices[i];
|
|
1010
1023
|
const sidecar = payloadInput.getColumn(index);
|
|
1011
1024
|
if (!sidecar) {
|
|
1012
|
-
|
|
1025
|
+
continue;
|
|
1013
1026
|
}
|
|
1014
1027
|
const serialized = this.serializedCache.get(sidecar);
|
|
1015
1028
|
if (serialized) {
|
|
1016
|
-
|
|
1029
|
+
dataColumnSidecars[i] = serialized;
|
|
1030
|
+
} else {
|
|
1031
|
+
dataColumnSidecars[i] = sszTypesFor(fork as ForkPostGloas).DataColumnSidecar.serialize(sidecar);
|
|
1017
1032
|
}
|
|
1018
|
-
|
|
1019
|
-
});
|
|
1033
|
+
}
|
|
1020
1034
|
}
|
|
1021
1035
|
} else {
|
|
1022
1036
|
// Before gloas, columns are tracked in BlockInput
|
|
@@ -1025,26 +1039,42 @@ export class BeaconChain implements IBeaconChain {
|
|
|
1025
1039
|
if (!isBlockInputColumns(blockInput)) {
|
|
1026
1040
|
throw new Error(`Expected block input to have columns: slot=${blockSlot} root=${blockRootHex}`);
|
|
1027
1041
|
}
|
|
1028
|
-
|
|
1042
|
+
for (let i = 0; i < indices.length; i++) {
|
|
1043
|
+
const index = indices[i];
|
|
1029
1044
|
const sidecar = blockInput.getColumn(index);
|
|
1030
1045
|
if (!sidecar) {
|
|
1031
|
-
|
|
1046
|
+
continue;
|
|
1032
1047
|
}
|
|
1033
1048
|
const serialized = this.serializedCache.get(sidecar);
|
|
1034
1049
|
if (serialized) {
|
|
1035
|
-
|
|
1050
|
+
dataColumnSidecars[i] = serialized;
|
|
1051
|
+
} else {
|
|
1052
|
+
dataColumnSidecars[i] = sszTypesFor(blockInput.forkName as ForkPostFulu).DataColumnSidecar.serialize(
|
|
1053
|
+
sidecar
|
|
1054
|
+
);
|
|
1036
1055
|
}
|
|
1037
|
-
|
|
1038
|
-
});
|
|
1056
|
+
}
|
|
1039
1057
|
}
|
|
1040
1058
|
}
|
|
1041
1059
|
|
|
1042
|
-
const
|
|
1043
|
-
|
|
1044
|
-
|
|
1060
|
+
const missingPositions = dataColumnSidecars
|
|
1061
|
+
.map((sidecar, position) => (sidecar === undefined ? position : -1))
|
|
1062
|
+
.filter((position) => position !== -1);
|
|
1063
|
+
|
|
1064
|
+
if (missingPositions.length > 0) {
|
|
1065
|
+
const persistedSidecars = await this.db.dataColumns.getManyBinary(
|
|
1066
|
+
{slot: blockSlot, blockRoot: blockRootHex},
|
|
1067
|
+
missingPositions.map((position) => indices[position])
|
|
1068
|
+
);
|
|
1069
|
+
for (let i = 0; i < missingPositions.length; i++) {
|
|
1070
|
+
const sidecar = persistedSidecars[i];
|
|
1071
|
+
if (sidecar !== undefined) {
|
|
1072
|
+
dataColumnSidecars[missingPositions[i]] = sidecar;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1045
1075
|
}
|
|
1046
|
-
|
|
1047
|
-
return
|
|
1076
|
+
|
|
1077
|
+
return dataColumnSidecars;
|
|
1048
1078
|
}
|
|
1049
1079
|
|
|
1050
1080
|
async produceCommonBlockBody(blockAttributes: BlockAttributes): Promise<CommonBlockBody> {
|
package/src/db/beacon.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import {ChainForkConfig} from "@lodestar/config";
|
|
2
2
|
import {Db, LevelDbControllerMetrics, encodeKey} from "@lodestar/db";
|
|
3
|
+
import {Slot} from "@lodestar/types";
|
|
4
|
+
import {Logger} from "@lodestar/utils";
|
|
3
5
|
import {Bucket} from "./buckets.js";
|
|
6
|
+
import {type IDataColumnStore, LegacyDataColumnStore} from "./dataColumnStore.js";
|
|
7
|
+
import {FlatFileStore} from "./flatFileStore/flatFileStore.js";
|
|
8
|
+
import type {FlatFileStoreMetrics} from "./flatFileStore/metrics.js";
|
|
4
9
|
import {IBeaconDb} from "./interface.js";
|
|
5
10
|
import {CheckpointStateRepository} from "./repositories/checkpointState.js";
|
|
6
11
|
import {
|
|
@@ -24,9 +29,10 @@ import {
|
|
|
24
29
|
VoluntaryExitRepository,
|
|
25
30
|
} from "./repositories/index.js";
|
|
26
31
|
|
|
27
|
-
export type
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
export type BeaconDbOpts = {
|
|
33
|
+
dataColumnDir: string;
|
|
34
|
+
logger: Logger;
|
|
35
|
+
metrics?: FlatFileStoreMetrics | null;
|
|
30
36
|
};
|
|
31
37
|
|
|
32
38
|
export class BeaconDb implements IBeaconDb {
|
|
@@ -57,9 +63,14 @@ export class BeaconDb implements IBeaconDb {
|
|
|
57
63
|
|
|
58
64
|
backfilledRanges: BackfilledRanges;
|
|
59
65
|
|
|
66
|
+
readonly dataColumns: IDataColumnStore;
|
|
67
|
+
lastLegacyArchiveSlot: Slot | null = null;
|
|
68
|
+
private readonly flatFileStore: FlatFileStore;
|
|
69
|
+
|
|
60
70
|
constructor(
|
|
61
71
|
config: ChainForkConfig,
|
|
62
|
-
protected readonly db: Db
|
|
72
|
+
protected readonly db: Db,
|
|
73
|
+
opts: BeaconDbOpts
|
|
63
74
|
) {
|
|
64
75
|
// Warning: If code is ever run in the constructor, must change this stub to not extend 'packages/beacon-node/test/utils/stub/beaconDb.ts' -
|
|
65
76
|
this.block = new BlockRepository(config, db);
|
|
@@ -87,14 +98,30 @@ export class BeaconDb implements IBeaconDb {
|
|
|
87
98
|
this.syncCommitteeWitness = new SyncCommitteeWitnessRepository(config, db);
|
|
88
99
|
|
|
89
100
|
this.backfilledRanges = new BackfilledRanges(config, db);
|
|
101
|
+
|
|
102
|
+
this.flatFileStore = new FlatFileStore(opts.dataColumnDir, config, opts.logger, opts.metrics);
|
|
103
|
+
this.dataColumns = new LegacyDataColumnStore(
|
|
104
|
+
this.flatFileStore,
|
|
105
|
+
this.dataColumnSidecar,
|
|
106
|
+
this.dataColumnSidecarArchive,
|
|
107
|
+
this.blockArchive
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async init(): Promise<void> {
|
|
112
|
+
await this.flatFileStore.init();
|
|
113
|
+
const [lastLegacyKey] = await this.dataColumnSidecarArchive.keys({reverse: true, limit: 1});
|
|
114
|
+
this.lastLegacyArchiveSlot = lastLegacyKey?.prefix ?? null;
|
|
90
115
|
}
|
|
91
116
|
|
|
92
|
-
close(): Promise<void> {
|
|
117
|
+
async close(): Promise<void> {
|
|
118
|
+
await this.flatFileStore.close();
|
|
93
119
|
return this.db.close();
|
|
94
120
|
}
|
|
95
121
|
|
|
96
|
-
setMetrics(metrics: LevelDbControllerMetrics): void {
|
|
122
|
+
setMetrics(metrics: LevelDbControllerMetrics, flatFileStoreMetrics: FlatFileStoreMetrics | null = null): void {
|
|
97
123
|
this.db.setMetrics(metrics);
|
|
124
|
+
this.flatFileStore.setMetrics(flatFileStoreMetrics);
|
|
98
125
|
}
|
|
99
126
|
|
|
100
127
|
async pruneHotDb(): Promise<void> {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {ColumnIndex, DataColumnSidecar, RootHex, Slot} from "@lodestar/types";
|
|
2
|
+
import {fromHex} from "@lodestar/utils";
|
|
3
|
+
import type {IFlatFileStore} from "./flatFileStore/interface.js";
|
|
4
|
+
import type {
|
|
5
|
+
BlockArchiveRepository,
|
|
6
|
+
DataColumnSidecarArchiveRepository,
|
|
7
|
+
DataColumnSidecarRepository,
|
|
8
|
+
} from "./repositories/index.js";
|
|
9
|
+
|
|
10
|
+
export type DataColumnKey = {slot: Slot; blockRoot: RootHex};
|
|
11
|
+
export type IndexedDataColumnBytes = {index: ColumnIndex; data: Uint8Array};
|
|
12
|
+
|
|
13
|
+
type LegacyHotDataColumnStore = Pick<DataColumnSidecarRepository, "values" | "getManyBinary" | "deleteMany">;
|
|
14
|
+
type LegacyArchiveDataColumnStore = Pick<
|
|
15
|
+
DataColumnSidecarArchiveRepository,
|
|
16
|
+
"values" | "getManyBinary" | "keys" | "deleteMany"
|
|
17
|
+
>;
|
|
18
|
+
type BlockRootIndex = Pick<BlockArchiveRepository, "getSlotByRoot">;
|
|
19
|
+
|
|
20
|
+
export interface IDataColumnStore {
|
|
21
|
+
getAll(key: DataColumnKey): Promise<DataColumnSidecar[]>;
|
|
22
|
+
getManyBinary(key: DataColumnKey, indices: ColumnIndex[]): Promise<(Uint8Array | undefined)[]>;
|
|
23
|
+
putManyBinary(key: DataColumnKey, columns: IndexedDataColumnBytes[]): Promise<void>;
|
|
24
|
+
deleteMany(keys: DataColumnKey[]): Promise<void>;
|
|
25
|
+
pruneBefore(slot: Slot): Promise<Slot[]>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Bridges pre-flat-file LevelDB columns during the supported in-place upgrade window.
|
|
30
|
+
* Remove the legacy repositories after upgrades from the last pre-flat-file release are no longer supported.
|
|
31
|
+
*/
|
|
32
|
+
export class LegacyDataColumnStore implements IDataColumnStore {
|
|
33
|
+
constructor(
|
|
34
|
+
private readonly flatFiles: IFlatFileStore,
|
|
35
|
+
private readonly legacyHot: LegacyHotDataColumnStore,
|
|
36
|
+
private readonly legacyArchive: LegacyArchiveDataColumnStore,
|
|
37
|
+
private readonly blockArchive: BlockRootIndex
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
async getAll({slot, blockRoot}: DataColumnKey): Promise<DataColumnSidecar[]> {
|
|
41
|
+
const sidecars = await this.flatFiles.getDataColumns(slot, blockRoot);
|
|
42
|
+
if (sidecars.length > 0) return sidecars;
|
|
43
|
+
|
|
44
|
+
const sidecarsByIndex = new Map<ColumnIndex, DataColumnSidecar>();
|
|
45
|
+
const root = fromHex(blockRoot);
|
|
46
|
+
for (const sidecar of await this.legacyHot.values(root)) {
|
|
47
|
+
if (!sidecarsByIndex.has(sidecar.index)) sidecarsByIndex.set(sidecar.index, sidecar);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if ((await this.blockArchive.getSlotByRoot(root)) === slot) {
|
|
51
|
+
for (const sidecar of await this.legacyArchive.values(slot)) {
|
|
52
|
+
if (!sidecarsByIndex.has(sidecar.index)) sidecarsByIndex.set(sidecar.index, sidecar);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return [...sidecarsByIndex.values()].sort((a, b) => a.index - b.index);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async getManyBinary({slot, blockRoot}: DataColumnKey, indices: ColumnIndex[]): Promise<(Uint8Array | undefined)[]> {
|
|
60
|
+
const flatFileSidecars = await this.flatFiles.getDataColumnsBinary(slot, blockRoot, indices);
|
|
61
|
+
if (flatFileSidecars !== null) return flatFileSidecars;
|
|
62
|
+
if (indices.length === 0) return [];
|
|
63
|
+
|
|
64
|
+
const root = fromHex(blockRoot);
|
|
65
|
+
const result = await this.legacyHot.getManyBinary(root, indices);
|
|
66
|
+
|
|
67
|
+
const missingPositions = getMissingPositions(result);
|
|
68
|
+
if (missingPositions.length === 0 || (await this.blockArchive.getSlotByRoot(root)) !== slot) return result;
|
|
69
|
+
|
|
70
|
+
const archivedSidecars = await this.legacyArchive.getManyBinary(
|
|
71
|
+
slot,
|
|
72
|
+
missingPositions.map((position) => indices[position])
|
|
73
|
+
);
|
|
74
|
+
fillMissing(result, missingPositions, archivedSidecars);
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async putManyBinary({slot, blockRoot}: DataColumnKey, columns: IndexedDataColumnBytes[]): Promise<void> {
|
|
79
|
+
await this.flatFiles.putDataColumnsBinary(slot, blockRoot, columns);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async deleteMany(keys: DataColumnKey[]): Promise<void> {
|
|
83
|
+
await this.flatFiles.deleteMany(keys);
|
|
84
|
+
await this.legacyHot.deleteMany(keys.map(({blockRoot}) => fromHex(blockRoot)));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async pruneBefore(slot: Slot): Promise<Slot[]> {
|
|
88
|
+
const flatFileSlots = await this.flatFiles.pruneBefore(slot);
|
|
89
|
+
const prefixedKeys = await this.legacyArchive.keys({lt: {prefix: slot, id: 0}});
|
|
90
|
+
const slots = [...new Set(prefixedKeys.map(({prefix}) => prefix))];
|
|
91
|
+
if (slots.length > 0) await this.legacyArchive.deleteMany(slots);
|
|
92
|
+
return [...new Set([...flatFileSlots, ...slots])].sort((a, b) => a - b);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getMissingPositions(sidecars: (Uint8Array | undefined)[]): number[] {
|
|
97
|
+
const positions: number[] = [];
|
|
98
|
+
for (let i = 0; i < sidecars.length; i++) {
|
|
99
|
+
if (sidecars[i] === undefined) positions.push(i);
|
|
100
|
+
}
|
|
101
|
+
return positions;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function fillMissing(
|
|
105
|
+
result: (Uint8Array | undefined)[],
|
|
106
|
+
missingPositions: number[],
|
|
107
|
+
sidecars: (Uint8Array | null | undefined)[]
|
|
108
|
+
): void {
|
|
109
|
+
for (let i = 0; i < missingPositions.length; i++) {
|
|
110
|
+
const sidecar = sidecars[i];
|
|
111
|
+
if (sidecar != null) result[missingPositions[i]] = sidecar;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {mkdir, open, rename, rm} from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const pendingDirectorySyncs = new Map<string, string[]>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Atomic write: write to `.part` temp file -> datasync -> rename to target.
|
|
8
|
+
* This guarantees crash-safe writes — if the process crashes mid-write,
|
|
9
|
+
* only the `.part` file is corrupted, never the target.
|
|
10
|
+
*/
|
|
11
|
+
export async function atomicWrite(targetPath: string, data: Uint8Array): Promise<void> {
|
|
12
|
+
const dir = path.dirname(targetPath);
|
|
13
|
+
const partPath = `${targetPath}.part-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
14
|
+
await mkdirDurable(dir);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const fd = await open(partPath, "wx");
|
|
18
|
+
try {
|
|
19
|
+
await fd.writeFile(data);
|
|
20
|
+
await fd.datasync();
|
|
21
|
+
} finally {
|
|
22
|
+
await fd.close();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
await rename(partPath, targetPath);
|
|
26
|
+
|
|
27
|
+
// Ensure rename metadata is durable on crash.
|
|
28
|
+
await syncDirectory(dir);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
await rm(partPath, {force: true}).catch(() => {});
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function mkdirDurable(dir: string): Promise<void> {
|
|
36
|
+
const targetDir = path.resolve(dir);
|
|
37
|
+
let directoriesToSync = pendingDirectorySyncs.get(targetDir);
|
|
38
|
+
if (directoriesToSync === undefined) {
|
|
39
|
+
const firstCreatedDir = await mkdir(targetDir, {recursive: true});
|
|
40
|
+
if (firstCreatedDir === undefined) return;
|
|
41
|
+
|
|
42
|
+
let createdDir = path.resolve(firstCreatedDir);
|
|
43
|
+
directoriesToSync = [path.dirname(createdDir)];
|
|
44
|
+
while (createdDir !== targetDir) {
|
|
45
|
+
directoriesToSync.push(createdDir);
|
|
46
|
+
const [nextSegment] = path.relative(createdDir, targetDir).split(path.sep);
|
|
47
|
+
createdDir = path.join(createdDir, nextSegment);
|
|
48
|
+
}
|
|
49
|
+
pendingDirectorySyncs.set(targetDir, directoriesToSync);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const directory of directoriesToSync) {
|
|
53
|
+
await syncDirectory(directory);
|
|
54
|
+
}
|
|
55
|
+
pendingDirectorySyncs.delete(targetDir);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function syncDirectory(dir: string): Promise<void> {
|
|
59
|
+
const dirFd = await open(dir, "r");
|
|
60
|
+
try {
|
|
61
|
+
await dirFd.sync();
|
|
62
|
+
} finally {
|
|
63
|
+
await dirFd.close();
|
|
64
|
+
}
|
|
65
|
+
}
|