Skip to content

Commit

Permalink
remove magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
programskillforverification committed Oct 17, 2024
1 parent 6b39516 commit 341c689
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub mod accounts {
pub const DUMMY_EMPTY: &str = "JohnDoe";

pub fn init_balances() -> Vec<AccountId> {
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect()
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|x| x.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|x| x.to_account_id()).collect(),
testnet_parachains_constants::rococo::currency::UNITS * 1_000_000,
1000.into(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
WND * 1_000_000,
1000.into(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1013.into(),
Some(Sr25519Keyring::Bob.to_account_id()),
rococo_runtime_constants::system_parachain::ASSET_HUB_ID.into(),
Expand All @@ -112,7 +112,7 @@ pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1013.into(),
Some(Sr25519Keyring::Bob.to_account_id()),
rococo_runtime_constants::system_parachain::ASSET_HUB_ID.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1002.into(),
Some(Sr25519Keyring::Bob.to_account_id()),
westend_runtime_constants::system_parachain::ASSET_HUB_ID.into(),
Expand All @@ -112,7 +112,7 @@ pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1002.into(),
Some(Sr25519Keyring::Bob.to_account_id()),
westend_runtime_constants::system_parachain::ASSET_HUB_ID.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1001.into(),
),
Ok(sp_genesis_builder::DEV_RUNTIME_PRESET) => collectives_westend_genesis(
Expand Down
2 changes: 1 addition & 1 deletion cumulus/polkadot-parachain/src/chain_spec/penpal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn get_penpal_chain_spec(id: ParaId, relay_chain: &str) -> GenericChainSpec
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
id,
))
.build()
Expand Down
4 changes: 2 additions & 2 deletions cumulus/polkadot-parachain/src/chain_spec/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub mod rococo {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
para_id,
))
.with_properties(properties)
Expand Down Expand Up @@ -245,7 +245,7 @@ pub mod westend {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
para_id,
))
.with_properties(properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn rococo_parachain_local_config() -> GenericChainSpec {
AuraId::from(Sr25519Keyring::Alice.public()),
AuraId::from(Sr25519Keyring::Bob.public()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
1000.into(),
))
.build()
Expand Down
5 changes: 2 additions & 3 deletions cumulus/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ pub fn testnet_genesis_with_default_endowed(
mut extra_endowed_accounts: Vec<AccountId>,
self_para_id: Option<ParaId>,
) -> serde_json::Value {
let mut endowed =
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect::<Vec<_>>();
let mut endowed = Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect::<Vec<_>>();
endowed.append(&mut extra_endowed_accounts);
let invulnerables =
Sr25519Keyring::iter().take(6).map(|k| k.public().into()).collect::<Vec<_>>();
Sr25519Keyring::invulnerable().map(|k| k.public().into()).collect::<Vec<_>>();
testnet_genesis(Sr25519Keyring::Alice.to_account_id(), invulnerables, endowed, self_para_id)
}

Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn get_authority_keys_from_seed(
}

fn testnet_accounts() -> Vec<AccountId> {
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect()
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect()
}

/// Helper function to create polkadot `RuntimeGenesisConfig` for testing
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn get_authority_keys_from_seed_no_beefy(
}

fn testnet_accounts() -> Vec<AccountId> {
Sr25519Keyring::iter().take(12).map(|x| x.to_account_id()).collect()
Sr25519Keyring::well_known().map(|x| x.to_account_id()).collect()
}

fn rococo_session_keys(
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn get_authority_keys_from_seed_no_beefy(
}

fn testnet_accounts() -> Vec<AccountId> {
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect()
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect()
}

fn westend_session_keys(
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn configure_accounts(
Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
) {
let mut endowed_accounts: Vec<AccountId> = endowed_accounts
.unwrap_or_else(|| Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect());
.unwrap_or_else(|| Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect());
// endow all authorities and nominators.
initial_authorities
.iter()
Expand Down
4 changes: 4 additions & 0 deletions substrate/bin/node/testing/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ pub fn ferdie() -> AccountId {
}

/// Convert keyrings into `SessionKeys`.
///
/// # Panics
///
/// Function will panic when invalid string is provided.
pub fn session_keys_from_seed(seed: &str) -> SessionKeys {
SessionKeys {
grandpa: get_public_from_string_or_panic::<ed25519::Public>(seed).into(),
Expand Down
56 changes: 50 additions & 6 deletions substrate/primitives/keyring/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ impl Keyring {
pub fn to_seed(self) -> String {
format!("//{}", self)
}

pub fn well_known() -> impl Iterator<Item = Keyring> {
Self::iter().take(12)
}

pub fn invulnerable() -> impl Iterator<Item = Keyring> {
Self::iter().take(6)
}
}

impl From<Keyring> for &'static str {
Expand Down Expand Up @@ -147,12 +155,12 @@ impl FromStr for Keyring {
"Dave" | "dave" => Ok(Keyring::Dave),
"Eve" | "eve" => Ok(Keyring::Eve),
"Ferdie" | "ferdie" => Ok(Keyring::Ferdie),
"Alice//stash" => Ok(Keyring::AliceStash),
"Bob//stash" => Ok(Keyring::BobStash),
"Charlie//stash" => Ok(Keyring::CharlieStash),
"Dave//stash" => Ok(Keyring::DaveStash),
"Eve//stash" => Ok(Keyring::EveStash),
"Ferdie//stash" => Ok(Keyring::FerdieStash),
"Alice//stash" | "alice//stash" => Ok(Keyring::AliceStash),
"Bob//stash" | "bob//stash" => Ok(Keyring::BobStash),
"Charlie//stash" | "charlie//stash" => Ok(Keyring::CharlieStash),
"Dave//stash" | "dave//stash" => Ok(Keyring::DaveStash),
"Eve//stash" | "eve//stash" => Ok(Keyring::EveStash),
"Ferdie//stash" | "ferdie//stash" => Ok(Keyring::FerdieStash),
"One" | "one" => Ok(Keyring::One),
"Two" | "two" => Ok(Keyring::Two),
_ => Err(ParseKeyringError),
Expand Down Expand Up @@ -247,4 +255,40 @@ mod tests {
fn verify_static_public_keys() {
assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
}

#[test]
fn verfity_well_known() {
assert_eq!(
Keyring::well_known().collect::<Vec<Keyring>>(),
vec![
Keyring::Alice,
Keyring::Bob,
Keyring::Charlie,
Keyring::Dave,
Keyring::Eve,
Keyring::Ferdie,
Keyring::AliceStash,
Keyring::BobStash,
Keyring::CharlieStash,
Keyring::DaveStash,
Keyring::EveStash,
Keyring::FerdieStash
]
);
}

#[test]
fn verify_invulnerable() {
assert_eq!(
Keyring::invulnerable().collect::<Vec<Keyring>>(),
vec![
Keyring::Alice,
Keyring::Bob,
Keyring::Charlie,
Keyring::Dave,
Keyring::Eve,
Keyring::Ferdie
]
);
}
}
57 changes: 51 additions & 6 deletions substrate/primitives/keyring/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ impl Keyring {
pub fn numeric_id(idx: usize) -> AccountId32 {
(*Self::numeric(idx).public().as_array_ref()).into()
}

pub fn well_known() -> impl Iterator<Item = Keyring> {
Self::iter().take(12)
}

pub fn invulnerable() -> impl Iterator<Item = Keyring> {
Self::iter().take(6)
}
}

impl From<Keyring> for &'static str {
Expand Down Expand Up @@ -158,12 +166,12 @@ impl FromStr for Keyring {
"Dave" | "dave" => Ok(Keyring::Dave),
"Eve" | "eve" => Ok(Keyring::Eve),
"Ferdie" | "ferdie" => Ok(Keyring::Ferdie),
"Alice//stash" => Ok(Keyring::AliceStash),
"Bob//stash" => Ok(Keyring::BobStash),
"Charlie//stash" => Ok(Keyring::CharlieStash),
"Dave//stash" => Ok(Keyring::DaveStash),
"Eve//stash" => Ok(Keyring::EveStash),
"Ferdie//stash" => Ok(Keyring::FerdieStash),
"Alice//stash" | "alice//stash" => Ok(Keyring::AliceStash),
"Bob//stash" | "bob//stash" => Ok(Keyring::BobStash),
"Charlie//stash" | "charlie//stash" => Ok(Keyring::CharlieStash),
"Dave//stash" | "dave//stash" => Ok(Keyring::DaveStash),
"Eve//stash" | "eve//stash" => Ok(Keyring::EveStash),
"Ferdie//stash" | "ferdie//stash" => Ok(Keyring::FerdieStash),
"One" | "one" => Ok(Keyring::One),
"Two" | "two" => Ok(Keyring::Two),
_ => Err(ParseKeyringError),
Expand Down Expand Up @@ -253,8 +261,45 @@ mod tests {
&Keyring::Bob.public(),
));
}

#[test]
fn verify_static_public_keys() {
assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
}

#[test]
fn verfity_well_known() {
assert_eq!(
Keyring::well_known().collect::<Vec<Keyring>>(),
vec![
Keyring::Alice,
Keyring::Bob,
Keyring::Charlie,
Keyring::Dave,
Keyring::Eve,
Keyring::Ferdie,
Keyring::AliceStash,
Keyring::BobStash,
Keyring::CharlieStash,
Keyring::DaveStash,
Keyring::EveStash,
Keyring::FerdieStash
]
);
}

#[test]
fn verify_invulnerable() {
assert_eq!(
Keyring::invulnerable().collect::<Vec<Keyring>>(),
vec![
Keyring::Alice,
Keyring::Bob,
Keyring::Charlie,
Keyring::Dave,
Keyring::Eve,
Keyring::Ferdie
]
);
}
}
4 changes: 2 additions & 2 deletions templates/parachain/runtime/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn local_testnet_genesis() -> Value {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
Sr25519Keyring::Alice.to_account_id(),
1000.into(),
)
Expand All @@ -87,7 +87,7 @@ fn development_config_genesis() -> Value {
(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
],
Sr25519Keyring::iter().take(12).map(|k| k.to_account_id()).collect(),
Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
Sr25519Keyring::Alice.to_account_id(),
1000.into(),
)
Expand Down

0 comments on commit 341c689

Please sign in to comment.