Skip to content

Commit

Permalink
author prediction test
Browse files Browse the repository at this point in the history
  • Loading branch information
girazoki committed Sep 3, 2024
1 parent ceacf9e commit fbce154
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 8 deletions.
21 changes: 13 additions & 8 deletions solo-chains/runtime/starlight/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,16 @@ fn starlight_testnet_genesis(
invulnerables: Vec<String>,
) -> serde_json::Value {
let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(testnet_accounts);
let invulnerables: Vec<_> = invulnerables
let invulnerable_keys: Vec<_> = invulnerables
.iter()
.map(|seed| get_authority_keys_from_seed(seed, None))
.collect();

let invulnerable_accounts: Vec<_> = invulnerables
.iter()
.map(|seed| get_account_id_from_seed::<sr25519::Public>(seed))
.collect();

let data_preservers_bootnodes: Vec<_> = container_chains
.iter()
.flat_map(|(para_id, _genesis_data, bootnodes)| {
Expand Down Expand Up @@ -289,13 +294,13 @@ fn starlight_testnet_genesis(
)
})
.collect::<Vec<_>>(),
"nonAuthorityKeys": invulnerables
.iter()
.cloned()
.map(|x| {
"nonAuthorityKeys": invulnerable_keys
.into_iter()
.enumerate()
.map(|(i, x)| {
(
x.stash.clone(),
x.stash.clone(),
invulnerable_accounts[i].clone(),
invulnerable_accounts[i].clone(),
starlight_session_keys(
x.babe.clone(),
x.grandpa.clone(),
Expand Down Expand Up @@ -337,7 +342,7 @@ fn starlight_testnet_genesis(
"nextFreeParaId": primitives::LOWEST_PUBLIC_ID,
},
"tanssiInvulnerables": crate::TanssiInvulnerablesConfig {
invulnerables: invulnerables.iter().cloned().map(|x| x.stash.clone()).collect(),
invulnerables: invulnerable_accounts ,
},
"containerRegistrar": crate::ContainerRegistrarConfig { para_ids, ..Default::default() },
"servicesPayment": crate::ServicesPaymentConfig { para_id_credits },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import "@tanssi/api-augment";
import { describeSuite, expect, beforeAll } from "@moonwall/cli";
import { KeyringPair } from "@moonwall/util";
import { ApiPromise } from "@polkadot/api";
import { jumpSessions, jumpToSession } from "../../../util/block";
import { u8aToHex, stringToHex } from "@polkadot/util";
import { Keyring } from '@polkadot/keyring';
const includesAll = (arr, values) => values.every(v => arr.includes(v));

describeSuite({
id: "CT0101",
title: "Session keys assignment test suite",
foundationMethods: "dev",
testCases: ({ it, context }) => {
let polkadotJs: ApiPromise;
let alice: KeyringPair;

Check failure on line 16 in test/suites/dev-tanssi-relay/author-slot-prediction/test_author_slot_prediction.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'alice' is assigned a value but never used
let bob: KeyringPair;
let charlie: KeyringPair;
let dave: KeyringPair;
let eve: KeyringPair;
let ferdie: KeyringPair;

Check failure on line 21 in test/suites/dev-tanssi-relay/author-slot-prediction/test_author_slot_prediction.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'ferdie' is assigned a value but never used
let aliceStash: KeyringPair;
beforeAll(() => {
let keyring = new Keyring({ type: 'sr25519' });

Check failure on line 24 in test/suites/dev-tanssi-relay/author-slot-prediction/test_author_slot_prediction.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'keyring' is never reassigned. Use 'const' instead
alice = context.keyring.alice;
bob = context.keyring.bob;
charlie = context.keyring.charlie;
dave = context.keyring.dave;
eve = keyring.addFromUri('//Eve');
ferdie = keyring.addFromUri('//Ferdie');
aliceStash = keyring.addFromUri('//Alice//stash')
polkadotJs = context.polkadotJs();
});

it({
id: "E01",
title: "Checking that authority assignment is correct in the first assignment",
test: async function () {
// We need at least a couple of session to start seeing the first assignment
// The reason is that the session pallet goes before the tanssiInvulnerables in the starlight runtime
// Otherwise we would not need this (as in dancebox)
await jumpToSession(context, 2);


// for session 2
const assignment2 = (await polkadotJs.query.tanssiAuthorityAssignment.collatorContainerChain(2))
.unwrap()
.toHuman();
const assignment3 = (await polkadotJs.query.tanssiAuthorityAssignment.collatorContainerChain(3))
.unwrap()
.toHuman();
expect(assignment2.orchestratorChain).to.deep.equal([]);
const allKeys = [u8aToHex(bob.publicKey), u8aToHex(charlie.publicKey), u8aToHex(dave.publicKey), u8aToHex(eve.publicKey)];

// the keys are assigned randomly but we check all of them exist in allKeys
expect(includesAll(allKeys, assignment3.containerChains["2000"])).to.be.true;
expect(includesAll(allKeys, assignment3.containerChains["2001"])).to.be.true;

// Session 3 is the same as session 2
expect(assignment2).to.deep.equal(assignment3);
// Session 4 is empty
expect((await polkadotJs.query.tanssiAuthorityAssignment.collatorContainerChain(4)).isNone).to.be.true;

// Check authorities are correct

const sessionIndex = (await polkadotJs.query.session.currentIndex()).toNumber();

const authorities = (await context
.polkadotJs()
.query.tanssiAuthorityAssignment.collatorContainerChain(sessionIndex))
.unwrap()
.toHuman();

expect(includesAll(allKeys, authorities.containerChains["2000"])).to.be.true;
},
});

it({
id: "E02",
title: "Checking session key changes are reflected at the session length boundary block",
test: async function () {
const newKey = await polkadotJs.rpc.author.rotateKeys();
const newKeyCharlie = await polkadotJs.rpc.author.rotateKeys();

// from alice and also charlie, to make sure the next authorities are changed
await polkadotJs.tx.session.setKeys(newKey, []).signAndSend(aliceStash);
await polkadotJs.tx.session.setKeys(newKeyCharlie, []).signAndSend(charlie);


await context.createBlock();
// Check key is reflected in next key
// But its not yet in queued
const queuedKeys = await polkadotJs.query.session.queuedKeys();
const result = queuedKeys.filter((keyItem) => keyItem[1].nimbus == newKey);
expect(result).is.empty;
const nextKey = await polkadotJs.query.session.nextKeys(aliceStash.address);
const nextKeyCharlie = await polkadotJs.query.session.nextKeys(charlie.address);
// the last 32 are the nimbus key
expect(u8aToHex(nextKey.unwrap().nimbus)).to.be.eq(u8aToHex(newKey.slice(-32)));
expect(u8aToHex(nextKeyCharlie.unwrap().nimbus)).to.be.eq(u8aToHex(newKeyCharlie.slice(-32)));

// Let's jump 2 sessiona
await jumpSessions(context, 2);

// The very first block produced by the second session should contain the new key

let babeAuthorities = (await polkadotJs.query.babe.authorities());

Check failure on line 107 in test/suites/dev-tanssi-relay/author-slot-prediction/test_author_slot_prediction.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'babeAuthorities' is never reassigned. Use 'const' instead
let nextBabeKey = nextKey.unwrap().babe;

Check failure on line 108 in test/suites/dev-tanssi-relay/author-slot-prediction/test_author_slot_prediction.ts

View workflow job for this annotation

GitHub Actions / typescript-linting

'nextBabeKey' is never reassigned. Use 'const' instead

expect(babeAuthorities[0].includes(nextBabeKey))
// The change should have been applied, and now both nimbus and authorityMapping should reflect
const digests = (await polkadotJs.query.system.digest()).logs;
const filtered = digests.filter(
(log) => log.isConsensus === true && log.asConsensus[0].toHex() == stringToHex("BABE")
);

expect(filtered[0].asConsensus[1].toHex().includes(nextBabeKey.toHex().slice(2))).to.be.true;


// Charlie should have his changed reflected in either 2000 or 2001
const sessionIndex = (await polkadotJs.query.session.currentIndex()).toNumber();

const authorities = (await context
.polkadotJs()
.query.tanssiAuthorityAssignment.collatorContainerChain(sessionIndex))
.unwrap()
.toHuman();
expect(authorities.containerChains["2000"].includes(u8aToHex(newKeyCharlie.slice(-32))) || authorities.containerChains["2001"].includes(u8aToHex(newKeyCharlie.slice(-32)))).to.be.true;
},
});
},
});

0 comments on commit fbce154

Please sign in to comment.