Skip to content

Commit

Permalink
fix: edge case for GENESIS_SLOT as post-altair block
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil authored and g11tech committed Oct 30, 2023
1 parent 9a60b32 commit adfa2f5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
13 changes: 4 additions & 9 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {Logger, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {ForkSeq, SLOTS_PER_EPOCH, GENESIS_SLOT} from "@lodestar/params";

import {toHexString} from "@lodestar/utils";
import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
Expand All @@ -43,7 +43,6 @@ import {isOptimisticBlock} from "../util/forkChoice.js";
import {
blindedOrFullBlockToFull,
deserializeFullOrBlindedSignedBeaconBlock,
getEth1BlockHashFromSerializedBlock,
serializeFullOrBlindedSignedBeaconBlock,
} from "../util/fullOrBlindedBlock.js";
import {ExecutionPayloadBody} from "../execution/engine/types.js";
Expand Down Expand Up @@ -470,6 +469,7 @@ export class BeaconChain implements IBeaconChain {
}

async blindedOrFullBlockToFull(block: allForks.FullOrBlindedSignedBeaconBlock): Promise<allForks.SignedBeaconBlock> {
if (block.message.slot === GENESIS_SLOT) return block;
const info = this.config.getForkInfo(block.message.slot);
return blindedOrFullBlockToFull(
this.config,
Expand All @@ -479,15 +479,10 @@ export class BeaconChain implements IBeaconChain {
);
}

async blindedOrFullBlockToFullBytes(forkSeq: ForkSeq, block: Uint8Array): Promise<Uint8Array> {
async blindedOrFullBlockToFullBytes(block: Uint8Array): Promise<Uint8Array> {
return serializeFullOrBlindedSignedBeaconBlock(
this.config,
blindedOrFullBlockToFull(
this.config,
forkSeq,
deserializeFullOrBlindedSignedBeaconBlock(this.config, block),
await this.getTransactionsAndWithdrawals(forkSeq, toHexString(getEth1BlockHashFromSerializedBlock(block)))
)
await this.blindedOrFullBlockToFull(deserializeFullOrBlindedSignedBeaconBlock(this.config, block))
);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {BeaconConfig} from "@lodestar/config";
import {Logger} from "@lodestar/utils";

import {IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {ForkSeq} from "@lodestar/params";
import {IEth1ForBlockProduction} from "../eth1/index.js";
import {IExecutionEngine, IExecutionBuilder} from "../execution/index.js";
import {Metrics} from "../metrics/metrics.js";
Expand Down Expand Up @@ -145,7 +144,7 @@ export interface IBeaconChain {
): Promise<{block: allForks.BlindedBeaconBlock; executionPayloadValue: Wei}>;

blindedOrFullBlockToFull(block: allForks.FullOrBlindedSignedBeaconBlock): Promise<allForks.SignedBeaconBlock>;
blindedOrFullBlockToFullBytes(forkSeq: ForkSeq, block: Uint8Array): Promise<Uint8Array>;
blindedOrFullBlockToFullBytes(block: Uint8Array): Promise<Uint8Array>;

/** Process a block until complete */
processBlock(block: BlockInput, opts?: ImportBlockOpts): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export async function* onBeaconBlocksByRange(
if (startSlot <= finalizedSlot) {
// Chain of blobs won't change
for await (const {key, value} of finalized.binaryEntriesStream({gte: startSlot, lt: endSlot})) {
const {name, seq} = chain.config.getForkInfo(finalized.decodeKey(key));
yield {
data: await chain.blindedOrFullBlockToFullBytes(seq, value),
fork: name,
data: await chain.blindedOrFullBlockToFullBytes(value),
fork: chain.config.getForkName(finalized.decodeKey(key)),
};
}
}
Expand Down Expand Up @@ -55,10 +54,9 @@ export async function* onBeaconBlocksByRange(
throw new ResponseError(RespStatus.SERVER_ERROR, `No item for root ${block.blockRoot} slot ${block.slot}`);
}

const {name, seq} = chain.config.getForkInfo(block.slot);
yield {
data: await chain.blindedOrFullBlockToFullBytes(seq, blockBytes),
fork: name,
data: await chain.blindedOrFullBlockToFullBytes(blockBytes),
fork: chain.config.getForkName(block.slot),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ export async function* onBeaconBlocksByRoot(
slot = slotFromBytes;
}

const {name, seq} = chain.config.getForkInfo(slot);

yield {
data: await chain.blindedOrFullBlockToFullBytes(seq, blockBytes),
fork: name,
data: await chain.blindedOrFullBlockToFullBytes(blockBytes),
fork: chain.config.getForkName(slot),
};
}
}
Expand Down
12 changes: 2 additions & 10 deletions packages/beacon-node/src/util/fullOrBlindedBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {allForks, bellatrix, capella, deneb} from "@lodestar/types";
import {BYTES_PER_LOGS_BLOOM, ForkSeq, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
import {executionPayloadToPayloadHeader} from "@lodestar/state-transition";
import {ExecutionPayloadBody} from "../execution/engine/types.js";
import {ROOT_SIZE, getSlotFromSignedBeaconBlockSerialized} from "./sszBytes.js";
import {getSlotFromSignedBeaconBlockSerialized} from "./sszBytes.js";

/**
* * class SignedBeaconBlock(Container):
Expand Down Expand Up @@ -49,15 +49,7 @@ const BEACON_BLOCK_FIXED_LENGTH = 8 + 8 + 32 + 32 + 4;
* Deneb:
* blobKzgCommitments [offset - 4 bytes]
*/

const LOCATION_OF_ETH1_BLOCK_HASH = 96 + 32 + 8;
export function getEth1BlockHashFromSerializedBlock(block: Uint8Array): Uint8Array {
const firstByte = SIGNED_BEACON_BLOCK_FIXED_LENGTH + BEACON_BLOCK_FIXED_LENGTH + LOCATION_OF_ETH1_BLOCK_HASH;
return block.slice(firstByte, firstByte + ROOT_SIZE);
}

const LOCATION_OF_EXECUTION_PAYLOAD_OFFSET =
LOCATION_OF_ETH1_BLOCK_HASH + 32 + 32 + 4 + 4 + 4 + 4 + 4 + SYNC_COMMITTEE_SIZE / 8 + 96;
const LOCATION_OF_EXECUTION_PAYLOAD_OFFSET = 96 + 32 + 8 + 32 + 32 + 4 + 4 + 4 + 4 + 4 + SYNC_COMMITTEE_SIZE / 8 + 96;

/**
* class ExecutionPayload(Container) or class ExecutionPayloadHeader(Container)
Expand Down

0 comments on commit adfa2f5

Please sign in to comment.