From 2b4cece0aca6def64fc025bfd454c21c630eb3ef Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 15:10:06 +0800 Subject: [PATCH 1/6] chore: reduce copy bytes for core-hashing --- primitives/core/hashing/src/lib.rs | 51 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/primitives/core/hashing/src/lib.rs b/primitives/core/hashing/src/lib.rs index 5bb68586a1340..ab9d167d96659 100644 --- a/primitives/core/hashing/src/lib.rs +++ b/primitives/core/hashing/src/lib.rs @@ -35,48 +35,51 @@ pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) { /// Do a Blake2 512-bit hash and return result. pub fn blake2_512(data: &[u8]) -> [u8; 64] { - let mut r = [0; 64]; - blake2_512_into(data, &mut r); - r + let mut h = blake2::Blake2b512::new(); + h.update(data); + h.finalize().into() } +type Blake2b256 = blake2::Blake2b; + /// Do a Blake2 256-bit hash and place result in `dest`. pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) { - type Blake2b256 = blake2::Blake2b; dest.copy_from_slice(Blake2b256::digest(data).as_slice()); } /// Do a Blake2 256-bit hash and return result. pub fn blake2_256(data: &[u8]) -> [u8; 32] { - let mut r = [0; 32]; - blake2_256_into(data, &mut r); - r + let mut h = Blake2b256::new(); + h.update(data); + h.finalize().into() } +type Blake2b128 = blake2::Blake2b; + /// Do a Blake2 128-bit hash and place result in `dest`. pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) { - type Blake2b128 = blake2::Blake2b; dest.copy_from_slice(Blake2b128::digest(data).as_slice()); } /// Do a Blake2 128-bit hash and return result. pub fn blake2_128(data: &[u8]) -> [u8; 16] { - let mut r = [0; 16]; - blake2_128_into(data, &mut r); - r + let mut h = Blake2b128::new(); + h.update(data); + h.finalize().into() } +type Blake2b64 = blake2::Blake2b; + /// Do a Blake2 64-bit hash and place result in `dest`. pub fn blake2_64_into(data: &[u8], dest: &mut [u8; 8]) { - type Blake2b64 = blake2::Blake2b; dest.copy_from_slice(Blake2b64::digest(data).as_slice()); } /// Do a Blake2 64-bit hash and return result. pub fn blake2_64(data: &[u8]) -> [u8; 8] { - let mut r = [0; 8]; - blake2_64_into(data, &mut r); - r + let mut h = Blake2b64::new(); + h.update(data); + h.finalize().into() } /// Do a XX 64-bit hash and place result in `dest`. @@ -128,21 +131,21 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] { /// Do a keccak 256-bit hash and return result. pub fn keccak_256(data: &[u8]) -> [u8; 32] { - let mut output = [0u8; 32]; - output.copy_from_slice(sha3::Keccak256::digest(data).as_slice()); - output + let mut h = sha3::Keccak256::new(); + h.update(data); + h.finalize().into() } /// Do a keccak 512-bit hash and return result. pub fn keccak_512(data: &[u8]) -> [u8; 64] { - let mut output = [0u8; 64]; - output.copy_from_slice(sha3::Keccak512::digest(data).as_slice()); - output + let mut h = sha3::Keccak512::new(); + h.update(data); + h.finalize().into() } /// Do a sha2 256-bit hash and return result. pub fn sha2_256(data: &[u8]) -> [u8; 32] { - let mut output = [0u8; 32]; - output.copy_from_slice(sha2::Sha256::digest(data).as_slice()); - output + let mut h = sha2::Sha256::new(); + h.update(data); + h.finalize().into() } From 58ad855070f13aed85aa80dbf15a9715fba581b4 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 17:19:20 +0800 Subject: [PATCH 2/6] improve by suggestions and remove unused `xx_into` --- primitives/core/hashing/src/lib.rs | 49 +++++------------------------- 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/primitives/core/hashing/src/lib.rs b/primitives/core/hashing/src/lib.rs index ab9d167d96659..26941bb60fa51 100644 --- a/primitives/core/hashing/src/lib.rs +++ b/primitives/core/hashing/src/lib.rs @@ -35,51 +35,22 @@ pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) { /// Do a Blake2 512-bit hash and return result. pub fn blake2_512(data: &[u8]) -> [u8; 64] { - let mut h = blake2::Blake2b512::new(); - h.update(data); - h.finalize().into() -} - -type Blake2b256 = blake2::Blake2b; - -/// Do a Blake2 256-bit hash and place result in `dest`. -pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) { - dest.copy_from_slice(Blake2b256::digest(data).as_slice()); + blake2::Blake2b512::digest(data).into() } /// Do a Blake2 256-bit hash and return result. pub fn blake2_256(data: &[u8]) -> [u8; 32] { - let mut h = Blake2b256::new(); - h.update(data); - h.finalize().into() -} - -type Blake2b128 = blake2::Blake2b; - -/// Do a Blake2 128-bit hash and place result in `dest`. -pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) { - dest.copy_from_slice(Blake2b128::digest(data).as_slice()); + blake2::Blake2b::::digest(data).into() } /// Do a Blake2 128-bit hash and return result. pub fn blake2_128(data: &[u8]) -> [u8; 16] { - let mut h = Blake2b128::new(); - h.update(data); - h.finalize().into() -} - -type Blake2b64 = blake2::Blake2b; - -/// Do a Blake2 64-bit hash and place result in `dest`. -pub fn blake2_64_into(data: &[u8], dest: &mut [u8; 8]) { - dest.copy_from_slice(Blake2b64::digest(data).as_slice()); + blake2::Blake2b::::digest(data).into() } /// Do a Blake2 64-bit hash and return result. pub fn blake2_64(data: &[u8]) -> [u8; 8] { - let mut h = Blake2b64::new(); - h.update(data); - h.finalize().into() + blake2::Blake2b::::digest(data).into() } /// Do a XX 64-bit hash and place result in `dest`. @@ -131,21 +102,15 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] { /// Do a keccak 256-bit hash and return result. pub fn keccak_256(data: &[u8]) -> [u8; 32] { - let mut h = sha3::Keccak256::new(); - h.update(data); - h.finalize().into() + sha3::Keccak256::digest(data).into() } /// Do a keccak 512-bit hash and return result. pub fn keccak_512(data: &[u8]) -> [u8; 64] { - let mut h = sha3::Keccak512::new(); - h.update(data); - h.finalize().into() + sha3::Keccak512::digest(data).into() } /// Do a sha2 256-bit hash and return result. pub fn sha2_256(data: &[u8]) -> [u8; 32] { - let mut h = sha2::Sha256::new(); - h.update(data); - h.finalize().into() + sha2::Sha256::digest(data).into() } From da00eef6e96e058f60cb4e48c707224cf1934130 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 17:33:18 +0800 Subject: [PATCH 3/6] chore: replace sha2 crate by `sp_core::hashing` for pallet-alliance --- Cargo.lock | 1 - frame/alliance/Cargo.toml | 3 --- frame/alliance/src/benchmarking.rs | 7 ++----- frame/alliance/src/mock.rs | 7 ++----- 4 files changed, 4 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a412347bde5c0..149c0f835b6fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5273,7 +5273,6 @@ dependencies = [ "pallet-identity", "parity-scale-codec", "scale-info", - "sha2 0.10.6", "sp-core", "sp-io", "sp-runtime", diff --git a/frame/alliance/Cargo.toml b/frame/alliance/Cargo.toml index d6a16e2c32e36..47538039cfe63 100644 --- a/frame/alliance/Cargo.toml +++ b/frame/alliance/Cargo.toml @@ -14,7 +14,6 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] array-bytes = { version = "4.1", optional = true } -sha2 = { version = "0.10.1", default-features = false, optional = true } log = { version = "0.4.14", default-features = false } codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] } @@ -34,7 +33,6 @@ pallet-collective = { version = "4.0.0-dev", path = "../collective", default-fea [dev-dependencies] array-bytes = "4.1" -sha2 = "0.10.1" pallet-balances = { version = "4.0.0-dev", path = "../balances" } pallet-collective = { version = "4.0.0-dev", path = "../collective" } @@ -56,7 +54,6 @@ std = [ ] runtime-benchmarks = [ "array-bytes", - "sha2", "frame-benchmarking/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "frame-support/runtime-benchmarks", diff --git a/frame/alliance/src/benchmarking.rs b/frame/alliance/src/benchmarking.rs index a30b5adcee3ed..bd924763d54a9 100644 --- a/frame/alliance/src/benchmarking.rs +++ b/frame/alliance/src/benchmarking.rs @@ -40,11 +40,8 @@ fn assert_last_event, I: 'static>(generic_event: >:: } fn cid(input: impl AsRef<[u8]>) -> Cid { - use sha2::{Digest, Sha256}; - let mut hasher = Sha256::new(); - hasher.update(input); - let result = hasher.finalize(); - Cid::new_v0(&*result) + let result = sp_core::hashing::sha2_256(input.as_ref()); + Cid::new_v0(result) } fn rule(input: impl AsRef<[u8]>) -> Cid { diff --git a/frame/alliance/src/mock.rs b/frame/alliance/src/mock.rs index 0925a82fdbfa3..3157afc169f1d 100644 --- a/frame/alliance/src/mock.rs +++ b/frame/alliance/src/mock.rs @@ -368,11 +368,8 @@ pub fn new_bench_ext() -> sp_io::TestExternalities { } pub fn test_cid() -> Cid { - use sha2::{Digest, Sha256}; - let mut hasher = Sha256::new(); - hasher.update(b"hello world"); - let result = hasher.finalize(); - Cid::new_v0(&*result) + let result = sp_core::hashing::sha2_256(b"hello world"); + Cid::new_v0(result) } pub fn make_remark_proposal(value: u64) -> (RuntimeCall, u32, H256) { From 10e9e0c47c7e66e53a9bc2d2ec71392b2edad5a9 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 17:46:16 +0800 Subject: [PATCH 4/6] fix features --- frame/alliance/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/alliance/Cargo.toml b/frame/alliance/Cargo.toml index 47538039cfe63..05b39f3745d31 100644 --- a/frame/alliance/Cargo.toml +++ b/frame/alliance/Cargo.toml @@ -39,6 +39,7 @@ pallet-collective = { version = "4.0.0-dev", path = "../collective" } [features] default = ["std"] std = [ + "sp-core/full_crypto", "pallet-collective?/std", "frame-benchmarking?/std", "log/std", @@ -54,6 +55,7 @@ std = [ ] runtime-benchmarks = [ "array-bytes", + "sp-core/full_crypto", "frame-benchmarking/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "frame-support/runtime-benchmarks", From 7c0a91c7ce96d85e0c937a38ec87d5e8934e71c5 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 17:58:31 +0800 Subject: [PATCH 5/6] use sp-core-hashing directly --- Cargo.lock | 1 + frame/alliance/Cargo.toml | 5 +++-- frame/alliance/src/benchmarking.rs | 2 +- frame/alliance/src/mock.rs | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 149c0f835b6fb..531ed96690d42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5274,6 +5274,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", + "sp-core-hashing", "sp-io", "sp-runtime", "sp-std", diff --git a/frame/alliance/Cargo.toml b/frame/alliance/Cargo.toml index 05b39f3745d31..93417fd3aef59 100644 --- a/frame/alliance/Cargo.toml +++ b/frame/alliance/Cargo.toml @@ -21,6 +21,7 @@ scale-info = { version = "2.0.1", default-features = false, features = ["derive" sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" } +sp-core-hashing = { version = "5.0.0", default-features = false, path = "../../primitives/core/hashing", optional = true } sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" } @@ -39,7 +40,7 @@ pallet-collective = { version = "4.0.0-dev", path = "../collective" } [features] default = ["std"] std = [ - "sp-core/full_crypto", + "sp-core-hashing?/std", "pallet-collective?/std", "frame-benchmarking?/std", "log/std", @@ -55,7 +56,7 @@ std = [ ] runtime-benchmarks = [ "array-bytes", - "sp-core/full_crypto", + "sp-core-hashing", "frame-benchmarking/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "frame-support/runtime-benchmarks", diff --git a/frame/alliance/src/benchmarking.rs b/frame/alliance/src/benchmarking.rs index bd924763d54a9..92bf1ae4468df 100644 --- a/frame/alliance/src/benchmarking.rs +++ b/frame/alliance/src/benchmarking.rs @@ -40,7 +40,7 @@ fn assert_last_event, I: 'static>(generic_event: >:: } fn cid(input: impl AsRef<[u8]>) -> Cid { - let result = sp_core::hashing::sha2_256(input.as_ref()); + let result = sp_core_hashing::sha2_256(input.as_ref()); Cid::new_v0(result) } diff --git a/frame/alliance/src/mock.rs b/frame/alliance/src/mock.rs index 3157afc169f1d..b8fd998eb20a0 100644 --- a/frame/alliance/src/mock.rs +++ b/frame/alliance/src/mock.rs @@ -368,7 +368,7 @@ pub fn new_bench_ext() -> sp_io::TestExternalities { } pub fn test_cid() -> Cid { - let result = sp_core::hashing::sha2_256(b"hello world"); + let result = sp_core_hashing::sha2_256(b"hello world"); Cid::new_v0(result) } From f8bfbbb713bbf2c74a80cb5853c711f464c8ea17 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Fri, 3 Mar 2023 18:13:16 +0800 Subject: [PATCH 6/6] add to dev-dep --- frame/alliance/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/alliance/Cargo.toml b/frame/alliance/Cargo.toml index 93417fd3aef59..d70dfd6d752eb 100644 --- a/frame/alliance/Cargo.toml +++ b/frame/alliance/Cargo.toml @@ -34,6 +34,7 @@ pallet-collective = { version = "4.0.0-dev", path = "../collective", default-fea [dev-dependencies] array-bytes = "4.1" +sp-core-hashing = { version = "5.0.0", default-features = false, path = "../../primitives/core/hashing" } pallet-balances = { version = "4.0.0-dev", path = "../balances" } pallet-collective = { version = "4.0.0-dev", path = "../collective" }