Skip to content

Commit

Permalink
Remove I prefix for fork-choice types (#4188)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Jun 26, 2022
1 parent a21514a commit 6cef5d9
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 138 deletions.
50 changes: 25 additions & 25 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {
import {IChainConfig, IChainForkConfig} from "@chainsafe/lodestar-config";

import {computeDeltas} from "../protoArray/computeDeltas.js";
import {HEX_ZERO_HASH, IVoteTracker, IProtoBlock, ExecutionStatus} from "../protoArray/interface.js";
import {HEX_ZERO_HASH, VoteTracker, ProtoBlock, ExecutionStatus} from "../protoArray/interface.js";
import {ProtoArray} from "../protoArray/protoArray.js";

import {IForkChoiceMetrics} from "../metrics.js";
import {ForkChoiceError, ForkChoiceErrorCode, InvalidBlockCode, InvalidAttestationCode} from "./errors.js";
import {IForkChoice, ILatestMessage, IQueuedAttestation, OnBlockPrecachedData, PowBlockHex} from "./interface.js";
import {IForkChoice, LatestMessage, QueuedAttestation, OnBlockPrecachedData, PowBlockHex} from "./interface.js";
import {IForkChoiceStore, CheckpointWithHex, toCheckpointWithHex} from "./store.js";

/* eslint-disable max-len */
Expand Down Expand Up @@ -49,13 +49,13 @@ export class ForkChoice implements IForkChoice {
* Indexed by validator index
* Each vote contains the latest message and previous message
*/
private readonly votes: IVoteTracker[] = [];
private readonly votes: VoteTracker[] = [];

/**
* Attestations that arrived at the current slot and must be queued for later processing.
* NOT currently tracked in the protoArray
*/
private readonly queuedAttestations = new Set<IQueuedAttestation>();
private readonly queuedAttestations = new Set<QueuedAttestation>();

/**
* Balances tracked in the protoArray, or soon to be tracked
Expand All @@ -68,7 +68,7 @@ export class ForkChoice implements IForkChoice {
/** Avoid having to compute detas all the times. */
private synced = false;
/** Cached head */
private head: IProtoBlock;
private head: ProtoBlock;
/**
* Only cache attestation data root hex if it's tree backed since it's available.
**/
Expand Down Expand Up @@ -151,7 +151,7 @@ export class ForkChoice implements IForkChoice {
/**
* Get the cached head
*/
getHead(): IProtoBlock {
getHead(): ProtoBlock {
return this.head;
}

Expand All @@ -174,7 +174,7 @@ export class ForkChoice implements IForkChoice {
*
* https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/fork-choice.md#get_head
*/
updateHead(): IProtoBlock {
updateHead(): ProtoBlock {
// balances is not changed but votes are changed

let timer;
Expand Down Expand Up @@ -240,7 +240,7 @@ export class ForkChoice implements IForkChoice {
}

/** Very expensive function, iterates the entire ProtoArray. Called only in debug API */
getHeads(): IProtoBlock[] {
getHeads(): ProtoBlock[] {
return this.protoArray.nodes.filter((node) => node.bestChild === undefined);
}

Expand Down Expand Up @@ -490,7 +490,7 @@ export class ForkChoice implements IForkChoice {
}
}

getLatestMessage(validatorIndex: ValidatorIndex): ILatestMessage | undefined {
getLatestMessage(validatorIndex: ValidatorIndex): LatestMessage | undefined {
const vote = this.votes[validatorIndex];
if (vote === undefined) {
return undefined;
Expand Down Expand Up @@ -525,8 +525,8 @@ export class ForkChoice implements IForkChoice {
hasBlock(blockRoot: Root): boolean {
return this.hasBlockHex(toHexString(blockRoot));
}
/** Returns a `IProtoBlock` if the block is known **and** a descendant of the finalized root. */
getBlock(blockRoot: Root): IProtoBlock | null {
/** Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root. */
getBlock(blockRoot: Root): ProtoBlock | null {
return this.getBlockHex(toHexString(blockRoot));
}

Expand All @@ -538,9 +538,9 @@ export class ForkChoice implements IForkChoice {
}

/**
* Returns a `IProtoBlock` if the block is known **and** a descendant of the finalized root.
* Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root.
*/
getBlockHex(blockRoot: RootHex): IProtoBlock | null {
getBlockHex(blockRoot: RootHex): ProtoBlock | null {
const block = this.protoArray.getBlock(blockRoot);
if (!block) {
return null;
Expand All @@ -554,7 +554,7 @@ export class ForkChoice implements IForkChoice {
return block;
}

getJustifiedBlock(): IProtoBlock {
getJustifiedBlock(): ProtoBlock {
const block = this.getBlockHex(this.fcStore.justifiedCheckpoint.rootHex);
if (!block) {
throw new ForkChoiceError({
Expand All @@ -565,7 +565,7 @@ export class ForkChoice implements IForkChoice {
return block;
}

getFinalizedBlock(): IProtoBlock {
getFinalizedBlock(): ProtoBlock {
const block = this.getBlockHex(this.fcStore.finalizedCheckpoint.rootHex);
if (!block) {
throw new ForkChoiceError({
Expand Down Expand Up @@ -593,7 +593,7 @@ export class ForkChoice implements IForkChoice {
return this.protoArray.isDescendant(ancestorRoot, descendantRoot);
}

prune(finalizedRoot: RootHex): IProtoBlock[] {
prune(finalizedRoot: RootHex): ProtoBlock[] {
return this.protoArray.maybePrune(finalizedRoot);
}

Expand All @@ -605,15 +605,15 @@ export class ForkChoice implements IForkChoice {
* Iterates backwards through block summaries, starting from a block root.
* Return only the non-finalized blocks.
*/
iterateAncestorBlocks(blockRoot: RootHex): IterableIterator<IProtoBlock> {
iterateAncestorBlocks(blockRoot: RootHex): IterableIterator<ProtoBlock> {
return this.protoArray.iterateAncestorNodes(blockRoot);
}

/**
* Returns all blocks backwards starting from a block root.
* Return only the non-finalized blocks.
*/
getAllAncestorBlocks(blockRoot: RootHex): IProtoBlock[] {
getAllAncestorBlocks(blockRoot: RootHex): ProtoBlock[] {
const blocks = this.protoArray.getAllAncestorNodes(blockRoot);
// the last node is the previous finalized one, it's there to check onBlock finalized checkpoint only.
return blocks.slice(0, blocks.length - 1);
Expand All @@ -622,11 +622,11 @@ export class ForkChoice implements IForkChoice {
/**
* The same to iterateAncestorBlocks but this gets non-ancestor nodes instead of ancestor nodes.
*/
getAllNonAncestorBlocks(blockRoot: RootHex): IProtoBlock[] {
getAllNonAncestorBlocks(blockRoot: RootHex): ProtoBlock[] {
return this.protoArray.getAllNonAncestorNodes(blockRoot);
}

getCanonicalBlockAtSlot(slot: Slot): IProtoBlock | null {
getCanonicalBlockAtSlot(slot: Slot): ProtoBlock | null {
if (slot > this.head.slot) {
return null;
}
Expand All @@ -644,19 +644,19 @@ export class ForkChoice implements IForkChoice {
}

/** Very expensive function, iterates the entire ProtoArray. TODO: Is this function even necessary? */
forwarditerateAncestorBlocks(): IProtoBlock[] {
forwarditerateAncestorBlocks(): ProtoBlock[] {
return this.protoArray.nodes;
}

/** Very expensive function, iterates the entire ProtoArray. TODO: Is this function even necessary? */
getBlockSummariesByParentRoot(parentRoot: RootHex): IProtoBlock[] {
getBlockSummariesByParentRoot(parentRoot: RootHex): ProtoBlock[] {
return this.protoArray.nodes.filter((node) => node.parentRoot === parentRoot);
}

/** Very expensive function, iterates the entire ProtoArray. TODO: Is this function even necessary? */
getBlockSummariesAtSlot(slot: Slot): IProtoBlock[] {
getBlockSummariesAtSlot(slot: Slot): ProtoBlock[] {
const nodes = this.protoArray.nodes;
const blocksAtSlot: IProtoBlock[] = [];
const blocksAtSlot: ProtoBlock[] = [];
for (let i = 0, len = nodes.length; i < len; i++) {
const node = nodes[i];
if (node.slot === slot) {
Expand All @@ -667,7 +667,7 @@ export class ForkChoice implements IForkChoice {
}

/** Returns the distance of common ancestor of nodes to newNode. Returns null if newNode is descendant of prevNode */
getCommonAncestorDistance(prevBlock: IProtoBlock, newBlock: IProtoBlock): number | null {
getCommonAncestorDistance(prevBlock: ProtoBlock, newBlock: ProtoBlock): number | null {
const prevNode = this.protoArray.getNode(prevBlock.blockRoot);
const newNode = this.protoArray.getNode(newBlock.blockRoot);
if (!prevNode) throw Error(`No node if forkChoice for blockRoot ${prevBlock.blockRoot}`);
Expand Down
46 changes: 23 additions & 23 deletions packages/fork-choice/src/forkChoice/interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {EffectiveBalanceIncrements} from "@chainsafe/lodestar-beacon-state-transition";
import {BeaconStateAllForks} from "@chainsafe/lodestar-beacon-state-transition";
import {Epoch, Slot, ValidatorIndex, phase0, allForks, Root, RootHex} from "@chainsafe/lodestar-types";
import {IProtoBlock, ExecutionStatus} from "../protoArray/interface.js";
import {ProtoBlock, ExecutionStatus} from "../protoArray/interface.js";
import {CheckpointWithHex} from "./store.js";

export type CheckpointHex = {
Expand Down Expand Up @@ -31,12 +31,12 @@ export interface IForkChoice {
* https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/fork-choice.md#get_head
*/
getHeadRoot(): RootHex;
getHead(): IProtoBlock;
updateHead(): IProtoBlock;
getHead(): ProtoBlock;
updateHead(): ProtoBlock;
/**
* Retrieves all possible chain heads (leaves of fork choice tree).
*/
getHeads(): IProtoBlock[];
getHeads(): ProtoBlock[];
getFinalizedCheckpoint(): CheckpointWithHex;
getJustifiedCheckpoint(): CheckpointWithHex;
/**
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface IForkChoice {
* will not be run here.
*/
onAttestation(attestation: phase0.IndexedAttestation, attDataRoot?: string): void;
getLatestMessage(validatorIndex: ValidatorIndex): ILatestMessage | undefined;
getLatestMessage(validatorIndex: ValidatorIndex): LatestMessage | undefined;
/**
* Call `onTick` for all slots between `fcStore.getCurrentSlot()` and the provided `currentSlot`.
*/
Expand All @@ -94,12 +94,12 @@ export interface IForkChoice {
hasBlock(blockRoot: Root): boolean;
hasBlockHex(blockRoot: RootHex): boolean;
/**
* Returns a `IProtoBlock` if the block is known **and** a descendant of the finalized root.
* Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root.
*/
getBlock(blockRoot: Root): IProtoBlock | null;
getBlockHex(blockRoot: RootHex): IProtoBlock | null;
getFinalizedBlock(): IProtoBlock;
getJustifiedBlock(): IProtoBlock;
getBlock(blockRoot: Root): ProtoBlock | null;
getBlockHex(blockRoot: RootHex): ProtoBlock | null;
getFinalizedBlock(): ProtoBlock;
getJustifiedBlock(): ProtoBlock;
/**
* Return `true` if `block_root` is equal to the finalized root, or a known descendant of it.
*/
Expand All @@ -114,26 +114,26 @@ export interface IForkChoice {
/**
* Prune items up to a finalized root.
*/
prune(finalizedRoot: RootHex): IProtoBlock[];
prune(finalizedRoot: RootHex): ProtoBlock[];
setPruneThreshold(threshold: number): void;
/**
* Iterates backwards through ancestor block summaries, starting from a block root
*/
iterateAncestorBlocks(blockRoot: RootHex): IterableIterator<IProtoBlock>;
getAllAncestorBlocks(blockRoot: RootHex): IProtoBlock[];
iterateAncestorBlocks(blockRoot: RootHex): IterableIterator<ProtoBlock>;
getAllAncestorBlocks(blockRoot: RootHex): ProtoBlock[];
/**
* The same to iterateAncestorBlocks but this gets non-ancestor nodes instead of ancestor nodes.
*/
getAllNonAncestorBlocks(blockRoot: RootHex): IProtoBlock[];
getCanonicalBlockAtSlot(slot: Slot): IProtoBlock | null;
getAllNonAncestorBlocks(blockRoot: RootHex): ProtoBlock[];
getCanonicalBlockAtSlot(slot: Slot): ProtoBlock | null;
/**
* Iterates forwards through block summaries, exact order is not guaranteed
*/
forwarditerateAncestorBlocks(): IProtoBlock[];
getBlockSummariesByParentRoot(parentRoot: RootHex): IProtoBlock[];
getBlockSummariesAtSlot(slot: Slot): IProtoBlock[];
forwarditerateAncestorBlocks(): ProtoBlock[];
getBlockSummariesByParentRoot(parentRoot: RootHex): ProtoBlock[];
getBlockSummariesAtSlot(slot: Slot): ProtoBlock[];
/** Returns the distance of common ancestor of nodes to newNode. Returns null if newNode is descendant of prevNode */
getCommonAncestorDistance(prevBlock: IProtoBlock, newBlock: IProtoBlock): number | null;
getCommonAncestorDistance(prevBlock: ProtoBlock, newBlock: ProtoBlock): number | null;
/**
* Optimistic sync validate till validated latest hash, invalidate any decendant branch if invalidated branch decendant provided
*/
Expand All @@ -155,18 +155,18 @@ export type OnBlockPrecachedData = {
executionStatus?: ExecutionStatus;
};

export interface ILatestMessage {
export type LatestMessage = {
epoch: Epoch;
root: RootHex;
}
};

/**
* Used for queuing attestations from the current slot. Only contains the minimum necessary
* information about the attestation.
*/
export interface IQueuedAttestation {
export type QueuedAttestation = {
slot: Slot;
attestingIndices: ValidatorIndex[];
blockRoot: RootHex;
targetEpoch: Epoch;
}
};
10 changes: 2 additions & 8 deletions packages/fork-choice/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
export {ProtoArray} from "./protoArray/protoArray.js";
export {IProtoBlock, IProtoNode, ExecutionStatus} from "./protoArray/interface.js";
export {ProtoBlock, ProtoNode, ExecutionStatus} from "./protoArray/interface.js";

export {ForkChoice, assertValidTerminalPowBlock} from "./forkChoice/forkChoice.js";
export {
IForkChoice,
OnBlockPrecachedData,
PowBlockHex,
ILatestMessage,
IQueuedAttestation,
} from "./forkChoice/interface.js";
export {IForkChoice, OnBlockPrecachedData, PowBlockHex} from "./forkChoice/interface.js";
export {ForkChoiceStore, IForkChoiceStore, CheckpointWithHex} from "./forkChoice/store.js";
export {
InvalidAttestation,
Expand Down
4 changes: 2 additions & 2 deletions packages/fork-choice/src/protoArray/computeDeltas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EffectiveBalanceIncrements} from "@chainsafe/lodestar-beacon-state-transition";
import {IVoteTracker, HEX_ZERO_HASH} from "./interface.js";
import {VoteTracker, HEX_ZERO_HASH} from "./interface.js";
import {ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";

/**
Expand All @@ -13,7 +13,7 @@ import {ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";
*/
export function computeDeltas(
indices: Map<string, number>,
votes: IVoteTracker[],
votes: VoteTracker[],
oldBalances: EffectiveBalanceIncrements,
newBalances: EffectiveBalanceIncrements
): number[] {
Expand Down
8 changes: 4 additions & 4 deletions packages/fork-choice/src/protoArray/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export const HEX_ZERO_HASH = "0x000000000000000000000000000000000000000000000000
/**
* Simplified 'latest message' with previous message
*/
export interface IVoteTracker {
export type VoteTracker = {
currentRoot: RootHex;
nextRoot: RootHex;
nextEpoch: Epoch;
}
};

export enum ExecutionStatus {
Valid = "Valid",
Expand All @@ -28,7 +28,7 @@ type BlockExecution =
* A simplified version of BeaconBlock
*/

export type IProtoBlock = BlockExecution & {
export type ProtoBlock = BlockExecution & {
/**
* The slot is not necessary for ProtoArray,
* it just exists so external components can easily query the block slot.
Expand Down Expand Up @@ -60,7 +60,7 @@ export type IProtoBlock = BlockExecution & {
* A block root with additional metadata required to form a DAG
* with vote weights and best blocks stored as metadata
*/
export type IProtoNode = IProtoBlock & {
export type ProtoNode = ProtoBlock & {
parent?: number;
weight: number;
bestChild?: number;
Expand Down
Loading

0 comments on commit 6cef5d9

Please sign in to comment.