chore: move TestConensus to reth-consensus (#7813)

This commit is contained in:
Matthias Seitz
2024-04-23 14:42:51 +02:00
committed by GitHub
parent 672e4c512c
commit 9fd35f948c
19 changed files with 122 additions and 104 deletions

View File

@ -1259,8 +1259,8 @@ mod tests {
use super::*; use super::*;
use assert_matches::assert_matches; use assert_matches::assert_matches;
use linked_hash_set::LinkedHashSet; use linked_hash_set::LinkedHashSet;
use reth_consensus::test_utils::TestConsensus;
use reth_db::{tables, test_utils::TempDatabase, transaction::DbTxMut, DatabaseEnv}; use reth_db::{tables, test_utils::TempDatabase, transaction::DbTxMut, DatabaseEnv};
use reth_interfaces::test_utils::TestConsensus;
use reth_node_ethereum::EthEvmConfig; use reth_node_ethereum::EthEvmConfig;
#[cfg(not(feature = "optimism"))] #[cfg(not(feature = "optimism"))]
use reth_primitives::proofs::calculate_receipt_root; use reth_primitives::proofs::calculate_receipt_root;

View File

@ -45,6 +45,7 @@ schnellru.workspace = true
# reth # reth
reth-payload-builder = { workspace = true, features = ["test-utils"] } reth-payload-builder = { workspace = true, features = ["test-utils"] }
reth-primitives = { workspace = true, features = ["test-utils"] } reth-primitives = { workspace = true, features = ["test-utils"] }
reth-consensus = { workspace = true, features = ["test-utils"] }
reth-interfaces = { workspace = true, features = ["test-utils"] } reth-interfaces = { workspace = true, features = ["test-utils"] }
reth-stages = { workspace = true, features = ["test-utils"] } reth-stages = { workspace = true, features = ["test-utils"] }
reth-blockchain-tree = { workspace = true, features = ["test-utils"] } reth-blockchain-tree = { workspace = true, features = ["test-utils"] }
@ -57,7 +58,6 @@ reth-downloaders.workspace = true
reth-evm-ethereum.workspace = true reth-evm-ethereum.workspace = true
reth-ethereum-engine-primitives.workspace = true reth-ethereum-engine-primitives.workspace = true
reth-config.workspace = true reth-config.workspace = true
reth-consensus.workspace = true
assert_matches.workspace = true assert_matches.workspace = true

View File

@ -7,7 +7,7 @@ use reth_blockchain_tree::{
config::BlockchainTreeConfig, externals::TreeExternals, BlockchainTree, ShareableBlockchainTree, config::BlockchainTreeConfig, externals::TreeExternals, BlockchainTree, ShareableBlockchainTree,
}; };
use reth_config::config::EtlConfig; use reth_config::config::EtlConfig;
use reth_consensus::Consensus; use reth_consensus::{test_utils::TestConsensus, Consensus};
use reth_db::{test_utils::TempDatabase, DatabaseEnv as DE}; use reth_db::{test_utils::TempDatabase, DatabaseEnv as DE};
use reth_downloaders::{ use reth_downloaders::{
bodies::bodies::BodiesDownloaderBuilder, bodies::bodies::BodiesDownloaderBuilder,
@ -19,7 +19,7 @@ use reth_interfaces::{
executor::BlockExecutionError, executor::BlockExecutionError,
p2p::{bodies::client::BodiesClient, either::EitherDownloader, headers::client::HeadersClient}, p2p::{bodies::client::BodiesClient, either::EitherDownloader, headers::client::HeadersClient},
sync::NoopSyncStateUpdater, sync::NoopSyncStateUpdater,
test_utils::{NoopFullBlockClient, TestConsensus}, test_utils::NoopFullBlockClient,
}; };
use reth_payload_builder::test_utils::spawn_test_payload_service; use reth_payload_builder::test_utils::spawn_test_payload_service;
use reth_primitives::{BlockNumber, ChainSpec, FinishedExExHeight, PruneModes, B256}; use reth_primitives::{BlockNumber, ChainSpec, FinishedExExHeight, PruneModes, B256};

View File

@ -15,4 +15,7 @@ reth-primitives.workspace = true
# misc # misc
auto_impl.workspace = true auto_impl.workspace = true
thiserror.workspace = true thiserror.workspace = true
[features]
test-utils = []

View File

@ -14,6 +14,10 @@ use reth_primitives::{
}; };
use std::fmt::Debug; use std::fmt::Debug;
#[cfg(any(test, feature = "test-utils"))]
/// test helpers for mocking consensus
pub mod test_utils;
/// Consensus is a protocol that chooses canonical chain. /// Consensus is a protocol that chooses canonical chain.
#[auto_impl::auto_impl(&, Arc)] #[auto_impl::auto_impl(&, Arc)]
pub trait Consensus: Debug + Send + Sync { pub trait Consensus: Debug + Send + Sync {

View File

@ -0,0 +1,70 @@
use crate::{Consensus, ConsensusError};
use reth_primitives::{Header, SealedBlock, SealedHeader, U256};
use std::sync::atomic::{AtomicBool, Ordering};
/// Consensus engine implementation for testing
#[derive(Debug)]
pub struct TestConsensus {
/// Flag whether the header validation should purposefully fail
fail_validation: AtomicBool,
}
impl Default for TestConsensus {
fn default() -> Self {
Self { fail_validation: AtomicBool::new(false) }
}
}
impl TestConsensus {
/// Get the failed validation flag.
pub fn fail_validation(&self) -> bool {
self.fail_validation.load(Ordering::SeqCst)
}
/// Update the validation flag.
pub fn set_fail_validation(&self, val: bool) {
self.fail_validation.store(val, Ordering::SeqCst)
}
}
impl Consensus for TestConsensus {
fn validate_header(&self, _header: &SealedHeader) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_against_parent(
&self,
_header: &SealedHeader,
_parent: &SealedHeader,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_with_total_difficulty(
&self,
_header: &Header,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_block(&self, _block: &SealedBlock) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
}

View File

@ -34,12 +34,14 @@ parking_lot = { workspace = true, optional = true }
rand = { workspace = true, optional = true } rand = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]
reth-consensus = { workspace = true, features = ["test-utils"] }
parking_lot.workspace = true parking_lot.workspace = true
rand.workspace = true rand.workspace = true
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
secp256k1 = { workspace = true, features = ["alloc", "recovery", "rand"] } secp256k1 = { workspace = true, features = ["alloc", "recovery", "rand"] }
[features] [features]
test-utils = ["secp256k1", "rand", "parking_lot"] test-utils = ["reth-consensus/test-utils", "secp256k1", "rand", "parking_lot"]
cli = ["clap"] cli = ["clap"]
optimism = ["reth-eth-wire-types/optimism"] optimism = ["reth-eth-wire-types/optimism"]

View File

@ -36,7 +36,7 @@ impl<Client> FullBlockClient<Client> {
/// Returns a client with Test consensus /// Returns a client with Test consensus
#[cfg(any(test, feature = "test-utils"))] #[cfg(any(test, feature = "test-utils"))]
pub fn test_client(client: Client) -> Self { pub fn test_client(client: Client) -> Self {
Self::new(client, Arc::new(crate::test_utils::TestConsensus::default())) Self::new(client, Arc::new(reth_consensus::test_utils::TestConsensus::default()))
} }
} }

View File

@ -1,5 +1,18 @@
//! Testing support for headers related interfaces. //! Testing support for headers related interfaces.
use std::{
fmt,
pin::Pin,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
task::{ready, Context, Poll},
};
use futures::{Future, FutureExt, Stream, StreamExt};
use tokio::sync::Mutex;
use crate::p2p::{ use crate::p2p::{
download::DownloadClient, download::DownloadClient,
error::{DownloadError, DownloadResult, PeerRequestResult, RequestError}, error::{DownloadError, DownloadResult, PeerRequestResult, RequestError},
@ -10,21 +23,8 @@ use crate::p2p::{
}, },
priority::Priority, priority::Priority,
}; };
use futures::{Future, FutureExt, Stream, StreamExt}; use reth_consensus::{test_utils::TestConsensus, Consensus};
use reth_consensus::{Consensus, ConsensusError}; use reth_primitives::{Header, HeadersDirection, PeerId, SealedHeader, WithPeerId};
use reth_primitives::{
Header, HeadersDirection, PeerId, SealedBlock, SealedHeader, WithPeerId, U256,
};
use std::{
fmt,
pin::Pin,
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Arc,
},
task::{ready, Context, Poll},
};
use tokio::sync::Mutex;
/// A test downloader which just returns the values that have been pushed to it. /// A test downloader which just returns the values that have been pushed to it.
#[derive(Debug)] #[derive(Debug)]
@ -243,70 +243,3 @@ impl HeadersClient for TestHeadersClient {
}) })
} }
} }
/// Consensus engine implementation for testing
#[derive(Debug)]
pub struct TestConsensus {
/// Flag whether the header validation should purposefully fail
fail_validation: AtomicBool,
}
impl Default for TestConsensus {
fn default() -> Self {
Self { fail_validation: AtomicBool::new(false) }
}
}
impl TestConsensus {
/// Get the failed validation flag.
pub fn fail_validation(&self) -> bool {
self.fail_validation.load(Ordering::SeqCst)
}
/// Update the validation flag.
pub fn set_fail_validation(&self, val: bool) {
self.fail_validation.store(val, Ordering::SeqCst)
}
}
impl Consensus for TestConsensus {
fn validate_header(&self, _header: &SealedHeader) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_against_parent(
&self,
_header: &SealedHeader,
_parent: &SealedHeader,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_header_with_total_difficulty(
&self,
_header: &Header,
_total_difficulty: U256,
) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
fn validate_block(&self, _block: &SealedBlock) -> Result<(), ConsensusError> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}
}

View File

@ -45,6 +45,7 @@ itertools.workspace = true
[dev-dependencies] [dev-dependencies]
reth-db = { workspace = true, features = ["test-utils"] } reth-db = { workspace = true, features = ["test-utils"] }
reth-consensus = { workspace = true, features = ["test-utils"] }
reth-interfaces = { workspace = true, features = ["test-utils"] } reth-interfaces = { workspace = true, features = ["test-utils"] }
reth-provider = { workspace = true, features = ["test-utils"] } reth-provider = { workspace = true, features = ["test-utils"] }
reth-tracing.workspace = true reth-tracing.workspace = true
@ -58,5 +59,5 @@ rand.workspace = true
tempfile.workspace = true tempfile.workspace = true
[features] [features]
test-utils = ["dep:tempfile", "reth-db/test-utils", "reth-interfaces/test-utils"] test-utils = ["dep:tempfile", "reth-db/test-utils", "reth-consensus/test-utils", "reth-interfaces/test-utils"]

View File

@ -604,8 +604,9 @@ mod tests {
test_utils::{generate_bodies, TestBodiesClient}, test_utils::{generate_bodies, TestBodiesClient},
}; };
use assert_matches::assert_matches; use assert_matches::assert_matches;
use reth_consensus::test_utils::TestConsensus;
use reth_db::test_utils::{create_test_rw_db, create_test_static_files_dir}; use reth_db::test_utils::{create_test_rw_db, create_test_static_files_dir};
use reth_interfaces::test_utils::{generators, generators::random_block_range, TestConsensus}; use reth_interfaces::test_utils::{generators, generators::random_block_range};
use reth_primitives::{BlockBody, B256, MAINNET}; use reth_primitives::{BlockBody, B256, MAINNET};
use reth_provider::ProviderFactory; use reth_provider::ProviderFactory;
use std::collections::HashMap; use std::collections::HashMap;

View File

@ -250,7 +250,8 @@ mod tests {
bodies::test_utils::zip_blocks, bodies::test_utils::zip_blocks,
test_utils::{generate_bodies, TestBodiesClient}, test_utils::{generate_bodies, TestBodiesClient},
}; };
use reth_interfaces::test_utils::{generators, generators::random_header_range, TestConsensus}; use reth_consensus::test_utils::TestConsensus;
use reth_interfaces::test_utils::{generators, generators::random_header_range};
/// Check if future returns empty bodies without dispatching any requests. /// Check if future returns empty bodies without dispatching any requests.
#[tokio::test] #[tokio::test]

View File

@ -170,7 +170,8 @@ mod tests {
test_utils::{generate_bodies, TestBodiesClient}, test_utils::{generate_bodies, TestBodiesClient},
}; };
use assert_matches::assert_matches; use assert_matches::assert_matches;
use reth_interfaces::{p2p::error::DownloadError, test_utils::TestConsensus}; use reth_consensus::test_utils::TestConsensus;
use reth_interfaces::p2p::error::DownloadError;
use reth_provider::test_utils::create_test_provider_factory; use reth_provider::test_utils::create_test_provider_factory;
use std::sync::Arc; use std::sync::Arc;

View File

@ -434,12 +434,10 @@ mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use rand::Rng; use rand::Rng;
use reth_interfaces::{ use reth_consensus::test_utils::TestConsensus;
p2p::{ use reth_interfaces::p2p::{
bodies::downloader::BodyDownloader, bodies::downloader::BodyDownloader,
headers::downloader::{HeaderDownloader, SyncTarget}, headers::downloader::{HeaderDownloader, SyncTarget},
},
test_utils::TestConsensus,
}; };
use reth_provider::test_utils::create_test_provider_factory; use reth_provider::test_utils::create_test_provider_factory;
use std::{mem, sync::Arc}; use std::{mem, sync::Arc};

View File

@ -1223,7 +1223,8 @@ mod tests {
use crate::headers::test_utils::child_header; use crate::headers::test_utils::child_header;
use assert_matches::assert_matches; use assert_matches::assert_matches;
use reth_interfaces::test_utils::{TestConsensus, TestHeadersClient}; use reth_consensus::test_utils::TestConsensus;
use reth_interfaces::test_utils::TestHeadersClient;
/// Tests that `replace_number` works the same way as Option::replace /// Tests that `replace_number` works the same way as Option::replace
#[test] #[test]

View File

@ -183,7 +183,8 @@ mod tests {
use crate::headers::{ use crate::headers::{
reverse_headers::ReverseHeadersDownloaderBuilder, test_utils::child_header, reverse_headers::ReverseHeadersDownloaderBuilder, test_utils::child_header,
}; };
use reth_interfaces::test_utils::{TestConsensus, TestHeadersClient}; use reth_consensus::test_utils::TestConsensus;
use reth_interfaces::test_utils::TestHeadersClient;
use std::sync::Arc; use std::sync::Arc;
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]

View File

@ -45,6 +45,7 @@ reth-primitives = { workspace = true, features = ["test-utils", "arbitrary"] }
reth-db = { workspace = true, features = ["test-utils", "mdbx"] } reth-db = { workspace = true, features = ["test-utils", "mdbx"] }
reth-evm-ethereum.workspace = true reth-evm-ethereum.workspace = true
reth-interfaces = { workspace = true, features = ["test-utils"] } reth-interfaces = { workspace = true, features = ["test-utils"] }
reth-consensus = { workspace = true, features = ["test-utils"] }
reth-downloaders.workspace = true reth-downloaders.workspace = true
reth-revm.workspace = true reth-revm.workspace = true
reth-static-file.workspace = true reth-static-file.workspace = true

View File

@ -15,7 +15,7 @@
//! # use std::sync::Arc; //! # use std::sync::Arc;
//! # use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder; //! # use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder;
//! # use reth_downloaders::headers::reverse_headers::ReverseHeadersDownloaderBuilder; //! # use reth_downloaders::headers::reverse_headers::ReverseHeadersDownloaderBuilder;
//! # use reth_interfaces::test_utils::{TestBodiesClient, TestConsensus, TestHeadersClient}; //! # use reth_interfaces::test_utils::{TestBodiesClient, TestHeadersClient};
//! # use reth_revm::EvmProcessorFactory; //! # use reth_revm::EvmProcessorFactory;
//! # use reth_primitives::{PeerId, MAINNET, B256, PruneModes}; //! # use reth_primitives::{PeerId, MAINNET, B256, PruneModes};
//! # use reth_stages::Pipeline; //! # use reth_stages::Pipeline;
@ -28,6 +28,7 @@
//! # use reth_static_file::StaticFileProducer; //! # use reth_static_file::StaticFileProducer;
//! # use reth_config::config::EtlConfig; //! # use reth_config::config::EtlConfig;
//! # use reth_consensus::Consensus; //! # use reth_consensus::Consensus;
//! # use reth_consensus::test_utils::TestConsensus;
//! # //! #
//! # let chain_spec = MAINNET.clone(); //! # let chain_spec = MAINNET.clone();
//! # let consensus: Arc<dyn Consensus> = Arc::new(TestConsensus::default()); //! # let consensus: Arc<dyn Consensus> = Arc::new(TestConsensus::default());

View File

@ -371,13 +371,13 @@ mod tests {
mod test_runner { mod test_runner {
use super::*; use super::*;
use crate::test_utils::{TestRunnerError, TestStageDB}; use crate::test_utils::{TestRunnerError, TestStageDB};
use reth_consensus::test_utils::TestConsensus;
use reth_db::{test_utils::TempDatabase, DatabaseEnv}; use reth_db::{test_utils::TempDatabase, DatabaseEnv};
use reth_downloaders::headers::reverse_headers::{ use reth_downloaders::headers::reverse_headers::{
ReverseHeadersDownloader, ReverseHeadersDownloaderBuilder, ReverseHeadersDownloader, ReverseHeadersDownloaderBuilder,
}; };
use reth_interfaces::test_utils::{ use reth_interfaces::test_utils::{
generators, generators::random_header_range, TestConsensus, TestHeaderDownloader, generators, generators::random_header_range, TestHeaderDownloader, TestHeadersClient,
TestHeadersClient,
}; };
use reth_provider::BlockNumReader; use reth_provider::BlockNumReader;
use tokio::sync::watch; use tokio::sync::watch;