From f49feff5a735ec791ea4dcc641f0baa3a73b2516 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 29 Jun 2023 17:28:28 +0200 Subject: [PATCH] chore: phase out some ethers usage (#3467) --- crates/net/network-api/src/test_utils.rs | 4 +- crates/primitives/src/block.rs | 16 +--- crates/primitives/src/chain/mod.rs | 3 +- crates/primitives/src/header.rs | 77 ++++++++++--------- crates/primitives/src/lib.rs | 1 - .../rpc/rpc-types/src/eth/transaction/mod.rs | 6 +- crates/stages/src/stages/tx_lookup.rs | 4 +- 7 files changed, 50 insertions(+), 61 deletions(-) diff --git a/crates/net/network-api/src/test_utils.rs b/crates/net/network-api/src/test_utils.rs index 6b2a1c3768..f914138b92 100644 --- a/crates/net/network-api/src/test_utils.rs +++ b/crates/net/network-api/src/test_utils.rs @@ -3,7 +3,7 @@ use crate::{ }; use async_trait::async_trait; use reth_eth_wire::{DisconnectReason, ProtocolVersion}; -use reth_primitives::{rpc::Chain::Mainnet, NodeRecord, PeerId}; +use reth_primitives::{Chain, NodeRecord, PeerId}; use reth_rpc_types::{EthProtocolInfo, NetworkStatus}; use std::net::{IpAddr, SocketAddr}; @@ -33,7 +33,7 @@ impl NetworkInfo for NoopNetwork { } fn chain_id(&self) -> u64 { - Mainnet.into() + Chain::mainnet().into() } fn is_syncing(&self) -> bool { diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index 07741609c7..f5a2b10b20 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -1,7 +1,6 @@ use crate::{ - Address, BlockHash, BlockNumber, Header, SealedHeader, TransactionSigned, Withdrawal, H256, + Address, BlockHash, BlockNumber, Header, SealedHeader, TransactionSigned, Withdrawal, H256, U64, }; -use ethers_core::types::{BlockNumber as EthersBlockNumber, U64}; use fixed_hash::rustc_hex::FromHexError; use reth_codecs::derive_arbitrary; use reth_rlp::{Decodable, DecodeError, Encodable, RlpDecodable, RlpEncodable}; @@ -564,19 +563,6 @@ impl From for BlockNumberOrTag { } } -impl From for BlockNumberOrTag { - fn from(value: EthersBlockNumber) -> Self { - match value { - EthersBlockNumber::Latest => BlockNumberOrTag::Latest, - EthersBlockNumber::Finalized => BlockNumberOrTag::Finalized, - EthersBlockNumber::Safe => BlockNumberOrTag::Safe, - EthersBlockNumber::Earliest => BlockNumberOrTag::Earliest, - EthersBlockNumber::Pending => BlockNumberOrTag::Pending, - EthersBlockNumber::Number(num) => BlockNumberOrTag::Number(num.as_u64()), - } - } -} - impl From for BlockNumberOrTag { fn from(num: U64) -> Self { num.as_u64().into() diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index 8a5a5c7d33..43f4647a8c 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -1,8 +1,7 @@ use crate::{ net::{goerli_nodes, mainnet_nodes, sepolia_nodes}, - NodeRecord, U256, + NodeRecord, U256, U64, }; -use ethers_core::types::U64; use reth_codecs::add_arbitrary_tests; use reth_rlp::{Decodable, Encodable}; use serde::{Deserialize, Serialize}; diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index d8778de70c..5a87c6411b 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -2,10 +2,10 @@ use crate::{ basefee::calculate_next_block_base_fee, keccak256, proofs::{EMPTY_LIST_HASH, EMPTY_ROOT}, - BlockHash, BlockNumHash, BlockNumber, Bloom, Bytes, H160, H256, U256, + BlockHash, BlockNumHash, BlockNumber, Bloom, Bytes, H160, H256, H64, U256, }; use bytes::{Buf, BufMut, BytesMut}; -use ethers_core::types::{Block, H256 as EthersH256, H64}; + use reth_codecs::{add_arbitrary_tests, derive_arbitrary, main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, Encodable, EMPTY_STRING_CODE}; use serde::{Deserialize, Serialize}; @@ -343,40 +343,6 @@ impl<'a> arbitrary::Arbitrary<'a> for SealedHeader { } } -impl From<&Block> for Header { - fn from(block: &Block) -> Self { - Header { - parent_hash: block.parent_hash.0.into(), - number: block.number.unwrap().as_u64(), - gas_limit: block.gas_limit.as_u64(), - difficulty: block.difficulty.into(), - nonce: block.nonce.unwrap().to_low_u64_be(), - extra_data: block.extra_data.0.clone().into(), - state_root: block.state_root.0.into(), - transactions_root: block.transactions_root.0.into(), - receipts_root: block.receipts_root.0.into(), - timestamp: block.timestamp.as_u64(), - mix_hash: block.mix_hash.unwrap().0.into(), - beneficiary: block.author.unwrap().0.into(), - base_fee_per_gas: block.base_fee_per_gas.map(|fee| fee.as_u64()), - ommers_hash: block.uncles_hash.0.into(), - gas_used: block.gas_used.as_u64(), - withdrawals_root: None, - logs_bloom: block.logs_bloom.unwrap_or_default().0.into(), - } - } -} - -impl From<&Block> for SealedHeader { - fn from(block: &Block) -> Self { - let header = Header::from(block); - match block.hash { - Some(hash) => header.seal(hash.0.into()), - None => header.seal_slow(), - } - } -} - impl Default for SealedHeader { fn default() -> Self { Header::default().seal_slow() @@ -506,6 +472,45 @@ impl From for bool { } } +mod ethers_compat { + use super::*; + use ethers_core::types::{Block, H256 as EthersH256}; + + impl From<&Block> for Header { + fn from(block: &Block) -> Self { + Header { + parent_hash: block.parent_hash.0.into(), + number: block.number.unwrap().as_u64(), + gas_limit: block.gas_limit.as_u64(), + difficulty: block.difficulty.into(), + nonce: block.nonce.unwrap().to_low_u64_be(), + extra_data: block.extra_data.0.clone().into(), + state_root: block.state_root.0.into(), + transactions_root: block.transactions_root.0.into(), + receipts_root: block.receipts_root.0.into(), + timestamp: block.timestamp.as_u64(), + mix_hash: block.mix_hash.unwrap().0.into(), + beneficiary: block.author.unwrap().0.into(), + base_fee_per_gas: block.base_fee_per_gas.map(|fee| fee.as_u64()), + ommers_hash: block.uncles_hash.0.into(), + gas_used: block.gas_used.as_u64(), + withdrawals_root: None, + logs_bloom: block.logs_bloom.unwrap_or_default().0.into(), + } + } + } + + impl From<&Block> for SealedHeader { + fn from(block: &Block) -> Self { + let header = Header::from(block); + match block.hash { + Some(hash) => header.seal(hash.0.into()), + None => header.seal_slow(), + } + } + } +} + #[cfg(test)] mod tests { use super::{Bytes, Decodable, Encodable, Header, H256}; diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index ada92bd096..59c1262479 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -115,7 +115,6 @@ pub type StorageValue = U256; pub type Selector = [u8; 4]; pub use ethers_core::{ - types as rpc, types::{BigEndianHash, H128, H64, U64}, utils as rpc_utils, }; diff --git a/crates/rpc/rpc-types/src/eth/transaction/mod.rs b/crates/rpc/rpc-types/src/eth/transaction/mod.rs index 1fc2e3ca8d..c61d6080b9 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/mod.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/mod.rs @@ -11,9 +11,9 @@ pub use signature::Signature; pub use typed::*; use reth_primitives::{ - rpc::transaction::eip2930::AccessListItem, Address, BlockNumber, Bytes, - Transaction as PrimitiveTransaction, TransactionKind as PrimitiveTransactionKind, - TransactionSignedEcRecovered, TxType, H256, U128, U256, U64, + AccessListItem, Address, BlockNumber, Bytes, Transaction as PrimitiveTransaction, + TransactionKind as PrimitiveTransactionKind, TransactionSignedEcRecovered, TxType, H256, U128, + U256, U64, }; use serde::{Deserialize, Serialize}; diff --git a/crates/stages/src/stages/tx_lookup.rs b/crates/stages/src/stages/tx_lookup.rs index f572a7f27b..09e0e6d674 100644 --- a/crates/stages/src/stages/tx_lookup.rs +++ b/crates/stages/src/stages/tx_lookup.rs @@ -9,7 +9,7 @@ use reth_db::{ DatabaseError, }; use reth_primitives::{ - rpc_utils::keccak256, + keccak256, stage::{EntitiesCheckpoint, StageCheckpoint, StageId}, TransactionSignedNoHash, TxNumber, H256, }; @@ -178,7 +178,7 @@ fn calculate_hash( ) -> Result<(H256, TxNumber), Box> { let (tx_id, tx) = entry.map_err(|e| Box::new(e.into()))?; tx.transaction.encode_with_signature(&tx.signature, rlp_buf, false); - Ok((H256(keccak256(rlp_buf)), tx_id)) + Ok((keccak256(rlp_buf), tx_id)) } fn stage_checkpoint(