Skip to content

Commit

Permalink
Ensure proto array weights are coherent (#3524)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Dec 15, 2021
1 parent b4459d2 commit 7f4a590
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/fork-choice/src/protoArray/protoArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class ProtoArray {
// Apply the delta to the node
node.weight += nodeDelta;

// If the node has a parent, try to update its best-child and best-descendant
// Update the parent delta (if any)
const parentIndex = node.parent;
if (parentIndex !== undefined) {
const parentDelta = deltas[parentIndex];
Expand All @@ -143,7 +143,26 @@ export class ProtoArray {

// back-propagate the nodes delta to its parent
deltas[parentIndex] += nodeDelta;
}
}

// A second time, iterate backwards through all indices in `this.nodes`.
//
// We _must_ perform these functions separate from the weight-updating loop above to ensure
// that we have a fully coherent set of weights before updating parent
// best-child/descendant.
for (let nodeIndex = this.nodes.length - 1; nodeIndex >= 0; nodeIndex--) {
const node = this.nodes[nodeIndex];
if (node === undefined) {
throw new ProtoArrayError({
code: ProtoArrayErrorCode.INVALID_NODE_INDEX,
index: nodeIndex,
});
}

// If the node has a parent, try to update its best-child and best-descendant.
const parentIndex = node.parent;
if (parentIndex !== undefined) {
this.maybeUpdateBestChildAndDescendant(parentIndex, nodeIndex);
}
}
Expand Down

0 comments on commit 7f4a590

Please sign in to comment.