Skip to content

Commit

Permalink
fix: block value in produceBlockV3 should be in wei (#6286)
Browse files Browse the repository at this point in the history
This is the Wei
  • Loading branch information
ensi321 authored Jan 16, 2024
1 parent e4cd53a commit 4ec413c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
StringType,
SubcommitteeIndex,
Wei,
Gwei,
ProducedBlockSource,
} from "@lodestar/types";
import {ApiClientResponse} from "../../interfaces.js";
Expand Down Expand Up @@ -59,11 +58,11 @@ export type ExtraProduceBlockOps = {
blindedLocal?: boolean;
};

export type ProduceBlockOrContentsRes = {executionPayloadValue: Wei; consensusBlockValue: Gwei} & (
export type ProduceBlockOrContentsRes = {executionPayloadValue: Wei; consensusBlockValue: Wei} & (
| {data: allForks.BeaconBlock; version: ForkPreBlobs}
| {data: allForks.BlockContents; version: ForkBlobs}
);
export type ProduceBlindedBlockRes = {executionPayloadValue: Wei; consensusBlockValue: Gwei} & {
export type ProduceBlindedBlockRes = {executionPayloadValue: Wei; consensusBlockValue: Wei} & {
data: allForks.BlindedBeaconBlock;
version: ForkExecution;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function WithExecutionOptimistic<T extends {data: unknown}>(
}

/**
* SSZ factory helper to wrap an existing type with `{executionPayloadValue: Wei, consensusBlockValue: GWei}`
* SSZ factory helper to wrap an existing type with `{executionPayloadValue: Wei, consensusBlockValue: Wei}`
*/
export function WithBlockValues<T extends {data: unknown}>(
type: TypeJson<T>
Expand Down
6 changes: 3 additions & 3 deletions packages/api/test/unit/beacon/testData/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const testData: GenericServerTestCases<Api> = {
data: ssz.altair.BeaconBlock.defaultValue(),
version: ForkName.altair,
executionPayloadValue: ssz.Wei.defaultValue(),
consensusBlockValue: ssz.Gwei.defaultValue(),
consensusBlockValue: ssz.Wei.defaultValue(),
},
},
produceBlockV3: {
Expand All @@ -99,7 +99,7 @@ export const testData: GenericServerTestCases<Api> = {
data: ssz.altair.BeaconBlock.defaultValue(),
version: ForkName.altair,
executionPayloadValue: ssz.Wei.defaultValue(),
consensusBlockValue: ssz.Gwei.defaultValue(),
consensusBlockValue: ssz.Wei.defaultValue(),
executionPayloadBlinded: false,
executionPayloadSource: ProducedBlockSource.engine,
},
Expand All @@ -122,7 +122,7 @@ export const testData: GenericServerTestCases<Api> = {
data: ssz.bellatrix.BlindedBeaconBlock.defaultValue(),
version: ForkName.bellatrix,
executionPayloadValue: ssz.Wei.defaultValue(),
consensusBlockValue: ssz.Gwei.defaultValue(),
consensusBlockValue: ssz.Wei.defaultValue(),
},
},
produceAttestationData: {
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
phase0,
} from "@lodestar/types";
import {ExecutionStatus} from "@lodestar/fork-choice";
import {toHex, racePromisesWithCutoff, RaceEvent, gweiToWei} from "@lodestar/utils";
import {toHex, racePromisesWithCutoff, RaceEvent} from "@lodestar/utils";
import {AttestationError, AttestationErrorCode, GossipAction, SyncCommitteeError} from "../../../chain/errors/index.js";
import {validateApiAggregateAndProof} from "../../../chain/validation/index.js";
import {ZERO_HASH} from "../../../constants/index.js";
Expand Down Expand Up @@ -573,8 +573,8 @@ export function getValidatorApi({
const consensusBlockValueBuilder = blindedBlock?.consensusBlockValue ?? BigInt(0);
const consensusBlockValueEngine = fullBlock?.consensusBlockValue ?? BigInt(0);

const blockValueBuilder = builderPayloadValue + gweiToWei(consensusBlockValueBuilder); // Total block value is in wei
const blockValueEngine = enginePayloadValue + gweiToWei(consensusBlockValueEngine); // Total block value is in wei
const blockValueBuilder = builderPayloadValue + consensusBlockValueBuilder; // Total block value is in wei
const blockValueEngine = enginePayloadValue + consensusBlockValueEngine; // Total block value is in wei

let executionPayloadSource: ProducedBlockSource | null = null;
const shouldOverrideBuilder = fullBlock?.shouldOverrideBuilder ?? false;
Expand Down
11 changes: 5 additions & 6 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ import {
Wei,
bellatrix,
isBlindedBeaconBlock,
Gwei,
} from "@lodestar/types";
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {Logger, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
import {Logger, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";

import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
Expand Down Expand Up @@ -498,7 +497,7 @@ export class BeaconChain implements IBeaconChain {
produceBlock(blockAttributes: BlockAttributes & {commonBlockBody?: CommonBlockBody}): Promise<{
block: allForks.BeaconBlock;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
consensusBlockValue: Wei;
shouldOverrideBuilder?: boolean;
}> {
return this.produceBlockWrapper<BlockType.Full>(BlockType.Full, blockAttributes);
Expand All @@ -507,7 +506,7 @@ export class BeaconChain implements IBeaconChain {
produceBlindedBlock(blockAttributes: BlockAttributes & {commonBlockBody?: CommonBlockBody}): Promise<{
block: allForks.BlindedBeaconBlock;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
consensusBlockValue: Wei;
}> {
return this.produceBlockWrapper<BlockType.Blinded>(BlockType.Blinded, blockAttributes);
}
Expand All @@ -518,7 +517,7 @@ export class BeaconChain implements IBeaconChain {
): Promise<{
block: AssembledBlockType<T>;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
consensusBlockValue: Wei;
shouldOverrideBuilder?: boolean;
}> {
const head = this.forkChoice.getHead();
Expand Down Expand Up @@ -598,7 +597,7 @@ export class BeaconChain implements IBeaconChain {
this.metrics?.blockProductionCaches.producedContentsCache.set(this.producedContentsCache.size);
}

return {block, executionPayloadValue, consensusBlockValue: proposerReward, shouldOverrideBuilder};
return {block, executionPayloadValue, consensusBlockValue: gweiToWei(proposerReward), shouldOverrideBuilder};
}

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ValidatorIndex,
deneb,
Wei,
Gwei,
capella,
altair,
} from "@lodestar/types";
Expand Down Expand Up @@ -162,13 +161,13 @@ export interface IBeaconChain {
produceBlock(blockAttributes: BlockAttributes & {commonBlockBody?: CommonBlockBody}): Promise<{
block: allForks.BeaconBlock;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
consensusBlockValue: Wei;
shouldOverrideBuilder?: boolean;
}>;
produceBlindedBlock(blockAttributes: BlockAttributes & {commonBlockBody?: CommonBlockBody}): Promise<{
block: allForks.BlindedBeaconBlock;
executionPayloadValue: Wei;
consensusBlockValue: Gwei;
consensusBlockValue: Wei;
}>;

/** Process a block until complete */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("api/validator - produceBlockV2", function () {

const fullBlock = ssz.bellatrix.BeaconBlock.defaultValue();
const executionPayloadValue = ssz.Wei.defaultValue();
const consensusBlockValue = ssz.Gwei.defaultValue();
const consensusBlockValue = ssz.Wei.defaultValue();

const currentSlot = 100000;
vi.spyOn(server.chainStub.clock, "currentSlot", "get").mockReturnValue(currentSlot);
Expand Down
1 change: 1 addition & 0 deletions packages/state-transition/src/cache/rewardCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* A simple data structure to store rewards payable to block proposer in the memory.
* Rewards are updated throughout the state transition
* Should only hold info for one state transition
* All values are in Gwei
*/
export type RewardCache = {
attestations: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/src/services/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {ForkPreBlobs, ForkBlobs, ForkSeq, ForkExecution} from "@lodestar/params";
import {ETH_TO_GWEI, ETH_TO_WEI, extendError, gweiToWei, prettyBytes} from "@lodestar/utils";
import {ETH_TO_WEI, extendError, gweiToWei, prettyBytes} from "@lodestar/utils";
import {Api, ApiError, routes} from "@lodestar/api";
import {IClock, LoggerVc} from "../util/index.js";
import {PubkeyHex} from "../types.js";
Expand Down Expand Up @@ -221,7 +221,7 @@ export class BlockProposingService {
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_GWEI, MAX_DECIMAL_FACTOR)} ETH`,
consensusBlockValue: `${formatBigDecimal(response.consensusBlockValue, ETH_TO_WEI, MAX_DECIMAL_FACTOR)} ETH`,
totalBlockValue: `${formatBigDecimal(
response.executionPayloadValue + gweiToWei(response.consensusBlockValue),
ETH_TO_WEI,
Expand Down

0 comments on commit 4ec413c

Please sign in to comment.