From bca1d6376537535c2eeff0443a8c92ec0307b11a Mon Sep 17 00:00:00 2001 From: harkamal Date: Mon, 8 Jan 2024 17:34:15 +0530 Subject: [PATCH] fix the keymanager routes --- packages/api/src/keymanager/routes.ts | 52 +++++++++++++++++-- packages/api/test/unit/keymanager/testData.ts | 14 +++-- .../cli/src/cmds/validator/keymanager/impl.ts | 18 ++++++- .../validator/src/services/validatorStore.ts | 18 ++++++- 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/packages/api/src/keymanager/routes.ts b/packages/api/src/keymanager/routes.ts index e43f3b6ffee7..48f928e86100 100644 --- a/packages/api/src/keymanager/routes.ts +++ b/packages/api/src/keymanager/routes.ts @@ -72,6 +72,10 @@ export type GasLimitData = { pubkey: string; gasLimit: number; }; +export type BuilderBoostFactorData = { + pubkey: string; + builderBoostFactor: bigint; +}; export type SignerDefinition = { pubkey: PubkeyHex; @@ -247,7 +251,10 @@ export type Api = { > >; - updateBuilderBoostFactor( + getBuilderBoostFactor( + pubkey: string + ): Promise>; + setBuilderBoostFactor( pubkey: string, builderBoostFactor: bigint ): Promise< @@ -256,6 +263,14 @@ export type Api = { HttpStatusCode.UNAUTHORIZED | HttpStatusCode.FORBIDDEN | HttpStatusCode.NOT_FOUND > >; + deleteBuilderBoostFactor( + pubkey: string + ): 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 @@ -300,7 +315,9 @@ export const routesData: RoutesData = { 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}, + getBuilderBoostFactor: {url: "/eth/v1/validator/{pubkey}/builder_boost_factor", method: "GET"}, + setBuilderBoostFactor: {url: "/eth/v1/validator/{pubkey}/builder_boost_factor", method: "POST", statusOk: 202}, + deleteBuilderBoostFactor: {url: "/eth/v1/validator/{pubkey}/builder_boost_factor", method: "DELETE", statusOk: 204}, signVoluntaryExit: {url: "/eth/v1/validator/{pubkey}/voluntary_exit", method: "POST"}, }; @@ -338,7 +355,9 @@ export type ReqTypes = { setGasLimit: {params: {pubkey: string}; body: {gas_limit: string}}; deleteGasLimit: {params: {pubkey: string}}; - updateBuilderBoostFactor: {params: {pubkey: string}; body: {builder_boost_factor: string}}; + getBuilderBoostFactor: {params: {pubkey: string}}; + setBuilderBoostFactor: {params: {pubkey: string}; body: {builder_boost_factor: string}}; + deleteBuilderBoostFactor: {params: {pubkey: string}}; signVoluntaryExit: {params: {pubkey: string}; query: {epoch?: number}}; }; @@ -437,7 +456,15 @@ export function getReqSerializers(): ReqSerializers { params: {pubkey: Schema.StringRequired}, }, }, - updateBuilderBoostFactor: { + + getBuilderBoostFactor: { + writeReq: (pubkey) => ({params: {pubkey}}), + parseReq: ({params: {pubkey}}) => [pubkey], + schema: { + params: {pubkey: Schema.StringRequired}, + }, + }, + setBuilderBoostFactor: { writeReq: (pubkey, builderBoostFactor) => ({ params: {pubkey}, body: {builder_boost_factor: builderBoostFactor.toString(10)}, @@ -448,6 +475,14 @@ export function getReqSerializers(): ReqSerializers { body: Schema.Object, }, }, + deleteBuilderBoostFactor: { + writeReq: (pubkey) => ({params: {pubkey}}), + parseReq: ({params: {pubkey}}) => [pubkey], + schema: { + params: {pubkey: Schema.StringRequired}, + }, + }, + signVoluntaryExit: { writeReq: (pubkey, epoch) => ({params: {pubkey}, query: epoch !== undefined ? {epoch} : {}}), parseReq: ({params: {pubkey}, query: {epoch}}) => [pubkey, epoch], @@ -480,6 +515,15 @@ export function getReturnTypes(): ReturnTypes { {jsonCase: "eth2"} ) ), + getBuilderBoostFactor: ContainerData( + new ContainerType( + { + pubkey: stringType, + builderBoostFactor: ssz.UintBn64, + }, + {jsonCase: "eth2"} + ) + ), signVoluntaryExit: ContainerData(ssz.phase0.SignedVoluntaryExit), }; } diff --git a/packages/api/test/unit/keymanager/testData.ts b/packages/api/test/unit/keymanager/testData.ts index c63d974ab5c4..2c66610c8733 100644 --- a/packages/api/test/unit/keymanager/testData.ts +++ b/packages/api/test/unit/keymanager/testData.ts @@ -13,7 +13,7 @@ const pubkeyRand = "0x84105a985058fc8740a48bf1ede9d223ef09e8c6b1735ba0a55cf4a9ff const ethaddressRand = "0xabcf8e0d4e9587369b2301d0790347320302cc09"; const graffitiRandUtf8 = "636861696e736166652f6c6f64657374"; const gasLimitRand = 30_000_000; -const builderBoostFactor = BigInt(100); +const builderBoostFactorRand = BigInt(100); export const testData: GenericServerTestCases = { listKeys: { @@ -100,8 +100,16 @@ export const testData: GenericServerTestCases = { args: [pubkeyRand, 1], res: {data: ssz.phase0.SignedVoluntaryExit.defaultValue()}, }, - updateBuilderBoostFactor: { - args: [pubkeyRand, builderBoostFactor], + getBuilderBoostFactor: { + args: [pubkeyRand], + res: {data: {pubkey: pubkeyRand, builderBoostFactor: builderBoostFactorRand}}, + }, + setBuilderBoostFactor: { + args: [pubkeyRand, builderBoostFactorRand], + res: undefined, + }, + deleteBuilderBoostFactor: { + args: [pubkeyRand], res: undefined, }, }; diff --git a/packages/cli/src/cmds/validator/keymanager/impl.ts b/packages/cli/src/cmds/validator/keymanager/impl.ts index 0f94943a706c..4628c96285df 100644 --- a/packages/cli/src/cmds/validator/keymanager/impl.ts +++ b/packages/cli/src/cmds/validator/keymanager/impl.ts @@ -390,9 +390,23 @@ export class KeymanagerApi implements Api { }; } - async updateBuilderBoostFactor(pubkeyHex: string, builderBoostFactor: bigint): Promise { + async getBuilderBoostFactor(pubkeyHex: string): ReturnType { + const builderBoostFactor = this.validator.validatorStore.getBuilderBoostFactor(pubkeyHex); + return {data: {pubkey: pubkeyHex, builderBoostFactor}}; + } + + async setBuilderBoostFactor(pubkeyHex: string, builderBoostFactor: bigint): Promise { + this.checkIfProposerWriteEnabled(); + this.validator.validatorStore.setBuilderBoostFactor(pubkeyHex, builderBoostFactor); + this.persistedKeysBackend.writeProposerConfig( + pubkeyHex, + this.validator.validatorStore.getProposerConfig(pubkeyHex) + ); + } + + async deleteBuilderBoostFactor(pubkeyHex: string): Promise { this.checkIfProposerWriteEnabled(); - this.validator.validatorStore.updateBuilderBoostFactor(pubkeyHex, builderBoostFactor); + this.validator.validatorStore.deleteBuilderBoostFactor(pubkeyHex); this.persistedKeysBackend.writeProposerConfig( pubkeyHex, this.validator.validatorStore.getProposerConfig(pubkeyHex) diff --git a/packages/validator/src/services/validatorStore.ts b/packages/validator/src/services/validatorStore.ts index b00ff562bf6d..809ca0c8a7c6 100644 --- a/packages/validator/src/services/validatorStore.ts +++ b/packages/validator/src/services/validatorStore.ts @@ -316,7 +316,15 @@ export class ValidatorStore { delete validatorData.builder?.gasLimit; } - updateBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: bigint): void { + getBuilderBoostFactor(pubkeyHex: PubkeyHex): bigint { + const validatorData = this.validators.get(pubkeyHex); + if (validatorData === undefined) { + throw Error(`Validator pubkey ${pubkeyHex} not known`); + } + return validatorData?.builder?.boostFactor ?? this.defaultProposerConfig.builder.boostFactor; + } + + setBuilderBoostFactor(pubkeyHex: PubkeyHex, boostFactor: bigint): void { if (boostFactor > MAX_BUILDER_BOOST_FACTOR) { throw Error(`Invalid builderBoostFactor=${boostFactor} > MAX_BUILDER_BOOST_FACTOR`); } @@ -328,6 +336,14 @@ export class ValidatorStore { validatorData.builder = {...validatorData.builder, boostFactor}; } + deleteBuilderBoostFactor(pubkeyHex: PubkeyHex): void { + const validatorData = this.validators.get(pubkeyHex); + if (validatorData === undefined) { + throw Error(`Validator pubkey ${pubkeyHex} not known`); + } + delete validatorData.builder?.boostFactor; + } + /** Return true if `index` is active part of this validator client */ hasValidatorIndex(index: ValidatorIndex): boolean { return this.indicesService.index2pubkey.has(index);