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: Configurable Number of Genesis Accounts with Specified Balances for Benchmarking #6267

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f3bc304
add dev_accounts field
runcomet Oct 28, 2024
cb0d3ba
populate dev_accounts field
runcomet Nov 4, 2024
78fa5fc
use default dev_accounts
runcomet Nov 4, 2024
a4a1eff
BalancesConfig
runcomet Nov 4, 2024
3c968f4
Merge branch 'master' into dev-accounts
runcomet Nov 5, 2024
3163d5f
configure ensure_ti_valid awareness of dev_accounts
runcomet Nov 13, 2024
d8701a8
Merge branch 'master' into dev-accounts
runcomet Nov 13, 2024
25ca49c
nit
runcomet Nov 14, 2024
4cac3a8
Option String
runcomet Nov 21, 2024
b3e4daf
Merge branch 'master' into dev-accounts
runcomet Nov 21, 2024
beb8056
doc fix
runcomet Nov 21, 2024
e647bca
derive_dev_account helper function
runcomet Nov 22, 2024
e8efd68
add prdoc
runcomet Nov 30, 2024
dddac55
Merge branch 'master' into dev-accounts
runcomet Nov 30, 2024
d53d795
adjust genesisconfig addition
runcomet Dec 1, 2024
9128ab9
nit
runcomet Dec 2, 2024
4e8ae42
Merge branch 'master' into dev-accounts
runcomet Dec 11, 2024
be5acd6
remove feature flag
runcomet Dec 27, 2024
fed102f
mutate_account
runcomet Jan 3, 2025
5e8f9e7
update PR doc
runcomet Jan 3, 2025
0474e2c
as_deref, dev_accounts
runcomet Jan 9, 2025
00a7a0f
DEFAULT_ADDRESS_URI
runcomet Jan 9, 2025
88fc87a
fix conficts + Optional dev_accounts + hard derivation
runcomet Jan 10, 2025
c987262
Merge branch 'master' into dev-accounts
runcomet Jan 11, 2025
589af1f
cargo +nightly fmt
runcomet Jan 11, 2025
d13f0d6
add dev_accounts field in missing pallet tests
runcomet Jan 11, 2025
dea6676
CI check to confirm chain-spec issue
runcomet Jan 11, 2025
5b4e062
devAccounts: null
runcomet Jan 15, 2025
f62a76a
chain-spec tests devAccounts
runcomet Jan 15, 2025
9b6046e
taplo
runcomet Jan 15, 2025
d002bf3
../../../.cofig/taplo.toml
runcomet Jan 15, 2025
b3f5c45
prdoc patch & major
runcomet Jan 15, 2025
e735769
Merge branch 'master' into dev-accounts
runcomet Jan 15, 2025
c8c49f9
Merge branch 'master' into dev-accounts
runcomet Jan 15, 2025
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
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")]
runcomet marked this conversation as resolved.
Show resolved Hide resolved
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