Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Align the registerValidatorKeys method to LIP
Browse files Browse the repository at this point in the history
- align registerValidatorKeys method to lip
- update unit tests

Update unit test
  • Loading branch information
mosmartin committed Jun 23, 2023
1 parent 4709bdd commit 6af4137
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 28 deletions.
6 changes: 1 addition & 5 deletions framework/src/modules/pos/commands/register_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,14 @@ export class RegisterValidatorCommand extends BaseCommand {
} = context;
const methodContext = context.getMethodContext();

const isRegistered = await this._validatorsMethod.registerValidatorKeys(
await this._validatorsMethod.registerValidatorKeys(
methodContext,
transaction.senderAddress,
blsKey,
generatorKey,
proofOfPossession,
);

if (!isRegistered) {
throw new Error('Failed to register validator keys');
}

this._feeMethod.payFee(context, this._validatorRegistrationFee);

const validatorSubstore = this.stores.get(ValidatorStore);
Expand Down
5 changes: 1 addition & 4 deletions framework/src/modules/pos/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/pos/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface ValidatorsMethod {
blsKey: Buffer,
generatorKey: Buffer,
proofOfPossession: Buffer,
): Promise<boolean>;
): Promise<void>;
registerValidatorWithoutBLSKey(
methodContext: MethodContext,
validatorAddress: Buffer,
Expand Down
4 changes: 1 addition & 3 deletions framework/src/modules/validators/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ValidatorsMethod extends BaseMethod {
blsKey: Buffer,
generatorKey: Buffer,
proofOfPossession: Buffer,
): Promise<boolean> {
): Promise<void> {
if (validatorAddress.length !== ADDRESS_LENGTH) {
throw new Error(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
}
Expand Down Expand Up @@ -96,8 +96,6 @@ export class ValidatorsMethod extends BaseMethod {
proofOfPossession,
result: KeyRegResult.SUCCESS,
});

return true;
}

public async registerValidatorWithoutBLSKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 6 additions & 5 deletions framework/test/unit/modules/pos/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 () => {
Expand Down
17 changes: 8 additions & 9 deletions framework/test/unit/modules/validators/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 6af4137

Please sign in to comment.