Skip to content

Commit

Permalink
feat: add getFull to chain.getBlockBy*
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Sep 16, 2024
1 parent dea3b7d commit 357968a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
38 changes: 32 additions & 6 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,10 @@ export class BeaconChain implements IBeaconChain {
return null;
}

async getCanonicalBlockAtSlot(slot: Slot): Promise<{
async getCanonicalBlockAtSlot(
slot: Slot,
getFull = true
): Promise<{
block: SignedBeaconBlock | SignedBlindedBeaconBlock;
executionOptimistic: boolean;
finalized: boolean;
Expand All @@ -576,7 +579,11 @@ export class BeaconChain implements IBeaconChain {
if (block) {
const data = await this.db.block.get(fromHex(block.blockRoot));
if (data) {
return {block: data, executionOptimistic: isOptimisticBlock(block), finalized: false};
return {
block: getFull ? await this.fullOrBlindedSignedBeaconBlockToFull(data) : data,
executionOptimistic: isOptimisticBlock(block),
finalized: false,
};
}
}
// A non-finalized slot expected to be found in the hot db, could be archived during
Expand All @@ -585,10 +592,19 @@ export class BeaconChain implements IBeaconChain {
}

const data = await this.db.blockArchive.get(slot);
return data && {block: data, executionOptimistic: false, finalized: true};
return (
data && {
block: getFull ? await this.fullOrBlindedSignedBeaconBlockToFull(data) : data,
executionOptimistic: false,
finalized: true,
}
);
}

async getBlockByRoot(root: string): Promise<{
async getBlockByRoot(
root: string,
getFull = true
): Promise<{
block: SignedBeaconBlock | SignedBlindedBeaconBlock;
executionOptimistic: boolean;
finalized: boolean;
Expand All @@ -597,14 +613,24 @@ export class BeaconChain implements IBeaconChain {
if (block) {
const data = await this.db.block.get(fromHex(root));
if (data) {
return {block: data, executionOptimistic: isOptimisticBlock(block), finalized: false};
return {
block: getFull ? await this.fullOrBlindedSignedBeaconBlockToFull(data) : data,
executionOptimistic: isOptimisticBlock(block),
finalized: false,
};
}
// If block is not found in hot db, try cold db since there could be an archive cycle happening
// TODO: Add a lock to the archiver to have deterministic behavior on where are blocks
}

const data = await this.db.blockArchive.getByRoot(fromHex(root));
return data && {block: data, executionOptimistic: false, finalized: true};
return (
data && {
block: getFull ? await this.fullOrBlindedSignedBeaconBlockToFull(data) : data,
executionOptimistic: false,
finalized: true,
}
);
}

async produceCommonBlockBody(blockAttributes: BlockAttributes): Promise<CommonBlockBody> {
Expand Down
10 changes: 8 additions & 2 deletions packages/beacon-node/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,21 @@ export interface IBeaconChain {
* this methods returns blocks in current chain head according to
* forkchoice. Works for finalized slots as well
*/
getCanonicalBlockAtSlot(slot: Slot): Promise<{
getCanonicalBlockAtSlot(
slot: Slot,
getFull?: boolean
): Promise<{
block: SignedBeaconBlock | SignedBlindedBeaconBlock;
executionOptimistic: boolean;
finalized: boolean;
} | null>;
/**
* Get local block by root, does not fetch from the network
*/
getBlockByRoot(root: RootHex): Promise<{
getBlockByRoot(
root: RootHex,
getFull?: boolean
): Promise<{
block: SignedBeaconBlock | SignedBlindedBeaconBlock;
executionOptimistic: boolean;
finalized: boolean;
Expand Down

0 comments on commit 357968a

Please sign in to comment.