@lodestar/beacon-node 2.1.0-dev.8b3889166b → 2.1.0-dev.8e668b372b

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.
@@ -1,6 +1,7 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
2
  import {
3
3
  BLS_WITHDRAWAL_PREFIX,
4
+ FAR_FUTURE_EPOCH,
4
5
  ForkName,
5
6
  ForkSeq,
6
7
  MAX_ATTESTER_SLASHINGS,
@@ -17,6 +18,7 @@ import {
17
18
  getAttesterSlashableIndices,
18
19
  getAttesterSlashingSignatureSets,
19
20
  getProposerSlashingSignatureSets,
21
+ getValidatorFromStateBytes,
20
22
  } from "@lodestar/state-transition";
21
23
  import {
22
24
  AttesterSlashing,
@@ -389,12 +391,19 @@ export class OpPool {
389
391
  }
390
392
 
391
393
  /**
392
- * Prune all types of transactions given the latest head state
394
+ * Prune all types of transactions given the latest head state and the finalized checkpoint state.
395
+ * `finalizedState` is only needed to prune voluntary exits, and may be null when there are none.
393
396
  */
394
- pruneAll(headBlock: SignedBeaconBlock, headState: IBeaconStateView): void {
397
+ pruneAll(
398
+ headBlock: SignedBeaconBlock,
399
+ headState: IBeaconStateView,
400
+ finalizedState: IBeaconStateView | Uint8Array | null
401
+ ): void {
395
402
  this.pruneAttesterSlashings(headState);
396
403
  this.pruneProposerSlashings(headState);
397
- this.pruneVoluntaryExits(headState);
404
+ if (finalizedState !== null) {
405
+ this.pruneVoluntaryExits(finalizedState);
406
+ }
398
407
  this.pruneBlsToExecutionChanges(headBlock, headState);
399
408
  }
400
409
 
@@ -438,21 +447,23 @@ export class OpPool {
438
447
 
439
448
  /**
440
449
  * Call after finalizing
441
- * Prune if validator has already exited at or before the finalized checkpoint of the head.
450
+ * Prune if validator has already exited in the finalized state, so an exit included only in a block that is later
451
+ * orphaned stays in the pool. The finalized state may be serialized bytes if it is not in memory.
452
+ *
453
+ * Exits are not pruned by fork: from Deneb the signature domain is fixed to Capella (EIP-7044), so a fork transition
454
+ * cannot invalidate an exit verified at a Deneb+ state. Exits verified with a pre-Capella domain before crossing into
455
+ * Deneb are not handled, as only chains that cross the Deneb fork are affected.
442
456
  */
443
- private pruneVoluntaryExits(headState: IBeaconStateView): void {
444
- const headStateFork = this.config.getForkSeq(headState.slot);
445
- const finalizedEpoch = headState.finalizedCheckpoint.epoch;
446
-
457
+ private pruneVoluntaryExits(finalizedState: IBeaconStateView | Uint8Array): void {
447
458
  for (const [key, voluntaryExit] of this.voluntaryExits.entries()) {
448
- // VoluntaryExit messages signed in the previous fork become invalid and can never be included in any future
449
- // block, so just drop as the head state advances into the next fork.
450
- if (this.config.getForkSeq(computeStartSlotAtEpoch(voluntaryExit.message.epoch)) < headStateFork) {
451
- this.voluntaryExits.delete(key);
452
- }
453
-
454
- // TODO: Improve this simplistic condition
455
- if (voluntaryExit.message.epoch <= finalizedEpoch) {
459
+ const {validatorIndex} = voluntaryExit.message;
460
+ const finalizedValidator =
461
+ finalizedState instanceof Uint8Array
462
+ ? getValidatorFromStateBytes(this.config, finalizedState, validatorIndex)
463
+ : validatorIndex < finalizedState.validatorCount
464
+ ? finalizedState.getValidator(validatorIndex)
465
+ : null;
466
+ if (finalizedValidator !== null && finalizedValidator.exitEpoch !== FAR_FUTURE_EPOCH) {
456
467
  this.voluntaryExits.delete(key);
457
468
  }
458
469
  }
@@ -378,7 +378,7 @@ export class SeenBlockInput {
378
378
  for (const [, blockInput] of sorted) {
379
379
  this.evictBlockInput(blockInput);
380
380
  itemsToDelete--;
381
- if (itemsToDelete <= 0) return;
381
+ if (itemsToDelete <= 0) break;
382
382
  }
383
383
  }
384
384
  pruneSetToMax(this.verifiedProposerSignatures, MAX_BLOCK_INPUT_CACHE_SIZE);
@@ -98,13 +98,16 @@ export async function validateGossipBlock(
98
98
  // If fork choice does *not* consider the parent to be a descendant of the finalized block,
99
99
  // then there are two more cases:
100
100
  //
101
- // 1. We have the parent stored in our database. Because fork-choice has confirmed the
102
- // parent is *not* in our post-finalization DAG, all other blocks must be either
103
- // pre-finalization or conflicting with finalization.
101
+ // 1. Fork choice still holds the parent. Because it is *not* in our post-finalization DAG,
102
+ // the parent is either pre-finalization or conflicting with finalization, so the
103
+ // finalized checkpoint is not an ancestor of the block: REJECT.
104
104
  // 2. The parent is unknown to us, we probably want to download it since it might actually
105
105
  // descend from the finalized root.
106
- // (Non-Lighthouse): Since we prune all blocks non-descendant from finalized checking the `db.block` database won't be useful to guard
107
- // against known bad fork blocks, so we throw PARENT_BLOCK_UNKNOWN for cases (1) and (2)
106
+ // Blocks that fork choice has already pruned fall into case (2), as the `db.block` database
107
+ // does not keep non-descendants of the finalized block either.
108
+ if (chain.forkChoice.hasBlockHexUnsafe(parentRoot)) {
109
+ throw new BlockGossipError(GossipAction.REJECT, {code: BlockErrorCode.NOT_FINALIZED_DESCENDANT, parentRoot});
110
+ }
108
111
  throw new BlockGossipError(GossipAction.IGNORE, {code: BlockErrorCode.PARENT_BLOCK_UNKNOWN, parentRoot});
109
112
  }
110
113
 
@@ -36,7 +36,11 @@ async function validateProposerSlashing(
36
36
 
37
37
  // [REJECT] All of the conditions within process_proposer_slashing pass validation.
38
38
  try {
39
- const proposer = state.getValidator(proposerSlashing.signedHeader1.message.proposerIndex);
39
+ const {proposerIndex} = proposerSlashing.signedHeader1.message;
40
+ if (proposerIndex >= state.validatorCount) {
41
+ throw Error(`Unknown proposer index ${proposerIndex}`);
42
+ }
43
+ const proposer = state.getValidator(proposerIndex);
40
44
  // verifySignature = false, verified in batch below
41
45
  assertValidProposerSlashing(chain.config, state.slot, proposerSlashing, proposer, false);
42
46
  } catch (e) {
@@ -1295,20 +1295,25 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand
1295
1295
  try {
1296
1296
  const delaySec = chain.clock.secFromSlot(payloadAttestationMessage.data.slot, seenTimestampSec);
1297
1297
  metrics?.gossipPayloadAttestationMessage.elapsedTimeTillReceived.observe({source: OpSource.gossip}, delaySec);
1298
- const nextSlotProposer = chain.getHeadState().getBeaconProposer(payloadAttestationMessage.data.slot + 1);
1299
- const isNextSlotProposer = chain.beaconProposerCache.has(nextSlotProposer);
1300
-
1301
- if (isNextSlotProposer) {
1302
- try {
1298
+ try {
1299
+ const nextSlotProposer = chain.getHeadState().getBeaconProposer(payloadAttestationMessage.data.slot + 1);
1300
+ if (chain.beaconProposerCache.has(nextSlotProposer)) {
1303
1301
  const insertOutcome = chain.payloadAttestationPool.add(
1304
1302
  payloadAttestationMessage,
1305
1303
  validationResult.attDataRootHex,
1306
1304
  validationResult.validatorCommitteeIndices
1307
1305
  );
1308
1306
  metrics?.opPool.payloadAttestationPool.gossipInsertOutcome.inc({insertOutcome});
1309
- } catch (e) {
1310
- logger.debug("Error adding to payloadAttestation pool", {}, e as Error);
1311
1307
  }
1308
+ } catch (e) {
1309
+ logger.debug(
1310
+ "Error adding to payloadAttestation pool",
1311
+ {
1312
+ slot: payloadAttestationMessage.data.slot,
1313
+ root: toRootHex(payloadAttestationMessage.data.beaconBlockRoot),
1314
+ },
1315
+ e as Error
1316
+ );
1312
1317
  }
1313
1318
 
1314
1319
  // Gossip accepts adjacent slots within the clock disparity but fork choice only counts the current slot