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

Pallet inflation #1091

Merged
merged 22 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 18 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
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pallet-xc-asset-config = { path = "./pallets/xc-asset-config", default-features
pallet-xvm = { path = "./pallets/xvm", default-features = false }
pallet-xcm = { path = "./pallets/pallet-xcm", default-features = false }
pallet-ethereum-checked = { path = "./pallets/ethereum-checked", default-features = false }
pallet-inflation = { path = "./pallets/inflation", default-features = false }

astar-primitives = { path = "./primitives", default-features = false }

Expand Down
7 changes: 5 additions & 2 deletions bin/collator/src/local/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use local_runtime::{
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, BalancesConfig, BaseFeeConfig,
BlockRewardConfig, CouncilConfig, DappStakingConfig, DemocracyConfig, EVMConfig, GenesisConfig,
GrandpaConfig, GrandpaId, Precompiles, Signature, SudoConfig, SystemConfig,
TechnicalCommitteeConfig, TreasuryConfig, VestingConfig, AST,
GrandpaConfig, GrandpaId, InflationConfig, InflationParameters, Precompiles, Signature,
SudoConfig, SystemConfig, TechnicalCommitteeConfig, TreasuryConfig, VestingConfig, AST,
};
use sc_service::ChainType;
use sp_core::{crypto::Ss58Codec, sr25519, Pair, Public};
Expand Down Expand Up @@ -213,6 +213,9 @@ fn testnet_genesis(
],
slots_per_tier: vec![10, 20, 30, 40],
},
inflation: InflationConfig {
params: InflationParameters::default(),
},
}
}

Expand Down
48 changes: 48 additions & 0 deletions pallets/inflation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "pallet-inflation"
version = "0.1.0"
license = "GPL-3.0-or-later"
description = "Manages inflation rate & inflation distribution"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
parity-scale-codec = { workspace = true }
serde = { workspace = true }

astar-primitives = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
scale-info = { workspace = true }
sp-runtime = { workspace = true }

frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
pallet-balances = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"sp-core/std",
"scale-info/std",
"serde/std",
"frame-support/std",
"frame-system/std",
"pallet-timestamp/std",
"pallet-balances/std",
"astar-primitives/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
178 changes: 178 additions & 0 deletions pallets/inflation/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use super::*;

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());
}

// Set up initial config in the database, so it's not empty.
fn initial_config<T: Config>() {
// Some dummy inflation params
let params = InflationParameters {
max_inflation_rate: Perquintill::from_percent(7),
treasury_part: Perquintill::from_percent(5),
collators_part: Perquintill::from_percent(3),
dapps_part: Perquintill::from_percent(20),
base_stakers_part: Perquintill::from_percent(25),
adjustable_stakers_part: Perquintill::from_percent(35),
bonus_part: Perquintill::from_percent(12),
ideal_staking_rate: Perquintill::from_percent(50),
};
assert!(params.is_valid());

// Some dummy inflation config
let config = InflationConfiguration {
recalculation_block: 123,
collator_reward_per_block: 11111,
treasury_reward_per_block: 33333,
dapp_reward_pool_per_era: 55555,
base_staker_reward_pool_per_era: 77777,
adjustable_staker_reward_pool_per_era: 99999,
bonus_reward_pool_per_period: 123987,
ideal_staking_rate: Perquintill::from_percent(50),
};

// Some dummy inflation tracker
let tracker = InflationTracker {
cap: 1000000,
issued: 30000,
};
assert!(tracker.issued <= tracker.cap);

InflationParams::<T>::put(params);
ActiveInflationConfig::<T>::put(config);
SafetyInflationTracker::<T>::put(tracker);

// Create some issuance so it's not zero
let dummy_account = whitelisted_caller();
T::Currency::make_free_balance_be(&dummy_account, 1_000_000_000_000_000_000_000);
}

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn force_set_inflation_params() {
initial_config::<T>();

let params = InflationParameters::default();
assert!(params.is_valid());

#[extrinsic_call]
_(RawOrigin::Root, params);

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

#[benchmark]
fn force_set_inflation_config() {
initial_config::<T>();
let config = InflationConfiguration::default();

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

assert_last_event::<T>(Event::<T>::InflationConfigurationForceChanged { config }.into());
}

#[benchmark]
fn force_inflation_recalculation() {
initial_config::<T>();

#[extrinsic_call]
_(RawOrigin::Root);

let config = ActiveInflationConfig::<T>::get();
assert_last_event::<T>(Event::<T>::ForcedInflationRecalculation { config }.into());
}

#[benchmark]
fn hook_with_recalculation() {
initial_config::<T>();

ActiveInflationConfig::<T>::mutate(|config| {
config.recalculation_block = 0;
});

let block = 1;
#[block]
{
Pallet::<T>::on_initialize(block);
Pallet::<T>::on_finalize(block);
}

assert!(ActiveInflationConfig::<T>::get().recalculation_block > 0);
}

#[benchmark]
fn hook_without_recalculation() {
initial_config::<T>();

ActiveInflationConfig::<T>::mutate(|config| {
config.recalculation_block = 2;
});
let init_config = ActiveInflationConfig::<T>::get();

// Has to be at least 2 blocks less than the recalculation block.
let block = 0;
#[block]
{
Pallet::<T>::on_initialize(block);
Pallet::<T>::on_finalize(block);
}

assert_eq!(ActiveInflationConfig::<T>::get(), init_config);
}

#[benchmark]
fn on_timestamp_set() {
initial_config::<T>();
let init_tracker = SafetyInflationTracker::<T>::get();

#[block]
{
Pallet::<T>::on_timestamp_set(1);
}

// The 'sane' assumption is that at least something will be issued for treasury & collators
assert!(SafetyInflationTracker::<T>::get().issued > init_tracker.issued);
}

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

#[cfg(test)]
mod tests {
use crate::mock;
use frame_support::sp_io::TestExternalities;

pub fn new_test_ext() -> TestExternalities {
mock::ExternalityBuilder::build()
}
}
Loading
Loading