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

Reward block author in Dancelight #784

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion solo-chains/runtime/dancelight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,17 @@ impl pallet_timestamp::Config for Runtime {
type WeightInfo = weights::pallet_timestamp::SubstrateWeight<Runtime>;
}

pub struct RewardPoints;

impl pallet_authorship::EventHandler<AccountId, BlockNumberFor<Runtime>> 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<Self, Babe>;
type EventHandler = ();
type EventHandler = RewardPoints;
}

impl_opaque_keys! {
Expand Down
6 changes: 6 additions & 0 deletions solo-chains/runtime/dancelight/src/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ impl ExtBuilder {
.assimilate_storage(&mut t)
.unwrap();

snowbridge_pallet_system::GenesisConfig::<Runtime> {
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();

if self.safe_xcm_version.is_some() {
// Disable run_block checks in XCM tests, because the XCM emulator runs on_initialize and
// on_finalize automatically
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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();

// 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);
Copy link
Collaborator

@girazoki girazoki Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait they get 20 per candidate and per block created. So you will always have higher than 20?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if so, you need to check the rewards totalRewards > num_blocks*20

// 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);
},
});
},
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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";

describeSuite({
id: "DTR1601",
id: "DTR1602",
title: "Paras inherent tests",
foundationMethods: "dev",

Expand All @@ -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
Expand Down
Loading