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

keyring: remove lazy_static public keys hash maps #2387

Merged
merged 38 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8ac1a1a
keyring: remove lazy_static hash maps
michalkucharczyk Nov 17, 2023
599045c
license fix
michalkucharczyk Nov 17, 2023
a76af76
no static cache
michalkucharczyk Nov 17, 2023
97bca05
missing change
michalkucharczyk Nov 17, 2023
e2121ca
sr25519+bandersnatch
michalkucharczyk Nov 20, 2023
8d2ea05
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 20, 2023
88ae185
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 21, 2023
25a0a89
public() method uses raw bytes
michalkucharczyk Nov 23, 2023
1a227a0
fix
michalkucharczyk Nov 24, 2023
981eade
Merge remote-tracking branch 'origin/master' into mku-remove-lazy-sta…
michalkucharczyk Nov 24, 2023
ebd6260
const fn hex2array util added
michalkucharczyk Nov 24, 2023
7d6d327
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 24, 2023
8d94b6b
hex2arr moved to sp_core
michalkucharczyk Nov 24, 2023
192a021
const keys added
michalkucharczyk Nov 25, 2023
84fccb0
one more try
michalkucharczyk Nov 25, 2023
006452b
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 25, 2023
9b90651
debugs removed
michalkucharczyk Nov 25, 2023
b1a0f00
".git/.scripts/commands/fmt/fmt.sh"
Nov 25, 2023
ae69b21
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 27, 2023
2e716dd
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 28, 2023
bcaacc6
hex2array: more tests + better doc
michalkucharczyk Nov 28, 2023
bfd8f05
Apply suggestions from code review
michalkucharczyk Nov 28, 2023
1406b91
better test
michalkucharczyk Nov 28, 2023
ab13181
doc fixed
michalkucharczyk Nov 28, 2023
f01fe2f
test simplified
michalkucharczyk Nov 28, 2023
1372cef
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 1, 2023
1649b92
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 1, 2023
8aab10a
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 6, 2023
96c3574
macro added
michalkucharczyk Dec 8, 2023
5697147
Merge remote-tracking branch 'origin/master' into mku-remove-lazy-sta…
michalkucharczyk Dec 8, 2023
62ba4f4
Apply suggestions from code review
michalkucharczyk Dec 8, 2023
b278916
fixes
michalkucharczyk Dec 8, 2023
0f7cccc
better naming
michalkucharczyk Dec 8, 2023
b725ac4
Apply suggestions from code review
michalkucharczyk Dec 8, 2023
b38e666
".git/.scripts/commands/update-ui/update-ui.sh"
Dec 8, 2023
ac8361f
Revert "".git/.scripts/commands/update-ui/update-ui.sh""
michalkucharczyk Dec 11, 2023
c8b2c2d
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 11, 2023
8e0d00e
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 11, 2023
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion substrate/primitives/keyring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
lazy_static = "1.4.0"
strum = { version = "0.24.1", features = ["derive"], default-features = false }
sp-core = { path = "../core" }
sp-runtime = { path = "../runtime" }
array-bytes = { version = "6.1" }

[features]
# This feature adds Bandersnatch crypto primitives.
Expand Down
73 changes: 32 additions & 41 deletions substrate/primitives/keyring/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

//! A set of well-known keys used for testing.

use array_bytes::hex2array_unchecked as hex2arr;
pub use sp_core::bandersnatch;
use sp_core::{
bandersnatch::{Pair, Public, Signature},
crypto::UncheckedFrom,
ByteArray, Pair as PairT,
};

use lazy_static::lazy_static;
use std::{collections::HashMap, ops::Deref, sync::Mutex};

/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
pub enum Keyring {
Expand Down Expand Up @@ -129,20 +127,9 @@ impl std::str::FromStr for Keyring {
}
}

lazy_static! {
static ref PRIVATE_KEYS: Mutex<HashMap<Keyring, Pair>> =
Mutex::new(Keyring::iter().map(|who| (who, who.pair())).collect());
static ref PUBLIC_KEYS: HashMap<Keyring, Public> = PRIVATE_KEYS
.lock()
.unwrap()
.iter()
.map(|(&who, pair)| (who, pair.public()))
.collect();
}

impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::unchecked_from(<[u8; PUBLIC_RAW_LEN]>::from(k))
}
}

Expand All @@ -154,32 +141,24 @@ impl From<Keyring> for Pair {

impl From<Keyring> for [u8; PUBLIC_RAW_LEN] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_ref()
}
}

impl From<Keyring> for &'static [u8; PUBLIC_RAW_LEN] {
fn from(k: Keyring) -> Self {
PUBLIC_KEYS.get(&k).unwrap().as_ref()
}
}

impl AsRef<[u8; PUBLIC_RAW_LEN]> for Keyring {
fn as_ref(&self) -> &[u8; PUBLIC_RAW_LEN] {
PUBLIC_KEYS.get(self).unwrap().as_ref()
}
}

impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
PUBLIC_KEYS.get(self).unwrap()
}
}

impl Deref for Keyring {
type Target = [u8; PUBLIC_RAW_LEN];
fn deref(&self) -> &[u8; PUBLIC_RAW_LEN] {
PUBLIC_KEYS.get(self).unwrap().as_ref()
match k {
Keyring::Alice =>
hex2arr("9c8af77d3a4e3f6f076853922985b9e6724fc9675329087f47aff1ceaaae772180"),
Keyring::Bob =>
hex2arr("1abfbb76dc8374a1a6d93d59a5c81f07c18835f4681a6258aa0f514d363bff4780"),
Keyring::Charlie =>
hex2arr("0f4a9990aca3d39a7cd8bf187e2e81a9ea6f9cedb2db405f2fffff384c5dd02680"),
Keyring::Dave =>
hex2arr("bd7a87d4dfa89926a408b5acbed554ae3b053fa3532531053295cbabf07d337000"),
Keyring::Eve =>
hex2arr("f992d5b8eac8fc004d521bee6edc1174cfa7fae3a1baec8262511ee351f9f85e00"),
Keyring::Ferdie =>
hex2arr("1ce2613e89bc5c8e358aad884099cfb576a61176f2f9968cd0d486a04457245180"),
Keyring::One =>
hex2arr("a29e03ac273e521274d8e501a6242abd2ab393d7e197221a9113bdf8e2e5b34d00"),
Keyring::Two =>
hex2arr("f968d47e819ddb18a9d0f2ebd16501680b1a3f07ee375c6f81310e5f99a04f4d00"),
}
}
}

Expand All @@ -206,4 +185,16 @@ mod tests {
&Keyring::Bob.public(),
));
}
#[test]
fn verify_static_public_keys() {
assert!(Keyring::iter()
.all(|k| { k.pair().public().as_ref() == <[u8; PUBLIC_RAW_LEN]>::from(k) }));
// little helper to print out public keys hex string
// use array_bytes::Hex;
// Keyring::iter().map(|i| (i, i.pair())).for_each(|(name, pair)| {
// let public = pair.public();
// let bytes: &[u8; PUBLIC_RAW_LEN] = public.as_ref();
// println!("Keyring::{} => hex2arr({:?}),", name, bytes.hex(""));
// });
}
}
82 changes: 45 additions & 37 deletions substrate/primitives/keyring/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

//! Support code for the runtime. A set of test accounts.

use lazy_static::lazy_static;
use array_bytes::hex2array_unchecked as hex2arr;
pub use sp_core::ed25519;
use sp_core::{
ed25519::{Pair, Public, Signature},
ByteArray, Pair as PairT, H256,
};
use sp_runtime::AccountId32;
use std::{collections::HashMap, ops::Deref};

/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
Expand Down Expand Up @@ -128,16 +127,9 @@ impl From<Keyring> for sp_runtime::MultiSigner {
}
}

lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> =
Keyring::iter().map(|i| (i, i.pair())).collect();
static ref PUBLIC_KEYS: HashMap<Keyring, Public> =
PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect();
}

impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::from_raw(k.into())
}
}

Expand All @@ -155,38 +147,42 @@ impl From<Keyring> for Pair {

impl From<Keyring> for [u8; 32] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
match k {
Keyring::Alice =>
hex2arr("88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"),
Keyring::Bob =>
hex2arr("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69"),
Keyring::Charlie =>
hex2arr("439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de234f"),
Keyring::Dave =>
hex2arr("5e639b43e0052c47447dac87d6fd2b6ec50bdd4d0f614e4299c665249bbd09d9"),
Keyring::Eve =>
hex2arr("1dfe3e22cc0d45c70779c1095f7489a8ef3cf52d62fbd8c2fa38c9f1723502b5"),
Keyring::Ferdie =>
hex2arr("568cb4a574c6d178feb39c27dfc8b3f789e5f5423e19c71633c748b9acf086b5"),
Keyring::AliceStash =>
hex2arr("451781cd0c5504504f69ceec484cc66e4c22a2b6a9d20fb1a426d91ad074a2a8"),
Keyring::BobStash =>
hex2arr("292684abbb28def63807c5f6e84e9e8689769eb37b1ab130d79dbfbf1b9a0d44"),
Keyring::CharlieStash =>
hex2arr("dd6a6118b6c11c9c9e5a4f34ed3d545e2c74190f90365c60c230fa82e9423bb9"),
Keyring::DaveStash =>
hex2arr("1d0432d75331ab299065bee79cdb1bdc2497c597a3087b4d955c67e3c000c1e2"),
Keyring::EveStash =>
hex2arr("c833bdd2e1a7a18acc1c11f8596e2e697bb9b42d6b6051e474091a1d43a294d7"),
Keyring::FerdieStash =>
hex2arr("199d749dbf4b8135cb1f3c8fd697a390fc0679881a8a110c1d06375b3b62cd09"),
Keyring::One =>
hex2arr("16f97016bbea8f7b45ae6757b49efc1080accc175d8f018f9ba719b60b0815e4"),
Keyring::Two =>
hex2arr("5079bcd20fd97d7d2f752c4607012600b401950260a91821f73e692071c82bf5"),
}
}
}

impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}

impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
}
}

impl AsRef<[u8; 32]> for Keyring {
fn as_ref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
}
}

impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
(*PUBLIC_KEYS).get(self).unwrap()
}
}

impl Deref for Keyring {
type Target = [u8; 32];
fn deref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
k.into()
}
}

Expand All @@ -213,4 +209,16 @@ 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) }));
// little helper to print out public keys hex string
// use array_bytes::Hex;
// Keyring::iter().map(|i| (i, i.pair())).for_each(|(name, pair)| {
// let public = pair.public();
// let bytes: &[u8; 32] = public.as_ref();
// println!("Keyring::{}: {:?}", name, bytes.hex(""));
// });
}
}
81 changes: 44 additions & 37 deletions substrate/primitives/keyring/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

//! Support code for the runtime. A set of test accounts.

use lazy_static::lazy_static;
use array_bytes::hex2array_unchecked as hex2arr;
pub use sp_core::sr25519;
use sp_core::{
sr25519::{Pair, Public, Signature},
ByteArray, Pair as PairT, H256,
};
use sp_runtime::AccountId32;
use std::{collections::HashMap, ops::Deref};

/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
Expand Down Expand Up @@ -165,13 +164,6 @@ impl std::str::FromStr for Keyring {
}
}

lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> =
Keyring::iter().map(|i| (i, i.pair())).collect();
static ref PUBLIC_KEYS: HashMap<Keyring, Public> =
PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect();
}

impl From<Keyring> for AccountId32 {
fn from(k: Keyring) -> Self {
k.to_account_id()
Expand All @@ -180,7 +172,7 @@ impl From<Keyring> for AccountId32 {

impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::from_raw(k.into())
}
}

Expand All @@ -192,38 +184,42 @@ impl From<Keyring> for Pair {

impl From<Keyring> for [u8; 32] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
match k {
Keyring::Alice =>
hex2arr("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"),
Keyring::Bob =>
hex2arr("8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"),
Keyring::Charlie =>
hex2arr("90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22"),
Keyring::Dave =>
hex2arr("306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20"),
Keyring::Eve =>
hex2arr("e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e"),
Keyring::Ferdie =>
hex2arr("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c"),
Keyring::AliceStash =>
hex2arr("be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f"),
Keyring::BobStash =>
hex2arr("fe65717dad0447d715f660a0a58411de509b42e6efb8375f562f58a554d5860e"),
Keyring::CharlieStash =>
hex2arr("1e07379407fecc4b89eb7dbd287c2c781cfb1907a96947a3eb18e4f8e7198625"),
Keyring::DaveStash =>
hex2arr("e860f1b1c7227f7c22602f53f15af80747814dffd839719731ee3bba6edc126c"),
Keyring::EveStash =>
hex2arr("8ac59e11963af19174d0b94d5d78041c233f55d2e19324665bafdfb62925af2d"),
Keyring::FerdieStash =>
hex2arr("101191192fc877c24d725b337120fa3edc63d227bbc92705db1e2cb65f56981a"),
Keyring::One =>
hex2arr("ac859f8a216eeb1b320b4c76d118da3d7407fa523484d0a980126d3b4d0d220a"),
Keyring::Two =>
hex2arr("1254f7017f0b8347ce7ab14f96d818802e7e9e0c0d1b7c9acb3c726b080e7a03"),
}
}
}

impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}

impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
}
}

impl AsRef<[u8; 32]> for Keyring {
fn as_ref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
}
}

impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
(*PUBLIC_KEYS).get(self).unwrap()
}
}

impl Deref for Keyring {
type Target = [u8; 32];
fn deref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
k.into()
}
}

Expand All @@ -250,4 +246,15 @@ 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) }));
// little helper to print out public keys hex string
// use array_bytes::Hex;
// Keyring::iter().map(|i| (i, i.pair())).for_each(|(name, pair)| {
// let public = pair.public();
// let bytes: &[u8; 32] = public.as_ref();
// println!("Keyring::{}: {:?}", name, bytes.hex(""));
// });
}
}
Loading