From 0983b377680475f4ab331bf7c6f6954342c6e376 Mon Sep 17 00:00:00 2001 From: John Guibas Date: Wed, 10 Jul 2024 10:32:59 -0700 Subject: [PATCH 1/2] feat: initial pass at zkvm compilation add std feature to network-peers get every crate to compile Wasm cleanup cleanup diff --- .github/assets/check_no_std.sh | 33 ----- .github/assets/check_wasm.sh | 32 ++++ .github/workflows/lint.yml | 4 +- Cargo.lock | 74 ++++++++-- Cargo.toml | 23 ++- crates/consensus/consensus/Cargo.toml | 2 +- crates/errors/Cargo.toml | 4 + crates/ethereum/evm/Cargo.toml | 2 +- crates/net/peers/Cargo.toml | 2 + .../src/constants/gas_units.rs | 5 +- crates/primitives-traits/src/lib.rs | 3 + crates/primitives/Cargo.toml | 8 +- crates/primitives/src/lib.rs | 8 +- crates/primitives/src/transaction/eip1559.rs | 3 + crates/primitives/src/transaction/eip2930.rs | 3 + crates/primitives/src/transaction/legacy.rs | 3 + crates/primitives/src/transaction/util.rs | 137 ++++++++++++------ crates/rpc/rpc-eth-api/Cargo.toml | 6 +- crates/storage/codecs/src/lib.rs | 7 +- crates/storage/db-api/Cargo.toml | 2 + crates/storage/db/Cargo.toml | 6 +- crates/storage/db/src/lib.rs | 3 + crates/storage/nippy-jar/Cargo.toml | 2 +- crates/storage/provider/Cargo.toml | 2 +- crates/transaction-pool/Cargo.toml | 4 +- 25 files changed, 259 insertions(+), 119 deletions(-) delete mode 100755 .github/assets/check_no_std.sh create mode 100755 .github/assets/check_wasm.sh diff --git a/.github/assets/check_no_std.sh b/.github/assets/check_no_std.sh deleted file mode 100755 index f19e39ddac90..000000000000 --- a/.github/assets/check_no_std.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail - -# TODO -no_std_packages=( -# reth-codecs -# reth-consensus -# reth-db -# reth-errors -# reth-ethereum-forks -# reth-evm -# reth-evm-ethereum -# reth-network-peers -# reth-primitives -# reth-primitives-traits -# reth-revm -) - -for package in "${no_std_packages[@]}"; do - cmd="cargo +stable build -p $package --target riscv32imac-unknown-none-elf --no-default-features" - - if [ -n "$CI" ]; then - echo "::group::$cmd" - else - printf "\n%s:\n %s\n" "$package" "$cmd" - fi - - $cmd - - if [ -n "$CI" ]; then - echo "::endgroup::" - fi -done diff --git a/.github/assets/check_wasm.sh b/.github/assets/check_wasm.sh new file mode 100755 index 000000000000..4b6dd6202d31 --- /dev/null +++ b/.github/assets/check_wasm.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -eo pipefail + +wasm_packages=( + reth-codecs + reth-consensus + reth-db + reth-db-api + reth-ethereum-forks + reth-evm + reth-evm-ethereum + reth-network-peers + reth-primitives + reth-primitives-traits + reth-revm +) + +for package in "${wasm_packages[@]}"; do + cmd="cargo +stable build -p $package --target wasm32-wasi --no-default-features --ignore-rust-version --features std" + + if [ -n "$CI" ]; then + echo "::group::$cmd1 || $cmd2" + else + printf "\n%s:\n %s\n" "$package" "$cmd" + fi + + $cmd + + if [ -n "$CI" ]; then + echo "::endgroup::" + fi +done \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3aefc21c8389..a5d0a5416d56 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -45,7 +45,7 @@ jobs: env: RUSTFLAGS: -D warnings - no-std: + wasm: runs-on: ubuntu-latest timeout-minutes: 30 steps: @@ -57,7 +57,7 @@ jobs: - uses: Swatinem/rust-cache@v2 with: cache-on-failure: true - - name: Run no_std checks + - name: Run wasm checks run: .github/assets/check_no_std.sh crate-checks: diff --git a/Cargo.lock b/Cargo.lock index 1494c0d9d21b..3a1934db6e4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1278,6 +1278,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bls12_381" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7bc6d6292be3a19e6379786dac800f551e5865a5bb51ebbe3064ab80433f403" +dependencies = [ + "ff", + "group", + "pairing", + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "blst" version = "0.3.12" @@ -3170,6 +3183,7 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ + "bitvec", "rand_core 0.6.4", "subtle", ] @@ -4533,6 +4547,21 @@ version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4933f3f57a8e9d9da04db23fb153356ecaf00cbd14aee46279c33dc80925c37" +[[package]] +name = "kzg-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd9920cd4460ce3cbca19c62f3bb9a9611562478a4dc9d2c556f4a7d049c5b6b" +dependencies = [ + "bls12_381", + "glob", + "hex", + "once_cell", + "serde", + "serde_derive", + "serde_yaml", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -4561,7 +4590,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" dependencies = [ "cfg-if", - "windows-targets 0.52.6", + "windows-targets 0.48.5", ] [[package]] @@ -5410,6 +5439,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "pairing" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fec4625e73cf41ef4bb6846cafa6d44736525f442ba45e407c4a000a13996f" +dependencies = [ + "group", +] + [[package]] name = "parity-scale-codec" version = "3.6.12" @@ -8060,8 +8098,10 @@ dependencies = [ "assert_matches", "bytes", "c-kzg", + "cfg-if", "criterion", "derive_more", + "k256", "modular-bitfield", "nybbles", "once_cell", @@ -8861,8 +8901,6 @@ dependencies = [ [[package]] name = "revm" version = "11.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44102920a77b38b0144f4b84dcaa31fe44746e78f53685c2ca0149af5312e048" dependencies = [ "auto_impl", "cfg-if", @@ -8876,8 +8914,6 @@ dependencies = [ [[package]] name = "revm-inspectors" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "083fe9c20db39ab4d371e9c4d10367408fa3565ad277a4fa1770f7d9314e1b92" dependencies = [ "alloy-primitives", "alloy-rpc-types", @@ -8894,8 +8930,6 @@ dependencies = [ [[package]] name = "revm-interpreter" version = "7.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b319602039af3d130f792beba76592e7744bb3c4f2db5179758be33985a16b" dependencies = [ "revm-primitives", "serde", @@ -8904,14 +8938,12 @@ dependencies = [ [[package]] name = "revm-precompile" version = "9.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b441000a0d30e06269f822f42a13fa6bec922e951a84b643818651472c4fe6" dependencies = [ "aurora-engine-modexp", - "blst", "c-kzg", "cfg-if", "k256", + "kzg-rs", "once_cell", "p256", "revm-primitives", @@ -8924,8 +8956,6 @@ dependencies = [ [[package]] name = "revm-primitives" version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b518f536bacee396eb28a43f0984b25b2cd80f052ba4f2e794d554d711c13f33" dependencies = [ "alloy-eips", "alloy-primitives", @@ -8939,6 +8969,7 @@ dependencies = [ "enumn", "hashbrown 0.14.5", "hex", + "kzg-rs", "once_cell", "serde", ] @@ -9509,6 +9540,19 @@ dependencies = [ "syn 2.0.69", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.2.6", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "serial_test" version = "3.1.1" @@ -10719,6 +10763,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "unsigned-varint" version = "0.7.2" diff --git a/Cargo.toml b/Cargo.toml index 64cb72a53a33..d387f1ba1786 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -309,7 +309,7 @@ reth-evm = { path = "crates/evm" } reth-evm-ethereum = { path = "crates/ethereum/evm" } reth-evm-optimism = { path = "crates/optimism/evm" } reth-execution-errors = { path = "crates/evm/execution-errors" } -reth-execution-types = { path = "crates/evm/execution-types" } +reth-execution-types = { path = "crates/evm/execution-types", default-features = false } reth-exex = { path = "crates/exex/exex" } reth-exex-test-utils = { path = "crates/exex/test-utils" } reth-exex-types = { path = "crates/exex/types" } @@ -326,7 +326,7 @@ reth-network-api = { path = "crates/net/network-api" } reth-network-types = { path = "crates/net/network-types" } reth-network-peers = { path = "crates/net/peers", default-features = false } reth-network-p2p = { path = "crates/net/p2p" } -reth-nippy-jar = { path = "crates/storage/nippy-jar" } +reth-nippy-jar = { path = "crates/storage/nippy-jar", default-features = false } reth-node-api = { path = "crates/node/api" } reth-node-builder = { path = "crates/node/builder" } reth-node-core = { path = "crates/node/core" } @@ -341,12 +341,12 @@ reth-optimism-rpc = { path = "crates/optimism/rpc" } reth-payload-builder = { path = "crates/payload/builder" } reth-payload-primitives = { path = "crates/payload/primitives" } reth-payload-validator = { path = "crates/payload/validator" } -reth-primitives = { path = "crates/primitives" } +reth-primitives = { path = "crates/primitives", default-features = false } reth-primitives-traits = { path = "crates/primitives-traits" } reth-provider = { path = "crates/storage/provider" } reth-prune = { path = "crates/prune/prune" } reth-prune-types = { path = "crates/prune/types" } -reth-revm = { path = "crates/revm" } +reth-revm = { path = "crates/revm", default-features = false } reth-rpc = { path = "crates/rpc/rpc" } reth-rpc-api = { path = "crates/rpc/rpc-api" } reth-rpc-api-testing-util = { path = "crates/rpc/rpc-testing-util" } @@ -363,27 +363,26 @@ reth-stages-api = { path = "crates/stages/api" } reth-stages-types = { path = "crates/stages/types" } reth-static-file = { path = "crates/static-file/static-file" } reth-static-file-types = { path = "crates/static-file/types" } -reth-storage-api = { path = "crates/storage/storage-api" } +reth-storage-api = { path = "crates/storage/storage-api", default-features = false } reth-storage-errors = { path = "crates/storage/errors" } reth-tasks = { path = "crates/tasks" } reth-testing-utils = { path = "testing/testing-utils" } reth-tokio-util = { path = "crates/tokio-util" } reth-tracing = { path = "crates/tracing" } reth-transaction-pool = { path = "crates/transaction-pool" } -reth-trie = { path = "crates/trie/trie" } +reth-trie = { path = "crates/trie/trie", default-features = false } reth-trie-common = { path = "crates/trie/common" } reth-trie-parallel = { path = "crates/trie/parallel" } # revm -revm = { version = "11.0.0", features = [ +revm = { path = "../bluealloy-revm/crates/revm", features = [ "std", - "secp256k1", - "blst", + "kzg-rs", ], default-features = false } -revm-primitives = { version = "6.0.0", features = [ - "std", +revm-primitives = { path = "../bluealloy-revm/crates/primitives", features = [ + "std" ], default-features = false } -revm-inspectors = "0.4" +revm-inspectors = { path = "../revm-inspectors" } # eth alloy-chains = "0.1.15" diff --git a/crates/consensus/consensus/Cargo.toml b/crates/consensus/consensus/Cargo.toml index 1d4d6d758c42..5b69fb68cf35 100644 --- a/crates/consensus/consensus/Cargo.toml +++ b/crates/consensus/consensus/Cargo.toml @@ -20,5 +20,5 @@ thiserror-no-std = {workspace = true, default-features = false } [features] default = ["std"] -std = ["thiserror-no-std/std"] +std = ["thiserror-no-std/std", "reth-primitives/std"] test-utils = [] diff --git a/crates/errors/Cargo.toml b/crates/errors/Cargo.toml index bb56a8bace56..f7448d1a136d 100644 --- a/crates/errors/Cargo.toml +++ b/crates/errors/Cargo.toml @@ -19,3 +19,7 @@ reth-storage-errors.workspace = true # misc thiserror.workspace = true + +[features] +default = ["std"] +std = [] diff --git a/crates/ethereum/evm/Cargo.toml b/crates/ethereum/evm/Cargo.toml index 7ea2e4b587c9..3bf51ea07e24 100644 --- a/crates/ethereum/evm/Cargo.toml +++ b/crates/ethereum/evm/Cargo.toml @@ -36,4 +36,4 @@ serde_json.workspace = true [features] default = ["std"] -std = [] \ No newline at end of file +std = ["reth-revm/std"] \ No newline at end of file diff --git a/crates/net/peers/Cargo.toml b/crates/net/peers/Cargo.toml index 5ac24edea759..8b30ffacb396 100644 --- a/crates/net/peers/Cargo.toml +++ b/crates/net/peers/Cargo.toml @@ -35,5 +35,7 @@ serde_json.workspace = true tokio = { workspace = true, features = ["net", "macros", "rt"] } [features] +default = ["std"] +std = [] secp256k1 = ["dep:secp256k1", "enr/secp256k1"] net = ["dep:tokio", "tokio?/net"] diff --git a/crates/primitives-traits/src/constants/gas_units.rs b/crates/primitives-traits/src/constants/gas_units.rs index 0af0d2c24ce1..cba13e67f6cb 100644 --- a/crates/primitives-traits/src/constants/gas_units.rs +++ b/crates/primitives-traits/src/constants/gas_units.rs @@ -1,4 +1,7 @@ -use std::time::Duration; +#[cfg(not(feature = "std"))] +use alloc::{format, string::String}; + +use core::time::Duration; /// Represents one Kilogas, or `1_000` gas. pub const KILOGAS: u64 = 1_000; diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index b7a42d9c7b5c..be0d5f772f7f 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -9,6 +9,9 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![cfg_attr(not(feature = "std"), no_std)] +#[cfg(not(feature = "std"))] +extern crate alloc; + #[cfg(feature = "alloy-compat")] mod alloy_compat; diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 1e37184ab85a..79bce02bfd79 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -33,7 +33,8 @@ secp256k1 = { workspace = true, features = [ "global-context", "recovery", "rand", -] } +], optional = true } +k256 = { version = "0.13.3", default-features = false, features = ["ecdsa"], optional = true } # for eip-4844 c-kzg = { workspace = true, features = ["serde"], optional = true } @@ -51,6 +52,7 @@ zstd = { version = "0.13", features = ["experimental"], optional = true } # arbitrary utils arbitrary = { workspace = true, features = ["derive"], optional = true } proptest = { workspace = true, optional = true } +cfg-if = "1.0.0" # proptest-derive = { workspace = true, optional = true } [dev-dependencies] @@ -84,7 +86,7 @@ pprof = { workspace = true, features = [ secp256k1.workspace = true [features] -default = ["c-kzg", "zstd-codec", "alloy-compat", "std"] +default = ["c-kzg", "zstd-codec", "alloy-compat", "std", "secp256k1"] asm-keccak = ["alloy-primitives/asm-keccak"] arbitrary = [ "reth-primitives-traits/arbitrary", @@ -98,6 +100,8 @@ arbitrary = [ "dep:proptest", "zstd-codec", ] +secp256k1 = ["dep:secp256k1"] +k256 = ["dep:k256"] c-kzg = ["dep:c-kzg", "revm-primitives/c-kzg", "dep:tempfile", "alloy-eips/kzg"] zstd-codec = ["dep:zstd"] optimism = [ diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 29154d591e8e..961f688b4e7e 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -64,7 +64,6 @@ pub use transaction::{ pub use transaction::BlobTransactionValidationError; pub use transaction::{ - util::secp256k1::{public_key_to_address, recover_signer_unchecked, sign_message}, AccessList, AccessListItem, IntoRecoveredTransaction, InvalidTransactionError, Signature, Transaction, TransactionMeta, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, TryFromRecoveredTransaction, TxEip1559, TxEip2930, TxEip4844, @@ -72,6 +71,13 @@ pub use transaction::{ LEGACY_TX_TYPE_ID, }; +#[cfg(feature = "secp256k1")] +pub use transaction::util::secp256k1::{ + public_key_to_address, sign_message, +}; +pub use transaction::util::secp256k1::recover_signer_unchecked; + + // Re-exports pub use self::ruint::UintTryTo; pub use alloy_primitives::{ diff --git a/crates/primitives/src/transaction/eip1559.rs b/crates/primitives/src/transaction/eip1559.rs index cce6f0ca22fc..f4989fc07787 100644 --- a/crates/primitives/src/transaction/eip1559.rs +++ b/crates/primitives/src/transaction/eip1559.rs @@ -4,6 +4,9 @@ use alloy_rlp::{length_of_length, Decodable, Encodable, Header}; use core::mem; use reth_codecs::{main_codec, Compact}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// A transaction with a priority fee ([EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)). #[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] diff --git a/crates/primitives/src/transaction/eip2930.rs b/crates/primitives/src/transaction/eip2930.rs index ebaa12785c1d..69caa6a1d376 100644 --- a/crates/primitives/src/transaction/eip2930.rs +++ b/crates/primitives/src/transaction/eip2930.rs @@ -4,6 +4,9 @@ use alloy_rlp::{length_of_length, Decodable, Encodable, Header}; use core::mem; use reth_codecs::{main_codec, Compact}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)). #[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] diff --git a/crates/primitives/src/transaction/legacy.rs b/crates/primitives/src/transaction/legacy.rs index 09b661cf7995..5d99570032b6 100644 --- a/crates/primitives/src/transaction/legacy.rs +++ b/crates/primitives/src/transaction/legacy.rs @@ -3,6 +3,9 @@ use alloy_rlp::{length_of_length, Encodable, Header}; use core::mem; use reth_codecs::{main_codec, Compact}; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Legacy transaction. #[main_codec] #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] diff --git a/crates/primitives/src/transaction/util.rs b/crates/primitives/src/transaction/util.rs index b4a2db7f6b52..1f6704e6f45c 100644 --- a/crates/primitives/src/transaction/util.rs +++ b/crates/primitives/src/transaction/util.rs @@ -1,49 +1,98 @@ -pub(crate) mod secp256k1 { - use super::*; - use crate::{keccak256, Address, Signature}; - pub(crate) use ::secp256k1::Error; - use ::secp256k1::{ - ecdsa::{RecoverableSignature, RecoveryId}, - Message, PublicKey, SecretKey, SECP256K1, - }; - use revm_primitives::{B256, U256}; - - /// Recovers the address of the sender using secp256k1 pubkey recovery. - /// - /// Converts the public key into an ethereum address by hashing the public key with keccak256. - /// - /// This does not ensure that the `s` value in the signature is low, and _just_ wraps the - /// underlying secp256k1 library. - pub fn recover_signer_unchecked(sig: &[u8; 65], msg: &[u8; 32]) -> Result { - let sig = - RecoverableSignature::from_compact(&sig[0..64], RecoveryId::from_i32(sig[64] as i32)?)?; - - let public = SECP256K1.recover_ecdsa(&Message::from_digest(*msg), &sig)?; - Ok(public_key_to_address(public)) - } +cfg_if::cfg_if! { + if #[cfg(feature = "secp256k1")] { + pub(crate) mod secp256k1 { + use super::*; + use crate::{keccak256, Address, Signature}; + pub(crate) use ::secp256k1::Error; + use ::secp256k1::{ + ecdsa::{RecoverableSignature, RecoveryId}, + Message, PublicKey, SecretKey, SECP256K1, + }; + use revm_primitives::{B256, U256}; - /// Signs message with the given secret key. - /// Returns the corresponding signature. - pub fn sign_message(secret: B256, message: B256) -> Result { - let sec = SecretKey::from_slice(secret.as_ref())?; - let s = SECP256K1.sign_ecdsa_recoverable(&Message::from_digest(message.0), &sec); - let (rec_id, data) = s.serialize_compact(); - - let signature = Signature { - r: U256::try_from_be_slice(&data[..32]).expect("The slice has at most 32 bytes"), - s: U256::try_from_be_slice(&data[32..64]).expect("The slice has at most 32 bytes"), - odd_y_parity: rec_id.to_i32() != 0, - }; - Ok(signature) - } + /// Recovers the address of the sender using secp256k1 pubkey recovery. + /// + /// Converts the public key into an ethereum address by hashing the public key with keccak256. + /// + /// This does not ensure that the `s` value in the signature is low, and _just_ wraps the + /// underlying secp256k1 library. + pub fn recover_signer_unchecked(sig: &[u8; 65], msg: &[u8; 32]) -> Result { + let sig = + RecoverableSignature::from_compact(&sig[0..64], RecoveryId::from_i32(sig[64] as i32)?)?; + + let public = SECP256K1.recover_ecdsa(&Message::from_digest(*msg), &sig)?; + Ok(public_key_to_address(public)) + } + + /// Signs message with the given secret key. + /// Returns the corresponding signature. + pub fn sign_message(secret: B256, message: B256) -> Result { + let sec = SecretKey::from_slice(secret.as_ref())?; + let s = SECP256K1.sign_ecdsa_recoverable(&Message::from_digest(message.0), &sec); + let (rec_id, data) = s.serialize_compact(); + + let signature = Signature { + r: U256::try_from_be_slice(&data[..32]).expect("The slice has at most 32 bytes"), + s: U256::try_from_be_slice(&data[32..64]).expect("The slice has at most 32 bytes"), + odd_y_parity: rec_id.to_i32() != 0, + }; + Ok(signature) + } + + /// Converts a public key into an ethereum address by hashing the encoded public key with + /// keccak256. + pub fn public_key_to_address(public: PublicKey) -> Address { + // strip out the first byte because that should be the SECP256K1_TAG_PUBKEY_UNCOMPRESSED + // tag returned by libsecp's uncompressed pubkey serialization + let hash = keccak256(&public.serialize_uncompressed()[1..]); + Address::from_slice(&hash[12..]) + } + } + } else if #[cfg(feature = "k256")] { + pub(crate) mod secp256k1 { + use super::*; + use crate::{keccak256, Address}; + pub(crate) use ::k256::ecdsa::Error; + use k256::ecdsa::{RecoveryId, Signature, VerifyingKey}; + + /// Recovers the address of the sender using secp256k1 pubkey recovery. + /// + /// Converts the public key into an ethereum address by hashing the public key with keccak256. + /// + /// This does not ensure that the `s` value in the signature is low, and _just_ wraps the + /// underlying secp256k1 library. + pub fn recover_signer_unchecked(sig: &[u8; 65], msg: &[u8; 32]) -> Result { + let mut signature = Signature::from_slice(&sig[0..64])?; + let mut recid = sig[64]; + + // normalize signature and flip recovery id if needed. + if let Some(sig_normalized) = signature.normalize_s() { + signature = sig_normalized; + recid ^= 1; + } + let recid = RecoveryId::from_byte(recid).expect("recovery ID is valid"); + + // recover key + let recovered_key = VerifyingKey::recover_from_prehash(&msg[..], &signature, recid)?; + Ok(public_key_to_address(recovered_key)) + } + + /// Converts a public key into an ethereum address by hashing the encoded public key with + /// keccak256. + fn public_key_to_address(public: VerifyingKey) -> Address { + let hash = keccak256(&public.to_encoded_point(/* compress = */ false).as_bytes()[1..]); + Address::from_slice(&hash[12..]) + } + } + } else { + pub(crate) mod secp256k1 { + use super::*; + use crate::{keccak256, Address}; - /// Converts a public key into an ethereum address by hashing the encoded public key with - /// keccak256. - pub fn public_key_to_address(public: PublicKey) -> Address { - // strip out the first byte because that should be the SECP256K1_TAG_PUBKEY_UNCOMPRESSED - // tag returned by libsecp's uncompressed pubkey serialization - let hash = keccak256(&public.serialize_uncompressed()[1..]); - Address::from_slice(&hash[12..]) + pub fn recover_signer_unchecked(sig: &[u8; 65], msg: &[u8; 32]) -> Result { + unimplemented!("please enable the `secp256k1` or `k256` feature") + } + } } } diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index fb8d84b3f4b7..ad0c23e71e20 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -18,15 +18,15 @@ revm-inspectors = { workspace = true, features = ["js-tracer"] } revm-primitives = { workspace = true, features = ["dev"] } reth-errors.workspace = true reth-evm.workspace = true -reth-primitives.workspace = true +reth-primitives = { workspace = true, default-features = true } reth-provider.workspace = true -reth-revm.workspace = true +reth-revm = { workspace = true, default-features = true } reth-rpc-types.workspace = true reth-rpc-types-compat.workspace = true reth-tasks = { workspace = true, features = ["rayon"] } reth-transaction-pool.workspace = true reth-chainspec.workspace = true -reth-execution-types.workspace = true +reth-execution-types = { workspace = true, default-features = true } reth-rpc-eth-types.workspace = true reth-rpc-server-types.workspace = true diff --git a/crates/storage/codecs/src/lib.rs b/crates/storage/codecs/src/lib.rs index bea26090deb1..e1220d176ef7 100644 --- a/crates/storage/codecs/src/lib.rs +++ b/crates/storage/codecs/src/lib.rs @@ -17,8 +17,13 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![cfg_attr(not(feature = "std"), no_std)] -pub use reth_codecs_derive::*; +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; +pub use reth_codecs_derive::*; use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U256}; use bytes::Buf; diff --git a/crates/storage/db-api/Cargo.toml b/crates/storage/db-api/Cargo.toml index 7286e03f2da4..cf95a801c2d5 100644 --- a/crates/storage/db-api/Cargo.toml +++ b/crates/storage/db-api/Cargo.toml @@ -65,6 +65,8 @@ paste.workspace = true assert_matches.workspace = true [features] +default = ["std"] +std = ["reth-primitives/std"] test-utils = ["arbitrary"] arbitrary = [ "reth-primitives/arbitrary", diff --git a/crates/storage/db/Cargo.toml b/crates/storage/db/Cargo.toml index 117ec5ccc7b6..738f4d6fbf9c 100644 --- a/crates/storage/db/Cargo.toml +++ b/crates/storage/db/Cargo.toml @@ -18,7 +18,7 @@ reth-primitives.workspace = true reth-primitives-traits.workspace = true reth-fs-util.workspace = true reth-storage-errors.workspace = true -reth-nippy-jar.workspace = true +reth-nippy-jar = { workspace = true, optional = true } reth-prune-types.workspace = true reth-stages-types.workspace = true reth-tracing.workspace = true @@ -76,13 +76,15 @@ paste.workspace = true assert_matches.workspace = true [features] -default = ["mdbx"] +default = ["mdbx", "static-file", "std"] +std = ["reth-primitives/std"] mdbx = ["dep:reth-libmdbx", "dep:eyre"] test-utils = ["dep:tempfile", "arbitrary"] bench = [] arbitrary = ["reth-primitives/arbitrary", "reth-db-api/arbitrary"] optimism = [] disable-lock = [] +static-file = ["dep:reth-nippy-jar"] [[bench]] name = "hash_keys" diff --git a/crates/storage/db/src/lib.rs b/crates/storage/db/src/lib.rs index e5414b574328..001108ef92a2 100644 --- a/crates/storage/db/src/lib.rs +++ b/crates/storage/db/src/lib.rs @@ -18,7 +18,10 @@ mod implementation; pub mod lockfile; mod metrics; + +#[cfg(feature = "static-file")] pub mod static_file; + pub mod tables; mod utils; pub mod version; diff --git a/crates/storage/nippy-jar/Cargo.toml b/crates/storage/nippy-jar/Cargo.toml index dcc916e783f8..7ec1ab656c71 100644 --- a/crates/storage/nippy-jar/Cargo.toml +++ b/crates/storage/nippy-jar/Cargo.toml @@ -47,4 +47,4 @@ tempfile.workspace = true [features] default = [] -test-utils = [] +test-utils = [] \ No newline at end of file diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index 6cf456665bdf..3da8b0274555 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -22,7 +22,7 @@ reth-errors.workspace = true reth-storage-errors.workspace = true reth-storage-api.workspace = true reth-network-p2p.workspace = true -reth-db = { workspace = true, features = ["mdbx"] } +reth-db = { workspace = true, features = ["mdbx", "static-file"] } reth-db-api.workspace = true reth-prune-types.workspace = true reth-stages-types.workspace = true diff --git a/crates/transaction-pool/Cargo.toml b/crates/transaction-pool/Cargo.toml index 77edd6f3e541..5997b5c27fc2 100644 --- a/crates/transaction-pool/Cargo.toml +++ b/crates/transaction-pool/Cargo.toml @@ -15,12 +15,12 @@ workspace = true # reth reth-chainspec.workspace = true reth-eth-wire-types.workspace = true -reth-primitives.workspace = true +reth-primitives = { workspace = true, features = ["c-kzg"] } reth-execution-types.workspace = true reth-fs-util.workspace = true reth-provider.workspace = true reth-tasks.workspace = true -revm.workspace = true +revm = { workspace = true, features = ["c-kzg"] } # ethereum alloy-rlp.workspace = true From 2d2d9e0c7008f402c3dc5d27003edd1782aa0982 Mon Sep 17 00:00:00 2001 From: John Guibas Date: Wed, 10 Jul 2024 17:59:21 -0700 Subject: [PATCH 2/2] page_size feature --- crates/storage/db/Cargo.toml | 5 +++-- crates/storage/db/src/utils.rs | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/storage/db/Cargo.toml b/crates/storage/db/Cargo.toml index 738f4d6fbf9c..f6ceb6670ecb 100644 --- a/crates/storage/db/Cargo.toml +++ b/crates/storage/db/Cargo.toml @@ -40,7 +40,7 @@ metrics.workspace = true # misc bytes.workspace = true -page_size = "0.6.0" +page_size = { version = "0.6.0", optional = true } thiserror.workspace = true tempfile = { workspace = true, optional = true } derive_more.workspace = true @@ -76,7 +76,7 @@ paste.workspace = true assert_matches.workspace = true [features] -default = ["mdbx", "static-file", "std"] +default = ["mdbx", "static-file", "std", "page-size"] std = ["reth-primitives/std"] mdbx = ["dep:reth-libmdbx", "dep:eyre"] test-utils = ["dep:tempfile", "arbitrary"] @@ -85,6 +85,7 @@ arbitrary = ["reth-primitives/arbitrary", "reth-db-api/arbitrary"] optimism = [] disable-lock = [] static-file = ["dep:reth-nippy-jar"] +page-size = ["dep:page_size"] [[bench]] name = "hash_keys" diff --git a/crates/storage/db/src/utils.rs b/crates/storage/db/src/utils.rs index cf6a0341ef7a..d90572143a90 100644 --- a/crates/storage/db/src/utils.rs +++ b/crates/storage/db/src/utils.rs @@ -3,6 +3,7 @@ use std::path::Path; /// Returns the default page size that can be used in this OS. +#[cfg(feature = "page_size")] pub(crate) fn default_page_size() -> usize { let os_page_size = page_size::get(); @@ -16,6 +17,11 @@ pub(crate) fn default_page_size() -> usize { os_page_size.clamp(min_page_size, libmdbx_max_page_size) } +#[cfg(not(feature = "page_size"))] +pub(crate) fn default_page_size() -> usize { + 0 +} + /// Check if a db is empty. It does not provide any information on the /// validity of the data in it. We consider a database as non empty when it's a non empty directory. pub fn is_database_empty>(path: P) -> bool {