Skip to content

Commit

Permalink
add keymanager endpoint and update the test
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Jan 4, 2024
1 parent cacc59a commit d6f0e7f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/api/src/keymanager/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ export type Api = {
>
>;

updateBuilderBoostFactor(
pubkey: string,
builderBoostFactor: number
): Promise<
ApiClientResponse<
{[HttpStatusCode.OK]: void; [HttpStatusCode.NO_CONTENT]: void},
HttpStatusCode.UNAUTHORIZED | HttpStatusCode.FORBIDDEN | HttpStatusCode.NOT_FOUND
>
>;

/**
* Create a signed voluntary exit message for an active validator, identified by a public key known to the validator
* client. This endpoint returns a `SignedVoluntaryExit` object, which can be used to initiate voluntary exit via the
Expand Down Expand Up @@ -290,6 +300,8 @@ export const routesData: RoutesData<Api> = {
setGasLimit: {url: "/eth/v1/validator/{pubkey}/gas_limit", method: "POST", statusOk: 202},
deleteGasLimit: {url: "/eth/v1/validator/{pubkey}/gas_limit", method: "DELETE", statusOk: 204},

updateBuilderBoostFactor: {url: "/eth/v1/validator/{pubkey}/builder_boost_factor", method: "POST", statusOk: 202},

signVoluntaryExit: {url: "/eth/v1/validator/{pubkey}/voluntary_exit", method: "POST"},
};

Expand Down Expand Up @@ -326,6 +338,8 @@ export type ReqTypes = {
setGasLimit: {params: {pubkey: string}; body: {gas_limit: string}};
deleteGasLimit: {params: {pubkey: string}};

updateBuilderBoostFactor: {params: {pubkey: string}; body: {builder_boost_factor: string}};

signVoluntaryExit: {params: {pubkey: string}; query: {epoch?: number}};
};

Expand Down Expand Up @@ -423,6 +437,17 @@ export function getReqSerializers(): ReqSerializers<Api, ReqTypes> {
params: {pubkey: Schema.StringRequired},
},
},
updateBuilderBoostFactor: {
writeReq: (pubkey, builderBoostFactor) => ({
params: {pubkey},
body: {builder_boost_factor: builderBoostFactor.toString(10)},
}),
parseReq: ({params: {pubkey}, body: {builder_boost_factor}}) => [pubkey, parseBoostFactor(builder_boost_factor)],
schema: {
params: {pubkey: Schema.StringRequired},
body: Schema.Object,
},
},
signVoluntaryExit: {
writeReq: (pubkey, epoch) => ({params: {pubkey}, query: epoch !== undefined ? {epoch} : {}}),
parseReq: ({params: {pubkey}, query: {epoch}}) => [pubkey, epoch],
Expand Down Expand Up @@ -469,3 +494,18 @@ 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;
}
9 changes: 9 additions & 0 deletions packages/cli/src/cmds/validator/keymanager/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ export class KeymanagerApi implements Api {
};
}

async updateBuilderBoostFactor(pubkeyHex: string, builderBoostFactor: number): Promise<void> {
this.checkIfProposerWriteEnabled();
this.validator.validatorStore.updateBuilderBoostFactor(pubkeyHex, builderBoostFactor);
this.persistedKeysBackend.writeProposerConfig(
pubkeyHex,
this.validator.validatorStore.getProposerConfig(pubkeyHex)
);
}

/**
* Create and sign a voluntary exit message for an active validator
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/validator/src/services/validatorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ export class ValidatorStore {
delete validatorData.builder?.gasLimit;
}

updateBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: number): void {
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
validatorData.builder = {...validatorData.builder, boostFactor};
}

/** Return true if `index` is active part of this validator client */
hasValidatorIndex(index: ValidatorIndex): boolean {
return this.indicesService.index2pubkey.has(index);
Expand Down
26 changes: 26 additions & 0 deletions packages/validator/test/unit/services/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe("BlockDutiesService", function () {
const signedBlock = ssz.phase0.SignedBeaconBlock.defaultValue();
validatorStore.signRandao.resolves(signedBlock.message.body.randaoReveal);
validatorStore.signBlock.callsFake(async (_, block) => ({message: block, signature: signedBlock.signature}));
validatorStore.getBuilderSelectionParams.returns({
selection: routes.validator.BuilderSelection.MaxProfit,
boostFactor: 100,
});
validatorStore.getGraffiti.returns("aaaa");
validatorStore.getFeeRecipient.returns("0x00");
validatorStore.strictFeeRecipientCheck.returns(false);

api.validator.produceBlockV3.resolves({
response: {
data: signedBlock.message,
Expand Down Expand Up @@ -86,6 +94,24 @@ describe("BlockDutiesService", function () {
[signedBlock, {broadcastValidation: routes.beacon.BroadcastValidation.consensus}],
"wrong publishBlock() args"
);

// ProduceBlockV3 is called with all correct arguments
expect(api.validator.produceBlockV3.getCall(0).args).to.deep.equal(
[
1,
signedBlock.message.body.randaoReveal,
"aaaa",
false,
{
feeRecipient: "0x00",
builderSelection: routes.validator.BuilderSelection.MaxProfit,
strictFeeRecipientCheck: false,
blindedLocal: false,
builderBoostFactor: 100,
},
],
"wrong produceBlockV3() args"
);
});

it("Should produce, sign, and publish a blinded block", async function () {
Expand Down

0 comments on commit d6f0e7f

Please sign in to comment.