From 9b1d676438296e3a00fd6879903315aa1ba100b5 Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Sat, 30 Nov 2024 23:01:19 +0700 Subject: [PATCH] feat: move eip1186 conversion helpers to reth-trie-common proofs (#12985) Co-authored-by: Matthias Seitz --- Cargo.lock | 5 +-- crates/exex/types/Cargo.toml | 2 +- crates/exex/types/src/notification.rs | 9 ++--- crates/rpc/rpc-engine-api/Cargo.toml | 1 - crates/rpc/rpc-eth-api/Cargo.toml | 3 +- crates/rpc/rpc-eth-api/src/helpers/state.rs | 10 ++---- crates/rpc/rpc-eth-api/src/lib.rs | 11 +++--- crates/rpc/rpc-types-compat/Cargo.toml | 2 -- crates/rpc/rpc-types-compat/src/lib.rs | 2 -- crates/rpc/rpc-types-compat/src/proof.rs | 37 -------------------- crates/trie/common/Cargo.toml | 29 ++++++++++------ crates/trie/common/src/proofs.rs | 38 +++++++++++++++++++++ 12 files changed, 75 insertions(+), 74 deletions(-) delete mode 100644 crates/rpc/rpc-types-compat/src/proof.rs diff --git a/Cargo.lock b/Cargo.lock index e7859a6cc..424ff503b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9001,6 +9001,7 @@ dependencies = [ "reth-tasks", "reth-transaction-pool", "reth-trie", + "reth-trie-common", "revm", "revm-inspectors", "revm-primitives", @@ -9093,10 +9094,8 @@ dependencies = [ "alloy-rlp", "alloy-rpc-types-engine", "alloy-rpc-types-eth", - "alloy-serde", "jsonrpsee-types", "reth-primitives", - "reth-trie-common", "serde", "serde_json", ] @@ -9403,6 +9402,8 @@ dependencies = [ "alloy-genesis", "alloy-primitives", "alloy-rlp", + "alloy-rpc-types-eth", + "alloy-serde", "alloy-trie", "arbitrary", "bincode", diff --git a/crates/exex/types/Cargo.toml b/crates/exex/types/Cargo.toml index 4d99bd7e6..b7e659d80 100644 --- a/crates/exex/types/Cargo.toml +++ b/crates/exex/types/Cargo.toml @@ -15,7 +15,7 @@ workspace = true # reth reth-chain-state.workspace = true reth-execution-types.workspace = true -reth-primitives.workspace = true +reth-primitives = { workspace = true, optional = true } reth-primitives-traits.workspace = true # reth diff --git a/crates/exex/types/src/notification.rs b/crates/exex/types/src/notification.rs index 44eeb2508..19e47c0a1 100644 --- a/crates/exex/types/src/notification.rs +++ b/crates/exex/types/src/notification.rs @@ -73,12 +73,11 @@ impl From> for ExExNotification

/// Bincode-compatible [`ExExNotification`] serde implementation. #[cfg(all(feature = "serde", feature = "serde-bincode-compat"))] pub(super) mod serde_bincode_compat { - use std::sync::Arc; - use reth_execution_types::serde_bincode_compat::Chain; use reth_primitives::{EthPrimitives, NodePrimitives}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_with::{DeserializeAs, SerializeAs}; + use std::sync::Arc; /// Bincode-compatible [`super::ExExNotification`] serde implementation. /// @@ -171,16 +170,14 @@ pub(super) mod serde_bincode_compat { #[cfg(test)] mod tests { - use std::sync::Arc; - + use super::super::{serde_bincode_compat, ExExNotification}; use arbitrary::Arbitrary; use rand::Rng; use reth_execution_types::Chain; use reth_primitives::SealedBlockWithSenders; use serde::{Deserialize, Serialize}; use serde_with::serde_as; - - use super::super::{serde_bincode_compat, ExExNotification}; + use std::sync::Arc; #[test] fn test_exex_notification_bincode_roundtrip() { diff --git a/crates/rpc/rpc-engine-api/Cargo.toml b/crates/rpc/rpc-engine-api/Cargo.toml index 4854ac44d..f9f05da33 100644 --- a/crates/rpc/rpc-engine-api/Cargo.toml +++ b/crates/rpc/rpc-engine-api/Cargo.toml @@ -54,7 +54,6 @@ reth-provider = { workspace = true, features = ["test-utils"] } reth-payload-builder = { workspace = true, features = ["test-utils"] } reth-tokio-util.workspace = true reth-testing-utils.workspace = true - alloy-rlp.workspace = true assert_matches.workspace = true \ No newline at end of file diff --git a/crates/rpc/rpc-eth-api/Cargo.toml b/crates/rpc/rpc-eth-api/Cargo.toml index e4b1b2807..859caa821 100644 --- a/crates/rpc/rpc-eth-api/Cargo.toml +++ b/crates/rpc/rpc-eth-api/Cargo.toml @@ -29,8 +29,9 @@ reth-execution-types.workspace = true reth-rpc-eth-types.workspace = true reth-rpc-server-types.workspace = true reth-network-api.workspace = true -reth-trie.workspace = true reth-node-api.workspace = true +reth-trie.workspace = true +reth-trie-common = { workspace = true, features = ["eip1186"] } # ethereum alloy-serde.workspace = true diff --git a/crates/rpc/rpc-eth-api/src/helpers/state.rs b/crates/rpc/rpc-eth-api/src/helpers/state.rs index 7ff9fa4de..a8ca28fed 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/state.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/state.rs @@ -1,6 +1,7 @@ //! Loads a pending block from database. Helper trait for `eth_` block, transaction, call and trace //! RPC methods. - +use super::{EthApiSpec, LoadPendingBlock, SpawnBlocking}; +use crate::{EthApiTypes, FromEthApiError, RpcNodeCore, RpcNodeCoreExt}; use alloy_consensus::{constants::KECCAK_EMPTY, Header}; use alloy_eips::BlockId; use alloy_primitives::{Address, Bytes, B256, U256}; @@ -15,14 +16,9 @@ use reth_provider::{ StateProviderFactory, }; use reth_rpc_eth_types::{EthApiError, PendingBlockEnv, RpcInvalidTransactionError}; -use reth_rpc_types_compat::proof::from_primitive_account_proof; use reth_transaction_pool::TransactionPool; use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId}; -use crate::{EthApiTypes, FromEthApiError, RpcNodeCore, RpcNodeCoreExt}; - -use super::{EthApiSpec, LoadPendingBlock, SpawnBlocking}; - /// Helper methods for `eth_` methods relating to state (accounts). pub trait EthState: LoadState + SpawnBlocking { /// Returns the maximum number of blocks into the past for generating state proofs. @@ -122,7 +118,7 @@ pub trait EthState: LoadState + SpawnBlocking { let proof = state .proof(Default::default(), address, &storage_keys) .map_err(Self::Error::from_eth_err)?; - Ok(from_primitive_account_proof(proof, keys)) + Ok(proof.into_eip1186_response(keys)) }) .await }) diff --git a/crates/rpc/rpc-eth-api/src/lib.rs b/crates/rpc/rpc-eth-api/src/lib.rs index cb97a03e8..c4a255985 100644 --- a/crates/rpc/rpc-eth-api/src/lib.rs +++ b/crates/rpc/rpc-eth-api/src/lib.rs @@ -20,16 +20,15 @@ pub mod node; pub mod pubsub; pub mod types; -pub use reth_rpc_eth_types::error::{ - AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError, -}; -pub use reth_rpc_types_compat::TransactionCompat; - pub use bundle::{EthBundleApiServer, EthCallBundleApiServer}; pub use core::{EthApiServer, FullEthApiServer}; pub use filter::EthFilterApiServer; pub use node::{RpcNodeCore, RpcNodeCoreExt}; pub use pubsub::EthPubSubApiServer; +pub use reth_rpc_eth_types::error::{ + AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError, +}; +pub use reth_rpc_types_compat::TransactionCompat; pub use types::{EthApiTypes, FullEthApiTypes, RpcBlock, RpcReceipt, RpcTransaction}; #[cfg(feature = "client")] @@ -38,3 +37,5 @@ pub use bundle::{EthBundleApiClient, EthCallBundleApiClient}; pub use core::EthApiClient; #[cfg(feature = "client")] pub use filter::EthFilterApiClient; + +use reth_trie_common as _; diff --git a/crates/rpc/rpc-types-compat/Cargo.toml b/crates/rpc/rpc-types-compat/Cargo.toml index 887986ada..d39443561 100644 --- a/crates/rpc/rpc-types-compat/Cargo.toml +++ b/crates/rpc/rpc-types-compat/Cargo.toml @@ -14,10 +14,8 @@ workspace = true [dependencies] # reth reth-primitives.workspace = true -reth-trie-common.workspace = true # ethereum -alloy-serde.workspace = true alloy-eips.workspace = true alloy-primitives.workspace = true alloy-rlp.workspace = true diff --git a/crates/rpc/rpc-types-compat/src/lib.rs b/crates/rpc/rpc-types-compat/src/lib.rs index c324eebc8..206d502f8 100644 --- a/crates/rpc/rpc-types-compat/src/lib.rs +++ b/crates/rpc/rpc-types-compat/src/lib.rs @@ -12,7 +12,5 @@ pub mod block; pub mod engine; -pub mod proof; pub mod transaction; - pub use transaction::TransactionCompat; diff --git a/crates/rpc/rpc-types-compat/src/proof.rs b/crates/rpc/rpc-types-compat/src/proof.rs deleted file mode 100644 index b860bc349..000000000 --- a/crates/rpc/rpc-types-compat/src/proof.rs +++ /dev/null @@ -1,37 +0,0 @@ -//! Compatibility functions for rpc proof related types. - -use alloy_rpc_types_eth::{EIP1186AccountProofResponse, EIP1186StorageProof}; -use alloy_serde::JsonStorageKey; -use reth_trie_common::{AccountProof, StorageProof}; - -/// Creates a new rpc storage proof from a primitive storage proof type. -pub fn from_primitive_storage_proof( - proof: StorageProof, - slot: JsonStorageKey, -) -> EIP1186StorageProof { - EIP1186StorageProof { key: slot, value: proof.value, proof: proof.proof } -} - -/// Creates a new rpc account proof from a primitive account proof type. -pub fn from_primitive_account_proof( - proof: AccountProof, - slots: Vec, -) -> EIP1186AccountProofResponse { - let info = proof.info.unwrap_or_default(); - EIP1186AccountProofResponse { - address: proof.address, - balance: info.balance, - code_hash: info.get_bytecode_hash(), - nonce: info.nonce, - storage_hash: proof.storage_root, - account_proof: proof.proof, - storage_proof: proof - .storage_proofs - .into_iter() - .filter_map(|proof| { - let input_slot = slots.iter().find(|s| s.as_b256() == proof.key)?; - Some(from_primitive_storage_proof(proof, *input_slot)) - }) - .collect(), - } -} diff --git a/crates/trie/common/Cargo.toml b/crates/trie/common/Cargo.toml index 8b0d930b0..9f81d020e 100644 --- a/crates/trie/common/Cargo.toml +++ b/crates/trie/common/Cargo.toml @@ -17,12 +17,14 @@ alloy-primitives.workspace = true alloy-rlp = { workspace = true, features = ["arrayvec"] } alloy-trie.workspace = true alloy-consensus.workspace = true -alloy-genesis.workspace = true - reth-primitives-traits.workspace = true reth-codecs.workspace = true revm-primitives.workspace = true +alloy-genesis.workspace = true +alloy-rpc-types-eth = { workspace = true, optional = true } +alloy-serde = { workspace = true, optional = true } + bytes.workspace = true derive_more.workspace = true itertools.workspace = true @@ -54,6 +56,10 @@ serde_json.workspace = true serde_with.workspace = true [features] +eip1186 = [ + "dep:alloy-rpc-types-eth", + "dep:alloy-serde", +] serde = [ "dep:serde", "bytes/serde", @@ -61,6 +67,7 @@ serde = [ "alloy-primitives/serde", "alloy-consensus/serde", "alloy-trie/serde", + "alloy-rpc-types-eth/serde", "revm-primitives/serde", "reth-primitives-traits/serde", "reth-codecs/serde" @@ -79,14 +86,16 @@ test-utils = [ "reth-codecs/test-utils", ] arbitrary = [ - "alloy-trie/arbitrary", - "dep:arbitrary", - "reth-primitives-traits/arbitrary", - "alloy-consensus/arbitrary", - "alloy-primitives/arbitrary", - "nybbles/arbitrary", - "revm-primitives/arbitrary", - "reth-codecs/arbitrary", + "alloy-trie/arbitrary", + "dep:arbitrary", + "alloy-serde/arbitrary", + "reth-primitives-traits/arbitrary", + "alloy-consensus/arbitrary", + "alloy-primitives/arbitrary", + "nybbles/arbitrary", + "revm-primitives/arbitrary", + "reth-codecs/arbitrary", + "alloy-rpc-types-eth?/arbitrary" ] [[bench]] diff --git a/crates/trie/common/src/proofs.rs b/crates/trie/common/src/proofs.rs index 78659116c..517f9fb7c 100644 --- a/crates/trie/common/src/proofs.rs +++ b/crates/trie/common/src/proofs.rs @@ -190,6 +190,33 @@ pub struct AccountProof { pub storage_proofs: Vec, } +#[cfg(feature = "eip1186")] +impl AccountProof { + /// Convert into an EIP-1186 account proof response + pub fn into_eip1186_response( + self, + slots: Vec, + ) -> alloy_rpc_types_eth::EIP1186AccountProofResponse { + let info = self.info.unwrap_or_default(); + alloy_rpc_types_eth::EIP1186AccountProofResponse { + address: self.address, + balance: info.balance, + code_hash: info.get_bytecode_hash(), + nonce: info.nonce, + storage_hash: self.storage_root, + account_proof: self.proof, + storage_proof: self + .storage_proofs + .into_iter() + .filter_map(|proof| { + let input_slot = slots.iter().find(|s| s.as_b256() == proof.key)?; + Some(proof.into_eip1186_proof(*input_slot)) + }) + .collect(), + } + } +} + impl Default for AccountProof { fn default() -> Self { Self::new(Address::default()) @@ -244,6 +271,17 @@ pub struct StorageProof { pub proof: Vec, } +impl StorageProof { + /// Convert into an EIP-1186 storage proof + #[cfg(feature = "eip1186")] + pub fn into_eip1186_proof( + self, + slot: alloy_serde::JsonStorageKey, + ) -> alloy_rpc_types_eth::EIP1186StorageProof { + alloy_rpc_types_eth::EIP1186StorageProof { key: slot, value: self.value, proof: self.proof } + } +} + impl StorageProof { /// Create new storage proof from the storage slot. pub fn new(key: B256) -> Self {