@lodestar/state-transition 1.50.0-dev.023ad6c012 → 1.50.0-dev.040a952dd7

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.
@@ -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
+ }