@lodestar/beacon-node 1.39.0-dev.882891d89c → 1.39.0-dev.90334173dc

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 (226) hide show
  1. package/README.md +1 -1
  2. package/lib/api/impl/beacon/state/utils.d.ts +2 -7
  3. package/lib/api/impl/beacon/state/utils.d.ts.map +1 -1
  4. package/lib/api/impl/beacon/state/utils.js +0 -12
  5. package/lib/api/impl/beacon/state/utils.js.map +1 -1
  6. package/lib/api/impl/lodestar/index.js +1 -1
  7. package/lib/api/impl/lodestar/index.js.map +1 -1
  8. package/lib/api/impl/proof/index.d.ts.map +1 -1
  9. package/lib/api/impl/proof/index.js +1 -2
  10. package/lib/api/impl/proof/index.js.map +1 -1
  11. package/lib/api/impl/validator/index.d.ts.map +1 -1
  12. package/lib/api/impl/validator/index.js +1 -3
  13. package/lib/api/impl/validator/index.js.map +1 -1
  14. package/lib/chain/archiveStore/historicalState/getHistoricalState.d.ts.map +1 -1
  15. package/lib/chain/archiveStore/historicalState/getHistoricalState.js +5 -3
  16. package/lib/chain/archiveStore/historicalState/getHistoricalState.js.map +1 -1
  17. package/lib/chain/archiveStore/utils/archiveBlocks.d.ts.map +1 -1
  18. package/lib/chain/archiveStore/utils/archiveBlocks.js +19 -14
  19. package/lib/chain/archiveStore/utils/archiveBlocks.js.map +1 -1
  20. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  21. package/lib/chain/blocks/importBlock.js +12 -3
  22. package/lib/chain/blocks/importBlock.js.map +1 -1
  23. package/lib/chain/blocks/verifyBlock.d.ts.map +1 -1
  24. package/lib/chain/blocks/verifyBlock.js +8 -1
  25. package/lib/chain/blocks/verifyBlock.js.map +1 -1
  26. package/lib/chain/blocks/verifyBlocksExecutionPayloads.d.ts.map +1 -1
  27. package/lib/chain/blocks/verifyBlocksExecutionPayloads.js +4 -2
  28. package/lib/chain/blocks/verifyBlocksExecutionPayloads.js.map +1 -1
  29. package/lib/chain/blocks/verifyBlocksSignatures.d.ts.map +1 -1
  30. package/lib/chain/blocks/verifyBlocksSignatures.js +2 -1
  31. package/lib/chain/blocks/verifyBlocksSignatures.js.map +1 -1
  32. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.d.ts.map +1 -1
  33. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.js +1 -0
  34. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.js.map +1 -1
  35. package/lib/chain/chain.d.ts +11 -11
  36. package/lib/chain/chain.d.ts.map +1 -1
  37. package/lib/chain/chain.js +32 -38
  38. package/lib/chain/chain.js.map +1 -1
  39. package/lib/chain/forkChoice/index.d.ts.map +1 -1
  40. package/lib/chain/forkChoice/index.js +3 -3
  41. package/lib/chain/forkChoice/index.js.map +1 -1
  42. package/lib/chain/initState.d.ts +1 -1
  43. package/lib/chain/initState.d.ts.map +1 -1
  44. package/lib/chain/initState.js +5 -3
  45. package/lib/chain/initState.js.map +1 -1
  46. package/lib/chain/interface.d.ts +6 -9
  47. package/lib/chain/interface.d.ts.map +1 -1
  48. package/lib/chain/interface.js.map +1 -1
  49. package/lib/chain/lightClient/proofs.d.ts.map +1 -1
  50. package/lib/chain/lightClient/proofs.js +0 -2
  51. package/lib/chain/lightClient/proofs.js.map +1 -1
  52. package/lib/chain/opPools/aggregatedAttestationPool.d.ts +5 -9
  53. package/lib/chain/opPools/aggregatedAttestationPool.d.ts.map +1 -1
  54. package/lib/chain/opPools/aggregatedAttestationPool.js +12 -141
  55. package/lib/chain/opPools/aggregatedAttestationPool.js.map +1 -1
  56. package/lib/chain/opPools/opPool.js +5 -8
  57. package/lib/chain/opPools/opPool.js.map +1 -1
  58. package/lib/chain/prepareNextSlot.d.ts.map +1 -1
  59. package/lib/chain/prepareNextSlot.js +4 -7
  60. package/lib/chain/prepareNextSlot.js.map +1 -1
  61. package/lib/chain/produceBlock/produceBlockBody.d.ts.map +1 -1
  62. package/lib/chain/produceBlock/produceBlockBody.js +2 -2
  63. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  64. package/lib/chain/regen/interface.d.ts +1 -5
  65. package/lib/chain/regen/interface.d.ts.map +1 -1
  66. package/lib/chain/regen/queued.d.ts +4 -7
  67. package/lib/chain/regen/queued.d.ts.map +1 -1
  68. package/lib/chain/regen/queued.js +15 -25
  69. package/lib/chain/regen/queued.js.map +1 -1
  70. package/lib/chain/regen/regen.d.ts +1 -1
  71. package/lib/chain/regen/regen.d.ts.map +1 -1
  72. package/lib/chain/regen/regen.js +13 -17
  73. package/lib/chain/regen/regen.js.map +1 -1
  74. package/lib/chain/shufflingCache.d.ts +16 -11
  75. package/lib/chain/shufflingCache.d.ts.map +1 -1
  76. package/lib/chain/shufflingCache.js +47 -41
  77. package/lib/chain/shufflingCache.js.map +1 -1
  78. package/lib/chain/stateCache/blockStateCacheImpl.d.ts +1 -2
  79. package/lib/chain/stateCache/blockStateCacheImpl.d.ts.map +1 -1
  80. package/lib/chain/stateCache/blockStateCacheImpl.js +2 -2
  81. package/lib/chain/stateCache/blockStateCacheImpl.js.map +1 -1
  82. package/lib/chain/stateCache/fifoBlockStateCache.d.ts +1 -2
  83. package/lib/chain/stateCache/fifoBlockStateCache.d.ts.map +1 -1
  84. package/lib/chain/stateCache/fifoBlockStateCache.js +4 -4
  85. package/lib/chain/stateCache/fifoBlockStateCache.js.map +1 -1
  86. package/lib/chain/stateCache/inMemoryCheckpointsCache.d.ts +4 -5
  87. package/lib/chain/stateCache/inMemoryCheckpointsCache.d.ts.map +1 -1
  88. package/lib/chain/stateCache/inMemoryCheckpointsCache.js +9 -10
  89. package/lib/chain/stateCache/inMemoryCheckpointsCache.js.map +1 -1
  90. package/lib/chain/stateCache/persistentCheckpointsCache.d.ts +5 -6
  91. package/lib/chain/stateCache/persistentCheckpointsCache.d.ts.map +1 -1
  92. package/lib/chain/stateCache/persistentCheckpointsCache.js +17 -17
  93. package/lib/chain/stateCache/persistentCheckpointsCache.js.map +1 -1
  94. package/lib/chain/stateCache/types.d.ts +5 -6
  95. package/lib/chain/stateCache/types.d.ts.map +1 -1
  96. package/lib/chain/stateCache/types.js.map +1 -1
  97. package/lib/chain/validation/aggregateAndProof.js +9 -0
  98. package/lib/chain/validation/aggregateAndProof.js.map +1 -1
  99. package/lib/chain/validation/attestation.d.ts.map +1 -1
  100. package/lib/chain/validation/attestation.js +2 -2
  101. package/lib/chain/validation/attestation.js.map +1 -1
  102. package/lib/chain/validation/attesterSlashing.d.ts.map +1 -1
  103. package/lib/chain/validation/attesterSlashing.js +2 -2
  104. package/lib/chain/validation/attesterSlashing.js.map +1 -1
  105. package/lib/chain/validation/blobSidecar.d.ts.map +1 -1
  106. package/lib/chain/validation/blobSidecar.js +2 -3
  107. package/lib/chain/validation/blobSidecar.js.map +1 -1
  108. package/lib/chain/validation/block.d.ts.map +1 -1
  109. package/lib/chain/validation/block.js +5 -3
  110. package/lib/chain/validation/block.js.map +1 -1
  111. package/lib/chain/validation/blsToExecutionChange.d.ts.map +1 -1
  112. package/lib/chain/validation/blsToExecutionChange.js +9 -2
  113. package/lib/chain/validation/blsToExecutionChange.js.map +1 -1
  114. package/lib/chain/validation/dataColumnSidecar.d.ts.map +1 -1
  115. package/lib/chain/validation/dataColumnSidecar.js +2 -3
  116. package/lib/chain/validation/dataColumnSidecar.js.map +1 -1
  117. package/lib/chain/validation/proposerSlashing.js +3 -2
  118. package/lib/chain/validation/proposerSlashing.js.map +1 -1
  119. package/lib/chain/validation/voluntaryExit.js +1 -1
  120. package/lib/chain/validation/voluntaryExit.js.map +1 -1
  121. package/lib/db/repositories/checkpointState.d.ts +2 -6
  122. package/lib/db/repositories/checkpointState.d.ts.map +1 -1
  123. package/lib/db/repositories/checkpointState.js +3 -15
  124. package/lib/db/repositories/checkpointState.js.map +1 -1
  125. package/lib/db/repositories/stateArchive.d.ts +9 -9
  126. package/lib/db/repositories/stateArchive.d.ts.map +1 -1
  127. package/lib/db/repositories/stateArchive.js +6 -21
  128. package/lib/db/repositories/stateArchive.js.map +1 -1
  129. package/lib/execution/engine/mock.d.ts +9 -6
  130. package/lib/execution/engine/mock.d.ts.map +1 -1
  131. package/lib/execution/engine/mock.js +34 -7
  132. package/lib/execution/engine/mock.js.map +1 -1
  133. package/lib/index.d.ts +1 -1
  134. package/lib/index.d.ts.map +1 -1
  135. package/lib/index.js +1 -1
  136. package/lib/index.js.map +1 -1
  137. package/lib/metrics/metrics/lodestar.d.ts +1 -6
  138. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  139. package/lib/metrics/metrics/lodestar.js +3 -17
  140. package/lib/metrics/metrics/lodestar.js.map +1 -1
  141. package/lib/network/peers/discover.d.ts.map +1 -1
  142. package/lib/network/peers/discover.js.map +1 -1
  143. package/lib/network/reqresp/utils/dataColumnResponseValidation.js +1 -1
  144. package/lib/network/reqresp/utils/dataColumnResponseValidation.js.map +1 -1
  145. package/lib/node/nodejs.d.ts +6 -3
  146. package/lib/node/nodejs.d.ts.map +1 -1
  147. package/lib/node/nodejs.js +20 -3
  148. package/lib/node/nodejs.js.map +1 -1
  149. package/lib/node/notifier.d.ts.map +1 -1
  150. package/lib/node/notifier.js +9 -6
  151. package/lib/node/notifier.js.map +1 -1
  152. package/lib/sync/backfill/backfill.d.ts.map +1 -1
  153. package/lib/sync/backfill/backfill.js +2 -2
  154. package/lib/sync/backfill/backfill.js.map +1 -1
  155. package/lib/sync/backfill/verify.d.ts +2 -2
  156. package/lib/sync/backfill/verify.d.ts.map +1 -1
  157. package/lib/sync/backfill/verify.js +3 -3
  158. package/lib/sync/backfill/verify.js.map +1 -1
  159. package/lib/util/sszBytes.js +1 -1
  160. package/lib/util/sszBytes.js.map +1 -1
  161. package/package.json +28 -20
  162. package/src/api/impl/beacon/state/utils.ts +2 -22
  163. package/src/api/impl/lodestar/index.ts +1 -1
  164. package/src/api/impl/proof/index.ts +1 -2
  165. package/src/api/impl/validator/index.ts +1 -3
  166. package/src/chain/archiveStore/historicalState/getHistoricalState.ts +5 -3
  167. package/src/chain/archiveStore/utils/archiveBlocks.ts +21 -14
  168. package/src/chain/blocks/importBlock.ts +13 -3
  169. package/src/chain/blocks/verifyBlock.ts +9 -3
  170. package/src/chain/blocks/verifyBlocksExecutionPayloads.ts +9 -2
  171. package/src/chain/blocks/verifyBlocksSignatures.ts +11 -3
  172. package/src/chain/blocks/verifyBlocksStateTransitionOnly.ts +1 -0
  173. package/src/chain/chain.ts +46 -46
  174. package/src/chain/forkChoice/index.ts +3 -2
  175. package/src/chain/initState.ts +5 -3
  176. package/src/chain/interface.ts +6 -8
  177. package/src/chain/lightClient/proofs.ts +0 -2
  178. package/src/chain/opPools/aggregatedAttestationPool.ts +19 -191
  179. package/src/chain/opPools/opPool.ts +5 -7
  180. package/src/chain/prepareNextSlot.ts +2 -6
  181. package/src/chain/produceBlock/produceBlockBody.ts +7 -2
  182. package/src/chain/regen/interface.ts +1 -5
  183. package/src/chain/regen/queued.ts +15 -34
  184. package/src/chain/regen/regen.ts +12 -18
  185. package/src/chain/shufflingCache.ts +67 -50
  186. package/src/chain/stateCache/blockStateCacheImpl.ts +2 -3
  187. package/src/chain/stateCache/fifoBlockStateCache.ts +4 -5
  188. package/src/chain/stateCache/inMemoryCheckpointsCache.ts +9 -15
  189. package/src/chain/stateCache/persistentCheckpointsCache.ts +17 -25
  190. package/src/chain/stateCache/types.ts +5 -10
  191. package/src/chain/validation/aggregateAndProof.ts +12 -0
  192. package/src/chain/validation/attestation.ts +3 -3
  193. package/src/chain/validation/attesterSlashing.ts +14 -2
  194. package/src/chain/validation/blobSidecar.ts +3 -3
  195. package/src/chain/validation/block.ts +6 -2
  196. package/src/chain/validation/blsToExecutionChange.ts +9 -7
  197. package/src/chain/validation/dataColumnSidecar.ts +3 -3
  198. package/src/chain/validation/proposerSlashing.ts +8 -2
  199. package/src/chain/validation/voluntaryExit.ts +1 -1
  200. package/src/db/repositories/checkpointState.ts +3 -19
  201. package/src/db/repositories/stateArchive.ts +13 -27
  202. package/src/execution/engine/mock.ts +40 -13
  203. package/src/index.ts +1 -1
  204. package/src/metrics/metrics/lodestar.ts +3 -17
  205. package/src/network/peers/discover.ts +3 -3
  206. package/src/network/reqresp/utils/dataColumnResponseValidation.ts +1 -1
  207. package/src/node/nodejs.ts +26 -4
  208. package/src/node/notifier.ts +13 -7
  209. package/src/sync/backfill/backfill.ts +2 -14
  210. package/src/sync/backfill/verify.ts +2 -9
  211. package/src/util/sszBytes.ts +1 -1
  212. package/lib/chain/rewards/attestationsRewards.d.ts +0 -8
  213. package/lib/chain/rewards/attestationsRewards.d.ts.map +0 -1
  214. package/lib/chain/rewards/attestationsRewards.js +0 -112
  215. package/lib/chain/rewards/attestationsRewards.js.map +0 -1
  216. package/lib/chain/rewards/blockRewards.d.ts +0 -15
  217. package/lib/chain/rewards/blockRewards.d.ts.map +0 -1
  218. package/lib/chain/rewards/blockRewards.js +0 -94
  219. package/lib/chain/rewards/blockRewards.js.map +0 -1
  220. package/lib/chain/rewards/syncCommitteeRewards.d.ts +0 -7
  221. package/lib/chain/rewards/syncCommitteeRewards.d.ts.map +0 -1
  222. package/lib/chain/rewards/syncCommitteeRewards.js +0 -36
  223. package/lib/chain/rewards/syncCommitteeRewards.js.map +0 -1
  224. package/src/chain/rewards/attestationsRewards.ts +0 -206
  225. package/src/chain/rewards/blockRewards.ts +0 -153
  226. package/src/chain/rewards/syncCommitteeRewards.ts +0 -60
@@ -8,7 +8,12 @@ import {
8
8
  ProtoBlock,
9
9
  } from "@lodestar/fork-choice";
10
10
  import {ForkSeq} from "@lodestar/params";
11
- import {CachedBeaconStateAllForks, isExecutionBlockBodyType, isExecutionStateType} from "@lodestar/state-transition";
11
+ import {
12
+ CachedBeaconStateAllForks,
13
+ isExecutionBlockBodyType,
14
+ isExecutionEnabled,
15
+ isExecutionStateType,
16
+ } from "@lodestar/state-transition";
12
17
  import {bellatrix, electra} from "@lodestar/types";
13
18
  import {ErrorAborted, Logger, toRootHex} from "@lodestar/utils";
14
19
  import {ExecutionPayloadStatus, IExecutionEngine} from "../../execution/engine/interface.js";
@@ -145,7 +150,9 @@ export async function verifyBlockExecutionPayload(
145
150
  const block = blockInput.getBlock();
146
151
  /** Not null if execution is enabled */
147
152
  const executionPayloadEnabled =
148
- isExecutionStateType(preState0) && isExecutionBlockBodyType(block.message.body)
153
+ isExecutionStateType(preState0) &&
154
+ isExecutionBlockBodyType(block.message.body) &&
155
+ isExecutionEnabled(preState0, block.message)
149
156
  ? block.message.body.executionPayload
150
157
  : null;
151
158
 
@@ -28,6 +28,7 @@ export async function verifyBlocksSignatures(
28
28
  ): Promise<{verifySignaturesTime: number}> {
29
29
  const isValidPromises: Promise<boolean>[] = [];
30
30
  const recvToValLatency = Date.now() / 1000 - (opts.seenTimestampSec ?? Date.now() / 1000);
31
+ const currentSyncCommitteeIndexed = preState0.epochCtx.currentSyncCommitteeIndexed;
31
32
 
32
33
  // Verifies signatures after running state transition, so all SyncCommittee signed roots are known at this point.
33
34
  // We must ensure block.slot <= state.slot before running getAllBlockSignatureSets().
@@ -41,9 +42,16 @@ export async function verifyBlocksSignatures(
41
42
  : //
42
43
  // Verify signatures per block to track which block is invalid
43
44
  bls.verifySignatureSets(
44
- getBlockSignatureSets(config, index2pubkey, preState0, block, indexedAttestationsByBlock[i], {
45
- skipProposerSignature: opts.validProposerSignature,
46
- })
45
+ getBlockSignatureSets(
46
+ config,
47
+ index2pubkey,
48
+ currentSyncCommitteeIndexed,
49
+ block,
50
+ indexedAttestationsByBlock[i],
51
+ {
52
+ skipProposerSignature: opts.validProposerSignature,
53
+ }
54
+ )
47
55
  );
48
56
 
49
57
  // getBlockSignatureSets() takes 45ms in benchmarks for 2022Q2 mainnet blocks (100 sigs). When syncing a 32 blocks
@@ -59,6 +59,7 @@ export async function verifyBlocksStateTransitionOnly(
59
59
  // if block is trusted don't verify proposer or op signature
60
60
  verifyProposer: !useBlsBatchVerify && !validSignatures && !validProposerSignature,
61
61
  verifySignatures: !useBlsBatchVerify && !validSignatures,
62
+ dontTransferCache: false,
62
63
  },
63
64
  {metrics, validatorMonitor}
64
65
  );
@@ -14,13 +14,14 @@ import {
14
14
  EpochShuffling,
15
15
  Index2PubkeyCache,
16
16
  computeAnchorCheckpoint,
17
+ computeAttestationsRewards,
18
+ computeBlockRewards,
17
19
  computeEndSlotAtEpoch,
18
20
  computeEpochAtSlot,
19
21
  computeStartSlotAtEpoch,
20
- createCachedBeaconState,
22
+ computeSyncCommitteeRewards,
21
23
  getEffectiveBalanceIncrementsZeroInactive,
22
24
  getEffectiveBalancesFromStateBytes,
23
- isCachedBeaconState,
24
25
  processSlots,
25
26
  } from "@lodestar/state-transition";
26
27
  import {
@@ -38,6 +39,7 @@ import {
38
39
  Wei,
39
40
  isBlindedBeaconBlock,
40
41
  phase0,
42
+ rewards,
41
43
  } from "@lodestar/types";
42
44
  import {Logger, fromHex, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toRootHex} from "@lodestar/utils";
43
45
  import {ProcessShutdownCallback} from "@lodestar/validator";
@@ -50,6 +52,7 @@ import {computeNodeIdFromPrivateKey} from "../network/subnets/interface.js";
50
52
  import {BufferPool} from "../util/bufferPool.js";
51
53
  import {Clock, ClockEvent, IClock} from "../util/clock.js";
52
54
  import {CustodyConfig, getValidatorsCustodyRequirement} from "../util/dataColumns.js";
55
+ import {callInNextEventLoop} from "../util/eventLoop.js";
53
56
  import {ensureDir, writeIfNotExist} from "../util/file.js";
54
57
  import {isOptimisticBlock} from "../util/forkChoice.js";
55
58
  import {SerializedCache} from "../util/serializedCache.js";
@@ -79,9 +82,6 @@ import {AssembledBlockType, BlockType, ProduceResult} from "./produceBlock/index
79
82
  import {BlockAttributes, produceBlockBody, produceCommonBlockBody} from "./produceBlock/produceBlockBody.js";
80
83
  import {QueuedStateRegenerator, RegenCaller} from "./regen/index.js";
81
84
  import {ReprocessController} from "./reprocess.js";
82
- import {AttestationsRewards, computeAttestationsRewards} from "./rewards/attestationsRewards.js";
83
- import {BlockRewards, computeBlockRewards} from "./rewards/blockRewards.js";
84
- import {SyncCommitteeRewards, computeSyncCommitteeRewards} from "./rewards/syncCommitteeRewards.js";
85
85
  import {
86
86
  SeenAggregators,
87
87
  SeenAttesters,
@@ -204,6 +204,8 @@ export class BeaconChain implements IBeaconChain {
204
204
  {
205
205
  privateKey,
206
206
  config,
207
+ pubkey2index,
208
+ index2pubkey,
207
209
  db,
208
210
  dbName,
209
211
  dataDir,
@@ -219,6 +221,8 @@ export class BeaconChain implements IBeaconChain {
219
221
  }: {
220
222
  privateKey: PrivateKey;
221
223
  config: BeaconConfig;
224
+ pubkey2index: PubkeyIndexMap;
225
+ index2pubkey: Index2PubkeyCache;
222
226
  db: IBeaconDb;
223
227
  dbName: string;
224
228
  dataDir: string;
@@ -228,7 +232,7 @@ export class BeaconChain implements IBeaconChain {
228
232
  clock?: IClock;
229
233
  metrics: Metrics | null;
230
234
  validatorMonitor: ValidatorMonitor | null;
231
- anchorState: BeaconStateAllForks;
235
+ anchorState: CachedBeaconStateAllForks;
232
236
  isAnchorStateFinalized: boolean;
233
237
  executionEngine: IExecutionEngine;
234
238
  executionBuilder?: IExecutionBuilder;
@@ -287,39 +291,26 @@ export class BeaconChain implements IBeaconChain {
287
291
  logger,
288
292
  });
289
293
 
290
- // Restore state caches
291
- // anchorState may already by a CachedBeaconState. If so, don't create the cache again, since deserializing all
292
- // pubkeys takes ~30 seconds for 350k keys (mainnet 2022Q2).
293
- // When the BeaconStateCache is created in initializeBeaconStateFromEth1 it may be incorrect. Until we can ensure that
294
- // it's safe to re-use _ANY_ BeaconStateCache, this option is disabled by default and only used in tests.
295
- const cachedState =
296
- isCachedBeaconState(anchorState) && opts.skipCreateStateCacheIfAvailable
297
- ? anchorState
298
- : createCachedBeaconState(anchorState, {
299
- config,
300
- pubkey2index: new PubkeyIndexMap(),
301
- index2pubkey: [],
302
- });
303
- this._earliestAvailableSlot = cachedState.slot;
304
-
305
- this.shufflingCache = cachedState.epochCtx.shufflingCache = new ShufflingCache(metrics, logger, this.opts, [
294
+ this._earliestAvailableSlot = anchorState.slot;
295
+
296
+ this.shufflingCache = new ShufflingCache(metrics, logger, this.opts, [
306
297
  {
307
- shuffling: cachedState.epochCtx.previousShuffling,
308
- decisionRoot: cachedState.epochCtx.previousDecisionRoot,
298
+ shuffling: anchorState.epochCtx.previousShuffling,
299
+ decisionRoot: anchorState.epochCtx.previousDecisionRoot,
309
300
  },
310
301
  {
311
- shuffling: cachedState.epochCtx.currentShuffling,
312
- decisionRoot: cachedState.epochCtx.currentDecisionRoot,
302
+ shuffling: anchorState.epochCtx.currentShuffling,
303
+ decisionRoot: anchorState.epochCtx.currentDecisionRoot,
313
304
  },
314
305
  {
315
- shuffling: cachedState.epochCtx.nextShuffling,
316
- decisionRoot: cachedState.epochCtx.nextDecisionRoot,
306
+ shuffling: anchorState.epochCtx.nextShuffling,
307
+ decisionRoot: anchorState.epochCtx.nextDecisionRoot,
317
308
  },
318
309
  ]);
319
310
 
320
- // Persist single global instance of state caches
321
- this.pubkey2index = cachedState.epochCtx.pubkey2index;
322
- this.index2pubkey = cachedState.epochCtx.index2pubkey;
311
+ // Global cache of validators pubkey/index mapping
312
+ this.pubkey2index = pubkey2index;
313
+ this.index2pubkey = index2pubkey;
323
314
 
324
315
  const fileDataStore = opts.nHistoricalStatesFileDataStore ?? true;
325
316
  const blockStateCache = this.opts.nHistoricalStates
@@ -350,15 +341,15 @@ export class BeaconChain implements IBeaconChain {
350
341
  }
351
342
 
352
343
  const {checkpoint} = computeAnchorCheckpoint(config, anchorState);
353
- blockStateCache.add(cachedState);
354
- blockStateCache.setHeadState(cachedState);
355
- checkpointStateCache.add(checkpoint, cachedState);
344
+ blockStateCache.add(anchorState);
345
+ blockStateCache.setHeadState(anchorState);
346
+ checkpointStateCache.add(checkpoint, anchorState);
356
347
 
357
348
  const forkChoice = initializeForkChoice(
358
349
  config,
359
350
  emitter,
360
351
  clock.currentSlot,
361
- cachedState,
352
+ anchorState,
362
353
  isAnchorStateFinalized,
363
354
  opts,
364
355
  this.justifiedBalancesGetter.bind(this),
@@ -428,6 +419,7 @@ export class BeaconChain implements IBeaconChain {
428
419
  clock.addListener(ClockEvent.epoch, this.onClockEpoch.bind(this));
429
420
  emitter.addListener(ChainEvent.forkChoiceFinalized, this.onForkChoiceFinalized.bind(this));
430
421
  emitter.addListener(ChainEvent.forkChoiceJustified, this.onForkChoiceJustified.bind(this));
422
+ emitter.addListener(ChainEvent.checkpoint, this.onCheckpoint.bind(this));
431
423
  }
432
424
 
433
425
  async init(): Promise<void> {
@@ -515,7 +507,7 @@ export class BeaconChain implements IBeaconChain {
515
507
  async getStateBySlot(
516
508
  slot: Slot,
517
509
  opts?: StateGetOpts
518
- ): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null> {
510
+ ): Promise<{state: CachedBeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null> {
519
511
  const finalizedBlock = this.forkChoice.getFinalizedBlock();
520
512
 
521
513
  if (slot < finalizedBlock.slot) {
@@ -570,7 +562,7 @@ export class BeaconChain implements IBeaconChain {
570
562
  async getStateByStateRoot(
571
563
  stateRoot: RootHex,
572
564
  opts?: StateGetOpts
573
- ): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null> {
565
+ ): Promise<{state: CachedBeaconStateAllForks | Uint8Array; executionOptimistic: boolean; finalized: boolean} | null> {
574
566
  if (opts?.allowRegen) {
575
567
  const state = await this.regen.getState(stateRoot, RegenCaller.restApi);
576
568
  const block = this.forkChoice.getBlock(state.latestBlockHeader.hashTreeRoot());
@@ -598,7 +590,8 @@ export class BeaconChain implements IBeaconChain {
598
590
  };
599
591
  }
600
592
 
601
- const data = await this.db.stateArchive.getByRoot(fromHex(stateRoot));
593
+ // this is mostly useful for a node with `--chain.archiveStateEpochFrequency 1`
594
+ const data = await this.db.stateArchive.getBinaryByRoot(fromHex(stateRoot));
602
595
  return data && {state: data, executionOptimistic: false, finalized: true};
603
596
  }
604
597
 
@@ -991,8 +984,8 @@ export class BeaconChain implements IBeaconChain {
991
984
  this.metrics?.gossipAttestation.useHeadBlockState.inc({caller: regenCaller});
992
985
  state = await this.regen.getState(attHeadBlock.stateRoot, regenCaller);
993
986
  }
994
-
995
- // should always be the current epoch of the active context so no need to await a result from the ShufflingCache
987
+ // resolve the promise to unblock other calls of the same epoch and dependent root
988
+ this.shufflingCache.processState(state);
996
989
  return state.epochCtx.getShufflingAtEpoch(attEpoch);
997
990
  }
998
991
 
@@ -1176,6 +1169,13 @@ export class BeaconChain implements IBeaconChain {
1176
1169
  this.logger.verbose("Fork choice justified", {epoch: cp.epoch, root: cp.rootHex});
1177
1170
  }
1178
1171
 
1172
+ private onCheckpoint(this: BeaconChain, _checkpoint: phase0.Checkpoint, state: CachedBeaconStateAllForks): void {
1173
+ // Defer to not block other checkpoint event handlers, which can cause lightclient update delays
1174
+ callInNextEventLoop(() => {
1175
+ this.shufflingCache.processState(state);
1176
+ });
1177
+ }
1178
+
1179
1179
  private async onForkChoiceFinalized(this: BeaconChain, cp: CheckpointWithHex): Promise<void> {
1180
1180
  this.logger.verbose("Fork choice finalized", {epoch: cp.epoch, root: cp.rootHex});
1181
1181
  this.seenBlockProposers.prune(computeStartSlotAtEpoch(cp.epoch));
@@ -1297,7 +1297,7 @@ export class BeaconChain implements IBeaconChain {
1297
1297
  }
1298
1298
  }
1299
1299
 
1300
- async getBlockRewards(block: BeaconBlock | BlindedBeaconBlock): Promise<BlockRewards> {
1300
+ async getBlockRewards(block: BeaconBlock | BlindedBeaconBlock): Promise<rewards.BlockRewards> {
1301
1301
  let preState = this.regen.getPreStateSync(block);
1302
1302
 
1303
1303
  if (preState === null) {
@@ -1306,15 +1306,15 @@ export class BeaconChain implements IBeaconChain {
1306
1306
 
1307
1307
  preState = processSlots(preState, block.slot); // Dial preState's slot to block.slot
1308
1308
 
1309
- const postState = this.regen.getStateSync(toRootHex(block.stateRoot)) ?? undefined;
1309
+ const proposerRewards = this.regen.getStateSync(toRootHex(block.stateRoot))?.proposerRewards ?? undefined;
1310
1310
 
1311
- return computeBlockRewards(this.config, block, preState.clone(), postState?.clone());
1311
+ return computeBlockRewards(this.config, block, preState, proposerRewards);
1312
1312
  }
1313
1313
 
1314
1314
  async getAttestationsRewards(
1315
1315
  epoch: Epoch,
1316
1316
  validatorIds?: (ValidatorIndex | string)[]
1317
- ): Promise<{rewards: AttestationsRewards; executionOptimistic: boolean; finalized: boolean}> {
1317
+ ): Promise<{rewards: rewards.AttestationsRewards; executionOptimistic: boolean; finalized: boolean}> {
1318
1318
  // We use end slot of (epoch + 1) to ensure we have seen all attestations. On-time or late. Any late attestation beyond this slot is not considered
1319
1319
  const slot = computeEndSlotAtEpoch(epoch + 1);
1320
1320
  const stateResult = await this.getStateBySlot(slot, {allowRegen: false}); // No regen if state not in cache
@@ -1340,7 +1340,7 @@ export class BeaconChain implements IBeaconChain {
1340
1340
  async getSyncCommitteeRewards(
1341
1341
  block: BeaconBlock | BlindedBeaconBlock,
1342
1342
  validatorIds?: (ValidatorIndex | string)[]
1343
- ): Promise<SyncCommitteeRewards> {
1343
+ ): Promise<rewards.SyncCommitteeRewards> {
1344
1344
  let preState = this.regen.getPreStateSync(block);
1345
1345
 
1346
1346
  if (preState === null) {
@@ -1349,6 +1349,6 @@ export class BeaconChain implements IBeaconChain {
1349
1349
 
1350
1350
  preState = processSlots(preState, block.slot); // Dial preState's slot to block.slot
1351
1351
 
1352
- return computeSyncCommitteeRewards(this.config, this.index2pubkey, block, preState.clone(), validatorIds);
1352
+ return computeSyncCommitteeRewards(this.config, this.index2pubkey, block, preState, validatorIds);
1353
1353
  }
1354
1354
  }
@@ -18,6 +18,7 @@ import {
18
18
  getBlockRootAtSlot,
19
19
  getEffectiveBalanceIncrementsZeroInactive,
20
20
  isExecutionStateType,
21
+ isMergeTransitionComplete,
21
22
  } from "@lodestar/state-transition";
22
23
  import {Slot, ssz} from "@lodestar/types";
23
24
  import {Logger, toRootHex} from "@lodestar/utils";
@@ -134,7 +135,7 @@ export function initializeForkChoiceFromFinalizedState(
134
135
  unrealizedFinalizedEpoch: finalizedCheckpoint.epoch,
135
136
  unrealizedFinalizedRoot: toRootHex(finalizedCheckpoint.root),
136
137
 
137
- ...(isExecutionStateType(state)
138
+ ...(isExecutionStateType(state) && isMergeTransitionComplete(state)
138
139
  ? {
139
140
  executionPayloadBlockHash: toRootHex(state.latestExecutionPayloadHeader.blockHash),
140
141
  executionPayloadNumber: state.latestExecutionPayloadHeader.blockNumber,
@@ -215,7 +216,7 @@ export function initializeForkChoiceFromUnfinalizedState(
215
216
  unrealizedFinalizedEpoch: finalizedCheckpoint.epoch,
216
217
  unrealizedFinalizedRoot: toRootHex(finalizedCheckpoint.root),
217
218
 
218
- ...(isExecutionStateType(unfinalizedState)
219
+ ...(isExecutionStateType(unfinalizedState) && isMergeTransitionComplete(unfinalizedState)
219
220
  ? {
220
221
  executionPayloadBlockHash: toRootHex(unfinalizedState.latestExecutionPayloadHeader.blockHash),
221
222
  executionPayloadNumber: unfinalizedState.latestExecutionPayloadHeader.blockNumber,
@@ -6,6 +6,7 @@ import {Logger, toHex, toRootHex} from "@lodestar/utils";
6
6
  import {GENESIS_SLOT} from "../constants/index.js";
7
7
  import {IBeaconDb} from "../db/index.js";
8
8
  import {Metrics} from "../metrics/index.js";
9
+ import {getStateTypeFromBytes} from "../util/multifork.js";
9
10
 
10
11
  export async function persistAnchorState(
11
12
  config: ChainForkConfig,
@@ -53,14 +54,15 @@ export function createGenesisBlock(config: ChainForkConfig, genesisState: Beacon
53
54
  * Restore the latest beacon state from db
54
55
  */
55
56
  export async function initStateFromDb(
56
- _config: ChainForkConfig,
57
+ config: ChainForkConfig,
57
58
  db: IBeaconDb,
58
59
  logger: Logger
59
60
  ): Promise<BeaconStateAllForks> {
60
- const state = await db.stateArchive.lastValue();
61
- if (!state) {
61
+ const stateBytes = await db.stateArchive.lastBinary();
62
+ if (stateBytes == null) {
62
63
  throw new Error("No state exists in database");
63
64
  }
65
+ const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
64
66
 
65
67
  logger.info("Initializing beacon state from db", {
66
68
  slot: state.slot,
@@ -23,6 +23,7 @@ import {
23
23
  altair,
24
24
  capella,
25
25
  phase0,
26
+ rewards,
26
27
  } from "@lodestar/types";
27
28
  import {Logger} from "@lodestar/utils";
28
29
  import {IExecutionBuilder, IExecutionEngine} from "../execution/index.js";
@@ -48,9 +49,6 @@ import {IChainOptions} from "./options.js";
48
49
  import {AssembledBlockType, BlockAttributes, BlockType, ProduceResult} from "./produceBlock/produceBlockBody.js";
49
50
  import {IStateRegenerator, RegenCaller} from "./regen/index.js";
50
51
  import {ReprocessController} from "./reprocess.js";
51
- import {AttestationsRewards} from "./rewards/attestationsRewards.js";
52
- import {BlockRewards} from "./rewards/blockRewards.js";
53
- import {SyncCommitteeRewards} from "./rewards/syncCommitteeRewards.js";
54
52
  import {
55
53
  SeenAggregators,
56
54
  SeenAttesters,
@@ -170,12 +168,12 @@ export interface IBeaconChain {
170
168
  getStateBySlot(
171
169
  slot: Slot,
172
170
  opts?: StateGetOpts
173
- ): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null>;
171
+ ): Promise<{state: CachedBeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null>;
174
172
  /** Returns a local state by state root */
175
173
  getStateByStateRoot(
176
174
  stateRoot: RootHex,
177
175
  opts?: StateGetOpts
178
- ): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null>;
176
+ ): Promise<{state: CachedBeaconStateAllForks | Uint8Array; executionOptimistic: boolean; finalized: boolean} | null>;
179
177
  /** Return serialized bytes of a persisted checkpoint state */
180
178
  getPersistedCheckpointState(checkpoint?: phase0.Checkpoint): Promise<Uint8Array | null>;
181
179
  /** Returns a cached state by checkpoint */
@@ -255,15 +253,15 @@ export interface IBeaconChain {
255
253
  regenCanAcceptWork(): boolean;
256
254
  blsThreadPoolCanAcceptWork(): boolean;
257
255
 
258
- getBlockRewards(blockRef: BeaconBlock | BlindedBeaconBlock): Promise<BlockRewards>;
256
+ getBlockRewards(blockRef: BeaconBlock | BlindedBeaconBlock): Promise<rewards.BlockRewards>;
259
257
  getAttestationsRewards(
260
258
  epoch: Epoch,
261
259
  validatorIds?: (ValidatorIndex | string)[]
262
- ): Promise<{rewards: AttestationsRewards; executionOptimistic: boolean; finalized: boolean}>;
260
+ ): Promise<{rewards: rewards.AttestationsRewards; executionOptimistic: boolean; finalized: boolean}>;
263
261
  getSyncCommitteeRewards(
264
262
  blockRef: BeaconBlock | BlindedBeaconBlock,
265
263
  validatorIds?: (ValidatorIndex | string)[]
266
- ): Promise<SyncCommitteeRewards>;
264
+ ): Promise<rewards.SyncCommitteeRewards>;
267
265
  }
268
266
 
269
267
  export type SSZObjectType =
@@ -12,7 +12,6 @@ import {BeaconBlockBody, SSZTypesFor, ssz} from "@lodestar/types";
12
12
  import {SyncCommitteeWitness} from "./types.js";
13
13
 
14
14
  export function getSyncCommitteesWitness(fork: ForkName, state: BeaconStateAllForks): SyncCommitteeWitness {
15
- state.commit();
16
15
  const n1 = state.node;
17
16
  let witness: Uint8Array[];
18
17
  let currentSyncCommitteeRoot: Uint8Array;
@@ -71,7 +70,6 @@ export function getCurrentSyncCommitteeBranch(syncCommitteesWitness: SyncCommitt
71
70
  }
72
71
 
73
72
  export function getFinalizedRootProof(state: CachedBeaconStateAllForks): Uint8Array[] {
74
- state.commit();
75
73
  const finalizedRootGindex = state.epochCtx.isPostElectra() ? FINALIZED_ROOT_GINDEX_ELECTRA : FINALIZED_ROOT_GINDEX;
76
74
  return new Tree(state.node).getSingleProof(BigInt(finalizedRootGindex));
77
75
  }