Skip to content

Commit

Permalink
Fix the formatting of blockValue in the validator proposer logs (#5385)
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech authored Apr 19, 2023
1 parent 3ee1d8a commit 0488388
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/validator/src/services/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
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);
});
}
});

0 comments on commit 0488388

Please sign in to comment.