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

Conversation

runcomet
Copy link
Contributor

@runcomet runcomet commented Oct 28, 2024

Derived Dev Accounts

Resolves #6040

Description

This update introduces support for creating an arbitrary number of developer accounts at the genesis block based on a specified derivation path. This functionality is gated by the runtime-benchmarks feature, ensuring it is only enabled during benchmarking scenarios.

Key Features

  • Arbitrary Dev Accounts at Genesis: Developers can now specify any number of accounts to be generated at genesis using a derivation path. This enables more flexibility for benchmarking use cases.

  • Default Derivation Path: If no derivation path is provided (i.e., when dev_accounts: (..., None) is set at genesis), the system will default to the path //Sender/{}.

  • Default Single Dev Account: A single developer account is created by default using the path //Sender/{}.

  • No Impact on Total Token Issuance: Developer accounts are excluded from the total issuance of the token supply at genesis, ensuring they do not affect the overall balance or token distribution.

polkadot address: 14SRqZTC1d8rfxL8W1tBTnfUBPU23ACFVPzp61FyGf4ftUFg

@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
Contributor Author

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

@runcomet
Copy link
Contributor Author

Hello @michalkucharczyk @ggwpez please review 🤝

@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
Contributor Author

runcomet commented Dec 1, 2024

Should be ready for another review @michalkucharczyk @ggwpez

Self {
balances: Default::default(),

#[cfg(feature = "runtime-benchmarks")]
Copy link
Member

Choose a reason for hiding this comment

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

As the benchmarks are a little bit different to the runtime benchmarks, I would just expose dev_accounts always. If not needed, people don't need to set it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As the benchmarks are a little bit different to the runtime benchmarks,

Could you explain a bit more @bkchr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe a link in the code differentiating benchmarks from runtime benchmarks? familiar with just the feature flag runtime-benchmarks.

substrate/frame/balances/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/balances/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/balances/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 1304 to 1308
.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()));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.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()));
.unwrap_or_else(|| format!("Failed to parse derivation string: {derivation_string}"));
// Convert the public key to AccountId.
let who = T::AccountId::decode(&mut &pair.public().encode()[..])
.unwrap_or_else(|| format!("Failed to decode public key from pair: {:?}", pair.public()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would expect a default value for the respective operations, .expect() can't be used at genesis?

@runcomet runcomet requested a review from bkchr December 28, 2024 13:15
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);
Copy link
Member

Choose a reason for hiding this comment

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

Why this instead of doing a force_set_balance or something?

The less cases where we manually modify account references, the better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Getting familiar with the codebase.

I prefer your suggestion.

Thanks for the review!

Copy link
Member

Choose a reason for hiding this comment

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

You can look at the tests of the balances pallet, it has plenty of examples on how to force-set the balance of an account 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used mutate_account_handling_dust

Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

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

Looks broadly good, thanks 😄

@runcomet
Copy link
Contributor Author

runcomet commented Jan 3, 2025

please review @ggwpez @bkchr

Comment on lines +566 to +572
// Check if `derivation` is `Some` and generate key pair
if let Some(derivation_string) = derivation {
Pallet::<T, I>::derive_dev_account(num_accounts, balance, derivation_string);
} else {
// Derivation string is missing, using default..
Pallet::<T, I>::derive_dev_account(num_accounts, balance, "//Sender/{}");
}
Copy link
Contributor

@michalkucharczyk michalkucharczyk Jan 6, 2025

Choose a reason for hiding this comment

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

Could be simplified maybe?

Suggested change
// Check if `derivation` is `Some` and generate key pair
if let Some(derivation_string) = derivation {
Pallet::<T, I>::derive_dev_account(num_accounts, balance, derivation_string);
} else {
// Derivation string is missing, using default..
Pallet::<T, I>::derive_dev_account(num_accounts, balance, "//Sender/{}");
}
// Use `derivation` (or default) to generate key pair
Pallet::<T, I>::derive_dev_account(num_accounts, balance, derivation.as_deref().unwrap_or("//Sender/{}"));

dev_accounts: (
One::one(),
<T as Config<I>>::ExistentialDeposit::get(),
Some("//Sender/{}".to_string()),
Copy link
Contributor

Choose a reason for hiding this comment

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

"//Sender/{}" could be defined as const to avoid duplications.

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
4 participants