feat(test): add TestApi type (#183)

This commit is contained in:
Matthias Seitz
2022-11-10 19:17:36 +01:00
committed by GitHub
parent d4aabe4751
commit fc9b6f35e7
5 changed files with 40 additions and 5 deletions

View File

@ -17,7 +17,7 @@ pub trait HeaderProvider: Send + Sync {
fn header(&self, block_hash: &BlockHash) -> Result<Option<Header>>;
}
/// Client trait for fetching `Block` related data.
/// Api trait for fetching `Block` related data.
pub trait BlockProvider: Send + Sync {
/// Returns the current info for the chain.
fn chain_info(&self) -> Result<ChainInfo>;

View File

@ -3,7 +3,7 @@ pub mod db_provider;
mod error;
mod storage;
pub use block::{BlockProvider, HeaderProvider};
pub use block::{BlockProvider, ChainInfo, HeaderProvider};
pub use db_provider::{self as db, ProviderImpl};
pub use error::Error;
pub use storage::{StateProvider, StateProviderFactory, StorageProvider};

View File

@ -0,0 +1,31 @@
use crate::{provider, provider::BlockProvider};
use reth_primitives::{rpc::BlockId, Block, BlockNumber, H256, U256};
/// Supports various api interfaces for testing purposes.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct TestApi;
/// Noop implementation for testing purposes
impl BlockProvider for TestApi {
fn chain_info(&self) -> crate::Result<provider::ChainInfo> {
Ok(provider::ChainInfo {
best_hash: Default::default(),
best_number: 0,
last_finalized: None,
safe_finalized: None,
})
}
fn block(&self, _id: BlockId) -> crate::Result<Option<Block>> {
Ok(None)
}
fn block_number(&self, _hash: H256) -> crate::Result<Option<BlockNumber>> {
Ok(None)
}
fn block_hash(&self, _number: U256) -> crate::Result<Option<H256>> {
Ok(None)
}
}

View File

@ -1,3 +1,4 @@
//! Testing support for headers related interfaces.
use crate::{
consensus::{self, Consensus},
p2p::headers::{
@ -5,11 +6,9 @@ use crate::{
downloader::{DownloadError, Downloader},
},
};
use std::{collections::HashSet, sync::Arc, time::Duration};
use reth_primitives::{Header, SealedHeader, H256, H512, U256};
use reth_rpc_types::engine::ForkchoiceState;
use std::{collections::HashSet, sync::Arc, time::Duration};
use tokio::sync::{broadcast, mpsc, watch};
use tokio_stream::{wrappers::BroadcastStream, StreamExt};

View File

@ -0,0 +1,5 @@
mod api;
mod headers;
pub use api::TestApi;
pub use headers::*;