Skip to content
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
26 changes: 16 additions & 10 deletions crates/engine/local/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use tracing::error;

/// A mining mode for the local dev engine.
#[derive(Debug)]
pub enum MiningMode {
pub enum MiningMode<Pool: TransactionPool + Unpin> {
/// In this mode a block is built as soon as
/// a valid transaction reaches the pool.
/// If `max_transactions` is set, a block is built when that many transactions have
/// accumulated.
Instant {
/// The transaction pool.
pool: Pool,
/// Stream of transaction notifications.
rx: Fuse<ReceiverStream<TxHash>>,
/// Maximum number of transactions to accumulate before mining a block.
Expand All @@ -42,11 +44,11 @@ pub enum MiningMode {
Interval(Interval),
}

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

/// Constructor for a [`MiningMode::Interval`]
Expand All @@ -56,15 +58,18 @@ impl MiningMode {
}
}

impl Future for MiningMode {
impl<Pool: TransactionPool + Unpin> Future for MiningMode<Pool> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
match this {
Self::Instant { rx, max_transactions, accumulated } => {
Self::Instant { pool, rx, max_transactions, accumulated } => {
// Poll for new transaction notifications
while let Poll::Ready(Some(_)) = rx.poll_next_unpin(cx) {
if pool.pending_and_queued_txn_count().0 == 0 {
continue;
}
if let Some(max_tx) = max_transactions {
*accumulated += 1;
// If we've reached the max transactions threshold, mine a block
Expand All @@ -91,13 +96,13 @@ impl Future for MiningMode {

/// Local miner advancing the chain
#[derive(Debug)]
pub struct LocalMiner<T: PayloadTypes, B> {
pub struct LocalMiner<T: PayloadTypes, B, Pool: TransactionPool + Unpin> {
/// The payload attribute builder for the engine
payload_attributes_builder: B,
/// Sender for events to engine.
to_engine: ConsensusEngineHandle<T>,
/// The mining mode for the engine
mode: MiningMode,
mode: MiningMode<Pool>,
/// The payload builder for the engine
payload_builder: PayloadBuilderHandle<T>,
/// Timestamp for the next block.
Expand All @@ -106,17 +111,18 @@ pub struct LocalMiner<T: PayloadTypes, B> {
last_block_hashes: Vec<B256>,
}

impl<T, B> LocalMiner<T, B>
impl<T, B, Pool> LocalMiner<T, B, Pool>
where
T: PayloadTypes,
B: PayloadAttributesBuilder<<T as PayloadTypes>::PayloadAttributes>,
Pool: TransactionPool + Unpin,
{
/// Spawns a new [`LocalMiner`] with the given parameters.
pub fn new(
provider: impl BlockReader,
payload_attributes_builder: B,
to_engine: ConsensusEngineHandle<T>,
mode: MiningMode,
mode: MiningMode<Pool>,
payload_builder: PayloadBuilderHandle<T>,
) -> Self {
let latest_header =
Expand Down
5 changes: 4 additions & 1 deletion crates/node/builder/src/launch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,10 @@ impl<R, ChainSpec: EthChainSpec> LaunchContextWith<Attached<WithConfigs<ChainSpe
}

/// Returns the [`MiningMode`] intended for --dev mode.
pub fn dev_mining_mode(&self, pool: impl TransactionPool) -> MiningMode {
pub fn dev_mining_mode<Pool>(&self, pool: Pool) -> MiningMode<Pool>
where
Pool: TransactionPool + Unpin,
{
if let Some(interval) = self.node_config().dev.block_time {
MiningMode::interval(interval)
} else {
Expand Down
5 changes: 4 additions & 1 deletion crates/node/core/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,10 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
}

/// Returns the [`MiningMode`] intended for --dev mode.
pub fn dev_mining_mode(&self, pool: impl TransactionPool) -> MiningMode {
pub fn dev_mining_mode<Pool>(&self, pool: Pool) -> MiningMode<Pool>
where
Pool: TransactionPool + Unpin,
{
if let Some(interval) = self.dev.block_time {
MiningMode::interval(interval)
} else {
Expand Down