Skip to content

Commit

Permalink
Merge be939b9 into 4d5f777
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths authored Aug 15, 2023
2 parents 4d5f777 + be939b9 commit 2fdc742
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 138 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-node/src/chain/archiver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ export class Archiver {
this.chain.regen.pruneOnFinalized(finalizedEpoch);

// tasks rely on extended fork choice
this.chain.forkChoice.prune(finalized.rootHex);
const prunedBlocks = this.chain.forkChoice.prune(finalized.rootHex);
await this.updateBackfillRange(finalized);

this.logger.verbose("Finish processing finalized checkpoint", {
epoch: finalizedEpoch,
rootHex: finalized.rootHex,
prunedBlocks: prunedBlocks.length,
});
} catch (e) {
this.logger.error("Error processing finalized checkpoint", {epoch: finalized.epoch}, e as Error);
Expand Down
47 changes: 41 additions & 6 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class ForkChoice implements IForkChoice {
const oldBalances = this.balances;
const newBalances = this.fcStore.justified.balances;
const deltas = computeDeltas(
this.protoArray.indices,
this.protoArray.nodes.length,
this.votes,
oldBalances,
newBalances,
Expand Down Expand Up @@ -555,7 +555,7 @@ export class ForkChoice implements IForkChoice {
}
return {
epoch: vote.nextEpoch,
root: vote.nextRoot,
root: vote.nextIndex === null ? HEX_ZERO_HASH : this.protoArray.nodes[vote.nextIndex].blockRoot,
};
}

Expand Down Expand Up @@ -665,8 +665,37 @@ export class ForkChoice implements IForkChoice {
return this.protoArray.isDescendant(ancestorRoot, descendantRoot);
}

/**
* All indices in votes are relative to proto array so always keep it up to date
*/
prune(finalizedRoot: RootHex): ProtoBlock[] {
return this.protoArray.maybePrune(finalizedRoot);
const prunedNodes = this.protoArray.maybePrune(finalizedRoot);
const prunedCount = prunedNodes.length;
for (const vote of this.votes) {
// validator has never voted
if (vote === undefined) {
continue;
}

if (vote.currentIndex !== null) {
if (vote.currentIndex >= prunedCount) {
vote.currentIndex -= prunedCount;
} else {
// the vote was for a pruned proto node
vote.currentIndex = null;
}
}

if (vote.nextIndex !== null) {
if (vote.nextIndex >= prunedCount) {
vote.nextIndex -= prunedCount;
} else {
// the vote was for a pruned proto node
vote.nextIndex = null;
}
}
}
return prunedNodes;
}

setPruneThreshold(threshold: number): void {
Expand Down Expand Up @@ -1091,14 +1120,20 @@ export class ForkChoice implements IForkChoice {
*/
private addLatestMessage(validatorIndex: ValidatorIndex, nextEpoch: Epoch, nextRoot: RootHex): void {
const vote = this.votes[validatorIndex];
// should not happen, attestation is validated before this step
const nextIndex = this.protoArray.indices.get(nextRoot);
if (nextIndex === undefined) {
throw new Error(`Could not find proto index for nextRoot ${nextRoot}`);
}

if (vote === undefined) {
this.votes[validatorIndex] = {
currentRoot: HEX_ZERO_HASH,
nextRoot,
currentIndex: null,
nextIndex,
nextEpoch,
};
} else if (nextEpoch > vote.nextEpoch) {
vote.nextRoot = nextRoot;
vote.nextIndex = nextIndex;
vote.nextEpoch = nextEpoch;
}
// else its an old vote, don't count it
Expand Down
76 changes: 29 additions & 47 deletions packages/fork-choice/src/protoArray/computeDeltas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ValidatorIndex} from "@lodestar/types";
import {EffectiveBalanceIncrements} from "@lodestar/state-transition";
import {VoteTracker, HEX_ZERO_HASH} from "./interface.js";
import {VoteTracker} from "./interface.js";
import {ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";

/**
Expand All @@ -13,31 +13,20 @@ import {ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";
* - If a value in `indices` is greater to or equal to `indices.length`.
*/
export function computeDeltas(
indices: Map<string, number>,
numProtoNodes: number,
votes: VoteTracker[],
oldBalances: EffectiveBalanceIncrements,
newBalances: EffectiveBalanceIncrements,
equivocatingIndices: Set<ValidatorIndex>
): number[] {
const deltas = Array<number>(indices.size).fill(0);
const zeroHash = HEX_ZERO_HASH;
const deltas = new Array<number>(numProtoNodes);
for (let i = 0; i < numProtoNodes; i++) {
deltas[i] = 0;
}

// avoid creating new variables in the loop to potentially reduce GC pressure
let oldBalance, newBalance: number;
let currentRoot, nextRoot: string;
let currentDeltaIndex, nextDeltaIndex: number | undefined;
// this function tends to get some very few roots from `indices` so create a small cache to improve performance
const cachedIndices = new Map<string, number>();

const getIndex = (root: string): number | undefined => {
let index = cachedIndices.get(root);
if (index === undefined) {
index = indices.get(root);
if (index !== undefined) {
cachedIndices.set(root, index);
}
}
return index;
};
let currentIndex, nextIndex: number | null;

for (let vIndex = 0; vIndex < votes.length; vIndex++) {
const vote = votes[vIndex];
Expand All @@ -46,11 +35,8 @@ export function computeDeltas(
if (vote === undefined) {
continue;
}
currentRoot = vote.currentRoot;
nextRoot = vote.nextRoot;
if (currentRoot === zeroHash && nextRoot === zeroHash) {
continue;
}
currentIndex = vote.currentIndex;
nextIndex = vote.nextIndex;

// IF the validator was not included in the _old_ balances (i.e. it did not exist yet)
// then say its balance was 0
Expand All @@ -65,49 +51,45 @@ export function computeDeltas(

if (equivocatingIndices.size > 0 && equivocatingIndices.has(vIndex)) {
// this function could be called multiple times but we only want to process slashing validator for 1 time
if (currentRoot !== zeroHash) {
currentDeltaIndex = getIndex(currentRoot);
if (currentDeltaIndex !== undefined) {
if (currentDeltaIndex >= deltas.length) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: currentDeltaIndex,
});
}
deltas[currentDeltaIndex] -= oldBalance;
if (currentIndex !== null) {
if (currentIndex >= numProtoNodes) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: currentIndex,
});
}
deltas[currentIndex] -= oldBalance;
}
vote.currentRoot = zeroHash;
vote.currentIndex = null;
continue;
}

if (currentRoot !== nextRoot || oldBalance !== newBalance) {
if (currentIndex !== nextIndex || oldBalance !== newBalance) {
// We ignore the vote if it is not known in `indices .
// We assume that it is outside of our tree (ie: pre-finalization) and therefore not interesting
currentDeltaIndex = getIndex(currentRoot);
if (currentDeltaIndex !== undefined) {
if (currentDeltaIndex >= deltas.length) {
if (currentIndex !== null) {
if (currentIndex >= numProtoNodes) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: currentDeltaIndex,
index: currentIndex,
});
}
deltas[currentDeltaIndex] -= oldBalance;
deltas[currentIndex] -= oldBalance;
}

// We ignore the vote if it is not known in `indices .
// We assume that it is outside of our tree (ie: pre-finalization) and therefore not interesting
nextDeltaIndex = getIndex(nextRoot);
if (nextDeltaIndex !== undefined) {
if (nextDeltaIndex >= deltas.length) {
if (nextIndex !== null) {
if (nextIndex >= numProtoNodes) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: nextDeltaIndex,
index: nextIndex,
});
}
deltas[nextDeltaIndex] += newBalance;
deltas[nextIndex] += newBalance;
}
}
vote.currentRoot = nextRoot;
vote.currentIndex = nextIndex;
}

return deltas;
Expand Down
6 changes: 4 additions & 2 deletions packages/fork-choice/src/protoArray/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export const HEX_ZERO_HASH = "0x000000000000000000000000000000000000000000000000

/**
* Simplified 'latest message' with previous message
* The index is relative to ProtoArray indices
*/
export type VoteTracker = {
currentRoot: RootHex;
nextRoot: RootHex;
currentIndex: number | null;
// if a vode is out of date (the voted index was in the past while proto array is pruned), it will be set to null
nextIndex: number | null;
nextEpoch: Epoch;
};

Expand Down
16 changes: 3 additions & 13 deletions packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import crypto from "node:crypto";
import {itBench, setBenchOpts} from "@dapplion/benchmark";
import {toHexString} from "@chainsafe/ssz";
import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsZeroed} from "@lodestar/state-transition";
import {VoteTracker} from "../../../src/protoArray/interface.js";
import {computeDeltas} from "../../../src/protoArray/computeDeltas.js";
Expand All @@ -10,8 +8,6 @@ describe("computeDeltas", () => {
let oldBalances: EffectiveBalanceIncrements;
let newBalances: EffectiveBalanceIncrements;

const oldRoot = "0x32dec344944029ba183ac387a7aa1f2068591c00e9bfadcfb238e50fbe9ea38e";
const newRoot = "0xb59f3a209f639dd6b5645ea9fad8d441df44c3be93bd1bbf50ef90bf124d1238";
const oneHourProtoNodes = (60 * 60) / 12;
const fourHourProtoNodes = 4 * oneHourProtoNodes;
const oneDayProtoNodes = 24 * oneHourProtoNodes;
Expand All @@ -35,28 +31,22 @@ describe("computeDeltas", () => {
});

for (const numProtoNode of [oneHourProtoNodes, fourHourProtoNodes, oneDayProtoNodes]) {
const indices: Map<string, number> = new Map<string, number>();
for (let i = 0; i < numProtoNode; i++) {
indices.set(toHexString(crypto.randomBytes(32)), i);
}
indices.set(oldRoot, Math.floor(numProtoNode / 2));
indices.set(newRoot, Math.floor(numProtoNode / 2) + 1);
itBench({
id: `computeDeltas ${numValidator} validators ${numProtoNode} proto nodes`,
beforeEach: () => {
const votes: VoteTracker[] = [];
const epoch = 100_000;
for (let i = 0; i < numValidator; i++) {
votes.push({
currentRoot: oldRoot,
nextRoot: newRoot,
currentIndex: Math.floor(numProtoNode / 2),
nextIndex: Math.floor(numProtoNode / 2) + 1,
nextEpoch: epoch,
});
}
return votes;
},
fn: (votes) => {
computeDeltas(indices, votes, oldBalances, newBalances, new Set());
computeDeltas(numProtoNode, votes, oldBalances, newBalances, new Set());
},
});
}
Expand Down
Loading

0 comments on commit 2fdc742

Please sign in to comment.