Skip to content

Commit adfa2f5

Browse files
matthewkeilg11tech
authored andcommitted
fix: edge case for GENESIS_SLOT as post-altair block
1 parent 9a60b32 commit adfa2f5

File tree

5 files changed

+13
-31
lines changed

5 files changed

+13
-31
lines changed

packages/beacon-node/src/chain/chain.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
3030
import {ProcessShutdownCallback} from "@lodestar/validator";
3131
import {Logger, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
32-
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
32+
import {ForkSeq, SLOTS_PER_EPOCH, GENESIS_SLOT} from "@lodestar/params";
3333

3434
import {toHexString} from "@lodestar/utils";
3535
import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
@@ -43,7 +43,6 @@ import {isOptimisticBlock} from "../util/forkChoice.js";
4343
import {
4444
blindedOrFullBlockToFull,
4545
deserializeFullOrBlindedSignedBeaconBlock,
46-
getEth1BlockHashFromSerializedBlock,
4746
serializeFullOrBlindedSignedBeaconBlock,
4847
} from "../util/fullOrBlindedBlock.js";
4948
import {ExecutionPayloadBody} from "../execution/engine/types.js";
@@ -470,6 +469,7 @@ export class BeaconChain implements IBeaconChain {
470469
}
471470

472471
async blindedOrFullBlockToFull(block: allForks.FullOrBlindedSignedBeaconBlock): Promise<allForks.SignedBeaconBlock> {
472+
if (block.message.slot === GENESIS_SLOT) return block;
473473
const info = this.config.getForkInfo(block.message.slot);
474474
return blindedOrFullBlockToFull(
475475
this.config,
@@ -479,15 +479,10 @@ export class BeaconChain implements IBeaconChain {
479479
);
480480
}
481481

482-
async blindedOrFullBlockToFullBytes(forkSeq: ForkSeq, block: Uint8Array): Promise<Uint8Array> {
482+
async blindedOrFullBlockToFullBytes(block: Uint8Array): Promise<Uint8Array> {
483483
return serializeFullOrBlindedSignedBeaconBlock(
484484
this.config,
485-
blindedOrFullBlockToFull(
486-
this.config,
487-
forkSeq,
488-
deserializeFullOrBlindedSignedBeaconBlock(this.config, block),
489-
await this.getTransactionsAndWithdrawals(forkSeq, toHexString(getEth1BlockHashFromSerializedBlock(block)))
490-
)
485+
await this.blindedOrFullBlockToFull(deserializeFullOrBlindedSignedBeaconBlock(this.config, block))
491486
);
492487
}
493488

packages/beacon-node/src/chain/interface.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {BeaconConfig} from "@lodestar/config";
1010
import {Logger} from "@lodestar/utils";
1111

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

147146
blindedOrFullBlockToFull(block: allForks.FullOrBlindedSignedBeaconBlock): Promise<allForks.SignedBeaconBlock>;
148-
blindedOrFullBlockToFullBytes(forkSeq: ForkSeq, block: Uint8Array): Promise<Uint8Array>;
147+
blindedOrFullBlockToFullBytes(block: Uint8Array): Promise<Uint8Array>;
149148

150149
/** Process a block until complete */
151150
processBlock(block: BlockInput, opts?: ImportBlockOpts): Promise<void>;

packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRange.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ export async function* onBeaconBlocksByRange(
2323
if (startSlot <= finalizedSlot) {
2424
// Chain of blobs won't change
2525
for await (const {key, value} of finalized.binaryEntriesStream({gte: startSlot, lt: endSlot})) {
26-
const {name, seq} = chain.config.getForkInfo(finalized.decodeKey(key));
2726
yield {
28-
data: await chain.blindedOrFullBlockToFullBytes(seq, value),
29-
fork: name,
27+
data: await chain.blindedOrFullBlockToFullBytes(value),
28+
fork: chain.config.getForkName(finalized.decodeKey(key)),
3029
};
3130
}
3231
}
@@ -55,10 +54,9 @@ export async function* onBeaconBlocksByRange(
5554
throw new ResponseError(RespStatus.SERVER_ERROR, `No item for root ${block.blockRoot} slot ${block.slot}`);
5655
}
5756

58-
const {name, seq} = chain.config.getForkInfo(block.slot);
5957
yield {
60-
data: await chain.blindedOrFullBlockToFullBytes(seq, blockBytes),
61-
fork: name,
58+
data: await chain.blindedOrFullBlockToFullBytes(blockBytes),
59+
fork: chain.config.getForkName(block.slot),
6260
};
6361
}
6462

packages/beacon-node/src/network/reqresp/handlers/beaconBlocksByRoot.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ export async function* onBeaconBlocksByRoot(
3838
slot = slotFromBytes;
3939
}
4040

41-
const {name, seq} = chain.config.getForkInfo(slot);
42-
4341
yield {
44-
data: await chain.blindedOrFullBlockToFullBytes(seq, blockBytes),
45-
fork: name,
42+
data: await chain.blindedOrFullBlockToFullBytes(blockBytes),
43+
fork: chain.config.getForkName(slot),
4644
};
4745
}
4846
}

packages/beacon-node/src/util/fullOrBlindedBlock.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {allForks, bellatrix, capella, deneb} from "@lodestar/types";
33
import {BYTES_PER_LOGS_BLOOM, ForkSeq, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
44
import {executionPayloadToPayloadHeader} from "@lodestar/state-transition";
55
import {ExecutionPayloadBody} from "../execution/engine/types.js";
6-
import {ROOT_SIZE, getSlotFromSignedBeaconBlockSerialized} from "./sszBytes.js";
6+
import {getSlotFromSignedBeaconBlockSerialized} from "./sszBytes.js";
77

88
/**
99
* * class SignedBeaconBlock(Container):
@@ -49,15 +49,7 @@ const BEACON_BLOCK_FIXED_LENGTH = 8 + 8 + 32 + 32 + 4;
4949
* Deneb:
5050
* blobKzgCommitments [offset - 4 bytes]
5151
*/
52-
53-
const LOCATION_OF_ETH1_BLOCK_HASH = 96 + 32 + 8;
54-
export function getEth1BlockHashFromSerializedBlock(block: Uint8Array): Uint8Array {
55-
const firstByte = SIGNED_BEACON_BLOCK_FIXED_LENGTH + BEACON_BLOCK_FIXED_LENGTH + LOCATION_OF_ETH1_BLOCK_HASH;
56-
return block.slice(firstByte, firstByte + ROOT_SIZE);
57-
}
58-
59-
const LOCATION_OF_EXECUTION_PAYLOAD_OFFSET =
60-
LOCATION_OF_ETH1_BLOCK_HASH + 32 + 32 + 4 + 4 + 4 + 4 + 4 + SYNC_COMMITTEE_SIZE / 8 + 96;
52+
const LOCATION_OF_EXECUTION_PAYLOAD_OFFSET = 96 + 32 + 8 + 32 + 32 + 4 + 4 + 4 + 4 + 4 + SYNC_COMMITTEE_SIZE / 8 + 96;
6153

6254
/**
6355
* class ExecutionPayload(Container) or class ExecutionPayloadHeader(Container)

0 commit comments

Comments
 (0)