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

Add smoke tests for BABE #749

Merged
merged 7 commits into from
Nov 20, 2024
62 changes: 62 additions & 0 deletions test/suites/smoke-test-dancelight/test-babe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { beforeAll, describeSuite, expect } from "@moonwall/cli";

import { stringToHex } from "@polkadot/util";
import { ApiPromise } from "@polkadot/api";

describeSuite({
id: "S19",
title: "Sample suite that only runs on Dancelight chains",
foundationMethods: "read_only",
testCases: ({ it, context }) => {
let api: ApiPromise;

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

it({
id: "C01",
title: "BABE keys are set and validators from logs match validators from pallet",
test: async function () {
const blockToCheck = (await api.query.babe.epochStart()).toJSON()[1];

const apiAtSessionChange = await api.at(await api.rpc.chain.getBlockHash(blockToCheck));

const digestsInSessionChange = (await apiAtSessionChange.query.system.digest()).logs;
const filteredDigests = digestsInSessionChange.filter(
(log) => log.isConsensus === true && log.asConsensus[0].toHex() == stringToHex("BABE")
);
expect(filteredDigests.length).to.eq(1);

// 0x01 corresponds to ConsensusLog::NextEpochData enum variant.
expect(filteredDigests[0].asConsensus[1].toHex().startsWith("0x01")).to.be.true;

// Assert that authorities from log == authorities from pallet
const babeAuthoritiesFromPallet = await api.query.babe.authorities();
const asdas = api.registry.createType(
tmpolaczyk marked this conversation as resolved.
Show resolved Hide resolved
"(u8, Vec<(SpConsensusBabeAppPublic, u64)>, [u8; 32])",
filteredDigests[0].asConsensus[1].toHex()
);

expect(asdas[1]).to.deep.equal(babeAuthoritiesFromPallet);

// Get babe keys from pallet session
const sessionValidators = await api.query.session.validators();

const babeKeysInPalletSession = [];

for (const account of sessionValidators) {
const accountKeys = await api.query.session.nextKeys(account);
expect(accountKeys.isSome, `Missing babe key for validator ${account.toJSON()}`).toBeTruthy();
babeKeysInPalletSession.push(accountKeys.unwrap().babe.toHex());
}

// Assert that all validators have babe keys
const babeAuthoritiesSorted = babeAuthoritiesFromPallet.map((x) => x[0].toHex());
babeAuthoritiesSorted.sort();
babeKeysInPalletSession.sort();
expect(babeKeysInPalletSession).to.deep.equal(babeAuthoritiesSorted);
},
});
},
});
Loading