Skip to content

Commit

Permalink
Add ability to update issuance parameters in pallet-rewards + enabl…
Browse files Browse the repository at this point in the history
…e benchmarking there
  • Loading branch information
nazar-pc committed Feb 7, 2024
1 parent db10cc3 commit bd7eb34
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/pallet-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
log = { version = "0.4.20", default-features = false }
Expand All @@ -42,3 +43,7 @@ std = [
"subspace-runtime-primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
]
23 changes: 23 additions & 0 deletions crates/pallet-rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Benchmarking for `pallet-rewards`.

use frame_benchmarking::v2::*;

#[benchmarks]
mod benchmarks {
use crate::pallet::{ProposerSubsidyParams, VoterSubsidyParams};
use crate::{Call, Config, IssuanceComponentParams, Pallet};
use frame_system::RawOrigin;

#[benchmark]
fn update_issuance_params() {
#[extrinsic_call]
_(
RawOrigin::Root,
IssuanceComponentParams::default(),
IssuanceComponentParams::default(),
);

assert!(ProposerSubsidyParams::<T>::get().is_some());
assert!(VoterSubsidyParams::<T>::get().is_some());
}
}
19 changes: 0 additions & 19 deletions crates/pallet-rewards/src/default_weights.rs

This file was deleted.

44 changes: 36 additions & 8 deletions crates/pallet-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#![warn(rust_2018_idioms, missing_debug_implementations)]
#![feature(try_blocks)]

mod default_weights;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;

use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
Expand All @@ -36,11 +38,6 @@ use subspace_runtime_primitives::{BlockNumber, FindBlockRewardAddress, FindVotin
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

pub trait WeightInfo {
// TODO: We are doing non-trivial amount of work in block finalization, should we charge for it
// during initialization?
}

/// Hooks to notify when there are any rewards for specific account.
pub trait OnReward<AccountId, Balance> {
fn on_reward(account: AccountId, reward: Balance);
Expand All @@ -51,7 +48,18 @@ impl<AccountId, Balance> OnReward<AccountId, Balance> for () {
}

#[derive(
Debug, Default, Copy, Clone, Encode, Decode, MaxEncodedLen, TypeInfo, Serialize, Deserialize,
Debug,
Default,
Copy,
Clone,
Eq,
PartialEq,
Encode,
Decode,
MaxEncodedLen,
TypeInfo,
Serialize,
Deserialize,
)]
pub struct IssuanceComponentParams<BlockNumber, Balance> {
/// Maximum subsidy for initial issuance per block, total for all recipients
Expand All @@ -68,7 +76,8 @@ pub struct IssuanceComponentParams<BlockNumber, Balance> {

#[frame_support::pallet]
mod pallet {
use super::{BalanceOf, IssuanceComponentParams, OnReward, WeightInfo};
use crate::weights::WeightInfo;
use crate::{BalanceOf, IssuanceComponentParams, OnReward};
use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -183,6 +192,25 @@ mod pallet {
Self::do_finalize(now);
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Update dynamic issuance parameters
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::update_issuance_params())]
pub fn update_issuance_params(
origin: OriginFor<T>,
proposer_subsidy_params: IssuanceComponentParams<BlockNumberFor<T>, BalanceOf<T>>,
voter_subsidy_params: IssuanceComponentParams<BlockNumberFor<T>, BalanceOf<T>>,
) -> DispatchResult {
ensure_root(origin)?;

ProposerSubsidyParams::<T>::put(proposer_subsidy_params);
VoterSubsidyParams::<T>::put(voter_subsidy_params);

Ok(())
}
}
}

impl<T: Config> Pallet<T> {
Expand Down
67 changes: 67 additions & 0 deletions crates/pallet-rewards/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

//! Autogenerated weights for pallet_rewards
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-02-07, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `nazar-pc`, CPU: `13th Gen Intel(R) Core(TM) i9-13900K`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// target/release/subspace-node
// benchmark
// pallet
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_rewards
// --extrinsic=*
// --heap-pages=4096
// --output=crates/pallet-rewards/src/weights.rs
// --template=frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_debug_implementations)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use core::marker::PhantomData;

/// Weight functions needed for pallet_rewards.
pub trait WeightInfo {
fn update_issuance_params() -> Weight;
}

/// Weights for pallet_rewards using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: `Rewards::VoterSubsidyParams` (r:0 w:1)
/// Proof: `Rewards::VoterSubsidyParams` (`max_values`: Some(1), `max_size`: Some(40), added: 535, mode: `MaxEncodedLen`)
/// Storage: `Rewards::ProposerSubsidyParams` (r:0 w:1)
/// Proof: `Rewards::ProposerSubsidyParams` (`max_values`: Some(1), `max_size`: Some(40), added: 535, mode: `MaxEncodedLen`)
fn update_issuance_params() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 4_198_000 picoseconds.
Weight::from_parts(4_351_000, 0)
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: `Rewards::VoterSubsidyParams` (r:0 w:1)
/// Proof: `Rewards::VoterSubsidyParams` (`max_values`: Some(1), `max_size`: Some(40), added: 535, mode: `MaxEncodedLen`)
/// Storage: `Rewards::ProposerSubsidyParams` (r:0 w:1)
/// Proof: `Rewards::ProposerSubsidyParams` (`max_values`: Some(1), `max_size`: Some(40), added: 535, mode: `MaxEncodedLen`)
fn update_issuance_params() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 4_198_000 picoseconds.
Weight::from_parts(4_351_000, 0)
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
}
2 changes: 1 addition & 1 deletion crates/pallet-runtime-configs/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Benchmarking for `pallet-subspace`.
//! Benchmarking for `pallet-runtime-configs`.

use frame_benchmarking::v2::*;

Expand Down
1 change: 1 addition & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-domains/runtime-benchmarks",
"pallet-mmr/runtime-benchmarks",
"pallet-rewards/runtime-benchmarks",
"pallet-runtime-configs/runtime-benchmarks",
"pallet-subspace/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
Expand Down
5 changes: 3 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ impl pallet_rewards::Config for Runtime {
type TransactionByteFee = TransactionByteFee;
type FindBlockRewardAddress = Subspace;
type FindVotingRewardAddresses = Subspace;
type WeightInfo = ();
type WeightInfo = pallet_rewards::weights::SubstrateWeight<Runtime>;
type OnReward = ();
}

Expand Down Expand Up @@ -800,6 +800,7 @@ mod benches {
[pallet_balances, Balances]
[pallet_domains, Domains]
[pallet_mmr, Mmr]
[pallet_rewards, Rewards]
[pallet_runtime_configs, RuntimeConfigs]
[pallet_subspace, Subspace]
[pallet_timestamp, Timestamp]
Expand Down Expand Up @@ -830,7 +831,7 @@ impl_runtime_apis! {
Runtime::metadata_at_version(version)
}

fn metadata_versions() -> sp_std::vec::Vec<u32> {
fn metadata_versions() -> Vec<u32> {
Runtime::metadata_versions()
}
}
Expand Down

0 comments on commit bd7eb34

Please sign in to comment.