From 6af413702b23c14bf3ba325c3bbba87056922eaa Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Wed, 21 Jun 2023 14:05:24 +0200 Subject: [PATCH] Align the registerValidatorKeys method to LIP - align registerValidatorKeys method to lip - update unit tests Update unit test --- .../modules/pos/commands/register_validator.ts | 6 +----- framework/src/modules/pos/module.ts | 5 +---- framework/src/modules/pos/types.ts | 2 +- framework/src/modules/validators/method.ts | 4 +--- .../pos/commands/validator_registration.spec.ts | 2 +- framework/test/unit/modules/pos/module.spec.ts | 11 ++++++----- .../test/unit/modules/validators/method.spec.ts | 17 ++++++++--------- 7 files changed, 19 insertions(+), 28 deletions(-) diff --git a/framework/src/modules/pos/commands/register_validator.ts b/framework/src/modules/pos/commands/register_validator.ts index 79dad4e79c7..b851af34b80 100644 --- a/framework/src/modules/pos/commands/register_validator.ts +++ b/framework/src/modules/pos/commands/register_validator.ts @@ -91,7 +91,7 @@ export class RegisterValidatorCommand extends BaseCommand { } = context; const methodContext = context.getMethodContext(); - const isRegistered = await this._validatorsMethod.registerValidatorKeys( + await this._validatorsMethod.registerValidatorKeys( methodContext, transaction.senderAddress, blsKey, @@ -99,10 +99,6 @@ export class RegisterValidatorCommand extends BaseCommand { proofOfPossession, ); - if (!isRegistered) { - throw new Error('Failed to register validator keys'); - } - this._feeMethod.payFee(context, this._validatorRegistrationFee); const validatorSubstore = this.stores.get(ValidatorStore); diff --git a/framework/src/modules/pos/module.ts b/framework/src/modules/pos/module.ts index 341b26a618a..af4422b50ec 100644 --- a/framework/src/modules/pos/module.ts +++ b/framework/src/modules/pos/module.ts @@ -498,16 +498,13 @@ export class PoSModule extends BaseModule { } } else { for (const posValidator of genesisStore.validators) { - const valid = await this._validatorsMethod.registerValidatorKeys( + await this._validatorsMethod.registerValidatorKeys( methodContext, posValidator.address, posValidator.blsKey, posValidator.generatorKey, posValidator.proofOfPossession, ); - if (!valid) { - throw new Error('Invalid validator key.'); - } } } const stakerStore = this.stores.get(StakerStore); diff --git a/framework/src/modules/pos/types.ts b/framework/src/modules/pos/types.ts index 83804faf0cf..99713ce6aa6 100644 --- a/framework/src/modules/pos/types.ts +++ b/framework/src/modules/pos/types.ts @@ -63,7 +63,7 @@ export interface ValidatorsMethod { blsKey: Buffer, generatorKey: Buffer, proofOfPossession: Buffer, - ): Promise; + ): Promise; registerValidatorWithoutBLSKey( methodContext: MethodContext, validatorAddress: Buffer, diff --git a/framework/src/modules/validators/method.ts b/framework/src/modules/validators/method.ts index 4d0c478a7e2..6849a64f957 100644 --- a/framework/src/modules/validators/method.ts +++ b/framework/src/modules/validators/method.ts @@ -44,7 +44,7 @@ export class ValidatorsMethod extends BaseMethod { blsKey: Buffer, generatorKey: Buffer, proofOfPossession: Buffer, - ): Promise { + ): Promise { if (validatorAddress.length !== ADDRESS_LENGTH) { throw new Error(`Validator address must be ${ADDRESS_LENGTH} bytes long.`); } @@ -96,8 +96,6 @@ export class ValidatorsMethod extends BaseMethod { proofOfPossession, result: KeyRegResult.SUCCESS, }); - - return true; } public async registerValidatorWithoutBLSKey( diff --git a/framework/test/unit/modules/pos/commands/validator_registration.spec.ts b/framework/test/unit/modules/pos/commands/validator_registration.spec.ts index 06b32ab68e6..4fc8df99a59 100644 --- a/framework/test/unit/modules/pos/commands/validator_registration.spec.ts +++ b/framework/test/unit/modules/pos/commands/validator_registration.spec.ts @@ -113,7 +113,7 @@ describe('Validator registration command', () => { }); mockValidatorsMethod = { setValidatorGeneratorKey: jest.fn(), - registerValidatorKeys: jest.fn().mockResolvedValue(true), + registerValidatorKeys: jest.fn(), registerValidatorWithoutBLSKey: jest.fn().mockResolvedValue(true), getValidatorKeys: jest.fn(), getGeneratorsBetweenTimestamps: jest.fn(), diff --git a/framework/test/unit/modules/pos/module.spec.ts b/framework/test/unit/modules/pos/module.spec.ts index 61f0eb2c45d..31041231cec 100644 --- a/framework/test/unit/modules/pos/module.spec.ts +++ b/framework/test/unit/modules/pos/module.spec.ts @@ -134,7 +134,7 @@ describe('PoS module', () => { }; const validatorMethod = { setValidatorGeneratorKey: jest.fn(), - registerValidatorKeys: jest.fn().mockResolvedValue(true), + registerValidatorKeys: jest.fn(), registerValidatorWithoutBLSKey: jest.fn().mockResolvedValue(true), getValidatorKeys: jest.fn().mockResolvedValue({ blsKey: utils.getRandomBytes(48), @@ -276,11 +276,12 @@ describe('PoS module', () => { ); }); - it('should fail if registerValidatorKeys return false', async () => { - (pos['_validatorsMethod'].registerValidatorKeys as jest.Mock).mockResolvedValue(false); - + it('should fail if registerValidatorKeys throws an error', async () => { + (pos['_validatorsMethod'].registerValidatorKeys as jest.Mock).mockRejectedValue( + new Error('error'), + ); await expect(pos.initGenesisState(context)).toResolve(); - await expect(pos.finalizeGenesisState(context)).rejects.toThrow('Invalid validator key'); + await expect(pos.finalizeGenesisState(context)).rejects.toThrow('error'); }); it('should fail if getLockedAmount return different value', async () => { diff --git a/framework/test/unit/modules/validators/method.spec.ts b/framework/test/unit/modules/validators/method.spec.ts index e948a094fd0..8eb9c00ffc4 100644 --- a/framework/test/unit/modules/validators/method.spec.ts +++ b/framework/test/unit/modules/validators/method.spec.ts @@ -113,15 +113,14 @@ describe('ValidatorsModuleMethod', () => { result: KeyRegResult.SUCCESS, }); - await expect( - validatorsModule.method.registerValidatorKeys( - methodContext, - address, - blsKey, - generatorKey, - proofOfPossession, - ), - ).resolves.toBe(true); + await validatorsModule.method.registerValidatorKeys( + methodContext, + address, + blsKey, + generatorKey, + proofOfPossession, + ); + const returnedAccount = await validatorsSubStore.get(methodContext, address); const returnedAddress = await blsKeysSubStore.get(methodContext, blsKey); expect(returnedAccount).toStrictEqual({ generatorKey, blsKey });