Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the formatting of blockValue in the validator proposer logs #5385

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/validator/src/services/block.ts
Original file line number Diff line number Diff line change
@@ -17,10 +17,14 @@ import {Api, ApiError, ServerApi} from "@lodestar/api";
import {IClock, LoggerVc} from "../util/index.js";
import {PubkeyHex} from "../types.js";
import {Metrics} from "../metrics.js";
import {formatBigDecimal} from "../util/format.js";
import {ValidatorStore, BuilderSelection} from "./validatorStore.js";
import {BlockDutiesService, GENESIS_SLOT} from "./blockDuties.js";

const ETH_TO_WEI = BigInt("1000000000000000000");
// display upto 5 decimal places
const MAX_DECIMAL_FACTOR = BigInt("100000");

/**
* Cutoff time to wait for execution and builder block production apis to resolve
* Post this time, race execution and builder to pick whatever resolves first
@@ -243,7 +247,7 @@ export class BlockProposingService {
const debugLogCtx = {
source: source,
// winston logger doesn't like bigint
"blockValue(eth)": `${fullOrBlindedBlock.blockValue / ETH_TO_WEI}`,
blockValue: `${formatBigDecimal(fullOrBlindedBlock.blockValue, ETH_TO_WEI, MAX_DECIMAL_FACTOR)} ETH`,
};
const blockFeeRecipient = (fullOrBlindedBlock.data as bellatrix.BeaconBlock).body.executionPayload?.feeRecipient;
const feeRecipient = blockFeeRecipient !== undefined ? toHexString(blockFeeRecipient) : undefined;
6 changes: 6 additions & 0 deletions packages/validator/src/util/format.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function isValidatePubkeyHex(pubkeyHex: string): boolean {
return /^0x[0-9a-fA-F]{96}$/.test(pubkeyHex);
}

export function formatBigDecimal(numerator: bigint, denominator: bigint, maxDecimalFactor: bigint): string {
const full = numerator / denominator;
const fraction = ((numerator - full * denominator) * maxDecimalFactor) / denominator;
return `${full}.${fraction}`;
}
17 changes: 17 additions & 0 deletions packages/validator/test/unit/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {expect} from "chai";
import {formatBigDecimal} from "../../../src/util/format.js";

describe("util / formatBigDecimal", function () {
const testCases: [bigint, bigint, bigint, string][] = [
[BigInt("103797739275696858"), BigInt("1000000000000000000"), BigInt("100000"), "0.10379"],
[BigInt("103797739275696858"), BigInt("1000000000000000000"), BigInt("1000"), "0.103"],
[BigInt("111103797739275696858"), BigInt("1000000000000000000"), BigInt("100000"), "111.10379"],
[BigInt("111103797739275696858"), BigInt("1000000000000000000"), BigInt("1000"), "111.103"],
[BigInt("1037977392756"), BigInt("1000000000000000000"), BigInt("100000"), "0.0"],
];
for (const [numerator, denominator, decimalFactor, expectedString] of testCases) {
it(`format ${numerator} / ${denominator} correctly to ${expectedString}`, () => {
expect(formatBigDecimal(numerator, denominator, decimalFactor)).to.be.equal(expectedString);
});
}
});