Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Make all work properly and nice!!
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed Dec 13, 2022
1 parent 8085d07 commit d5bc5b8
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 56 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.

3 changes: 2 additions & 1 deletion frame/assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ frame-support = { version = "4.0.0-dev", default-features = false, path = "../su
# `system` module provides us with all sorts of useful stuff and macros depend on it being around.
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }

[dev-dependencies]
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-std = { version = "5.0.0", path = "../../primitives/std" }
sp-io = { version = "7.0.0", path = "../../primitives/io" }
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
Expand All @@ -35,6 +35,7 @@ default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-core/std",
"sp-std/std",
"sp-runtime/std",
"frame-support/std",
Expand Down
2 changes: 2 additions & 0 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,3 +1459,5 @@ pub mod pallet {
}
}
}

sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);
58 changes: 30 additions & 28 deletions frame/bags-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,26 @@ impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I>
List::<T, I>::unsafe_clear()
}

#[cfg(feature = "runtime-benchmarks")]
fn score_update_worst_case(who: &T::AccountId, is_increase: bool) -> Self::Score {
use frame_support::traits::Get as _;
let thresholds = T::BagThresholds::get();
let node = list::Node::<T, I>::get(who).unwrap();
let current_bag_idx = thresholds
.iter()
.chain(sp_std::iter::once(&T::Score::max_value()))
.position(|w| w == &node.bag_upper())
.unwrap();

if is_increase {
let next_threshold_idx = current_bag_idx + 1;
assert!(thresholds.len() > next_threshold_idx);
thresholds[next_threshold_idx]
} else {
assert!(current_bag_idx != 0);
let prev_threshold_idx = current_bag_idx - 1;
thresholds[prev_threshold_idx]
frame_election_provider_support::runtime_benchmarks_enabled! {
fn score_update_worst_case(who: &T::AccountId, is_increase: bool) -> Self::Score {
use frame_support::traits::Get as _;
let thresholds = T::BagThresholds::get();
let node = list::Node::<T, I>::get(who).unwrap();
let current_bag_idx = thresholds
.iter()
.chain(sp_std::iter::once(&T::Score::max_value()))
.position(|w| w == &node.bag_upper)
.unwrap();

if is_increase {
let next_threshold_idx = current_bag_idx + 1;
assert!(thresholds.len() > next_threshold_idx);
thresholds[next_threshold_idx]
} else {
assert!(current_bag_idx != 0);
let prev_threshold_idx = current_bag_idx - 1;
thresholds[prev_threshold_idx]
}
}
}
}
Expand All @@ -387,14 +388,15 @@ impl<T: Config<I>, I: 'static> ScoreProvider<T::AccountId> for Pallet<T, I> {
Node::<T, I>::get(id).map(|node| node.score()).unwrap_or_default()
}

#[cfg(any(feature = "runtime-benchmarks", feature = "fuzz", test))]
fn set_score_of(id: &T::AccountId, new_score: T::Score) {
ListNodes::<T, I>::mutate(id, |maybe_node| {
if let Some(node) = maybe_node.as_mut() {
node.set_score(new_score)
} else {
panic!("trying to mutate {:?} which does not exists", id);
}
})
frame_election_provider_support::runtime_benchmarks_or_fuzz_enabled! {
fn set_score_of(id: &T::AccountId, new_score: T::Score) {
ListNodes::<T, I>::mutate(id, |maybe_node| {
if let Some(node) = maybe_node.as_mut() {
node.score = new_score;
} else {
panic!("trying to mutate {:?} which does not exists", id);
}
})
}
}
}
3 changes: 2 additions & 1 deletion frame/election-provider-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ sp-arithmetic = { version = "6.0.0", default-features = false, path = "../../pri
sp-npos-elections = { version = "4.0.0-dev", default-features = false, path = "../../primitives/npos-elections" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }

[dev-dependencies]
rand = "0.7.3"
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-io = { version = "7.0.0", path = "../../primitives/io" }
sp-npos-elections = { version = "4.0.0-dev", path = "../../primitives/npos-elections" }

Expand All @@ -38,6 +38,7 @@ std = [
"scale-info/std",
"sp-arithmetic/std",
"sp-npos-elections/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
Expand Down
3 changes: 3 additions & 0 deletions frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,6 @@ pub type BoundedSupportsOf<E> = BoundedSupports<
<E as ElectionProviderBase>::AccountId,
<E as ElectionProviderBase>::MaxWinners,
>;

sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);
sp_core::generate_feature_enabled_macro!(runtime_benchmarks_or_fuzz_enabled, any(feature = "runtime-benchmarks", feature = "fuzzing"), $);
2 changes: 1 addition & 1 deletion frame/fast-unstake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pallet-staking-reward-curve = { version = "4.0.0-dev", path = "../staking/reward
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }
substrate-test-utils = { version = "4.0.0-dev", path = "../../test-utils" }
sp-tracing = { version = "6.0.0", path = "../../primitives/tracing" }
pallet-staking = { path = "../staking", features = [ "runtime-benchmarks" ] }
pallet-staking = { path = "../staking" }
pallet-balances = { path = "../balances" }
pallet-timestamp = { path = "../timestamp" }

Expand Down
2 changes: 1 addition & 1 deletion frame/nomination-pools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sp-core = { version = "7.0.0", default-features = false, path = "../../primitive
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
log = { version = "0.4.0", default-features = false }

# Optional: usef for testing and/or fuzzing
# Optional: use for testing and/or fuzzing
pallet-balances = { version = "4.0.0-dev", path = "../balances", optional = true }
sp-tracing = { version = "6.0.0", path = "../../primitives/tracing", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ sp-npos-elections = { version = "4.0.0-dev", path = "../../primitives/npos-elect
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
pallet-timestamp = { version = "4.0.0-dev", path = "../timestamp" }
pallet-staking-reward-curve = { version = "4.0.0-dev", path = "../staking/reward-curve" }
pallet-bags-list = { version = "4.0.0-dev", path = "../bags-list", features = [ "runtime-benchmarks" ] }
pallet-bags-list = { version = "4.0.0-dev", path = "../bags-list" }
substrate-test-utils = { version = "4.0.0-dev", path = "../../test-utils" }
frame-benchmarking = { version = "4.0.0-dev", path = "../benchmarking" }
frame-election-provider-support = { version = "4.0.0-dev", path = "../election-provider-support" }
Expand Down
39 changes: 19 additions & 20 deletions frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,28 +1603,27 @@ impl<T: Config> StakingInterface for Pallet<T> {
Self::nominate(RawOrigin::Signed(ctrl).into(), targets)
}

#[cfg(feature = "runtime-benchmarks")]
fn nominations(who: Self::AccountId) -> Option<Vec<T::AccountId>> {
Nominators::<T>::get(who).map(|n| n.targets.into_inner())
}
sp_staking::runtime_benchmarks_enabled! {
fn nominations(who: Self::AccountId) -> Option<Vec<T::AccountId>> {
Nominators::<T>::get(who).map(|n| n.targets.into_inner())
}

#[cfg(feature = "runtime-benchmarks")]
fn add_era_stakers(
current_era: &EraIndex,
stash: &T::AccountId,
exposures: Vec<(Self::AccountId, Self::Balance)>,
) {
let others = exposures
.iter()
.map(|(who, value)| IndividualExposure { who: who.clone(), value: value.clone() })
.collect::<Vec<_>>();
let exposure = Exposure { total: Default::default(), own: Default::default(), others };
Self::add_era_stakers(current_era.clone(), stash.clone(), exposure)
}
fn add_era_stakers(
current_era: &EraIndex,
stash: &T::AccountId,
exposures: Vec<(Self::AccountId, Self::Balance)>,
) {
let others = exposures
.iter()
.map(|(who, value)| IndividualExposure { who: who.clone(), value: value.clone() })
.collect::<Vec<_>>();
let exposure = Exposure { total: Default::default(), own: Default::default(), others };
<ErasStakers<T>>::insert(&current_era, &stash, &exposure);
}

#[cfg(feature = "runtime-benchmarks")]
fn set_current_era(era: EraIndex) {
CurrentEra::<T>::put(era);
fn set_current_era(era: EraIndex) {
CurrentEra::<T>::put(era);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/transaction-payment/asset-tx-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ serde_json = "1.0.85"

sp-storage = { version = "7.0.0", default-features = false, path = "../../../primitives/storage" }

pallet-assets = { version = "4.0.0-dev", path = "../../assets", features = [ "runtime-benchmarks" ] }
pallet-assets = { version = "4.0.0-dev", path = "../../assets" }
pallet-authorship = { version = "4.0.0-dev", path = "../../authorship" }
pallet-balances = { version = "4.0.0-dev", path = "../../balances" }

Expand Down
5 changes: 3 additions & 2 deletions frame/transaction-payment/asset-tx-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ impl pallet_assets::Config for Runtime {
type Extra = ();
type WeightInfo = ();
type RemoveItemsLimit = ConstU32<1000>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
pallet_assets::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
}

pub struct HardcodedAuthor;
Expand Down
43 changes: 43 additions & 0 deletions primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,46 @@ macro_rules! bounded_btree_map {
}
};
}

/// Generates a macro for checking if a certain feature is enabled.
///
/// These feature checking macros can be used to conditionally enable/disable code in a dependent
/// crate based on a feature in the crate where the macro is called.
#[macro_export]
macro_rules! generate_feature_enabled_macro {
( $macro_name:ident, $feature_name:meta, $d:tt ) => {
/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg($feature_name)]
#[macro_export]
macro_rules! $macro_name {
( $d ( $d input:tt )* ) => {
$d ( $d input )*
}
}

/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg(not($feature_name))]
#[macro_export]
macro_rules! $macro_name {
( $d ( $d input:tt )* ) => {};
}
};
}
2 changes: 2 additions & 0 deletions primitives/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
sp-core = { version = "7.0.0", default-features = false, path = "../core" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../runtime" }
sp-std = { version = "5.0.0", default-features = false, path = "../std" }

Expand All @@ -23,6 +24,7 @@ default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
Expand Down
2 changes: 2 additions & 0 deletions primitives/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,5 @@ pub trait StakingInterface {
#[cfg(feature = "runtime-benchmarks")]
fn set_current_era(era: EraIndex);
}

sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);

0 comments on commit d5bc5b8

Please sign in to comment.