Repository navigation
feat: defer payload processing to next block #9257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 28 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
a791964
feat: implement EIP-7843 slot_number in ExecutionPayload (specs#4840)
ensi321 3958681
Revert changes introduced by accident
ensi321 a912bc8
Bump version, skip fast_confirmation
ensi321 03c4349
Rest of the changes
ensi321 d3d74a9
Skip some tests
ensi321 40b5f9f
lint
ensi321 da7cc41
ethspecify
ensi321 e49b699
lint
ensi321 813cb52
ethspecify
ensi321 845bec9
fix unit test
ensi321 1d15ece
fix e2e
ensi321 c3d429d
address comment
ensi321 afbb9c1
feat: add processParentExecutionPayload and remove withdrawal early r…
ensi321 311eeca
refactor: add processParentExecutionPayload as first step in processB…
ensi321 e2d3ad2
refactor: transform processExecutionPayloadEnvelope to pure verification
ensi321 c3c73b4
refactor: remove executionPayloadStateRoot from fork choice onExecuti…
ensi321 bc621c3
refactor: simplify envelope import pipeline for deferred processing
ensi321 f4d4021
feat: block production, gossip validation, and cleanup for deferred p…
ensi321 63e5271
fix: type errors in processParentExecutionPayload and produceBlockBody
ensi321 06ab9c1
Fix spec test
ensi321 6552bc8
Address comments & follow up on spec change
ensi321 e10d287
fix upgrade state
ensi321 5421487
fix spec test
ensi321 b5937ec
Harmonize
ensi321 a0bbecf
Merge remote-tracking branch 'origin/unstable' into nc/defer-payload-…
nflaig 941a710
review
nflaig 532ffcc
ethspecify
nflaig 16941ff
restore some comments
nflaig 6b08376
Remove block production
ensi321 8967ae5
refactor
nflaig 5b1e344
simplify applyParentExecutionPayload function signature
nflaig 9a9fe6b
remove genesis block handling, will be separate pr
nflaig b07b363
Merge branch 'unstable' into nc/defer-payload-processing
nflaig bf7009e
we don't know and it's wrong, removed confusing todo
nflaig d2424d5
review computeAnchorCheckpoint
nflaig 9bf5103
review fork_choice.test.ts
nflaig 08b2712
revert changes to computeAnchorCheckpoint
nflaig e62a7a1
ahhh damn, we need that for spec tests
nflaig 8acf7c8
remove type cast
nflaig 24a3da5
clarify withdrawals
nflaig a04a4d0
review getExpectedWithdrawalsForFullParent
nflaig 2fa1c4f
review processParentExecutionPayload.ts
nflaig b89af0b
review operations.test.ts
nflaig c2ef8aa
final pass on processParentExecutionPayload
nflaig 78a7201
small nit
nflaig f8c73af
fix order of gossip checks
nflaig e399921
clarify comment
nflaig 56f686c
review import
nflaig 7728204
Update packages/beacon-node/src/chain/blocks/types.ts
nflaig 11f9d16
Update packages/beacon-node/test/spec/presets/fork_choice.test.ts
nflaig 2054188
Update packages/state-transition/src/block/index.ts
nflaig 2a7d8e6
Update packages/state-transition/src/block/processWithdrawals.ts
nflaig 372f485
.
nflaig 6218a8e
wording
nflaig e450f52
restore some comments
nflaig 232e294
ok that's it
nflaig e7759b0
not yet
nflaig c06c946
why is that comment even modified...
nflaig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| import {routes} from "@lodestar/api"; | ||
| import {ExecutionStatus, PayloadExecutionStatus} from "@lodestar/fork-choice"; | ||
| import {SLOTS_PER_EPOCH} from "@lodestar/params"; | ||
| import {getExecutionPayloadEnvelopeSignatureSet, isStatePostGloas} from "@lodestar/state-transition"; | ||
| import {fromHex, toRootHex} from "@lodestar/utils"; | ||
| import {isStatePostGloas} from "@lodestar/state-transition"; | ||
| import {fromHex} from "@lodestar/utils"; | ||
| import {ExecutionPayloadStatus} from "../../execution/index.js"; | ||
| import {isQueueErrorAborted} from "../../util/queue/index.js"; | ||
| import {BeaconChain} from "../chain.js"; | ||
| import {RegenCaller} from "../regen/interface.js"; | ||
| import {PayloadEnvelopeInput} from "../seenCache/seenPayloadEnvelopeInput.js"; | ||
| import {ImportPayloadOpts} from "./types.js"; | ||
| import { | ||
| verifyExecutionPayloadEnvelope, | ||
| verifyExecutionPayloadEnvelopeSignature, | ||
| } from "./verifyExecutionPayloadEnvelope.js"; | ||
| import {verifyPayloadsDataAvailability} from "./verifyPayloadsDataAvailability.js"; | ||
|
|
||
| const EVENTSTREAM_EMIT_RECENT_EXECUTION_PAYLOAD_SLOTS = 64; | ||
|
|
@@ -69,18 +72,19 @@ function toForkChoiceExecutionStatus(status: ExecutionPayloadStatus): PayloadExe | |
| /** | ||
| * Import an execution payload envelope after all data is available. | ||
| * | ||
| * This function: | ||
| * 1. Emits `execution_payload_available` if payload is for current slot | ||
| * 2. Gets the ProtoBlock from fork choice | ||
| * 3. Applies write-queue backpressure (waitForSpace) early, before verification | ||
| * 4. Regenerates the block state | ||
| * 5. Runs EL verification (notifyNewPayload) in parallel with signature verification and processExecutionPayloadEnvelope | ||
| * 6. Persists verified payload envelope to hot DB | ||
| * 7. Updates fork choice | ||
| * 8. Caches the post-execution payload state | ||
| * 9. Records metrics for column sources | ||
| * 10. Emits `execution_payload` for recent enough payloads after successful import | ||
| * The envelope is purely verified here, no state mutation. State effects from the | ||
| * payload are applied on the next block via processParentExecutionPayload. | ||
| * | ||
| * Steps: | ||
| * 1. Emit `execution_payload_available` for payload attestation | ||
| * 2. Get the ProtoBlock from fork choice | ||
| * 3. Apply write-queue backpressure | ||
| * 4. Regenerate block state for envelope field validation | ||
| * 5. Run EL verification and signature verification in parallel, plus pure envelope verification | ||
| * 6. Persist verified payload envelope to hot DB | ||
| * 7. Update fork choice (transitions PENDING -> FULL) | ||
| * 8. Record metrics | ||
| * 9. Emit `execution_payload` event | ||
| */ | ||
| export async function importExecutionPayload( | ||
| this: BeaconChain, | ||
|
|
@@ -94,10 +98,7 @@ export async function importExecutionPayload( | |
| const blockHashHex = payloadInput.getBlockHashHex(); | ||
| const fork = this.config.getForkName(envelope.payload.slotNumber); | ||
|
|
||
| // 1. Emit `execution_payload_available` event at the start of import. At this point the payload input | ||
| // is already complete, so the payload and required data are available for payload attestation. | ||
| // This event is only about availability, not validity of the execution payload, hence we can emit | ||
| // it before getting a response from the execution client on whether the payload is valid or not. | ||
| // 1. Emit `execution_payload_available` event at the start of import | ||
| if (this.clock.currentSlot - envelope.payload.slotNumber < EVENTSTREAM_EMIT_RECENT_EXECUTION_PAYLOAD_SLOTS) { | ||
| this.emitter.emit(routes.events.EventType.executionPayloadAvailable, { | ||
| slot: envelope.payload.slotNumber, | ||
|
|
@@ -122,8 +123,8 @@ export async function importExecutionPayload( | |
| // The actual DB write is deferred until after verification succeeds. | ||
| await this.unfinalizedPayloadEnvelopeWrites.waitForSpace(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why we did this, this doesn't make sense to me, I moved that check down after verification |
||
|
|
||
| // 5. Get pre-state for processExecutionPayloadEnvelope | ||
| // We need the block state (post-block, pre-payload) to process the envelope | ||
| // 5. Get pre-state for envelope verification | ||
| // We need the block state (post-block, pre-payload) to verify the envelope | ||
| const blockState = await this.regen.getBlockSlotState( | ||
| protoBlock, | ||
| protoBlock.slot, | ||
|
|
@@ -138,7 +139,7 @@ export async function importExecutionPayload( | |
| } | ||
|
|
||
| // 6. Run verification steps in parallel | ||
| const [execResult, signatureValid, postPayloadResult] = await Promise.all([ | ||
| const [execResult, signatureValid] = await Promise.all([ | ||
| this.executionEngine.notifyNewPayload( | ||
| fork, | ||
| envelope.payload, | ||
|
|
@@ -149,45 +150,39 @@ export async function importExecutionPayload( | |
|
|
||
| opts.validSignature === true | ||
| ? Promise.resolve(true) | ||
| : (async () => { | ||
| const signatureSet = getExecutionPayloadEnvelopeSignatureSet( | ||
| this.config, | ||
| this.pubkeyCache, | ||
| blockState, | ||
| signedEnvelope, | ||
| payloadInput.proposerIndex | ||
| ); | ||
| return this.bls.verifySignatureSets([signatureSet]); | ||
| })(), | ||
|
|
||
| // Signature verified separately above. | ||
| // State root check is done separately below with better error typing (matching block pipeline pattern). | ||
| (async () => { | ||
| try { | ||
| return { | ||
| postPayloadState: blockState.processExecutionPayloadEnvelope(signedEnvelope, { | ||
| verifySignature: false, | ||
| verifyStateRoot: false, | ||
| }), | ||
| }; | ||
| } catch (e) { | ||
| throw new PayloadError( | ||
| { | ||
| code: PayloadErrorCode.STATE_TRANSITION_ERROR, | ||
| message: (e as Error).message, | ||
| }, | ||
| `State transition error: ${(e as Error).message}` | ||
| ); | ||
| } | ||
| })(), | ||
| : verifyExecutionPayloadEnvelopeSignature( | ||
| this.config, | ||
| blockState, | ||
| this.pubkeyCache, | ||
| signedEnvelope, | ||
| payloadInput.proposerIndex, | ||
| this.bls | ||
| ), | ||
| ]); | ||
|
|
||
| // 5a. Check signature verification result | ||
| // 5a. Verify envelope fields against state (spec: verify_execution_payload_envelope) | ||
| try { | ||
| // When validSignature is true, the envelope came from gossip/API where both | ||
| // signature and executionRequestsRoot were already verified, skip re-hashing | ||
| verifyExecutionPayloadEnvelope(this.config, blockState, envelope, { | ||
| verifyExecutionRequestsRoot: !opts.validSignature, | ||
| }); | ||
| } catch (e) { | ||
| throw new PayloadError( | ||
| { | ||
| code: PayloadErrorCode.STATE_TRANSITION_ERROR, | ||
| message: (e as Error).message, | ||
| }, | ||
| `Envelope verification error: ${(e as Error).message}` | ||
| ); | ||
| } | ||
|
|
||
| // 5b. Check signature verification result | ||
| if (!signatureValid) { | ||
| throw new PayloadError({code: PayloadErrorCode.INVALID_SIGNATURE}); | ||
| } | ||
|
|
||
| // 5b. Handle EL response | ||
| // 5c. Handle EL response | ||
| switch (execResult.status) { | ||
| case ExecutionPayloadStatus.VALID: | ||
| break; | ||
|
|
@@ -213,11 +208,7 @@ export async function importExecutionPayload( | |
| }); | ||
| } | ||
|
|
||
| // 5c. Compute post-payload state root | ||
| const postPayloadState = postPayloadResult.postPayloadState; | ||
| const postPayloadStateRoot = postPayloadState.hashTreeRoot(); | ||
|
|
||
| // 6. Persist payload envelope to hot DB (performed asynchronously to avoid blocking) | ||
| // 6. Persist payload envelope to hot DB | ||
| this.unfinalizedPayloadEnvelopeWrites.push(payloadInput).catch((e) => { | ||
| if (!isQueueErrorAborted(e)) { | ||
| this.logger.error( | ||
|
|
@@ -228,29 +219,17 @@ export async function importExecutionPayload( | |
| } | ||
| }); | ||
|
|
||
| // 7. Update fork choice | ||
| this.forkChoice.onExecutionPayload( | ||
| blockRootHex, | ||
| blockHashHex, | ||
| envelope.payload.blockNumber, | ||
| toRootHex(postPayloadStateRoot), | ||
| toForkChoiceExecutionStatus(execResult.status) | ||
| ); | ||
|
|
||
| // 8. Cache payload state | ||
| this.regen.processState(blockRootHex, postPayloadState); | ||
| if (postPayloadState.slot % SLOTS_PER_EPOCH === 0) { | ||
| const {checkpoint} = postPayloadState.computeAnchorCheckpoint(); | ||
| this.regen.addCheckpointState(checkpoint, postPayloadState); | ||
| } | ||
| // 7. Update fork choice, transitions the block's PENDING variant to FULL | ||
| const execStatus = toForkChoiceExecutionStatus(execResult.status); | ||
| this.forkChoice.onExecutionPayload(blockRootHex, blockHashHex, envelope.payload.blockNumber, execStatus); | ||
|
|
||
| // 9. Record metrics for payload envelope and column sources | ||
| // 8. Record metrics for payload envelope and column sources | ||
| this.metrics?.importPayload.bySource.inc({source: payloadInput.getPayloadEnvelopeSource().source}); | ||
| for (const {source} of payloadInput.getSampledColumnsWithSource()) { | ||
| this.metrics?.importPayload.columnsBySource.inc({source}); | ||
| } | ||
|
|
||
| // 10. Emit event after payload is fully verified and imported to fork choice, only for recent enough payloads | ||
| // 9. Emit event after payload is fully verified and imported to fork choice | ||
| if (this.clock.currentSlot - envelope.payload.slotNumber < EVENTSTREAM_EMIT_RECENT_EXECUTION_PAYLOAD_SLOTS) { | ||
| this.emitter.emit(routes.events.EventType.executionPayload, { | ||
| slot: envelope.payload.slotNumber, | ||
|
|
||
This file contains hidden or 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 hidden or 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
128 changes: 128 additions & 0 deletions
128
packages/beacon-node/src/chain/blocks/verifyExecutionPayloadEnvelope.ts
This file contains hidden or 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,128 @@ | ||
| import {BeaconConfig} from "@lodestar/config"; | ||
| import { | ||
| type IBeaconStateViewGloas, | ||
| type PubkeyCache, | ||
| computeTimeAtSlot, | ||
| getExecutionPayloadEnvelopeSignatureSet, | ||
| } from "@lodestar/state-transition"; | ||
| import {gloas, ssz} from "@lodestar/types"; | ||
| import {byteArrayEquals, toHex, toRootHex} from "@lodestar/utils"; | ||
| import {IBlsVerifier} from "../bls/index.js"; | ||
|
|
||
| export type VerifyExecutionPayloadEnvelopeOpts = { | ||
| verifyExecutionRequestsRoot?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Verify execution payload envelope fields against the post-block state. | ||
| * | ||
| * Signature verification and the execution engine call (`verify_and_notify_new_payload`) are | ||
| * performed outside this function, see `verifyExecutionPayloadEnvelopeSignature` and | ||
| * `importExecutionPayload` which run both in parallel with this check. | ||
| * | ||
| * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.5/specs/gloas/fork-choice.md#new-verify_execution_payload_envelope | ||
| */ | ||
| export function verifyExecutionPayloadEnvelope( | ||
| config: BeaconConfig, | ||
| state: IBeaconStateViewGloas, | ||
| envelope: gloas.ExecutionPayloadEnvelope, | ||
| opts?: VerifyExecutionPayloadEnvelopeOpts | ||
| ): void { | ||
| const {verifyExecutionRequestsRoot = true} = opts ?? {}; | ||
| const payload = envelope.payload; | ||
|
|
||
| // Verify consistency with the beacon block. | ||
| // Compute header root on a copy of latestBlockHeader to avoid mutating state. | ||
| const headerValue = {...state.latestBlockHeader}; | ||
| if (byteArrayEquals(headerValue.stateRoot, ssz.Root.defaultValue())) { | ||
| headerValue.stateRoot = state.hashTreeRoot(); | ||
| } | ||
| const headerRoot = ssz.phase0.BeaconBlockHeader.hashTreeRoot(headerValue); | ||
| if (!byteArrayEquals(envelope.beaconBlockRoot, headerRoot)) { | ||
| throw new Error( | ||
| `Envelope's block is not the latest block header envelope=${toRootHex(envelope.beaconBlockRoot)} latestBlockHeader=${toRootHex(headerRoot)}` | ||
| ); | ||
| } | ||
|
|
||
| // Verify consistency with the committed bid | ||
| const bid = state.latestExecutionPayloadBid; | ||
| if (envelope.builderIndex !== bid.builderIndex) { | ||
| throw new Error( | ||
| `Builder index mismatch between envelope and committed bid envelope=${envelope.builderIndex} bid=${bid.builderIndex}` | ||
| ); | ||
| } | ||
| if (!byteArrayEquals(bid.prevRandao, payload.prevRandao)) { | ||
| throw new Error( | ||
| `Prev randao mismatch between bid and payload bid=${toHex(bid.prevRandao)} payload=${toHex(payload.prevRandao)}` | ||
| ); | ||
| } | ||
| if (Number(bid.gasLimit) !== payload.gasLimit) { | ||
| throw new Error( | ||
| `Gas limit mismatch between payload and bid payload=${payload.gasLimit} bid=${Number(bid.gasLimit)}` | ||
| ); | ||
| } | ||
| if (!byteArrayEquals(bid.blockHash, payload.blockHash)) { | ||
| throw new Error( | ||
| `Block hash mismatch between payload and bid payload=${toRootHex(payload.blockHash)} bid=${toRootHex(bid.blockHash)}` | ||
| ); | ||
| } | ||
| // Verify execution_requests_root matches bid commitment. | ||
| // Can be skipped if already verified during gossip validation. | ||
| if (verifyExecutionRequestsRoot) { | ||
| const requestsRoot = ssz.electra.ExecutionRequests.hashTreeRoot(envelope.executionRequests); | ||
| if (!byteArrayEquals(requestsRoot, bid.executionRequestsRoot)) { | ||
| throw new Error( | ||
| `Execution requests root mismatch envelope=${toRootHex(requestsRoot)} bid=${toRootHex(bid.executionRequestsRoot)}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Verify the execution payload is valid | ||
| if (payload.slotNumber !== state.slot) { | ||
| throw new Error(`Slot mismatch between payload and state payload=${payload.slotNumber} state=${state.slot}`); | ||
| } | ||
| if (!byteArrayEquals(payload.parentHash, state.latestBlockHash)) { | ||
| throw new Error( | ||
| `Parent hash mismatch between payload and state payload=${toRootHex(payload.parentHash)} state=${toRootHex(state.latestBlockHash)}` | ||
| ); | ||
| } | ||
| if (payload.timestamp !== computeTimeAtSlot(config, state.slot, state.genesisTime)) { | ||
| throw new Error( | ||
| `Timestamp mismatch between payload and state payload=${payload.timestamp} state=${computeTimeAtSlot(config, state.slot, state.genesisTime)}` | ||
| ); | ||
| } | ||
|
|
||
| // Verify consistency with expected withdrawals | ||
| const payloadWithdrawalsRoot = ssz.capella.Withdrawals.hashTreeRoot(payload.withdrawals); | ||
| const expectedWithdrawalsRoot = ssz.capella.Withdrawals.hashTreeRoot(state.payloadExpectedWithdrawals); | ||
| if (!byteArrayEquals(payloadWithdrawalsRoot, expectedWithdrawalsRoot)) { | ||
| throw new Error( | ||
| `Withdrawals mismatch between payload and expected payload=${toRootHex(payloadWithdrawalsRoot)} expected=${toRootHex(expectedWithdrawalsRoot)}` | ||
| ); | ||
| } | ||
|
|
||
| // Execution engine verification (verify_and_notify_new_payload) is done externally by the caller | ||
| } | ||
|
|
||
| /** | ||
| * Verify the BLS signature of an execution payload envelope. | ||
| * | ||
| * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.5/specs/gloas/fork-choice.md#new-verify_execution_payload_envelope_signature | ||
| */ | ||
| export async function verifyExecutionPayloadEnvelopeSignature( | ||
| config: BeaconConfig, | ||
| state: IBeaconStateViewGloas, | ||
| pubkeyCache: PubkeyCache, | ||
| signedEnvelope: gloas.SignedExecutionPayloadEnvelope, | ||
| proposerIndex: number, | ||
| bls: IBlsVerifier | ||
| ): Promise<boolean> { | ||
| const signatureSet = getExecutionPayloadEnvelopeSignatureSet( | ||
| config, | ||
| pubkeyCache, | ||
| state, | ||
| signedEnvelope, | ||
| proposerIndex | ||
| ); | ||
| return bls.verifySignatureSets([signatureSet]); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to have more eyes on
importExecutionPayloadlater but it's good enough to merge this cc @wemeetagain @ensi321 @twoeths