-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for async transaction execution (#1439)
- Loading branch information
Showing
13 changed files
with
218 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::future::Future; | ||
|
||
use revm::Database; | ||
use revm_primitives::{AccountInfo, Address, BlockEnv, Bytecode, EVMError, EVMResult, B256, U256}; | ||
use tokio::{runtime, task}; | ||
|
||
use super::{create_evm, tx_env_modifier::TxEnvModifier}; | ||
|
||
/// The async version of the [revm::Database]. | ||
pub trait AsyncDatabase { | ||
/// The database error type. | ||
type Error; | ||
|
||
/// Get basic account information. | ||
fn basic_async( | ||
&mut self, | ||
address: Address, | ||
) -> impl Future<Output = Result<Option<AccountInfo>, Self::Error>> + Send; | ||
|
||
/// Get account code by its hash. | ||
fn code_by_hash_async( | ||
&mut self, | ||
code_hash: B256, | ||
) -> impl Future<Output = Result<Bytecode, Self::Error>> + Send; | ||
|
||
/// Get storage value of address at index. | ||
fn storage_async( | ||
&mut self, | ||
address: Address, | ||
index: U256, | ||
) -> impl Future<Output = Result<U256, Self::Error>> + Send; | ||
|
||
/// Get block hash by block number. | ||
fn block_hash_async( | ||
&mut self, | ||
number: u64, | ||
) -> impl Future<Output = Result<B256, Self::Error>> + Send; | ||
} | ||
|
||
/// Wraps the [AsyncDatabase] to provide [revm::Database] implementation. | ||
/// | ||
/// This should only be used when blocking thread is allowed, e.g. from within spawn::blocking. | ||
pub(super) struct WrapAsyncDatabase<DB: AsyncDatabase> { | ||
db: DB, | ||
rt: runtime::Runtime, | ||
} | ||
|
||
impl<DB: AsyncDatabase> WrapAsyncDatabase<DB> { | ||
pub fn new(db: DB, rt: runtime::Runtime) -> Self { | ||
Self { db, rt } | ||
} | ||
} | ||
|
||
impl<DB: AsyncDatabase> Database for WrapAsyncDatabase<DB> { | ||
type Error = DB::Error; | ||
|
||
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> { | ||
self.rt.block_on(self.db.basic_async(address)) | ||
} | ||
|
||
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> { | ||
self.rt.block_on(self.db.code_by_hash_async(code_hash)) | ||
} | ||
|
||
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> { | ||
self.rt.block_on(self.db.storage_async(address, index)) | ||
} | ||
|
||
fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> { | ||
self.rt.block_on(self.db.block_hash_async(number)) | ||
} | ||
} | ||
|
||
pub async fn execute_transaction<DB, DBError, Tx>( | ||
block_env: BlockEnv, | ||
tx: Tx, | ||
db: DB, | ||
) -> EVMResult<DB::Error> | ||
where | ||
DB: AsyncDatabase<Error = DBError> + Send + 'static, | ||
DBError: Send + 'static, | ||
Tx: TxEnvModifier + Send + 'static, | ||
{ | ||
task::spawn_blocking(move || { | ||
let rt = runtime::Runtime::new().expect("to create Runtime within spawn_blocking"); | ||
let mut db = WrapAsyncDatabase::new(db, rt); | ||
let mut evm = create_evm(block_env, &tx, &mut db); | ||
evm.transact() | ||
}) | ||
.await | ||
.unwrap_or_else(|err| { | ||
Err(EVMError::Custom(format!( | ||
"Error while executing transactions asynchronously: {err:?}" | ||
))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,68 @@ | ||
use ethportal_api::Header; | ||
use revm::{inspector_handle_register, inspectors::TracerEip3155, Database, Evm}; | ||
use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, SpecId, U256}; | ||
use spec_id::get_spec_id; | ||
use tx_env_modifier::TxEnvModifier; | ||
|
||
pub mod async_db; | ||
pub mod block_executor; | ||
pub mod blocking; | ||
pub mod dao_fork; | ||
pub mod post_block_beneficiaries; | ||
pub mod spec_id; | ||
pub mod tx_env_modifier; | ||
|
||
/// Creates [BlockEnv] based on data in the [Header]. | ||
pub fn create_block_env(header: &Header) -> BlockEnv { | ||
// EIP-4844: Excess blob gas and blob gasprice, introduced in Cancun | ||
let blob_excess_gas_and_price = header | ||
.excess_blob_gas | ||
.map(|excess_blob_gas| BlobExcessGasAndPrice::new(excess_blob_gas.to())); | ||
|
||
// EIP-4399: Expose beacon chain randomness in eth EVM, introduced in Paris (aka the Merge) | ||
let prevrandao = if get_spec_id(header.number).is_enabled_in(SpecId::MERGE) { | ||
header.mix_hash | ||
} else { | ||
None | ||
}; | ||
|
||
BlockEnv { | ||
number: U256::from(header.number), | ||
coinbase: header.author, | ||
timestamp: U256::from(header.timestamp), | ||
gas_limit: header.gas_limit, | ||
basefee: header.base_fee_per_gas.unwrap_or_default(), | ||
difficulty: header.difficulty, | ||
prevrandao, | ||
blob_excess_gas_and_price, | ||
} | ||
} | ||
|
||
/// Creates [Evm] that is ready to execute provided transaction. | ||
pub fn create_evm<'evm, 'db, DB: Database, Tx: TxEnvModifier>( | ||
block_env: BlockEnv, | ||
tx: &Tx, | ||
db: &'db mut DB, | ||
) -> Evm<'evm, (), &'db mut DB> { | ||
let block_number = block_env.number.to(); | ||
let spec_id = get_spec_id(block_number); | ||
Evm::builder() | ||
.with_block_env(block_env) | ||
.modify_tx_env(|tx_env| tx.modify(block_number, tx_env)) | ||
.with_spec_id(spec_id) | ||
.with_db(db) | ||
.build() | ||
} | ||
|
||
/// Creates [Evm] that is ready to execute provided transaction, with attached tracer. | ||
pub fn create_evm_with_tracer<'evm, 'db, DB: Database, Tx: TxEnvModifier>( | ||
block_env: BlockEnv, | ||
tx: &Tx, | ||
db: &'db mut DB, | ||
tracer: TracerEip3155, | ||
) -> Evm<'evm, TracerEip3155, &'db mut DB> { | ||
create_evm(block_env, tx, db) | ||
.modify() | ||
.reset_handler_with_external_context(tracer) | ||
.append_handler_register(inspector_handle_register) | ||
.build() | ||
} |
Oops, something went wrong.