UNPKG

@lodestar/beacon-node

Version:

A Typescript implementation of the beacon chain

55 lines 2.08 kB
import { Repository } from "@lodestar/db"; import { ssz } from "@lodestar/types"; import { bytesToInt, toHex } from "@lodestar/utils"; import { getStateTypeFromBytes } from "../../util/multifork.js"; import { Bucket, getBucketNameByValue } from "../buckets.js"; import { getRootIndexKey, storeRootIndex } from "./stateArchiveIndex.js"; export class StateArchiveRepository extends Repository { constructor(config, db) { // Pick some type but won't be used. Casted to any because no type can match `BeaconStateAllForks` // biome-ignore lint/suspicious/noExplicitAny: <explanation> const type = ssz.phase0.BeaconState; const bucket = Bucket.allForks_stateArchive; super(config, db, bucket, type, getBucketNameByValue(bucket)); } // Overrides for multi-fork encodeValue(value) { return value.serialize(); } decodeValue(data) { return getStateTypeFromBytes(this.config, data).deserializeToViewDU(data); } // Handle key as slot async put(key, value) { await Promise.all([super.put(key, value), storeRootIndex(this.db, key, value.hashTreeRoot())]); } getId(state) { return state.slot; } decodeKey(data) { return bytesToInt(super.decodeKey(data), "be"); } // Index Root -> Slot async getByRoot(stateRoot) { const slot = await this.getSlotByRoot(stateRoot); if (slot !== null && Number.isInteger(slot)) { return this.get(slot); } return null; } async dumpRootIndexEntries() { const entries = await this.db.entries({ lte: getRootIndexKey(Buffer.alloc(32, 0xff)), gte: getRootIndexKey(Buffer.alloc(32, 0x00)), }); return entries.map((entry) => ({ root: toHex(entry.key), slot: bytesToInt(entry.value, "be"), })); } async getSlotByRoot(root) { const value = await this.db.get(getRootIndexKey(root)); return value && bytesToInt(value, "be"); } } //# sourceMappingURL=stateArchive.js.map