From fa0494add66a8c7dc01a12a2a4d43ffd0f8a2efe Mon Sep 17 00:00:00 2001 From: Agusrodri Date: Wed, 13 Nov 2024 13:14:29 -0800 Subject: [PATCH 1/4] add smoke test for BEEFY digests --- .../smoke-test-dancelight/test-beefy.ts | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/suites/smoke-test-dancelight/test-beefy.ts diff --git a/test/suites/smoke-test-dancelight/test-beefy.ts b/test/suites/smoke-test-dancelight/test-beefy.ts new file mode 100644 index 000000000..815757b47 --- /dev/null +++ b/test/suites/smoke-test-dancelight/test-beefy.ts @@ -0,0 +1,84 @@ +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: "Session change block should update BEEFY and MMR root digests properly", + test: async function () { + const blocksPerSession = 600; + const sessionIndex = (await api.query.session.currentIndex()).toNumber(); + + // 636564 -> block in which session changed to 1061. + // 637164 -> actual block where next session (1062) will happen. + // 637200 -> computed block based on currentIndex * sessionLength (600 blocks). + // We have a small rounding diff of 36 blocks in all the cases. + const blockToCheck = sessionIndex * blocksPerSession - 36; + + const apiAtBeforeSessionChange = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 5)); + const beefyNextAuthorities = await apiAtBeforeSessionChange.query.beefy.nextAuthorities(); + + 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("BEEF") + ); + + // As session changed, it should contain two BEEFY digests: AuthoritiesChange and MmrRoot. + expect(filteredDigests.length).to.eq(2); + + // 0x01 corresponds to ConsensusLog::AuthoritiesChange enum variant. + expect(filteredDigests[0].asConsensus[1].toHex().startsWith("0x01")).to.be.true; + + // Check if each authority is included in the BEEFY digest + for (const authority of Object.values(beefyNextAuthorities.toJSON())) { + expect(filteredDigests[0].asConsensus[1].toHex().includes(authority.slice(2))).to.be.true; + } + + const firstMmrRootDigest = filteredDigests[1].asConsensus[1].toHex(); + + // 0x03 corresponds to ConsensusLog::MmrRoot enum variant. + expect(firstMmrRootDigest.startsWith("0x03")).to.be.true; + + // Second BEEF log should contain the MMR root. + // Length should be 68 (0x03 + 32 bytes MMR root). + expect(firstMmrRootDigest.length).to.eq(68); + + // Now let's check just after session change + const apiAtAfterSessionChange = await api.at(await api.rpc.chain.getBlockHash(blockToCheck + 1)); + const digestsAfterSessionChange = (await apiAtAfterSessionChange.query.system.digest()).logs; + const filteredDigestsAfterSessionChange = digestsAfterSessionChange.filter( + (log) => log.isConsensus === true && log.asConsensus[0].toHex() == stringToHex("BEEF") + ); + + // Now we should only have the MmrRoot BEEFY digest (as session didn't change yet). + expect(filteredDigestsAfterSessionChange.length).to.eq(1); + + const secondMmrRootDigest = filteredDigestsAfterSessionChange[0].asConsensus[1].toHex(); + + // New MmrRoot digest should be different than the first one found. + expect(secondMmrRootDigest).to.not.eq(firstMmrRootDigest); + + // 0x03 corresponds to ConsensusLog::MmrRoot enum variant. + expect(secondMmrRootDigest.startsWith("0x03")).to.be.true; + + // Second BEEF log should contain the MMR root. + // Length should be 68 (0x03 + 32 bytes MMR root). + expect(secondMmrRootDigest.length).to.eq(68); + }, + }); + }, +}); From 117f585c4354d881f538e24644fb34fa0f2e618c Mon Sep 17 00:00:00 2001 From: Agusrodri Date: Wed, 13 Nov 2024 13:18:43 -0800 Subject: [PATCH 2/4] fmt --- test/suites/smoke-test-dancelight/test-beefy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/suites/smoke-test-dancelight/test-beefy.ts b/test/suites/smoke-test-dancelight/test-beefy.ts index 815757b47..c93509646 100644 --- a/test/suites/smoke-test-dancelight/test-beefy.ts +++ b/test/suites/smoke-test-dancelight/test-beefy.ts @@ -25,7 +25,7 @@ describeSuite({ // 637164 -> actual block where next session (1062) will happen. // 637200 -> computed block based on currentIndex * sessionLength (600 blocks). // We have a small rounding diff of 36 blocks in all the cases. - const blockToCheck = sessionIndex * blocksPerSession - 36; + const blockToCheck = (sessionIndex * blocksPerSession) - 36; const apiAtBeforeSessionChange = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 5)); const beefyNextAuthorities = await apiAtBeforeSessionChange.query.beefy.nextAuthorities(); From 29f2ce94d6240c2b6f8432dd04106c76714a8cd1 Mon Sep 17 00:00:00 2001 From: Agusrodri Date: Thu, 14 Nov 2024 06:51:18 -0800 Subject: [PATCH 3/4] rely on babe's epoch start --- .../test-consistency-services-payment.ts | 6 +++--- .../{test-beefy.ts => test-beefy-mmr-digests.ts} | 9 +-------- .../test-configuration-consistency.ts | 12 ++++-------- 3 files changed, 8 insertions(+), 19 deletions(-) rename test/suites/smoke-test-dancelight/{test-beefy.ts => test-beefy-mmr-digests.ts} (87%) diff --git a/test/suites/smoke-test-common-all/test-consistency-services-payment.ts b/test/suites/smoke-test-common-all/test-consistency-services-payment.ts index ece261233..7e3d788e0 100644 --- a/test/suites/smoke-test-common-all/test-consistency-services-payment.ts +++ b/test/suites/smoke-test-common-all/test-consistency-services-payment.ts @@ -19,7 +19,7 @@ describeSuite({ api = context.polkadotJs(); runtimeVersion = api.runtimeVersion.specVersion.toNumber(); chain = api.consts.system.version.specName.toString(); - blocksPerSession = chain == "dancebox" ? 600n : 50n; + blocksPerSession = chain == "dancebox" || chain == "dancelight" ? 600n : 50n; }); it({ @@ -30,8 +30,8 @@ describeSuite({ return; } const currentBlock = (await api.rpc.chain.getBlock()).block.header.number.toNumber(); - - const blockToCheck = Math.trunc(currentBlock / Number(blocksPerSession)) * Number(blocksPerSession); + const blockToCheck = chain == "dancelight" ? (await api.query.babe.epochStart()).toJSON()[1] + : Math.trunc(currentBlock / Number(blocksPerSession)) * Number(blocksPerSession); const apiBeforeLatestNewSession = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1)); // If they have collators scheduled, they should have at least enough money to pay diff --git a/test/suites/smoke-test-dancelight/test-beefy.ts b/test/suites/smoke-test-dancelight/test-beefy-mmr-digests.ts similarity index 87% rename from test/suites/smoke-test-dancelight/test-beefy.ts rename to test/suites/smoke-test-dancelight/test-beefy-mmr-digests.ts index c93509646..048ae0044 100644 --- a/test/suites/smoke-test-dancelight/test-beefy.ts +++ b/test/suites/smoke-test-dancelight/test-beefy-mmr-digests.ts @@ -18,14 +18,7 @@ describeSuite({ id: "C01", title: "Session change block should update BEEFY and MMR root digests properly", test: async function () { - const blocksPerSession = 600; - const sessionIndex = (await api.query.session.currentIndex()).toNumber(); - - // 636564 -> block in which session changed to 1061. - // 637164 -> actual block where next session (1062) will happen. - // 637200 -> computed block based on currentIndex * sessionLength (600 blocks). - // We have a small rounding diff of 36 blocks in all the cases. - const blockToCheck = (sessionIndex * blocksPerSession) - 36; + const blockToCheck = (await api.query.babe.epochStart()).toJSON()[1]; const apiAtBeforeSessionChange = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 5)); const beefyNextAuthorities = await apiAtBeforeSessionChange.query.beefy.nextAuthorities(); diff --git a/test/suites/smoke-test-dancelight/test-configuration-consistency.ts b/test/suites/smoke-test-dancelight/test-configuration-consistency.ts index 30598dc3d..1f22baff2 100644 --- a/test/suites/smoke-test-dancelight/test-configuration-consistency.ts +++ b/test/suites/smoke-test-dancelight/test-configuration-consistency.ts @@ -10,28 +10,24 @@ describeSuite({ foundationMethods: "read_only", testCases: ({ it, context }) => { let api: ApiPromise; - let blocksPerSession; + const blocksPerSession = 600n; const costPerSession = 100_000_000n; const costPerBlock = 1_000_000n; beforeAll(() => { api = context.polkadotJs(); - const chain = api.consts.system.version.specName.toString(); - blocksPerSession = chain == "dancebox" ? 600n : 50n; }); it({ id: "C01", title: "Config for registered paras should be consistent", test: async function () { - const currentBlock = (await api.rpc.chain.getBlock()).block.header.number.toNumber(); + const sessionIndex = (await api.query.session.currentIndex()).toNumber(); + const blockToCheck = (await api.query.babe.epochStart()).toJSON()[1]; - const blockToCheck = Math.trunc(currentBlock / Number(blocksPerSession)) * Number(blocksPerSession); const apiBeforeLatestNewSession = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1)); - const config = await api.query.collatorConfiguration.activeConfig(); - // get current session - const sessionIndex = (await api.query.session.currentIndex()).toNumber(); + // get pending authorities // the reason for getting pending is that the hasEnoughCredits check it's done over the pending ones const pendingAuthorityAssignment = ( From 003049338a988d31a1ab9e648dc40f5cc98d96ce Mon Sep 17 00:00:00 2001 From: Agusrodri Date: Thu, 14 Nov 2024 06:58:51 -0800 Subject: [PATCH 4/4] fmt --- .../test-consistency-services-payment.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/suites/smoke-test-common-all/test-consistency-services-payment.ts b/test/suites/smoke-test-common-all/test-consistency-services-payment.ts index 7e3d788e0..1fc3288f8 100644 --- a/test/suites/smoke-test-common-all/test-consistency-services-payment.ts +++ b/test/suites/smoke-test-common-all/test-consistency-services-payment.ts @@ -30,8 +30,10 @@ describeSuite({ return; } const currentBlock = (await api.rpc.chain.getBlock()).block.header.number.toNumber(); - const blockToCheck = chain == "dancelight" ? (await api.query.babe.epochStart()).toJSON()[1] - : Math.trunc(currentBlock / Number(blocksPerSession)) * Number(blocksPerSession); + const blockToCheck = + chain == "dancelight" + ? (await api.query.babe.epochStart()).toJSON()[1] + : Math.trunc(currentBlock / Number(blocksPerSession)) * Number(blocksPerSession); const apiBeforeLatestNewSession = await api.at(await api.rpc.chain.getBlockHash(blockToCheck - 1)); // If they have collators scheduled, they should have at least enough money to pay