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

Align the registerValidatorKeys method to LIP #8633

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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