Skip to content

Commit

Permalink
fix: consistently validate pubkey and throw 404 if not found (#7214)
Browse files Browse the repository at this point in the history
* Throw error if pubkey is unknown when getting graffiti

* Consistently validate pubkey and throw 404 if not found
  • Loading branch information
nflaig authored Nov 3, 2024
1 parent 06b4c2d commit 07d1145
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/cli/src/cmds/validator/keymanager/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,28 @@ export class KeymanagerApi implements Api {
}

async listFeeRecipient({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["listFeeRecipient"]> {
this.assertValidKnownPubkey(pubkey);
return {data: {pubkey, ethaddress: this.validator.validatorStore.getFeeRecipient(pubkey)}};
}

async setFeeRecipient({pubkey, ethaddress}: FeeRecipientData): ReturnType<Api["setFeeRecipient"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.setFeeRecipient(pubkey, parseFeeRecipient(ethaddress));
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 202};
}

async deleteFeeRecipient({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["deleteFeeRecipient"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.deleteFeeRecipient(pubkey);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 204};
}

async getGraffiti({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["getGraffiti"]> {
this.assertValidKnownPubkey(pubkey);
const graffiti = this.validator.validatorStore.getGraffiti(pubkey);
if (graffiti === undefined) {
throw new ApiError(404, `No graffiti for pubkey ${pubkey}`);
Expand All @@ -69,32 +73,37 @@ export class KeymanagerApi implements Api {

async setGraffiti({pubkey, graffiti}: GraffitiData): ReturnType<Api["setGraffiti"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.setGraffiti(pubkey, graffiti);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 202};
}

async deleteGraffiti({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["deleteGraffiti"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.deleteGraffiti(pubkey);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 204};
}

async getGasLimit({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["getGasLimit"]> {
this.assertValidKnownPubkey(pubkey);
const gasLimit = this.validator.validatorStore.getGasLimit(pubkey);
return {data: {pubkey, gasLimit}};
}

async setGasLimit({pubkey, gasLimit}: GasLimitData): ReturnType<Api["setGasLimit"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.setGasLimit(pubkey, gasLimit);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 202};
}

async deleteGasLimit({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["deleteGasLimit"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.deleteGasLimit(pubkey);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 204};
Expand Down Expand Up @@ -344,6 +353,7 @@ export class KeymanagerApi implements Api {
}

async getBuilderBoostFactor({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["getBuilderBoostFactor"]> {
this.assertValidKnownPubkey(pubkey);
const builderBoostFactor = this.validator.validatorStore.getBuilderBoostFactor(pubkey);
return {data: {pubkey, builderBoostFactor}};
}
Expand All @@ -353,6 +363,7 @@ export class KeymanagerApi implements Api {
builderBoostFactor,
}: BuilderBoostFactorData): ReturnType<Api["setBuilderBoostFactor"]> {
this.checkIfProposerWriteEnabled();
this.assertValidKnownPubkey(pubkey);
this.validator.validatorStore.setBuilderBoostFactor(pubkey, builderBoostFactor);
this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey));
return {status: 202};
Expand All @@ -366,6 +377,8 @@ export class KeymanagerApi implements Api {
}

async getProposerConfig({pubkey}: {pubkey: PubkeyHex}): ReturnType<Api["getProposerConfig"]> {
this.assertValidKnownPubkey(pubkey);

const config = this.validator.validatorStore.getProposerConfig(pubkey);

const data: ProposerConfigResponse = {
Expand All @@ -383,10 +396,18 @@ export class KeymanagerApi implements Api {
}

async signVoluntaryExit({pubkey, epoch}: {pubkey: PubkeyHex; epoch?: Epoch}): ReturnType<Api["signVoluntaryExit"]> {
this.assertValidKnownPubkey(pubkey);
return {data: await this.validator.signVoluntaryExit(pubkey, epoch)};
}

private assertValidKnownPubkey(pubkey: PubkeyHex): void {
if (!isValidatePubkeyHex(pubkey)) {
throw new ApiError(400, `Invalid pubkey ${pubkey}`);
}
return {data: await this.validator.signVoluntaryExit(pubkey, epoch)};

if (!this.validator.validatorStore.hasVotingPubkey(pubkey)) {
throw new ApiError(404, `Validator pubkey ${pubkey} not known`);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/validator/src/services/validatorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ export class ValidatorStore {
}

getGraffiti(pubkeyHex: PubkeyHex): string | undefined {
return this.validators.get(pubkeyHex)?.graffiti ?? this.defaultProposerConfig.graffiti;
const validatorData = this.validators.get(pubkeyHex);
if (validatorData === undefined) {
throw Error(`Validator pubkey ${pubkeyHex} not known`);
}
return validatorData.graffiti ?? this.defaultProposerConfig.graffiti;
}

setGraffiti(pubkeyHex: PubkeyHex, graffiti: string): void {
Expand Down

0 comments on commit 07d1145

Please sign in to comment.