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

Conversation

runcomet
Copy link

Derived Dev Accounts

Resolves #6040

@cla-bot-2021
Copy link

cla-bot-2021 bot commented Oct 28, 2024

User @runcomet, please sign the CLA here.

@runcomet runcomet marked this pull request as ready for review November 5, 2024 20:30
@runcomet runcomet requested review from cheme and a team as code owners November 5, 2024 20:30
@paritytech-review-bot paritytech-review-bot bot requested a review from a team November 5, 2024 20:31
@runcomet
Copy link
Author

Balances of dev_accounts should not be included in the total issuance? @ggwpez

@runcomet
Copy link
Author

Hello @michalkucharczyk @ggwpez please review 🤝

{
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),
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Balances: Arbitrary number of genesis accounts
3 participants