@lodestar/beacon-node 1.50.0-dev.c02b971f5b → 1.50.0-dev.c24762bc60

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 (162) hide show
  1. package/lib/api/impl/beacon/state/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/state/index.js +6 -4
  3. package/lib/api/impl/beacon/state/index.js.map +1 -1
  4. package/lib/api/impl/validator/index.d.ts.map +1 -1
  5. package/lib/api/impl/validator/index.js +26 -0
  6. package/lib/api/impl/validator/index.js.map +1 -1
  7. package/lib/chain/beaconProposerCache.d.ts +6 -0
  8. package/lib/chain/beaconProposerCache.d.ts.map +1 -1
  9. package/lib/chain/beaconProposerCache.js +16 -0
  10. package/lib/chain/beaconProposerCache.js.map +1 -1
  11. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  12. package/lib/chain/blocks/importBlock.js +7 -5
  13. package/lib/chain/blocks/importBlock.js.map +1 -1
  14. package/lib/chain/blocks/verifyBlock.d.ts.map +1 -1
  15. package/lib/chain/blocks/verifyBlock.js +6 -6
  16. package/lib/chain/blocks/verifyBlock.js.map +1 -1
  17. package/lib/chain/chain.d.ts +1 -0
  18. package/lib/chain/chain.d.ts.map +1 -1
  19. package/lib/chain/chain.js +49 -15
  20. package/lib/chain/chain.js.map +1 -1
  21. package/lib/chain/interface.d.ts +2 -0
  22. package/lib/chain/interface.d.ts.map +1 -1
  23. package/lib/chain/opPools/opPool.d.ts.map +1 -1
  24. package/lib/chain/opPools/opPool.js +1 -23
  25. package/lib/chain/opPools/opPool.js.map +1 -1
  26. package/lib/chain/opPools/proposerPreferencesPool.d.ts +4 -0
  27. package/lib/chain/opPools/proposerPreferencesPool.d.ts.map +1 -1
  28. package/lib/chain/opPools/proposerPreferencesPool.js +13 -1
  29. package/lib/chain/opPools/proposerPreferencesPool.js.map +1 -1
  30. package/lib/chain/opPools/utils.d.ts +11 -0
  31. package/lib/chain/opPools/utils.d.ts.map +1 -1
  32. package/lib/chain/opPools/utils.js +22 -0
  33. package/lib/chain/opPools/utils.js.map +1 -1
  34. package/lib/chain/prepareNextSlot.d.ts.map +1 -1
  35. package/lib/chain/prepareNextSlot.js +11 -3
  36. package/lib/chain/prepareNextSlot.js.map +1 -1
  37. package/lib/chain/produceBlock/produceBlockBody.d.ts +8 -0
  38. package/lib/chain/produceBlock/produceBlockBody.d.ts.map +1 -1
  39. package/lib/chain/produceBlock/produceBlockBody.js +40 -28
  40. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  41. package/lib/chain/shufflingCache.d.ts +17 -8
  42. package/lib/chain/shufflingCache.d.ts.map +1 -1
  43. package/lib/chain/shufflingCache.js +68 -18
  44. package/lib/chain/shufflingCache.js.map +1 -1
  45. package/lib/db/beacon.d.ts +2 -1
  46. package/lib/db/beacon.d.ts.map +1 -1
  47. package/lib/db/beacon.js +3 -1
  48. package/lib/db/beacon.js.map +1 -1
  49. package/lib/db/buckets.d.ts +2 -1
  50. package/lib/db/buckets.d.ts.map +1 -1
  51. package/lib/db/buckets.js +1 -0
  52. package/lib/db/buckets.js.map +1 -1
  53. package/lib/db/interface.d.ts +2 -1
  54. package/lib/db/interface.d.ts.map +1 -1
  55. package/lib/db/repositories/blockArchiveIndex.d.ts +2 -2
  56. package/lib/db/repositories/blockArchiveIndex.d.ts.map +1 -1
  57. package/lib/db/repositories/index.d.ts +1 -0
  58. package/lib/db/repositories/index.d.ts.map +1 -1
  59. package/lib/db/repositories/index.js +1 -0
  60. package/lib/db/repositories/index.js.map +1 -1
  61. package/lib/db/repositories/proposerPreferences.d.ts +14 -0
  62. package/lib/db/repositories/proposerPreferences.d.ts.map +1 -0
  63. package/lib/db/repositories/proposerPreferences.js +21 -0
  64. package/lib/db/repositories/proposerPreferences.js.map +1 -0
  65. package/lib/execution/engine/blobVersionedHashes.d.ts +9 -0
  66. package/lib/execution/engine/blobVersionedHashes.d.ts.map +1 -0
  67. package/lib/execution/engine/blobVersionedHashes.js +74 -0
  68. package/lib/execution/engine/blobVersionedHashes.js.map +1 -0
  69. package/lib/execution/engine/http.d.ts +45 -13
  70. package/lib/execution/engine/http.d.ts.map +1 -1
  71. package/lib/execution/engine/http.js +236 -198
  72. package/lib/execution/engine/http.js.map +1 -1
  73. package/lib/execution/engine/index.d.ts +3 -2
  74. package/lib/execution/engine/index.d.ts.map +1 -1
  75. package/lib/execution/engine/index.js +33 -8
  76. package/lib/execution/engine/index.js.map +1 -1
  77. package/lib/execution/engine/interface.d.ts +1 -1
  78. package/lib/execution/engine/interface.d.ts.map +1 -1
  79. package/lib/execution/engine/jsonRpcTransport.d.ts +25 -0
  80. package/lib/execution/engine/jsonRpcTransport.d.ts.map +1 -0
  81. package/lib/execution/engine/jsonRpcTransport.js +158 -0
  82. package/lib/execution/engine/jsonRpcTransport.js.map +1 -0
  83. package/lib/execution/engine/restHttpClient.d.ts +71 -0
  84. package/lib/execution/engine/restHttpClient.d.ts.map +1 -0
  85. package/lib/execution/engine/restHttpClient.js +201 -0
  86. package/lib/execution/engine/restHttpClient.js.map +1 -0
  87. package/lib/execution/engine/restTransport.d.ts +46 -0
  88. package/lib/execution/engine/restTransport.d.ts.map +1 -0
  89. package/lib/execution/engine/restTransport.js +399 -0
  90. package/lib/execution/engine/restTransport.js.map +1 -0
  91. package/lib/execution/engine/sszTypes.d.ts +1261 -0
  92. package/lib/execution/engine/sszTypes.d.ts.map +1 -0
  93. package/lib/execution/engine/sszTypes.js +216 -0
  94. package/lib/execution/engine/sszTypes.js.map +1 -0
  95. package/lib/execution/engine/transport.d.ts +39 -0
  96. package/lib/execution/engine/transport.d.ts.map +1 -0
  97. package/lib/execution/engine/transport.js +2 -0
  98. package/lib/execution/engine/transport.js.map +1 -0
  99. package/lib/execution/engine/types.d.ts +2 -0
  100. package/lib/execution/engine/types.d.ts.map +1 -1
  101. package/lib/execution/engine/types.js +24 -57
  102. package/lib/execution/engine/types.js.map +1 -1
  103. package/lib/execution/engine/utils.d.ts.map +1 -1
  104. package/lib/execution/engine/utils.js +9 -2
  105. package/lib/execution/engine/utils.js.map +1 -1
  106. package/lib/metrics/metrics/lodestar.d.ts +9 -0
  107. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  108. package/lib/metrics/metrics/lodestar.js +18 -0
  109. package/lib/metrics/metrics/lodestar.js.map +1 -1
  110. package/lib/network/core/events.d.ts +5 -1
  111. package/lib/network/core/events.d.ts.map +1 -1
  112. package/lib/network/core/events.js +8 -0
  113. package/lib/network/core/events.js.map +1 -1
  114. package/lib/network/processor/gossipHandlers.js +1 -1
  115. package/lib/network/processor/gossipHandlers.js.map +1 -1
  116. package/lib/node/options.d.ts +3 -2
  117. package/lib/node/options.d.ts.map +1 -1
  118. package/lib/node/options.js +2 -2
  119. package/lib/node/options.js.map +1 -1
  120. package/lib/util/asyncIterableToEvents.d.ts +10 -2
  121. package/lib/util/asyncIterableToEvents.d.ts.map +1 -1
  122. package/lib/util/asyncIterableToEvents.js +29 -3
  123. package/lib/util/asyncIterableToEvents.js.map +1 -1
  124. package/lib/util/metadata.d.ts.map +1 -1
  125. package/lib/util/metadata.js +2 -1
  126. package/lib/util/metadata.js.map +1 -1
  127. package/package.json +16 -15
  128. package/src/api/impl/beacon/state/index.ts +13 -4
  129. package/src/api/impl/validator/index.ts +37 -0
  130. package/src/chain/beaconProposerCache.ts +19 -1
  131. package/src/chain/blocks/importBlock.ts +8 -4
  132. package/src/chain/blocks/verifyBlock.ts +7 -6
  133. package/src/chain/chain.ts +59 -15
  134. package/src/chain/interface.ts +2 -0
  135. package/src/chain/opPools/opPool.ts +1 -31
  136. package/src/chain/opPools/proposerPreferencesPool.ts +20 -1
  137. package/src/chain/opPools/utils.ts +31 -0
  138. package/src/chain/prepareNextSlot.ts +15 -3
  139. package/src/chain/produceBlock/produceBlockBody.ts +68 -34
  140. package/src/chain/shufflingCache.ts +79 -22
  141. package/src/db/beacon.ts +3 -0
  142. package/src/db/buckets.ts +1 -0
  143. package/src/db/interface.ts +2 -0
  144. package/src/db/repositories/index.ts +1 -0
  145. package/src/db/repositories/proposerPreferences.ts +23 -0
  146. package/src/execution/engine/blobVersionedHashes.ts +72 -0
  147. package/src/execution/engine/http.ts +304 -271
  148. package/src/execution/engine/index.ts +41 -7
  149. package/src/execution/engine/interface.ts +1 -1
  150. package/src/execution/engine/jsonRpcTransport.ts +247 -0
  151. package/src/execution/engine/restHttpClient.ts +269 -0
  152. package/src/execution/engine/restTransport.ts +544 -0
  153. package/src/execution/engine/sszTypes.ts +354 -0
  154. package/src/execution/engine/transport.ts +61 -0
  155. package/src/execution/engine/types.ts +34 -74
  156. package/src/execution/engine/utils.ts +12 -2
  157. package/src/metrics/metrics/lodestar.ts +19 -0
  158. package/src/network/core/events.ts +16 -1
  159. package/src/network/processor/gossipHandlers.ts +1 -1
  160. package/src/node/options.ts +10 -1
  161. package/src/util/asyncIterableToEvents.ts +44 -5
  162. package/src/util/metadata.ts +2 -1
@@ -6,7 +6,7 @@ const PROPOSER_PRESERVE_EPOCHS = 2;
6
6
  export type ProposerPreparationData = routes.validator.ProposerPreparationData;
7
7
 
8
8
  export class BeaconProposerCache {
9
- private readonly feeRecipientByValidatorIndex: Map<number, {epoch: Epoch; feeRecipient: string}>;
9
+ private readonly feeRecipientByValidatorIndex: Map<number, {epoch: Epoch; feeRecipient?: string}>;
10
10
  constructor(readonly opts: {suggestedFeeRecipient: string}) {
11
11
  this.feeRecipientByValidatorIndex = new Map();
12
12
  }
@@ -15,6 +15,24 @@ export class BeaconProposerCache {
15
15
  this.feeRecipientByValidatorIndex.set(validatorIndex, {epoch, feeRecipient});
16
16
  }
17
17
 
18
+ /**
19
+ * Track a validator that uses this node for duties, keeping the fee recipient it may have
20
+ * registered. Returns true if the validator was not tracked before.
21
+ */
22
+ track(epoch: Epoch, validatorIndex: number): boolean {
23
+ const entry = this.feeRecipientByValidatorIndex.get(validatorIndex);
24
+ if (entry !== undefined) {
25
+ entry.epoch = epoch;
26
+ return false;
27
+ }
28
+ this.feeRecipientByValidatorIndex.set(validatorIndex, {epoch});
29
+ return true;
30
+ }
31
+
32
+ has(validatorIndex: number): boolean {
33
+ return this.feeRecipientByValidatorIndex.has(validatorIndex);
34
+ }
35
+
18
36
  prune(epoch: Epoch): void {
19
37
  // This is not so optimized function, but could maintain a 2d array may be?
20
38
  for (const [validatorIndex, feeRecipientEntry] of this.feeRecipientByValidatorIndex.entries()) {
@@ -22,6 +22,7 @@ import {
22
22
  RootCache,
23
23
  computeEpochAtSlot,
24
24
  computeTimeAtSlot,
25
+ getBeaconCommittees,
25
26
  isStatePostAltair,
26
27
  isStatePostBellatrix,
27
28
  } from "@lodestar/state-transition";
@@ -406,9 +407,8 @@ export async function importBlock(
406
407
  const proposalSlot = blockSlot + 1;
407
408
  try {
408
409
  const proposerIndex = postState.getBeaconProposer(proposalSlot);
409
- const feeRecipient = this.beaconProposerCache.get(proposerIndex);
410
410
 
411
- if (feeRecipient) {
411
+ if (this.beaconProposerCache.has(proposerIndex)) {
412
412
  // We would set this to true if
413
413
  // 1) This is a gossip block
414
414
  // 2) We are proposer of next slot
@@ -663,8 +663,12 @@ export function addAttestationPostElectra(
663
663
  } else {
664
664
  const attSlot = attestation.data.slot;
665
665
  const attEpoch = computeEpochAtSlot(attSlot);
666
- const decisionRoot = state.getShufflingDecisionRoot(attEpoch);
667
- const committees = this.shufflingCache.getBeaconCommittees(attEpoch, decisionRoot, attSlot, committeeIndices);
666
+ // calling getShufflingAtEpoch may take some time for NativeBeaconStateView
667
+ // we should have the shuffling inside ShufflingCache most of the time
668
+ const shuffling =
669
+ this.shufflingCache.getSync(attEpoch, state.getShufflingDecisionRoot(attEpoch)) ??
670
+ state.getShufflingAtEpoch(attEpoch);
671
+ const committees = getBeaconCommittees(shuffling, attSlot, committeeIndices);
668
672
  const aggregationBools = attestation.aggregationBits.toBoolArray();
669
673
  let offset = 0;
670
674
  for (let i = 0; i < committees.length; i++) {
@@ -4,6 +4,7 @@ import {
4
4
  DataAvailabilityStatus,
5
5
  IBeaconStateView,
6
6
  computeEpochAtSlot,
7
+ getIndexedAttestation,
7
8
  signedBlockToSignedHeader,
8
9
  } from "@lodestar/state-transition";
9
10
  import {IndexedAttestation, Slot, deneb} from "@lodestar/types";
@@ -81,10 +82,6 @@ export async function verifyBlocksInEpoch(
81
82
  throw new BlockError(block0, {code: BlockErrorCode.PRESTATE_MISSING, error: e as Error});
82
83
  });
83
84
 
84
- // in forky condition, make sure to populate ShufflingCache with regened state
85
- // otherwise it may fail to get indexed attestations from shuffling cache later
86
- this.shufflingCache.processState(preState0);
87
-
88
85
  if (!preState0.isStateValidatorsNodesPopulated()) {
89
86
  this.logger.verbose("verifyBlocksInEpoch preState0 SSZ cache stats", {
90
87
  slot: preState0.slot,
@@ -117,8 +114,12 @@ export async function verifyBlocksInEpoch(
117
114
  for (const [i, block] of blocks.entries()) {
118
115
  indexedAttestationsByBlock[i] = block.message.body.attestations.map((attestation) => {
119
116
  const attEpoch = computeEpochAtSlot(attestation.data.slot);
120
- const decisionRoot = preState0.getShufflingDecisionRoot(attEpoch);
121
- return this.shufflingCache.getIndexedAttestation(attEpoch, decisionRoot, fork, attestation);
117
+ // calling getShufflingAtEpoch may take some time for NativeBeaconStateView
118
+ // we should have the shuffling inside ShufflingCache most of the time
119
+ const shuffling =
120
+ this.shufflingCache.getSync(attEpoch, preState0.getShufflingDecisionRoot(attEpoch)) ??
121
+ preState0.getShufflingAtEpoch(attEpoch);
122
+ return getIndexedAttestation(shuffling, fork, attestation);
122
123
  });
123
124
  }
124
125
 
@@ -128,7 +128,7 @@ import {SeenAggregatedAttestations} from "./seenCache/seenAggregateAndProof.js";
128
128
  import {SeenAttestationDatas} from "./seenCache/seenAttestationData.js";
129
129
  import {SeenBlockAttesters} from "./seenCache/seenBlockAttesters.js";
130
130
  import {SeenBlockInput} from "./seenCache/seenGossipBlockInput.js";
131
- import {ShufflingCache} from "./shufflingCache.js";
131
+ import {ShufflingCache, ShufflingPromiseCancelReason} from "./shufflingCache.js";
132
132
  import {DbCPStateDatastore, checkpointToDatastoreKey} from "./stateCache/datastore/db.js";
133
133
  import {FileCPStateDatastore} from "./stateCache/datastore/file.js";
134
134
  import {CPStateDatastore} from "./stateCache/datastore/types.js";
@@ -620,12 +620,14 @@ export class BeaconChain implements IBeaconChain {
620
620
  async loadFromDisk(): Promise<void> {
621
621
  await this.regen.init();
622
622
  await this.opPool.fromPersisted(this.db, this.getHeadState(), this.bls, this.clock.currentSlot);
623
+ await this.proposerPreferencesPool.fromPersisted(this.db, this.clock.currentSlot);
623
624
  }
624
625
 
625
626
  /** Persist in-memory data to the DB. Call at least once before stopping the process */
626
627
  async persistToDisk(): Promise<void> {
627
628
  await this.archiveStore.persistToDisk();
628
629
  await this.opPool.toPersisted(this.db);
630
+ await this.proposerPreferencesPool.toPersisted(this.db);
629
631
  }
630
632
 
631
633
  getHeadState(): IBeaconStateView {
@@ -1629,21 +1631,48 @@ export class BeaconChain implements IBeaconChain {
1629
1631
  const blockEpoch = computeEpochAtSlot(attHeadBlock.slot);
1630
1632
 
1631
1633
  let state: IBeaconStateView;
1632
- if (blockEpoch < attEpoch - 1) {
1633
- // thanks to one epoch look ahead, we don't need to dial up to attEpoch
1634
- const targetSlot = computeStartSlotAtEpoch(attEpoch - 1);
1635
- this.metrics?.gossipAttestation.useHeadBlockStateDialedToTargetEpoch.inc({caller: regenCaller});
1636
- state = await this.regen.getBlockSlotState(attHeadBlock, targetSlot, {dontTransferCache: true}, regenCaller);
1637
- } else if (blockEpoch > attEpoch) {
1638
- // should not happen, handled inside attestation verification code
1639
- throw Error(`Block epoch ${blockEpoch} is after attestation epoch ${attEpoch}`);
1640
- } else {
1641
- // should use either current or next shuffling of head state
1642
- // it's not likely to hit this since these shufflings are cached already
1643
- // so handle just in case
1644
- this.metrics?.gossipAttestation.useHeadBlockState.inc({caller: regenCaller});
1645
- state = await this.regen.getState(attHeadBlock.stateRoot, regenCaller);
1634
+ try {
1635
+ if (blockEpoch < attEpoch - 1) {
1636
+ // thanks to one epoch look ahead, we don't need to dial up to attEpoch
1637
+ const targetSlot = computeStartSlotAtEpoch(attEpoch - 1);
1638
+ this.metrics?.gossipAttestation.useHeadBlockStateDialedToTargetEpoch.inc({caller: regenCaller});
1639
+ state = await this.regen.getBlockSlotState(attHeadBlock, targetSlot, {dontTransferCache: true}, regenCaller);
1640
+ } else if (blockEpoch > attEpoch) {
1641
+ // should not happen, handled inside attestation verification code
1642
+ throw Error(`Block epoch ${blockEpoch} is after attestation epoch ${attEpoch}`);
1643
+ } else {
1644
+ // should use either current or next shuffling of head state
1645
+ // it's not likely to hit this since these shufflings are cached already
1646
+ // so handle just in case
1647
+ this.metrics?.gossipAttestation.useHeadBlockState.inc({caller: regenCaller});
1648
+ state = await this.regen.getState(attHeadBlock.stateRoot, regenCaller);
1649
+ }
1650
+ } catch (e) {
1651
+ this.shufflingCache.cancelPromise(attEpoch, shufflingDependentRoot, ShufflingPromiseCancelReason.regenError);
1652
+ throw e;
1653
+ }
1654
+
1655
+ const stateDecisionRoot = state.getShufflingDecisionRoot(attEpoch);
1656
+ if (stateDecisionRoot !== shufflingDependentRoot) {
1657
+ // the promise would never be resolved by the state's shuffling
1658
+ this.shufflingCache.cancelPromise(
1659
+ attEpoch,
1660
+ shufflingDependentRoot,
1661
+ ShufflingPromiseCancelReason.decisionRootMismatch
1662
+ );
1663
+ this.logger.debug("Shuffling decision root mismatch on attestation regen", {
1664
+ slot: attHeadBlock.slot,
1665
+ root: attHeadBlock.blockRoot,
1666
+ epoch: attEpoch,
1667
+ shufflingDependentRoot,
1668
+ stateDecisionRoot,
1669
+ });
1670
+
1671
+ throw Error(
1672
+ `Shuffling decision root mismatch epoch=${attEpoch} slot=${attHeadBlock.slot} root=${attHeadBlock.blockRoot} expected=${shufflingDependentRoot} actual=${stateDecisionRoot}`
1673
+ );
1646
1674
  }
1675
+
1647
1676
  // resolve the promise to unblock other calls of the same epoch and dependent root
1648
1677
  this.shufflingCache.processState(state);
1649
1678
  return state.getShufflingAtEpoch(attEpoch);
@@ -1907,6 +1936,21 @@ export class BeaconChain implements IBeaconChain {
1907
1936
  }
1908
1937
  }
1909
1938
 
1939
+ async updateAttachedValidators(epoch: Epoch, validatorIndices: ValidatorIndex[]): Promise<void> {
1940
+ let hasNewValidators = false;
1941
+ for (const validatorIndex of validatorIndices) {
1942
+ if (this.beaconProposerCache.track(epoch, validatorIndex)) {
1943
+ hasNewValidators = true;
1944
+ }
1945
+ }
1946
+
1947
+ // Only update validator custody if we discovered new validators
1948
+ if (hasNewValidators) {
1949
+ const finalizedCheckpoint = this.forkChoice.getFinalizedCheckpoint();
1950
+ await this.updateValidatorsCustodyRequirement(finalizedCheckpoint);
1951
+ }
1952
+ }
1953
+
1910
1954
  private async updateValidatorsCustodyRequirement(finalizedCheckpoint: CheckpointWithHex): Promise<void> {
1911
1955
  if (this.custodyConfig.targetCustodyGroupCount === this.config.NUMBER_OF_CUSTODY_GROUPS) {
1912
1956
  // Custody requirements can only be increased, we can disable dynamic custody updates
@@ -288,6 +288,8 @@ export interface IBeaconChain {
288
288
  waitForBlock(slot: Slot, root: RootHex): Promise<boolean>;
289
289
 
290
290
  updateBeaconProposerData(epoch: Epoch, proposers: ProposerPreparationData[]): Promise<void>;
291
+ /** Track validators that use this node for duties, e.g. by subscribing to attestation subnets */
292
+ updateAttachedValidators(epoch: Epoch, validatorIndices: ValidatorIndex[]): Promise<void>;
291
293
 
292
294
  persistBlock(data: BeaconBlock | BlindedBeaconBlock, suffix?: string): void;
293
295
  persistExecutionPayloadEnvelope(envelope: gloas.ExecutionPayloadEnvelope): void;
@@ -1,5 +1,4 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
- import {DbBatch, Id, Repository} from "@lodestar/db";
3
2
  import {
4
3
  BLS_WITHDRAWAL_PREFIX,
5
4
  ForkName,
@@ -42,6 +41,7 @@ import {
42
41
  getAttesterSlashingSignatureDomains,
43
42
  getProposerSlashingSignatureDomain,
44
43
  isValidBlsToExecutionChangeForBlockInclusion,
44
+ persistDiff,
45
45
  } from "./utils.js";
46
46
 
47
47
  type HexRoot = string;
@@ -500,33 +500,3 @@ function isVoluntaryExitSignatureIncludable(stateFork: ForkSeq, voluntaryExitFor
500
500
  function isSlashableAtEpoch(validator: phase0.Validator, epoch: Epoch): boolean {
501
501
  return !validator.slashed && validator.activationEpoch <= epoch && epoch < validator.withdrawableEpoch;
502
502
  }
503
-
504
- /**
505
- * Persist target items `items` in `dbRepo` doing minimum put and delete writes.
506
- * Reads all keys in repository to compute the diff between current persisted data and target data.
507
- */
508
- async function persistDiff<K extends Id, V>(
509
- dbRepo: Repository<K, V>,
510
- items: {key: K; value: V}[],
511
- serializeKey: (key: K) => number | string,
512
- opts: {updateExisting?: boolean} = {}
513
- ): Promise<void> {
514
- const persistedKeys = await dbRepo.keys();
515
- const batch: DbBatch<K, V> = [];
516
-
517
- const persistedKeysSerialized = new Set(persistedKeys.map(serializeKey));
518
- for (const item of items) {
519
- if (opts.updateExisting || !persistedKeysSerialized.has(serializeKey(item.key))) {
520
- batch.push({type: "put", key: item.key, value: item.value});
521
- }
522
- }
523
-
524
- const targetKeysSerialized = new Set(items.map((item) => serializeKey(item.key)));
525
- for (const persistedKey of persistedKeys) {
526
- if (!targetKeysSerialized.has(serializeKey(persistedKey))) {
527
- batch.push({type: "del", key: persistedKey});
528
- }
529
- }
530
-
531
- if (batch.length > 0) await dbRepo.batch(batch);
532
- }
@@ -1,5 +1,7 @@
1
1
  import {RootHex, Slot, ValidatorIndex, gloas} from "@lodestar/types";
2
- import {toRootHex} from "@lodestar/utils";
2
+ import {toHex, toRootHex} from "@lodestar/utils";
3
+ import {IBeaconDb} from "../../db/index.js";
4
+ import {persistDiff} from "./utils.js";
3
5
 
4
6
  /**
5
7
  * Pool of validated `SignedProposerPreferences` indexed by `(slot, dependent_root)`.
@@ -64,4 +66,21 @@ export class ProposerPreferencesPool {
64
66
  if (slot < currentSlot) this.bySlot.delete(slot);
65
67
  }
66
68
  }
69
+
70
+ /** Restore the preferences of upcoming slots, they were validated before they were persisted */
71
+ async fromPersisted(db: IBeaconDb, currentSlot: Slot): Promise<void> {
72
+ for (const signed of await db.proposerPreferences.values()) {
73
+ if (signed.message.proposalSlot >= currentSlot) {
74
+ this.add(signed);
75
+ }
76
+ }
77
+ }
78
+
79
+ async toPersisted(db: IBeaconDb): Promise<void> {
80
+ await persistDiff(
81
+ db.proposerPreferences,
82
+ this.getAll().map((signed) => ({key: db.proposerPreferences.getId(signed), value: signed})),
83
+ toHex
84
+ );
85
+ }
67
86
  }
@@ -1,5 +1,6 @@
1
1
  import {Signature} from "@chainsafe/lodestar-z/blst";
2
2
  import {BeaconConfig} from "@lodestar/config";
3
+ import {DbBatch, Id, Repository} from "@lodestar/db";
3
4
  import {BLS_WITHDRAWAL_PREFIX, DOMAIN_BEACON_ATTESTER, DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
4
5
  import {IBeaconStateView, computeStartSlotAtEpoch} from "@lodestar/state-transition";
5
6
  import {AttesterSlashing, Domain, Slot, capella, phase0} from "@lodestar/types";
@@ -91,3 +92,33 @@ export function isValidBlsToExecutionChangeForBlockInclusion(
91
92
  export function isElectraAggregate(aggregate: AggregateFast): aggregate is AggregateFastElectra {
92
93
  return (aggregate as AggregateFastElectra).committeeBits !== undefined;
93
94
  }
95
+
96
+ /**
97
+ * Persist target items `items` in `dbRepo` doing minimum put and delete writes.
98
+ * Reads all keys in repository to compute the diff between current persisted data and target data.
99
+ */
100
+ export async function persistDiff<K extends Id, V>(
101
+ dbRepo: Repository<K, V>,
102
+ items: {key: K; value: V}[],
103
+ serializeKey: (key: K) => number | string,
104
+ opts: {updateExisting?: boolean} = {}
105
+ ): Promise<void> {
106
+ const persistedKeys = await dbRepo.keys();
107
+ const batch: DbBatch<K, V> = [];
108
+
109
+ const persistedKeysSerialized = new Set(persistedKeys.map(serializeKey));
110
+ for (const item of items) {
111
+ if (opts.updateExisting || !persistedKeysSerialized.has(serializeKey(item.key))) {
112
+ batch.push({type: "put", key: item.key, value: item.value});
113
+ }
114
+ }
115
+
116
+ const targetKeysSerialized = new Set(items.map((item) => serializeKey(item.key)));
117
+ for (const persistedKey of persistedKeys) {
118
+ if (!targetKeysSerialized.has(serializeKey(persistedKey))) {
119
+ batch.push({type: "del", key: persistedKey});
120
+ }
121
+ }
122
+
123
+ if (batch.length > 0) await dbRepo.batch(batch);
124
+ }
@@ -22,7 +22,7 @@ import {
22
22
  isStatePostGloas,
23
23
  } from "@lodestar/state-transition";
24
24
  import {Bytes32, Slot, ValidatorIndex} from "@lodestar/types";
25
- import {Logger, fromHex, isErrorAborted, sleep} from "@lodestar/utils";
25
+ import {Logger, fromHex, isErrorAborted, sleep, toHex} from "@lodestar/utils";
26
26
  import {GENESIS_SLOT} from "../constants/constants.js";
27
27
  import {BuilderStatus} from "../execution/builder/http.js";
28
28
  import {PayloadAttributes} from "../execution/index.js";
@@ -31,7 +31,11 @@ import {ClockEvent} from "../util/clock.js";
31
31
  import {isQueueErrorAborted} from "../util/queue/index.js";
32
32
  import {ForkchoiceCaller} from "./forkChoice/index.js";
33
33
  import {IBeaconChain} from "./interface.js";
34
- import {getPayloadAttributesForSSE, prepareExecutionPayload} from "./produceBlock/produceBlockBody.js";
34
+ import {
35
+ getPayloadAttributesForSSE,
36
+ getPooledProposerPreferences,
37
+ prepareExecutionPayload,
38
+ } from "./produceBlock/produceBlockBody.js";
35
39
  import {RegenCaller} from "./regen/index.js";
36
40
 
37
41
  // TODO GLOAS: re-evaluate this timing
@@ -158,7 +162,15 @@ export class PrepareNextSlotScheduler {
158
162
  };
159
163
 
160
164
  const proposerIndex = await getProposerIndex();
161
- feeRecipient = this.chain.beaconProposerCache.get(proposerIndex);
165
+ if (this.chain.beaconProposerCache.has(proposerIndex)) {
166
+ const pooledPreferences = isForkPostGloas(fork)
167
+ ? getPooledProposerPreferences(this.chain, prepareSlot, headRoot)
168
+ : null;
169
+ feeRecipient =
170
+ pooledPreferences !== null
171
+ ? toHex(pooledPreferences.feeRecipient)
172
+ : this.chain.beaconProposerCache.getOrDefault(proposerIndex);
173
+ }
162
174
 
163
175
  // Predict the proposer head of the next slot when either:
164
176
  // - we are proposing the next slot (single-slot proposer-boost-reorg), or
@@ -77,6 +77,7 @@ import {IExecutionBuilder, IExecutionEngine, PayloadAttributes, PayloadId} from
77
77
  import {getShufflingDependentRoot} from "../../util/dependentRoot.js";
78
78
  import {fromGraffitiBytes} from "../../util/graffiti.js";
79
79
  import {kzg} from "../../util/kzg.js";
80
+ import {BeaconProposerCache} from "../beaconProposerCache.js";
80
81
  import type {BeaconChain} from "../chain.js";
81
82
  import {CommonBlockBody} from "../interface.js";
82
83
  import {ProposerPreferencesPool} from "../opPools/index.js";
@@ -286,10 +287,13 @@ export async function produceBlockBody<T extends BlockType>(
286
287
  // this into a completely separate function and have pre/post gloas more separated
287
288
  const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice, this.logger);
288
289
  const finalizedBlockHash = getFinalizedExecutionBlockHash(this.forkChoice);
289
- // TODO GLOAS: post-Gloas, proposer feeRecipient is also carried (signed) in
290
- // ProposerPreferencesPool. Consider using this unified cache instead
291
- // see https://github.com/ChainSafe/lodestar/issues/9379
292
- const feeRecipient = requestedFeeRecipient ?? this.beaconProposerCache.getOrDefault(proposerIndex);
290
+ const {feeRecipient, feeRecipientType} = getProposerFeeRecipient(
291
+ requestedFeeRecipient,
292
+ getPooledProposerPreferences(this, blockSlot, parentBlock.blockRoot),
293
+ this.beaconProposerCache,
294
+ proposerIndex
295
+ );
296
+ Object.assign(logMeta, {feeRecipientType, feeRecipient});
293
297
 
294
298
  const endExecutionPayload = this.metrics?.executionBlockProductionTimeSteps.startTimer();
295
299
 
@@ -442,13 +446,12 @@ export async function produceBlockBody<T extends BlockType>(
442
446
 
443
447
  const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice, this.logger);
444
448
  const finalizedBlockHash = getFinalizedExecutionBlockHash(this.forkChoice);
445
- const feeRecipient = requestedFeeRecipient ?? this.beaconProposerCache.getOrDefault(proposerIndex);
446
- const feeRecipientType = requestedFeeRecipient
447
- ? "requested"
448
- : this.beaconProposerCache.get(proposerIndex)
449
- ? "cached"
450
- : "default";
451
-
449
+ const {feeRecipient, feeRecipientType} = getProposerFeeRecipient(
450
+ requestedFeeRecipient,
451
+ null,
452
+ this.beaconProposerCache,
453
+ proposerIndex
454
+ );
452
455
  Object.assign(logMeta, {feeRecipientType, feeRecipient});
453
456
 
454
457
  if (blockType === BlockType.Blinded) {
@@ -974,9 +977,8 @@ function preparePayloadAttributes(
974
977
  * Resolve the proposer's preferred (target) gas limit for the Gloas `PayloadAttributesV4`
975
978
  * `targetGasLimit` field (consensus-specs#5235, execution-apis#796).
976
979
  *
977
- * Sourced from the `SignedProposerPreferences` the proposer's VC submitted to the pool
978
- * (same `(slot, dependent_root)` lookup as gossip bid validation). When no matching
979
- * preferences are pooled, target the parent payload's gas limit so the gas limit stays
980
+ * Sourced from the `SignedProposerPreferences` the proposer's VC submitted to the pool. When no
981
+ * matching preferences are pooled, target the parent payload's gas limit so the gas limit stays
980
982
  * unchanged (`is_gas_limit_target_compatible` then requires `gas_limit == parent_gas_limit`).
981
983
  *
982
984
  * The parent payload's gas_limit is read from fork choice — the variant matching
@@ -991,26 +993,9 @@ function getProposerTargetGasLimit(
991
993
  parentBlockHash: Bytes32
992
994
  ): bigint {
993
995
  const parentBlockRootHex = toRootHex(parentBlockRoot);
994
- const parentBlock = chain.forkChoice.getBlockHexDefaultStatus(parentBlockRootHex);
995
- const dependentRootHex = (() => {
996
- if (parentBlock === null) {
997
- return null;
998
- }
999
- try {
1000
- return getShufflingDependentRoot(
1001
- chain.forkChoice,
1002
- computeEpochAtSlot(prepareSlot),
1003
- computeEpochAtSlot(parentBlock.slot),
1004
- parentBlock
1005
- );
1006
- } catch {
1007
- return null;
1008
- }
1009
- })();
1010
-
1011
- const pref = dependentRootHex !== null ? chain.proposerPreferencesPool.get(prepareSlot, dependentRootHex) : null;
1012
- if (pref !== null) {
1013
- return pref.message.targetGasLimit;
996
+ const preferences = getPooledProposerPreferences(chain, prepareSlot, parentBlockRootHex);
997
+ if (preferences !== null) {
998
+ return preferences.targetGasLimit;
1014
999
  }
1015
1000
 
1016
1001
  const parentPayloadVariant = chain.forkChoice.getBlockHexAndBlockHash(parentBlockRootHex, toRootHex(parentBlockHash));
@@ -1022,6 +1007,55 @@ function getProposerTargetGasLimit(
1022
1007
  return BigInt(parentPayloadVariant.executionPayloadGasLimit);
1023
1008
  }
1024
1009
 
1010
+ /** The proposer's request wins over its signed preferences, which win over its registered proposer data */
1011
+ function getProposerFeeRecipient(
1012
+ requestedFeeRecipient: string | undefined,
1013
+ pooledPreferences: gloas.ProposerPreferences | null,
1014
+ beaconProposerCache: BeaconProposerCache,
1015
+ proposerIndex: ValidatorIndex
1016
+ ): {feeRecipient: string; feeRecipientType: "requested" | "preferences" | "cached" | "default"} {
1017
+ if (requestedFeeRecipient !== undefined) {
1018
+ return {feeRecipient: requestedFeeRecipient, feeRecipientType: "requested"};
1019
+ }
1020
+ if (pooledPreferences !== null) {
1021
+ return {feeRecipient: toHex(pooledPreferences.feeRecipient), feeRecipientType: "preferences"};
1022
+ }
1023
+ const cachedFeeRecipient = beaconProposerCache.get(proposerIndex);
1024
+ if (cachedFeeRecipient !== undefined) {
1025
+ return {feeRecipient: cachedFeeRecipient, feeRecipientType: "cached"};
1026
+ }
1027
+ return {feeRecipient: beaconProposerCache.getOrDefault(proposerIndex), feeRecipientType: "default"};
1028
+ }
1029
+
1030
+ /**
1031
+ * The preferences the proposer of `slot` signed for the branch of `parentBlockRoot`, the same
1032
+ * `(slot, dependent_root)` lookup as gossip bid validation. Null if none are pooled.
1033
+ */
1034
+ export function getPooledProposerPreferences(
1035
+ chain: {forkChoice: IForkChoice; proposerPreferencesPool: ProposerPreferencesPool},
1036
+ slot: Slot,
1037
+ parentBlockRootHex: RootHex
1038
+ ): gloas.ProposerPreferences | null {
1039
+ const parentBlock = chain.forkChoice.getBlockHexDefaultStatus(parentBlockRootHex);
1040
+ if (parentBlock === null) {
1041
+ return null;
1042
+ }
1043
+
1044
+ let dependentRootHex: RootHex;
1045
+ try {
1046
+ dependentRootHex = getShufflingDependentRoot(
1047
+ chain.forkChoice,
1048
+ computeEpochAtSlot(slot),
1049
+ computeEpochAtSlot(parentBlock.slot),
1050
+ parentBlock
1051
+ );
1052
+ } catch {
1053
+ return null;
1054
+ }
1055
+
1056
+ return chain.proposerPreferencesPool.get(slot, dependentRootHex)?.message ?? null;
1057
+ }
1058
+
1025
1059
  export async function produceCommonBlockBody<T extends BlockType>(
1026
1060
  this: BeaconChain,
1027
1061
  blockType: T,