Skip to content

Commit

Permalink
update builder boost factor to bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Jan 7, 2024
1 parent f8960af commit 4eefc31
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 41 deletions.
15 changes: 10 additions & 5 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Slot,
ssz,
UintNum64,
UintBn64,
ValidatorIndex,
RootHex,
StringType,
Expand Down Expand Up @@ -56,7 +57,7 @@ export type ExtraProduceBlockOps = {
// precise value isn't required because super high values will be treated as always builder prefered
// and hence UintNum64 is sufficient. If this param is present, builderSelection will be infered to
// be of maxprofit (unless explicity provided) with this %age boost factor applied to the builder values
builderBoostFactor?: UintNum64;
builderBoostFactor?: UintBn64;
strictFeeRecipientCheck?: boolean;
blindedLocal?: boolean;
};
Expand Down Expand Up @@ -491,7 +492,7 @@ export type ReqTypes = {
skip_randao_verification?: boolean;
fee_recipient?: string;
builder_selection?: string;
builder_boost_factor?: UintNum64;
builder_boost_factor?: string;
strict_fee_recipient_check?: boolean;
blinded_local?: boolean;
};
Expand Down Expand Up @@ -560,7 +561,7 @@ export function getReqSerializers(): ReqSerializers<Api, ReqTypes> {
fee_recipient: opts?.feeRecipient,
skip_randao_verification: skipRandaoVerification,
builder_selection: opts?.builderSelection,
builder_boost_factor: opts?.builderBoostFactor,
builder_boost_factor: opts?.builderBoostFactor?.toString(),
strict_fee_recipient_check: opts?.strictFeeRecipientCheck,
blinded_local: opts?.blindedLocal,
},
Expand All @@ -573,7 +574,7 @@ export function getReqSerializers(): ReqSerializers<Api, ReqTypes> {
{
feeRecipient: query.fee_recipient,
builderSelection: query.builder_selection as BuilderSelection,
builderBoostFactor: query.builder_boost_factor,
builderBoostFactor: parseBuilderBoostFactor(query.builder_boost_factor),
strictFeeRecipientCheck: query.strict_fee_recipient_check,
blindedLocal: query.blinded_local,
},
Expand All @@ -586,7 +587,7 @@ export function getReqSerializers(): ReqSerializers<Api, ReqTypes> {
fee_recipient: Schema.String,
skip_randao_verification: Schema.Boolean,
builder_selection: Schema.String,
builder_boost_factor: Schema.Uint,
builder_boost_factor: Schema.String,
strict_fee_recipient_check: Schema.Boolean,
blinded_local: Schema.Boolean,
},
Expand Down Expand Up @@ -793,3 +794,7 @@ export function getReturnTypes(): ReturnTypes<Api> {
getLiveness: jsonType("snake"),
};
}

function parseBuilderBoostFactor(builderBoostFactorInput?: string | number | bigint): bigint | undefined {
return builderBoostFactorInput !== undefined ? BigInt(builderBoostFactorInput) : undefined;
}
19 changes: 2 additions & 17 deletions packages/api/src/keymanager/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export type Api = {

updateBuilderBoostFactor(
pubkey: string,
builderBoostFactor: number
builderBoostFactor: bigint
): Promise<
ApiClientResponse<
{[HttpStatusCode.OK]: void; [HttpStatusCode.NO_CONTENT]: void},
Expand Down Expand Up @@ -442,7 +442,7 @@ export function getReqSerializers(): ReqSerializers<Api, ReqTypes> {
params: {pubkey},
body: {builder_boost_factor: builderBoostFactor.toString(10)},
}),
parseReq: ({params: {pubkey}, body: {builder_boost_factor}}) => [pubkey, parseBoostFactor(builder_boost_factor)],
parseReq: ({params: {pubkey}, body: {builder_boost_factor}}) => [pubkey, BigInt(builder_boost_factor)],
schema: {
params: {pubkey: Schema.StringRequired},
body: Schema.Object,
Expand Down Expand Up @@ -494,18 +494,3 @@ function parseGasLimit(gasLimitInput: string | number): number {
}
return gasLimit;
}

function parseBoostFactor(builderBoostFactorInput: string | number): number {
if (
(typeof builderBoostFactorInput !== "string" && typeof builderBoostFactorInput !== "number") ||
`${builderBoostFactorInput}`.trim() === ""
) {
throw Error("Not valid Builder Boost Factor");
}

const builderBoostFactor = Number(builderBoostFactorInput);
if (Number.isNaN(builderBoostFactor)) {
throw Error(`Builder Boost Factor is not valid builderBoostFactor=${builderBoostFactor}`);
}
return builderBoostFactor;
}
32 changes: 28 additions & 4 deletions packages/api/test/unit/beacon/testData/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export const testData: GenericServerTestCases<Api> = {
randaoReveal,
graffiti,
undefined,
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined},
{
feeRecipient,
builderSelection: undefined,
strictFeeRecipientCheck: undefined,
blindedLocal: undefined,
builderBoostFactor: 100n,
},
] as unknown as GenericServerTestCases<Api>["produceBlock"]["args"],
res: {data: ssz.phase0.BeaconBlock.defaultValue()},
},
Expand All @@ -60,7 +66,13 @@ export const testData: GenericServerTestCases<Api> = {
randaoReveal,
graffiti,
undefined,
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined},
{
feeRecipient,
builderSelection: undefined,
strictFeeRecipientCheck: undefined,
blindedLocal: undefined,
builderBoostFactor: 100n,
},
] as unknown as GenericServerTestCases<Api>["produceBlockV2"]["args"],
res: {
data: ssz.altair.BeaconBlock.defaultValue(),
Expand All @@ -75,7 +87,13 @@ export const testData: GenericServerTestCases<Api> = {
randaoReveal,
graffiti,
true,
{feeRecipient, builderSelection: undefined, strictFeeRecipientCheck: undefined},
{
feeRecipient,
builderSelection: undefined,
strictFeeRecipientCheck: undefined,
blindedLocal: undefined,
builderBoostFactor: 100n,
},
],
res: {
data: ssz.altair.BeaconBlock.defaultValue(),
Expand All @@ -92,7 +110,13 @@ export const testData: GenericServerTestCases<Api> = {
randaoReveal,
graffiti,
undefined,
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined},
{
feeRecipient,
builderSelection: undefined,
strictFeeRecipientCheck: undefined,
blindedLocal: undefined,
builderBoostFactor: 100n,
},
] as unknown as GenericServerTestCases<Api>["produceBlindedBlock"]["args"],
res: {
data: ssz.bellatrix.BlindedBeaconBlock.defaultValue(),
Expand Down
5 changes: 5 additions & 0 deletions packages/api/test/unit/keymanager/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pubkeyRand = "0x84105a985058fc8740a48bf1ede9d223ef09e8c6b1735ba0a55cf4a9ff
const ethaddressRand = "0xabcf8e0d4e9587369b2301d0790347320302cc09";
const graffitiRandUtf8 = "636861696e736166652f6c6f64657374";
const gasLimitRand = 30_000_000;
const builderBoostFactor = BigInt(100);

export const testData: GenericServerTestCases<Api> = {
listKeys: {
Expand Down Expand Up @@ -99,4 +100,8 @@ export const testData: GenericServerTestCases<Api> = {
args: [pubkeyRand, 1],
res: {data: ssz.phase0.SignedVoluntaryExit.defaultValue()},
},
updateBuilderBoostFactor: {
args: [pubkeyRand, builderBoostFactor],
res: undefined,
},
};
9 changes: 5 additions & 4 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export function getValidatorApi({
// builderSelection will be deprecated and will run in mode MaxProfit if builder is enabled
// and the actual selection will be determined using builderBoostFactor passed by the validator
builderSelection = builderSelection ?? routes.validator.BuilderSelection.MaxProfit;
builderBoostFactor = builderBoostFactor ?? 100;
builderBoostFactor = builderBoostFactor ?? BigInt(100);
const isBuilderEnabled =
ForkSeq[fork] >= ForkSeq.bellatrix &&
chain.executionBuilder !== undefined &&
Expand All @@ -456,7 +456,8 @@ export function getValidatorApi({
slot,
isBuilderEnabled,
strictFeeRecipientCheck,
builderBoostFactor,
// winston logger doesn't like bigint
builderBoostFactor: `${builderBoostFactor}`,
});
// Start calls for building execution and builder blocks
const blindedBlockPromise = isBuilderEnabled
Expand Down Expand Up @@ -550,7 +551,7 @@ export function getValidatorApi({
if (fullBlock && blindedBlock) {
switch (builderSelection) {
case routes.validator.BuilderSelection.MaxProfit: {
if (blockValueEngine >= (blockValueBuilder * BigInt(builderBoostFactor)) / BigInt(100)) {
if (blockValueEngine >= (blockValueBuilder * builderBoostFactor) / BigInt(100)) {
executionPayloadSource = ProducedBlockSource.engine;
} else {
executionPayloadSource = ProducedBlockSource.builder;
Expand All @@ -570,8 +571,8 @@ export function getValidatorApi({
}
logger.verbose(`Selected executionPayloadSource=${executionPayloadSource} block`, {
builderSelection,
builderBoostFactor,
// winston logger doesn't like bigint
builderBoostFactor: `${builderBoostFactor}`,
enginePayloadValue: `${enginePayloadValue}`,
builderPayloadValue: `${builderPayloadValue}`,
consensusBlockValueEngine: `${consensusBlockValueEngine}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/validator/keymanager/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class KeymanagerApi implements Api {
};
}

async updateBuilderBoostFactor(pubkeyHex: string, builderBoostFactor: number): Promise<void> {
async updateBuilderBoostFactor(pubkeyHex: string, builderBoostFactor: bigint): Promise<void> {
this.checkIfProposerWriteEnabled();
this.validator.validatorStore.updateBuilderBoostFactor(pubkeyHex, builderBoostFactor);
this.persistedKeysBackend.writeProposerConfig(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/validator/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type IValidatorCliArgs = AccountValidatorArgs &

builder?: boolean;
"builder.selection"?: string;
"builder.boostFactor"?: number;
"builder.boostFactor"?: bigint;

useProduceBlockV3?: boolean;
broadcastValidation?: string;
Expand Down
14 changes: 7 additions & 7 deletions packages/validator/src/services/validatorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type DefaultProposerConfig = {
builder: {
gasLimit: number;
selection: routes.validator.BuilderSelection;
boostFactor: number;
boostFactor: bigint;
};
};

Expand All @@ -80,7 +80,7 @@ export type ProposerConfig = {
builder?: {
gasLimit?: number;
selection?: routes.validator.BuilderSelection;
boostFactor?: number;
boostFactor?: bigint;
};
};

Expand Down Expand Up @@ -125,7 +125,7 @@ export const defaultOptions = {
defaultGasLimit: 30_000_000,
builderSelection: routes.validator.BuilderSelection.ExecutionOnly,
builderAliasSelection: routes.validator.BuilderSelection.MaxProfit,
builderBoostFactor: 100,
builderBoostFactor: BigInt(100),
// turn it off by default, turn it back on once other clients support v3 api
useProduceBlockV3: false,
// spec asks for gossip validation by default
Expand All @@ -134,7 +134,7 @@ export const defaultOptions = {
blindedLocal: false,
};

const MAX_BUILDER_BOOST_FACTOR = 2 ** 64 - 1;
const MAX_BUILDER_BOOST_FACTOR = BigInt(2 ** 64 - 1);

/**
* Service that sets up and handles validator attester duties.
Expand Down Expand Up @@ -258,7 +258,7 @@ export class ValidatorStore {
delete validatorData["graffiti"];
}

getBuilderSelectionParams(pubkeyHex: PubkeyHex): {selection: routes.validator.BuilderSelection; boostFactor: number} {
getBuilderSelectionParams(pubkeyHex: PubkeyHex): {selection: routes.validator.BuilderSelection; boostFactor: bigint} {
const selection =
(this.validators.get(pubkeyHex)?.builder || {}).selection ?? this.defaultProposerConfig.builder.selection;

Expand All @@ -275,7 +275,7 @@ export class ValidatorStore {
break;

case routes.validator.BuilderSelection.ExecutionOnly:
boostFactor = 0;
boostFactor = BigInt(0);
}

return {selection, boostFactor};
Expand Down Expand Up @@ -311,7 +311,7 @@ export class ValidatorStore {
delete validatorData.builder?.gasLimit;
}

updateBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: number): void {
updateBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: bigint): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/test/unit/services/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("BlockDutiesService", function () {
validatorStore.signBlock.callsFake(async (_, block) => ({message: block, signature: signedBlock.signature}));
validatorStore.getBuilderSelectionParams.returns({
selection: routes.validator.BuilderSelection.MaxProfit,
boostFactor: 100,
boostFactor: BigInt(100),
});
validatorStore.getGraffiti.returns("aaaa");
validatorStore.getFeeRecipient.returns("0x00");
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("BlockDutiesService", function () {
builderSelection: routes.validator.BuilderSelection.MaxProfit,
strictFeeRecipientCheck: false,
blindedLocal: false,
builderBoostFactor: 100,
builderBoostFactor: BigInt(100),
},
],
"wrong produceBlockV3() args"
Expand Down

0 comments on commit 4eefc31

Please sign in to comment.