@lodestar/state-transition 1.42.0-dev.5f9f015475 → 1.42.0-dev.6fa48cb5f2

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 (47) hide show
  1. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  2. package/lib/block/isValidIndexedAttestation.js +2 -3
  3. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  4. package/lib/block/processAttestationsAltair.d.ts +2 -1
  5. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  6. package/lib/block/processAttestationsAltair.js +5 -3
  7. package/lib/block/processAttestationsAltair.js.map +1 -1
  8. package/lib/signatureSets/index.d.ts +2 -2
  9. package/lib/signatureSets/index.d.ts.map +1 -1
  10. package/lib/signatureSets/index.js +1 -2
  11. package/lib/signatureSets/index.js.map +1 -1
  12. package/lib/slot/upgradeStateToAltair.d.ts.map +1 -1
  13. package/lib/slot/upgradeStateToAltair.js +2 -1
  14. package/lib/slot/upgradeStateToAltair.js.map +1 -1
  15. package/lib/stateView/beaconStateView.d.ts +6 -9
  16. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  17. package/lib/stateView/beaconStateView.js +8 -32
  18. package/lib/stateView/beaconStateView.js.map +1 -1
  19. package/lib/stateView/index.d.ts +1 -0
  20. package/lib/stateView/index.d.ts.map +1 -1
  21. package/lib/stateView/index.js +1 -0
  22. package/lib/stateView/index.js.map +1 -1
  23. package/lib/stateView/interface.d.ts +12 -8
  24. package/lib/stateView/interface.d.ts.map +1 -1
  25. package/lib/stateView/stateViewFactory.d.ts +40 -0
  26. package/lib/stateView/stateViewFactory.d.ts.map +1 -0
  27. package/lib/stateView/stateViewFactory.js +46 -0
  28. package/lib/stateView/stateViewFactory.js.map +1 -0
  29. package/lib/util/rootCache.d.ts +2 -2
  30. package/lib/util/rootCache.d.ts.map +1 -1
  31. package/lib/util/rootCache.js +2 -3
  32. package/lib/util/rootCache.js.map +1 -1
  33. package/lib/util/shuffling.d.ts +2 -1
  34. package/lib/util/shuffling.d.ts.map +1 -1
  35. package/lib/util/shuffling.js +2 -2
  36. package/lib/util/shuffling.js.map +1 -1
  37. package/package.json +7 -7
  38. package/src/block/isValidIndexedAttestation.ts +2 -3
  39. package/src/block/processAttestationsAltair.ts +7 -4
  40. package/src/signatureSets/index.ts +3 -4
  41. package/src/slot/upgradeStateToAltair.ts +2 -1
  42. package/src/stateView/beaconStateView.ts +16 -48
  43. package/src/stateView/index.ts +1 -0
  44. package/src/stateView/interface.ts +10 -5
  45. package/src/stateView/stateViewFactory.ts +78 -0
  46. package/src/util/rootCache.ts +4 -5
  47. package/src/util/shuffling.ts +5 -4
@@ -0,0 +1,78 @@
1
+ import {BeaconConfig} from "@lodestar/config";
2
+ import {PubkeyCache, createPubkeyCache} from "../cache/pubkeyCache.js";
3
+ import {createCachedBeaconState} from "../cache/stateCache.js";
4
+ import {BeaconStateAllForks} from "../cache/types.js";
5
+ import {getStateTypeFromBytes} from "../util/sszBytes.js";
6
+ import {BeaconStateView} from "./beaconStateView.js";
7
+ import {IBeaconStateView} from "./interface.js";
8
+
9
+ // ---- createBeaconStateView (startup path) ----
10
+
11
+ type NodeJSOpts = {
12
+ useNative: false;
13
+ anchorState: BeaconStateAllForks;
14
+ config: BeaconConfig;
15
+ pubkeyCache: PubkeyCache;
16
+ };
17
+
18
+ type NativeOpts = {
19
+ useNative: true;
20
+ stateBytes: Uint8Array;
21
+ };
22
+
23
+ /**
24
+ * Create a BeaconStateView from a pre-deserialized state. Used at node startup.
25
+ *
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).
28
+ *
29
+ * Set `useNative: true` to use the native (Zig) implementation once available.
30
+ */
31
+ export function createBeaconStateView(opts: NodeJSOpts | NativeOpts): IBeaconStateView {
32
+ if (opts.useNative) {
33
+ throw new Error("Native (Zig) BeaconStateView not yet implemented");
34
+ }
35
+ const {anchorState, config, pubkeyCache} = opts;
36
+ const cachedState = createCachedBeaconState(anchorState, {config, pubkeyCache}, {skipSyncPubkeys: true});
37
+ return new BeaconStateView(cachedState);
38
+ }
39
+
40
+ // ---- createBeaconStateViewForHistoricalRegen (regen path) ----
41
+
42
+ // Reused across all historical state regen calls in the worker thread
43
+ const pubkeyCacheRegen = createPubkeyCache();
44
+
45
+ function syncPubkeyCache(state: BeaconStateAllForks, pubkeyCache: PubkeyCache): void {
46
+ const newCount = state.validators.length;
47
+ for (let i = pubkeyCache.size; i < newCount; i++) {
48
+ const pubkey = state.validators.getReadonly(i).pubkey;
49
+ pubkeyCache.set(i, pubkey);
50
+ }
51
+ }
52
+
53
+ type RegenNodeJSOpts = {
54
+ useNative: false;
55
+ config: BeaconConfig;
56
+ stateBytes: Uint8Array;
57
+ };
58
+
59
+ type RegenNativeOpts = {
60
+ useNative: true;
61
+ stateBytes: Uint8Array;
62
+ };
63
+
64
+ /**
65
+ * Create a BeaconStateView from raw SSZ bytes. Used in the historical state regen worker thread.
66
+ *
67
+ * Set `useNative: true` to use the native (Zig) implementation once available.
68
+ */
69
+ export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts | RegenNativeOpts): IBeaconStateView {
70
+ if (opts.useNative) {
71
+ throw new Error("Native (Zig) BeaconStateView not yet implemented");
72
+ }
73
+ const {config, stateBytes} = opts;
74
+ const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
75
+ syncPubkeyCache(state, pubkeyCacheRegen);
76
+ const cachedState = createCachedBeaconState(state, {config, pubkeyCache: pubkeyCacheRegen}, {skipSyncPubkeys: true});
77
+ return new BeaconStateView(cachedState);
78
+ }
@@ -1,6 +1,5 @@
1
1
  import {Epoch, Root, Slot, phase0} from "@lodestar/types";
2
- import {CachedBeaconStateAllForks} from "../types.js";
3
- import {getBlockRoot, getBlockRootAtSlot} from "./blockRoot.js";
2
+ import {IBeaconStateView} from "../stateView/interface.js";
4
3
 
5
4
  /**
6
5
  * Cache to prevent accessing the state tree to fetch block roots repeteadly.
@@ -12,7 +11,7 @@ export class RootCache {
12
11
  private readonly blockRootEpochCache = new Map<Epoch, Root>();
13
12
  private readonly blockRootSlotCache = new Map<Slot, Root>();
14
13
 
15
- constructor(private readonly state: CachedBeaconStateAllForks) {
14
+ constructor(private readonly state: IBeaconStateView) {
16
15
  this.currentJustifiedCheckpoint = state.currentJustifiedCheckpoint;
17
16
  this.previousJustifiedCheckpoint = state.previousJustifiedCheckpoint;
18
17
  }
@@ -20,7 +19,7 @@ export class RootCache {
20
19
  getBlockRoot(epoch: Epoch): Root {
21
20
  let root = this.blockRootEpochCache.get(epoch);
22
21
  if (!root) {
23
- root = getBlockRoot(this.state, epoch);
22
+ root = this.state.getBlockRootAtEpoch(epoch);
24
23
  this.blockRootEpochCache.set(epoch, root);
25
24
  }
26
25
  return root;
@@ -29,7 +28,7 @@ export class RootCache {
29
28
  getBlockRootAtSlot(slot: Slot): Root {
30
29
  let root = this.blockRootSlotCache.get(slot);
31
30
  if (!root) {
32
- root = getBlockRootAtSlot(this.state, slot);
31
+ root = this.state.getBlockRootAtSlot(slot);
33
32
  this.blockRootSlotCache.set(slot, root);
34
33
  }
35
34
  return root;
@@ -11,6 +11,7 @@ import {
11
11
  } from "@lodestar/types";
12
12
  import {LodestarError} from "@lodestar/utils";
13
13
  import {CachedBeaconStateAllForks} from "../cache/stateCache.js";
14
+ import {IBeaconStateView} from "../stateView/interface.js";
14
15
  import {getBlockRootAtSlot} from "./blockRoot.js";
15
16
  import {computeStartSlotAtEpoch} from "./epoch.js";
16
17
  import {EpochShuffling} from "./epochShuffling.js";
@@ -22,21 +23,21 @@ import {EpochShuffling} from "./epochShuffling.js";
22
23
  * Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
23
24
  * It should be set to the latest block applied to this `state` or the genesis block root.
24
25
  */
25
- export function proposerShufflingDecisionRoot(fork: ForkName, state: CachedBeaconStateAllForks): Root | null {
26
+ export function proposerShufflingDecisionRoot(fork: ForkName, state: IBeaconStateView): Root | null {
26
27
  const decisionSlot = proposerShufflingDecisionSlot(fork, state);
27
28
  if (state.slot === decisionSlot) {
28
29
  return null;
29
30
  }
30
- return getBlockRootAtSlot(state, decisionSlot);
31
+ return state.getBlockRootAtSlot(decisionSlot);
31
32
  }
32
33
 
33
34
  /**
34
35
  * Returns the slot at which the proposer shuffling was decided. The block root at this slot
35
36
  * can be used to key the proposer shuffling for the current epoch.
36
37
  */
37
- function proposerShufflingDecisionSlot(fork: ForkName, state: CachedBeaconStateAllForks): Slot {
38
+ function proposerShufflingDecisionSlot(fork: ForkName, state: IBeaconStateView): Slot {
38
39
  // After fulu, the decision slot is in previous epoch due to deterministic proposer lookahead
39
- const epoch = isForkPostFulu(fork) ? state.epochCtx.epoch - 1 : state.epochCtx.epoch;
40
+ const epoch = isForkPostFulu(fork) ? state.epoch - 1 : state.epoch;
40
41
  const startSlot = computeStartSlotAtEpoch(epoch);
41
42
  return Math.max(startSlot - 1, 0);
42
43
  }