Skip to content
Closed
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
42 changes: 28 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"bin/reth",
"crates/consensus/auto-seal",
"crates/consensus/beacon",
"crates/consensus/common",
"crates/executor",
Expand Down
26 changes: 26 additions & 0 deletions crates/consensus/auto-seal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "reth-auto-seal-consensus"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/paradigmxyz/reth"
readme = "README.md"
description = "A consensus impl for local testing purposes"

[dependencies]
# reth
reth-consensus-common = { path = "../common" }
reth-primitives = { path = "../../primitives" }
reth-interfaces = { path = "../../interfaces" }
reth-transaction-pool = { path = "../../transaction-pool" }

# async
futures-util = "0.3"
tokio = { version = "1", features = ["sync", "time"] }
tokio-stream = { version = "0.1", feautres = ["tokio-util"] }

# misc
tracing = "0.1"

[dev-dependencies]
reth-interfaces = { path = "../../interfaces", features = ["test-utils"] }
18 changes: 18 additions & 0 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![warn(missing_docs, unreachable_pub, unused_crate_dependencies)]
#![deny(unused_must_use, rust_2018_idioms)]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
))]

//! A [Consensus](reth_interfaces::consensus::Consensus) implementation for local testing purposes
//! that automatically seals blocks.

use reth_interfaces::consensus::ForkchoiceState;

mod miner_client;
mod mode;

/// A consensus implementation that follows a strategy for announcing blocks via [ForkchoiceState]
#[derive(Debug)]
pub struct AutoSealConsensus {}
106 changes: 106 additions & 0 deletions crates/consensus/auto-seal/src/miner_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! This includes download client implementations for auto sealing miners.
//!
//! Auto sealing miners can be polled, and will return a list of transactions that are ready to be
//! mined.
//!
//! TODO: this polls the miners, so maybe the consensus should also be implemented here?
//!
//! These downloaders poll the miner, assemble the block, and return transactions that are ready to
//! be mined.
use std::{
collections::HashMap,
fmt::Debug,
pin::Pin,
task::{Context, Poll},
};

use crate::mode::MiningMode;
use futures_util::Future;
use reth_interfaces::p2p::{
bodies::client::{BodiesClient, BodiesFut},
download::DownloadClient,
headers::client::{HeadersClient, HeadersFut, HeadersRequest},
priority::Priority,
};
use reth_primitives::{BlockHash, BlockNumber, Header, PeerId, H256, BlockBody};
use reth_transaction_pool::TransactionPool;
use tracing::warn;

/// A download client that polls the miner for transactions and assembles blocks to be returned in
/// the download process.
///
/// When polled, the miner will assemble blocks when miners produce ready transactions and store the
/// blocks in memory.
#[derive(Debug)]
pub struct AutoSealClient<Pool> {
/// The active miner
miner: MiningMode,

/// Headers buffered for download.
headers: HashMap<BlockNumber, Header>,

/// A mapping between block hash and number.
hash_to_number: HashMap<BlockHash, BlockNumber>,

/// Bodies buffered for download.
bodies: HashMap<BlockHash, BlockBody>,

/// The mempool to poll for new ready transactions.
pool: Pool,
}

impl<Pool> Future for AutoSealClient<Pool> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
while let Poll::Ready(txs) = Pin::new(&mut self.miner).poll(&self.pool, cx) {
todo!("assemble block and put the header / body in maps")
}
Poll::Pending
}
}

impl<Pool> HeadersClient for AutoSealClient<Pool>
where
Pool: TransactionPool + Clone + 'static + Debug,
{
type Output = HeadersFut;

fn get_headers_with_priority(
&self,
request: HeadersRequest,
_priority: Priority,
) -> Self::Output {
todo!("return from map")
}
}

impl<Pool> BodiesClient for AutoSealClient<Pool>
where
Pool: TransactionPool + Clone + 'static + Debug,
{
type Output = BodiesFut;

fn get_block_bodies_with_priority(
&self,
hashes: Vec<H256>,
priority: Priority,
) -> Self::Output {
todo!("return from map")
}
}

impl<Pool> DownloadClient for AutoSealClient<Pool>
where
Pool: TransactionPool + Clone + 'static + Debug,
{
fn report_bad_message(&self, _peer_id: PeerId) {
warn!("Reported a bad message on a miner, we should never produce bad blocks");
// noop
}

fn num_connected_peers(&self) -> usize {
// no such thing as connected peers when we are mining ourselves
1
}
}
Loading