feat: move eip1186 conversion helpers to reth-trie-common proofs (#12985)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Hoa Nguyen
2024-11-30 23:01:19 +07:00
committed by GitHub
parent 7353dc94a8
commit 9b1d676438
12 changed files with 75 additions and 74 deletions

5
Cargo.lock generated
View File

@ -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",

View File

@ -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

View File

@ -73,12 +73,11 @@ impl<P: NodePrimitives> From<CanonStateNotification<P>> for ExExNotification<P>
/// 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() {

View File

@ -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

View File

@ -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

View File

@ -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
})

View File

@ -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 _;

View File

@ -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

View File

@ -12,7 +12,5 @@
pub mod block;
pub mod engine;
pub mod proof;
pub mod transaction;
pub use transaction::TransactionCompat;

View File

@ -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<JsonStorageKey>,
) -> 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(),
}
}

View File

@ -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]]

View File

@ -190,6 +190,33 @@ pub struct AccountProof {
pub storage_proofs: Vec<StorageProof>,
}
#[cfg(feature = "eip1186")]
impl AccountProof {
/// Convert into an EIP-1186 account proof response
pub fn into_eip1186_response(
self,
slots: Vec<alloy_serde::JsonStorageKey>,
) -> 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<Bytes>,
}
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 {