import {DbReqOpts, encodeKey} from "@lodestar/db"; import {Root, UintNum64, ssz} from "@lodestar/types"; import {Bucket, getBucketNameByValue} from "../buckets.js"; import {LodestarValidatorDatabaseController} from "../types.js"; const GENESIS_VALIDATORS_ROOT = Buffer.from("GENESIS_VALIDATORS_ROOT"); const GENESIS_TIME = Buffer.from("GENESIS_TIME"); /** * Store MetaData of validator. */ export class MetaDataRepository { protected bucket = Bucket.validator_metaData; private readonly bucketId = getBucketNameByValue(this.bucket); private readonly dbReqOpts: DbReqOpts = {bucketId: this.bucketId}; constructor(protected db: LodestarValidatorDatabaseController) { this.dbReqOpts = {bucketId: this.bucketId}; } async getGenesisValidatorsRoot(): Promise { return this.db.get(this.encodeKey(GENESIS_VALIDATORS_ROOT), this.dbReqOpts); } async setGenesisValidatorsRoot(genesisValidatorsRoot: Root): Promise { await this.db.put(this.encodeKey(GENESIS_VALIDATORS_ROOT), genesisValidatorsRoot, this.dbReqOpts); } async getGenesisTime(): Promise { const bytes = await this.db.get(this.encodeKey(GENESIS_TIME), this.dbReqOpts); return bytes ? ssz.UintNum64.deserialize(bytes) : null; } async setGenesisTime(genesisTime: UintNum64): Promise { await this.db.put(this.encodeKey(GENESIS_TIME), ssz.UintNum64.serialize(genesisTime), this.dbReqOpts); } private encodeKey(key: Uint8Array): Uint8Array { return encodeKey(this.bucket, key); } }