import { encodeKey } from "@lodestar/db";
import { Bucket } from "./buckets.js";
import { CheckpointStateRepository } from "./repositories/checkpointState.js";
import { AttesterSlashingRepository, BLSToExecutionChangeRepository, BackfilledRanges, BestLightClientUpdateRepository, BlobSidecarsArchiveRepository, BlobSidecarsRepository, BlockArchiveRepository, BlockRepository, CheckpointHeaderRepository, DataColumnSidecarArchiveRepository, DataColumnSidecarRepository, ExecutionPayloadEnvelopeArchiveRepository, ExecutionPayloadEnvelopeRepository, ProposerSlashingRepository, StateArchiveRepository, SyncCommitteeRepository, SyncCommitteeWitnessRepository, VoluntaryExitRepository, } from "./repositories/index.js";
export class BeaconDb {
    db;
    block;
    blockArchive;
    blobSidecars;
    blobSidecarsArchive;
    dataColumnSidecar;
    dataColumnSidecarArchive;
    executionPayloadEnvelope;
    executionPayloadEnvelopeArchive;
    stateArchive;
    checkpointState;
    voluntaryExit;
    proposerSlashing;
    attesterSlashing;
    blsToExecutionChange;
    // lightclient
    bestLightClientUpdate;
    checkpointHeader;
    syncCommittee;
    syncCommitteeWitness;
    backfilledRanges;
    constructor(config, db) {
        this.db = db;
        // Warning: If code is ever run in the constructor, must change this stub to not extend 'packages/beacon-node/test/utils/stub/beaconDb.ts' -
        this.block = new BlockRepository(config, db);
        this.blockArchive = new BlockArchiveRepository(config, db);
        this.blobSidecars = new BlobSidecarsRepository(config, db);
        this.blobSidecarsArchive = new BlobSidecarsArchiveRepository(config, db);
        this.dataColumnSidecar = new DataColumnSidecarRepository(config, db);
        this.dataColumnSidecarArchive = new DataColumnSidecarArchiveRepository(config, db);
        this.executionPayloadEnvelope = new ExecutionPayloadEnvelopeRepository(config, db);
        this.executionPayloadEnvelopeArchive = new ExecutionPayloadEnvelopeArchiveRepository(config, db);
        this.stateArchive = new StateArchiveRepository(config, db);
        this.checkpointState = new CheckpointStateRepository(config, db);
        this.voluntaryExit = new VoluntaryExitRepository(config, db);
        this.blsToExecutionChange = new BLSToExecutionChangeRepository(config, db);
        this.proposerSlashing = new ProposerSlashingRepository(config, db);
        this.attesterSlashing = new AttesterSlashingRepository(config, db);
        // lightclient
        this.bestLightClientUpdate = new BestLightClientUpdateRepository(config, db);
        this.checkpointHeader = new CheckpointHeaderRepository(config, db);
        this.syncCommittee = new SyncCommitteeRepository(config, db);
        this.syncCommitteeWitness = new SyncCommitteeWitnessRepository(config, db);
        this.backfilledRanges = new BackfilledRanges(config, db);
    }
    close() {
        return this.db.close();
    }
    setMetrics(metrics) {
        this.db.setMetrics(metrics);
    }
    async pruneHotDb() {
        // Prune all hot blobs
        await this.blobSidecars.batchDelete(await this.blobSidecars.keys());
        // Prune all hot blocks
        // TODO: Enable once it's deemed safe
        // await this.block.batchDelete(await this.block.keys());
    }
    async deleteDeprecatedEth1Data() {
        const deprecatedBuckets = [
            Bucket.phase0_eth1Data,
            Bucket.index_depositDataRoot,
            Bucket.phase0_depositData,
            Bucket.phase0_depositEvent,
            Bucket.phase0_preGenesisState,
            Bucket.phase0_preGenesisStateLastProcessedBlock,
        ];
        for (const bucket of deprecatedBuckets) {
            await this.deleteBucketData(bucket);
        }
    }
    async deleteBucketData(bucket) {
        const minKey = encodeKey(bucket, Buffer.alloc(0));
        const maxKey = encodeKey(bucket + 1, Buffer.alloc(0));
        // Batch delete to avoid loading all keys into memory at once
        const BATCH_DELETE_SIZE = 1000;
        let keysBatch = [];
        for await (const key of this.db.keysStream({ gte: minKey, lt: maxKey })) {
            keysBatch.push(key);
            if (keysBatch.length >= BATCH_DELETE_SIZE) {
                await this.db.batchDelete(keysBatch);
                keysBatch = [];
            }
        }
        if (keysBatch.length > 0) {
            await this.db.batchDelete(keysBatch);
        }
    }
}
//# sourceMappingURL=beacon.js.map