Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax nodeIsViableForHead if justified epoch is previous epoch #4520

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const defaultChainOptions: IChainOptions = {
disableBlsBatchVerify: false,
proposerBoostEnabled: true,
computeUnrealized: true,
countUnrealizedFull: false,
safeSlotsToImportOptimistically: SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY,
suggestedFeeRecipient: defaultValidatorOptions.suggestedFeeRecipient,
assertCorrectProgressiveBalances: false,
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IChainArgs {
"chain.proposerBoostEnabled": boolean;
"chain.disableImportExecutionFcU": boolean;
"chain.computeUnrealized": boolean;
"chain.countUnrealizedFull": boolean;
"chain.assertCorrectProgressiveBalances": boolean;
"chain.maxSkipSlots": number;
"safe-slots-to-import-optimistically": number;
Expand All @@ -30,6 +31,7 @@ export function parseArgs(args: IChainArgs): IBeaconNodeOptions["chain"] {
proposerBoostEnabled: args["chain.proposerBoostEnabled"],
disableImportExecutionFcU: args["chain.disableImportExecutionFcU"],
computeUnrealized: args["chain.computeUnrealized"],
countUnrealizedFull: args["chain.countUnrealizedFull"],
assertCorrectProgressiveBalances: args["chain.assertCorrectProgressiveBalances"],
maxSkipSlots: args["chain.maxSkipSlots"],
safeSlotsToImportOptimistically: args["safe-slots-to-import-optimistically"],
Expand Down Expand Up @@ -101,6 +103,14 @@ Will double processing times. Use only for debugging purposes.",
group: "chain",
},

"chain.countUnrealizedFull": {
hidden: true,
type: "boolean",
description: "Compute unrealized checkpoints and fully use it",
defaultDescription: String(defaultOptions.chain.computeUnrealized),
group: "chain",
},

"chain.maxSkipSlots": {
hidden: true,
type: "number",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/test/unit/options/beaconNodeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("options / beaconNodeOptions", () => {
"chain.proposerBoostEnabled": false,
"chain.disableImportExecutionFcU": false,
"chain.computeUnrealized": true,
"chain.countUnrealizedFull": true,
suggestedFeeRecipient: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"chain.assertCorrectProgressiveBalances": true,
"chain.maxSkipSlots": 100,
Expand Down Expand Up @@ -91,6 +92,7 @@ describe("options / beaconNodeOptions", () => {
proposerBoostEnabled: false,
disableImportExecutionFcU: false,
computeUnrealized: true,
countUnrealizedFull: true,
safeSlotsToImportOptimistically: 256,
suggestedFeeRecipient: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
assertCorrectProgressiveBalances: true,
Expand Down
1 change: 1 addition & 0 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {IForkChoiceStore, CheckpointWithHex, toCheckpointWithHex, JustifiedBalan
export type ForkChoiceOpts = {
proposerBoostEnabled?: boolean;
computeUnrealized?: boolean;
countUnrealizedFull?: boolean;
};

/**
Expand Down
78 changes: 48 additions & 30 deletions packages/fork-choice/src/protoArray/protoArray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Epoch, RootHex, Slot} from "@lodestar/types";
import {computeEpochAtSlot} from "@lodestar/state-transition";
import {GENESIS_EPOCH} from "@lodestar/params";
import {toHexString} from "@chainsafe/ssz";

import {ForkChoiceOpts} from "../forkChoice/forkChoice.js";
import {ProtoBlock, ProtoNode, HEX_ZERO_HASH, ExecutionStatus, LVHExecResponse} from "./interface.js";
import {ProtoArrayError, ProtoArrayErrorCode, LVHExecError, LVHExecErrorCode} from "./errors.js";

Expand All @@ -23,35 +25,43 @@ export class ProtoArray {
lvhError?: LVHExecError;

private previousProposerBoost: ProposerBoost | null = null;

constructor({
pruneThreshold,
justifiedEpoch,
justifiedRoot,
finalizedEpoch,
finalizedRoot,
}: {
pruneThreshold: number;
justifiedEpoch: Epoch;
justifiedRoot: RootHex;
finalizedEpoch: Epoch;
finalizedRoot: RootHex;
}) {
private countUnrealizedFull = false;

constructor(
{
pruneThreshold,
justifiedEpoch,
justifiedRoot,
finalizedEpoch,
finalizedRoot,
}: {
pruneThreshold: number;
justifiedEpoch: Epoch;
justifiedRoot: RootHex;
finalizedEpoch: Epoch;
finalizedRoot: RootHex;
},
opts?: ForkChoiceOpts
) {
this.pruneThreshold = pruneThreshold;
this.justifiedEpoch = justifiedEpoch;
this.justifiedRoot = justifiedRoot;
this.finalizedEpoch = finalizedEpoch;
this.finalizedRoot = finalizedRoot;
this.countUnrealizedFull = opts?.countUnrealizedFull ?? false;
}

static initialize(block: Omit<ProtoBlock, "targetRoot">, currentSlot: Slot): ProtoArray {
const protoArray = new ProtoArray({
pruneThreshold: DEFAULT_PRUNE_THRESHOLD,
justifiedEpoch: block.justifiedEpoch,
justifiedRoot: block.justifiedRoot,
finalizedEpoch: block.finalizedEpoch,
finalizedRoot: block.finalizedRoot,
});
static initialize(block: Omit<ProtoBlock, "targetRoot">, currentSlot: Slot, opts?: ForkChoiceOpts): ProtoArray {
const protoArray = new ProtoArray(
{
pruneThreshold: DEFAULT_PRUNE_THRESHOLD,
justifiedEpoch: block.justifiedEpoch,
justifiedRoot: block.justifiedRoot,
finalizedEpoch: block.finalizedEpoch,
finalizedRoot: block.finalizedRoot,
},
opts
);
protoArray.onBlock(
{
...block,
Expand Down Expand Up @@ -731,22 +741,30 @@ export class ProtoArray {
if (node.executionStatus === ExecutionStatus.Invalid) {
return false;
}
const currentEpoch = computeEpochAtSlot(currentSlot);
const previousEpoch = currentEpoch - 1;

// If block is from a previous epoch, filter using unrealized justification & finalization information
// If block is from the current epoch, filter using the head state's justification & finalization information
const isFromPrevEpoch = computeEpochAtSlot(node.slot) < computeEpochAtSlot(currentSlot);
const isFromPrevEpoch = computeEpochAtSlot(node.slot) < currentEpoch;
const nodeJustifiedEpoch = isFromPrevEpoch ? node.unrealizedJustifiedEpoch : node.justifiedEpoch;
const nodeJustifiedRoot = isFromPrevEpoch ? node.unrealizedJustifiedRoot : node.justifiedRoot;
const nodeFinalizedEpoch = isFromPrevEpoch ? node.unrealizedFinalizedEpoch : node.finalizedEpoch;
const nodeFinalizedRoot = isFromPrevEpoch ? node.unrealizedFinalizedRoot : node.finalizedRoot;

const correctJustified =
(nodeJustifiedEpoch === this.justifiedEpoch && nodeJustifiedRoot === this.justifiedRoot) ||
this.justifiedEpoch === 0;
const correctFinalized =
(nodeFinalizedEpoch === this.finalizedEpoch && nodeFinalizedRoot === this.finalizedRoot) ||
this.finalizedEpoch === 0;
return correctJustified && correctFinalized;
// If previous epoch is justified, pull up all tips to at least the previous epoch
if (this.countUnrealizedFull && currentEpoch > GENESIS_EPOCH && this.justifiedEpoch === previousEpoch) {
return node.unrealizedJustifiedEpoch >= previousEpoch;
// If previous epoch is not justified, pull up only tips from past epochs up to the current epoch
} else {
const correctJustified =
(nodeJustifiedEpoch === this.justifiedEpoch && nodeJustifiedRoot === this.justifiedRoot) ||
this.justifiedEpoch === 0;
const correctFinalized =
(nodeFinalizedEpoch === this.finalizedEpoch && nodeFinalizedRoot === this.finalizedRoot) ||
this.finalizedEpoch === 0;
return correctJustified && correctFinalized;
}
}

/**
Expand Down