Skip to content

Commit

Permalink
feat: relative treasury inflation (#1740)
Browse files Browse the repository at this point in the history
* feat: relative treasury inflation

* refactor: apply benchmarking v2 to block rewards

* feat: add migration

* fix: build + clippy

* Update pallets/block-rewards/src/mock.rs

Co-authored-by: Luis Enrique Muñoz Martín <[email protected]>

* chore: minor fixes

* refactor: remove max changes per session

* refactor: remove more redundant trait bounds

---------

Co-authored-by: Luis Enrique Muñoz Martín <[email protected]>
  • Loading branch information
wischli and lemunozm authored Feb 23, 2024
1 parent 5822bff commit 59f7f68
Show file tree
Hide file tree
Showing 16 changed files with 651 additions and 478 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.

24 changes: 20 additions & 4 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::{ChainType, Properties};
use serde::{Deserialize, Serialize};
use sp_core::{crypto::UncheckedInto, sr25519, Encode, Pair, Public, H160};
use sp_runtime::traits::{IdentifyAccount, Verify};
use sp_runtime::{
traits::{IdentifyAccount, Verify},
FixedPointNumber,
};
use xcm::{
latest::MultiLocation,
prelude::{GeneralIndex, GeneralKey, PalletInstance, Parachain, X2, X3},
Expand All @@ -62,6 +65,7 @@ pub type DevelopmentChainSpec =

use altair_runtime::AltairPrecompiles;
use centrifuge_runtime::CentrifugePrecompiles;
use cfg_types::fixed_point::Rate;
use development_runtime::DevelopmentPrecompiles;

/// Helper function to generate a crypto pair from seed
Expand Down Expand Up @@ -661,7 +665,11 @@ fn centrifuge_genesis(
.map(|(acc, _)| acc)
.collect(),
collator_reward: 8_325 * MILLI_CFG,
total_reward: 10_048 * CFG,
treasury_inflation_rate: Rate::saturating_from_rational(3, 100),
last_update: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
.as_secs(),
},
block_rewards_base: Default::default(),
base_fee: Default::default(),
Expand Down Expand Up @@ -759,7 +767,11 @@ fn altair_genesis(
.map(|(acc, _)| acc)
.collect(),
collator_reward: 98_630 * MILLI_AIR,
total_reward: 98_630 * MILLI_AIR * 100,
treasury_inflation_rate: Rate::saturating_from_rational(3, 100),
last_update: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
.as_secs(),
},
block_rewards_base: Default::default(),
collator_allowlist: Default::default(),
Expand Down Expand Up @@ -945,7 +957,11 @@ fn development_genesis(
.map(|(acc, _)| acc)
.collect(),
collator_reward: 8_325 * MILLI_CFG,
total_reward: 10_048 * CFG,
treasury_inflation_rate: Rate::saturating_from_rational(3, 100),
last_update: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
.as_secs(),
},
base_fee: Default::default(),
evm_chain_id: development_runtime::EVMChainIdConfig {
Expand Down
1 change: 1 addition & 0 deletions pallets/block-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sp-std = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
cfg-mocks = { workspace = true, default-features = true }
orml-tokens = { workspace = true, default-features = true }
orml-traits = { workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
Expand Down
105 changes: 67 additions & 38 deletions pallets/block-rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cfg_primitives::CFG;
use cfg_types::tokens::CurrencyId;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
traits::{fungibles::Inspect, Currency as CurrencyT},
Expand All @@ -14,56 +14,85 @@ use crate::{pallet::Config, Pallet as BlockRewards};
const REWARD: u128 = 1 * CFG;
const SEED: u32 = 0;

benchmarks! {
where_clause {
where
#[benchmarks(
where
T::Balance: From<u128>,
T::BlockNumber: From<u32> + One,
T::Weight: From<u32>,
<T as Config>::Currency: frame_support::traits::fungibles::Inspect<T::AccountId> + CurrencyT<T::AccountId>,
<T as Config>::Tokens: Inspect<T::AccountId> + CurrencyT<T::AccountId>,
<T as Config>::CurrencyId: From<CurrencyId>,
}
)]
mod benchmarks {
use super::*;

claim_reward {
let caller = whitelisted_caller();
let beneficiary: T::AccountId = account("collator", 0, SEED);
#[benchmark]
fn claim_reward() -> Result<(), BenchmarkError> {
let caller: T::AccountId = account("caller", 0, SEED);
let beneficiary: T::AccountId = account("collator", 0, SEED);

assert_ok!(BlockRewards::<T>::do_init_collator(&beneficiary));
assert_ok!(T::Rewards::reward_group(T::StakeGroupId::get(), REWARD.into()));
assert_ok!(T::Rewards::reward_group(
T::StakeGroupId::get(),
REWARD.into()
));
assert!(T::Rewards::is_ready(T::StakeGroupId::get()));
assert!(
!T::Rewards::compute_reward(
T::StakeCurrencyId::get(),
&beneficiary,
).unwrap().is_zero()
!T::Rewards::compute_reward(T::StakeCurrencyId::get(), &beneficiary,)
.unwrap()
.is_zero()
);
let before =
<T::Tokens as Inspect<T::AccountId>>::balance(CurrencyId::Native.into(), &beneficiary);

#[extrinsic_call]
claim_reward(RawOrigin::Signed(caller), beneficiary.clone());

let num_collators: u128 = BlockRewards::<T>::next_session_changes()
.collator_count
.unwrap_or(BlockRewards::<T>::active_session_data().collator_count)
.into();
// Does not get entire reward since another collator is auto-staked via genesis
// config
assert_eq!(
<T::Tokens as Inspect<T::AccountId>>::balance(CurrencyId::Native.into(), &beneficiary)
.saturating_sub(before),
(REWARD / (num_collators + 1)).into()
);
let before = <T as Config>::Currency::balance(CurrencyId::Native.into(), &beneficiary);

}: _(RawOrigin::Signed(caller), beneficiary.clone())
verify {
let num_collators: u128 = BlockRewards::<T>::next_session_changes().collator_count.unwrap_or(
BlockRewards::<T>::active_session_data().collator_count
).into();
// Does not get entire reward since another collator is auto-staked via genesis config
assert_eq!(<T as Config>::Currency::balance(CurrencyId::Native.into(), &beneficiary).saturating_sub(before), (REWARD / (num_collators + 1)).into());

Ok(())
}

set_collator_reward {
assert_ok!(BlockRewards::<T>::set_total_reward(RawOrigin::Root.into(), u128::MAX.into()));
}: _(RawOrigin::Root, REWARD.into())
verify {
assert_eq!(BlockRewards::<T>::next_session_changes().collator_reward, Some(REWARD.into()));
#[benchmark]
fn set_collator_reward_per_session() -> Result<(), BenchmarkError> {
#[extrinsic_call]
set_collator_reward_per_session(RawOrigin::Root, REWARD.into());

assert_eq!(
BlockRewards::<T>::next_session_changes().collator_reward,
Some(REWARD.into())
);

Ok(())
}

set_total_reward {
}: _(RawOrigin::Root, u128::MAX.into())
verify {
assert_eq!(BlockRewards::<T>::next_session_changes().total_reward, Some(u128::MAX.into()));
#[benchmark]
fn set_annual_treasury_inflation_rate() -> Result<(), BenchmarkError> {
let rate = T::Rate::saturating_from_rational(1, 2);

#[extrinsic_call]
set_annual_treasury_inflation_rate(RawOrigin::Root, rate);

assert_eq!(
BlockRewards::<T>::next_session_changes().treasury_inflation_rate,
Some(rate)
);

Ok(())
}
}

impl_benchmark_test_suite!(
BlockRewards,
crate::mock::ExtBuilder::default().build(),
crate::mock::Test,
);
impl_benchmark_test_suite!(
BlockRewards,
crate::mock::ExtBuilder::default().build(),
crate::mock::Test,
);
}
Loading

0 comments on commit 59f7f68

Please sign in to comment.