-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Root, Slot} from "@chainsafe/lodestar-types"; | ||
import {toHexString} from "@chainsafe/ssz"; | ||
|
||
/** | ||
* Sync this up to this target. Uses slot instead of epoch to re-use logic for finalized sync | ||
* and head sync. The root is used to uniquely identify this chain on different forks | ||
*/ | ||
export type ChainTarget = { | ||
slot: Slot; | ||
root: Root; | ||
}; | ||
|
||
export function computeMostCommonTarget(targets: ChainTarget[]): ChainTarget | null { | ||
const targetsById = new Map<string, ChainTarget>(); | ||
const countById = new Map<string, number>(); | ||
|
||
for (const target of targets) { | ||
const targetId = `${target.slot}-${toHexString(target.root)}`; | ||
targetsById.set(targetId, target); | ||
countById.set(targetId, 1 + (countById.get(targetId) ?? 0)); | ||
} | ||
|
||
let mostCommon: {count: number; targetId: string} | null = null; | ||
for (const [targetId, count] of countById.entries()) { | ||
if (!mostCommon || count > mostCommon.count) { | ||
mostCommon = {count, targetId}; | ||
} | ||
} | ||
|
||
return mostCommon && (targetsById.get(mostCommon.targetId) ?? null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export * from "./batches"; | ||
export * from "./chainTarget"; | ||
export * from "./hashBlocks"; | ||
export * from "./peerBalancer"; | ||
export * from "./shouldRemoveChain"; | ||
export * from "./updateChains"; | ||
export * from "./wrapError"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters