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: replace background with short-lived tasks #3842

Merged
merged 7 commits into from
Apr 20, 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: 1 addition & 1 deletion crates/rethnet_eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
open-fastrlp = { version = "0.1.2", default-features = false, features = ["derive"], optional = true }
primitive-types = { version = "0.11.1", default-features = false, features = ["rlp"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
revm-primitives = { git = "https://github.com/bluealloy/revm", rev = "3789509", version = "1.0", default-features = false }
revm-primitives = { git = "https://github.com/Wodann/revm", rev = "a4550cc", version = "1.1", default-features = false }
# revm-primitives = { path = "../../../revm/crates/primitives", version = "1.0", default-features = false }
rlp = { version = "0.5.2", default-features = false, features = ["derive"] }
ruint = { version = "1.7.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/rethnet_evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ hasher = { git = "https://github.com/Wodann/hasher", rev = "89d3fc9", version =
log = { version = "0.4.17", default-features = false }
parking_lot = { version = "0.12.1", default-features = false }
rethnet_eth = { version = "0.1.0-dev", path = "../rethnet_eth", features = ["serde"] }
revm = { git = "https://github.com/bluealloy/revm", rev = "3789509", version = "3.0", default-features = false, features = ["dev", "serde", "std"] }
revm = { git = "https://github.com/Wodann/revm", rev = "a4550cc", version = "3.1", default-features = false, features = ["dev", "secp256k1", "serde", "std"] }
# revm = { path = "../../../revm/crates/revm", version = "3.0", default-features = false, features = ["dev", "serde", "std"] }
rlp = { version = "0.5.2", default-features = false }
serde = { version = "1.0.158", default-features = false, features = ["std"] }
Expand Down
60 changes: 25 additions & 35 deletions crates/rethnet_evm/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use revm::{
db::DatabaseComponentError,
primitives::{BlockEnv, CfgEnv, EVMError, ExecutionResult, InvalidTransaction, SpecId, TxEnv},
};
use tokio::runtime::Runtime;
use tokio::sync::RwLock;

use crate::{
blockchain::AsyncBlockchain,
evm::{run_transaction, AsyncInspector},
state::{AccountModifierFn, AsyncState},
blockchain::SyncBlockchain,
evm::{build_evm, run_transaction, SyncInspector},
state::{AccountModifierFn, SyncState},
trace::Trace,
HeaderData,
};
Expand Down Expand Up @@ -51,8 +51,8 @@ where
BE: Debug + Send + 'static,
SE: Debug + Send + 'static,
{
blockchain: Arc<AsyncBlockchain<BE>>,
state: Arc<AsyncState<SE>>,
blockchain: Arc<RwLock<Box<dyn SyncBlockchain<BE>>>>,
state: Arc<RwLock<Box<dyn SyncState<SE>>>>,
header: PartialHeader,
transactions: Vec<TxEnv>,
cfg: CfgEnv,
Expand All @@ -65,8 +65,8 @@ where
{
/// Creates an intance of [`BlockBuilder`], creating a checkpoint in the process.
pub fn new(
blockchain: Arc<AsyncBlockchain<BE>>,
state: Arc<AsyncState<SE>>,
blockchain: Arc<RwLock<Box<dyn SyncBlockchain<BE>>>>,
state: Arc<RwLock<Box<dyn SyncState<SE>>>>,
cfg: CfgEnv,
parent: Header,
header: HeaderData,
Expand All @@ -91,11 +91,6 @@ where
}
}

/// Retrieves the runtime of the [`BlockBuilder`].
pub fn runtime(&self) -> &Runtime {
self.state.runtime()
}

/// Retrieves the amount of gas used in the block, so far.
pub fn gas_used(&self) -> U256 {
self.header.gas_used
Expand All @@ -118,7 +113,7 @@ where
pub async fn add_transaction(
&mut self,
transaction: TxEnv,
inspector: Option<Box<dyn AsyncInspector<BE, SE>>>,
inspector: Option<Box<dyn SyncInspector<BE, SE>>>,
) -> Result<(ExecutionResult, Trace), BlockTransactionError<BE, SE>> {
// transaction's gas limit cannot be greater than the remaining gas in the block
if U256::from(transaction.gas_limit) > self.gas_remaining() {
Expand All @@ -140,19 +135,14 @@ where
},
};

let (result, changes, trace) = run_transaction(
self.state.runtime(),
self.blockchain.clone(),
self.state.clone(),
self.cfg.clone(),
transaction,
block,
inspector,
)
.await
.unwrap()?;
let mut state = self.state.write().await;
let blockchain = self.blockchain.read().await;

let evm = build_evm(&*blockchain, &*state, self.cfg.clone(), transaction, block);

let (result, changes, trace) = run_transaction(evm, inspector)?;

self.state.apply(changes).await;
state.commit(changes);

self.header.gas_used += U256::from(result.gas_used());

Expand All @@ -163,22 +153,22 @@ where
/// Finalizes the block, returning the state root.
/// TODO: Build a full block
pub async fn finalize(self, rewards: Vec<(Address, U256)>) -> Result<(), SE> {
let mut state = self.state.write().await;
for (address, reward) in rewards {
self.state
.modify_account(
address,
AccountModifierFn::new(Box::new(move |balance, _nonce, _code| {
*balance += reward;
})),
)
.await?;
state.modify_account(
address,
AccountModifierFn::new(Box::new(move |balance, _nonce, _code| {
*balance += reward;
})),
)?;
}

Ok(())
}

/// Aborts building of the block, reverting all transactions in the process.
pub async fn abort(self) -> Result<(), SE> {
self.state.revert().await
let mut state = self.state.write().await;
state.revert()
}
}
19 changes: 16 additions & 3 deletions crates/rethnet_evm/src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
mod request;
mod sync;
use std::fmt::Debug;

pub use sync::{AsyncBlockchain, SyncBlockchain};
use revm::db::BlockHashRef;

/// Trait that meets all requirements for a synchronous database that can be used by [`AsyncBlockchain`].
pub trait SyncBlockchain<E>: BlockHashRef<Error = E> + Send + Sync + Debug + 'static
where
E: Debug + Send,
{
}

impl<B, E> SyncBlockchain<E> for B
where
B: BlockHashRef<Error = E> + Send + Sync + Debug + 'static,
E: Debug + Send,
{
}
49 changes: 0 additions & 49 deletions crates/rethnet_evm/src/blockchain/request.rs

This file was deleted.

124 changes: 0 additions & 124 deletions crates/rethnet_evm/src/blockchain/sync.rs

This file was deleted.

Loading