@lodestar/state-transition 1.50.0-dev.9c3f7bebe9 → 1.50.0-dev.9c7f3c203f

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 (72) hide show
  1. package/lib/block/indexedBuilderState.d.ts +19 -0
  2. package/lib/block/indexedBuilderState.d.ts.map +1 -0
  3. package/lib/block/indexedBuilderState.js +60 -0
  4. package/lib/block/indexedBuilderState.js.map +1 -0
  5. package/lib/block/processBuilderDepositRequest.d.ts +2 -2
  6. package/lib/block/processBuilderDepositRequest.d.ts.map +1 -1
  7. package/lib/block/processBuilderDepositRequest.js +5 -4
  8. package/lib/block/processBuilderDepositRequest.js.map +1 -1
  9. package/lib/block/processBuilderExitRequest.d.ts +2 -2
  10. package/lib/block/processBuilderExitRequest.d.ts.map +1 -1
  11. package/lib/block/processBuilderExitRequest.js +4 -3
  12. package/lib/block/processBuilderExitRequest.js.map +1 -1
  13. package/lib/block/processParentExecutionPayload.d.ts.map +1 -1
  14. package/lib/block/processParentExecutionPayload.js +23 -18
  15. package/lib/block/processParentExecutionPayload.js.map +1 -1
  16. package/lib/index.d.ts +3 -1
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +2 -0
  19. package/lib/index.js.map +1 -1
  20. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  21. package/lib/rewards/attestationsRewards.js +8 -1
  22. package/lib/rewards/attestationsRewards.js.map +1 -1
  23. package/lib/signatureSets/index.d.ts.map +1 -1
  24. package/lib/signatureSets/index.js +2 -2
  25. package/lib/signatureSets/index.js.map +1 -1
  26. package/lib/signatureSets/voluntaryExits.d.ts +2 -6
  27. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  28. package/lib/signatureSets/voluntaryExits.js +2 -32
  29. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  30. package/lib/stateView/beaconStateView.d.ts +22 -21
  31. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  32. package/lib/stateView/beaconStateView.js +71 -71
  33. package/lib/stateView/beaconStateView.js.map +1 -1
  34. package/lib/stateView/interface.d.ts +34 -21
  35. package/lib/stateView/interface.d.ts.map +1 -1
  36. package/lib/stateView/interface.js.map +1 -1
  37. package/lib/stateView/nativeBeaconStateView.d.ts +92 -100
  38. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  39. package/lib/stateView/nativeBeaconStateView.js +339 -368
  40. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  41. package/lib/stateView/stateViewFactory.d.ts +10 -17
  42. package/lib/stateView/stateViewFactory.d.ts.map +1 -1
  43. package/lib/stateView/stateViewFactory.js +17 -13
  44. package/lib/stateView/stateViewFactory.js.map +1 -1
  45. package/lib/util/gloas.d.ts +11 -14
  46. package/lib/util/gloas.d.ts.map +1 -1
  47. package/lib/util/gloas.js +2 -42
  48. package/lib/util/gloas.js.map +1 -1
  49. package/lib/util/index.d.ts +1 -1
  50. package/lib/util/index.d.ts.map +1 -1
  51. package/lib/util/index.js +1 -1
  52. package/lib/util/index.js.map +1 -1
  53. package/lib/util/sszBytes.d.ts +3 -0
  54. package/lib/util/sszBytes.d.ts.map +1 -1
  55. package/lib/util/sszBytes.js +40 -14
  56. package/lib/util/sszBytes.js.map +1 -1
  57. package/package.json +8 -8
  58. package/src/block/indexedBuilderState.ts +65 -0
  59. package/src/block/processBuilderDepositRequest.ts +6 -17
  60. package/src/block/processBuilderExitRequest.ts +5 -9
  61. package/src/block/processParentExecutionPayload.ts +27 -23
  62. package/src/index.ts +4 -1
  63. package/src/rewards/attestationsRewards.ts +8 -1
  64. package/src/signatureSets/index.ts +4 -2
  65. package/src/signatureSets/voluntaryExits.ts +3 -55
  66. package/src/stateView/beaconStateView.ts +75 -76
  67. package/src/stateView/interface.ts +61 -21
  68. package/src/stateView/nativeBeaconStateView.ts +354 -375
  69. package/src/stateView/stateViewFactory.ts +29 -27
  70. package/src/util/gloas.ts +2 -53
  71. package/src/util/index.ts +6 -1
  72. package/src/util/sszBytes.ts +52 -16
@@ -1,68 +1,70 @@
1
- import {type PubkeyCache, pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
1
+ import bindings from "@chainsafe/lodestar-z";
2
+ import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
2
3
  import {BeaconConfig} from "@lodestar/config";
3
4
  import {createCachedBeaconState} from "../cache/stateCache.js";
4
- import {BeaconStateAllForks} from "../cache/types.js";
5
5
  import {getStateTypeFromBytes} from "../util/sszBytes.js";
6
6
  import {BeaconStateView} from "./beaconStateView.js";
7
- import {IBeaconStateView} from "./interface.js";
7
+ import {IBeaconStateView, IBeaconStateViewNative} from "./interface.js";
8
+ import {NativeBeaconStateView} from "./nativeBeaconStateView.js";
8
9
 
9
10
  // ---- createBeaconStateView (startup path) ----
10
11
 
11
- type NodeJSOpts = {
12
- useNative: false;
13
- anchorState: BeaconStateAllForks;
12
+ type CreateBeaconStateViewOpts = {
13
+ nativeStateTransition: boolean;
14
14
  config: BeaconConfig;
15
- pubkeyCache: PubkeyCache;
16
- };
17
-
18
- type NativeOpts = {
19
- useNative: true;
20
15
  stateBytes: Uint8Array;
21
16
  };
22
17
 
23
18
  /**
24
- * Create a BeaconStateView from a pre-deserialized state. Used at node startup.
19
+ * Create a BeaconStateView from raw SSZ bytes. Used at node startup.
25
20
  *
26
- * The caller is responsible for creating and populating `pubkeyCache` (it is also
27
- * passed separately to BeaconNode.init, so it must live outside this factory).
21
+ * Caller must reserve pubkey capacity before calling this function.
28
22
  *
29
- * Set `useNative: true` to use the native (Zig) implementation once available.
23
+ * Set `nativeStateTransition: true` to use the native (Zig) implementation.
30
24
  */
31
- export function createBeaconStateView(opts: NodeJSOpts | NativeOpts): IBeaconStateView {
32
- if (opts.useNative) {
33
- throw new Error("Native (Zig) BeaconStateView not yet implemented");
34
- // TODO: return a new instance of NativeBeaconStateView
25
+ export function createBeaconStateView(opts: CreateBeaconStateViewOpts): IBeaconStateView {
26
+ if (opts.nativeStateTransition) {
27
+ return createNativeBeaconStateView(opts.config, opts.stateBytes);
35
28
  }
36
- const {anchorState, config, pubkeyCache} = opts;
37
- const cachedState = createCachedBeaconState(anchorState, {config, pubkeyCache}, {skipSyncPubkeys: true});
29
+ const {config, stateBytes} = opts;
30
+ const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
31
+ const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: false});
38
32
  return new BeaconStateView(cachedState);
39
33
  }
40
34
 
41
35
  // ---- createBeaconStateViewForHistoricalRegen (regen path) ----
42
36
 
43
37
  type RegenNodeJSOpts = {
44
- useNative: false;
38
+ nativeStateTransition: false;
45
39
  config: BeaconConfig;
46
40
  stateBytes: Uint8Array;
47
41
  };
48
42
 
49
43
  type RegenNativeOpts = {
50
- useNative: true;
44
+ nativeStateTransition: true;
45
+ config: BeaconConfig;
51
46
  stateBytes: Uint8Array;
52
47
  };
53
48
 
54
49
  /**
55
50
  * Create a BeaconStateView from raw SSZ bytes. Used in the historical state regen worker thread.
56
51
  *
57
- * Set `useNative: true` to use the native (Zig) implementation once available.
52
+ * Set `nativeStateTransition: true` to use the native (Zig) implementation.
58
53
  */
59
54
  export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts | RegenNativeOpts): IBeaconStateView {
60
- if (opts.useNative) {
61
- throw new Error("Native (Zig) BeaconStateView not yet implemented");
62
- // TODO: return a new instance of NativeBeaconStateView
55
+ if (opts.nativeStateTransition) {
56
+ return createNativeBeaconStateView(opts.config, opts.stateBytes);
63
57
  }
64
58
  const {config, stateBytes} = opts;
65
59
  const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
66
60
  const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: true});
67
61
  return new BeaconStateView(cachedState);
68
62
  }
63
+
64
+ function createNativeBeaconStateView(config: BeaconConfig, stateBytes: Uint8Array): IBeaconStateView {
65
+ const nativeConfig = new bindings.BeaconConfig(config, config.genesisValidatorsRoot);
66
+ return new NativeBeaconStateView(
67
+ config,
68
+ bindings.BeaconStateView.createFromBytes(stateBytes, nativeConfig) as IBeaconStateViewNative
69
+ );
70
+ }
package/src/util/gloas.ts CHANGED
@@ -178,21 +178,6 @@ export function initiateBuilderExit(state: CachedBeaconStateGloas, builderIndex:
178
178
  builder.withdrawableEpoch = currentEpoch + state.config.MIN_BUILDER_WITHDRAWABILITY_DELAY;
179
179
  }
180
180
 
181
- /**
182
- * Find the index of a builder by their public key.
183
- * Returns null if not found.
184
- *
185
- * May consider builder pubkey cache if performance becomes an issue.
186
- */
187
- export function findBuilderIndexByPubkey(state: CachedBeaconStateGloas, pubkey: Uint8Array): BuilderIndex | null {
188
- for (let i = 0; i < state.builders.length; i++) {
189
- if (byteArrayEquals(state.builders.getReadonly(i).pubkey, pubkey)) {
190
- return i;
191
- }
192
- }
193
- return null;
194
- }
195
-
196
181
  /**
197
182
  * Use cached block roots to avoid repeated state root lookups while matching the spec's is_attestation_same_slot behavior.
198
183
  */
@@ -248,41 +233,6 @@ export function getPtcWindowEpochCacheData(state: CachedBeaconStateGloas): {
248
233
  };
249
234
  }
250
235
 
251
- /**
252
- * Add a new builder to the builders registry. Reuses slots from exited and fully withdrawn
253
- * builders when available, otherwise appends.
254
- *
255
- * Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/beacon-chain.md#new-add_builder_to_registry
256
- */
257
- export function addBuilderToRegistry(
258
- state: CachedBeaconStateGloas,
259
- pubkey: Uint8Array,
260
- version: number,
261
- executionAddress: Uint8Array,
262
- amount: number,
263
- slot: number
264
- ): void {
265
- const currentEpoch = computeEpochAtSlot(state.slot);
266
- const depositEpoch = computeEpochAtSlot(slot);
267
-
268
- let builderIndex = state.builders.length;
269
- for (let i = 0; i < state.builders.length; i++) {
270
- const builder = state.builders.getReadonly(i);
271
- if (builder.withdrawableEpoch <= currentEpoch && builder.balance === 0) {
272
- builderIndex = i;
273
- break;
274
- }
275
- }
276
-
277
- const newBuilder = createBuilderView(pubkey, version, executionAddress, amount, depositEpoch);
278
-
279
- if (builderIndex < state.builders.length) {
280
- state.builders.set(builderIndex, newBuilder);
281
- } else {
282
- state.builders.push(newBuilder);
283
- }
284
- }
285
-
286
236
  /**
287
237
  * Append a new builder to the registry without scanning for a reusable slot.
288
238
  *
@@ -301,10 +251,9 @@ export function appendBuilderToRegistry(
301
251
  }
302
252
 
303
253
  /**
304
- * Build a Builder view for registry insertion. Shared by the scan-based {@link addBuilderToRegistry}
305
- * and the append-only {@link appendBuilderToRegistry} so both paths produce an identical view.
254
+ * Build a Builder view from builder fields.
306
255
  */
307
- function createBuilderView(
256
+ export function createBuilderView(
308
257
  pubkey: Uint8Array,
309
258
  version: number,
310
259
  executionAddress: Uint8Array,
package/src/util/index.ts CHANGED
@@ -28,7 +28,12 @@ export * from "./signatureSets.js";
28
28
  export * from "./signingRoot.js";
29
29
  export * from "./slot.js";
30
30
  export * from "./ssz.js";
31
- export {type StateBytesMetadata, readBeaconStateBytesMetadata} from "./sszBytes.js";
31
+ export {
32
+ type StateBytesMetadata,
33
+ getValidatorCountFromStateBytes,
34
+ getValidatorPubkeyFromStateBytes,
35
+ readBeaconStateBytesMetadata,
36
+ } from "./sszBytes.js";
32
37
  export * from "./syncCommittee.js";
33
38
  export * from "./validator.js";
34
39
  export * from "./weakSubjectivity.js";
@@ -19,7 +19,8 @@ const ROOT_SIZE = 32;
19
19
  * withdrawable_epoch: Epoch [fixed - 8 bytes]
20
20
  * ```
21
21
  */
22
- const VALIDATOR_EFFECTIVE_BALANCE_OFFSET = 48 + ROOT_SIZE;
22
+ const PUBKEY_SIZE = 48;
23
+ const VALIDATOR_EFFECTIVE_BALANCE_OFFSET = PUBKEY_SIZE + ROOT_SIZE;
23
24
  const VALIDATOR_ACTIVATION_EPOCH_OFFSET = VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE + 1 + UINT64_SIZE;
24
25
  const VALIDATOR_EXIT_EPOCH_OFFSET = VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE;
25
26
  export const VALIDATOR_BYTES_SIZE = VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE + UINT64_SIZE;
@@ -77,25 +78,15 @@ export function scanActiveValidatorsFromStateBytes(
77
78
  stateType: SSZTypesFor<ForkAll, "BeaconState">,
78
79
  epoch: Epoch
79
80
  ): {activeValidatorCount: number; totalActiveBalanceIncrements: number} | null {
81
+ const range = getValidatorsRangeFromStateBytes(stateBytes, stateType);
82
+ if (range === null) {
83
+ return null;
84
+ }
85
+
80
86
  const data = {
81
87
  uint8Array: stateBytes,
82
88
  dataView: new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength),
83
89
  };
84
- let range: {start: number; end: number};
85
- try {
86
- const containerType = stateType as (typeof ssz)[ForkAll]["BeaconState"];
87
- const ranges = containerType.getFieldRanges(data.dataView, 0, stateBytes.length);
88
- range = ranges[Object.keys(containerType.fields).indexOf("validators")];
89
- } catch {
90
- return null;
91
- }
92
- if (!range || range.start < 0 || range.end > stateBytes.length || range.end < range.start) {
93
- return null;
94
- }
95
- if ((range.end - range.start) % VALIDATOR_BYTES_SIZE !== 0) {
96
- return null;
97
- }
98
-
99
90
  let activeValidatorCount = 0;
100
91
  let totalActiveBalanceIncrements = 0;
101
92
  for (let offset = range.start; offset < range.end; offset += VALIDATOR_BYTES_SIZE) {
@@ -121,3 +112,48 @@ export function scanActiveValidatorsFromStateBytes(
121
112
  }
122
113
  return {activeValidatorCount, totalActiveBalanceIncrements: Math.max(1, totalActiveBalanceIncrements)};
123
114
  }
115
+
116
+ export function getValidatorCountFromStateBytes(config: ChainForkConfig, stateBytes: Uint8Array): number | null {
117
+ const range = getValidatorsRangeFromStateBytes(stateBytes, getStateTypeFromBytes(config, stateBytes));
118
+ // A non-null range always holds a whole number of validator records, so the division is exact
119
+ return range === null ? null : (range.end - range.start) / VALIDATOR_BYTES_SIZE;
120
+ }
121
+
122
+ /** Returns a view into `stateBytes`, or null if the index is not in the registry. */
123
+ export function getValidatorPubkeyFromStateBytes(
124
+ config: ChainForkConfig,
125
+ stateBytes: Uint8Array,
126
+ index: number
127
+ ): Uint8Array | null {
128
+ const range = getValidatorsRangeFromStateBytes(stateBytes, getStateTypeFromBytes(config, stateBytes));
129
+ if (range === null) {
130
+ return null;
131
+ }
132
+ const start = range.start + index * VALIDATOR_BYTES_SIZE;
133
+ if (index < 0 || start >= range.end) {
134
+ return null;
135
+ }
136
+ return stateBytes.subarray(start, start + PUBKEY_SIZE);
137
+ }
138
+
139
+ function getValidatorsRangeFromStateBytes(
140
+ stateBytes: Uint8Array,
141
+ stateType: SSZTypesFor<ForkAll, "BeaconState">
142
+ ): {start: number; end: number} | null {
143
+ let range: {start: number; end: number};
144
+ try {
145
+ const containerType = stateType as (typeof ssz)[ForkAll]["BeaconState"];
146
+ const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
147
+ const ranges = containerType.getFieldRanges(dataView, 0, stateBytes.length);
148
+ range = ranges[Object.keys(containerType.fields).indexOf("validators")];
149
+ } catch {
150
+ return null;
151
+ }
152
+ if (!range || range.start < 0 || range.end > stateBytes.length || range.end < range.start) {
153
+ return null;
154
+ }
155
+ if ((range.end - range.start) % VALIDATOR_BYTES_SIZE !== 0) {
156
+ return null;
157
+ }
158
+ return range;
159
+ }