@lodestar/state-transition 1.50.0-dev.f2fb26d453 → 1.50.0-dev.fc618d94e9

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 +6 -5
  42. package/lib/stateView/stateViewFactory.d.ts.map +1 -1
  43. package/lib/stateView/stateViewFactory.js +12 -8
  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 +2 -0
  54. package/lib/util/sszBytes.d.ts.map +1 -1
  55. package/lib/util/sszBytes.js +14 -1
  56. package/lib/util/sszBytes.js.map +1 -1
  57. package/package.json +7 -7
  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 +21 -12
  70. package/src/util/gloas.ts +2 -53
  71. package/src/util/index.ts +1 -0
  72. package/src/util/sszBytes.ts +19 -1
@@ -1,14 +1,16 @@
1
+ import bindings from "@chainsafe/lodestar-z";
1
2
  import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
2
3
  import {BeaconConfig} from "@lodestar/config";
3
4
  import {createCachedBeaconState} from "../cache/stateCache.js";
4
5
  import {getStateTypeFromBytes} from "../util/sszBytes.js";
5
6
  import {BeaconStateView} from "./beaconStateView.js";
6
- import {IBeaconStateView} from "./interface.js";
7
+ import {IBeaconStateView, IBeaconStateViewNative} from "./interface.js";
8
+ import {NativeBeaconStateView} from "./nativeBeaconStateView.js";
7
9
 
8
10
  // ---- createBeaconStateView (startup path) ----
9
11
 
10
12
  type CreateBeaconStateViewOpts = {
11
- useNative: boolean;
13
+ nativeStateTransition: boolean;
12
14
  config: BeaconConfig;
13
15
  stateBytes: Uint8Array;
14
16
  };
@@ -18,12 +20,11 @@ type CreateBeaconStateViewOpts = {
18
20
  *
19
21
  * Caller must reserve pubkey capacity before calling this function.
20
22
  *
21
- * Set `useNative: true` to use the native (Zig) implementation once available.
23
+ * Set `nativeStateTransition: true` to use the native (Zig) implementation.
22
24
  */
23
25
  export function createBeaconStateView(opts: CreateBeaconStateViewOpts): IBeaconStateView {
24
- if (opts.useNative) {
25
- throw new Error("Native (Zig) BeaconStateView not yet implemented");
26
- // TODO: return a new instance of NativeBeaconStateView
26
+ if (opts.nativeStateTransition) {
27
+ return createNativeBeaconStateView(opts.config, opts.stateBytes);
27
28
  }
28
29
  const {config, stateBytes} = opts;
29
30
  const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
@@ -34,28 +35,36 @@ export function createBeaconStateView(opts: CreateBeaconStateViewOpts): IBeaconS
34
35
  // ---- createBeaconStateViewForHistoricalRegen (regen path) ----
35
36
 
36
37
  type RegenNodeJSOpts = {
37
- useNative: false;
38
+ nativeStateTransition: false;
38
39
  config: BeaconConfig;
39
40
  stateBytes: Uint8Array;
40
41
  };
41
42
 
42
43
  type RegenNativeOpts = {
43
- useNative: true;
44
+ nativeStateTransition: true;
45
+ config: BeaconConfig;
44
46
  stateBytes: Uint8Array;
45
47
  };
46
48
 
47
49
  /**
48
50
  * Create a BeaconStateView from raw SSZ bytes. Used in the historical state regen worker thread.
49
51
  *
50
- * Set `useNative: true` to use the native (Zig) implementation once available.
52
+ * Set `nativeStateTransition: true` to use the native (Zig) implementation.
51
53
  */
52
54
  export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts | RegenNativeOpts): IBeaconStateView {
53
- if (opts.useNative) {
54
- throw new Error("Native (Zig) BeaconStateView not yet implemented");
55
- // TODO: return a new instance of NativeBeaconStateView
55
+ if (opts.nativeStateTransition) {
56
+ return createNativeBeaconStateView(opts.config, opts.stateBytes);
56
57
  }
57
58
  const {config, stateBytes} = opts;
58
59
  const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
59
60
  const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: true});
60
61
  return new BeaconStateView(cachedState);
61
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
@@ -31,6 +31,7 @@ export * from "./ssz.js";
31
31
  export {
32
32
  type StateBytesMetadata,
33
33
  getValidatorCountFromStateBytes,
34
+ getValidatorPubkeyFromStateBytes,
34
35
  readBeaconStateBytesMetadata,
35
36
  } from "./sszBytes.js";
36
37
  export * from "./syncCommittee.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;
@@ -118,6 +119,23 @@ export function getValidatorCountFromStateBytes(config: ChainForkConfig, stateBy
118
119
  return range === null ? null : (range.end - range.start) / VALIDATOR_BYTES_SIZE;
119
120
  }
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
+
121
139
  function getValidatorsRangeFromStateBytes(
122
140
  stateBytes: Uint8Array,
123
141
  stateType: SSZTypesFor<ForkAll, "BeaconState">