Skip to content

Commit

Permalink
Skip check for old finalized epoch in assertPeerRelevance (#3405)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Nov 3, 2021
1 parent f5f60ea commit 397327a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/lodestar/src/network/peers/utils/assertPeerRelevance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {computeStartSlotAtEpoch, getBlockRootAtSlot} from "@chainsafe/lodestar-beacon-state-transition";
import {SLOTS_PER_HISTORICAL_ROOT} from "@chainsafe/lodestar-params";
import {Epoch, ForkDigest, Root, phase0, ssz} from "@chainsafe/lodestar-types";
import {LodestarError} from "@chainsafe/lodestar-utils";
import {toHexString} from "@chainsafe/ssz";
Expand Down Expand Up @@ -71,7 +72,7 @@ export function assertPeerRelevance(remote: phase0.Status, chain: IBeaconChain):
: // This will get the latest known block at the start of the epoch.
getRootAtHistoricalEpoch(chain, remote.finalizedEpoch);

if (!ssz.Root.equals(remoteRoot, expectedRoot)) {
if (expectedRoot !== null && !ssz.Root.equals(remoteRoot, expectedRoot)) {
throw new IrrelevantPeerError({
code: IrrelevantPeerErrorCode.DIFFERENT_FINALIZED,
expectedRoot: toHexString(expectedRoot), // forkChoice returns Tree BranchNode which the logger prints as {}
Expand All @@ -88,11 +89,17 @@ export function isZeroRoot(root: Root): boolean {
return ssz.Root.equals(root, ZERO_ROOT);
}

function getRootAtHistoricalEpoch(chain: IBeaconChain, epoch: Epoch): Root {
function getRootAtHistoricalEpoch(chain: IBeaconChain, epoch: Epoch): Root | null {
const headState = chain.getHeadState();

const slot = computeStartSlotAtEpoch(epoch);

if (slot < headState.slot - SLOTS_PER_HISTORICAL_ROOT) {
// TODO: If the slot is very old, go to the historical blocks DB and fetch the block with less or equal `slot`.
// Note that our db schema will have to be updated to persist the block root to prevent re-hashing.
// For now peers will be accepted, since it's better than throwing an error on `getBlockRootAtSlot()`
return null;
}

// This will get the latest known block at the start of the epoch.
// NOTE: Throws if the epoch if from a long-ago epoch
return getBlockRootAtSlot(headState, slot);
Expand Down

0 comments on commit 397327a

Please sign in to comment.