From 1820535771a64a627e85a0deec25b3ecc94f66fb Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Tue, 13 Feb 2024 10:11:55 +0100 Subject: [PATCH] refactor: reuse utils added in block production race refactor --- .../src/api/impl/validator/index.ts | 18 +++++++++--------- packages/utils/src/format.ts | 10 +++------- packages/utils/src/metrics.ts | 4 +++- packages/utils/src/promise.ts | 5 ----- packages/utils/src/types.ts | 5 +++++ packages/validator/src/services/block.ts | 16 ++++------------ 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/packages/beacon-node/src/api/impl/validator/index.ts b/packages/beacon-node/src/api/impl/validator/index.ts index 0b45540c791e..553146a6cc58 100644 --- a/packages/beacon-node/src/api/impl/validator/index.ts +++ b/packages/beacon-node/src/api/impl/validator/index.ts @@ -187,21 +187,21 @@ export function getValidatorApi({ if (source == null) { return { - executionPayloadValue: prettyWeiToEth(executionValue, true), - consensusBlockValue: prettyWeiToEth(consensusValue, true), - blockTotalValue: prettyWeiToEth(totalValue, true), + executionPayloadValue: prettyWeiToEth(executionValue), + consensusBlockValue: prettyWeiToEth(consensusValue), + blockTotalValue: prettyWeiToEth(totalValue), }; } else if (source === ProducedBlockSource.builder) { return { - builderExecutionPayloadValue: prettyWeiToEth(executionValue, true), - builderConsensusBlockValue: prettyWeiToEth(consensusValue, true), - builderBlockTotalValue: prettyWeiToEth(totalValue, true), + builderExecutionPayloadValue: prettyWeiToEth(executionValue), + builderConsensusBlockValue: prettyWeiToEth(consensusValue), + builderBlockTotalValue: prettyWeiToEth(totalValue), }; } else { return { - engineExecutionPayloadValue: prettyWeiToEth(executionValue, true), - engineConsensusBlockValue: prettyWeiToEth(consensusValue, true), - engineBlockTotalValue: prettyWeiToEth(totalValue, true), + engineExecutionPayloadValue: prettyWeiToEth(executionValue), + engineConsensusBlockValue: prettyWeiToEth(consensusValue), + engineBlockTotalValue: prettyWeiToEth(totalValue), }; } } diff --git a/packages/utils/src/format.ts b/packages/utils/src/format.ts index 0e89748af8f8..8bd8a40273f1 100644 --- a/packages/utils/src/format.ts +++ b/packages/utils/src/format.ts @@ -45,14 +45,10 @@ export function formatBigDecimal(numerator: bigint, denominator: bigint, maxDeci const MAX_DECIMAL_FACTOR = BigInt("100000"); /** - * Format wei as ETH, with up to 5 decimals - * - * if suffix is true, append ' ETH' + * Format wei as ETH, with up to 5 decimals and append ' ETH' */ -export function prettyWeiToEth(wei: bigint, suffix = false): string { - let eth = formatBigDecimal(wei, ETH_TO_WEI, MAX_DECIMAL_FACTOR); - if (suffix) eth += " ETH"; - return eth; +export function prettyWeiToEth(wei: bigint): string { + return `${formatBigDecimal(wei, ETH_TO_WEI, MAX_DECIMAL_FACTOR)} ETH`; } /** diff --git a/packages/utils/src/metrics.ts b/packages/utils/src/metrics.ts index a25518280ee1..f8a965f2800a 100644 --- a/packages/utils/src/metrics.ts +++ b/packages/utils/src/metrics.ts @@ -1,3 +1,5 @@ +import {NonEmptyArray} from "./types.js"; + export type NoLabels = Record; export type LabelsGeneric = Record; export type LabelKeys = Extract; @@ -39,7 +41,7 @@ export interface Counter { export type GaugeConfig = { name: string; help: string; -} & (NoLabels extends Labels ? {labelNames?: never} : {labelNames: [LabelKeys, ...LabelKeys[]]}); +} & (NoLabels extends Labels ? {labelNames?: never} : {labelNames: NonEmptyArray>}); export type HistogramConfig = GaugeConfig & { buckets?: number[]; diff --git a/packages/utils/src/promise.ts b/packages/utils/src/promise.ts index ba63854e9e85..d77e237498cd 100644 --- a/packages/utils/src/promise.ts +++ b/packages/utils/src/promise.ts @@ -74,11 +74,6 @@ export function wrapPromise(promise: PromiseLike): PromiseResult { return result; } -/** - * ArrayToTuple converts an `Array` to `[T, ...T]` - * - * eg: `[1, 2, 3]` from type `number[]` to `[number, number, number]` - */ type ReturnPromiseWithTuple>> = { [Index in keyof ArrayToTuple]: PromiseResult>; }; diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts index cf07331e4456..5b46d65053ef 100644 --- a/packages/utils/src/types.ts +++ b/packages/utils/src/types.ts @@ -17,6 +17,11 @@ export function bnToNum(bn: bigint): number { export type NonEmptyArray = [T, ...T[]]; +/** + * ArrayToTuple converts an `Array` to `[T, ...T]` + * + * eg: `[1, 2, 3]` from type `number[]` to `[number, number, number]` + */ export type ArrayToTuple> = { [Index in keyof Tuple]: Tuple[Index]; }; diff --git a/packages/validator/src/services/block.ts b/packages/validator/src/services/block.ts index a28f7c019d0b..faec8d9e04ee 100644 --- a/packages/validator/src/services/block.ts +++ b/packages/validator/src/services/block.ts @@ -11,7 +11,7 @@ import { } from "@lodestar/types"; import {ChainForkConfig} from "@lodestar/config"; import {ForkPreBlobs, ForkBlobs, ForkSeq, ForkExecution} from "@lodestar/params"; -import {ETH_TO_WEI, extendError, formatBigDecimal, prettyBytes} from "@lodestar/utils"; +import {extendError, prettyBytes, prettyWeiToEth} from "@lodestar/utils"; import {Api, ApiError, routes} from "@lodestar/api"; import {IClock, LoggerVc} from "../util/index.js"; import {PubkeyHex} from "../types.js"; @@ -19,9 +19,6 @@ import {Metrics} from "../metrics.js"; import {ValidatorStore} from "./validatorStore.js"; import {BlockDutiesService, GENESIS_SLOT} from "./blockDuties.js"; -// display upto 5 decimal places -const MAX_DECIMAL_FACTOR = BigInt("100000"); - // The following combination of blocks and blobs can be produced // i) a full block pre deneb // ii) a full block and full blobs post deneb @@ -218,14 +215,9 @@ export class BlockProposingService { const debugLogCtx = { executionPayloadSource: response.executionPayloadSource, executionPayloadBlinded: response.executionPayloadBlinded, - // winston logger doesn't like bigint - executionPayloadValue: `${formatBigDecimal(response.executionPayloadValue, ETH_TO_WEI, MAX_DECIMAL_FACTOR)} ETH`, - consensusBlockValue: `${formatBigDecimal(response.consensusBlockValue, ETH_TO_WEI, MAX_DECIMAL_FACTOR)} ETH`, - totalBlockValue: `${formatBigDecimal( - response.executionPayloadValue + response.consensusBlockValue, - ETH_TO_WEI, - MAX_DECIMAL_FACTOR - )} ETH`, + executionPayloadValue: prettyWeiToEth(response.executionPayloadValue), + consensusBlockValue: prettyWeiToEth(response.consensusBlockValue), + totalBlockValue: prettyWeiToEth(response.executionPayloadValue + response.consensusBlockValue), // TODO PR: should be used in api call instead of adding in log strictFeeRecipientCheck, builderSelection,