Skip to content

Commit

Permalink
fix: set vote index to null for out of date vote
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 14, 2023
1 parent aadb2ab commit 6f6011c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 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
18 changes: 15 additions & 3 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export class ForkChoice implements IForkChoice {
}
return {
epoch: vote.nextEpoch,
root: this.protoArray.nodes[vote.nextIndex].blockRoot,
root: vote.nextIndex === null ? HEX_ZERO_HASH : this.protoArray.nodes[vote.nextIndex].blockRoot,
};
}

Expand Down Expand Up @@ -673,9 +673,21 @@ export class ForkChoice implements IForkChoice {
const prunedCount = prunedNodes.length;
for (const vote of this.votes) {
if (vote.currentIndex !== null) {
vote.currentIndex -= prunedCount;
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;
}
}
vote.nextIndex -= prunedCount;
}
return prunedNodes;
}
Expand Down
16 changes: 9 additions & 7 deletions packages/fork-choice/src/protoArray/computeDeltas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function computeDeltas(
const deltas = Array<number>(numProtoNodes).fill(0);
// avoid creating new variables in the loop to potentially reduce GC pressure
let oldBalance, newBalance: number;
let currentIndex, nextIndex: number;
let currentIndex, nextIndex: number | null;

for (let vIndex = 0; vIndex < votes.length; vIndex++) {
const vote = votes[vIndex];
Expand Down Expand Up @@ -75,13 +75,15 @@ export function computeDeltas(

// 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
if (nextIndex >= numProtoNodes) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: nextIndex,
});
if (nextIndex !== null) {
if (nextIndex >= numProtoNodes) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_DELTA,
index: nextIndex,
});
}
deltas[nextIndex] += newBalance;
}
deltas[nextIndex] += newBalance;
}
vote.currentIndex = nextIndex;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/fork-choice/src/protoArray/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const HEX_ZERO_HASH = "0x000000000000000000000000000000000000000000000000
*/
export type VoteTracker = {
currentIndex: number | null;
nextIndex: number;
// 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

0 comments on commit 6f6011c

Please sign in to comment.