-
Notifications
You must be signed in to change notification settings - Fork 112
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
feat: add support for async transaction execution #1439
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,61 @@ | ||
use ethportal_api::Header; | ||
use revm::{inspector_handle_register, inspectors::TracerEip3155, Database, Evm}; | ||
use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, 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 of this block, introduced in Cancun | ||
let blob_excess_gas_and_price = header | ||
.excess_blob_gas | ||
.map(|excess_blob_gas| BlobExcessGasAndPrice::new(excess_blob_gas.to())); | ||
|
||
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: header.mix_hash, | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prevrandao: header.mix_hash,
I believe we should still be checking if we are past the Merge forprevrandao
.mix_hash won't be none in pre-merge blocks, it was used in PoW mining. Maybe it will be fine? but I would rather not take chances
For difficulty I think what you did is fine as the processed header should always have the right difficulty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know that
header.mix_hash
won't be none pre-merge. I fixed it.