Skip to content

Commit 27012f9

Browse files
authored
fix: return finalized as false if finalized epoch is genesis epoch (#6965)
* fix: return finalized as false if called with genesis slot or epoch * Update genesis epoch / slot checks
1 parent 81f9d97 commit 27012f9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/beacon-node/src/chain/chain.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock, UpdateHeadOpt} from "@lodestar/fork-choice";
3737
import {ProcessShutdownCallback} from "@lodestar/validator";
3838
import {Logger, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
39-
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
39+
import {ForkSeq, GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";
4040

4141
import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
4242
import {IBeaconDb} from "../db/index.js";
@@ -430,7 +430,11 @@ export class BeaconChain implements IBeaconChain {
430430
{dontTransferCache: true},
431431
RegenCaller.restApi
432432
);
433-
return {state, executionOptimistic: isOptimisticBlock(block), finalized: slot === finalizedBlock.slot};
433+
return {
434+
state,
435+
executionOptimistic: isOptimisticBlock(block),
436+
finalized: slot === finalizedBlock.slot && finalizedBlock.slot !== GENESIS_SLOT,
437+
};
434438
} else {
435439
// Just check if state is already in the cache. If it's not dialed to the correct slot,
436440
// do not bother in advancing the state. restApiCanTriggerRegen == false means do no work
@@ -440,7 +444,13 @@ export class BeaconChain implements IBeaconChain {
440444
}
441445

442446
const state = this.regen.getStateSync(block.stateRoot);
443-
return state && {state, executionOptimistic: isOptimisticBlock(block), finalized: slot === finalizedBlock.slot};
447+
return (
448+
state && {
449+
state,
450+
executionOptimistic: isOptimisticBlock(block),
451+
finalized: slot === finalizedBlock.slot && finalizedBlock.slot !== GENESIS_SLOT,
452+
}
453+
);
444454
}
445455
} else {
446456
// request for finalized state
@@ -458,10 +468,11 @@ export class BeaconChain implements IBeaconChain {
458468
if (opts?.allowRegen) {
459469
const state = await this.regen.getState(stateRoot, RegenCaller.restApi);
460470
const block = this.forkChoice.getBlock(state.latestBlockHeader.hashTreeRoot());
471+
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
461472
return {
462473
state,
463474
executionOptimistic: block != null && isOptimisticBlock(block),
464-
finalized: state.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
475+
finalized: state.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
465476
};
466477
}
467478

@@ -473,10 +484,11 @@ export class BeaconChain implements IBeaconChain {
473484
const cachedStateCtx = this.regen.getStateSync(stateRoot);
474485
if (cachedStateCtx) {
475486
const block = this.forkChoice.getBlock(cachedStateCtx.latestBlockHeader.hashTreeRoot());
487+
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
476488
return {
477489
state: cachedStateCtx,
478490
executionOptimistic: block != null && isOptimisticBlock(block),
479-
finalized: cachedStateCtx.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
491+
finalized: cachedStateCtx.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
480492
};
481493
}
482494

@@ -491,10 +503,11 @@ export class BeaconChain implements IBeaconChain {
491503
const cachedStateCtx = this.regen.getCheckpointStateSync(checkpoint);
492504
if (cachedStateCtx) {
493505
const block = this.forkChoice.getBlock(cachedStateCtx.latestBlockHeader.hashTreeRoot());
506+
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
494507
return {
495508
state: cachedStateCtx,
496509
executionOptimistic: block != null && isOptimisticBlock(block),
497-
finalized: cachedStateCtx.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
510+
finalized: cachedStateCtx.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
498511
};
499512
}
500513

0 commit comments

Comments
 (0)