Skip to content

Commit

Permalink
Use block state slot to get fork type (#2440)
Browse files Browse the repository at this point in the history
* Use block and state slot to get ForkName and type

* Update consumers

* Update tests
  • Loading branch information
dapplion committed Apr 27, 2021
1 parent a4a9e1b commit caf0f4a
Show file tree
Hide file tree
Showing 26 changed files with 269 additions and 610 deletions.
12 changes: 4 additions & 8 deletions packages/db/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {BUCKET_LENGTH} from "./const";
export enum Bucket {
// beacon chain
// finalized states
phase0_stateArchive = 0, // Root -> phase0.BeaconState
allForks_stateArchive = 0, // Root -> phase0.BeaconState
// unfinalized blocks
phase0_block = 1, // Root -> phase0.SignedBeaconBlock
allForks_block = 1, // Root -> phase0.SignedBeaconBlock
// finalized blocks
phase0_blockArchive = 2, // Slot -> phase0.SignedBeaconBlock
allForks_blockArchive = 2, // Slot -> phase0.SignedBeaconBlock
// finalized block additional indices
index_blockArchiveParentRootIndex = 3, // parent Root -> Slot
index_blockArchiveRootIndex = 4, // Root -> Slot
Expand Down Expand Up @@ -45,14 +45,10 @@ export enum Bucket {
phase0_slashingProtectionAttestationLowerBound = 22,
index_slashingProtectionMinSpanDistance = 23,
index_slashingProtectionMaxSpanDistance = 24,
phase0_pendingBlock = 25, // Root -> SignedBeaconBlock
allForks_pendingBlock = 25, // Root -> SignedBeaconBlock

index_stateArchiveRootIndex = 26, // State Root -> slot

altair_block = 27, // Root -> altair.SignedBeaconBlock
altair_blockArchive = 28, // Slot -> altair.SignedBeaconBlock
altair_pendingBlock = 29, // Slot -> altair.SignedBeaconBlock
altair_stateArchive = 30, // Slot -> altair.BeaconState
altair_syncCommitteeSignature = 31, // Root => SyncCommitteeSignature
altair_contributionAndProof = 32, // Root => ContributionAndProof
}
Expand Down
4 changes: 2 additions & 2 deletions packages/lodestar/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class BeaconBlockApi implements IBeaconBlocksApi {
);
await Promise.all(
nonFinalizedBlockSummaries.map(async (summary) => {
const block = await this.db.block.get(summary.blockRoot, summary.slot);
const block = await this.db.block.get(summary.blockRoot);
if (block) {
const cannonical = this.chain.forkChoice.getCanonicalBlockSummaryAtSlot(block.message.slot);
if (cannonical) {
Expand Down Expand Up @@ -86,7 +86,7 @@ export class BeaconBlockApi implements IBeaconBlocksApi {
await Promise.all(
this.chain.forkChoice.getBlockSummariesAtSlot(filters.slot).map(async (summary) => {
if (!this.config.types.Root.equals(summary.blockRoot, canonicalRoot)) {
const block = await this.db.block.get(summary.blockRoot, summary.slot);
const block = await this.db.block.get(summary.blockRoot);
if (block) {
result.push(toBeaconHeaderResponse(this.config, block));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/lodestar/src/api/impl/beacon/blocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function resolveBlockIdOrNull(
blockId = blockId.toLowerCase();
if (blockId === "head") {
const head = forkChoice.getHead();
return db.block.get(head.blockRoot, head.slot);
return db.block.get(head.blockRoot);
}

if (blockId === "genesis") {
Expand All @@ -59,7 +59,7 @@ async function resolveBlockIdOrNull(
const root = fromHexString(blockId);
const summary = forkChoice.getBlock(root);
if (summary) {
return await db.block.get(summary.blockRoot, summary.slot);
return await db.block.get(summary.blockRoot);
} else {
return await db.blockArchive.getByRoot(root);
}
Expand All @@ -73,7 +73,7 @@ async function resolveBlockIdOrNull(

const blockSummary = forkChoice.getCanonicalBlockSummaryAtSlot(slot);
if (blockSummary) {
return db.block.get(blockSummary.blockRoot, blockSummary.slot);
return db.block.get(blockSummary.blockRoot);
} else {
return await db.blockArchive.get(slot);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/lodestar/src/chain/blocks/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,18 @@ export class BlockPool {
return Boolean(this.blocks.get(this.getBlockKeyByRoot(blockRoot)));
}

getByParent(parentRoot: Root): {root: Uint8Array; slot: Slot}[] {
getByParent(parentRoot: Root): Uint8Array[] {
const blockRoots = Array.from(this.blocksByParent.get(toHexString(parentRoot))?.values() ?? []);
return blockRoots.map((root) => ({
root: fromHexString(root),
slot: this.blocks.get(root)!.slot,
}));
return blockRoots.map((root) => fromHexString(root));
}

getBySlot(slot: Slot): {root: Uint8Array; slot: Slot}[] {
getBySlot(slot: Slot): Uint8Array[] {
const slots = Array.from(this.blocksBySlot.keys()).filter((cachedSlot) => cachedSlot <= slot);
const blockRoots: string[] = [];
for (const cachedSlot of slots) {
blockRoots.push(...Array.from(this.blocksBySlot.get(cachedSlot)?.values() ?? []));
}
return Array.from(new Set(blockRoots)).map((root) => ({
root: fromHexString(root),
slot: this.blocks.get(root)!.slot,
}));
return Array.from(new Set(blockRoots)).map((root) => fromHexString(root));
}

private getParentKey(block: phase0.SignedBeaconBlock): string {
Expand Down
6 changes: 3 additions & 3 deletions packages/lodestar/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class BeaconChain implements IBeaconChain {

async getHeadBlock(): Promise<allForks.SignedBeaconBlock | null> {
const headSummary = this.forkChoice.getHead();
const unfinalizedBlock = await this.db.block.get(headSummary.blockRoot, headSummary.slot);
const unfinalizedBlock = await this.db.block.get(headSummary.blockRoot);
if (unfinalizedBlock) {
return unfinalizedBlock;
}
Expand All @@ -187,7 +187,7 @@ export class BeaconChain implements IBeaconChain {
if (!summary) {
return null;
}
return await this.db.block.get(summary.blockRoot, summary.slot);
return await this.db.block.get(summary.blockRoot);
}

async getStateByBlockRoot(blockRoot: Root): Promise<CachedBeaconState<allForks.BeaconState> | null> {
Expand All @@ -214,7 +214,7 @@ export class BeaconChain implements IBeaconChain {
// these blocks are on the same chain to head
for (const summary of this.forkChoice.iterateBlockSummaries(this.forkChoice.getHeadRoot())) {
if (slotsSet.has(summary.slot)) {
blockRootsPerSlot.set(summary.slot, this.db.block.get(summary.blockRoot, summary.slot));
blockRootsPerSlot.set(summary.slot, this.db.block.get(summary.blockRoot));
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/lodestar/src/chain/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ export async function onClockSlot(this: BeaconChain, slot: Slot): Promise<void>
);

await Promise.all(
this.pendingBlocks.getBySlot(slot).map(async ({root, slot}) => {
const pendingBlock = await this.db.pendingBlock.get(root, slot);
this.pendingBlocks.getBySlot(slot).map(async (root) => {
const pendingBlock = await this.db.pendingBlock.get(root);
if (pendingBlock) {
this.pendingBlocks.remove(pendingBlock);
await this.db.pendingBlock.delete(root, slot);
await this.db.pendingBlock.delete(root);
return this.blockProcessor.processBlockJob({
signedBlock: pendingBlock,
reprocess: false,
Expand Down Expand Up @@ -244,11 +244,11 @@ export async function onBlock(
if (!job.reprocess) {
await this.db.processBlockOperations(block);
await Promise.all(
this.pendingBlocks.getByParent(blockRoot).map(async ({root, slot}) => {
const pendingBlock = await this.db.pendingBlock.get(root, slot);
this.pendingBlocks.getByParent(blockRoot).map(async (root) => {
const pendingBlock = await this.db.pendingBlock.get(root);
if (pendingBlock) {
this.pendingBlocks.remove(pendingBlock);
await this.db.pendingBlock.delete(root, slot);
await this.db.pendingBlock.delete(root);
return this.blockProcessor.processBlockJob({
signedBlock: pendingBlock,
reprocess: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/chain/regen/regen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class StateRegenerator implements IStateRegenerator {
}

for (const b of blocksToReplay.reverse()) {
const structBlock = (await this.db.block.get(b.blockRoot, b.slot))!;
const structBlock = (await this.db.block.get(b.blockRoot))!;
const block = this.config.getTypes(b.slot).SignedBeaconBlock.createTreeBackedFromStruct(structBlock);
if (!block) {
throw new RegenError({
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/chain/validation/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function validateGossipBlock(
});
}

const existingBlock = await db.block.get(blockRoot, block.message.slot);
const existingBlock = await db.block.get(blockRoot);
if (existingBlock?.message.proposerIndex === block.message.proposerIndex) {
throw new BlockError({
code: BlockErrorCode.REPEAT_PROPOSAL,
Expand Down
31 changes: 31 additions & 0 deletions packages/lodestar/src/db/repositories/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {Bucket, IDatabaseController, Repository} from "@chainsafe/lodestar-db";
import {allForks} from "@chainsafe/lodestar-types";
import {getBlockTypeFromBlock, getBlockTypeFromBytes} from "./utils/multifork";

/**
* Blocks by root
*
* Used to store unfinalized blocks
*/
export class BlockRepository extends Repository<Uint8Array, allForks.SignedBeaconBlock> {
constructor(config: IBeaconConfig, db: IDatabaseController<Buffer, Buffer>) {
const type = config.types.phase0.SignedBeaconBlock; // Pick some type but won't be used
super(config, db, Bucket.allForks_block, type);
}

/**
* Id is hashTreeRoot of unsigned BeaconBlock
*/
getId(value: allForks.SignedBeaconBlock): Uint8Array {
return getBlockTypeFromBlock(this.config, value).fields["message"].hashTreeRoot(value.message);
}

encodeValue(value: allForks.SignedBeaconBlock): Buffer {
return getBlockTypeFromBlock(this.config, value).serialize(value) as Buffer;
}

decodeValue(data: Buffer): allForks.SignedBeaconBlock {
return getBlockTypeFromBytes(this.config, data).deserialize(data);
}
}
30 changes: 0 additions & 30 deletions packages/lodestar/src/db/repositories/block/abstract.ts

This file was deleted.

84 changes: 0 additions & 84 deletions packages/lodestar/src/db/repositories/block/index.ts

This file was deleted.

0 comments on commit caf0f4a

Please sign in to comment.