Skip to content

Commit

Permalink
feat: local engine (#10803)
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 authored Sep 19, 2024
1 parent 89ca7a9 commit 6688078
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/assets/check_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ exclude_crates=(
reth-rpc-types
reth-stages
reth-storage-errors
reth-engine-local
# The following are not supposed to be working
reth # all of the crates below
reth-invalid-block-hooks # reth-provider
Expand Down
70 changes: 50 additions & 20 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"crates/consensus/debug-client/",
"crates/e2e-test-utils/",
"crates/engine/invalid-block-hooks/",
"crates/engine/local",
"crates/engine/primitives/",
"crates/engine/service",
"crates/engine/tree/",
Expand Down Expand Up @@ -314,6 +315,7 @@ reth-dns-discovery = { path = "crates/net/dns" }
reth-downloaders = { path = "crates/net/downloaders" }
reth-e2e-test-utils = { path = "crates/e2e-test-utils" }
reth-ecies = { path = "crates/net/ecies" }
reth-engine-local = { path = "crates/engine/local" }
reth-engine-primitives = { path = "crates/engine/primitives" }
reth-engine-tree = { path = "crates/engine/tree" }
reth-engine-service = { path = "crates/engine/service" }
Expand Down
46 changes: 46 additions & 0 deletions crates/engine/local/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "reth-engine-local"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
# reth
reth-beacon-consensus.workspace = true
reth-engine-tree.workspace = true
reth-node-types.workspace = true
reth-payload-builder.workspace = true
reth-payload-primitives.workspace = true
reth-primitives.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
reth-transaction-pool.workspace = true
reth-stages-api.workspace = true

# async
tokio.workspace = true
tokio-stream.workspace = true
futures-util.workspace = true

# misc
eyre.workspace = true
tracing.workspace = true

[dev-dependencies]
reth-chainspec.workspace = true
reth-chain-state.workspace = true
reth-config.workspace = true
reth-db = { workspace = true, features = ["test-utils"] }
reth-ethereum-engine-primitives.workspace = true
reth-exex-test-utils.workspace = true
reth-payload-builder = { workspace = true, features = ["test-utils"] }
reth-provider = { workspace = true, features = ["test-utils"] }
reth-rpc-types.workspace = true
reth-tracing.workspace = true

[lints]
workspace = true
3 changes: 3 additions & 0 deletions crates/engine/local/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! A local engine service that can be used to drive a dev chain.
pub mod miner;
pub mod service;
60 changes: 60 additions & 0 deletions crates/engine/local/src/miner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Contains the implementation of the mining mode for the local engine.

use futures_util::{stream::Fuse, StreamExt};
use reth_primitives::TxHash;
use reth_transaction_pool::TransactionPool;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use tokio::time::Interval;
use tokio_stream::wrappers::ReceiverStream;

/// A mining mode for the local dev engine.
#[derive(Debug)]
pub enum MiningMode {
/// In this mode a block is built as soon as
/// a valid transaction reaches the pool.
Instant(Fuse<ReceiverStream<TxHash>>),
/// In this mode a block is built at a fixed interval.
Interval(Interval),
}

impl MiningMode {
/// Constructor for a [`MiningMode::Instant`]
pub fn instant<Pool: TransactionPool>(pool: Pool) -> Self {
let rx = pool.pending_transactions_listener();
Self::Instant(ReceiverStream::new(rx).fuse())
}

/// Constructor for a [`MiningMode::Interval`]
pub fn interval(duration: Duration) -> Self {
let start = tokio::time::Instant::now() + duration;
Self::Interval(tokio::time::interval_at(start, duration))
}
}

impl Future for MiningMode {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
match this {
Self::Instant(rx) => {
// drain all transactions notifications
if let Poll::Ready(Some(_)) = rx.poll_next_unpin(cx) {
return Poll::Ready(())
}
Poll::Pending
}
Self::Interval(interval) => {
if interval.poll_tick(cx).is_ready() {
return Poll::Ready(())
}
Poll::Pending
}
}
}
}
Loading

0 comments on commit 6688078

Please sign in to comment.