Skip to content

Commit

Permalink
feat: use reth-ethereum-primitives (#13830)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Jan 17, 2025
1 parent 7e972ea commit 8efe441
Show file tree
Hide file tree
Showing 67 changed files with 941 additions and 3,025 deletions.
25 changes: 8 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/reth-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ min-info-logs = ["tracing/release_max_level_info"]
min-debug-logs = ["tracing/release_max_level_debug"]
min-trace-logs = ["tracing/release_max_level_trace"]

optimism = ["reth-primitives/optimism", "reth-node-core/optimism"]
optimism = ["reth-node-core/optimism"]

# no-op feature flag for switching between the `optimism` and default functionality in CI matrices
ethereum = []
Expand Down
11 changes: 7 additions & 4 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ use reth_execution_types::ExecutionOutcome;
use reth_fs_util as fs;
use reth_node_api::{BlockTy, EngineApiMessageVersion, PayloadBuilderAttributes};
use reth_node_ethereum::{consensus::EthBeaconConsensus, EthEvmConfig, EthExecutorProvider};
use reth_primitives::{EthPrimitives, SealedBlock, SealedHeader, Transaction, TransactionSigned};
use reth_primitives_traits::Block as _;
use reth_primitives::{
transaction::SignedTransactionIntoRecoveredExt, EthPrimitives, SealedBlock, SealedHeader,
Transaction, TransactionSigned,
};
use reth_primitives_traits::{Block as _, SignedTransaction};
use reth_provider::{
providers::{BlockchainProvider, ProviderNodeTypes},
BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider, ProviderFactory,
Expand Down Expand Up @@ -163,7 +166,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
for tx_bytes in &self.transactions {
debug!(target: "reth::cli", bytes = ?tx_bytes, "Decoding transaction");
let transaction = TransactionSigned::decode(&mut &Bytes::from_str(tx_bytes)?[..])?
.into_ecrecovered()
.try_ecrecovered()
.ok_or_else(|| eyre::eyre!("failed to recover tx"))?;

let encoded_length = match &transaction.transaction {
Expand All @@ -183,7 +186,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
let encoded_length = pooled.encode_2718_len();

// insert the blob into the store
blob_store.insert(transaction.hash(), sidecar)?;
blob_store.insert(*transaction.tx_hash(), sidecar)?;

encoded_length
}
Expand Down
7 changes: 5 additions & 2 deletions crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::{
in_memory::ExecutedBlock, CanonStateNotification, CanonStateNotifications,
CanonStateSubscriptions,
};
use alloy_consensus::{Header, Transaction as _, TxEip1559, EMPTY_ROOT_HASH};
use alloy_consensus::{
Header, SignableTransaction, Transaction as _, TxEip1559, TxReceipt, EMPTY_ROOT_HASH,
};
use alloy_eips::{
eip1559::{ETHEREUM_BLOCK_GAS_LIMIT, INITIAL_BASE_FEE},
eip7685::Requests,
Expand All @@ -17,6 +19,7 @@ use reth_chainspec::{ChainSpec, EthereumHardfork, MIN_TRANSACTION_GAS};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
proofs::{calculate_receipt_root, calculate_transaction_root, calculate_withdrawals_root},
transaction::SignedTransactionIntoRecoveredExt,
BlockBody, EthPrimitives, NodePrimitives, Receipt, Receipts, RecoveredBlock, RecoveredTx,
SealedBlock, SealedHeader, Transaction, TransactionSigned,
};
Expand Down Expand Up @@ -131,7 +134,7 @@ impl<N: NodePrimitives> TestBlockBuilder<N> {
cumulative_gas_used: (idx as u64 + 1) * MIN_TRANSACTION_GAS,
..Default::default()
}
.with_bloom()
.into_with_bloom()
})
.collect::<Vec<_>>();

Expand Down
1 change: 0 additions & 1 deletion crates/engine/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ tracing.workspace = true

[features]
optimism = [
"reth-primitives/optimism",
"reth-provider/optimism",
"revm-primitives/optimism",
]
8 changes: 4 additions & 4 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{
proofs, transaction::SignedTransactionIntoRecoveredExt, Block, BlockBody, Receipt, Receipts,
};
use reth_primitives_traits::block::Block as _;
use reth_primitives_traits::{block::Block as _, SignedTransaction};
use reth_provider::{BlockReader, ExecutionOutcome, ProviderError, StateProviderFactory};
use reth_revm::{
database::StateProviderDatabase,
Expand Down Expand Up @@ -329,19 +329,19 @@ where
let exec_result = match evm.transact() {
Ok(result) => result,
error @ Err(EVMError::Transaction(_) | EVMError::Header(_)) => {
trace!(target: "engine::stream::reorg", hash = %tx.hash(), ?error, "Error executing transaction from next block");
trace!(target: "engine::stream::reorg", hash = %tx.tx_hash(), ?error, "Error executing transaction from next block");
continue
}
// Treat error as fatal
Err(error) => {
return Err(RethError::Execution(BlockExecutionError::Validation(
BlockValidationError::EVM { hash: tx.hash(), error: Box::new(error) },
BlockValidationError::EVM { hash: *tx.tx_hash(), error: Box::new(error) },
)))
}
};
evm.db_mut().commit(exec_result.state);

if let Some(blob_tx) = tx.transaction.as_eip4844() {
if let Some(blob_tx) = tx.as_eip4844() {
sum_blob_gas_used += blob_tx.blob_gas();
versioned_hashes.extend(blob_tx.blob_versioned_hashes.clone());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use reth_evm::{
ConfigureEvm, TxEnvOverrides,
};
use reth_primitives::{EthPrimitives, Receipt, RecoveredBlock};
use reth_primitives_traits::BlockBody;
use reth_primitives_traits::{BlockBody, SignedTransaction};
use reth_revm::db::State;
use revm_primitives::{
db::{Database, DatabaseCommit},
Expand Down
15 changes: 7 additions & 8 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![allow(clippy::useless_let_if_seq)]

use alloy_consensus::{Header, EMPTY_OMMER_ROOT_HASH};
use alloy_consensus::{Header, Transaction, Typed2718, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::{
eip4844::MAX_DATA_GAS_PER_BLOCK, eip6110, eip7685::Requests, eip7840::BlobParams,
merge::BEACON_NONCE,
Expand All @@ -33,7 +33,7 @@ use reth_primitives::{
Block, BlockBody, EthereumHardforks, InvalidTransactionError, Receipt, RecoveredBlock,
TransactionSigned,
};
use reth_primitives_traits::Block as _;
use reth_primitives_traits::{Block as _, SignedTransaction};
use reth_revm::database::StateProviderDatabase;
use reth_storage_api::StateProviderFactory;
use reth_transaction_pool::{
Expand Down Expand Up @@ -250,7 +250,7 @@ where

// There's only limited amount of blob space available per block, so we need to check if
// the EIP-4844 can still fit in the block
if let Some(blob_tx) = tx.transaction.as_eip4844() {
if let Some(blob_tx) = tx.as_eip4844() {
let tx_blob_gas = blob_tx.blob_gas();
if sum_blob_gas_used + tx_blob_gas > MAX_DATA_GAS_PER_BLOCK {
// we can't fit this _blob_ transaction into the block, so we mark it as
Expand Down Expand Up @@ -306,7 +306,7 @@ where
evm.db_mut().commit(state);

// add to the total blob gas used if the transaction successfully executed
if let Some(blob_tx) = tx.transaction.as_eip4844() {
if let Some(blob_tx) = tx.as_eip4844() {
let tx_blob_gas = blob_tx.blob_gas();
sum_blob_gas_used += tx_blob_gas;

Expand All @@ -332,9 +332,8 @@ where
}));

// update add to total fees
let miner_fee = tx
.effective_tip_per_gas(Some(base_fee))
.expect("fee is always valid; execution succeeded");
let miner_fee =
tx.effective_tip_per_gas(base_fee).expect("fee is always valid; execution succeeded");
total_fees += U256::from(miner_fee) * U256::from(gas_used);

// append sender and transaction to the respective lists
Expand Down Expand Up @@ -419,7 +418,7 @@ where
// grab the blob sidecars from the executed txs
blob_sidecars = pool
.get_all_blobs_exact(
executed_txs.iter().filter(|tx| tx.is_eip4844()).map(|tx| tx.hash()).collect(),
executed_txs.iter().filter(|tx| tx.is_eip4844()).map(|tx| *tx.tx_hash()).collect(),
)
.map_err(PayloadBuilderError::other)?;

Expand Down
26 changes: 22 additions & 4 deletions crates/ethereum/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ reth-zstd-compressors = { workspace = true, optional = true }
# ethereum
alloy-eips.workspace = true
alloy-primitives.workspace = true
alloy-network = { workspace = true, optional = true }
alloy-consensus = { workspace = true, features = ["serde"] }
alloy-serde = { workspace = true, optional = true }
alloy-rlp.workspace = true
alloy-rpc-types = { workspace = true, optional = true }
revm-primitives.workspace = true

# misc
arbitrary = { workspace = true, optional = true, features = ["derive"] }
Expand All @@ -34,16 +38,20 @@ serde.workspace = true

[dev-dependencies]
arbitrary.workspace = true
bincode.workspace = true
proptest.workspace = true
proptest-arbitrary-interop.workspace = true
rand.workspace = true
reth-codecs.workspace = true
reth-codecs = { workspace = true, features = ["test-utils"] }
reth-testing-utils.workspace = true
reth-zstd-compressors.workspace = true
secp256k1.workspace = true
secp256k1 = { workspace = true, features = ["rand"] }
test-fuzz.workspace = true
alloy-consensus = { workspace = true, features = ["serde", "arbitrary"] }

[features]
default = ["std"]
alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types"]
std = [
"alloy-consensus/std",
"alloy-primitives/std",
Expand All @@ -54,7 +62,9 @@ std = [
"alloy-eips/std",
"derive_more/std",
"secp256k1?/std",
"once_cell/std"
"once_cell/std",
"revm-primitives/std",
"alloy-serde?/std"
]
reth-codec = [
"std",
Expand All @@ -70,5 +80,13 @@ arbitrary = [
"alloy-primitives/arbitrary",
"reth-codecs?/arbitrary",
"reth-primitives-traits/arbitrary",
"alloy-eips/arbitrary"
"alloy-eips/arbitrary",
"revm-primitives/arbitrary",
"alloy-rpc-types?/arbitrary",
"alloy-serde?/arbitrary"
]
serde-bincode-compat = [
"alloy-consensus/serde-bincode-compat",
"alloy-eips/serde-bincode-compat",
"reth-primitives-traits/serde-bincode-compat"
]
44 changes: 44 additions & 0 deletions crates/ethereum/primitives/src/alloy_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! Common conversions from alloy types.
use crate::{Transaction, TransactionSigned};
use alloc::string::ToString;
use alloy_consensus::TxEnvelope;
use alloy_network::{AnyRpcTransaction, AnyTxEnvelope};
use alloy_serde::WithOtherFields;

impl TryFrom<AnyRpcTransaction> for TransactionSigned {
type Error = alloy_rpc_types::ConversionError;

fn try_from(tx: AnyRpcTransaction) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let WithOtherFields { inner: tx, other: _ } = tx;

#[allow(unreachable_patterns)]
let (transaction, signature, hash) = match tx.inner {
AnyTxEnvelope::Ethereum(TxEnvelope::Legacy(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(Transaction::Legacy(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip2930(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(Transaction::Eip2930(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip1559(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(Transaction::Eip1559(tx), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip4844(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(Transaction::Eip4844(tx.into()), signature, hash)
}
AnyTxEnvelope::Ethereum(TxEnvelope::Eip7702(tx)) => {
let (tx, signature, hash) = tx.into_parts();
(Transaction::Eip7702(tx), signature, hash)
}
_ => return Err(ConversionError::Custom("unknown transaction type".to_string())),
};

Ok(Self { transaction, signature, hash: hash.into() })
}
}
3 changes: 3 additions & 0 deletions crates/ethereum/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ pub use receipt::*;

mod transaction;
pub use transaction::*;

#[cfg(feature = "alloy-compat")]
mod alloy_compat;
Loading

0 comments on commit 8efe441

Please sign in to comment.