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

Refactor get_account_id_from_seed / get_from_seed to one common place #5804

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7edba45
remove related func from integration-tests
programskillforverification Sep 23, 2024
320d9bd
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Sep 23, 2024
533f8dd
remove more and add from_str for ed25519
programskillforverification Sep 23, 2024
9c8aaff
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 2, 2024
12d0b3a
remove all
programskillforverification Oct 2, 2024
6c6d32b
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 4, 2024
81bf41c
simplify some code
programskillforverification Oct 7, 2024
8267fd6
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 7, 2024
63cee3d
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 7, 2024
a5fcf92
fix some errors
programskillforverification Oct 7, 2024
92dde5a
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 7, 2024
9db4b08
make ci happy
programskillforverification Oct 7, 2024
e85bdbe
Update prdoc/pr_5804.prdoc
programskillforverification Oct 8, 2024
f020937
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 8, 2024
0371cb1
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 9, 2024
4aa15ac
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 10, 2024
ef21eef
reuse get_from_seed
programskillforverification Oct 10, 2024
ab5b57f
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 11, 2024
ee39c0d
fix format not found
programskillforverification Oct 11, 2024
38d3aba
fix prdoc
programskillforverification Oct 11, 2024
41b0d6b
keep lowercase str
programskillforverification Oct 11, 2024
5f2364b
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 14, 2024
ae96dfe
rewirte helper func
programskillforverification Oct 14, 2024
48de94a
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 14, 2024
009fd5b
Update cumulus/parachains/integration-tests/emulated/tests/assets/ass…
programskillforverification Oct 15, 2024
f2be494
Update cumulus/parachains/integration-tests/emulated/tests/assets/ass…
programskillforverification Oct 15, 2024
eeb298b
Update substrate/primitives/core/src/crypto.rs
programskillforverification Oct 15, 2024
e9eb64b
Update substrate/primitives/core/src/crypto.rs
programskillforverification Oct 15, 2024
b86b000
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 15, 2024
b59cf20
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 16, 2024
262cfef
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 16, 2024
a161d07
adjust
programskillforverification Oct 16, 2024
f0e37bc
Update prdoc/pr_5804.prdoc
programskillforverification Oct 16, 2024
c7b31af
fix nit
programskillforverification Oct 16, 2024
6b39516
Merge branch 'master' into refactor-get_account_id_from_seed
programskillforverification Oct 17, 2024
341c689
remove magic number
programskillforverification Oct 17, 2024
b1fcb7f
Update substrate/primitives/keyring/src/ed25519.rs
programskillforverification Oct 17, 2024
d9ca423
Update substrate/primitives/keyring/src/sr25519.rs
programskillforverification Oct 17, 2024
5860e2d
Update substrate/primitives/keyring/src/lib.rs
programskillforverification Oct 17, 2024
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
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cumulus/xcm/xcm-emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ frame-support = { workspace = true, default-features = true }
frame-system = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-keyring = { workspace = true, default-features = true }
sp-crypto-hashing = { workspace = true, default-features = true }
sp-std = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
Expand Down
21 changes: 7 additions & 14 deletions cumulus/xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub use sp_arithmetic::traits::Bounded;
pub use sp_core::{parameter_types, sr25519, storage::Storage, Pair};
pub use sp_crypto_hashing::blake2_256;
pub use sp_io::TestExternalities;
pub use sp_keyring::Sr25519Keyring;
pub use sp_runtime::BoundedSlice;
pub use sp_tracing;

Expand Down Expand Up @@ -226,7 +227,12 @@ pub trait Chain: TestExt {
type OriginCaller;

fn account_id_of(seed: &str) -> AccountId {
helpers::get_account_id_from_seed::<sr25519::Public>(seed)
use sp_runtime::traits::IdentifyAccount;
use std::str::FromStr;
sp_runtime::MultiSigner::from(
Sr25519Keyring::from_str(seed).expect("should parse str seed to keyring"),
)
.into_account()
}

fn account_data_of(account: AccountIdOf<Self::Runtime>) -> AccountData<Balance>;
Expand Down Expand Up @@ -1608,17 +1614,4 @@ pub mod helpers {

ref_time_within && proof_size_within
}

/// Helper function to generate an account ID from seed.
pub fn get_account_id_from_seed<TPublic: sp_core::Public>(seed: &str) -> AccountId
where
sp_runtime::MultiSigner:
From<<<TPublic as sp_runtime::CryptoType>::Pair as sp_core::Pair>::Public>,
{
use sp_runtime::traits::IdentifyAccount;
let pubkey = TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public();
sp_runtime::MultiSigner::from(pubkey).into_account()
}
}
4 changes: 2 additions & 2 deletions polkadot/node/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn polkadot_local_testnet_genesis() -> serde_json::Value {
fn get_authority_keys_from_seed(
seed: &str,
) -> (AccountId, AccountId, BabeId, GrandpaId, ValidatorId, AssignmentId, AuthorityDiscoveryId) {
let sr25519_keyring = Sr25519Keyring::from_str(seed).expect("Parse keyring error");
let ed25519_keyring = Ed25519Keyring::from_str(seed).expect("Parse keyring error");
let sr25519_keyring = Sr25519Keyring::from_str(seed).expect("should parse str seed to keyring");
let ed25519_keyring = Ed25519Keyring::from_str(seed).expect("should parse str seed to keyring");
(
sr25519_keyring.to_account_id(),
sr25519_keyring.to_account_id(),
Expand Down
3 changes: 1 addition & 2 deletions polkadot/runtime/common/src/purchase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,9 @@ mod tests {
traits::{Currency, WithdrawReasons},
};
use sp_runtime::{
traits::{BlakeTwo256, Dispatchable, Identity, IdentityLookup, Verify},
traits::{BlakeTwo256, Dispatchable, Identity, IdentityLookup},
ArithmeticError, BuildStorage,
DispatchError::BadOrigin,
MultiSignature,
};

type Block = frame_system::mocking::MockBlock<Test>;
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp-storage = { workspace = true }
sp-version = { workspace = true }
sp-transaction-pool = { workspace = true }
sp-block-builder = { workspace = true }
sp-keyring = { workspace = true }

pallet-authority-discovery = { workspace = true }
pallet-authorship = { workspace = true }
Expand Down
96 changes: 53 additions & 43 deletions polkadot/runtime/rococo/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,15 @@ use crate::{
#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::{vec, vec::Vec};
use polkadot_primitives::{AccountId, AccountPublic, AssignmentId, SchedulerParams, ValidatorId};
use polkadot_primitives::{AccountId, AssignmentId, SchedulerParams, ValidatorId};
use rococo_runtime_constants::currency::UNITS as ROC;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_babe::AuthorityId as BabeId;
use sp_consensus_beefy::ecdsa_crypto::AuthorityId as BeefyId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use sp_core::{sr25519, Pair, Public};
use sp_core::Pair;
use sp_genesis_builder::PresetId;
use sp_runtime::traits::IdentifyAccount;

/// Helper function to generate a crypto pair from seed
fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}

/// Helper function to generate an account ID from seed
fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};

/// Helper function to generate stash, controller and session key from seed
fn get_authority_keys_from_seed(
Expand All @@ -62,39 +47,64 @@ fn get_authority_keys_from_seed(
BeefyId,
) {
let keys = get_authority_keys_from_seed_no_beefy(seed);
(keys.0, keys.1, keys.2, keys.3, keys.4, keys.5, keys.6, get_from_seed::<BeefyId>(seed))
(
keys.0,
keys.1,
keys.2,
keys.3,
keys.4,
keys.5,
keys.6,
BeefyId::from(
sp_consensus_beefy::ecdsa_crypto::Pair::from_string(&format!("//{}", seed), None)
.expect("should parse str seed to keyring")
.public(),
),
)
}

/// Helper function to generate stash, controller and session key from seed
fn get_authority_keys_from_seed_no_beefy(
seed: &str,
) -> (AccountId, AccountId, BabeId, GrandpaId, ValidatorId, AssignmentId, AuthorityDiscoveryId) {
use core::str::FromStr;
(
get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)),
get_account_id_from_seed::<sr25519::Public>(seed),
get_from_seed::<BabeId>(seed),
get_from_seed::<GrandpaId>(seed),
get_from_seed::<ValidatorId>(seed),
get_from_seed::<AssignmentId>(seed),
get_from_seed::<AuthorityDiscoveryId>(seed),
Sr25519Keyring::from_str(&format!("{}//stash", seed))
.expect("should parse str seed to keyring")
.to_account_id(),
Sr25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.to_account_id(),
BabeId::from(
Sr25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.public(),
),
GrandpaId::from(
Ed25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.public(),
),
ValidatorId::from(
Sr25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.public(),
),
AssignmentId::from(
Sr25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.public(),
),
AuthorityDiscoveryId::from(
Sr25519Keyring::from_str(seed)
.expect("should parse str seed to keyring")
.public(),
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
),
)
}

fn testnet_accounts() -> Vec<AccountId> {
Vec::from([
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
])
Sr25519Keyring::iter().map(|x| x.to_account_id()).collect()
}

fn rococo_session_keys(
Expand Down Expand Up @@ -478,7 +488,7 @@ fn rococo_staging_testnet_config_genesis() -> serde_json::Value {
fn rococo_development_config_genesis() -> serde_json::Value {
rococo_testnet_genesis(
Vec::from([get_authority_keys_from_seed("Alice")]),
get_account_id_from_seed::<sr25519::Public>("Alice"),
Sr25519Keyring::Alice.to_account_id(),
None,
)
}
Expand All @@ -487,7 +497,7 @@ fn rococo_development_config_genesis() -> serde_json::Value {
fn rococo_local_testnet_genesis() -> serde_json::Value {
rococo_testnet_genesis(
Vec::from([get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob")]),
get_account_id_from_seed::<sr25519::Public>("Alice"),
Sr25519Keyring::Alice.to_account_id(),
None,
)
}
Expand All @@ -502,7 +512,7 @@ fn versi_local_testnet_genesis() -> serde_json::Value {
get_authority_keys_from_seed("Charlie"),
get_authority_keys_from_seed("Dave"),
]),
get_account_id_from_seed::<sr25519::Public>("Alice"),
Sr25519Keyring::Alice.to_account_id(),
None,
)
}
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp-version = { workspace = true }
sp-transaction-pool = { workspace = true }
sp-block-builder = { workspace = true }
sp-npos-elections = { workspace = true }
sp-keyring = { workspace = true }

frame-election-provider-support = { workspace = true }
frame-executive = { workspace = true }
Expand Down
Loading
Loading