Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2377 from ChainSafe/tuyen/sync-committee-repos
Altair: Database for SyncCommittee
- Loading branch information
Showing
6 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/lodestar/src/db/repositories/contributionAndProof.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {computeStartSlotAtEpoch} from "@chainsafe/lodestar-beacon-state-transition"; | ||
import {IBeaconConfig} from "@chainsafe/lodestar-config"; | ||
import {Bucket, IDatabaseController, Repository} from "@chainsafe/lodestar-db"; | ||
import {altair, phase0} from "@chainsafe/lodestar-types"; | ||
|
||
/** | ||
* Repository for ContributionAndProof. | ||
* Added via gossip or api. | ||
* Removed when it's old. | ||
*/ | ||
export class ContributionAndProofRepository extends Repository<Uint8Array, altair.ContributionAndProof> { | ||
constructor(config: IBeaconConfig, db: IDatabaseController<Buffer, Buffer>) { | ||
super(config, db, Bucket.altair_contributionAndProof, config.types.altair.ContributionAndProof); | ||
} | ||
|
||
/** | ||
* Id is hashTreeRoot of SyncCommitteeContribution | ||
*/ | ||
getId(value: altair.ContributionAndProof): Uint8Array { | ||
return this.config.types.altair.SyncCommitteeContribution.hashTreeRoot(value.contribution); | ||
} | ||
|
||
async pruneFinalized(finalizedEpoch: phase0.Epoch): Promise<void> { | ||
const finalizedEpochStartSlot = computeStartSlotAtEpoch(this.config, finalizedEpoch); | ||
const entries = await this.entries(); | ||
const idsToDelete = entries.filter((e) => e.value.contribution.slot < finalizedEpochStartSlot).map((e) => e.key); | ||
await this.batchDelete(idsToDelete); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/lodestar/src/db/repositories/syncCommitteeSignature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {computeStartSlotAtEpoch} from "@chainsafe/lodestar-beacon-state-transition"; | ||
import {IBeaconConfig} from "@chainsafe/lodestar-config"; | ||
import {Bucket, IDatabaseController, Repository} from "@chainsafe/lodestar-db"; | ||
import {fromHexString, toHexString} from "@chainsafe/ssz"; | ||
import {altair, phase0} from "@chainsafe/lodestar-types"; | ||
import {notNullish} from "@chainsafe/lodestar-utils"; | ||
|
||
/** | ||
* Repository for SyncCommitteeSignature. | ||
* Added via gossip or api. | ||
* Removed when it's old. | ||
*/ | ||
export class SyncCommitteeSignatureRepository extends Repository<Uint8Array, altair.SyncCommitteeSignature> { | ||
// index to produce SyncCommitteeContribution | ||
// for each slot, index SyncCommitteeSignature roots by block root hex | ||
private contributionIndex = new Map<phase0.Slot, Map<string, string[]>>(); | ||
constructor(config: IBeaconConfig, db: IDatabaseController<Buffer, Buffer>) { | ||
super(config, db, Bucket.altair_syncCommitteeSignature, config.types.altair.SyncCommitteeSignature); | ||
} | ||
|
||
async put(key: Uint8Array, value: altair.SyncCommitteeSignature): Promise<void> { | ||
const blockRootHex = toHexString(value.beaconBlockRoot); | ||
const slot = value.slot; | ||
const rootHex = toHexString(key); | ||
let slotIndex = this.contributionIndex.get(slot); | ||
if (!slotIndex) { | ||
slotIndex = new Map(); | ||
this.contributionIndex.set(slot, slotIndex); | ||
} | ||
if (!slotIndex.get(blockRootHex)) { | ||
slotIndex.set(blockRootHex, [rootHex]); | ||
} else { | ||
const rootHexes = new Set([rootHex, ...(slotIndex.get(blockRootHex) || [])]); | ||
slotIndex.set(blockRootHex, Array.from(rootHexes)); | ||
} | ||
await super.put(key, value); | ||
} | ||
|
||
async getByBlock(slot: phase0.Slot, blockRootHex: phase0.Root): Promise<altair.SyncCommitteeSignature[]> { | ||
const rootsByBlockRoot = this.contributionIndex.get(slot); | ||
if (!rootsByBlockRoot) return []; | ||
const rootHexes = rootsByBlockRoot.get(toHexString(blockRootHex)); | ||
if (!rootHexes) return []; | ||
const syncCommitteeSignatures = await Promise.all(rootHexes.map((rootHex) => this.get(fromHexString(rootHex)))); | ||
return syncCommitteeSignatures.filter(notNullish); | ||
} | ||
|
||
async pruneFinalized(finalizedEpoch: phase0.Epoch): Promise<void> { | ||
const finalizedEpochStartSlot = computeStartSlotAtEpoch(this.config, finalizedEpoch); | ||
const entries = await this.entries(); | ||
const idsToDelete = entries.filter((e) => e.value.slot < finalizedEpochStartSlot).map((e) => e.key); | ||
await this.batchDelete(idsToDelete); | ||
for (const slot of this.contributionIndex.keys()) { | ||
if (slot < finalizedEpochStartSlot) this.contributionIndex.delete(slot); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters