@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
45 lines • 1.51 kB
JavaScript
import { toRootHex } from "@lodestar/utils";
/**
* Previously we use computeMostCommonTarget to compute the target for a chain.
* Starting from fulu, we use computeHighestTarget to compute the target for a chain.
*/
export function computeHighestTarget(targets) {
if (targets.length === 0) {
throw Error("Must provide at least one target");
}
let highestSlot = -1;
let highestTargets = [];
for (const target of targets) {
if (target.slot > highestSlot) {
highestSlot = target.slot;
highestTargets = [target];
}
else if (target.slot === highestSlot) {
highestTargets.push(target);
}
// ignore if target.slot < highestSlot
}
if (highestTargets.length === 1) {
return highestTargets[0];
}
return computeMostCommonTarget(highestTargets);
}
function computeMostCommonTarget(targets) {
if (targets.length === 0) {
throw Error("Must provide at least one target");
}
const countById = new Map();
let mostCommonTarget = targets[0];
let mostCommonCount = 0;
for (const target of targets) {
const targetId = `${target.slot}-${toRootHex(target.root)}`;
const count = 1 + (countById.get(targetId) ?? 0);
countById.set(targetId, count);
if (count > mostCommonCount) {
mostCommonCount = count;
mostCommonTarget = target;
}
}
return mostCommonTarget;
}
//# sourceMappingURL=chainTarget.js.map