Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 add extra validation for nominate procedure #1433

Merged
merged 1 commit into from
Jan 22, 2025
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
38 changes: 36 additions & 2 deletions src/api/procedures/__tests__/nominateValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('nominateValidators procedure', () => {
dsMockUtils.cleanup();
});

it('should throw an error if the target is not a controller', async () => {
it('should throw an error if the acting account is not a controller', async () => {
const proc = procedureMockUtils.getInstance<Params, void, Storage>(mockContext, {
...storage,
ledger: null,
Expand All @@ -88,7 +88,7 @@ describe('nominateValidators procedure', () => {

await expect(
prepareNominateValidators.call(proc, {
validators: [],
validators: [validator],
})
).rejects.toThrow(expectedError);
});
Expand Down Expand Up @@ -126,6 +126,40 @@ describe('nominateValidators procedure', () => {
).rejects.toThrow(expectedError);
});

it('should throw an error if a nominator is blocked', async () => {
const proc = procedureMockUtils.getInstance<Params, void, Storage>(mockContext, storage);

const args = {
validators: [
entityMockUtils.getAccountInstance({
stakingGetCommission: { blocked: true, commission: new BigNumber(10) },
}),
],
};

const expectedError = new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Validator(s) have been blocked',
});

await expect(prepareNominateValidators.call(proc, args)).rejects.toThrow(expectedError);
});

it('should throw an error if no nominators are received', async () => {
const proc = procedureMockUtils.getInstance<Params, void, Storage>(mockContext, storage);

const args = {
validators: [],
};

const expectedError = new PolymeshError({
code: ErrorCode.ValidationError,
message: 'At least one validator must be nominated',
});

await expect(prepareNominateValidators.call(proc, args)).rejects.toThrow(expectedError);
});

it('should return a nominate transaction spec', async () => {
const proc = procedureMockUtils.getInstance<Params, void, Storage>(mockContext, storage);

Expand Down
44 changes: 35 additions & 9 deletions src/api/procedures/nominateValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,65 @@ export async function prepareNominateValidators(

const validators = validatorsInput.map(validator => asAccount(validator, context));

if (validators.length === 0) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'At least one validator must be nominated',
});
}

if (uniqBy(validators, 'address').length !== validators.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Validators cannot be repeated',
});
}

const commissions = await Promise.all(
const invalidCommissions = await Promise.all(
validators.map(validator => {
return validator.staking.getCommission();
})
);

const missingCommissions = commissions.reduce((missing, commission, index) => {
if (!commission) {
missing.push(index);
}
const badValidators = invalidCommissions.reduce(
(invalidItems, commission, index) => {
if (!commission) {
invalidItems.missing.push(index);
}

if (commission?.blocked) {
invalidItems.blocked.push(index);
}

return missing;
}, [] as number[]);
return invalidItems;
},
{ missing: [] as number[], blocked: [] as number[] }
);

if (missingCommissions.length) {
if (badValidators.missing.length) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'Commission not found for validator(s)',
data: {
missingCommissions: missingCommissions.map(
missingCommissions: badValidators.missing.map(
missingIndex => validators[missingIndex].address
),
},
});
}

if (badValidators.blocked.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Validator(s) have been blocked',
data: {
blockedValidators: badValidators.blocked.map(
blockedIndex => validators[blockedIndex].address
),
},
});
}

if (!ledger) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
Expand Down
Loading