@lodestar/state-transition 1.43.0-dev.ade910fc78 → 1.43.0-dev.b741495bdc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/lib/block/index.d.ts +2 -2
  2. package/lib/block/index.d.ts.map +1 -1
  3. package/lib/block/index.js +11 -4
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/processParentExecutionPayload.d.ts +20 -0
  6. package/lib/block/processParentExecutionPayload.d.ts.map +1 -0
  7. package/lib/block/processParentExecutionPayload.js +100 -0
  8. package/lib/block/processParentExecutionPayload.js.map +1 -0
  9. package/lib/block/processWithdrawals.d.ts.map +1 -1
  10. package/lib/block/processWithdrawals.js +12 -4
  11. package/lib/block/processWithdrawals.js.map +1 -1
  12. package/lib/cache/epochCache.d.ts +3 -1
  13. package/lib/cache/epochCache.d.ts.map +1 -1
  14. package/lib/cache/epochCache.js +31 -13
  15. package/lib/cache/epochCache.js.map +1 -1
  16. package/lib/cache/epochTransitionCache.d.ts +5 -0
  17. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  18. package/lib/cache/epochTransitionCache.js +1 -0
  19. package/lib/cache/epochTransitionCache.js.map +1 -1
  20. package/lib/epoch/index.d.ts +3 -1
  21. package/lib/epoch/index.d.ts.map +1 -1
  22. package/lib/epoch/index.js +8 -1
  23. package/lib/epoch/index.js.map +1 -1
  24. package/lib/epoch/processPtcWindow.d.ts +11 -0
  25. package/lib/epoch/processPtcWindow.d.ts.map +1 -0
  26. package/lib/epoch/processPtcWindow.js +28 -0
  27. package/lib/epoch/processPtcWindow.js.map +1 -0
  28. package/lib/signatureSets/executionPayloadEnvelope.js +1 -1
  29. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -1
  30. package/lib/slot/upgradeStateToGloas.d.ts.map +1 -1
  31. package/lib/slot/upgradeStateToGloas.js +3 -1
  32. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  33. package/lib/stateTransition.d.ts +1 -2
  34. package/lib/stateTransition.d.ts.map +1 -1
  35. package/lib/stateTransition.js +1 -2
  36. package/lib/stateTransition.js.map +1 -1
  37. package/lib/stateView/beaconStateView.d.ts +8 -10
  38. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  39. package/lib/stateView/beaconStateView.js +34 -38
  40. package/lib/stateView/beaconStateView.js.map +1 -1
  41. package/lib/stateView/interface.d.ts +19 -11
  42. package/lib/stateView/interface.d.ts.map +1 -1
  43. package/lib/stateView/interface.js.map +1 -1
  44. package/lib/util/computeAnchorCheckpoint.d.ts +1 -1
  45. package/lib/util/computeAnchorCheckpoint.d.ts.map +1 -1
  46. package/lib/util/computeAnchorCheckpoint.js +6 -19
  47. package/lib/util/computeAnchorCheckpoint.js.map +1 -1
  48. package/lib/util/execution.d.ts +4 -2
  49. package/lib/util/execution.d.ts.map +1 -1
  50. package/lib/util/execution.js +7 -0
  51. package/lib/util/execution.js.map +1 -1
  52. package/lib/util/gloas.d.ts +8 -3
  53. package/lib/util/gloas.d.ts.map +1 -1
  54. package/lib/util/gloas.js +27 -1
  55. package/lib/util/gloas.js.map +1 -1
  56. package/package.json +8 -8
  57. package/src/block/index.ts +12 -4
  58. package/src/block/processParentExecutionPayload.ts +116 -0
  59. package/src/block/processWithdrawals.ts +12 -4
  60. package/src/cache/epochCache.ts +32 -30
  61. package/src/cache/epochTransitionCache.ts +7 -0
  62. package/src/epoch/index.ts +9 -0
  63. package/src/epoch/processPtcWindow.ts +38 -0
  64. package/src/signatureSets/executionPayloadEnvelope.ts +1 -1
  65. package/src/slot/upgradeStateToGloas.ts +5 -1
  66. package/src/stateTransition.ts +1 -2
  67. package/src/stateView/beaconStateView.ts +46 -49
  68. package/src/stateView/interface.ts +26 -14
  69. package/src/util/computeAnchorCheckpoint.ts +6 -19
  70. package/src/util/execution.ts +11 -1
  71. package/src/util/gloas.ts +50 -3
  72. package/lib/block/processExecutionPayloadEnvelope.d.ts +0 -9
  73. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +0 -1
  74. package/lib/block/processExecutionPayloadEnvelope.js +0 -106
  75. package/lib/block/processExecutionPayloadEnvelope.js.map +0 -1
  76. package/src/block/processExecutionPayloadEnvelope.ts +0 -175
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ChainSafe/lodestar/issues"
13
13
  },
14
- "version": "1.43.0-dev.ade910fc78",
14
+ "version": "1.43.0-dev.b741495bdc",
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
@@ -65,16 +65,16 @@
65
65
  "@chainsafe/persistent-merkle-tree": "^1.2.1",
66
66
  "@chainsafe/persistent-ts": "^1.0.0",
67
67
  "@chainsafe/pubkey-index-map": "^3.0.0",
68
- "@chainsafe/ssz": "^1.2.2",
68
+ "@chainsafe/ssz": "^1.4.0",
69
69
  "@chainsafe/swap-or-not-shuffle": "^1.2.1",
70
- "@lodestar/config": "^1.43.0-dev.ade910fc78",
71
- "@lodestar/params": "^1.43.0-dev.ade910fc78",
72
- "@lodestar/types": "^1.43.0-dev.ade910fc78",
73
- "@lodestar/utils": "^1.43.0-dev.ade910fc78",
70
+ "@lodestar/config": "^1.43.0-dev.b741495bdc",
71
+ "@lodestar/params": "^1.43.0-dev.b741495bdc",
72
+ "@lodestar/types": "^1.43.0-dev.b741495bdc",
73
+ "@lodestar/utils": "^1.43.0-dev.b741495bdc",
74
74
  "@vekexasia/bigint-buffer2": "^1.1.1"
75
75
  },
76
76
  "devDependencies": {
77
- "@lodestar/api": "^1.43.0-dev.ade910fc78"
77
+ "@lodestar/api": "^1.43.0-dev.b741495bdc"
78
78
  },
79
79
  "keywords": [
80
80
  "ethereum",
@@ -82,5 +82,5 @@
82
82
  "beacon",
83
83
  "blockchain"
84
84
  ],
85
- "gitHead": "28e20b48ea4cedefb77526a12286bb0b042a08bb"
85
+ "gitHead": "30fc761b43215b6e1b51e1d847af5c76f7447a4a"
86
86
  }
@@ -14,8 +14,8 @@ import {processBlockHeader} from "./processBlockHeader.js";
14
14
  import {processEth1Data} from "./processEth1Data.js";
15
15
  import {processExecutionPayload} from "./processExecutionPayload.js";
16
16
  import {processExecutionPayloadBid} from "./processExecutionPayloadBid.js";
17
- import {processExecutionPayloadEnvelope} from "./processExecutionPayloadEnvelope.js";
18
17
  import {processOperations} from "./processOperations.js";
18
+ import {processParentExecutionPayload} from "./processParentExecutionPayload.js";
19
19
  import {processPayloadAttestation} from "./processPayloadAttestation.js";
20
20
  import {processRandao} from "./processRandao.js";
21
21
  import {processSyncAggregate} from "./processSyncCommittee.js";
@@ -32,7 +32,7 @@ export {
32
32
  processWithdrawals,
33
33
  processExecutionPayloadBid,
34
34
  processPayloadAttestation,
35
- processExecutionPayloadEnvelope,
35
+ processParentExecutionPayload,
36
36
  };
37
37
 
38
38
  export * from "./externalData.js";
@@ -51,10 +51,16 @@ export function processBlock(
51
51
  ): void {
52
52
  const {verifySignatures = true} = opts ?? {};
53
53
 
54
+ // Apply the parent's deferred payload effects before everything else. Must run before
55
+ // processBlockHeader and processExecutionPayloadBid so subsequent steps see the updated state.
56
+ if (fork >= ForkSeq.gloas) {
57
+ processParentExecutionPayload(state as CachedBeaconStateGloas, block as BeaconBlock<ForkPostGloas>);
58
+ }
59
+
54
60
  processBlockHeader(state, block);
55
61
 
56
62
  if (fork >= ForkSeq.gloas) {
57
- // After gloas, processWithdrawals does not take a payload parameter
63
+ // Parent payload's execution requests were already applied by processParentExecutionPayload above
58
64
  processWithdrawals(fork, state as CachedBeaconStateGloas);
59
65
  } else if (fork >= ForkSeq.capella) {
60
66
  const fullOrBlindedPayload = getFullOrBlindedPayload(block);
@@ -67,7 +73,9 @@ export function processBlock(
67
73
 
68
74
  // The call to the process_execution_payload must happen before the call to the process_randao as the former depends
69
75
  // on the randao_mix computed with the reveal of the previous block.
70
- // TODO GLOAS: We call processExecutionPayload somewhere else post-gloas
76
+ // Post-gloas: process_execution_payload is not part of block processing. The parent's payload
77
+ // effects are applied earlier via processParentExecutionPayload, and each execution payload is
78
+ // verified out-of-band via verifyExecutionPayloadEnvelope when it arrives.
71
79
  if (
72
80
  fork < ForkSeq.gloas &&
73
81
  fork >= ForkSeq.bellatrix &&
@@ -0,0 +1,116 @@
1
+ import {ForkPostGloas, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
2
+ import {BeaconBlock, electra, ssz} from "@lodestar/types";
3
+ import {byteArrayEquals, toRootHex} from "@lodestar/utils";
4
+ import {CachedBeaconStateGloas} from "../types.js";
5
+ import {computeEpochAtSlot} from "../util/epoch.js";
6
+ import {processConsolidationRequest} from "./processConsolidationRequest.js";
7
+ import {getPendingValidatorPubkeys, processDepositRequest} from "./processDepositRequest.js";
8
+ import {processWithdrawalRequest} from "./processWithdrawalRequest.js";
9
+
10
+ /**
11
+ * Process parent execution payload effects as the first step of processBlock.
12
+ *
13
+ * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.5/specs/gloas/beacon-chain.md#new-process_parent_execution_payload
14
+ */
15
+ export function processParentExecutionPayload(state: CachedBeaconStateGloas, block: BeaconBlock<ForkPostGloas>): void {
16
+ const bid = block.body.signedExecutionPayloadBid.message;
17
+ const parentBid = state.latestExecutionPayloadBid;
18
+ const requests = block.body.parentExecutionRequests;
19
+
20
+ const isParentBlockFull = byteArrayEquals(bid.parentBlockHash, parentBid.blockHash);
21
+ if (!isParentBlockFull) {
22
+ // Parent was EMPTY -- no execution requests expected
23
+ assertEmptyExecutionRequests(requests);
24
+ return;
25
+ }
26
+
27
+ // Parent was FULL -- verify the bid commitment and apply the payload
28
+ const requestsRoot = ssz.electra.ExecutionRequests.hashTreeRoot(requests);
29
+ if (!byteArrayEquals(requestsRoot, parentBid.executionRequestsRoot)) {
30
+ throw new Error(
31
+ `Parent execution requests root mismatch actual=${toRootHex(requestsRoot)} expected=${toRootHex(parentBid.executionRequestsRoot)}`
32
+ );
33
+ }
34
+
35
+ applyParentExecutionPayload(state, requests);
36
+ }
37
+
38
+ /**
39
+ * Process the parent's execution requests, queue the builder payment, update payload availability,
40
+ * and update the latest block hash.
41
+ *
42
+ * Called from processParentExecutionPayload during block processing, and from the validator during
43
+ * block production before computing withdrawals.
44
+ *
45
+ * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.5/specs/gloas/beacon-chain.md#new-apply_parent_execution_payload
46
+ */
47
+ export function applyParentExecutionPayload(state: CachedBeaconStateGloas, requests: electra.ExecutionRequests): void {
48
+ const fork = state.config.getForkSeq(state.slot);
49
+ const parentBid = state.latestExecutionPayloadBid;
50
+ const parentSlot = parentBid.slot;
51
+ const parentEpoch = computeEpochAtSlot(parentSlot);
52
+ const currentEpoch = computeEpochAtSlot(state.slot);
53
+
54
+ // Process execution requests from parent's payload. The execution
55
+ // requests are processed at state.slot (child's slot), not the parent's slot.
56
+ if (requests.deposits.length > 0) {
57
+ const pendingValidatorPubkeys = getPendingValidatorPubkeys(state.config, state);
58
+ for (const deposit of requests.deposits) {
59
+ processDepositRequest(fork, state, deposit, pendingValidatorPubkeys);
60
+ }
61
+ }
62
+
63
+ for (const withdrawal of requests.withdrawals) {
64
+ processWithdrawalRequest(fork, state, withdrawal);
65
+ }
66
+
67
+ for (const consolidation of requests.consolidations) {
68
+ processConsolidationRequest(state, consolidation);
69
+ }
70
+
71
+ // Settle the builder payment
72
+ if (parentEpoch === currentEpoch) {
73
+ settleBuilderPayment(state, SLOTS_PER_EPOCH + (parentSlot % SLOTS_PER_EPOCH));
74
+ } else if (parentEpoch === currentEpoch - 1) {
75
+ settleBuilderPayment(state, parentSlot % SLOTS_PER_EPOCH);
76
+ } else if (parentBid.value > 0) {
77
+ // Parent is older than the previous epoch, its payment entry has been evicted from
78
+ // builder_pending_payments. Append the withdrawal directly.
79
+ state.builderPendingWithdrawals.push(
80
+ ssz.gloas.BuilderPendingWithdrawal.toViewDU({
81
+ feeRecipient: parentBid.feeRecipient,
82
+ amount: parentBid.value,
83
+ builderIndex: parentBid.builderIndex,
84
+ })
85
+ );
86
+ }
87
+
88
+ // Update parent payload availability and latest block hash
89
+ state.executionPayloadAvailability.set(parentSlot % SLOTS_PER_HISTORICAL_ROOT, true);
90
+ state.latestBlockHash = parentBid.blockHash;
91
+ }
92
+
93
+ /**
94
+ * Settle a builder payment at the given index: move its withdrawal (if any) to the
95
+ * pending withdrawals list and clear the payment slot.
96
+ *
97
+ * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.5/specs/gloas/beacon-chain.md#new-settle_builder_payment
98
+ */
99
+ function settleBuilderPayment(state: CachedBeaconStateGloas, paymentIndex: number): void {
100
+ if (paymentIndex >= state.builderPendingPayments.length) {
101
+ throw new Error(
102
+ `Invalid builder payment index paymentIndex=${paymentIndex} limit=${state.builderPendingPayments.length}`
103
+ );
104
+ }
105
+ const payment = state.builderPendingPayments.get(paymentIndex).clone();
106
+ if (payment.withdrawal.amount > 0) {
107
+ state.builderPendingWithdrawals.push(payment.withdrawal);
108
+ }
109
+ state.builderPendingPayments.set(paymentIndex, ssz.gloas.BuilderPendingPayment.defaultViewDU());
110
+ }
111
+
112
+ function assertEmptyExecutionRequests(requests: electra.ExecutionRequests): void {
113
+ if (requests.deposits.length !== 0 || requests.withdrawals.length !== 0 || requests.consolidations.length !== 0) {
114
+ throw new Error("Parent execution requests must be empty when parent block is EMPTY");
115
+ }
116
+ }
@@ -10,6 +10,7 @@ import {
10
10
  } from "@lodestar/params";
11
11
  import {BuilderIndex, ValidatorIndex, capella, ssz} from "@lodestar/types";
12
12
  import {byteArrayEquals, toRootHex} from "@lodestar/utils";
13
+ import {ZERO_HASH} from "../constants/index.js";
13
14
  import {CachedBeaconStateCapella, CachedBeaconStateElectra, CachedBeaconStateGloas} from "../types.js";
14
15
  import {
15
16
  convertBuilderIndexToValidatorIndex,
@@ -31,9 +32,14 @@ export function processWithdrawals(
31
32
  state: CachedBeaconStateCapella | CachedBeaconStateElectra | CachedBeaconStateGloas,
32
33
  payload?: capella.FullOrBlindedExecutionPayload
33
34
  ): void {
34
- // Return early if the parent block is empty
35
- if (fork >= ForkSeq.gloas && !isParentBlockFull(state as CachedBeaconStateGloas)) {
36
- return;
35
+ // Return early if this is genesis block or the parent block is empty
36
+ if (fork >= ForkSeq.gloas) {
37
+ const stateGloas = state as CachedBeaconStateGloas;
38
+ const isGenesisBlock = byteArrayEquals(stateGloas.latestBlockHash, ZERO_HASH);
39
+ const isParentBlockEmpty = !isParentBlockFull(stateGloas);
40
+ if (isGenesisBlock || isParentBlockEmpty) {
41
+ return;
42
+ }
37
43
  }
38
44
 
39
45
  // processedBuilderWithdrawalsCount is withdrawals coming from builder payment since gloas (EIP-7732)
@@ -48,7 +54,9 @@ export function processWithdrawals(
48
54
  } = getExpectedWithdrawals(fork, state);
49
55
  const numWithdrawals = expectedWithdrawals.length;
50
56
 
51
- // After gloas, withdrawals are verified later in processExecutionPayloadEnvelope
57
+ // Pre-gloas verifies the payload's withdrawals against expectedWithdrawals here.
58
+ // Post-gloas, the payload arrives later as an envelope and that consistency check
59
+ // happens in verifyExecutionPayloadEnvelope against state.payloadExpectedWithdrawals.
52
60
  if (fork < ForkSeq.gloas) {
53
61
  if (payload === undefined) {
54
62
  throw Error("payload is required for pre-gloas processWithdrawals");
@@ -32,10 +32,10 @@ import {
32
32
  calculateShufflingDecisionRoot,
33
33
  computeEpochShuffling,
34
34
  } from "../util/epochShuffling.js";
35
+ import {getPtcWindowEpochCacheData} from "../util/gloas.js";
35
36
  import {
36
37
  computeActivationExitEpoch,
37
38
  computeEpochAtSlot,
38
- computePayloadTimelinessCommitteesForEpoch,
39
39
  computeProposers,
40
40
  computeSyncPeriodAtEpoch,
41
41
  getActivationChurnLimit,
@@ -56,7 +56,7 @@ import {sumTargetUnslashedBalanceIncrements} from "../util/targetUnslashedBalanc
56
56
  import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsWithLen} from "./effectiveBalanceIncrements.js";
57
57
  import {EpochTransitionCache} from "./epochTransitionCache.js";
58
58
  import {PubkeyCache, createPubkeyCache, syncPubkeys} from "./pubkeyCache.js";
59
- import {CachedBeaconStateAllForks, CachedBeaconStateFulu} from "./stateCache.js";
59
+ import {CachedBeaconStateAllForks, CachedBeaconStateFulu, CachedBeaconStateGloas} from "./stateCache.js";
60
60
  import {
61
61
  SyncCommitteeCache,
62
62
  SyncCommitteeCacheEmpty,
@@ -226,11 +226,12 @@ export class EpochCache {
226
226
  /** TODO: Indexed SyncCommitteeCache */
227
227
  nextSyncCommitteeIndexed: SyncCommitteeCache;
228
228
 
229
- // TODO GLOAS: See if we need to cache PTC for next epoch
230
229
  // PTC for previous epoch, required for slot N block validating slot N-1 attestations
231
230
  previousPayloadTimelinessCommittees: Uint32Array[];
232
231
  // PTC for current epoch, computed eagerly at epoch transition
233
232
  payloadTimelinessCommittees: Uint32Array[];
233
+ // PTC for next epoch, precomputed from the ptc window for future duty serving
234
+ nextPayloadTimelinessCommittees: Uint32Array[];
234
235
 
235
236
  // TODO: Helper stats
236
237
  syncPeriod: SyncPeriod;
@@ -270,6 +271,7 @@ export class EpochCache {
270
271
  nextSyncCommitteeIndexed: SyncCommitteeCache;
271
272
  previousPayloadTimelinessCommittees: Uint32Array[];
272
273
  payloadTimelinessCommittees: Uint32Array[];
274
+ nextPayloadTimelinessCommittees: Uint32Array[];
273
275
  epoch: Epoch;
274
276
  syncPeriod: SyncPeriod;
275
277
  }) {
@@ -301,6 +303,7 @@ export class EpochCache {
301
303
  this.nextSyncCommitteeIndexed = data.nextSyncCommitteeIndexed;
302
304
  this.previousPayloadTimelinessCommittees = data.previousPayloadTimelinessCommittees;
303
305
  this.payloadTimelinessCommittees = data.payloadTimelinessCommittees;
306
+ this.nextPayloadTimelinessCommittees = data.nextPayloadTimelinessCommittees;
304
307
  this.epoch = data.epoch;
305
308
  this.syncPeriod = data.syncPeriod;
306
309
  }
@@ -451,25 +454,13 @@ export class EpochCache {
451
454
  nextSyncCommitteeIndexed = new SyncCommitteeCacheEmpty();
452
455
  }
453
456
 
454
- // Compute PTC for all slots in the prev/current epoch
457
+ // Copy previous/current epoch PTC slices from state.ptcWindow once, then serve hot-path lookups from epochCtx.
455
458
  let previousPayloadTimelinessCommittees: Uint32Array[] = [];
456
459
  let payloadTimelinessCommittees: Uint32Array[] = [];
460
+ let nextPayloadTimelinessCommittees: Uint32Array[] = [];
457
461
  if (currentEpoch >= config.GLOAS_FORK_EPOCH) {
458
- payloadTimelinessCommittees = computePayloadTimelinessCommitteesForEpoch(
459
- state,
460
- currentEpoch,
461
- currentShuffling.committees,
462
- effectiveBalanceIncrements
463
- );
464
-
465
- if (!isGenesis && previousEpoch >= config.GLOAS_FORK_EPOCH) {
466
- previousPayloadTimelinessCommittees = computePayloadTimelinessCommitteesForEpoch(
467
- state,
468
- previousEpoch,
469
- previousShuffling.committees,
470
- effectiveBalanceIncrements
471
- );
472
- }
462
+ ({previousPayloadTimelinessCommittees, payloadTimelinessCommittees, nextPayloadTimelinessCommittees} =
463
+ getPtcWindowEpochCacheData(state as CachedBeaconStateGloas));
473
464
  }
474
465
 
475
466
  // Precompute churnLimit for efficient initiateValidatorExit() during block proposing MUST be recompute everytime the
@@ -546,6 +537,7 @@ export class EpochCache {
546
537
  nextSyncCommitteeIndexed,
547
538
  previousPayloadTimelinessCommittees,
548
539
  payloadTimelinessCommittees,
540
+ nextPayloadTimelinessCommittees,
549
541
  epoch: currentEpoch,
550
542
  syncPeriod: computeSyncPeriodAtEpoch(currentEpoch),
551
543
  });
@@ -592,6 +584,7 @@ export class EpochCache {
592
584
  nextSyncCommitteeIndexed: this.nextSyncCommitteeIndexed,
593
585
  previousPayloadTimelinessCommittees: this.previousPayloadTimelinessCommittees,
594
586
  payloadTimelinessCommittees: this.payloadTimelinessCommittees,
587
+ nextPayloadTimelinessCommittees: this.nextPayloadTimelinessCommittees,
595
588
  epoch: this.epoch,
596
589
  syncPeriod: this.syncPeriod,
597
590
  });
@@ -695,21 +688,26 @@ export class EpochCache {
695
688
  /**
696
689
  * At fork boundary, this runs post-fork logic and it happens after `upgradeState*` is called.
697
690
  */
698
- finalProcessEpoch(state: CachedBeaconStateAllForks): void {
691
+ finalProcessEpoch(state: CachedBeaconStateAllForks, epochTransitionCache: EpochTransitionCache): void {
699
692
  // this.epoch was updated at the end of afterProcessEpoch()
700
693
  const upcomingEpoch = this.epoch;
701
694
  const epochAfterUpcoming = upcomingEpoch + 1;
702
695
 
703
696
  this.proposersPrevEpoch = this.proposers;
704
697
  if (upcomingEpoch >= this.config.GLOAS_FORK_EPOCH) {
705
- // Shift and compute current epoch PTC eagerly for all slots
706
- this.previousPayloadTimelinessCommittees = this.payloadTimelinessCommittees;
707
- this.payloadTimelinessCommittees = computePayloadTimelinessCommitteesForEpoch(
708
- state,
709
- upcomingEpoch,
710
- this.currentShuffling.committees,
711
- this.effectiveBalanceIncrements
712
- );
698
+ if (epochTransitionCache.nextEpochPayloadTimelinessCommittees) {
699
+ // shift arrays from transition cache
700
+ this.previousPayloadTimelinessCommittees = this.payloadTimelinessCommittees;
701
+ this.payloadTimelinessCommittees = this.nextPayloadTimelinessCommittees;
702
+ this.nextPayloadTimelinessCommittees = epochTransitionCache.nextEpochPayloadTimelinessCommittees;
703
+ } else {
704
+ // Fork boundary: processPtcWindow didn't run, read from freshly initialized state.ptcWindow
705
+ ({
706
+ previousPayloadTimelinessCommittees: this.previousPayloadTimelinessCommittees,
707
+ payloadTimelinessCommittees: this.payloadTimelinessCommittees,
708
+ nextPayloadTimelinessCommittees: this.nextPayloadTimelinessCommittees,
709
+ } = getPtcWindowEpochCacheData(state as CachedBeaconStateGloas));
710
+ }
713
711
  }
714
712
  if (upcomingEpoch >= this.config.FULU_FORK_EPOCH) {
715
713
  // Populate proposer cache with lookahead from state
@@ -1034,12 +1032,16 @@ export class EpochCache {
1034
1032
  throw new Error("Payload Timeliness Committee is not available before gloas fork");
1035
1033
  }
1036
1034
 
1035
+ if (epoch === this.epoch - 1) {
1036
+ return this.previousPayloadTimelinessCommittees[slot % SLOTS_PER_EPOCH];
1037
+ }
1038
+
1037
1039
  if (epoch === this.epoch) {
1038
1040
  return this.payloadTimelinessCommittees[slot % SLOTS_PER_EPOCH];
1039
1041
  }
1040
1042
 
1041
- if (epoch === this.epoch - 1 && this.previousPayloadTimelinessCommittees.length > 0) {
1042
- return this.previousPayloadTimelinessCommittees[slot % SLOTS_PER_EPOCH];
1043
+ if (epoch === this.epoch + 1) {
1044
+ return this.nextPayloadTimelinessCommittees[slot % SLOTS_PER_EPOCH];
1043
1045
  }
1044
1046
 
1045
1047
  throw new Error(`Payload Timeliness Committee is not available for slot=${slot}`);
@@ -158,6 +158,12 @@ export interface EpochTransitionCache {
158
158
  */
159
159
  nextShuffling: EpochShuffling | null;
160
160
 
161
+ /**
162
+ * Pre-computed PTC for epoch N + MIN_SEED_LOOKAHEAD + 1, populated by processPtcWindow (Gloas+).
163
+ * Used by finalProcessEpoch to shift PTC arrays in epoch cache without reading from state.
164
+ */
165
+ nextEpochPayloadTimelinessCommittees: Uint32Array[] | null;
166
+
161
167
  /**
162
168
  * Altair specific, this is total active balances for the next epoch.
163
169
  * This is only used in `afterProcessEpoch` to compute base reward and sync participant reward.
@@ -502,6 +508,7 @@ export function beforeProcessEpoch(
502
508
  indicesToEject,
503
509
  nextShufflingActiveIndices,
504
510
  nextShuffling: null,
511
+ nextEpochPayloadTimelinessCommittees: null,
505
512
  // to be updated in processEffectiveBalanceUpdates
506
513
  nextEpochTotalActiveBalanceByIncrement: 0,
507
514
  isActivePrevEpoch,
@@ -28,6 +28,7 @@ import {processParticipationRecordUpdates} from "./processParticipationRecordUpd
28
28
  import {processPendingConsolidations} from "./processPendingConsolidations.js";
29
29
  import {processPendingDeposits} from "./processPendingDeposits.js";
30
30
  import {processProposerLookahead} from "./processProposerLookahead.js";
31
+ import {processPtcWindow} from "./processPtcWindow.js";
31
32
  import {processRandaoMixesReset} from "./processRandaoMixesReset.js";
32
33
  import {processRegistryUpdates} from "./processRegistryUpdates.js";
33
34
  import {processRewardsAndPenalties} from "./processRewardsAndPenalties.js";
@@ -55,6 +56,7 @@ export {
55
56
  processPendingDeposits,
56
57
  processPendingConsolidations,
57
58
  processProposerLookahead,
59
+ processPtcWindow,
58
60
  processBuilderPendingPayments,
59
61
  };
60
62
 
@@ -81,6 +83,7 @@ export enum EpochTransitionStep {
81
83
  processPendingDeposits = "processPendingDeposits",
82
84
  processPendingConsolidations = "processPendingConsolidations",
83
85
  processProposerLookahead = "processProposerLookahead",
86
+ processPtcWindow = "processPtcWindow",
84
87
  processBuilderPendingPayments = "processBuilderPendingPayments",
85
88
  }
86
89
 
@@ -211,4 +214,10 @@ export function processEpoch(
211
214
  processProposerLookahead(fork, state as CachedBeaconStateFulu, cache);
212
215
  timer?.();
213
216
  }
217
+
218
+ if (fork >= ForkSeq.gloas) {
219
+ const timer = metrics?.epochTransitionStepTime.startTimer({step: EpochTransitionStep.processPtcWindow});
220
+ processPtcWindow(state as CachedBeaconStateGloas, cache);
221
+ timer?.();
222
+ }
214
223
  }
@@ -0,0 +1,38 @@
1
+ import {MIN_SEED_LOOKAHEAD} from "@lodestar/params";
2
+ import {ssz} from "@lodestar/types";
3
+ import {CachedBeaconStateGloas, EpochTransitionCache} from "../types.js";
4
+ import {computeEpochShuffling} from "../util/epochShuffling.js";
5
+ import {computePayloadTimelinessCommitteesForEpoch} from "../util/seed.js";
6
+
7
+ /**
8
+ * Update the `ptc_window` field in the beacon state by shifting out the oldest epoch's
9
+ * PTC entries and appending newly computed entries for the next lookahead epoch.
10
+ * Stashes the computed PTCs in the transition cache for finalProcessEpoch to shift
11
+ * into the epoch cache without reading from state.
12
+ *
13
+ * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.4/specs/gloas/beacon-chain.md#new-process_ptc_window
14
+ */
15
+ export function processPtcWindow(state: CachedBeaconStateGloas, cache: EpochTransitionCache): void {
16
+ const nextEpoch = state.epochCtx.epoch + MIN_SEED_LOOKAHEAD + 1;
17
+ const nextEpochShuffling =
18
+ cache.nextShuffling ?? computeEpochShuffling(state, cache.nextShufflingActiveIndices, nextEpoch);
19
+ cache.nextShuffling = nextEpochShuffling;
20
+
21
+ const newNextPayloadTimelinessCommittees = computePayloadTimelinessCommitteesForEpoch(
22
+ state,
23
+ nextEpoch,
24
+ nextEpochShuffling.committees,
25
+ state.epochCtx.effectiveBalanceIncrements
26
+ );
27
+
28
+ // Stash for finalProcessEpoch to shift into epoch cache
29
+ cache.nextEpochPayloadTimelinessCommittees = newNextPayloadTimelinessCommittees;
30
+
31
+ // Write shifted window to state: current(N) + next(N+1) + newlyComputed(N+2)
32
+ // From the perspective of upcoming epoch N+1, this is previous + current + next
33
+ state.ptcWindow = ssz.gloas.PtcWindow.toViewDU([
34
+ ...state.epochCtx.payloadTimelinessCommittees,
35
+ ...state.epochCtx.nextPayloadTimelinessCommittees,
36
+ ...newNextPayloadTimelinessCommittees,
37
+ ]);
38
+ }
@@ -11,7 +11,7 @@ export function getExecutionPayloadEnvelopeSigningRoot(
11
11
  config: BeaconConfig,
12
12
  envelope: gloas.ExecutionPayloadEnvelope
13
13
  ): Uint8Array {
14
- const domain = config.getDomain(envelope.slot, DOMAIN_BEACON_BUILDER);
14
+ const domain = config.getDomain(envelope.payload.slotNumber, DOMAIN_BEACON_BUILDER);
15
15
 
16
16
  return computeSigningRoot(ssz.gloas.ExecutionPayloadEnvelope, envelope, domain);
17
17
  }
@@ -5,7 +5,7 @@ import {isValidDepositSignature} from "../block/processDeposit.js";
5
5
  import {applyDepositForBuilder} from "../block/processDepositRequest.js";
6
6
  import {getCachedBeaconState} from "../cache/stateCache.js";
7
7
  import {CachedBeaconStateFulu, CachedBeaconStateGloas} from "../types.js";
8
- import {isBuilderWithdrawalCredential} from "../util/gloas.js";
8
+ import {initializePtcWindow, isBuilderWithdrawalCredential} from "../util/gloas.js";
9
9
  import {isValidatorKnown} from "../util/index.js";
10
10
 
11
11
  /**
@@ -48,6 +48,9 @@ export function upgradeStateToGloas(stateFulu: CachedBeaconStateFulu): CachedBea
48
48
  stateGloasView.currentSyncCommittee = stateGloasCloned.currentSyncCommittee;
49
49
  stateGloasView.nextSyncCommittee = stateGloasCloned.nextSyncCommittee;
50
50
  stateGloasView.latestExecutionPayloadBid.blockHash = stateFulu.latestExecutionPayloadHeader.blockHash;
51
+ stateGloasView.latestExecutionPayloadBid.executionRequestsRoot = ssz.electra.ExecutionRequests.hashTreeRoot(
52
+ ssz.electra.ExecutionRequests.defaultValue()
53
+ );
51
54
  stateGloasView.nextWithdrawalIndex = stateGloasCloned.nextWithdrawalIndex;
52
55
  stateGloasView.nextWithdrawalValidatorIndex = stateGloasCloned.nextWithdrawalValidatorIndex;
53
56
  stateGloasView.historicalSummaries = stateGloasCloned.historicalSummaries;
@@ -61,6 +64,7 @@ export function upgradeStateToGloas(stateFulu: CachedBeaconStateFulu): CachedBea
61
64
  stateGloasView.pendingPartialWithdrawals = stateGloasCloned.pendingPartialWithdrawals;
62
65
  stateGloasView.pendingConsolidations = stateGloasCloned.pendingConsolidations;
63
66
  stateGloasView.proposerLookahead = stateGloasCloned.proposerLookahead;
67
+ stateGloasView.ptcWindow = ssz.gloas.PtcWindow.toViewDU(initializePtcWindow(stateFulu));
64
68
 
65
69
  for (let i = 0; i < SLOTS_PER_HISTORICAL_ROOT; i++) {
66
70
  stateGloasView.executionPayloadAvailability.set(i, true);
@@ -76,7 +76,6 @@ export enum StateHashTreeRootSource {
76
76
  prepareNextEpoch = "prepare_next_epoch",
77
77
  regenState = "regen_state",
78
78
  computeNewStateRoot = "compute_new_state_root",
79
- computePayloadEnvelopeStateRoot = "compute_payload_envelope_state_root",
80
79
  }
81
80
 
82
81
  /**
@@ -283,7 +282,7 @@ function processSlotsWithTransientCache(
283
282
  {
284
283
  const timer = metrics?.epochTransitionStepTime.startTimer({step: EpochTransitionStep.finalProcessEpoch});
285
284
  // last step to prepare epoch data that depends on the upgraded state, for example proposerLookahead of BeaconStateFulu
286
- postState.epochCtx.finalProcessEpoch(postState);
285
+ postState.epochCtx.finalProcessEpoch(postState, epochTransitionCache);
287
286
  timer?.();
288
287
  }
289
288