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 15 commits into
base: master
Choose a base branch
from

Conversation

runcomet
Copy link

@runcomet runcomet commented Oct 28, 2024

Derived Dev Accounts

Resolves #6040

Description

Enables an arbitrary number of dev accounts at genesis from a derivation path, feature gated by runtime-benchmarks.

Notes

  • In the absence of a derivation path(i.e. dev_accounts: (...., None) at genesis), the system defaults to the path //Sender/{}.

  • One dev account is created by default with the derivation path //Sender/{}.

  • Developer accounts are excluded from the total issuance of the token supply, ensuring they do not impact the overall balance at genesis.

@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

Choose a reason for hiding this comment

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

I should add that there are test environments in other pallets that use .default().build()

using Option<> route with a default //Sender/{} would require the defaults to have at least one dev_account in each occurence.

@runcomet runcomet changed the title Balances: Allow Configurable Number of Genesis Accounts with Specified Balances for Benchmarking Balances: Configurable Number of Genesis Accounts with Specified Balances for Benchmarking Nov 30, 2024
@runcomet
Copy link
Author

runcomet commented Dec 1, 2024

Should be ready for another review @michalkucharczyk @ggwpez

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