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: remove async-trait #4590

Merged
merged 2 commits into from
Nov 17, 2023
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
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/edr_evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"

[dependencies]
async-rwlock = { version = "1.3.0", default-features = false }
async-trait = { version = "0.1.73", default-features = false }
auto_impl = { version = "1.0.1", default-features = false }
cita_trie = { git = "https://github.com/Wodann/cita-trie", rev = "60efef5", version = "4.0.0", default-features = false }
dyn-clone = { version = "1.0.13", default-features = false }
Expand Down
8 changes: 3 additions & 5 deletions crates/edr_evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod remote;

use std::{fmt::Debug, sync::Arc};

use async_trait::async_trait;
use auto_impl::auto_impl;
use edr_eth::{block, receipt::BlockReceipt, transaction::SignedTransaction, Address, B256};

Expand All @@ -15,7 +14,6 @@ pub use self::{
};

/// Trait for implementations of an Ethereum block.
#[async_trait]
#[auto_impl(Arc)]
pub trait Block: Debug {
/// The blockchain error type.
Expand All @@ -34,10 +32,10 @@ pub trait Block: Debug {
fn transaction_callers(&self) -> &[Address];

/// Returns the receipts of the block's transactions.
async fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error>;
fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error>;
}

/// Trait that meets all requirements for a synchronous block.
pub trait SyncBlock: Block + Send + Sync + 'static {}
pub trait SyncBlock: Block + Send + Sync {}

impl<BlockT> SyncBlock for BlockT where BlockT: Block + Send + Sync + 'static {}
impl<BlockT> SyncBlock for BlockT where BlockT: Block + Send + Sync {}
4 changes: 1 addition & 3 deletions crates/edr_evm/src/block/local.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use async_trait::async_trait;
use edr_eth::{
block::{self, Header, PartialHeader},
log::{FullBlockLog, Log, ReceiptLog},
Expand Down Expand Up @@ -94,7 +93,6 @@ impl LocalBlock {
}
}

#[async_trait]
impl Block for LocalBlock {
type Error = BlockchainError;

Expand All @@ -114,7 +112,7 @@ impl Block for LocalBlock {
&self.transaction_callers
}

async fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error> {
fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error> {
Ok(self.transaction_receipts.clone())
}
}
Expand Down
27 changes: 16 additions & 11 deletions crates/edr_evm/src/block/remote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::{Arc, OnceLock};

use async_trait::async_trait;
use edr_eth::{
block::{BlobGas, Header},
receipt::BlockReceipt,
Expand All @@ -12,6 +11,7 @@ use edr_eth::{
withdrawal::Withdrawal,
Address, B256, B64,
};
use tokio::runtime;

use crate::{blockchain::BlockchainError, Block, SyncBlock};

Expand Down Expand Up @@ -51,13 +51,15 @@ pub struct RemoteBlock {
hash: B256,
// The RPC client is needed to lazily fetch receipts
rpc_client: Arc<RpcClient>,
runtime: runtime::Handle,
}

impl RemoteBlock {
/// Constructs a new instance with the provided JSON-RPC block and client.
pub fn new(
block: eth::Block<eth::Transaction>,
rpc_client: Arc<RpcClient>,
runtime: runtime::Handle,
) -> Result<Self, CreationError> {
let header = Header {
parent_hash: block.parent_hash,
Expand Down Expand Up @@ -104,11 +106,11 @@ impl RemoteBlock {
_withdrawals: block.withdrawals,
hash,
rpc_client,
runtime,
})
}
}

#[async_trait]
impl Block for RemoteBlock {
type Error = BlockchainError;

Expand All @@ -128,19 +130,22 @@ impl Block for RemoteBlock {
&self.callers
}

async fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error> {
fn transaction_receipts(&self) -> Result<Vec<Arc<BlockReceipt>>, Self::Error> {
if let Some(receipts) = self.receipts.get() {
return Ok(receipts.clone());
}

let receipts: Vec<Arc<BlockReceipt>> = self
.rpc_client
.get_transaction_receipts(self.transactions.iter().map(SignedTransaction::hash))
.await?
.expect("All receipts of the block should exist")
.into_iter()
.map(Arc::new)
.collect();
let receipts: Vec<Arc<BlockReceipt>> = tokio::task::block_in_place(|| {
self.runtime.block_on(
self.rpc_client.get_transaction_receipts(
self.transactions.iter().map(SignedTransaction::hash),
),
)
})?
.expect("All receipts of the block should exist")
.into_iter()
.map(Arc::new)
.collect();

self.receipts
.set(receipts.clone())
Expand Down
42 changes: 17 additions & 25 deletions crates/edr_evm/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod storage;

use std::{fmt::Debug, sync::Arc};

use async_trait::async_trait;
use edr_eth::{
receipt::BlockReceipt, remote::RpcClientError, spec::HardforkActivations, B256, U256,
};
Expand Down Expand Up @@ -75,7 +74,6 @@ pub enum BlockchainError {
}

/// Trait for implementations of an Ethereum blockchain.
#[async_trait]
pub trait Blockchain {
/// The blockchain's error type
type BlockchainError;
Expand All @@ -84,86 +82,82 @@ pub trait Blockchain {
type StateError;

/// Retrieves the block with the provided hash, if it exists.
async fn block_by_hash(
#[allow(clippy::type_complexity)]
fn block_by_hash(
&self,
hash: &B256,
) -> Result<Option<Arc<dyn SyncBlock<Error = Self::BlockchainError>>>, Self::BlockchainError>;

/// Retrieves the block with the provided number, if it exists.
async fn block_by_number(
#[allow(clippy::type_complexity)]
fn block_by_number(
&self,
number: u64,
) -> Result<Option<Arc<dyn SyncBlock<Error = Self::BlockchainError>>>, Self::BlockchainError>;

/// Retrieves the block that contains a transaction with the provided hash,
/// if it exists.
async fn block_by_transaction_hash(
#[allow(clippy::type_complexity)]
fn block_by_transaction_hash(
&self,
transaction_hash: &B256,
) -> Result<Option<Arc<dyn SyncBlock<Error = Self::BlockchainError>>>, Self::BlockchainError>;

/// Retrieves the instances chain ID.
async fn chain_id(&self) -> u64;
fn chain_id(&self) -> u64;

/// Retrieves the last block in the blockchain.
async fn last_block(
fn last_block(
&self,
) -> Result<Arc<dyn SyncBlock<Error = Self::BlockchainError>>, Self::BlockchainError>;

/// Retrieves the last block number in the blockchain.
async fn last_block_number(&self) -> u64;
fn last_block_number(&self) -> u64;

/// Retrieves the receipt of the transaction with the provided hash, if it
/// exists.
async fn receipt_by_transaction_hash(
fn receipt_by_transaction_hash(
&self,
transaction_hash: &B256,
) -> Result<Option<Arc<BlockReceipt>>, Self::BlockchainError>;

/// Retrieves the hardfork specification of the block at the provided
/// number.
async fn spec_at_block_number(
&self,
block_number: u64,
) -> Result<SpecId, Self::BlockchainError>;
fn spec_at_block_number(&self, block_number: u64) -> Result<SpecId, Self::BlockchainError>;

/// Retrieves the hardfork specification used for new blocks.
fn spec_id(&self) -> SpecId;

/// Retrieves the state at a given block
async fn state_at_block_number(
fn state_at_block_number(
&self,
block_number: u64,
) -> Result<Box<dyn SyncState<Self::StateError>>, Self::BlockchainError>;

/// Retrieves the total difficulty at the block with the provided hash.
async fn total_difficulty_by_hash(
&self,
hash: &B256,
) -> Result<Option<U256>, Self::BlockchainError>;
fn total_difficulty_by_hash(&self, hash: &B256) -> Result<Option<U256>, Self::BlockchainError>;
}

/// Trait for implementations of a mutable Ethereum blockchain
#[async_trait]
pub trait BlockchainMut {
/// The blockchain's error type
type Error;

/// Inserts the provided block into the blockchain, returning a reference to
/// the inserted block.
async fn insert_block(
fn insert_block(
&mut self,
block: LocalBlock,
state_diff: StateDiff,
) -> Result<Arc<dyn SyncBlock<Error = Self::Error>>, Self::Error>;

/// Reserves the provided number of blocks, starting from the next block
/// number.
async fn reserve_blocks(&mut self, additional: u64, interval: u64) -> Result<(), Self::Error>;
fn reserve_blocks(&mut self, additional: u64, interval: u64) -> Result<(), Self::Error>;

/// Reverts to the block with the provided number, deleting all later
/// blocks.
async fn revert_to_block(&mut self, block_number: u64) -> Result<(), Self::Error>;
fn revert_to_block(&mut self, block_number: u64) -> Result<(), Self::Error>;
}

/// Trait that meets all requirements for a synchronous blockchain.
Expand All @@ -174,7 +168,6 @@ pub trait SyncBlockchain<BlockchainErrorT, StateErrorT>:
+ Send
+ Sync
+ Debug
+ 'static
where
BlockchainErrorT: Debug + Send,
{
Expand All @@ -188,8 +181,7 @@ where
+ BlockHashRef<Error = BlockchainErrorT>
+ Send
+ Sync
+ Debug
+ 'static,
+ Debug,
BlockchainErrorT: Debug + Send,
{
}
Expand Down
Loading
Loading