@lodestar/state-transition 1.41.0-dev.2074a31ba7 → 1.41.0-dev.21d4a81d4e

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 (291) hide show
  1. package/lib/block/externalData.d.ts +2 -1
  2. package/lib/block/externalData.d.ts.map +1 -1
  3. package/lib/block/externalData.js +6 -2
  4. package/lib/block/externalData.js.map +1 -1
  5. package/lib/block/index.d.ts +3 -3
  6. package/lib/block/index.js.map +1 -1
  7. package/lib/block/initiateValidatorExit.js.map +1 -1
  8. package/lib/block/isValidIndexedAttestation.d.ts +3 -3
  9. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  10. package/lib/block/isValidIndexedAttestation.js +4 -4
  11. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  12. package/lib/block/isValidIndexedPayloadAttestation.js +1 -1
  13. package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -1
  14. package/lib/block/processAttestationPhase0.js +1 -1
  15. package/lib/block/processAttestationPhase0.js.map +1 -1
  16. package/lib/block/processAttestations.js.map +1 -1
  17. package/lib/block/processAttestationsAltair.js +1 -1
  18. package/lib/block/processAttestationsAltair.js.map +1 -1
  19. package/lib/block/processAttesterSlashing.d.ts +2 -2
  20. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  21. package/lib/block/processAttesterSlashing.js +3 -3
  22. package/lib/block/processAttesterSlashing.js.map +1 -1
  23. package/lib/block/processBlobKzgCommitments.js.map +1 -1
  24. package/lib/block/processBlockHeader.js.map +1 -1
  25. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  26. package/lib/block/processConsolidationRequest.js.map +1 -1
  27. package/lib/block/processDeposit.js.map +1 -1
  28. package/lib/block/processDepositRequest.js.map +1 -1
  29. package/lib/block/processEth1Data.js.map +1 -1
  30. package/lib/block/processExecutionPayload.js.map +1 -1
  31. package/lib/block/processExecutionPayloadBid.d.ts +1 -1
  32. package/lib/block/processExecutionPayloadBid.js.map +1 -1
  33. package/lib/block/processExecutionPayloadEnvelope.d.ts +5 -2
  34. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
  35. package/lib/block/processExecutionPayloadEnvelope.js +25 -15
  36. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
  37. package/lib/block/processOperations.js.map +1 -1
  38. package/lib/block/processPayloadAttestation.d.ts +1 -1
  39. package/lib/block/processPayloadAttestation.js.map +1 -1
  40. package/lib/block/processProposerSlashing.d.ts +2 -2
  41. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  42. package/lib/block/processProposerSlashing.js +3 -3
  43. package/lib/block/processProposerSlashing.js.map +1 -1
  44. package/lib/block/processRandao.js +1 -1
  45. package/lib/block/processRandao.js.map +1 -1
  46. package/lib/block/processSyncCommittee.js +1 -1
  47. package/lib/block/processSyncCommittee.js.map +1 -1
  48. package/lib/block/processVoluntaryExit.js +3 -2
  49. package/lib/block/processVoluntaryExit.js.map +1 -1
  50. package/lib/block/processWithdrawalRequest.js +2 -2
  51. package/lib/block/processWithdrawalRequest.js.map +1 -1
  52. package/lib/block/processWithdrawals.js.map +1 -1
  53. package/lib/block/slashValidator.js.map +1 -1
  54. package/lib/block/types.js +2 -1
  55. package/lib/block/types.js.map +1 -1
  56. package/lib/cache/effectiveBalanceIncrements.js.map +1 -1
  57. package/lib/cache/epochCache.d.ts +8 -15
  58. package/lib/cache/epochCache.d.ts.map +1 -1
  59. package/lib/cache/epochCache.js +36 -34
  60. package/lib/cache/epochCache.js.map +1 -1
  61. package/lib/cache/epochTransitionCache.js.map +1 -1
  62. package/lib/cache/pubkeyCache.d.ts +21 -6
  63. package/lib/cache/pubkeyCache.d.ts.map +1 -1
  64. package/lib/cache/pubkeyCache.js +39 -14
  65. package/lib/cache/pubkeyCache.js.map +1 -1
  66. package/lib/cache/rewardCache.js.map +1 -1
  67. package/lib/cache/stateCache.d.ts +1 -1
  68. package/lib/cache/stateCache.d.ts.map +1 -1
  69. package/lib/cache/stateCache.js +3 -7
  70. package/lib/cache/stateCache.js.map +1 -1
  71. package/lib/cache/syncCommitteeCache.d.ts +3 -2
  72. package/lib/cache/syncCommitteeCache.d.ts.map +1 -1
  73. package/lib/cache/syncCommitteeCache.js +4 -4
  74. package/lib/cache/syncCommitteeCache.js.map +1 -1
  75. package/lib/epoch/computeUnrealizedCheckpoints.js.map +1 -1
  76. package/lib/epoch/getAttestationDeltas.js.map +1 -1
  77. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  78. package/lib/epoch/index.d.ts +1 -1
  79. package/lib/epoch/index.js +2 -1
  80. package/lib/epoch/index.js.map +1 -1
  81. package/lib/epoch/processBuilderPendingPayments.d.ts +1 -1
  82. package/lib/epoch/processBuilderPendingPayments.js.map +1 -1
  83. package/lib/epoch/processEffectiveBalanceUpdates.js.map +1 -1
  84. package/lib/epoch/processEth1DataReset.js.map +1 -1
  85. package/lib/epoch/processHistoricalRootsUpdate.js.map +1 -1
  86. package/lib/epoch/processHistoricalSummariesUpdate.js.map +1 -1
  87. package/lib/epoch/processInactivityUpdates.js.map +1 -1
  88. package/lib/epoch/processJustificationAndFinalization.js.map +1 -1
  89. package/lib/epoch/processParticipationFlagUpdates.js.map +1 -1
  90. package/lib/epoch/processParticipationRecordUpdates.js.map +1 -1
  91. package/lib/epoch/processPendingAttestations.js.map +1 -1
  92. package/lib/epoch/processPendingConsolidations.js.map +1 -1
  93. package/lib/epoch/processPendingDeposits.js.map +1 -1
  94. package/lib/epoch/processProposerLookahead.js.map +1 -1
  95. package/lib/epoch/processRandaoMixesReset.js.map +1 -1
  96. package/lib/epoch/processRegistryUpdates.js.map +1 -1
  97. package/lib/epoch/processRewardsAndPenalties.js.map +1 -1
  98. package/lib/epoch/processSlashings.js.map +1 -1
  99. package/lib/epoch/processSlashingsReset.js.map +1 -1
  100. package/lib/epoch/processSyncCommitteeUpdates.js.map +1 -1
  101. package/lib/index.d.ts +3 -1
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +2 -1
  104. package/lib/index.js.map +1 -1
  105. package/lib/lightClient/proofs.d.ts +10 -0
  106. package/lib/lightClient/proofs.d.ts.map +1 -0
  107. package/lib/lightClient/proofs.js +63 -0
  108. package/lib/lightClient/proofs.js.map +1 -0
  109. package/lib/lightClient/types.d.ts +34 -0
  110. package/lib/lightClient/types.d.ts.map +1 -0
  111. package/lib/lightClient/types.js +2 -0
  112. package/lib/lightClient/types.js.map +1 -0
  113. package/lib/metrics.d.ts.map +1 -1
  114. package/lib/metrics.js.map +1 -1
  115. package/lib/rewards/attestationsRewards.d.ts +2 -2
  116. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  117. package/lib/rewards/attestationsRewards.js +4 -4
  118. package/lib/rewards/attestationsRewards.js.map +1 -1
  119. package/lib/rewards/blockRewards.js.map +1 -1
  120. package/lib/rewards/syncCommitteeRewards.d.ts +2 -2
  121. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
  122. package/lib/rewards/syncCommitteeRewards.js +5 -2
  123. package/lib/rewards/syncCommitteeRewards.js.map +1 -1
  124. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  125. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  126. package/lib/signatureSets/executionPayloadBid.js.map +1 -1
  127. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -1
  128. package/lib/signatureSets/index.js.map +1 -1
  129. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  130. package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -1
  131. package/lib/signatureSets/proposer.d.ts +2 -2
  132. package/lib/signatureSets/proposer.d.ts.map +1 -1
  133. package/lib/signatureSets/proposer.js +2 -2
  134. package/lib/signatureSets/proposer.js.map +1 -1
  135. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  136. package/lib/signatureSets/randao.d.ts +2 -2
  137. package/lib/signatureSets/randao.d.ts.map +1 -1
  138. package/lib/signatureSets/randao.js +2 -2
  139. package/lib/signatureSets/randao.js.map +1 -1
  140. package/lib/signatureSets/voluntaryExits.d.ts +2 -2
  141. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  142. package/lib/signatureSets/voluntaryExits.js +2 -2
  143. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  144. package/lib/slot/index.js.map +1 -1
  145. package/lib/slot/upgradeStateToAltair.js.map +1 -1
  146. package/lib/slot/upgradeStateToBellatrix.js.map +1 -1
  147. package/lib/slot/upgradeStateToCapella.js.map +1 -1
  148. package/lib/slot/upgradeStateToDeneb.js.map +1 -1
  149. package/lib/slot/upgradeStateToElectra.js.map +1 -1
  150. package/lib/slot/upgradeStateToFulu.js.map +1 -1
  151. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  152. package/lib/stateTransition.d.ts +2 -1
  153. package/lib/stateTransition.d.ts.map +1 -1
  154. package/lib/stateTransition.js +6 -3
  155. package/lib/stateTransition.js.map +1 -1
  156. package/lib/stateView/beaconStateView.d.ts +160 -0
  157. package/lib/stateView/beaconStateView.d.ts.map +1 -0
  158. package/lib/stateView/beaconStateView.js +553 -0
  159. package/lib/stateView/beaconStateView.js.map +1 -0
  160. package/lib/stateView/index.d.ts +3 -0
  161. package/lib/stateView/index.d.ts.map +1 -0
  162. package/lib/stateView/index.js +3 -0
  163. package/lib/stateView/index.js.map +1 -0
  164. package/lib/stateView/interface.d.ts +135 -0
  165. package/lib/stateView/interface.d.ts.map +1 -0
  166. package/lib/stateView/interface.js +2 -0
  167. package/lib/stateView/interface.js.map +1 -0
  168. package/lib/testUtils/cache.d.ts +2 -0
  169. package/lib/testUtils/cache.d.ts.map +1 -0
  170. package/lib/testUtils/cache.js +7 -0
  171. package/lib/testUtils/cache.js.map +1 -0
  172. package/lib/testUtils/index.d.ts +6 -0
  173. package/lib/testUtils/index.d.ts.map +1 -0
  174. package/lib/testUtils/index.js +6 -0
  175. package/lib/testUtils/index.js.map +1 -0
  176. package/lib/testUtils/infura.d.ts +3 -0
  177. package/lib/testUtils/infura.d.ts.map +1 -0
  178. package/lib/testUtils/infura.js +8 -0
  179. package/lib/testUtils/infura.js.map +1 -0
  180. package/lib/testUtils/interop.d.ts +2 -0
  181. package/lib/testUtils/interop.d.ts.map +1 -0
  182. package/lib/testUtils/interop.js +24 -0
  183. package/lib/testUtils/interop.js.map +1 -0
  184. package/lib/testUtils/params.d.ts +18 -0
  185. package/lib/testUtils/params.d.ts.map +1 -0
  186. package/lib/testUtils/params.js +20 -0
  187. package/lib/testUtils/params.js.map +1 -0
  188. package/lib/testUtils/state.d.ts +20 -0
  189. package/lib/testUtils/state.d.ts.map +1 -0
  190. package/lib/testUtils/state.js +78 -0
  191. package/lib/testUtils/state.js.map +1 -0
  192. package/lib/testUtils/testFileCache.d.ts +17 -0
  193. package/lib/testUtils/testFileCache.d.ts.map +1 -0
  194. package/lib/testUtils/testFileCache.js +96 -0
  195. package/lib/testUtils/testFileCache.js.map +1 -0
  196. package/lib/testUtils/util.d.ts +50 -0
  197. package/lib/testUtils/util.d.ts.map +1 -0
  198. package/lib/testUtils/util.js +329 -0
  199. package/lib/testUtils/util.js.map +1 -0
  200. package/lib/util/aggregator.js.map +1 -1
  201. package/lib/util/altair.js.map +1 -1
  202. package/lib/util/array.js.map +1 -1
  203. package/lib/util/attestation.js.map +1 -1
  204. package/lib/util/attesterStatus.js.map +1 -1
  205. package/lib/util/balance.js.map +1 -1
  206. package/lib/util/blindedBlock.js.map +1 -1
  207. package/lib/util/blockRoot.js.map +1 -1
  208. package/lib/util/capella.js.map +1 -1
  209. package/lib/util/computeAnchorCheckpoint.js.map +1 -1
  210. package/lib/util/deposit.js.map +1 -1
  211. package/lib/util/domain.js.map +1 -1
  212. package/lib/util/electra.js.map +1 -1
  213. package/lib/util/epoch.js.map +1 -1
  214. package/lib/util/epochShuffling.js.map +1 -1
  215. package/lib/util/execution.js.map +1 -1
  216. package/lib/util/finality.js.map +1 -1
  217. package/lib/util/fulu.js.map +1 -1
  218. package/lib/util/genesis.js.map +1 -1
  219. package/lib/util/gloas.js.map +1 -1
  220. package/lib/util/interop.js.map +1 -1
  221. package/lib/util/loadState/findModifiedInactivityScores.js.map +1 -1
  222. package/lib/util/loadState/findModifiedValidators.js.map +1 -1
  223. package/lib/util/loadState/loadState.js.map +1 -1
  224. package/lib/util/loadState/loadValidator.js.map +1 -1
  225. package/lib/util/rootCache.d.ts.map +1 -1
  226. package/lib/util/rootCache.js.map +1 -1
  227. package/lib/util/seed.d.ts +0 -8
  228. package/lib/util/seed.d.ts.map +1 -1
  229. package/lib/util/seed.js +0 -64
  230. package/lib/util/seed.js.map +1 -1
  231. package/lib/util/shuffling.js +2 -1
  232. package/lib/util/shuffling.js.map +1 -1
  233. package/lib/util/signatureSets.d.ts +7 -7
  234. package/lib/util/signatureSets.d.ts.map +1 -1
  235. package/lib/util/signatureSets.js +20 -13
  236. package/lib/util/signatureSets.js.map +1 -1
  237. package/lib/util/signingRoot.js.map +1 -1
  238. package/lib/util/slot.js.map +1 -1
  239. package/lib/util/sszBytes.js.map +1 -1
  240. package/lib/util/syncCommittee.js.map +1 -1
  241. package/lib/util/targetUnslashedBalance.js.map +1 -1
  242. package/lib/util/validator.js.map +1 -1
  243. package/lib/util/weakSubjectivity.js +1 -1
  244. package/lib/util/weakSubjectivity.js.map +1 -1
  245. package/package.json +14 -9
  246. package/src/block/externalData.ts +2 -0
  247. package/src/block/index.ts +3 -3
  248. package/src/block/isValidIndexedAttestation.ts +5 -5
  249. package/src/block/isValidIndexedPayloadAttestation.ts +3 -3
  250. package/src/block/processAttestationPhase0.ts +1 -1
  251. package/src/block/processAttestationsAltair.ts +2 -2
  252. package/src/block/processAttesterSlashing.ts +4 -4
  253. package/src/block/processExecutionPayloadBid.ts +4 -4
  254. package/src/block/processExecutionPayloadEnvelope.ts +40 -22
  255. package/src/block/processOperations.ts +1 -1
  256. package/src/block/processPayloadAttestation.ts +2 -2
  257. package/src/block/processProposerSlashing.ts +4 -4
  258. package/src/block/processRandao.ts +1 -1
  259. package/src/block/processSyncCommittee.ts +1 -1
  260. package/src/block/processVoluntaryExit.ts +1 -1
  261. package/src/block/processWithdrawalRequest.ts +2 -2
  262. package/src/block/processWithdrawals.ts +1 -1
  263. package/src/cache/epochCache.ts +44 -36
  264. package/src/cache/pubkeyCache.ts +62 -21
  265. package/src/cache/stateCache.ts +4 -8
  266. package/src/cache/syncCommitteeCache.ts +4 -5
  267. package/src/epoch/index.ts +1 -1
  268. package/src/epoch/processBuilderPendingPayments.ts +2 -2
  269. package/src/index.ts +3 -1
  270. package/src/lightClient/proofs.ts +83 -0
  271. package/src/lightClient/types.ts +33 -0
  272. package/src/rewards/attestationsRewards.ts +5 -5
  273. package/src/rewards/syncCommitteeRewards.ts +6 -5
  274. package/src/signatureSets/proposer.ts +3 -3
  275. package/src/signatureSets/randao.ts +3 -7
  276. package/src/signatureSets/voluntaryExits.ts +3 -3
  277. package/src/stateTransition.ts +2 -1
  278. package/src/stateView/beaconStateView.ts +809 -0
  279. package/src/stateView/index.ts +2 -0
  280. package/src/stateView/interface.ts +213 -0
  281. package/src/testUtils/cache.ts +8 -0
  282. package/src/testUtils/index.ts +5 -0
  283. package/src/testUtils/infura.ts +10 -0
  284. package/src/testUtils/interop.ts +29 -0
  285. package/src/testUtils/params.ts +23 -0
  286. package/src/testUtils/state.ts +110 -0
  287. package/src/testUtils/testFileCache.ts +127 -0
  288. package/src/testUtils/util.ts +429 -0
  289. package/src/util/seed.ts +0 -71
  290. package/src/util/signatureSets.ts +23 -17
  291. package/src/util/weakSubjectivity.ts +1 -1
@@ -0,0 +1,809 @@
1
+ import {CompactMultiProof, ProofType, Tree, createProof} from "@chainsafe/persistent-merkle-tree";
2
+ import {ByteViews} from "@chainsafe/ssz";
3
+ import {BeaconConfig} from "@lodestar/config";
4
+ import {ForkSeq, SLOTS_PER_HISTORICAL_ROOT, isForkPostGloas} from "@lodestar/params";
5
+ import {
6
+ BeaconBlock,
7
+ BlindedBeaconBlock,
8
+ BuilderIndex,
9
+ Bytes32,
10
+ Epoch,
11
+ ExecutionPayloadBid,
12
+ ExecutionPayloadHeader,
13
+ Root,
14
+ RootHex,
15
+ SignedBeaconBlock,
16
+ SignedBlindedBeaconBlock,
17
+ Slot,
18
+ SyncCommittee,
19
+ ValidatorIndex,
20
+ capella,
21
+ electra,
22
+ fulu,
23
+ getValidatorStatus,
24
+ gloas,
25
+ mapToGeneralStatus,
26
+ phase0,
27
+ rewards,
28
+ } from "@lodestar/types";
29
+ import {Checkpoint, Fork} from "@lodestar/types/phase0";
30
+ import {processExecutionPayloadEnvelope} from "../block/index.js";
31
+ import {VoluntaryExitValidity, getVoluntaryExitValidity} from "../block/processVoluntaryExit.js";
32
+ import {getExpectedWithdrawals} from "../block/processWithdrawals.js";
33
+ import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
34
+ import {EpochTransitionCacheOpts} from "../cache/epochTransitionCache.js";
35
+ import {PubkeyCache, createPubkeyCache} from "../cache/pubkeyCache.js";
36
+ import {RewardCache} from "../cache/rewardCache.js";
37
+ import {
38
+ CachedBeaconStateAllForks,
39
+ CachedBeaconStateAltair,
40
+ CachedBeaconStateCapella,
41
+ CachedBeaconStateElectra,
42
+ CachedBeaconStateExecutions,
43
+ CachedBeaconStateFulu,
44
+ CachedBeaconStateGloas,
45
+ createCachedBeaconState,
46
+ isStateValidatorsNodesPopulated,
47
+ } from "../cache/stateCache.js";
48
+ import {SyncCommitteeCache} from "../cache/syncCommitteeCache.js";
49
+ import {BeaconStateAllForks} from "../cache/types.js";
50
+ import {computeUnrealizedCheckpoints} from "../epoch/computeUnrealizedCheckpoints.js";
51
+ import {getFinalizedRootProof, getSyncCommitteesWitness} from "../lightClient/proofs.js";
52
+ import {SyncCommitteeWitness} from "../lightClient/types.js";
53
+ import {computeAttestationsRewards} from "../rewards/attestationsRewards.js";
54
+ import {computeBlockRewards} from "../rewards/blockRewards.js";
55
+ import {computeSyncCommitteeRewards} from "../rewards/syncCommitteeRewards.js";
56
+ import {StateTransitionModules, StateTransitionOpts, processSlots, stateTransition} from "../stateTransition.js";
57
+ import {getEffectiveBalanceIncrementsZeroInactive} from "../util/balance.js";
58
+ import {getBlockRootAtSlot} from "../util/blockRoot.js";
59
+ import {computeAnchorCheckpoint} from "../util/computeAnchorCheckpoint.js";
60
+ import {computeEpochAtSlot, computeStartSlotAtEpoch} from "../util/epoch.js";
61
+ import {EpochShuffling} from "../util/epochShuffling.js";
62
+ import {isExecutionEnabled, isExecutionStateType, isMergeTransitionComplete} from "../util/execution.js";
63
+ import {canBuilderCoverBid} from "../util/gloas.js";
64
+ import {loadState} from "../util/loadState/loadState.js";
65
+ import {getRandaoMix} from "../util/seed.js";
66
+ import {getStateTypeFromBytes} from "../util/sszBytes.js";
67
+ import {getLatestWeakSubjectivityCheckpointEpoch} from "../util/weakSubjectivity.js";
68
+ import {IBeaconStateView} from "./interface.js";
69
+
70
+ export class BeaconStateView implements IBeaconStateView {
71
+ private readonly config: BeaconConfig;
72
+ // Cached values extracted from the tree
73
+ // phase0
74
+ private _fork: Fork | null = null;
75
+ private _latestBlockHeader: phase0.BeaconBlockHeader | null = null;
76
+ // altair
77
+ private _currentSyncCommittee: SyncCommittee | null = null;
78
+ private _nextSyncCommittee: SyncCommittee | null = null;
79
+ private _previousEpochParticipation: Uint8Array | null = null;
80
+ private _currentEpochParticipation: Uint8Array | null = null;
81
+ // bellatrix
82
+ private _latestExecutionPayloadHeader: ExecutionPayloadHeader | null = null;
83
+ // Caches the cross-fork latestBlockHash value
84
+ private _latestBlockHash: Bytes32 | null = null;
85
+ // capella
86
+ private _historicalSummaries: capella.HistoricalSummaries | null = null;
87
+ // electra
88
+ private _pendingPartialWithdrawals: electra.PendingPartialWithdrawals | null = null;
89
+ private _pendingConsolidations: electra.PendingConsolidations | null = null;
90
+ private _pendingDeposits: electra.PendingDeposits | null = null;
91
+ // fulu
92
+ private _proposerLookahead: fulu.ProposerLookahead | null = null;
93
+ // gloas
94
+ private _executionPayloadAvailability: boolean[] | null = null;
95
+ private _latestExecutionPayloadBid: ExecutionPayloadBid | null = null;
96
+
97
+ constructor(readonly cachedState: CachedBeaconStateAllForks) {
98
+ this.config = cachedState.config;
99
+ }
100
+
101
+ // phase0
102
+
103
+ get slot(): number {
104
+ return this.cachedState.slot;
105
+ }
106
+
107
+ get fork(): Fork {
108
+ if (this._fork === null) {
109
+ this._fork = this.cachedState.fork.toValue();
110
+ }
111
+ return this._fork;
112
+ }
113
+
114
+ get epoch(): number {
115
+ return computeEpochAtSlot(this.slot);
116
+ }
117
+
118
+ get genesisTime(): number {
119
+ return this.cachedState.genesisTime;
120
+ }
121
+
122
+ get genesisValidatorsRoot(): Root {
123
+ return this.cachedState.genesisValidatorsRoot;
124
+ }
125
+
126
+ get eth1Data(): phase0.Eth1Data {
127
+ return this.cachedState.eth1Data;
128
+ }
129
+
130
+ get latestBlockHeader(): phase0.BeaconBlockHeader {
131
+ if (this._latestBlockHeader === null) {
132
+ this._latestBlockHeader = this.cachedState.latestBlockHeader.toValue();
133
+ }
134
+ return this._latestBlockHeader;
135
+ }
136
+
137
+ get previousJustifiedCheckpoint(): Checkpoint {
138
+ return this.cachedState.previousJustifiedCheckpoint;
139
+ }
140
+
141
+ get currentJustifiedCheckpoint(): Checkpoint {
142
+ return this.cachedState.currentJustifiedCheckpoint;
143
+ }
144
+
145
+ get finalizedCheckpoint(): Checkpoint {
146
+ return this.cachedState.finalizedCheckpoint;
147
+ }
148
+
149
+ getBlockRootAtSlot(slot: Slot): Root {
150
+ return getBlockRootAtSlot(this.cachedState, slot);
151
+ }
152
+
153
+ getBlockRootAtEpoch(epoch: Epoch): Root {
154
+ return this.getBlockRootAtSlot(computeStartSlotAtEpoch(epoch));
155
+ }
156
+
157
+ getStateRootAtSlot(slot: Slot): Root {
158
+ return this.cachedState.stateRoots.get(slot % SLOTS_PER_HISTORICAL_ROOT);
159
+ }
160
+
161
+ getRandaoMix(epoch: Epoch): Bytes32 {
162
+ return getRandaoMix(this.cachedState, epoch);
163
+ }
164
+
165
+ get previousEpochParticipation(): Uint8Array {
166
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
167
+ throw new Error("previousEpochParticipation is not available before Altair");
168
+ }
169
+
170
+ if (this._previousEpochParticipation === null) {
171
+ this._previousEpochParticipation = (
172
+ this.cachedState as CachedBeaconStateAltair
173
+ ).previousEpochParticipation.serialize();
174
+ }
175
+
176
+ return this._previousEpochParticipation;
177
+ }
178
+
179
+ // altair
180
+
181
+ get currentEpochParticipation(): Uint8Array {
182
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
183
+ throw new Error("currentEpochParticipation is not available before Altair");
184
+ }
185
+
186
+ if (this._currentEpochParticipation === null) {
187
+ this._currentEpochParticipation = (
188
+ this.cachedState as CachedBeaconStateAltair
189
+ ).currentEpochParticipation.serialize();
190
+ }
191
+
192
+ return this._currentEpochParticipation;
193
+ }
194
+
195
+ getPreviousEpochParticipation(validatorIndex: ValidatorIndex): number {
196
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
197
+ throw new Error("previousEpochParticipation is not available before Altair");
198
+ }
199
+ return (this.cachedState as CachedBeaconStateAltair).previousEpochParticipation.get(validatorIndex);
200
+ }
201
+
202
+ getCurrentEpochParticipation(validatorIndex: ValidatorIndex): number {
203
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
204
+ throw new Error("currentEpochParticipation is not available before Altair");
205
+ }
206
+ return (this.cachedState as CachedBeaconStateAltair).currentEpochParticipation.get(validatorIndex);
207
+ }
208
+
209
+ // bellatrix
210
+
211
+ get latestExecutionPayloadHeader(): ExecutionPayloadHeader {
212
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.bellatrix) {
213
+ throw new Error("latestExecutionPayloadHeader is not available before Bellatrix");
214
+ }
215
+
216
+ if (this._latestExecutionPayloadHeader === null) {
217
+ this._latestExecutionPayloadHeader = (
218
+ this.cachedState as CachedBeaconStateExecutions
219
+ ).latestExecutionPayloadHeader.toValue();
220
+ }
221
+
222
+ return this._latestExecutionPayloadHeader;
223
+ }
224
+
225
+ /**
226
+ * Cross-fork accessor for the execution block hash of the most recently included payload.
227
+ * Pre-gloas: reads from latestExecutionPayloadHeader.blockHash.
228
+ * Gloas+: reads the dedicated latestBlockHash field (EIP-7732).
229
+ */
230
+ get latestBlockHash(): Bytes32 {
231
+ const forkSeq = this.config.getForkSeq(this.cachedState.slot);
232
+ if (forkSeq < ForkSeq.bellatrix) {
233
+ throw new Error("latestBlockHash is not available before Bellatrix");
234
+ }
235
+
236
+ if (this._latestBlockHash === null) {
237
+ if (forkSeq >= ForkSeq.gloas) {
238
+ this._latestBlockHash = (this.cachedState as CachedBeaconStateGloas).latestBlockHash;
239
+ } else {
240
+ this._latestBlockHash = (
241
+ this.cachedState as CachedBeaconStateExecutions
242
+ ).latestExecutionPayloadHeader.blockHash;
243
+ }
244
+ }
245
+
246
+ return this._latestBlockHash;
247
+ }
248
+
249
+ /**
250
+ * The execution block number of the most recently included payload.
251
+ * Named payloadBlockNumber (not latestBlockNumber) to mirror ExecutionPayloadHeader.blockNumber pre-gloas.
252
+ * Only available from bellatrix through fulu — not tracked on BeaconState in gloas+ (EIP-7732).
253
+ */
254
+ get payloadBlockNumber(): number {
255
+ const forkSeq = this.config.getForkSeq(this.cachedState.slot);
256
+ if (forkSeq < ForkSeq.bellatrix) {
257
+ throw new Error("payloadBlockNumber is not available before Bellatrix");
258
+ }
259
+ if (forkSeq >= ForkSeq.gloas) {
260
+ throw new Error("payloadBlockNumber is not available post-gloas");
261
+ }
262
+ return (this.cachedState as CachedBeaconStateExecutions).latestExecutionPayloadHeader.blockNumber;
263
+ }
264
+
265
+ // capella
266
+
267
+ get historicalSummaries(): capella.HistoricalSummaries {
268
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.capella) {
269
+ throw new Error("Historical summaries are not supported before Capella");
270
+ }
271
+
272
+ if (this._historicalSummaries === null) {
273
+ this._historicalSummaries = (this.cachedState as CachedBeaconStateCapella).historicalSummaries.toValue();
274
+ }
275
+
276
+ return this._historicalSummaries;
277
+ }
278
+
279
+ // electra
280
+
281
+ get pendingDeposits(): electra.PendingDeposits {
282
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
283
+ throw new Error("Pending deposits are not supported before Electra");
284
+ }
285
+
286
+ if (this._pendingDeposits === null) {
287
+ this._pendingDeposits = (this.cachedState as CachedBeaconStateElectra).pendingDeposits.toValue();
288
+ }
289
+
290
+ return this._pendingDeposits;
291
+ }
292
+
293
+ get pendingDepositsCount(): number {
294
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
295
+ throw new Error("Pending deposits are not supported before Electra");
296
+ }
297
+
298
+ return (this.cachedState as CachedBeaconStateElectra).pendingDeposits.length;
299
+ }
300
+
301
+ get pendingPartialWithdrawals(): electra.PendingPartialWithdrawals {
302
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
303
+ throw new Error("Pending partial withdrawals are not supported before Electra");
304
+ }
305
+
306
+ if (this._pendingPartialWithdrawals === null) {
307
+ this._pendingPartialWithdrawals = (
308
+ this.cachedState as CachedBeaconStateElectra
309
+ ).pendingPartialWithdrawals.toValue();
310
+ }
311
+
312
+ return this._pendingPartialWithdrawals;
313
+ }
314
+
315
+ get pendingPartialWithdrawalsCount(): number {
316
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
317
+ throw new Error("Pending partial withdrawals are not supported before Electra");
318
+ }
319
+
320
+ return (this.cachedState as CachedBeaconStateElectra).pendingPartialWithdrawals.length;
321
+ }
322
+
323
+ get pendingConsolidations(): electra.PendingConsolidations {
324
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
325
+ throw new Error("Pending consolidations are not supported before Electra");
326
+ }
327
+
328
+ if (this._pendingConsolidations === null) {
329
+ this._pendingConsolidations = (this.cachedState as CachedBeaconStateElectra).pendingConsolidations.toValue();
330
+ }
331
+
332
+ return this._pendingConsolidations;
333
+ }
334
+
335
+ get pendingConsolidationsCount(): number {
336
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.electra) {
337
+ throw new Error("Pending consolidations are not supported before Electra");
338
+ }
339
+
340
+ return (this.cachedState as CachedBeaconStateElectra).pendingConsolidations.length;
341
+ }
342
+
343
+ // fulu
344
+
345
+ get proposerLookahead(): fulu.ProposerLookahead {
346
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.fulu) {
347
+ throw new Error("Proposer lookahead is not supported before Fulu");
348
+ }
349
+
350
+ if (this._proposerLookahead === null) {
351
+ this._proposerLookahead = (this.cachedState as CachedBeaconStateFulu).proposerLookahead.toValue();
352
+ }
353
+
354
+ return this._proposerLookahead;
355
+ }
356
+
357
+ // gloas
358
+
359
+ get executionPayloadAvailability(): boolean[] {
360
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
361
+ throw new Error("executionPayloadAvailability is not available before GLOAS");
362
+ }
363
+
364
+ if (this._executionPayloadAvailability === null) {
365
+ this._executionPayloadAvailability = (this.cachedState as CachedBeaconStateGloas).executionPayloadAvailability
366
+ .toValue()
367
+ .toBoolArray();
368
+ }
369
+
370
+ return this._executionPayloadAvailability;
371
+ }
372
+
373
+ get latestExecutionPayloadBid(): ExecutionPayloadBid {
374
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
375
+ throw new Error("latestExecutionPayloadBid is not available before GLOAS");
376
+ }
377
+
378
+ if (this._latestExecutionPayloadBid === null) {
379
+ this._latestExecutionPayloadBid = (
380
+ this.cachedState as CachedBeaconStateGloas
381
+ ).latestExecutionPayloadBid.toValue();
382
+ }
383
+ return this._latestExecutionPayloadBid;
384
+ }
385
+
386
+ getBuilder(index: BuilderIndex): gloas.Builder {
387
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
388
+ throw new Error("Builders are not supported before GLOAS");
389
+ }
390
+
391
+ return (this.cachedState as CachedBeaconStateGloas).builders.getReadonly(index);
392
+ }
393
+
394
+ canBuilderCoverBid(builderIndex: BuilderIndex, bidAmount: number): boolean {
395
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
396
+ throw new Error("Builders are not supported before GLOAS");
397
+ }
398
+
399
+ return canBuilderCoverBid(this.cachedState as CachedBeaconStateGloas, builderIndex, bidAmount);
400
+ }
401
+
402
+ /**
403
+ * Return the index of the validator in the PTC committee for the given slot.
404
+ * return -1 if validator is not in the PTC committee for the given slot.
405
+ */
406
+ validatorPTCCommitteeIndex(validatorIndex: ValidatorIndex, slot: Slot): number {
407
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.gloas) {
408
+ throw new Error("PTC committees are not supported before GLOAS");
409
+ }
410
+
411
+ const ptcCommittee = (this.cachedState as CachedBeaconStateGloas).epochCtx.getPayloadTimelinessCommittee(slot);
412
+ return ptcCommittee.indexOf(validatorIndex);
413
+ }
414
+
415
+ // Shuffling and committees
416
+
417
+ getShufflingAtEpoch(epoch: Epoch): EpochShuffling {
418
+ return this.cachedState.epochCtx.getShufflingAtEpoch(epoch);
419
+ }
420
+
421
+ get previousDecisionRoot(): RootHex {
422
+ return this.cachedState.epochCtx.previousDecisionRoot;
423
+ }
424
+
425
+ get currentDecisionRoot(): RootHex {
426
+ return this.cachedState.epochCtx.currentDecisionRoot;
427
+ }
428
+
429
+ get nextDecisionRoot(): RootHex {
430
+ return this.cachedState.epochCtx.nextDecisionRoot;
431
+ }
432
+
433
+ getShufflingDecisionRoot(epoch: Epoch): RootHex {
434
+ return this.cachedState.epochCtx.getShufflingDecisionRoot(epoch);
435
+ }
436
+
437
+ getPreviousShuffling(): EpochShuffling {
438
+ return this.cachedState.epochCtx.previousShuffling;
439
+ }
440
+
441
+ getCurrentShuffling(): EpochShuffling {
442
+ return this.cachedState.epochCtx.currentShuffling;
443
+ }
444
+
445
+ getNextShuffling(): EpochShuffling {
446
+ return this.cachedState.epochCtx.nextShuffling;
447
+ }
448
+
449
+ // Proposer shuffling
450
+
451
+ get previousProposers(): ValidatorIndex[] | null {
452
+ return this.cachedState.epochCtx.proposersPrevEpoch;
453
+ }
454
+
455
+ get currentProposers(): ValidatorIndex[] {
456
+ return this.cachedState.epochCtx.getBeaconProposers();
457
+ }
458
+
459
+ get nextProposers(): ValidatorIndex[] {
460
+ return this.cachedState.epochCtx.getBeaconProposersNextEpoch();
461
+ }
462
+
463
+ getBeaconProposer(slot: number): ValidatorIndex {
464
+ return this.cachedState.epochCtx.getBeaconProposer(slot);
465
+ }
466
+
467
+ computeAnchorCheckpoint(): {checkpoint: phase0.Checkpoint; blockHeader: phase0.BeaconBlockHeader} {
468
+ return computeAnchorCheckpoint(this.config, this.cachedState);
469
+ }
470
+
471
+ // Sync committees
472
+
473
+ get currentSyncCommittee(): SyncCommittee {
474
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
475
+ throw new Error("currentSyncCommittee is not available before Altair");
476
+ }
477
+
478
+ if (this._currentSyncCommittee === null) {
479
+ this._currentSyncCommittee = (this.cachedState as CachedBeaconStateAltair).currentSyncCommittee.toValue();
480
+ }
481
+
482
+ return this._currentSyncCommittee;
483
+ }
484
+
485
+ get nextSyncCommittee(): SyncCommittee {
486
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.altair) {
487
+ throw new Error("nextSyncCommittee is not available before Altair");
488
+ }
489
+
490
+ if (this._nextSyncCommittee === null) {
491
+ this._nextSyncCommittee = (this.cachedState as CachedBeaconStateAltair).nextSyncCommittee.toValue();
492
+ }
493
+
494
+ return this._nextSyncCommittee;
495
+ }
496
+
497
+ get currentSyncCommitteeIndexed(): SyncCommitteeCache {
498
+ return this.cachedState.epochCtx.currentSyncCommitteeIndexed;
499
+ }
500
+
501
+ get syncProposerReward(): number {
502
+ return this.cachedState.epochCtx.syncProposerReward;
503
+ }
504
+
505
+ getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache {
506
+ return this.cachedState.epochCtx.getIndexedSyncCommitteeAtEpoch(epoch);
507
+ }
508
+
509
+ // Validators and balances
510
+
511
+ get effectiveBalanceIncrements(): EffectiveBalanceIncrements {
512
+ return this.cachedState.epochCtx.effectiveBalanceIncrements;
513
+ }
514
+
515
+ getEffectiveBalanceIncrementsZeroInactive(): EffectiveBalanceIncrements {
516
+ return getEffectiveBalanceIncrementsZeroInactive(this.cachedState);
517
+ }
518
+
519
+ getBalance(index: number): number {
520
+ return this.cachedState.balances.get(index);
521
+ }
522
+
523
+ getValidator(index: ValidatorIndex): phase0.Validator {
524
+ return this.cachedState.validators.getReadonly(index).toValue();
525
+ }
526
+
527
+ getValidatorsByStatus(statuses: Set<string>, currentEpoch: Epoch): phase0.Validator[] {
528
+ const validators: phase0.Validator[] = [];
529
+ const validatorsArr = this.cachedState.validators.getAllReadonlyValues();
530
+
531
+ for (const validator of validatorsArr) {
532
+ const validatorStatus = getValidatorStatus(validator, currentEpoch);
533
+ if (statuses.has(validatorStatus) || statuses.has(mapToGeneralStatus(validatorStatus))) {
534
+ validators.push(validator);
535
+ }
536
+ }
537
+ return validators;
538
+ }
539
+
540
+ get validatorCount(): number {
541
+ return this.cachedState.validators.length;
542
+ }
543
+
544
+ get activeValidatorCount(): number {
545
+ return this.cachedState.epochCtx.currentShuffling.activeIndices.length;
546
+ }
547
+
548
+ getAllValidators(): phase0.Validator[] {
549
+ return this.cachedState.validators.getAllReadonlyValues();
550
+ }
551
+
552
+ getAllBalances(): number[] {
553
+ return this.cachedState.balances.getAll();
554
+ }
555
+
556
+ // Merge
557
+
558
+ get isExecutionStateType(): boolean {
559
+ return this.config.getForkSeq(this.cachedState.slot) >= ForkSeq.bellatrix;
560
+ }
561
+
562
+ isExecutionEnabled(block: BeaconBlock | BlindedBeaconBlock): boolean {
563
+ if (this.config.getForkSeq(this.cachedState.slot) < ForkSeq.bellatrix) {
564
+ return false;
565
+ }
566
+
567
+ return isExecutionEnabled(this.cachedState as CachedBeaconStateExecutions, block);
568
+ }
569
+
570
+ get isMergeTransitionComplete(): boolean {
571
+ return isExecutionStateType(this.cachedState) && isMergeTransitionComplete(this.cachedState);
572
+ }
573
+
574
+ // Block production
575
+
576
+ getExpectedWithdrawals(): {
577
+ expectedWithdrawals: capella.Withdrawal[];
578
+ processedBuilderWithdrawalsCount: number;
579
+ processedPartialWithdrawalsCount: number;
580
+ processedBuildersSweepCount: number;
581
+ processedValidatorSweepCount: number;
582
+ } {
583
+ const fork = this.config.getForkSeq(this.cachedState.slot);
584
+ return getExpectedWithdrawals(
585
+ fork,
586
+ this.cachedState as CachedBeaconStateCapella | CachedBeaconStateElectra | CachedBeaconStateGloas
587
+ );
588
+ }
589
+
590
+ // API
591
+
592
+ get proposerRewards(): RewardCache {
593
+ return this.cachedState.proposerRewards;
594
+ }
595
+
596
+ async computeBlockRewards(block: BeaconBlock, proposerRewards?: RewardCache): Promise<rewards.BlockRewards> {
597
+ return computeBlockRewards(this.cachedState.config, block, this.cachedState, proposerRewards);
598
+ }
599
+
600
+ async computeAttestationsRewards(validatorIds?: (ValidatorIndex | string)[]): Promise<rewards.AttestationsRewards> {
601
+ return computeAttestationsRewards(
602
+ this.cachedState.config,
603
+ this.cachedState.epochCtx.pubkeyCache,
604
+ this.cachedState,
605
+ validatorIds
606
+ );
607
+ }
608
+
609
+ async computeSyncCommitteeRewards(
610
+ block: BeaconBlock,
611
+ validatorIds: (ValidatorIndex | string)[]
612
+ ): Promise<rewards.SyncCommitteeRewards> {
613
+ return computeSyncCommitteeRewards(
614
+ this.cachedState.config,
615
+ this.cachedState.epochCtx.pubkeyCache,
616
+ block,
617
+ this.cachedState,
618
+ validatorIds
619
+ );
620
+ }
621
+
622
+ getLatestWeakSubjectivityCheckpointEpoch(): Epoch {
623
+ return getLatestWeakSubjectivityCheckpointEpoch(this.config, this.cachedState);
624
+ }
625
+
626
+ // Validation
627
+
628
+ getVoluntaryExitValidity(
629
+ signedVoluntaryExit: phase0.SignedVoluntaryExit,
630
+ verifySignature = true
631
+ ): VoluntaryExitValidity {
632
+ const stateFork = this.config.getForkSeq(this.cachedState.slot);
633
+ return getVoluntaryExitValidity(stateFork, this.cachedState, signedVoluntaryExit, verifySignature);
634
+ }
635
+
636
+ isValidVoluntaryExit(signedVoluntaryExit: phase0.SignedVoluntaryExit, verifySignature: boolean): boolean {
637
+ return this.getVoluntaryExitValidity(signedVoluntaryExit, verifySignature) === VoluntaryExitValidity.valid;
638
+ }
639
+
640
+ // Proofs
641
+
642
+ getFinalizedRootProof(): Uint8Array[] {
643
+ return getFinalizedRootProof(this.cachedState);
644
+ }
645
+
646
+ getSyncCommitteesWitness(): SyncCommitteeWitness {
647
+ const fork = this.config.getForkName(this.cachedState.slot);
648
+ if (ForkSeq[fork] < ForkSeq.altair) {
649
+ throw new Error("Sync committees witness is not available before Altair");
650
+ }
651
+
652
+ return getSyncCommitteesWitness(fork, this.cachedState);
653
+ }
654
+
655
+ getSingleProof(gindex: bigint): Uint8Array[] {
656
+ return new Tree(this.cachedState.node).getSingleProof(gindex);
657
+ }
658
+
659
+ createMultiProof(descriptor: Uint8Array): CompactMultiProof {
660
+ const stateNode = this.cachedState.node;
661
+ return createProof(stateNode, {type: ProofType.compactMulti, descriptor}) as CompactMultiProof;
662
+ }
663
+
664
+ // Fork choice
665
+
666
+ computeUnrealizedCheckpoints(): {
667
+ justifiedCheckpoint: phase0.Checkpoint;
668
+ finalizedCheckpoint: phase0.Checkpoint;
669
+ } {
670
+ return computeUnrealizedCheckpoints(this.cachedState);
671
+ }
672
+
673
+ // this is for backward compatible
674
+
675
+ get clonedCount(): number {
676
+ return this.cachedState.clonedCount;
677
+ }
678
+
679
+ get clonedCountWithTransferCache(): number {
680
+ return this.cachedState.clonedCountWithTransferCache;
681
+ }
682
+
683
+ get createdWithTransferCache(): boolean {
684
+ return this.cachedState.createdWithTransferCache;
685
+ }
686
+
687
+ isStateValidatorsNodesPopulated(): boolean {
688
+ return isStateValidatorsNodesPopulated(this.cachedState);
689
+ }
690
+
691
+ // Serialization
692
+
693
+ loadOtherState(stateBytes: Uint8Array, seedValidatorsBytes?: Uint8Array): IBeaconStateView {
694
+ const {state} = loadState(this.config, this.cachedState, stateBytes, seedValidatorsBytes);
695
+
696
+ const cachedState = createCachedBeaconState(
697
+ state,
698
+ {
699
+ config: this.config,
700
+ // as of Feb 2026, it's not necessary to sync pubkey cache as it's shared across states in Lodestar
701
+ pubkeyCache: this.cachedState.epochCtx.pubkeyCache,
702
+ },
703
+ {
704
+ skipSyncPubkeys: true,
705
+ }
706
+ );
707
+
708
+ // load all cache in order for consumers (usually regen.getState()) to process blocks faster
709
+ cachedState.validators.getAllReadonlyValues();
710
+ cachedState.balances.getAll();
711
+
712
+ return new BeaconStateView(cachedState);
713
+ }
714
+
715
+ serialize(): Uint8Array {
716
+ return this.cachedState.serialize();
717
+ }
718
+
719
+ serializedSize(): number {
720
+ return this.cachedState.type.tree_serializedSize(this.cachedState.node);
721
+ }
722
+
723
+ serializeToBytes(output: ByteViews, offset: number): number {
724
+ return this.cachedState.serializeToBytes(output, offset);
725
+ }
726
+
727
+ serializeValidators(): Uint8Array {
728
+ return this.cachedState.validators.serialize();
729
+ }
730
+
731
+ serializedValidatorsSize(): number {
732
+ const type = this.cachedState.type.fields.validators;
733
+ return type.tree_serializedSize(this.cachedState.validators.node);
734
+ }
735
+
736
+ serializeValidatorsToBytes(output: ByteViews, offset: number): number {
737
+ return this.cachedState.validators.serializeToBytes(output, offset);
738
+ }
739
+
740
+ hashTreeRoot(): Uint8Array {
741
+ return this.cachedState.hashTreeRoot();
742
+ }
743
+
744
+ // State transition
745
+
746
+ stateTransition(
747
+ signedBlock: SignedBeaconBlock | SignedBlindedBeaconBlock,
748
+ options: StateTransitionOpts,
749
+ {metrics, validatorMonitor}: StateTransitionModules
750
+ ): IBeaconStateView {
751
+ const newState = stateTransition(this.cachedState, signedBlock, options, {metrics, validatorMonitor});
752
+ return new BeaconStateView(newState);
753
+ }
754
+
755
+ processSlots(
756
+ slot: Slot,
757
+ epochTransitionCacheOpts?: EpochTransitionCacheOpts & {dontTransferCache?: boolean},
758
+ modules?: StateTransitionModules
759
+ ): IBeaconStateView {
760
+ const newState = processSlots(this.cachedState, slot, epochTransitionCacheOpts, modules);
761
+ return new BeaconStateView(newState);
762
+ }
763
+
764
+ processExecutionPayloadEnvelope(signedEnvelope: gloas.SignedExecutionPayloadEnvelope, verify: boolean): void {
765
+ const fork = this.config.getForkName(this.cachedState.slot);
766
+ if (!isForkPostGloas(fork)) {
767
+ throw Error(`processExecutionPayloadEnvelope is only available for gloas+ forks, got fork=${fork}`);
768
+ }
769
+ processExecutionPayloadEnvelope(this.cachedState as CachedBeaconStateGloas, signedEnvelope, verify);
770
+ }
771
+ }
772
+
773
+ /**
774
+ * Create BeaconStateView for historical state regen, no need to sync pubkey cache there.
775
+ */
776
+ export function createBeaconStateViewForHistoricalRegen(
777
+ config: BeaconConfig,
778
+ stateBytes: Uint8Array
779
+ ): IBeaconStateView {
780
+ const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
781
+
782
+ const pubkeyCache = createPubkeyCache();
783
+ syncPubkeyCache(state, pubkeyCache);
784
+ const cachedState = createCachedBeaconState(
785
+ state,
786
+ {
787
+ config,
788
+ pubkeyCache,
789
+ },
790
+ {
791
+ skipSyncPubkeys: true,
792
+ }
793
+ );
794
+
795
+ return new BeaconStateView(cachedState);
796
+ }
797
+
798
+ /**
799
+ * Populate a PubkeyIndexMap with any new entries based on a BeaconState
800
+ */
801
+ function syncPubkeyCache(state: BeaconStateAllForks, pubkeyCache: PubkeyCache): void {
802
+ // Get the validators sub tree once for all the loop
803
+
804
+ const newCount = state.validators.length;
805
+ for (let i = pubkeyCache.size; i < newCount; i++) {
806
+ const pubkey = state.validators.getReadonly(i).pubkey;
807
+ pubkeyCache.set(i, pubkey);
808
+ }
809
+ }