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 bridges/modules/messages/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ pub fn inbound_unrewarded_relayers_state(lane: TestLaneIdType) -> UnrewardedRela
/// Return test externalities to use in tests.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(ENDOWED_ACCOUNT, 1_000_000)] }
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(ENDOWED_ACCOUNT, 1_000_000)], ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
sp_io::TestExternalities::new(t)
Expand Down
2 changes: 1 addition & 1 deletion cumulus/pallets/collator-selection/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
invulnerables,
};
let session = pallet_session::GenesisConfig::<Test> { keys, ..Default::default() };
pallet_balances::GenesisConfig::<Test> { balances }
pallet_balances::GenesisConfig::<Test> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
// collator selection must be initialized before session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn asset_hub_rococo_genesis(
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
..Default::default()
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn asset_hub_westend_genesis(
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
..Default::default()
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn bridge_hub_rococo_genesis(
.cloned()
.map(|k| (k, 1u128 << 60))
.collect::<Vec<_>>(),
..Default::default()
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn bridge_hub_westend_genesis(
.cloned()
.map(|k| (k, 1u128 << 60))
.collect::<Vec<_>>(),
..Default::default()
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn collectives_westend_genesis(
.cloned()
.map(|k| (k, COLLECTIVES_WESTEND_ED * 4096))
.collect::<Vec<_>>(),
..Default::default()
},
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
Expand Down
2 changes: 1 addition & 1 deletion cumulus/parachains/runtimes/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<Runtime: BasicParachainRuntime> ExtBuilder<Runtime> {
.unwrap();
}

pallet_balances::GenesisConfig::<Runtime> { balances: self.balances }
pallet_balances::GenesisConfig::<Runtime> { balances: self.balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/assigned_slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ mod tests {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/auctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ mod tests {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/crowdloan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ mod tests {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 1000), (2, 2000), (3, 3000), (4, 4000)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/paras_registrar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ mod tests {

pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10_000_000), (2, 10_000_000), (3, 10_000_000)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ mod tests {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fn rococo_testnet_genesis(
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
..Default::default()
},
session: SessionConfig {
keys: initial_authorities
Expand Down Expand Up @@ -428,6 +429,7 @@ fn rococo_staging_testnet_config_genesis() -> serde_json::Value {
.map(|k: &AccountId| (k.clone(), ENDOWMENT))
.chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
.collect::<Vec<_>>(),
..Default::default()
},
session: SessionConfig {
keys: initial_authorities
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/westend/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ fn westend_testnet_genesis(
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
..Default::default()
},
session: SessionConfig {
keys: initial_authorities
Expand Down Expand Up @@ -345,6 +346,7 @@ fn westend_staging_testnet_config_genesis() -> serde_json::Value {
.map(|k: &AccountId| (k.clone(), ENDOWMENT))
.chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
.collect::<Vec<_>>(),
..Default::default()
},
session: SessionConfig {
keys: initial_authorities
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/pallet-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ pub(crate) fn new_test_ext_with_balances_and_xcm_version(
) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Test> { balances }
pallet_balances::GenesisConfig::<Test> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(0, INITIAL_BALANCE), (1, INITIAL_BALANCE), (2, INITIAL_BALANCE)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-builder/tests/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ construct_runtime!(
pub fn kusama_like_with_balances(balances: Vec<(AccountId, Balance)>) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<Runtime> { balances }
pallet_balances::GenesisConfig::<Runtime> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions polkadot/xcm/xcm-runtime-apis/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl pallet_xcm::Config for TestRuntime {
pub fn new_test_ext_with_balances(balances: Vec<(AccountId, Balance)>) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<TestRuntime> { balances }
pallet_balances::GenesisConfig::<TestRuntime> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand All @@ -380,7 +380,7 @@ pub fn new_test_ext_with_balances_and_assets(
) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();

pallet_balances::GenesisConfig::<TestRuntime> { balances }
pallet_balances::GenesisConfig::<TestRuntime> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
2 changes: 2 additions & 0 deletions polkadot/xcm/xcm-simulator/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, INITIAL_BALANCE), (parent_account_id(), INITIAL_BALANCE)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand All @@ -125,6 +126,7 @@ pub fn relay_ext() -> sp_io::TestExternalities {
(child_account_id(1), INITIAL_BALANCE),
(child_account_id(2), INITIAL_BALANCE),
],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion polkadot/xcm/xcm-simulator/fuzzer/src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {

pallet_balances::GenesisConfig::<Runtime> {
balances: (0..6).map(|i| ([i; 32].into(), INITIAL_BALANCE)).collect(),
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand All @@ -138,7 +139,7 @@ pub fn relay_ext() -> sp_io::TestExternalities {
balances.append(&mut (1..=3).map(|i| (para_account_id(i), INITIAL_BALANCE)).collect());
balances.append(&mut (0..6).map(|i| ([i; 32].into(), INITIAL_BALANCE)).collect());

pallet_balances::GenesisConfig::<Runtime> { balances }
pallet_balances::GenesisConfig::<Runtime> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn config_endowed(extra_endowed: Vec<AccountId>) -> RuntimeGenesisConfig {

RuntimeGenesisConfig {
indices: IndicesConfig { indices: vec![] },
balances: BalancesConfig { balances: endowed },
balances: BalancesConfig { balances: endowed, ..Default::default() },
session: SessionConfig {
keys: vec![
(alice(), dave(), session_keys_from_seed(Ed25519Keyring::Alice.into())),
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
(8, 1000),
(9, 1000),
],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/asset-conversion/ops/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities {

pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10000), (2, 20000), (3, 30000), (4, 40000)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/asset-conversion/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities {

pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10000), (2, 20000), (3, 30000), (4, 40000)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub fn new_test_ext_raw_authorities(authorities: Vec<AuthorityId>) -> sp_io::Tes

let balances: Vec<_> = (0..authorities.len()).map(|i| (i as u64, 10_000_000)).collect();

pallet_balances::GenesisConfig::<Test> { balances }
pallet_balances::GenesisConfig::<Test> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
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 }

[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
63 changes: 62 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, Option<String>),
}

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: (One::one(), <T as Config<I>>::ExistentialDeposit::get(), None),
}
}
}

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

// Generate additional dev accounts.
#[cfg(feature = "runtime-benchmarks")]
{
let (num_accounts, balance, ref derivation) = self.dev_accounts;

// Check if `derivation` is `Some` and generate key pair
if let Some(derivation_string) = &derivation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use some default (//Sender/{}) here, if None is given in genesis config.

Copy link
Author

@runcomet runcomet Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be placed in a helper function

/// Generate dev account from derivation string.
#[cfg(feature = "runtime-benchmarks")]
pub fn derive_dev_account(
	num_accounts: u32,
	balance: T::Balance,
	derivation: &String,
) {

	// Ensure that the number of accounts is not zero
	assert!(num_accounts > 0, "num_accounts must be greater than zero");

	assert!(
		balance >= <T as Config<I>>::ExistentialDeposit::get(),
		"the balance of any account should always be at least the existential deposit.",
	);

	for index in 0..num_accounts {
		// Replace "{}" in the derivation string with the index.
		let derivation_string = derivation.replace("{}", &index.to_string());

		// Attempt to create the key pair from the derivation string with error handling.
		let pair: SrPair = Pair::from_string(&derivation_string, None)
			.expect(&format!("Failed to parse derivation string: {}", derivation_string));

		// Convert the public key to AccountId.
		let who = T::AccountId::decode(&mut &pair.public().encode()[..])
			.expect(&format!("Failed to decode public key from pair: {}", pair.public()));
				
		frame_system::Pallet::<T>::inc_providers(&who);
		// Insert the account into the store and ensure it succeeds.
		assert!(T::AccountStore::insert(
			&who,
			AccountData { free: balance, ..Default::default() }
		)
			.is_ok());
	}
}

Copy link
Author

@runcomet runcomet Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to generate one dev account with a balance in the pallets that require balances by default?

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

			#[cfg(feature = "runtime-benchmarks")]
			dev_accounts: (One::one(), <T as Config<I>>::ExistentialDeposit::get(), None),
		}
	}
}

Pallet::<T, I>::derive_dev_account(num_accounts, balance, derivation_string);
} else {
// Derivation string is missing, using default..
let default_derivation = "//Sender/{}".to_string();

Pallet::<T, I>::derive_dev_account(num_accounts, balance, &default_derivation);
}
}

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 Expand Up @@ -1248,5 +1276,38 @@ pub mod pallet {
});
Ok(actual)
}

/// Generate dev account from derivation string.
#[cfg(feature = "runtime-benchmarks")]
pub fn derive_dev_account(num_accounts: u32, balance: T::Balance, derivation: &String) {
// Ensure that the number of accounts is not zero
assert!(num_accounts > 0, "num_accounts must be greater than zero");

assert!(
balance >= <T as Config<I>>::ExistentialDeposit::get(),
"the balance of any account should always be at least the existential deposit.",
);

for index in 0..num_accounts {
// Replace "{}" in the derivation string with the index.
let derivation_string = derivation.replace("{}", &index.to_string());

// Attempt to create the key pair from the derivation string with error handling.
let pair: SrPair = Pair::from_string(&derivation_string, None)
.expect(&format!("Failed to parse derivation string: {}", derivation_string));

// Convert the public key to AccountId.
let who = T::AccountId::decode(&mut &pair.public().encode()[..])
.expect(&format!("Failed to decode public key from pair: {}", pair.public()));

frame_system::Pallet::<T>::inc_providers(&who);
// Insert the account into the store and ensure it succeeds.
assert!(T::AccountStore::insert(
&who,
AccountData { free: balance, ..Default::default() }
)
.is_ok());
}
}
}
}
11 changes: 7 additions & 4 deletions substrate/frame/balances/src/tests/currency_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ 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)], ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
}
Expand All @@ -730,9 +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)] }
.assimilate_storage(&mut t)
.unwrap();
let _ = crate::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (1, 15)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
}

#[test]
Expand Down
Loading