@lodestar/state-transition 1.46.0-dev.8b30182711 → 1.46.0-dev.8bab23d909

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 (101) hide show
  1. package/lib/block/processDeposit.d.ts +15 -0
  2. package/lib/block/processDeposit.d.ts.map +1 -1
  3. package/lib/block/processDeposit.js +73 -1
  4. package/lib/block/processDeposit.js.map +1 -1
  5. package/lib/block/processExecutionPayloadBid.d.ts +2 -2
  6. package/lib/block/processExecutionPayloadBid.d.ts.map +1 -1
  7. package/lib/block/processExecutionPayloadBid.js +8 -3
  8. package/lib/block/processExecutionPayloadBid.js.map +1 -1
  9. package/lib/cache/builderDepositSignatureCache.d.ts +48 -0
  10. package/lib/cache/builderDepositSignatureCache.d.ts.map +1 -0
  11. package/lib/cache/builderDepositSignatureCache.js +70 -0
  12. package/lib/cache/builderDepositSignatureCache.js.map +1 -0
  13. package/lib/cache/epochCache.d.ts +9 -0
  14. package/lib/cache/epochCache.d.ts.map +1 -1
  15. package/lib/cache/epochCache.js +13 -0
  16. package/lib/cache/epochCache.js.map +1 -1
  17. package/lib/cache/stateCache.d.ts +2 -1
  18. package/lib/cache/stateCache.d.ts.map +1 -1
  19. package/lib/cache/stateCache.js.map +1 -1
  20. package/lib/cache/types.d.ts +1 -0
  21. package/lib/cache/types.d.ts.map +1 -1
  22. package/lib/index.d.ts +2 -2
  23. package/lib/index.d.ts.map +1 -1
  24. package/lib/index.js +1 -1
  25. package/lib/index.js.map +1 -1
  26. package/lib/lightClient/spec/utils.d.ts.map +1 -1
  27. package/lib/lightClient/spec/utils.js +6 -0
  28. package/lib/lightClient/spec/utils.js.map +1 -1
  29. package/lib/signatureSets/executionPayloadBid.d.ts +2 -2
  30. package/lib/signatureSets/executionPayloadBid.d.ts.map +1 -1
  31. package/lib/signatureSets/executionPayloadBid.js +7 -5
  32. package/lib/signatureSets/executionPayloadBid.js.map +1 -1
  33. package/lib/slot/index.d.ts +1 -0
  34. package/lib/slot/index.d.ts.map +1 -1
  35. package/lib/slot/index.js +1 -0
  36. package/lib/slot/index.js.map +1 -1
  37. package/lib/slot/upgradeStateToGloas.js +13 -7
  38. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  39. package/lib/slot/upgradeStateToHeze.d.ts +7 -0
  40. package/lib/slot/upgradeStateToHeze.d.ts.map +1 -0
  41. package/lib/slot/upgradeStateToHeze.js +83 -0
  42. package/lib/slot/upgradeStateToHeze.js.map +1 -0
  43. package/lib/stateTransition.d.ts.map +1 -1
  44. package/lib/stateTransition.js +4 -1
  45. package/lib/stateTransition.js.map +1 -1
  46. package/lib/stateView/beaconStateView.d.ts +3 -0
  47. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  48. package/lib/stateView/beaconStateView.js +7 -0
  49. package/lib/stateView/beaconStateView.js.map +1 -1
  50. package/lib/stateView/interface.d.ts +16 -3
  51. package/lib/stateView/interface.d.ts.map +1 -1
  52. package/lib/stateView/interface.js +4 -1
  53. package/lib/stateView/interface.js.map +1 -1
  54. package/lib/stateView/nativeBeaconStateView.d.ts +3 -0
  55. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  56. package/lib/stateView/nativeBeaconStateView.js +6 -0
  57. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  58. package/lib/testUtils/util.d.ts +16 -1
  59. package/lib/testUtils/util.d.ts.map +1 -1
  60. package/lib/testUtils/util.js +55 -2
  61. package/lib/testUtils/util.js.map +1 -1
  62. package/lib/types.d.ts +2 -2
  63. package/lib/types.d.ts.map +1 -1
  64. package/lib/util/genesis.d.ts.map +1 -1
  65. package/lib/util/genesis.js +5 -0
  66. package/lib/util/genesis.js.map +1 -1
  67. package/lib/util/index.d.ts +1 -0
  68. package/lib/util/index.d.ts.map +1 -1
  69. package/lib/util/index.js +1 -0
  70. package/lib/util/index.js.map +1 -1
  71. package/lib/util/pendingDepositsLookup.d.ts +7 -10
  72. package/lib/util/pendingDepositsLookup.d.ts.map +1 -1
  73. package/lib/util/pendingDepositsLookup.js +10 -19
  74. package/lib/util/pendingDepositsLookup.js.map +1 -1
  75. package/lib/util/preVerifyBuilderDeposits.d.ts +52 -0
  76. package/lib/util/preVerifyBuilderDeposits.d.ts.map +1 -0
  77. package/lib/util/preVerifyBuilderDeposits.js +124 -0
  78. package/lib/util/preVerifyBuilderDeposits.js.map +1 -0
  79. package/package.json +7 -7
  80. package/src/block/processDeposit.ts +75 -1
  81. package/src/block/processExecutionPayloadBid.ts +18 -8
  82. package/src/cache/builderDepositSignatureCache.ts +81 -0
  83. package/src/cache/epochCache.ts +14 -0
  84. package/src/cache/stateCache.ts +2 -0
  85. package/src/cache/types.ts +1 -0
  86. package/src/index.ts +4 -0
  87. package/src/lightClient/spec/utils.ts +7 -0
  88. package/src/signatureSets/executionPayloadBid.ts +7 -9
  89. package/src/slot/index.ts +1 -0
  90. package/src/slot/upgradeStateToGloas.ts +17 -10
  91. package/src/slot/upgradeStateToHeze.ts +91 -0
  92. package/src/stateTransition.ts +5 -0
  93. package/src/stateView/beaconStateView.ts +13 -0
  94. package/src/stateView/interface.ts +21 -2
  95. package/src/stateView/nativeBeaconStateView.ts +9 -0
  96. package/src/testUtils/util.ts +73 -3
  97. package/src/types.ts +2 -0
  98. package/src/util/genesis.ts +6 -0
  99. package/src/util/index.ts +1 -0
  100. package/src/util/pendingDepositsLookup.ts +13 -24
  101. package/src/util/preVerifyBuilderDeposits.ts +153 -0
@@ -334,6 +334,12 @@ export function initializeBeaconStateFromEth1(
334
334
  stateGloas.fork.currentVersion = config.GLOAS_FORK_VERSION;
335
335
  }
336
336
 
337
+ if (fork >= ForkSeq.heze) {
338
+ const stateHeze = state as CompositeViewDU<typeof ssz.heze.BeaconState>;
339
+ stateHeze.fork.previousVersion = config.HEZE_FORK_VERSION;
340
+ stateHeze.fork.currentVersion = config.HEZE_FORK_VERSION;
341
+ }
342
+
337
343
  state.commit();
338
344
 
339
345
  return state;
package/src/util/index.ts CHANGED
@@ -20,6 +20,7 @@ export * from "./gloas.js";
20
20
  export * from "./interop.js";
21
21
  export * from "./loadState/index.js";
22
22
  export * from "./pendingDepositsLookup.js";
23
+ export * from "./preVerifyBuilderDeposits.js";
23
24
  export * from "./rootCache.js";
24
25
  export * from "./seed.js";
25
26
  export * from "./shuffling.js";
@@ -1,8 +1,7 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
2
  import {PubkeyHex, electra} from "@lodestar/types";
3
- import {toPubkeyHex} from "@lodestar/utils";
4
3
  import {isValidDepositSignature} from "../block/processDeposit.js";
5
- import {CachedBeaconStateGloas} from "../types.js";
4
+ import {BuilderDepositSignatureCache} from "../cache/builderDepositSignatureCache.js";
6
5
 
7
6
  type PendingDepositsValidation = {
8
7
  hasValidSignature: boolean;
@@ -34,28 +33,14 @@ export class PendingDepositsLookup {
34
33
  }
35
34
 
36
35
  /**
37
- * Build a pubkey -> pending-deposits lookup from `state.pendingDeposits`.
38
- * No BLS work is done here; signature verification happens lazily in `hasPendingValidator`.
36
+ * Append a pending deposit to the represented sequence, keyed by its (precomputed) `pubkeyHex`.
39
37
  */
40
- static build(state: CachedBeaconStateGloas): PendingDepositsLookup {
41
- const lookup = PendingDepositsLookup.buildEmpty();
42
- for (const pendingDeposit of state.pendingDeposits.getAllReadonly()) {
43
- lookup.add(pendingDeposit);
44
- }
45
- return lookup;
46
- }
47
-
48
- /**
49
- * Append a pending deposit to the represented sequence.
50
- * Pass `pubkeyHex` if the caller has already computed it.
51
- */
52
- add(pendingDeposit: electra.PendingDeposit, pubkeyHex?: PubkeyHex): void {
53
- const key = pubkeyHex ?? toPubkeyHex(pendingDeposit.pubkey);
54
- const existing = this.depositsByPubkey.get(key);
38
+ add(pendingDeposit: electra.PendingDeposit, pubkeyHex: PubkeyHex): void {
39
+ const existing = this.depositsByPubkey.get(pubkeyHex);
55
40
  if (existing) {
56
41
  existing.push(pendingDeposit);
57
42
  } else {
58
- this.depositsByPubkey.set(key, [pendingDeposit]);
43
+ this.depositsByPubkey.set(pubkeyHex, [pendingDeposit]);
59
44
  }
60
45
  }
61
46
 
@@ -63,8 +48,11 @@ export class PendingDepositsLookup {
63
48
  * Returns true if any pending deposit for `pubkeyHex` has a valid BLS deposit signature.
64
49
  * Memoizes the result in `validationCache` so repeated checks for the same pubkey
65
50
  * within a block only verify deposits that have not already been checked.
51
+ *
52
+ * Most of the time it should hit the BuilderDepositSignatureCache so we don't have to validate
53
+ * signatures here.
66
54
  */
67
- hasPendingValidator(config: BeaconConfig, pubkeyHex: PubkeyHex): boolean {
55
+ hasPendingValidator(config: BeaconConfig, pubkeyHex: PubkeyHex, cache: BuilderDepositSignatureCache): boolean {
68
56
  const validation = this.validationCache.get(pubkeyHex);
69
57
  if (validation?.hasValidSignature === true) {
70
58
  return true;
@@ -85,15 +73,16 @@ export class PendingDepositsLookup {
85
73
 
86
74
  for (let i = startIndex; i < deposits.length; i++) {
87
75
  const deposit = deposits[i];
88
- if (
76
+ const isValid =
77
+ cache.getSignatureValidity(deposit) ??
89
78
  isValidDepositSignature(
90
79
  config,
91
80
  deposit.pubkey,
92
81
  deposit.withdrawalCredentials,
93
82
  deposit.amount,
94
83
  deposit.signature
95
- )
96
- ) {
84
+ );
85
+ if (isValid) {
97
86
  this.validationCache.set(pubkeyHex, {hasValidSignature: true, validatedCount: i + 1});
98
87
  return true;
99
88
  }
@@ -0,0 +1,153 @@
1
+ import {electra} from "@lodestar/types";
2
+ import {toPubkeyHex} from "@lodestar/utils";
3
+ import {verifyDepositSignatures} from "../block/processDeposit.js";
4
+ import {CachedBeaconStateFulu} from "../types.js";
5
+ import {isBuilderWithdrawalCredential} from "./gloas.js";
6
+
7
+ /** Verify queued deposit signatures in batches of this size. Also the time-box granularity: the
8
+ * deadline is checked once per batch, so overrun is bounded to a single batch. Exported for tests. */
9
+ export const BUILDER_DEPOSIT_BATCH_SIZE = 32;
10
+
11
+ /**
12
+ * Per-tick cap on builder deposits the prepareForNextSlot scanner will verify.
13
+ * ~2.7s for 10_000 BLS verifications on a typical server — fits within the slot budget while the
14
+ * pre-window (see GLOAS_PREVERIFY_WINDOW_EPOCHS) covers up to ~620k deposits over its duration.
15
+ */
16
+ export const MAX_BUILDER_DEPOSITS_PER_SLOT = 10_000;
17
+
18
+ /**
19
+ * Number of epochs before GLOAS_FORK_EPOCH during which the prepareForNextSlot scanner pre-verifies
20
+ * builder-deposit signatures. Two: deposits arriving this late cannot collude with validator
21
+ * deposits — validator-vs-builder routing is decided at the fork boundary from what is already in
22
+ * pendingDeposits, so anything submitted inside this window is unambiguous.
23
+ */
24
+ export const GLOAS_PREVERIFY_WINDOW_EPOCHS = 2;
25
+
26
+ /**
27
+ * Summary of a single `preVerifyBuilderDepositsPreGloas` call.
28
+ */
29
+ export type PreVerifyBuilderDepositsResult = {
30
+ /** Builder-prefix deposit signatures verified valid in this call (cached true). */
31
+ validBuilderSignaturesCount: number;
32
+ /** Builder-prefix deposit signatures verified invalid in this call (cached false, dropped at fork). */
33
+ invalidBuilderSignaturesCount: number;
34
+ /** Validator (builder-pubkey) deposit signatures verified valid in this call. */
35
+ validValidatorSignaturesCount: number;
36
+ /** Validator (builder-pubkey) deposit signatures verified invalid in this call (abuse signal). */
37
+ invalidValidatorSignaturesCount: number;
38
+ /** Pending deposits examined this call (all creds; < pendingDepositsCount ⇒ per-tick cap hit). */
39
+ scannedPendingDeposits: number;
40
+ /** Cumulative deposits verified & cached so far this window (cache size, pass + fail). */
41
+ totalCachedDeposits: number;
42
+ /** Distinct builder pubkeys tracked so far this window. */
43
+ totalBuilderPubkeys: number;
44
+ /** Current `state.pendingDeposits.length` — the full backlog. */
45
+ pendingDepositsCount: number;
46
+ };
47
+
48
+ /**
49
+ * Scanner driven by `prepareForNextSlot` over the `GLOAS_PREVERIFY_WINDOW_EPOCHS` epochs before
50
+ * GLOAS_FORK_EPOCH. Walks `state.pendingDeposits` (as struct values via getAllReadonlyValues) and
51
+ * queues, for signature verification, the deposits the fork transition will check on its hot path:
52
+ * - builder-prefix deposits (for onboarding); records each builder pubkey on the cache, and
53
+ * - validator deposits sharing a builder pubkey (the set `hasPendingValidator` walks at the fork),
54
+ * but only until one is found valid — `hasPendingValidator` stops at the first valid signature, so
55
+ * the cache marks the pubkey and the scanner skips that pubkey's remaining validator deposits.
56
+ *
57
+ * Bounded by two knobs: at most `maxBuilderDeposits` new deposits are queued per call, and the chunk
58
+ * verification is **time-boxed** to `maxDurationMs`.
59
+ */
60
+ export function preVerifyBuilderDepositsPreGloas(
61
+ state: CachedBeaconStateFulu,
62
+ maxBuilderDeposits: number,
63
+ maxDurationMs: number
64
+ ): PreVerifyBuilderDepositsResult {
65
+ const cache = state.epochCtx.builderDepositSignatureCache;
66
+
67
+ // Collect uncached deposits to verify as struct values. Full rescan each tick: iteration is
68
+ // O(pendingDeposits) but runs in prepareNextSlot spare time, and isVerified() is an O(1) skip for
69
+ // already-cached deposits. For a ContainerNodeStructType, getAllReadonlyValues() returns each
70
+ // node.value by reference — the stable cache key.
71
+ const queue: electra.PendingDeposit[] = [];
72
+ let scannedPendingDeposits = 0;
73
+ for (const deposit of state.pendingDeposits.getAllReadonlyValues()) {
74
+ if (queue.length >= maxBuilderDeposits) break; // per-tick cap; next tick resumes via isVerified()
75
+ scannedPendingDeposits++;
76
+ if (cache.isVerified(deposit)) continue; // already verified in an earlier tick
77
+
78
+ if (isBuilderWithdrawalCredential(deposit.withdrawalCredentials)) {
79
+ // Record the builder pubkey so validator deposits sharing it get pre-verified — this or a
80
+ // later tick, since those validator deposits precede the builder deposit in the queue. A cached
81
+ // builder deposit already added its pubkey, so it's safe to skip via the `continue` above.
82
+ cache.addBuilderPubkey(toPubkeyHex(deposit.pubkey));
83
+ queue.push(deposit);
84
+ } else {
85
+ // Validator deposit — pre-verify only if it shares a builder pubkey (the set hasPendingValidator
86
+ // walks at the fork) AND we haven't already cached a valid validator deposit for that pubkey.
87
+ // this is the same logic to hasPendingValidator.
88
+ const pubkeyHex = toPubkeyHex(deposit.pubkey);
89
+ if (cache.isBuilderPubkey(pubkeyHex) && !cache.hasValidValidatorDeposit(pubkeyHex)) {
90
+ queue.push(deposit);
91
+ }
92
+ }
93
+ }
94
+
95
+ const pendingDepositsCount = state.pendingDeposits.length;
96
+ if (queue.length === 0) {
97
+ return {
98
+ validBuilderSignaturesCount: 0,
99
+ invalidBuilderSignaturesCount: 0,
100
+ validValidatorSignaturesCount: 0,
101
+ invalidValidatorSignaturesCount: 0,
102
+ scannedPendingDeposits,
103
+ totalCachedDeposits: cache.cachedDepositCount,
104
+ totalBuilderPubkeys: cache.builderPubkeyCount,
105
+ pendingDepositsCount,
106
+ };
107
+ }
108
+
109
+ // Verify in chunks; chunking caps the fallback blast radius so one bad signature only forces 32
110
+ // individual re-verifications. Key each result by the deposit value object. Counts are tracked
111
+ // incrementally (not queue.length - verified) so they stay correct when we time-box out early.
112
+ const deadline = Date.now() + maxDurationMs;
113
+ let validBuilderSignaturesCount = 0;
114
+ let invalidBuilderSignaturesCount = 0;
115
+ let validValidatorSignaturesCount = 0;
116
+ let invalidValidatorSignaturesCount = 0;
117
+ for (let chunkStart = 0; chunkStart < queue.length; chunkStart += BUILDER_DEPOSIT_BATCH_SIZE) {
118
+ const end = Math.min(chunkStart + BUILDER_DEPOSIT_BATCH_SIZE, queue.length);
119
+ const chunk = queue.slice(chunkStart, end);
120
+ const chunkResults = verifyDepositSignatures(state.epochCtx.config, chunk);
121
+ for (let j = 0; j < chunk.length; j++) {
122
+ cache.setSignatureValidity(chunk[j], chunkResults[j]);
123
+ const isBuilder = isBuilderWithdrawalCredential(chunk[j].withdrawalCredentials);
124
+ if (chunkResults[j]) {
125
+ if (isBuilder) {
126
+ validBuilderSignaturesCount++;
127
+ } else {
128
+ validValidatorSignaturesCount++;
129
+ // A valid validator deposit satisfies hasPendingValidator() for this pubkey; stop
130
+ // pre-verifying its remaining validator deposits on later ticks.
131
+ cache.setValidValidatorDeposit(toPubkeyHex(chunk[j].pubkey));
132
+ }
133
+ } else if (isBuilder) {
134
+ invalidBuilderSignaturesCount++;
135
+ } else {
136
+ invalidValidatorSignaturesCount++;
137
+ }
138
+ }
139
+ // Time-box: keep everything verified so far (already cached) and stop
140
+ if (Date.now() >= deadline) break;
141
+ }
142
+
143
+ return {
144
+ validBuilderSignaturesCount,
145
+ invalidBuilderSignaturesCount,
146
+ validValidatorSignaturesCount,
147
+ invalidValidatorSignaturesCount,
148
+ scannedPendingDeposits,
149
+ totalCachedDeposits: cache.cachedDepositCount,
150
+ totalBuilderPubkeys: cache.builderPubkeyCount,
151
+ pendingDepositsCount,
152
+ };
153
+ }