From 9dd58234734232e6bd780d1e018e8f4f8c7a4704 Mon Sep 17 00:00:00 2001 From: Cem Eliguzel Date: Sat, 12 Aug 2023 11:19:16 +0000 Subject: [PATCH] Replace sha3 crate with sp-core-hashing --- Cargo.lock | 32 ++++------------------- Cargo.toml | 3 ++- precompiles/Cargo.toml | 1 - precompiles/macro/Cargo.toml | 5 ++-- precompiles/macro/src/lib.rs | 4 +-- precompiles/macro/src/precompile/mod.rs | 2 +- precompiles/macro/src/precompile/parse.rs | 2 +- precompiles/macro/tests/tests.rs | 8 +++--- precompiles/tests-external/Cargo.toml | 1 - 9 files changed, 18 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d8be06d6e..8cc252054f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2694,7 +2694,7 @@ dependencies = [ "ethereum-types", "fp-evm", "frame-support", - "num_enum 0.6.1", + "num_enum", "parity-scale-codec", "sp-std", ] @@ -5293,33 +5293,13 @@ dependencies = [ "libc", ] -[[package]] -name = "num_enum" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f646caf906c20226733ed5b1374287eb97e3c2a5c227ce668c1f2ce20ae57c9" -dependencies = [ - "num_enum_derive 0.5.11", -] - [[package]] name = "num_enum" version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1" dependencies = [ - "num_enum_derive 0.6.1", -] - -[[package]] -name = "num_enum_derive" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", + "num_enum_derive", ] [[package]] @@ -6272,13 +6252,12 @@ dependencies = [ "hex-literal", "impl-trait-for-tuples", "log", - "num_enum 0.6.1", + "num_enum", "pallet-evm", "parity-scale-codec", "precompile-utils-macro", "scale-info", "serde", - "sha3", "similar-asserts", "sp-core", "sp-io", @@ -6294,13 +6273,13 @@ dependencies = [ "fp-evm", "frame-support", "macrotest", - "num_enum 0.5.11", + "num_enum", "precompile-utils", "prettyplease 0.1.25", "proc-macro2", "quote", "sha3", - "sp-core", + "sp-core-hashing", "sp-std", "syn 1.0.109", "trybuild", @@ -6323,7 +6302,6 @@ dependencies = [ "precompile-utils", "scale-info", "serde", - "sha3", "sp-core", "sp-io", "sp-runtime", diff --git a/Cargo.toml b/Cargo.toml index c62c723c58..7fead3edb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,6 @@ scale-codec = { package = "parity-scale-codec", version = "3.6.4", default-featu scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } serde_json = "1.0" -sha3 = { version = "0.10", default-features = false } similar-asserts = "1.1.0" sqlx = { version = "0.7.1", default-features = false, features = ["macros"] } thiserror = "1.0" @@ -104,6 +103,8 @@ sp-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/su sp-consensus-aura = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-consensus-grandpa = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-core = { version = "21.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +sp-core-hashing = { version = "9.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +sp-core-hashing-proc-macro = { version = "9.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-database = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" } sp-inherents = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-io = { version = "23.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } diff --git a/precompiles/Cargo.toml b/precompiles/Cargo.toml index c9dd239b2f..bb7f4359ef 100644 --- a/precompiles/Cargo.toml +++ b/precompiles/Cargo.toml @@ -15,7 +15,6 @@ log = { workspace = true } num_enum = { workspace = true } scale-info = { workspace = true, optional = true, features = ["derive"] } serde = { workspace = true, optional = true } -sha3 = { workspace = true } similar-asserts = { workspace = true, optional = true } # Moonbeam diff --git a/precompiles/macro/Cargo.toml b/precompiles/macro/Cargo.toml index 554d8aa6a0..4ca6246dec 100644 --- a/precompiles/macro/Cargo.toml +++ b/precompiles/macro/Cargo.toml @@ -14,11 +14,12 @@ path = "tests/tests.rs" [dependencies] case = "1.0" -num_enum = { version = "0.5.3", default-features = false } +num_enum = { workspace = true } prettyplease = "0.1.18" proc-macro2 = "1.0" quote = "1.0" sha3 = "0.10" +sp-core-hashing = { workspace = true } syn = { version = "1.0", features = ["extra-traits", "fold", "full", "visit"] } [dev-dependencies] @@ -29,5 +30,5 @@ precompile-utils = { path = "../", features = ["testing"] } fp-evm = { workspace = true } frame-support = { workspace = true } -sp-core = { workspace = true } +sp-core-hashing = { workspace = true } sp-std = { workspace = true } diff --git a/precompiles/macro/src/lib.rs b/precompiles/macro/src/lib.rs index 9214482837..804013ab76 100644 --- a/precompiles/macro/src/lib.rs +++ b/precompiles/macro/src/lib.rs @@ -19,7 +19,7 @@ extern crate proc_macro; use proc_macro::TokenStream; use quote::{quote, quote_spanned}; -use sha3::{Digest, Keccak256}; +use sp_core_hashing::keccak_256; use syn::{parse_macro_input, spanned::Spanned, Expr, Ident, ItemType, Lit, LitStr}; mod derive_codec; @@ -47,7 +47,7 @@ impl ::std::fmt::Debug for Bytes { pub fn keccak256(input: TokenStream) -> TokenStream { let lit_str = parse_macro_input!(input as LitStr); - let hash = Keccak256::digest(lit_str.value().as_bytes()); + let hash = keccak_256(lit_str.value().as_bytes()); let bytes = Bytes(hash.to_vec()); let eval_str = format!("{:?}", bytes); diff --git a/precompiles/macro/src/precompile/mod.rs b/precompiles/macro/src/precompile/mod.rs index 4cd63ed287..e69baf0972 100644 --- a/precompiles/macro/src/precompile/mod.rs +++ b/precompiles/macro/src/precompile/mod.rs @@ -19,7 +19,7 @@ use proc_macro::TokenStream; use proc_macro2::Span; use quote::{format_ident, quote, quote_spanned, ToTokens}; -use sha3::{Digest, Keccak256}; +use sp_core_hashing::keccak_256; use std::collections::BTreeMap; use syn::{parse_macro_input, spanned::Spanned}; diff --git a/precompiles/macro/src/precompile/parse.rs b/precompiles/macro/src/precompile/parse.rs index d4df60595f..cc5033b1ee 100644 --- a/precompiles/macro/src/precompile/parse.rs +++ b/precompiles/macro/src/precompile/parse.rs @@ -528,7 +528,7 @@ impl Precompile { } // Compute the 4-bytes selector. - let digest = Keccak256::digest(signature.as_bytes()); + let digest = keccak_256(signature.as_bytes()); let selector = u32::from_be_bytes([digest[0], digest[1], digest[2], digest[3]]); if let Some(previous) = self diff --git a/precompiles/macro/tests/tests.rs b/precompiles/macro/tests/tests.rs index b392fd027f..c91f52835a 100644 --- a/precompiles/macro/tests/tests.rs +++ b/precompiles/macro/tests/tests.rs @@ -14,21 +14,21 @@ // You should have received a copy of the GNU General Public License // along with Moonbeam. If not, see . -use sha3::{Digest, Keccak256}; +use sp_core_hashing::keccak_256; #[test] fn test_keccak256() { assert_eq!( &precompile_utils_macro::keccak256!(""), - Keccak256::digest(b"").as_slice(), + keccak_256(b"").as_slice(), ); assert_eq!( &precompile_utils_macro::keccak256!("toto()"), - Keccak256::digest(b"toto()").as_slice(), + keccak_256(b"toto()").as_slice(), ); assert_ne!( &precompile_utils_macro::keccak256!("toto()"), - Keccak256::digest(b"tata()").as_slice(), + keccak_256(b"tata()").as_slice(), ); } diff --git a/precompiles/tests-external/Cargo.toml b/precompiles/tests-external/Cargo.toml index 60e94642e5..87fae7e552 100644 --- a/precompiles/tests-external/Cargo.toml +++ b/precompiles/tests-external/Cargo.toml @@ -12,7 +12,6 @@ derive_more = { workspace = true } hex-literal = { workspace = true } precompile-utils = { workspace = true, features = ["testing"] } serde = { workspace = true } -sha3 = { workspace = true } frame-support = { workspace = true } frame-system = { workspace = true }