Skip to content

Commit

Permalink
fix: return finalized as false if finalized epoch is genesis epoch (#…
Browse files Browse the repository at this point in the history
…6965)

* fix: return finalized as false if called with genesis slot or epoch

* Update genesis epoch / slot checks
  • Loading branch information
nflaig authored Jul 23, 2024
1 parent 81f9d97 commit 27012f9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock, UpdateHeadOpt} from "@lodestar/fork-choice";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {Logger, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {ForkSeq, GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";

import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
import {IBeaconDb} from "../db/index.js";
Expand Down Expand Up @@ -430,7 +430,11 @@ export class BeaconChain implements IBeaconChain {
{dontTransferCache: true},
RegenCaller.restApi
);
return {state, executionOptimistic: isOptimisticBlock(block), finalized: slot === finalizedBlock.slot};
return {
state,
executionOptimistic: isOptimisticBlock(block),
finalized: slot === finalizedBlock.slot && finalizedBlock.slot !== GENESIS_SLOT,
};
} else {
// Just check if state is already in the cache. If it's not dialed to the correct slot,
// do not bother in advancing the state. restApiCanTriggerRegen == false means do no work
Expand All @@ -440,7 +444,13 @@ export class BeaconChain implements IBeaconChain {
}

const state = this.regen.getStateSync(block.stateRoot);
return state && {state, executionOptimistic: isOptimisticBlock(block), finalized: slot === finalizedBlock.slot};
return (
state && {
state,
executionOptimistic: isOptimisticBlock(block),
finalized: slot === finalizedBlock.slot && finalizedBlock.slot !== GENESIS_SLOT,
}
);
}
} else {
// request for finalized state
Expand All @@ -458,10 +468,11 @@ export class BeaconChain implements IBeaconChain {
if (opts?.allowRegen) {
const state = await this.regen.getState(stateRoot, RegenCaller.restApi);
const block = this.forkChoice.getBlock(state.latestBlockHeader.hashTreeRoot());
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
return {
state,
executionOptimistic: block != null && isOptimisticBlock(block),
finalized: state.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
finalized: state.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
};
}

Expand All @@ -473,10 +484,11 @@ export class BeaconChain implements IBeaconChain {
const cachedStateCtx = this.regen.getStateSync(stateRoot);
if (cachedStateCtx) {
const block = this.forkChoice.getBlock(cachedStateCtx.latestBlockHeader.hashTreeRoot());
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
return {
state: cachedStateCtx,
executionOptimistic: block != null && isOptimisticBlock(block),
finalized: cachedStateCtx.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
finalized: cachedStateCtx.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
};
}

Expand All @@ -491,10 +503,11 @@ export class BeaconChain implements IBeaconChain {
const cachedStateCtx = this.regen.getCheckpointStateSync(checkpoint);
if (cachedStateCtx) {
const block = this.forkChoice.getBlock(cachedStateCtx.latestBlockHeader.hashTreeRoot());
const finalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
return {
state: cachedStateCtx,
executionOptimistic: block != null && isOptimisticBlock(block),
finalized: cachedStateCtx.epochCtx.epoch <= this.forkChoice.getFinalizedCheckpoint().epoch,
finalized: cachedStateCtx.epochCtx.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
};
}

Expand Down

0 comments on commit 27012f9

Please sign in to comment.