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

Balances: Allow Configurable Number of Genesis Accounts with Specified Balances for Benchmarking #6267

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion substrate/frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
sp-runtime = { workspace = true }
docify = { workspace = true }
sp-core = { workspace = true, default-features = true }
runcomet marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
pallet-transaction-payment = { workspace = true, default-features = true }
frame-support = { features = ["experimental"], workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
paste = { workspace = true, default-features = true }

Expand Down
35 changes: 34 additions & 1 deletion substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ use sp_runtime::{
},
ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
};

#[cfg(feature = "runtime-benchmarks")]
use sp_core::{sr25519::Pair as SrPair, Pair};

pub use types::{
AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
};
Expand Down Expand Up @@ -505,11 +509,19 @@ pub mod pallet {
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub balances: Vec<(T::AccountId, T::Balance)>,

#[cfg(feature = "runtime-benchmarks")]
pub dev_accounts: (u32, T::Balance, String),
runcomet marked this conversation as resolved.
Show resolved Hide resolved
}

impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { balances: Default::default() }
Self {
balances: Default::default(),

#[cfg(feature = "runtime-benchmarks")]
dev_accounts: (Default::default(), Default::default(), String::new()),
}
}
}

Expand Down Expand Up @@ -540,6 +552,27 @@ pub mod pallet {
"duplicate balances in genesis."
);

// Generate additional dev accounts.
#[cfg(feature = "runtime-benchmarks")]
{
let (num_accounts, balance, ref derivation) = self.dev_accounts;
for index in 0..num_accounts {
assert!(
balance >= <T as Config<I>>::ExistentialDeposit::get(),
"the balance of any account should always be at least the existential deposit.",
);
// Create key pair from the derivation string
let derivation_string = &derivation.replace("{}", &index.to_string());
let pair: SrPair = Pair::from_string(&derivation_string, None).expect("Invalid derivation string");

// Convert the public key to AccountId
let who = T::AccountId::decode(&mut &pair.public().encode()[..]).unwrap();
frame_system::Pallet::<T>::inc_providers(&who);
assert!(T::AccountStore::insert(&who, AccountData { free: balance, ..Default::default() })
.is_ok());
}
}

for &(ref who, free) in self.balances.iter() {
frame_system::Pallet::<T>::inc_providers(who);
assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() })
Expand Down
14 changes: 12 additions & 2 deletions substrate/frame/balances/src/tests/currency_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,12 @@ fn burn_must_work() {
fn cannot_set_genesis_value_below_ed() {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = 11);
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let _ = crate::GenesisConfig::<Test> { balances: vec![(1, 10)] }
let _ = crate::GenesisConfig::<Test> {
balances: vec![(1, 10)],

#[cfg(feature = "runtime-benchmarks")]
dev_accounts: (1000000, 500, "//Sender/{}".to_string())
}
.assimilate_storage(&mut t)
.unwrap();
}
Expand All @@ -725,7 +730,12 @@ fn cannot_set_genesis_value_below_ed() {
#[should_panic = "duplicate balances in genesis."]
fn cannot_set_genesis_value_twice() {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let _ = crate::GenesisConfig::<Test> { balances: vec![(1, 10), (2, 20), (1, 15)] }
let _ = crate::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (1, 15)],

#[cfg(feature = "runtime-benchmarks")]
dev_accounts: (1000000, 500, "//Sender/{}".to_string())
}
.assimilate_storage(&mut t)
.unwrap();
}
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/balances/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ impl ExtBuilder {
} else {
vec![]
},
#[cfg(feature = "runtime-benchmarks")]
dev_accounts: (1000000, self.existential_deposit, "//Sender/{}".to_string())
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down