Skip to content

Commit d716b2c

Browse files
committed
update tests for error handling
1 parent c2beff8 commit d716b2c

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

yarn-project/validator-client/src/key_store/ha_key_store.test.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,22 @@ describe('HAKeyStore', () => {
179179
);
180180
});
181181

182-
it('should return null when duty was already signed', async () => {
183-
mockHASigner.signWithProtection.mockRejectedValue(
184-
new DutyAlreadySignedError(SlotNumber(100), DutyType.BLOCK_PROPOSAL, 'other-node'),
185-
);
186-
187-
const result = await haKeyStore.signMessageWithAddress(VALIDATOR_ADDRESS, SIGNING_ROOT, context);
188-
189-
expect(result).toBeNull();
190-
});
182+
it('should throw DutyAlreadySignedError when duty was already signed', async () => {
183+
const error = new DutyAlreadySignedError(SlotNumber(100), DutyType.BLOCK_PROPOSAL, 'other-node');
184+
mockHASigner.signWithProtection.mockRejectedValue(error);
191185

192-
it('should return null when slashing protection triggers', async () => {
193-
mockHASigner.signWithProtection.mockRejectedValue(
194-
new SlashingProtectionError(SlotNumber(100), DutyType.BLOCK_PROPOSAL, '0xexisting', '0xattempted'),
186+
await expect(haKeyStore.signMessageWithAddress(VALIDATOR_ADDRESS, SIGNING_ROOT, context)).rejects.toThrow(
187+
DutyAlreadySignedError,
195188
);
189+
});
196190

197-
const result = await haKeyStore.signMessageWithAddress(VALIDATOR_ADDRESS, SIGNING_ROOT, context);
191+
it('should throw SlashingProtectionError when slashing protection triggers', async () => {
192+
const error = new SlashingProtectionError(SlotNumber(100), DutyType.BLOCK_PROPOSAL, '0xexisting', '0xattempted');
193+
mockHASigner.signWithProtection.mockRejectedValue(error);
198194

199-
expect(result).toBeNull();
195+
await expect(haKeyStore.signMessageWithAddress(VALIDATOR_ADDRESS, SIGNING_ROOT, context)).rejects.toThrow(
196+
SlashingProtectionError,
197+
);
200198
});
201199

202200
it('should re-throw unexpected errors', async () => {
@@ -270,24 +268,22 @@ describe('HAKeyStore', () => {
270268
);
271269
});
272270

273-
it('should return null when duty was already signed', async () => {
274-
mockHASigner.signWithProtection.mockRejectedValue(
275-
new DutyAlreadySignedError(SlotNumber(100), DutyType.ATTESTATION, 'other-node'),
276-
);
271+
it('should throw DutyAlreadySignedError when duty was already signed', async () => {
272+
const error = new DutyAlreadySignedError(SlotNumber(100), DutyType.ATTESTATION, 'other-node');
273+
mockHASigner.signWithProtection.mockRejectedValue(error);
277274

278-
const result = await haKeyStore.signTypedDataWithAddress(VALIDATOR_ADDRESS, mockTypedData, context);
279-
280-
expect(result).toBeNull();
281-
});
282-
283-
it('should return null when slashing protection triggers', async () => {
284-
mockHASigner.signWithProtection.mockRejectedValue(
285-
new SlashingProtectionError(SlotNumber(100), DutyType.ATTESTATION, '0xexisting', '0xattempted'),
275+
await expect(haKeyStore.signTypedDataWithAddress(VALIDATOR_ADDRESS, mockTypedData, context)).rejects.toThrow(
276+
DutyAlreadySignedError,
286277
);
278+
});
287279

288-
const result = await haKeyStore.signTypedDataWithAddress(VALIDATOR_ADDRESS, mockTypedData, context);
280+
it('should throw SlashingProtectionError when slashing protection triggers', async () => {
281+
const error = new SlashingProtectionError(SlotNumber(100), DutyType.ATTESTATION, '0xexisting', '0xattempted');
282+
mockHASigner.signWithProtection.mockRejectedValue(error);
289283

290-
expect(result).toBeNull();
284+
await expect(haKeyStore.signTypedDataWithAddress(VALIDATOR_ADDRESS, mockTypedData, context)).rejects.toThrow(
285+
SlashingProtectionError,
286+
);
291287
});
292288

293289
it('should call base key store signTypedDataWithAddress through callback', async () => {

yarn-project/validator-client/src/key_store/ha_key_store.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import type { ExtendedValidatorKeyStore } from './interface.js';
3636
* // Without context - signs directly (no HA protection)
3737
* const sig = await haKeyStore.signMessageWithAddress(addr, msg);
3838
*
39-
* // With context - HA protected, returns null if already signed
39+
* // With context - HA protected, throws DutyAlreadySignedError if already signed
4040
* const result = await haKeyStore.signMessageWithAddress(addr, msg, {
4141
* slot: 100n,
4242
* blockNumber: 50n,
@@ -101,7 +101,8 @@ export class HAKeyStore implements ExtendedValidatorKeyStore {
101101
/**
102102
* Sign typed data with a specific address.
103103
* When context is provided and HA is enabled, coordinates across nodes to prevent double-signing.
104-
* Returns null if the duty was already signed by another node.
104+
* @throws DutyAlreadySignedError if the duty was already signed by another node
105+
* @throws SlashingProtectionError if attempting to sign different data for the same slot
105106
*/
106107
async signTypedDataWithAddress(
107108
address: EthAddress,
@@ -133,7 +134,8 @@ export class HAKeyStore implements ExtendedValidatorKeyStore {
133134
/**
134135
* Sign a message with a specific address.
135136
* When context is provided and HA is enabled, coordinates across nodes to prevent double-signing.
136-
* Returns null if the duty was already signed by another node.
137+
* @throws DutyAlreadySignedError if the duty was already signed by another node
138+
* @throws SlashingProtectionError if attempting to sign different data for the same slot
137139
*/
138140
async signMessageWithAddress(address: EthAddress, message: Buffer32, context?: SigningContext): Promise<Signature> {
139141
// No context or HA disabled - sign directly
@@ -194,8 +196,9 @@ export class HAKeyStore implements ExtendedValidatorKeyStore {
194196
}
195197

196198
/**
197-
* Handle signing errors from the HA signer.
198-
* Returns null for expected HA errors (already signed), re-throws unexpected errors.
199+
* Log signing errors from the HA signer.
200+
* Logs expected HA errors (already signed) at appropriate levels.
201+
* Re-throws unexpected errors.
199202
*/
200203
private logSigningError(error: unknown, context: SigningContext) {
201204
if (error instanceof DutyAlreadySignedError) {

0 commit comments

Comments
 (0)