Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreOssun committed Nov 3, 2023
1 parent 15369e5 commit 7c09144
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 112 deletions.
27 changes: 16 additions & 11 deletions pallets/block-rewards-hybrid/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,34 @@

use super::*;

use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_benchmarking::v2::*;
use frame_system::{Pallet as System, RawOrigin};

/// Assert that the last event equals the provided one.
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
System::<T>::assert_last_event(generic_event.into());
}

benchmarks! {
#[benchmarks(where T: Config)]
mod benchmarks {
use super::*;

set_configuration {
#[benchmark]
fn set_configuration() {
let reward_config = RewardDistributionConfig::default();
assert!(reward_config.is_consistent());
}: _(RawOrigin::Root, reward_config.clone())
verify {

#[extrinsic_call]
_(RawOrigin::Root, reward_config.clone());

assert_last_event::<T>(Event::<T>::DistributionConfigurationChanged(reward_config).into());
}

impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::TestRuntime,
);
}

#[cfg(test)]
Expand All @@ -48,9 +59,3 @@ mod tests {
mock::ExternalityBuilder::build()
}
}

impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::TestRuntime,
);
4 changes: 2 additions & 2 deletions pallets/block-rewards-hybrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub mod pallet {

/// The amount of issuance for each block.
#[pallet::constant]
type RewardAmount: Get<Balance>;
type MaxBlockRewardAmount: Get<Balance>;

/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand Down Expand Up @@ -202,7 +202,7 @@ pub mod pallet {

impl<Moment, T: Config> OnTimestampSet<Moment> for Pallet<T> {
fn on_timestamp_set(_moment: Moment) {
let rewards = Self::calculate_rewards(T::RewardAmount::get());
let rewards = Self::calculate_rewards(T::MaxBlockRewardAmount::get());
let inflation = T::Currency::issue(rewards.sum());
Self::distribute_rewards(inflation, rewards);
}
Expand Down
16 changes: 11 additions & 5 deletions pallets/block-rewards-hybrid/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use sp_runtime::{
testing::Header,
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup},
};
use sp_std::cell::RefCell;

pub(crate) type AccountId = u64;
pub(crate) type BlockNumber = u64;
Expand Down Expand Up @@ -126,23 +127,28 @@ impl pallet_timestamp::Config for TestRuntime {
// due to TVL changes.
pub(crate) const BLOCK_REWARD: Balance = 1_000_000;

// This gives us enough flexibility to get valid percentages by controlling issuance.
pub(crate) const TVL: Balance = 1_000_000_000;

// Fake accounts used to simulate reward beneficiaries balances
pub(crate) const TREASURY_POT: PalletId = PalletId(*b"moktrsry");
pub(crate) const COLLATOR_POT: PalletId = PalletId(*b"mokcolat");
pub(crate) const STAKERS_POT: PalletId = PalletId(*b"mokstakr");
pub(crate) const DAPPS_POT: PalletId = PalletId(*b"mokdapps");

thread_local! {
static TVL: RefCell<Balance> = RefCell::new(1_000_000_000);
}

// Type used as TVL provider
pub struct TvlProvider();
impl Get<Balance> for TvlProvider {
fn get() -> Balance {
TVL
TVL.with(|t| t.borrow().clone())
}
}

pub(crate) fn set_tvl(v: Balance) {
TVL.with(|t| *t.borrow_mut() = v)
}

// Type used as beneficiary payout handle
pub struct BeneficiaryPayout();
impl pallet_block_reward::BeneficiaryPayout<NegativeImbalanceOf<TestRuntime>>
Expand Down Expand Up @@ -172,7 +178,7 @@ parameter_types! {
impl pallet_block_reward::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type RewardAmount = RewardAmount;
type MaxBlockRewardAmount = RewardAmount;
type DappsStakingTvlProvider = TvlProvider;
type BeneficiaryPayout = BeneficiaryPayout;
type WeightInfo = ();
Expand Down
108 changes: 18 additions & 90 deletions pallets/block-rewards-hybrid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn reward_distribution_as_expected() {
));

// Initial adjustment of TVL
adjust_tvl_percentage(Perbill::from_percent(30));
set_tvl(30);

// Issue rewards a couple of times and verify distribution is as expected
// also ensure that the non distributed reward amount is burn
Expand Down Expand Up @@ -257,65 +257,22 @@ pub fn non_distributed_reward_amount_is_burned() {
reward_config.clone()
));

// Initial adjustment of TVL
adjust_tvl_percentage(Perbill::from_percent(30));
for tvl in [30, 50, 70, 100] {
// Initial adjustment of TVL
set_tvl(tvl);

for _block in 1..=100 {
let total_issuance_before = <TestRuntime as Config>::Currency::total_issuance();
let distributed_rewards = Rewards::calculate(&reward_config);
let burned_amount = BLOCK_REWARD - distributed_rewards.sum();
for _block in 1..=100 {
let total_issuance_before = <TestRuntime as Config>::Currency::total_issuance();
let distributed_rewards = Rewards::calculate(&reward_config);
let burned_amount = BLOCK_REWARD - distributed_rewards.sum();

BlockReward::on_timestamp_set(0);
BlockReward::on_timestamp_set(0);

assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
total_issuance_before + BLOCK_REWARD - burned_amount
);
}

adjust_tvl_percentage(Perbill::from_percent(50));

for _block in 1..=100 {
let total_issuance_before = <TestRuntime as Config>::Currency::total_issuance();
let distributed_rewards = Rewards::calculate(&reward_config);
let burned_amount = BLOCK_REWARD - distributed_rewards.sum();

BlockReward::on_timestamp_set(0);

assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
total_issuance_before + BLOCK_REWARD - burned_amount
);
}

adjust_tvl_percentage(Perbill::from_percent(70));

for _block in 1..=100 {
let total_issuance_before = <TestRuntime as Config>::Currency::total_issuance();
let distributed_rewards = Rewards::calculate(&reward_config);
let burned_amount = BLOCK_REWARD - distributed_rewards.sum();

BlockReward::on_timestamp_set(0);

assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
total_issuance_before + BLOCK_REWARD - burned_amount
);
}

adjust_tvl_percentage(Perbill::from_percent(100));

for _block in 1..=100 {
let total_issuance_before = <TestRuntime as Config>::Currency::total_issuance();
let distributed_rewards = Rewards::calculate(&reward_config);
let burned_amount = BLOCK_REWARD - distributed_rewards.sum();

BlockReward::on_timestamp_set(0);

assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
total_issuance_before + BLOCK_REWARD - burned_amount
);
assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
total_issuance_before + BLOCK_REWARD - burned_amount
);
}
}
})
}
Expand Down Expand Up @@ -493,38 +450,9 @@ impl Rewards {

fn sum(&self) -> Balance {
self.base_staker_reward
.saturating_add(self.adjustable_staker_reward)
.saturating_add(self.collators_reward)
.saturating_add(self.dapps_reward)
.saturating_add(self.treasury_reward)
}
}

/// Adjusts total_issuance in order to try-and-match the requested TVL percentage
fn adjust_tvl_percentage(desired_tvl_percentage: Perbill) {
// Calculate the required total issuance
let tvl = <TestRuntime as Config>::DappsStakingTvlProvider::get();
let required_total_issuance = desired_tvl_percentage.saturating_reciprocal_mul(tvl);

// Calculate how much more we need to issue in order to get the desired TVL percentage
let init_total_issuance = <TestRuntime as Config>::Currency::total_issuance();

// issue to if issuance should be greater
// or burn if it should be lower
if required_total_issuance > init_total_issuance {
let to_issue = required_total_issuance.saturating_sub(init_total_issuance);
<TestRuntime as Config>::Currency::resolve_creating(
&1,
<TestRuntime as Config>::Currency::issue(to_issue),
);
} else {
let to_burn = init_total_issuance.saturating_sub(required_total_issuance);
_ = <TestRuntime as Config>::Currency::slash(&1, to_burn);
+ self.adjustable_staker_reward
+ self.collators_reward
+ self.dapps_reward
+ self.treasury_reward
}

// Sanity check
assert_eq!(
<TestRuntime as Config>::Currency::total_issuance(),
required_total_issuance
);
}
4 changes: 2 additions & 2 deletions runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,14 @@ impl block_rewards_hybrid::BeneficiaryPayout<NegativeImbalance> for BeneficiaryP
}

parameter_types! {
pub const RewardAmount: Balance = 2_664 * MILLIAST;
pub const MaxBlockRewardAmount: Balance = 2_664 * MILLIAST;
}

impl block_rewards_hybrid::Config for Runtime {
type Currency = Balances;
type DappsStakingTvlProvider = DappsStakingTvlProvider;
type BeneficiaryPayout = BeneficiaryPayout;
type RewardAmount = RewardAmount;
type MaxBlockRewardAmount = MaxBlockRewardAmount;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = block_rewards_hybrid::weights::SubstrateWeight<Runtime>;
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,14 @@ impl block_rewards_hybrid::BeneficiaryPayout<NegativeImbalance> for BeneficiaryP
}

parameter_types! {
pub const RewardAmount: Balance = 2_530 * MILLISBY;
pub const MaxBlockRewardAmount: Balance = 2_530 * MILLISBY;
}

impl block_rewards_hybrid::Config for Runtime {
type Currency = Balances;
type DappsStakingTvlProvider = DappsStakingTvlProvider;
type BeneficiaryPayout = BeneficiaryPayout;
type RewardAmount = RewardAmount;
type MaxBlockRewardAmount = MaxBlockRewardAmount;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = block_rewards_hybrid::weights::SubstrateWeight<Runtime>;
}
Expand Down

0 comments on commit 7c09144

Please sign in to comment.