Skip to content

Commit

Permalink
refactor: separate block sealing logic from executing txs (#389)
Browse files Browse the repository at this point in the history
* separate block sealing logic from executing txs

* match anvil/hardhat behaviour + small refactoring
  • Loading branch information
itegulov authored Nov 19, 2024
1 parent e1d9a8f commit 2bb843d
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 473 deletions.
2 changes: 1 addition & 1 deletion src/namespaces/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait AnvilNamespaceT {
///
/// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
#[rpc(name = "anvil_mine")]
fn anvil_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<bool>;
fn anvil_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<()>;

/// Reset the state of the network back to a fresh forked state, or disable forking.
///
Expand Down
2 changes: 1 addition & 1 deletion src/namespaces/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait HardhatNamespaceT {
///
/// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
#[rpc(name = "hardhat_mine")]
fn hardhat_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<bool>;
fn hardhat_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<()>;

/// Retrieves the current automine status of the network.
///
Expand Down
2 changes: 1 addition & 1 deletion src/node/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> AnvilNames
.into_boxed_future()
}

fn anvil_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<bool> {
fn anvil_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<()> {
self.mine_blocks(num_blocks, interval)
.map_err(|err| {
tracing::error!("failed mining blocks: {:?}", err);
Expand Down
30 changes: 16 additions & 14 deletions src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
return Err(err.into());
};

self.run_l2_tx(l2_tx, system_contracts).map_err(|err| {
Web3Error::SubmitTransactionError(
format!("Execution error: {err}"),
hash.as_bytes().to_vec(),
)
})?;
self.seal_block(vec![l2_tx], system_contracts)
.map_err(|err| {
Web3Error::SubmitTransactionError(
format!("Execution error: {err}"),
hash.as_bytes().to_vec(),
)
})?;
Ok(hash)
}

Expand Down Expand Up @@ -188,12 +189,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
}
}

self.run_l2_tx(l2_tx, system_contracts).map_err(|err| {
Web3Error::SubmitTransactionError(
format!("Execution error: {err}"),
hash.as_bytes().to_vec(),
)
})?;
self.seal_block(vec![l2_tx], system_contracts)
.map_err(|err| {
Web3Error::SubmitTransactionError(
format!("Execution error: {err}"),
hash.as_bytes().to_vec(),
)
})?;
Ok(hash)
}
}
Expand Down Expand Up @@ -1596,15 +1598,15 @@ mod tests {
.expect("no block");

assert_eq!(0, block.number.as_u64());
assert_eq!(compute_hash(0, H256::zero()), block.hash);
assert_eq!(compute_hash(0, []), block.hash);
}

#[tokio::test]
async fn test_node_creates_genesis_block_with_hash_and_zero_parent_hash() {
let node = InMemoryNode::<HttpForkSource>::default();

let block = node
.get_block_by_hash(compute_hash(0, H256::zero()), false)
.get_block_by_hash(compute_hash(0, []), false)
.await
.expect("failed fetching block by hash")
.expect("no block");
Expand Down
2 changes: 1 addition & 1 deletion src/node/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> HardhatNam
.into_boxed_future()
}

fn hardhat_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<bool> {
fn hardhat_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<()> {
self.mine_blocks(num_blocks, interval)
.map_err(|err| {
tracing::error!("failed mining blocks: {:?}", err);
Expand Down
Loading

0 comments on commit 2bb843d

Please sign in to comment.