@lodestar/state-transition 1.50.1 → 1.51.0-dev.0586e6e617

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 (211) hide show
  1. package/lib/block/index.d.ts +1 -1
  2. package/lib/block/index.d.ts.map +1 -1
  3. package/lib/block/index.js +3 -3
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  6. package/lib/block/processAttestationsAltair.js +3 -1
  7. package/lib/block/processAttestationsAltair.js.map +1 -1
  8. package/lib/block/processDeposit.d.ts +2 -2
  9. package/lib/block/processDeposit.d.ts.map +1 -1
  10. package/lib/block/processDeposit.js +1 -1
  11. package/lib/block/processDeposit.js.map +1 -1
  12. package/lib/block/processEth1Data.d.ts +3 -3
  13. package/lib/block/processEth1Data.d.ts.map +1 -1
  14. package/lib/block/processEth1Data.js.map +1 -1
  15. package/lib/block/processOperations.d.ts +1 -1
  16. package/lib/block/processOperations.d.ts.map +1 -1
  17. package/lib/block/processOperations.js +9 -7
  18. package/lib/block/processOperations.js.map +1 -1
  19. package/lib/cache/epochCache.d.ts +5 -2
  20. package/lib/cache/epochCache.d.ts.map +1 -1
  21. package/lib/cache/epochCache.js +12 -5
  22. package/lib/cache/epochCache.js.map +1 -1
  23. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  24. package/lib/cache/epochTransitionCache.js +2 -1
  25. package/lib/cache/epochTransitionCache.js.map +1 -1
  26. package/lib/cache/stateCache.d.ts +2 -1
  27. package/lib/cache/stateCache.d.ts.map +1 -1
  28. package/lib/cache/stateCache.js.map +1 -1
  29. package/lib/cache/syncCommitteeCache.d.ts +1 -1
  30. package/lib/cache/syncCommitteeCache.d.ts.map +1 -1
  31. package/lib/cache/types.d.ts +2 -1
  32. package/lib/cache/types.d.ts.map +1 -1
  33. package/lib/epoch/getRewardsAndPenalties.d.ts.map +1 -1
  34. package/lib/epoch/getRewardsAndPenalties.js +9 -2
  35. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  36. package/lib/epoch/index.d.ts +2 -2
  37. package/lib/epoch/index.d.ts.map +1 -1
  38. package/lib/epoch/index.js +5 -3
  39. package/lib/epoch/index.js.map +1 -1
  40. package/lib/epoch/processEth1DataReset.d.ts +2 -2
  41. package/lib/epoch/processEth1DataReset.d.ts.map +1 -1
  42. package/lib/epoch/processEth1DataReset.js.map +1 -1
  43. package/lib/epoch/processSyncCommitteeUpdates.js +1 -1
  44. package/lib/epoch/processSyncCommitteeUpdates.js.map +1 -1
  45. package/lib/index.d.ts +1 -1
  46. package/lib/index.d.ts.map +1 -1
  47. package/lib/index.js.map +1 -1
  48. package/lib/lightClient/spec/store.d.ts +1 -1
  49. package/lib/lightClient/spec/store.d.ts.map +1 -1
  50. package/lib/lightClient/spec/utils.d.ts.map +1 -1
  51. package/lib/lightClient/spec/utils.js +1 -1
  52. package/lib/lightClient/spec/utils.js.map +1 -1
  53. package/lib/lightClient/spec/validateLightClientUpdate.d.ts.map +1 -1
  54. package/lib/lightClient/spec/validateLightClientUpdate.js +1 -1
  55. package/lib/lightClient/spec/validateLightClientUpdate.js.map +1 -1
  56. package/lib/metrics.d.ts +1 -1
  57. package/lib/metrics.d.ts.map +1 -1
  58. package/lib/metrics.js +4 -4
  59. package/lib/metrics.js.map +1 -1
  60. package/lib/rewards/attestationsRewards.d.ts +1 -1
  61. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  62. package/lib/rewards/attestationsRewards.js +10 -3
  63. package/lib/rewards/attestationsRewards.js.map +1 -1
  64. package/lib/rewards/syncCommitteeRewards.d.ts +1 -1
  65. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
  66. package/lib/signatureSets/inclusionList.d.ts +5 -0
  67. package/lib/signatureSets/inclusionList.d.ts.map +1 -0
  68. package/lib/signatureSets/inclusionList.js +14 -0
  69. package/lib/signatureSets/inclusionList.js.map +1 -0
  70. package/lib/signatureSets/index.d.ts +1 -0
  71. package/lib/signatureSets/index.d.ts.map +1 -1
  72. package/lib/signatureSets/index.js +1 -0
  73. package/lib/signatureSets/index.js.map +1 -1
  74. package/lib/slot/upgradeStateToHeze.d.ts.map +1 -1
  75. package/lib/slot/upgradeStateToHeze.js +4 -4
  76. package/lib/slot/upgradeStateToHeze.js.map +1 -1
  77. package/lib/stateTransition.js +2 -2
  78. package/lib/stateTransition.js.map +1 -1
  79. package/lib/stateView/beaconStateView.d.ts +2 -0
  80. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  81. package/lib/stateView/beaconStateView.js +11 -1
  82. package/lib/stateView/beaconStateView.js.map +1 -1
  83. package/lib/stateView/interface.d.ts +5 -4
  84. package/lib/stateView/interface.d.ts.map +1 -1
  85. package/lib/stateView/interface.js.map +1 -1
  86. package/lib/stateView/nativeBeaconStateView.d.ts +16 -6
  87. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  88. package/lib/stateView/nativeBeaconStateView.js +99 -25
  89. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  90. package/lib/stateView/stateViewFactory.d.ts.map +1 -1
  91. package/lib/stateView/stateViewFactory.js +5 -4
  92. package/lib/stateView/stateViewFactory.js.map +1 -1
  93. package/lib/testUtils/interopPubkeyCache.js +1 -1
  94. package/lib/testUtils/interopPubkeyCache.js.map +1 -1
  95. package/lib/testUtils/state.d.ts.map +1 -1
  96. package/lib/testUtils/state.js +1 -1
  97. package/lib/testUtils/state.js.map +1 -1
  98. package/lib/testUtils/util.d.ts +1 -1
  99. package/lib/testUtils/util.d.ts.map +1 -1
  100. package/lib/testUtils/util.js +2 -2
  101. package/lib/testUtils/util.js.map +1 -1
  102. package/lib/types.d.ts +2 -2
  103. package/lib/types.d.ts.map +1 -1
  104. package/lib/util/altair.d.ts +3 -1
  105. package/lib/util/altair.d.ts.map +1 -1
  106. package/lib/util/altair.js +7 -4
  107. package/lib/util/altair.js.map +1 -1
  108. package/lib/util/blindedBlock.d.ts +1 -1
  109. package/lib/util/blindedBlock.d.ts.map +1 -1
  110. package/lib/util/blindedBlock.js +9 -14
  111. package/lib/util/blindedBlock.js.map +1 -1
  112. package/lib/util/clock.d.ts +5 -0
  113. package/lib/util/clock.d.ts.map +1 -1
  114. package/lib/util/clock.js +20 -17
  115. package/lib/util/clock.js.map +1 -1
  116. package/lib/util/deposit.d.ts +2 -2
  117. package/lib/util/deposit.d.ts.map +1 -1
  118. package/lib/util/deposit.js.map +1 -1
  119. package/lib/util/epochShuffling.d.ts.map +1 -1
  120. package/lib/util/epochShuffling.js +1 -1
  121. package/lib/util/epochShuffling.js.map +1 -1
  122. package/lib/util/genesis.d.ts.map +1 -1
  123. package/lib/util/genesis.js +46 -23
  124. package/lib/util/genesis.js.map +1 -1
  125. package/lib/util/inclusionListCommittee.d.ts +9 -0
  126. package/lib/util/inclusionListCommittee.d.ts.map +1 -0
  127. package/lib/util/inclusionListCommittee.js +18 -0
  128. package/lib/util/inclusionListCommittee.js.map +1 -0
  129. package/lib/util/index.d.ts +1 -0
  130. package/lib/util/index.d.ts.map +1 -1
  131. package/lib/util/index.js +1 -0
  132. package/lib/util/index.js.map +1 -1
  133. package/lib/util/interop.d.ts +1 -1
  134. package/lib/util/interop.d.ts.map +1 -1
  135. package/lib/util/interop.js +1 -1
  136. package/lib/util/interop.js.map +1 -1
  137. package/lib/util/seed.js +1 -1
  138. package/lib/util/seed.js.map +1 -1
  139. package/lib/util/shuffling.d.ts +3 -0
  140. package/lib/util/shuffling.d.ts.map +1 -1
  141. package/lib/util/shuffling.js +41 -1
  142. package/lib/util/shuffling.js.map +1 -1
  143. package/lib/util/signatureSets.d.ts +1 -1
  144. package/lib/util/signatureSets.d.ts.map +1 -1
  145. package/lib/util/signatureSets.js +1 -1
  146. package/lib/util/signatureSets.js.map +1 -1
  147. package/lib/util/slot.d.ts +18 -2
  148. package/lib/util/slot.d.ts.map +1 -1
  149. package/lib/util/slot.js +70 -6
  150. package/lib/util/slot.js.map +1 -1
  151. package/lib/util/syncCommittee.d.ts +4 -3
  152. package/lib/util/syncCommittee.d.ts.map +1 -1
  153. package/lib/util/syncCommittee.js +10 -8
  154. package/lib/util/syncCommittee.js.map +1 -1
  155. package/lib/util/validator.d.ts +1 -1
  156. package/lib/util/validator.d.ts.map +1 -1
  157. package/lib/util/validator.js +32 -11
  158. package/lib/util/validator.js.map +1 -1
  159. package/lib/util/weakSubjectivity.d.ts.map +1 -1
  160. package/lib/util/weakSubjectivity.js +4 -3
  161. package/lib/util/weakSubjectivity.js.map +1 -1
  162. package/package.json +9 -9
  163. package/src/block/index.ts +15 -15
  164. package/src/block/processAttestationsAltair.ts +4 -1
  165. package/src/block/processDeposit.ts +9 -4
  166. package/src/block/processEth1Data.ts +3 -3
  167. package/src/block/processOperations.ts +19 -16
  168. package/src/cache/epochCache.ts +30 -6
  169. package/src/cache/epochTransitionCache.ts +2 -1
  170. package/src/cache/stateCache.ts +2 -0
  171. package/src/cache/syncCommitteeCache.ts +1 -1
  172. package/src/cache/types.ts +2 -1
  173. package/src/epoch/getRewardsAndPenalties.ts +11 -1
  174. package/src/epoch/index.ts +18 -16
  175. package/src/epoch/processEth1DataReset.ts +2 -2
  176. package/src/epoch/processSyncCommitteeUpdates.ts +1 -1
  177. package/src/index.ts +2 -0
  178. package/src/lightClient/spec/store.ts +1 -1
  179. package/src/lightClient/spec/utils.ts +1 -1
  180. package/src/lightClient/spec/validateLightClientUpdate.ts +1 -1
  181. package/src/metrics.ts +5 -4
  182. package/src/rewards/attestationsRewards.ts +13 -2
  183. package/src/rewards/syncCommitteeRewards.ts +1 -1
  184. package/src/signatureSets/inclusionList.ts +20 -0
  185. package/src/signatureSets/index.ts +1 -0
  186. package/src/slot/upgradeStateToHeze.ts +6 -4
  187. package/src/stateTransition.ts +2 -2
  188. package/src/stateView/beaconStateView.ts +14 -2
  189. package/src/stateView/interface.ts +5 -10
  190. package/src/stateView/nativeBeaconStateView.ts +100 -29
  191. package/src/stateView/stateViewFactory.ts +5 -4
  192. package/src/testUtils/interopPubkeyCache.ts +1 -1
  193. package/src/testUtils/state.ts +1 -1
  194. package/src/testUtils/util.ts +2 -2
  195. package/src/types.ts +2 -0
  196. package/src/util/altair.ts +15 -4
  197. package/src/util/blindedBlock.ts +12 -15
  198. package/src/util/clock.ts +26 -17
  199. package/src/util/deposit.ts +2 -2
  200. package/src/util/epochShuffling.ts +1 -1
  201. package/src/util/genesis.ts +56 -30
  202. package/src/util/inclusionListCommittee.ts +17 -0
  203. package/src/util/index.ts +1 -0
  204. package/src/util/interop.ts +1 -1
  205. package/src/util/seed.ts +1 -1
  206. package/src/util/shuffling.ts +46 -1
  207. package/src/util/signatureSets.ts +1 -1
  208. package/src/util/slot.ts +78 -8
  209. package/src/util/syncCommittee.ts +22 -10
  210. package/src/util/validator.ts +47 -20
  211. package/src/util/weakSubjectivity.ts +3 -3
@@ -1,7 +1,7 @@
1
1
  import {CompactMultiProof} from "@chainsafe/persistent-merkle-tree";
2
2
  import {BitArray, ByteViews} from "@chainsafe/ssz";
3
3
  import type {BeaconConfig} from "@lodestar/config";
4
- import {ForkName, ForkSeq} from "@lodestar/params";
4
+ import {ForkName, ForkPostElectra, ForkSeq} from "@lodestar/params";
5
5
  import {
6
6
  BeaconBlock,
7
7
  BeaconState,
@@ -28,6 +28,7 @@ import {
28
28
  phase0,
29
29
  rewards,
30
30
  ssz,
31
+ sszTypesFor,
31
32
  } from "@lodestar/types";
32
33
  import {Checkpoint, Fork} from "@lodestar/types/phase0";
33
34
  import {VoluntaryExitValidity} from "../block/processVoluntaryExit.js";
@@ -39,6 +40,7 @@ import {SyncCommitteeWitness} from "../lightClient/types.js";
39
40
  import {StateTransitionModules, StateTransitionOpts} from "../stateTransition.js";
40
41
  import {EpochShuffling} from "../util/epochShuffling.js";
41
42
  import {PreVerifyBuilderDepositsResult} from "../util/preVerifyBuilderDeposits.js";
43
+ import {getStateSlotFromBytes} from "../util/sszBytes.js";
42
44
  import {computeNewStateRootStateTransitionOpts, getComputeNewStateRootResult} from "./computeNewStateRoot.js";
43
45
  import {
44
46
  BlockSTFInput,
@@ -47,8 +49,15 @@ import {
47
49
  IBeaconStateViewGloas,
48
50
  IBeaconStateViewLatestFork,
49
51
  IBeaconStateViewNative,
52
+ isStatePostGloas,
50
53
  } from "./interface.js";
51
54
 
55
+ export function assertNativeForkSupported(config: BeaconConfig, slot: Slot): void {
56
+ if (config.getForkSeq(slot) > ForkSeq.gloas) {
57
+ throw Error(`Native state transition does not support ${config.getForkName(slot)}`);
58
+ }
59
+ }
60
+
52
61
  /**
53
62
  * Wraps a native binding (the auto-generated JS interface produced by a `.node`
54
63
  * file) and exposes it as a fully-conformant `IBeaconStateViewLatestFork`.
@@ -97,6 +106,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
97
106
  private cachedPreviousEpochParticipation: Uint8Array | null = null;
98
107
  private cachedCurrentEpochParticipation: Uint8Array | null = null;
99
108
  private cachedCurrentSyncCommitteeIndexed: SyncCommitteeCache | null = null;
109
+ private cachedCurrentSyncCommitteeValidatorIndices: Uint32Array | null = null;
100
110
  private cachedSyncProposerReward: number | null = null;
101
111
  // bellatrix
102
112
  private cachedLatestExecutionPayloadHeader: ExecutionPayloadHeader | null = null;
@@ -114,6 +124,14 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
114
124
  private cachedPendingConsolidationsCount: number | null = null;
115
125
  // fulu
116
126
  private cachedProposerLookahead: fulu.ProposerLookahead | null = null;
127
+ // gloas
128
+ private cachedLatestBlockHash: Bytes32 | null = null;
129
+ private cachedExecutionPayloadAvailability: BitArray | null = null;
130
+ private cachedLatestExecutionPayloadBid: ExecutionPayloadBid | null = null;
131
+ private cachedPayloadExpectedWithdrawals: capella.Withdrawal[] | null = null;
132
+ private cachedBuilderPendingPayments: gloas.BuilderPendingPayments | null = null;
133
+ private cachedBuilderPendingWithdrawals: gloas.BuilderPendingWithdrawals | null = null;
134
+ private cachedBuildersLength: number | null = null;
117
135
  // Per-argument caches for argument-taking methods. The binding is treated as
118
136
  // immutable for the view's lifetime, so a given argument always yields the
119
137
  // same result. Maps grow only with touched arguments — typical call patterns
@@ -173,7 +191,11 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
173
191
  }
174
192
 
175
193
  get executionPayloadAvailability(): BitArray {
176
- throw new Error("NativeBeaconStateView does not support Gloas");
194
+ if (this.cachedExecutionPayloadAvailability === null) {
195
+ const {uint8Array, bitLen} = this.binding.executionPayloadAvailability;
196
+ this.cachedExecutionPayloadAvailability = new BitArray(uint8Array, bitLen);
197
+ }
198
+ return this.cachedExecutionPayloadAvailability;
177
199
  }
178
200
 
179
201
  // ─── phase0 ──────────────────────────────────────────────────────────────
@@ -228,6 +250,9 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
228
250
  }
229
251
 
230
252
  get eth1Data(): phase0.Eth1Data {
253
+ if (this.forkSeq >= ForkSeq.heze) {
254
+ throw new Error("eth1Data was removed from the beacon state in Heze");
255
+ }
231
256
  if (this.cachedEth1Data === null) {
232
257
  this.cachedEth1Data = this.binding.eth1Data;
233
258
  }
@@ -594,6 +619,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
594
619
  seedValidatorsBytes?: Uint8Array,
595
620
  opts?: {preloadValidatorsAndBalances?: boolean}
596
621
  ): IBeaconStateView {
622
+ assertNativeForkSupported(this.config, getStateSlotFromBytes(stateBytes));
597
623
  return new NativeBeaconStateView(this.config, this.binding.loadOtherState(stateBytes, seedValidatorsBytes, opts));
598
624
  }
599
625
 
@@ -659,6 +685,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
659
685
  options: StateTransitionOpts,
660
686
  _modules: StateTransitionModules
661
687
  ): IBeaconStateView {
688
+ assertNativeForkSupported(this.config, block.message.slot);
662
689
  const isBlinded = isBlindedBeaconBlock(block.message);
663
690
  const signedBlockBytes =
664
691
  ssz ??
@@ -671,6 +698,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
671
698
  }
672
699
 
673
700
  processSlots(slot: Slot, opts?: {dontTransferCache?: boolean}, _modules?: StateTransitionModules): IBeaconStateView {
701
+ assertNativeForkSupported(this.config, slot);
674
702
  return new NativeBeaconStateView(this.config, this.binding.processSlots(slot, opts));
675
703
  }
676
704
 
@@ -719,6 +747,16 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
719
747
  return this.cachedCurrentSyncCommitteeIndexed;
720
748
  }
721
749
 
750
+ get currentSyncCommitteeValidatorIndices(): Uint32Array {
751
+ if (this.cachedCurrentSyncCommitteeValidatorIndices === null) {
752
+ this.cachedCurrentSyncCommitteeValidatorIndices =
753
+ this.cachedCurrentSyncCommitteeIndexed?.validatorIndices ??
754
+ this.binding.currentSyncCommitteeValidatorIndices ??
755
+ this.currentSyncCommitteeIndexed.validatorIndices;
756
+ }
757
+ return this.cachedCurrentSyncCommitteeValidatorIndices;
758
+ }
759
+
722
760
  get syncProposerReward(): number {
723
761
  if (this.cachedSyncProposerReward === null) {
724
762
  this.cachedSyncProposerReward = this.binding.syncProposerReward;
@@ -818,7 +856,8 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
818
856
 
819
857
  get pendingDeposits(): electra.PendingDeposits {
820
858
  if (this.cachedPendingDeposits === null) {
821
- this.cachedPendingDeposits = ssz.electra.PendingDeposits.deserialize(this.binding.pendingDeposits);
859
+ const type = sszTypesFor(this.forkName as ForkPostElectra).PendingDeposits;
860
+ this.cachedPendingDeposits = type.deserialize(this.binding.pendingDeposits);
822
861
  }
823
862
  return this.cachedPendingDeposits;
824
863
  }
@@ -832,9 +871,8 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
832
871
 
833
872
  get pendingPartialWithdrawals(): electra.PendingPartialWithdrawals {
834
873
  if (this.cachedPendingPartialWithdrawals === null) {
835
- this.cachedPendingPartialWithdrawals = ssz.electra.PendingPartialWithdrawals.deserialize(
836
- this.binding.pendingPartialWithdrawals
837
- );
874
+ const type = sszTypesFor(this.forkName as ForkPostElectra).PendingPartialWithdrawals;
875
+ this.cachedPendingPartialWithdrawals = type.deserialize(this.binding.pendingPartialWithdrawals);
838
876
  }
839
877
  return this.cachedPendingPartialWithdrawals;
840
878
  }
@@ -848,9 +886,8 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
848
886
 
849
887
  get pendingConsolidations(): electra.PendingConsolidations {
850
888
  if (this.cachedPendingConsolidations === null) {
851
- this.cachedPendingConsolidations = ssz.electra.PendingConsolidations.deserialize(
852
- this.binding.pendingConsolidations
853
- );
889
+ const type = sszTypesFor(this.forkName as ForkPostElectra).PendingConsolidations;
890
+ this.cachedPendingConsolidations = type.deserialize(this.binding.pendingConsolidations);
854
891
  }
855
892
  return this.cachedPendingConsolidations;
856
893
  }
@@ -875,60 +912,94 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
875
912
  _maxBuilderDeposits: number,
876
913
  _maxDurationMs: number
877
914
  ): PreVerifyBuilderDepositsResult {
878
- throw new Error("NativeBeaconStateView does not support Gloas");
915
+ // The native fork upgrade verifies directly; it does not maintain a warm-up cache.
916
+ return {
917
+ validBuilderSignaturesCount: 0,
918
+ invalidBuilderSignaturesCount: 0,
919
+ validValidatorSignaturesCount: 0,
920
+ invalidValidatorSignaturesCount: 0,
921
+ scannedPendingDeposits: 0,
922
+ totalCachedDeposits: 0,
923
+ totalBuilderPubkeys: 0,
924
+ pendingDepositsCount: this.pendingDepositsCount,
925
+ };
879
926
  }
880
927
 
881
928
  clearPreGloasBuilderDepositCache(): void {
882
- throw new Error("NativeBeaconStateView does not support Gloas");
929
+ // The native implementation has no pre-Gloas deposit cache to clear.
883
930
  }
884
931
 
885
932
  // ─── gloas ───────────────────────────────────────────────────────────────
886
933
 
887
934
  get latestBlockHash(): Bytes32 {
888
- throw new Error("NativeBeaconStateView does not support Gloas");
935
+ if (this.cachedLatestBlockHash === null) {
936
+ this.cachedLatestBlockHash = this.binding.latestBlockHash;
937
+ }
938
+ return this.cachedLatestBlockHash;
889
939
  }
890
940
 
891
941
  get latestExecutionPayloadBid(): ExecutionPayloadBid {
892
- throw new Error("NativeBeaconStateView does not support Gloas");
942
+ if (this.cachedLatestExecutionPayloadBid === null) {
943
+ this.cachedLatestExecutionPayloadBid = this.binding.latestExecutionPayloadBid;
944
+ }
945
+ return this.cachedLatestExecutionPayloadBid;
893
946
  }
894
947
 
895
948
  get payloadExpectedWithdrawals(): capella.Withdrawal[] {
896
- throw new Error("NativeBeaconStateView does not support Gloas");
949
+ if (this.cachedPayloadExpectedWithdrawals === null) {
950
+ this.cachedPayloadExpectedWithdrawals = this.binding.payloadExpectedWithdrawals;
951
+ }
952
+ return this.cachedPayloadExpectedWithdrawals;
897
953
  }
898
954
 
899
955
  get builderPendingPayments(): gloas.BuilderPendingPayments {
900
- throw new Error("NativeBeaconStateView does not support Gloas");
956
+ if (this.cachedBuilderPendingPayments === null) {
957
+ this.cachedBuilderPendingPayments = this.binding.builderPendingPayments;
958
+ }
959
+ return this.cachedBuilderPendingPayments;
901
960
  }
902
961
 
903
962
  get builderPendingWithdrawals(): gloas.BuilderPendingWithdrawals {
904
- throw new Error("NativeBeaconStateView does not support Gloas");
963
+ if (this.cachedBuilderPendingWithdrawals === null) {
964
+ this.cachedBuilderPendingWithdrawals = this.binding.builderPendingWithdrawals;
965
+ }
966
+ return this.cachedBuilderPendingWithdrawals;
905
967
  }
906
968
 
907
- getBuilder(_index: BuilderIndex): gloas.Builder {
908
- throw new Error("NativeBeaconStateView does not support Gloas");
969
+ getBuilder(index: BuilderIndex): gloas.Builder {
970
+ return this.binding.getBuilder(index);
909
971
  }
910
972
 
911
973
  getBuildersLength(): number {
912
- throw new Error("NativeBeaconStateView does not support Gloas");
974
+ if (this.cachedBuildersLength === null) {
975
+ this.cachedBuildersLength = this.binding.getBuildersLength();
976
+ }
977
+ return this.cachedBuildersLength;
913
978
  }
914
979
 
915
- canBuilderCoverBid(_builderIndex: BuilderIndex, _bidAmount: number): boolean {
916
- throw new Error("NativeBeaconStateView does not support Gloas");
980
+ canBuilderCoverBid(builderIndex: BuilderIndex, bidAmount: number): boolean {
981
+ return this.binding.canBuilderCoverBid(builderIndex, bidAmount);
917
982
  }
918
983
 
919
- getEpochPTCs(_epoch: Epoch): Uint32Array[] {
920
- throw new Error("NativeBeaconStateView does not support Gloas");
984
+ getEpochPTCs(epoch: Epoch): Uint32Array[] {
985
+ return this.binding.getEpochPTCs(epoch);
921
986
  }
922
987
 
923
- getPayloadTimelinessCommittee(_slot: Slot): Uint32Array {
924
- throw new Error("NativeBeaconStateView does not support Gloas");
988
+ getPayloadTimelinessCommittee(slot: Slot): Uint32Array {
989
+ return this.binding.getPayloadTimelinessCommittee(slot);
925
990
  }
926
991
 
927
- getIndicesInPayloadTimelinessCommittee(_validatorIndex: ValidatorIndex, _slot: Slot): number[] {
928
- throw new Error("NativeBeaconStateView does not support Gloas");
992
+ getIndicesInPayloadTimelinessCommittee(validatorIndex: ValidatorIndex, slot: Slot): number[] {
993
+ return this.binding.getIndicesInPayloadTimelinessCommittee(validatorIndex, slot);
929
994
  }
930
995
 
931
- withParentPayloadApplied(_executionRequests: gloas.ExecutionRequests): IBeaconStateViewGloas {
932
- throw new Error("NativeBeaconStateView does not support Gloas");
996
+ withParentPayloadApplied(executionRequests: gloas.ExecutionRequests): IBeaconStateViewGloas {
997
+ const bytes = ssz.gloas.ExecutionRequests.serialize(executionRequests);
998
+ const state = new NativeBeaconStateView(this.config, this.binding.withParentPayloadApplied(bytes));
999
+ if (!isStatePostGloas(state)) {
1000
+ state.release();
1001
+ throw Error("Expected Gloas state after applying the parent payload");
1002
+ }
1003
+ return state;
933
1004
  }
934
1005
  }
@@ -1,11 +1,11 @@
1
- import bindings from "@chainsafe/lodestar-z";
2
- import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
3
1
  import {BeaconConfig} from "@lodestar/config";
2
+ import bindings from "@lodestar/native";
3
+ import {pubkeyCache} from "@lodestar/native/pubkeys";
4
4
  import {createCachedBeaconState} from "../cache/stateCache.js";
5
- import {getStateTypeFromBytes} from "../util/sszBytes.js";
5
+ import {getStateSlotFromBytes, getStateTypeFromBytes} from "../util/sszBytes.js";
6
6
  import {BeaconStateView} from "./beaconStateView.js";
7
7
  import {IBeaconStateView, IBeaconStateViewNative} from "./interface.js";
8
- import {NativeBeaconStateView} from "./nativeBeaconStateView.js";
8
+ import {NativeBeaconStateView, assertNativeForkSupported} from "./nativeBeaconStateView.js";
9
9
 
10
10
  // ---- createBeaconStateView (startup path) ----
11
11
 
@@ -62,6 +62,7 @@ export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts |
62
62
  }
63
63
 
64
64
  function createNativeBeaconStateView(config: BeaconConfig, stateBytes: Uint8Array): IBeaconStateView {
65
+ assertNativeForkSupported(config, getStateSlotFromBytes(stateBytes));
65
66
  const nativeConfig = new bindings.BeaconConfig(config, config.genesisValidatorsRoot);
66
67
  return new NativeBeaconStateView(
67
68
  config,
@@ -1,6 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
3
+ import {pubkeyCache} from "@lodestar/native/pubkeys";
4
4
  import {interopSecretKey} from "../util/interop.js";
5
5
  import {testCachePath} from "./cache.js";
6
6
 
@@ -1,6 +1,6 @@
1
- import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
2
1
  import {ChainForkConfig, createBeaconConfig} from "@lodestar/config";
3
2
  import {config, config as minimalConfig} from "@lodestar/config/default";
3
+ import {pubkeyCache} from "@lodestar/native/pubkeys";
4
4
  import {
5
5
  EPOCHS_PER_HISTORICAL_VECTOR,
6
6
  EPOCHS_PER_SLASHINGS_VECTOR,
@@ -1,8 +1,8 @@
1
- import {PublicKey, SecretKey} from "@chainsafe/lodestar-z/blst";
2
- import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
3
1
  import {BitArray, fromHexString} from "@chainsafe/ssz";
4
2
  import {BeaconConfig, createBeaconConfig, createChainForkConfig} from "@lodestar/config";
5
3
  import {config} from "@lodestar/config/default";
4
+ import {PublicKey, SecretKey} from "@lodestar/native/blst";
5
+ import {pubkeyCache} from "@lodestar/native/pubkeys";
6
6
  import {
7
7
  BUILDER_WITHDRAWAL_PREFIX,
8
8
  DOMAIN_DEPOSIT,
package/src/types.ts CHANGED
@@ -12,6 +12,7 @@ export type {
12
12
  CachedBeaconStateGloas,
13
13
  CachedBeaconStateHeze,
14
14
  CachedBeaconStatePhase0,
15
+ CachedBeaconStatePreHeze,
15
16
  } from "./cache/stateCache.js";
16
17
  export type {
17
18
  BeaconStateAllForks,
@@ -25,5 +26,6 @@ export type {
25
26
  BeaconStateGloas,
26
27
  BeaconStateHeze,
27
28
  BeaconStatePhase0,
29
+ BeaconStatePreHeze,
28
30
  ShufflingGetter,
29
31
  } from "./cache/types.js";
@@ -1,13 +1,24 @@
1
- import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT} from "@lodestar/params";
1
+ import {ChainConfig} from "@lodestar/config";
2
+ import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT, GENESIS_EPOCH} from "@lodestar/params";
3
+ import {Epoch} from "@lodestar/types";
2
4
  import {bigIntSqrt, bnToNum} from "@lodestar/utils";
5
+ import {getSlotDurationMsAtEpoch} from "./slot.js";
3
6
 
4
7
  /**
5
8
  * Before we manage bigIntSqrt(totalActiveStake) as BigInt and return BigInt.
6
9
  * bigIntSqrt(totalActiveStake) should fit a number (2 ** 53 -1 max)
7
10
  **/
8
- export function computeBaseRewardPerIncrement(totalActiveStakeByIncrement: number): number {
11
+ export function computeBaseRewardPerIncrement(
12
+ config: ChainConfig,
13
+ epoch: Epoch,
14
+ totalActiveStakeByIncrement: number
15
+ ): number {
16
+ // Divide last so the exact slot duration ratio applies
17
+ const numerator = Math.floor(
18
+ (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR * getSlotDurationMsAtEpoch(config, epoch)) /
19
+ getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
20
+ );
9
21
  return Math.floor(
10
- (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR) /
11
- bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
22
+ numerator / bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
12
23
  );
13
24
  }
@@ -3,10 +3,12 @@ import {
3
3
  ForkName,
4
4
  ForkPostBellatrix,
5
5
  ForkPostDeneb,
6
+ ForkPreDeneb,
6
7
  ForkPreGloas,
7
8
  ForkSeq,
8
9
  isForkPostBellatrix,
9
10
  isForkPostDeneb,
11
+ isForkPostGloas,
10
12
  } from "@lodestar/params";
11
13
  import {
12
14
  BeaconBlock,
@@ -31,13 +33,9 @@ export function blindedOrFullBlockHashTreeRoot(
31
33
  ): Root {
32
34
  return isBlindedBeaconBlock(blindedOrFull)
33
35
  ? // Blinded
34
- config
35
- .getPostBellatrixForkTypes(blindedOrFull.slot)
36
- .BlindedBeaconBlock.hashTreeRoot(blindedOrFull)
36
+ config.getPostBellatrixForkTypes(blindedOrFull.slot).BlindedBeaconBlock.hashTreeRoot(blindedOrFull)
37
37
  : // Full
38
- config
39
- .getForkTypes(blindedOrFull.slot)
40
- .BeaconBlock.hashTreeRoot(blindedOrFull);
38
+ config.getForkTypes(blindedOrFull.slot).BeaconBlock.hashTreeRoot(blindedOrFull);
41
39
  }
42
40
 
43
41
  export function blindedOrFullBlockToHeader(
@@ -46,13 +44,9 @@ export function blindedOrFullBlockToHeader(
46
44
  ): BeaconBlockHeader {
47
45
  const bodyRoot = isBlindedBeaconBlock(blindedOrFull)
48
46
  ? // Blinded
49
- config
50
- .getPostBellatrixForkTypes(blindedOrFull.slot)
51
- .BlindedBeaconBlockBody.hashTreeRoot(blindedOrFull.body)
47
+ config.getPostBellatrixForkTypes(blindedOrFull.slot).BlindedBeaconBlockBody.hashTreeRoot(blindedOrFull.body)
52
48
  : // Full
53
- config
54
- .getForkTypes(blindedOrFull.slot)
55
- .BeaconBlockBody.hashTreeRoot(blindedOrFull.body);
49
+ config.getForkTypes(blindedOrFull.slot).BeaconBlockBody.hashTreeRoot(blindedOrFull.body);
56
50
 
57
51
  return {
58
52
  slot: blindedOrFull.slot,
@@ -128,7 +122,10 @@ export function reconstructSignedBlockContents(
128
122
  signedBlindedBlock: SignedBlindedBeaconBlock,
129
123
  executionPayload: ExecutionPayload | null,
130
124
  blobsBundle: BlobsBundle | null
131
- ): SignedBlockContents {
125
+ ): SignedBlockContents<ForkPreGloas> {
126
+ if (isForkPostGloas(fork)) {
127
+ throw Error("Blinded block reconstruction is not supported after Gloas");
128
+ }
132
129
  const signedBlock = signedBlindedBlockToFull(fork, signedBlindedBlock, executionPayload);
133
130
 
134
131
  if (isForkPostDeneb(fork)) {
@@ -136,10 +133,10 @@ export function reconstructSignedBlockContents(
136
133
  throw Error("Missing blobs bundle to reconstruct post-deneb block contents");
137
134
  }
138
135
  return {
139
- signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb>,
136
+ signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb & ForkPreGloas>,
140
137
  kzgProofs: blobsBundle.proofs,
141
138
  blobs: blobsBundle.blobs,
142
139
  };
143
140
  }
144
- return {signedBlock};
141
+ return {signedBlock: signedBlock as SignedBeaconBlock<ForkPreDeneb>};
145
142
  }
package/src/util/clock.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import {ChainForkConfig} from "@lodestar/config";
2
- import {GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";
2
+ import {SLOTS_PER_EPOCH} from "@lodestar/params";
3
3
  import {Epoch, Slot, TimeSeconds} from "@lodestar/types";
4
4
  import {ErrorAborted, Logger, isErrorAborted, sleep} from "@lodestar/utils";
5
5
  import {computeEpochAtSlot} from "./epoch.js";
6
- import {computeTimeAtSlot, getCurrentSlot} from "./slot.js";
6
+ import {computeTimeAtSlot, getCurrentSlot, getSlotDurationMs} from "./slot.js";
7
7
 
8
8
  type RunEveryFn = (slot: Slot, signal: AbortSignal) => Promise<void>;
9
9
 
@@ -13,6 +13,7 @@ export type ClockOptions = {
13
13
 
14
14
  export interface IClock {
15
15
  readonly genesisTime: number;
16
+ /** @deprecated Use getSlotDurationMs(slot) instead */
16
17
  readonly secondsPerSlot: number;
17
18
 
18
19
  readonly currentEpoch: number;
@@ -25,6 +26,8 @@ export interface IClock {
25
26
  secFromSlot(slot: Slot): number;
26
27
  getCurrentSlot(): Slot;
27
28
  getCurrentEpoch(): Epoch;
29
+ /** Slot duration in ms for a given slot */
30
+ getSlotDurationMs(slot: Slot): number;
28
31
  }
29
32
 
30
33
  export enum TimeItem {
@@ -95,6 +98,11 @@ export class Clock implements IClock {
95
98
  return Date.now() / 1000 - computeTimeAtSlot(this.config, slot, this.genesisTime);
96
99
  }
97
100
 
101
+ /** Slot duration in ms for a given slot */
102
+ getSlotDurationMs(slot: Slot): number {
103
+ return getSlotDurationMs(this.config, slot);
104
+ }
105
+
98
106
  /**
99
107
  * If a task happens to take more than one slot to run, we might skip a slot. This is unfortunate,
100
108
  * however the alternative is to *always* process every slot, which has the chance of creating a
@@ -125,7 +133,7 @@ export class Clock implements IClock {
125
133
  if (timeItem === TimeItem.Slot) {
126
134
  if (nextSlot > slot + 1) {
127
135
  // It's not very likely that we skip more than one slot as HTTP timeout is set
128
- // to SLOT_DURATION_MS so we will fail task before skipping another slot.
136
+ // to at most one slot duration so we will fail task before skipping another slot.
129
137
  this.logger.warn("Skipped slot due to task taking more than one slot to run", {
130
138
  skippedSlot: slot + 1,
131
139
  });
@@ -145,20 +153,17 @@ export class Clock implements IClock {
145
153
  }
146
154
 
147
155
  private timeUntilNext(timeItem: TimeItem): number {
148
- const milliSecondsPerSlot = this.config.SLOT_DURATION_MS;
149
- const msFromGenesis = Date.now() - this.genesisTime * 1000;
156
+ const currentSlot = getCurrentSlot(this.config, this.genesisTime);
150
157
 
151
158
  if (timeItem === TimeItem.Slot) {
152
- if (msFromGenesis >= 0) {
153
- return milliSecondsPerSlot - (msFromGenesis % milliSecondsPerSlot);
154
- }
155
- return Math.abs(msFromGenesis) % milliSecondsPerSlot;
159
+ const nextSlotTimeSec = computeTimeAtSlot(this.config, currentSlot + 1, this.genesisTime);
160
+ return Math.max(0, nextSlotTimeSec * 1000 - Date.now());
156
161
  }
157
- const milliSecondsPerEpoch = SLOTS_PER_EPOCH * milliSecondsPerSlot;
158
- if (msFromGenesis >= 0) {
159
- return milliSecondsPerEpoch - (msFromGenesis % milliSecondsPerEpoch);
160
- }
161
- return Math.abs(msFromGenesis) % milliSecondsPerEpoch;
162
+
163
+ const currentEpoch = computeEpochAtSlot(currentSlot);
164
+ const nextEpochSlot = (currentEpoch + 1) * SLOTS_PER_EPOCH;
165
+ const nextEpochTimeSec = computeTimeAtSlot(this.config, nextEpochSlot, this.genesisTime);
166
+ return Math.max(0, nextEpochTimeSec * 1000 - Date.now());
162
167
  }
163
168
  }
164
169
 
@@ -166,9 +171,13 @@ export class Clock implements IClock {
166
171
  * Same to the spec but we use Math.round instead of Math.floor.
167
172
  */
168
173
  export function getCurrentSlotAround(config: ChainForkConfig, genesisTime: TimeSeconds): Slot {
169
- const diffInSeconds = Date.now() / 1000 - genesisTime;
170
- const slotsSinceGenesis = Math.round((diffInSeconds * 1000) / config.SLOT_DURATION_MS);
171
- return GENESIS_SLOT + slotsSinceGenesis;
174
+ const floorSlot = getCurrentSlot(config, genesisTime);
175
+ const slotStartSec = computeTimeAtSlot(config, floorSlot, genesisTime);
176
+ const slotDurationMs = getSlotDurationMs(config, floorSlot);
177
+ const slotDurationSec = slotDurationMs / 1000;
178
+ const elapsed = Date.now() / 1000 - slotStartSec;
179
+
180
+ return elapsed >= slotDurationSec / 2 ? floorSlot + 1 : floorSlot;
172
181
  }
173
182
 
174
183
  // function useEventStream() {
@@ -1,8 +1,8 @@
1
1
  import {ForkSeq, MAX_DEPOSITS} from "@lodestar/params";
2
2
  import {UintNum64, phase0} from "@lodestar/types";
3
- import {CachedBeaconStateAllForks, CachedBeaconStateElectra} from "../types.js";
3
+ import {CachedBeaconStateElectra, CachedBeaconStatePreHeze} from "../types.js";
4
4
 
5
- export function getEth1DepositCount(state: CachedBeaconStateAllForks, eth1Data?: phase0.Eth1Data): UintNum64 {
5
+ export function getEth1DepositCount(state: CachedBeaconStatePreHeze, eth1Data?: phase0.Eth1Data): UintNum64 {
6
6
  const eth1DataToUse = eth1Data ?? state.eth1Data;
7
7
  // Proposer can set any value, use in bigint until the result is bounded by MAX_DEPOSITS
8
8
  const depositCount = eth1DataToUse.depositCount;
@@ -1,5 +1,5 @@
1
- import {unshuffleList} from "@chainsafe/lodestar-z/shuffle";
2
1
  import {BeaconConfig} from "@lodestar/config";
2
+ import {unshuffleList} from "@lodestar/native/shuffle";
3
3
  import {
4
4
  DOMAIN_BEACON_ATTESTER,
5
5
  GENESIS_SLOT,