Skip to content

Commit

Permalink
Implement blobsSidecar pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Nov 17, 2022
1 parent c90f0fb commit e16a699
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
24 changes: 21 additions & 3 deletions packages/beacon-node/src/chain/archiver/archiveBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {fromHexString} from "@chainsafe/ssz";
import {Epoch, Slot} from "@lodestar/types";
import {IForkChoice} from "@lodestar/fork-choice";
import {ILogger, toHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {computeEpochAtSlot} from "@lodestar/state-transition";
import {ForkSeq, MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS, SLOTS_PER_EPOCH} from "@lodestar/params";
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {IKeyValue} from "@lodestar/db";
import {IChainForkConfig} from "@lodestar/config";
import {IBeaconDb} from "../../db/index.js";
Expand Down Expand Up @@ -33,7 +33,8 @@ export async function archiveBlocks(
forkChoice: IForkChoice,
lightclientServer: LightClientServer,
logger: ILogger,
finalizedCheckpoint: {rootHex: string; epoch: Epoch}
finalizedCheckpoint: {rootHex: string; epoch: Epoch},
currentEpoch: Epoch
): Promise<void> {
// Use fork choice to determine the blocks to archive and delete
const finalizedCanonicalBlocks = forkChoice.getAllAncestorBlocks(finalizedCheckpoint.rootHex);
Expand Down Expand Up @@ -77,6 +78,23 @@ export async function archiveBlocks(
}
}

// Delete expired blobs
// Keep only `[max(GENESIS_EPOCH, current_epoch - MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS), current_epoch]`
if (finalizedPostEIP4844) {
const blobsSidecarMinEpoch = currentEpoch - MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS;
if (blobsSidecarMinEpoch > 0) {
const slotsToDelete = await db.blobsSidecarArchive.keys({lt: computeStartSlotAtEpoch(blobsSidecarMinEpoch)});
if (slotsToDelete.length > 0) {
await db.blobsSidecarArchive.batchDelete(slotsToDelete);
logger.verbose(
`blobsSidecar prune: batchDelete range ${slotsToDelete[0]}..${slotsToDelete[slotsToDelete.length - 1]}`
);
} else {
logger.verbose(`blobsSidecar prune: no entries before epoch ${blobsSidecarMinEpoch}`);
}
}
}

// Prunning potential checkpoint data
const finalizedCanonicalNonCheckpointBlocks = getNonCheckpointBlocks(finalizedCanonicalBlockRoots);
const nonCheckpointBlockRoots: Uint8Array[] = [...nonCanonicalBlockRoots];
Expand Down
10 changes: 9 additions & 1 deletion packages/beacon-node/src/chain/archiver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ export class Archiver {
try {
const finalizedEpoch = finalized.epoch;
this.logger.verbose("Start processing finalized checkpoint", {epoch: finalizedEpoch});
await archiveBlocks(this.db, this.chain.forkChoice, this.chain.lightClientServer, this.logger, finalized);
await archiveBlocks(
this.chain.config,
this.db,
this.chain.forkChoice,
this.chain.lightClientServer,
this.logger,
finalized,
this.chain.clock.currentEpoch
);

// should be after ArchiveBlocksTask to handle restart cleanly
await this.statesArchiver.maybeArchiveState(finalized);
Expand Down
6 changes: 6 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,9 @@ export const MAX_REQUEST_LIGHT_CLIENT_COMMITTEE_HASHES = 128;
*/
export const SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY = 128;
export const INTERVALS_PER_SLOT = 3;

// EIP-4844
// https://github.com/ethereum/consensus-specs/blob/11a037fd9227e29ee809c9397b09f8cc3383a8c0/specs/eip4844/p2p-interface.md#configuration
/** The minimum epoch range over which a node must serve blobs sidecars */
export const MIN_EPOCHS_FOR_BLOBS_SIDECARS_REQUESTS = 4096;
export const MAX_REQUEST_BLOBS_SIDECARS = 128;

0 comments on commit e16a699

Please sign in to comment.