From e9ddf5d451bf7b95ffb166b53a841b72c896b287 Mon Sep 17 00:00:00 2001 From: Renato Dinhani <101204870+dinhani-cw@users.noreply.github.com> Date: Wed, 24 Jul 2024 11:36:14 -0300 Subject: [PATCH] refactor: common aliases in alias.rs (#1535) --- src/alias.rs | 35 +++++++++++++++++++ src/eth/executor/evm.rs | 4 +-- src/eth/primitives/account.rs | 4 +-- src/eth/primitives/address.rs | 4 +-- src/eth/primitives/block.rs | 17 ++++----- src/eth/primitives/block_header.rs | 6 ++-- src/eth/primitives/block_number.rs | 2 +- src/eth/primitives/bytes.rs | 8 ++--- src/eth/primitives/external_block.rs | 14 ++++---- src/eth/primitives/external_receipt.rs | 2 +- src/eth/primitives/external_transaction.rs | 3 +- src/eth/primitives/log.rs | 5 ++- src/eth/primitives/log_mined.rs | 2 +- src/eth/primitives/log_topic.rs | 2 +- src/eth/primitives/slot_index.rs | 2 +- src/eth/primitives/slot_value.rs | 2 +- src/eth/primitives/transaction_input.rs | 2 +- src/eth/primitives/transaction_mined.rs | 4 +-- src/eth/primitives/transaction_stage.rs | 5 ++- src/eth/primitives/unix_time.rs | 3 +- src/eth/primitives/wei.rs | 2 +- .../blockchain_client/blockchain_client.rs | 10 +++--- .../blockchain_client/pending_transaction.rs | 4 +-- src/lib.rs | 1 + 24 files changed, 90 insertions(+), 53 deletions(-) create mode 100644 src/alias.rs diff --git a/src/alias.rs b/src/alias.rs new file mode 100644 index 000000000..fbff0b3de --- /dev/null +++ b/src/alias.rs @@ -0,0 +1,35 @@ +//! Type aliases for external crates types that conflict with our own types or are too verbose. + +use ethereum_types::H256; + +use crate::eth::primitives::ExternalTransaction; + +// ----------------------------------------------------------------------------- +// Serde +// ----------------------------------------------------------------------------- +pub type JsonValue = serde_json::Value; + +// ----------------------------------------------------------------------------- +// Ethers +// ----------------------------------------------------------------------------- +pub type EthersBlockVoid = ethers_core::types::Block<()>; +pub type EthersBlockEthersTransaction = ethers_core::types::Block; +pub type EthersBlockExternalTransaction = ethers_core::types::Block; +pub type EthersBlockH256 = ethers_core::types::Block; +pub type EthersBytes = ethers_core::types::Bytes; +pub type EthersLog = ethers_core::types::Log; +pub type EthersReceipt = ethers_core::types::TransactionReceipt; +pub type EthersTransaction = ethers_core::types::Transaction; + +// ----------------------------------------------------------------------------- +// REVM +// ----------------------------------------------------------------------------- +pub type RevmAccountInfo = revm::primitives::AccountInfo; +pub type RevmAddress = revm::primitives::Address; +pub type RevmB256 = revm::primitives::B256; +pub type RevmBytecode = revm::primitives::Bytecode; +pub type RevmBytes = revm::primitives::Bytes; +pub type RevmLog = revm::primitives::Log; +pub type RevmOutput = revm::primitives::Output; +pub type RevmState = revm::primitives::State; +pub type RevmU256 = revm::primitives::U256; diff --git a/src/eth/executor/evm.rs b/src/eth/executor/evm.rs index 2412303b2..a5f062fc3 100644 --- a/src/eth/executor/evm.rs +++ b/src/eth/executor/evm.rs @@ -4,8 +4,6 @@ use std::sync::Arc; use anyhow::anyhow; use itertools::Itertools; use revm::primitives::AccountInfo; -use revm::primitives::Address as RevmAddress; -use revm::primitives::Bytecode as RevmBytecode; use revm::primitives::EVMError; use revm::primitives::ExecutionResult as RevmExecutionResult; use revm::primitives::InvalidTransaction; @@ -19,6 +17,8 @@ use revm::Database; use revm::Evm as RevmEvm; use revm::Handler; +use crate::alias::RevmAddress; +use crate::alias::RevmBytecode; use crate::eth::executor::EvmExecutionResult; use crate::eth::executor::EvmInput; use crate::eth::primitives::Account; diff --git a/src/eth/primitives/account.rs b/src/eth/primitives/account.rs index 571aab732..c289a8b01 100644 --- a/src/eth/primitives/account.rs +++ b/src/eth/primitives/account.rs @@ -1,7 +1,7 @@ use display_json::DebugAsJson; -use revm::primitives::AccountInfo as RevmAccountInfo; -use revm::primitives::Address as RevmAddress; +use crate::alias::RevmAccountInfo; +use crate::alias::RevmAddress; use crate::eth::primitives::Address; use crate::eth::primitives::Bytes; use crate::eth::primitives::CodeHash; diff --git a/src/eth/primitives/address.rs b/src/eth/primitives/address.rs index 9ebc6e22f..670d70b78 100644 --- a/src/eth/primitives/address.rs +++ b/src/eth/primitives/address.rs @@ -9,7 +9,6 @@ use ethers_core::types::NameOrAddress; use fake::Dummy; use fake::Faker; use hex_literal::hex; -use revm::primitives::Address as RevmAddress; use sqlx::database::HasArguments; use sqlx::database::HasValueRef; use sqlx::encode::IsNull; @@ -17,6 +16,7 @@ use sqlx::error::BoxDynError; use sqlx::postgres::PgHasArrayType; use sqlx::Decode; +use crate::alias::RevmAddress; use crate::gen_newtype_from; /// Address of an Ethereum account (wallet or contract). @@ -162,7 +162,7 @@ impl From
for H160 { impl From
for RevmAddress { fn from(value: Address) -> Self { - RevmAddress(value.0 .0.into()) + revm::primitives::Address(value.0 .0.into()) } } diff --git a/src/eth/primitives/block.rs b/src/eth/primitives/block.rs index 819b57572..95b5b9af2 100644 --- a/src/eth/primitives/block.rs +++ b/src/eth/primitives/block.rs @@ -1,13 +1,14 @@ use std::collections::HashMap; use ethereum_types::H256; -use ethers_core::types::Block as EthersBlock; -use ethers_core::types::Transaction as EthersTransaction; use itertools::Itertools; use serde::Deserialize; use super::LogMined; use super::TransactionInput; +use crate::alias::EthersBlockEthersTransaction; +use crate::alias::EthersBlockH256; +use crate::alias::EthersTransaction; use crate::eth::executor::EvmExecutionResult; use crate::eth::primitives::Address; use crate::eth::primitives::BlockHeader; @@ -104,13 +105,13 @@ impl Block { /// Serializes itself to JSON-RPC block format with full transactions included. pub fn to_json_rpc_with_full_transactions(self) -> JsonValue { - let ethers_block: EthersBlock = self.into(); + let ethers_block: EthersBlockEthersTransaction = self.into(); to_json_value(ethers_block) } /// Serializes itself to JSON-RPC block format with only transactions hashes included. pub fn to_json_rpc_with_transactions_hashes(self) -> JsonValue { - let ethers_block: EthersBlock = self.into(); + let ethers_block: EthersBlockH256 = self.into(); to_json_value(ethers_block) } @@ -161,9 +162,9 @@ impl Block { // ----------------------------------------------------------------------------- // Conversions: Self -> Other // ----------------------------------------------------------------------------- -impl From for EthersBlock { +impl From for EthersBlockEthersTransaction { fn from(block: Block) -> Self { - let ethers_block = EthersBlock::::from(block.header.clone()); + let ethers_block = EthersBlockEthersTransaction::from(block.header.clone()); let ethers_block_transactions: Vec = block.transactions.clone().into_iter().map_into().collect(); Self { transactions: ethers_block_transactions, @@ -172,9 +173,9 @@ impl From for EthersBlock { } } -impl From for EthersBlock { +impl From for EthersBlockH256 { fn from(block: Block) -> Self { - let ethers_block = EthersBlock::::from(block.header); + let ethers_block = EthersBlockH256::from(block.header); let ethers_block_transactions: Vec = block.transactions.into_iter().map(|x| x.input.hash).map_into().collect(); Self { transactions: ethers_block_transactions, diff --git a/src/eth/primitives/block_header.rs b/src/eth/primitives/block_header.rs index 5ea5e2a08..033360afb 100644 --- a/src/eth/primitives/block_header.rs +++ b/src/eth/primitives/block_header.rs @@ -10,6 +10,7 @@ use fake::Faker; use hex_literal::hex; use jsonrpsee::SubscriptionMessage; +use crate::alias::EthersBlockVoid; use crate::eth::consensus::append_entry; use crate::eth::primitives::logs_bloom::LogsBloom; use crate::eth::primitives::Address; @@ -22,6 +23,7 @@ use crate::eth::primitives::Hash; use crate::eth::primitives::MinerNonce; use crate::eth::primitives::Size; use crate::eth::primitives::UnixTime; +use crate::ext::ResultExt; /// Special hash used in block mining to indicate no uncle blocks. const HASH_EMPTY_UNCLES: Hash = Hash::new(hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")); @@ -232,7 +234,7 @@ impl TryFrom<&ExternalBlock> for BlockHeader { impl From for SubscriptionMessage { fn from(value: BlockHeader) -> Self { - let ethers_block: EthersBlock<()> = EthersBlock::from(value); - Self::from_json(ðers_block).unwrap() + let ethers_block = EthersBlockVoid::from(value); + Self::from_json(ðers_block).expect_infallible() } } diff --git a/src/eth/primitives/block_number.rs b/src/eth/primitives/block_number.rs index dafd2d6ce..2d534cdd7 100644 --- a/src/eth/primitives/block_number.rs +++ b/src/eth/primitives/block_number.rs @@ -9,7 +9,6 @@ use ethereum_types::U64; use ethers_core::utils::keccak256; use fake::Dummy; use fake::Faker; -use revm::primitives::U256 as RevmU256; use sqlx::database::HasArguments; use sqlx::database::HasValueRef; use sqlx::encode::IsNull; @@ -17,6 +16,7 @@ use sqlx::error::BoxDynError; use sqlx::postgres::PgHasArrayType; use sqlx::types::BigDecimal; +use crate::alias::RevmU256; use crate::eth::primitives::Hash; use crate::gen_newtype_from; diff --git a/src/eth/primitives/bytes.rs b/src/eth/primitives/bytes.rs index 20c7e125f..8c78f4147 100644 --- a/src/eth/primitives/bytes.rs +++ b/src/eth/primitives/bytes.rs @@ -3,12 +3,12 @@ use std::fmt::Display; use std::ops::Deref; use std::ops::DerefMut; -use ethers_core::types::Bytes as EthersBytes; use revm::interpreter::analysis::to_analysed; -use revm::primitives::Bytecode as RevmBytecode; -use revm::primitives::Bytes as RevmBytes; -use revm::primitives::Output as RevmOutput; +use crate::alias::EthersBytes; +use crate::alias::RevmBytecode; +use crate::alias::RevmBytes; +use crate::alias::RevmOutput; use crate::gen_newtype_from; #[derive(Clone, Default, Eq, PartialEq, fake::Dummy)] diff --git a/src/eth/primitives/external_block.rs b/src/eth/primitives/external_block.rs index 3ab2ac7d3..096648e6f 100644 --- a/src/eth/primitives/external_block.rs +++ b/src/eth/primitives/external_block.rs @@ -1,7 +1,7 @@ -use ethers_core::types::Block as EthersBlock; -use ethers_core::types::Transaction as EthersTransaction; use serde::Deserialize; +use crate::alias::EthersBlockEthersTransaction; +use crate::alias::EthersBlockExternalTransaction; use crate::eth::primitives::Address; use crate::eth::primitives::Block; use crate::eth::primitives::BlockNumber; @@ -14,7 +14,7 @@ use crate::log_and_err; #[derive(Debug, Clone, derive_more:: Deref, serde::Deserialize, serde::Serialize)] #[serde(transparent)] -pub struct ExternalBlock(#[deref] pub EthersBlock); +pub struct ExternalBlock(#[deref] pub EthersBlockExternalTransaction); impl ExternalBlock { /// Returns the block hash. @@ -47,7 +47,7 @@ impl ExternalBlock { // Conversions: Self -> Other // ----------------------------------------------------------------------------- -impl From for EthersBlock { +impl From for EthersBlockExternalTransaction { fn from(value: ExternalBlock) -> Self { value.0 } @@ -77,12 +77,12 @@ impl TryFrom for ExternalBlock { } } -impl From> for ExternalBlock { - fn from(value: EthersBlock) -> Self { +impl From for ExternalBlock { + fn from(value: EthersBlockEthersTransaction) -> Self { let txs: Vec = value.transactions.into_iter().map(ExternalTransaction::from).collect(); // Is there a better way to do this? - let block = EthersBlock { + let block = EthersBlockExternalTransaction { transactions: txs, hash: value.hash, parent_hash: value.parent_hash, diff --git a/src/eth/primitives/external_receipt.rs b/src/eth/primitives/external_receipt.rs index ae28c2fe3..b6df96f68 100644 --- a/src/eth/primitives/external_receipt.rs +++ b/src/eth/primitives/external_receipt.rs @@ -1,7 +1,7 @@ use ethereum_types::U256; -use ethers_core::types::TransactionReceipt as EthersReceipt; use serde::Deserialize; +use crate::alias::EthersReceipt; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::Hash; use crate::eth::primitives::Wei; diff --git a/src/eth/primitives/external_transaction.rs b/src/eth/primitives/external_transaction.rs index 0f998921a..507ab115a 100644 --- a/src/eth/primitives/external_transaction.rs +++ b/src/eth/primitives/external_transaction.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; -use ethers_core::types::Transaction as EthersTransaction; - +use crate::alias::EthersTransaction; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::Hash; use crate::eth::primitives::Signature; diff --git a/src/eth/primitives/log.rs b/src/eth/primitives/log.rs index 39f7676cc..be121b958 100644 --- a/src/eth/primitives/log.rs +++ b/src/eth/primitives/log.rs @@ -1,6 +1,5 @@ -use ethers_core::types::Log as EthersLog; -use revm::primitives::Log as RevmLog; - +use crate::alias::EthersLog; +use crate::alias::RevmLog; use crate::eth::primitives::Address; use crate::eth::primitives::Bytes; use crate::eth::primitives::LogTopic; diff --git a/src/eth/primitives/log_mined.rs b/src/eth/primitives/log_mined.rs index f822b9031..a8199d8ee 100644 --- a/src/eth/primitives/log_mined.rs +++ b/src/eth/primitives/log_mined.rs @@ -1,7 +1,7 @@ -use ethers_core::types::Log as EthersLog; use itertools::Itertools; use jsonrpsee::SubscriptionMessage; +use crate::alias::EthersLog; use crate::eth::primitives::Address; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::Hash; diff --git a/src/eth/primitives/log_topic.rs b/src/eth/primitives/log_topic.rs index 8e9ce288c..041e94785 100644 --- a/src/eth/primitives/log_topic.rs +++ b/src/eth/primitives/log_topic.rs @@ -3,8 +3,8 @@ use std::fmt::Display; use ethereum_types::H256; use fake::Dummy; use fake::Faker; -use revm::primitives::B256 as RevmB256; +use crate::alias::RevmB256; use crate::gen_newtype_from; /// Topic is part of a [`Log`](super::Log) emitted by the EVM during contract execution. diff --git a/src/eth/primitives/slot_index.rs b/src/eth/primitives/slot_index.rs index b9d5d4f08..64b55828d 100644 --- a/src/eth/primitives/slot_index.rs +++ b/src/eth/primitives/slot_index.rs @@ -7,7 +7,6 @@ use ethereum_types::U256; use ethers_core::utils::keccak256; use fake::Dummy; use fake::Faker; -use revm::primitives::U256 as RevmU256; use sqlx::database::HasArguments; use sqlx::database::HasValueRef; use sqlx::encode::IsNull; @@ -15,6 +14,7 @@ use sqlx::error::BoxDynError; use sqlx::postgres::PgHasArrayType; use sqlx::Decode; +use crate::alias::RevmU256; use crate::gen_newtype_from; #[derive(Clone, Copy, Default, Hash, Eq, PartialEq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] diff --git a/src/eth/primitives/slot_value.rs b/src/eth/primitives/slot_value.rs index c636d261f..aa035326b 100644 --- a/src/eth/primitives/slot_value.rs +++ b/src/eth/primitives/slot_value.rs @@ -4,7 +4,6 @@ use std::str::FromStr; use ethereum_types::U256; use fake::Dummy; use fake::Faker; -use revm::primitives::U256 as RevmU256; use sqlx::database::HasArguments; use sqlx::database::HasValueRef; use sqlx::encode::IsNull; @@ -12,6 +11,7 @@ use sqlx::error::BoxDynError; use sqlx::postgres::PgHasArrayType; use sqlx::Decode; +use crate::alias::RevmU256; use crate::gen_newtype_from; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] diff --git a/src/eth/primitives/transaction_input.rs b/src/eth/primitives/transaction_input.rs index fee6d0cc6..e7619bfd4 100644 --- a/src/eth/primitives/transaction_input.rs +++ b/src/eth/primitives/transaction_input.rs @@ -5,7 +5,6 @@ use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; use ethers_core::types::NameOrAddress; -use ethers_core::types::Transaction as EthersTransaction; use ethers_core::types::TransactionRequest; use fake::Dummy; use fake::Fake; @@ -13,6 +12,7 @@ use fake::Faker; use rlp::Decodable; use serde::Deserialize; +use crate::alias::EthersTransaction; use crate::eth::primitives::Address; use crate::eth::primitives::Bytes; use crate::eth::primitives::ChainId; diff --git a/src/eth/primitives/transaction_mined.rs b/src/eth/primitives/transaction_mined.rs index 20d1a7037..8a4e23a86 100644 --- a/src/eth/primitives/transaction_mined.rs +++ b/src/eth/primitives/transaction_mined.rs @@ -1,9 +1,9 @@ use std::hash::Hash as HashTrait; -use ethers_core::types::Transaction as EthersTransaction; -use ethers_core::types::TransactionReceipt as EthersReceipt; use itertools::Itertools; +use crate::alias::EthersReceipt; +use crate::alias::EthersTransaction; use crate::eth::primitives::logs_bloom::LogsBloom; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::EvmExecution; diff --git a/src/eth/primitives/transaction_stage.rs b/src/eth/primitives/transaction_stage.rs index 42ab8b76a..b83bd3fb9 100644 --- a/src/eth/primitives/transaction_stage.rs +++ b/src/eth/primitives/transaction_stage.rs @@ -1,6 +1,5 @@ -use ethers_core::types::Transaction as EthersTransaction; -use ethers_core::types::TransactionReceipt as EthersReceipt; - +use crate::alias::EthersReceipt; +use crate::alias::EthersTransaction; use crate::eth::primitives::TransactionExecution; use crate::eth::primitives::TransactionMined; use crate::ext::to_json_value; diff --git a/src/eth/primitives/unix_time.rs b/src/eth/primitives/unix_time.rs index 5bb2c445b..8858a65bb 100644 --- a/src/eth/primitives/unix_time.rs +++ b/src/eth/primitives/unix_time.rs @@ -6,7 +6,8 @@ use chrono::Utc; use ethereum_types::U256; use fake::Dummy; use fake::Faker; -use revm::primitives::U256 as RevmU256; + +use crate::alias::RevmU256; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct UnixTime(u64); diff --git a/src/eth/primitives/wei.rs b/src/eth/primitives/wei.rs index bde2c4d79..7bdc6d287 100644 --- a/src/eth/primitives/wei.rs +++ b/src/eth/primitives/wei.rs @@ -4,7 +4,6 @@ use ethabi::Token; use ethereum_types::U256; use fake::Dummy; use fake::Faker; -use revm::primitives::U256 as RevmU256; use sqlx::database::HasArguments; use sqlx::database::HasValueRef; use sqlx::encode::IsNull; @@ -13,6 +12,7 @@ use sqlx::postgres::PgHasArrayType; use sqlx::types::BigDecimal; use sqlx::Decode; +use crate::alias::RevmU256; use crate::gen_newtype_from; /// Native token amount in wei. diff --git a/src/infra/blockchain_client/blockchain_client.rs b/src/infra/blockchain_client/blockchain_client.rs index 707c7e9a5..c63b26dd1 100644 --- a/src/infra/blockchain_client/blockchain_client.rs +++ b/src/infra/blockchain_client/blockchain_client.rs @@ -1,8 +1,6 @@ use std::time::Duration; use anyhow::Context; -use ethers_core::types::Bytes; -use ethers_core::types::Transaction; use jsonrpsee::core::client::ClientT; use jsonrpsee::core::client::Subscription; use jsonrpsee::core::client::SubscriptionClientT; @@ -15,6 +13,8 @@ use tokio::sync::RwLock; use tokio::sync::RwLockReadGuard; use super::pending_transaction::PendingTransaction; +use crate::alias::EthersBytes; +use crate::alias::EthersTransaction; use crate::eth::primitives::Address; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::ExternalBlock; @@ -159,14 +159,14 @@ impl BlockchainClient { } /// Fetches a transaction by hash. - pub async fn fetch_transaction(&self, tx_hash: Hash) -> anyhow::Result> { + pub async fn fetch_transaction(&self, tx_hash: Hash) -> anyhow::Result> { tracing::debug!(%tx_hash, "fetching transaction"); let hash = to_json_value(tx_hash); let result = self .http - .request::, Vec>("eth_getTransactionByHash", vec![hash]) + .request::, Vec>("eth_getTransactionByHash", vec![hash]) .await; match result { @@ -210,7 +210,7 @@ impl BlockchainClient { // ------------------------------------------------------------------------- /// Sends a signed transaction. - pub async fn send_raw_transaction(&self, tx: Bytes) -> anyhow::Result> { + pub async fn send_raw_transaction(&self, tx: EthersBytes) -> anyhow::Result> { tracing::debug!("sending raw transaction"); let tx = to_json_value(tx); diff --git a/src/infra/blockchain_client/pending_transaction.rs b/src/infra/blockchain_client/pending_transaction.rs index 68313c664..8baf8ecdc 100644 --- a/src/infra/blockchain_client/pending_transaction.rs +++ b/src/infra/blockchain_client/pending_transaction.rs @@ -4,7 +4,6 @@ use std::task::Context; use std::task::Poll; use std::time::Duration; -use ethers_core::types::Transaction; use futures::Future; use futures::Stream; use futures_timer::Delay; @@ -14,6 +13,7 @@ use futures_util::StreamExt; use pin_project::pin_project; use super::BlockchainClient; +use crate::alias::EthersTransaction; use crate::eth::primitives::ExternalReceipt; use crate::eth::primitives::Hash; @@ -32,7 +32,7 @@ enum PendingTxState<'a> { PausedGettingTx, /// Polling The blockchain to see if the Tx has confirmed or dropped - GettingTx(PinBoxFut<'a, Option>), + GettingTx(PinBoxFut<'a, Option>), /// Waiting for interval to elapse before calling API again PausedGettingReceipt, diff --git a/src/lib.rs b/src/lib.rs index a4fe2d41c..1c8e1f352 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod alias; pub mod config; pub mod eth; pub mod ext;