From edba9ec1360925635dc4084af19840a3ff02654a Mon Sep 17 00:00:00 2001 From: Tomasz Polaczyk Date: Mon, 16 Dec 2024 13:21:55 +0100 Subject: [PATCH 1/4] Reward block author in Dancelight --- solo-chains/runtime/dancelight/src/lib.rs | 10 +++++++++- solo-chains/runtime/dancelight/src/tests/common/mod.rs | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/solo-chains/runtime/dancelight/src/lib.rs b/solo-chains/runtime/dancelight/src/lib.rs index 4107d791f..6febcc51e 100644 --- a/solo-chains/runtime/dancelight/src/lib.rs +++ b/solo-chains/runtime/dancelight/src/lib.rs @@ -519,9 +519,17 @@ impl pallet_timestamp::Config for Runtime { type WeightInfo = weights::pallet_timestamp::SubstrateWeight; } +pub struct RewardPoints; + +impl pallet_authorship::EventHandler> for RewardPoints { + fn note_author(author: AccountId) { + ExternalValidatorsRewards::reward_by_ids(vec![(author, 20u32)]) + } +} + impl pallet_authorship::Config for Runtime { type FindAuthor = pallet_session::FindAccountFromAuthorIndex; - type EventHandler = (); + type EventHandler = RewardPoints; } impl_opaque_keys! { diff --git a/solo-chains/runtime/dancelight/src/tests/common/mod.rs b/solo-chains/runtime/dancelight/src/tests/common/mod.rs index fbb9d068b..97dc9791b 100644 --- a/solo-chains/runtime/dancelight/src/tests/common/mod.rs +++ b/solo-chains/runtime/dancelight/src/tests/common/mod.rs @@ -656,6 +656,13 @@ impl ExtBuilder { pallet_sudo::GenesisConfig:: { key: self.sudo } .assimilate_storage(&mut t) .unwrap(); + + snowbridge_pallet_system::GenesisConfig:: { + ..Default::default() + } + .assimilate_storage(&mut t) + .unwrap(); + t } From 8ec3aa9f67be16e7388f0175b04b574190a519cf Mon Sep 17 00:00:00 2001 From: Tomasz Polaczyk Date: Mon, 16 Dec 2024 14:02:04 +0100 Subject: [PATCH 2/4] Add reward test no candidates --- ...st_external_validator_reward_candidates.ts | 67 +++++++++++++++++++ .../test_external_validator_rewards.ts | 7 +- 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts new file mode 100644 index 000000000..dc4ef52ea --- /dev/null +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts @@ -0,0 +1,67 @@ +import "@tanssi/api-augment"; +import { describeSuite, customDevRpcRequest, expect, beforeAll } from "@moonwall/cli"; +import { ApiPromise, Keyring } from "@polkadot/api"; +import { jumpToSession } from "util/block"; + +describeSuite({ + id: "DTR1601", + title: "Paras inherent tests", + foundationMethods: "dev", + + testCases: ({ it, context }) => { + let polkadotJs: ApiPromise; + + beforeAll(async () => { + polkadotJs = context.polkadotJs(); + }); + + it({ + id: "E01", + title: "para candidates should trigger reward info", + test: async function () { + const keyring = new Keyring({ type: "sr25519" }); + const aliceStash = keyring.addFromUri("//Alice//stash"); + await context.createBlock(); + // Send RPC call to enable para inherent candidate generation + await customDevRpcRequest("mock_enableParaInherentCandidate", []); + // Since collators are not assigned until session 2, we need to go till session 2 to actually see heads being injected + await jumpToSession(context, 3); + await context.createBlock(); + + // we are still in era 0 + const validatorRewards = await context + .polkadotJs() + .query.externalValidatorsRewards.rewardPointsForEra(0); + const totalRewards = validatorRewards.total.toBigInt(); + + expect(totalRewards).to.be.greaterThan(0n); + // All of them come from alice as she is the only one validating candidates + expect(validatorRewards.individual.toHuman()[aliceStash.address]).to.be.eq(totalRewards.toString()); + }, + }); + + it({ + id: "E02", + title: "Check rewards storage clears after historyDepth", + test: async function () { + const sessionsPerEra = await polkadotJs.consts.externalValidators.sessionsPerEra; + const historyDepth = await polkadotJs.consts.externalValidatorsRewards.historyDepth; + + const currentIndex = await polkadotJs.query.session.currentIndex(); + + const targetSession = + currentIndex.toNumber() + sessionsPerEra.toNumber() * (historyDepth.toNumber() + 1); + + await jumpToSession(context, targetSession); + + const validatorRewards = await context + .polkadotJs() + .query.externalValidatorsRewards.rewardPointsForEra(0); + const totalRewards = validatorRewards.total.toBigInt(); + + // rewards should have expired + expect(totalRewards).to.be.equal(0n); + }, + }); + }, +}); diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts index dc4ef52ea..a55f9b5bf 100644 --- a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts @@ -4,7 +4,7 @@ import { ApiPromise, Keyring } from "@polkadot/api"; import { jumpToSession } from "util/block"; describeSuite({ - id: "DTR1601", + id: "DTR1602", title: "Paras inherent tests", foundationMethods: "dev", @@ -22,11 +22,6 @@ describeSuite({ const keyring = new Keyring({ type: "sr25519" }); const aliceStash = keyring.addFromUri("//Alice//stash"); await context.createBlock(); - // Send RPC call to enable para inherent candidate generation - await customDevRpcRequest("mock_enableParaInherentCandidate", []); - // Since collators are not assigned until session 2, we need to go till session 2 to actually see heads being injected - await jumpToSession(context, 3); - await context.createBlock(); // we are still in era 0 const validatorRewards = await context From 4397e337a084e7503c85ab8dff53b1c07d608fa7 Mon Sep 17 00:00:00 2001 From: Tomasz Polaczyk Date: Tue, 17 Dec 2024 11:31:37 +0100 Subject: [PATCH 3/4] Fix tests --- .../test_external_validator_reward_candidates.ts | 3 ++- .../test_external_validator_rewards.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts index dc4ef52ea..8aeb27c9c 100644 --- a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts @@ -34,7 +34,8 @@ describeSuite({ .query.externalValidatorsRewards.rewardPointsForEra(0); const totalRewards = validatorRewards.total.toBigInt(); - expect(totalRewards).to.be.greaterThan(0n); + // Validators get 20 points for creating a block, so if they included a candidate, they will get more than 20 + expect(totalRewards).to.be.greaterThan(20n); // All of them come from alice as she is the only one validating candidates expect(validatorRewards.individual.toHuman()[aliceStash.address]).to.be.eq(totalRewards.toString()); }, diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts index a55f9b5bf..473490d6b 100644 --- a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts @@ -1,5 +1,5 @@ import "@tanssi/api-augment"; -import { describeSuite, customDevRpcRequest, expect, beforeAll } from "@moonwall/cli"; +import { describeSuite, expect, beforeAll } from "@moonwall/cli"; import { ApiPromise, Keyring } from "@polkadot/api"; import { jumpToSession } from "util/block"; From d8d1d474a919c5b330e34c55e4c78704f4f79584 Mon Sep 17 00:00:00 2001 From: Tomasz Polaczyk Date: Thu, 19 Dec 2024 17:42:55 +0100 Subject: [PATCH 4/4] Check points per block in test --- .../test_external_validator_reward_candidates.ts | 4 +++- .../test_external_validator_rewards.ts | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts index 8aeb27c9c..5c9d328c7 100644 --- a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_reward_candidates.ts @@ -34,8 +34,10 @@ describeSuite({ .query.externalValidatorsRewards.rewardPointsForEra(0); const totalRewards = validatorRewards.total.toBigInt(); + const blockNumber = (await polkadotJs.rpc.chain.getHeader()).number.toBigInt(); + // Validators get 20 points for creating a block, so if they included a candidate, they will get more than 20 - expect(totalRewards).to.be.greaterThan(20n); + expect(totalRewards).to.be.greaterThan(20n * blockNumber); // All of them come from alice as she is the only one validating candidates expect(validatorRewards.individual.toHuman()[aliceStash.address]).to.be.eq(totalRewards.toString()); }, diff --git a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts index 473490d6b..72737d8a3 100644 --- a/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts +++ b/test/suites/dev-tanssi-relay/external-validators-rewards/test_external_validator_rewards.ts @@ -29,8 +29,11 @@ describeSuite({ .query.externalValidatorsRewards.rewardPointsForEra(0); const totalRewards = validatorRewards.total.toBigInt(); - expect(totalRewards).to.be.greaterThan(0n); - // All of them come from alice as she is the only one validating candidates + const blockNumber = (await polkadotJs.rpc.chain.getHeader()).number.toBigInt(); + + // 20 points per block + expect(totalRewards).toBe(20n * blockNumber); + // All of them come from alice as she is the only one producing blocks expect(validatorRewards.individual.toHuman()[aliceStash.address]).to.be.eq(totalRewards.toString()); }, });