Skip to content

Commit

Permalink
Test: Validators matching external validators smoke test (#806)
Browse files Browse the repository at this point in the history
* test: initial validators matching external validators smoke test implementation

* test: correct validators queries and linting

* test: updated external validators value type and using check whether it matches exactly with the session validators list

* test: handling both cases when external validators are more or less than session validators

* test: update externalValidators to query externalValidator.externalValidators instead of externalValidators.whitelistedValidators

* test: fetch the external validators as concatenation of whitelisted validators and external validators lists from the last block of the previous era

---------

Co-authored-by: Aleksandar Brayanov <aleksandar@moonsonglab.com>
  • Loading branch information
chexware and Aleksandar Brayanov authored Jan 27, 2025
1 parent ae3ddb7 commit 21fd2c8
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import { ApiPromise } from "@polkadot/api";
import { U32, Vec } from "@polkadot/types-codec";
import { AccountId32 } from "@polkadot/types/interfaces";

describeSuite({
id: "S23",
title: "Smoke tests for validators matching external validators",
foundationMethods: "read_only",
testCases: ({ it, context }) => {
let api: ApiPromise;

beforeAll(async () => {
api = context.polkadotJs();
});

it({
id: "C01",
title: "Validators should match external validators",

test: async function () {
// Find the last block in which the era changed
const currentEra = await api.query.externalValidators.currentEra<U32>();
let blockToCheck = (await api.query.babe.epochStart()).toJSON()[1];
let apiBeforeLatestNewSession = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1));

while (currentEra == (await apiBeforeLatestNewSession.query.externalValidators.currentEra<U32>())) {
blockToCheck = (await apiBeforeLatestNewSession.query.babe.epochStart()).toJSON()[1];
apiBeforeLatestNewSession = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1));
}

const externalValidatorsList = await (
await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1))
).query.externalValidators.externalValidators<Vec<AccountId32>>();
const whitelistedValidatorsList = await (
await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1))
).query.externalValidators.whitelistedValidators<Vec<AccountId32>>();

const sessionValidators = await api.query.session.validators();
const externalValidators = externalValidatorsList.toArray().concat(whitelistedValidatorsList.toArray());

if (externalValidators.length <= sessionValidators.length) {
// Less external validators than session validators: all external validators must be session validators
for (const externalValidator of externalValidators) {
expect(
sessionValidators.toString().includes(externalValidator.toString()),
`External validator should be in validators list: ${externalValidator.toString()}`
).to.be.true;
}
} else {
// More external validators than session validators: all session validators must be external validators
for (const validator of sessionValidators) {
expect(
externalValidators.toString().includes(validator.toString()),
`Validator should be in external validators list: ${validator.toString()}`
).to.be.true;
}
}
},
});
},
});

0 comments on commit 21fd2c8

Please sign in to comment.