Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: common type aliases in alias.rs #1535

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/alias.rs
Original file line number Diff line number Diff line change
@@ -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<ethers_core::types::Transaction>;
pub type EthersBlockExternalTransaction = ethers_core::types::Block<ExternalTransaction>;
pub type EthersBlockH256 = ethers_core::types::Block<H256>;
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;
4 changes: 2 additions & 2 deletions src/eth/executor/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/eth/primitives/account.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/eth/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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;
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).
Expand Down Expand Up @@ -162,7 +162,7 @@ impl From<Address> for H160 {

impl From<Address> for RevmAddress {
fn from(value: Address) -> Self {
RevmAddress(value.0 .0.into())
revm::primitives::Address(value.0 .0.into())
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/eth/primitives/block.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<EthersTransaction> = 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<H256> = self.into();
let ethers_block: EthersBlockH256 = self.into();
to_json_value(ethers_block)
}

Expand Down Expand Up @@ -161,9 +162,9 @@ impl Block {
// -----------------------------------------------------------------------------
// Conversions: Self -> Other
// -----------------------------------------------------------------------------
impl From<Block> for EthersBlock<EthersTransaction> {
impl From<Block> for EthersBlockEthersTransaction {
fn from(block: Block) -> Self {
let ethers_block = EthersBlock::<EthersTransaction>::from(block.header.clone());
let ethers_block = EthersBlockEthersTransaction::from(block.header.clone());
let ethers_block_transactions: Vec<EthersTransaction> = block.transactions.clone().into_iter().map_into().collect();
Self {
transactions: ethers_block_transactions,
Expand All @@ -172,9 +173,9 @@ impl From<Block> for EthersBlock<EthersTransaction> {
}
}

impl From<Block> for EthersBlock<H256> {
impl From<Block> for EthersBlockH256 {
fn from(block: Block) -> Self {
let ethers_block = EthersBlock::<H256>::from(block.header);
let ethers_block = EthersBlockH256::from(block.header);
let ethers_block_transactions: Vec<H256> = block.transactions.into_iter().map(|x| x.input.hash).map_into().collect();
Self {
transactions: ethers_block_transactions,
Expand Down
6 changes: 4 additions & 2 deletions src/eth/primitives/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
Expand Down Expand Up @@ -232,7 +234,7 @@ impl TryFrom<&ExternalBlock> for BlockHeader {

impl From<BlockHeader> for SubscriptionMessage {
fn from(value: BlockHeader) -> Self {
let ethers_block: EthersBlock<()> = EthersBlock::from(value);
Self::from_json(&ethers_block).unwrap()
let ethers_block = EthersBlockVoid::from(value);
Self::from_json(&ethers_block).expect_infallible()
}
}
2 changes: 1 addition & 1 deletion src/eth/primitives/block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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;
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;

Expand Down
8 changes: 4 additions & 4 deletions src/eth/primitives/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
14 changes: 7 additions & 7 deletions src/eth/primitives/external_block.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ExternalTransaction>);
pub struct ExternalBlock(#[deref] pub EthersBlockExternalTransaction);

impl ExternalBlock {
/// Returns the block hash.
Expand Down Expand Up @@ -47,7 +47,7 @@ impl ExternalBlock {
// Conversions: Self -> Other
// -----------------------------------------------------------------------------

impl From<ExternalBlock> for EthersBlock<ExternalTransaction> {
impl From<ExternalBlock> for EthersBlockExternalTransaction {
fn from(value: ExternalBlock) -> Self {
value.0
}
Expand Down Expand Up @@ -77,12 +77,12 @@ impl TryFrom<JsonValue> for ExternalBlock {
}
}

impl From<EthersBlock<EthersTransaction>> for ExternalBlock {
fn from(value: EthersBlock<EthersTransaction>) -> Self {
impl From<EthersBlockEthersTransaction> for ExternalBlock {
fn from(value: EthersBlockEthersTransaction) -> Self {
let txs: Vec<ExternalTransaction> = 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,
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/external_receipt.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/eth/primitives/external_transaction.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/eth/primitives/log.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/log_mined.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/log_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/slot_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ 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;
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)]
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/slot_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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;
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)]
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/transaction_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ 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;
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;
Expand Down
4 changes: 2 additions & 2 deletions src/eth/primitives/transaction_mined.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/eth/primitives/transaction_stage.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/unix_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/wei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Loading