mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-15 02:08:09 -05:00
chore(era): complete doc for ClientWithFakeIndex (#16984)
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
use alloy_primitives::bytes::Bytes;
|
||||
use futures_util::{Stream, TryStreamExt};
|
||||
use reqwest::{Client, IntoUrl, Url};
|
||||
use crate::{ClientWithFakeIndex, ITHACA_ERA_INDEX_URL};
|
||||
use reqwest::{Client, Url};
|
||||
use reth_db_common::init::init_genesis;
|
||||
use reth_era_downloader::{EraClient, EraStream, EraStreamConfig, HttpClient};
|
||||
use reth_era_downloader::{EraClient, EraStream, EraStreamConfig};
|
||||
use reth_etl::Collector;
|
||||
use reth_provider::test_utils::create_test_provider_factory;
|
||||
use std::{future::Future, str::FromStr};
|
||||
use std::str::FromStr;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_history_imports_from_fresh_state_successfully() {
|
||||
// URL where the ERA1 files are hosted
|
||||
let url = Url::from_str("https://era.ithaca.xyz/era1/index.html").unwrap();
|
||||
let url = Url::from_str(ITHACA_ERA_INDEX_URL).unwrap();
|
||||
|
||||
// Directory where the ERA1 files will be downloaded to
|
||||
let folder = tempdir().unwrap();
|
||||
@@ -35,38 +34,3 @@ async fn test_history_imports_from_fresh_state_successfully() {
|
||||
|
||||
assert_eq!(actual_block_number, expected_block_number);
|
||||
}
|
||||
|
||||
/// An HTTP client pre-programmed with canned answer to index.
|
||||
///
|
||||
/// Passes any other calls to a real HTTP client!
|
||||
#[derive(Debug, Clone)]
|
||||
struct ClientWithFakeIndex(Client);
|
||||
|
||||
impl HttpClient for ClientWithFakeIndex {
|
||||
fn get<U: IntoUrl + Send + Sync>(
|
||||
&self,
|
||||
url: U,
|
||||
) -> impl Future<
|
||||
Output = eyre::Result<impl Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin>,
|
||||
> + Send
|
||||
+ Sync {
|
||||
let url = url.into_url().unwrap();
|
||||
|
||||
async move {
|
||||
match url.to_string().as_str() {
|
||||
"https://era.ithaca.xyz/era1/index.html" => {
|
||||
Ok(Box::new(futures::stream::once(Box::pin(async move {
|
||||
Ok(bytes::Bytes::from_static(b"<a href=\"https://era.ithaca.xyz/era1/mainnet-00000-5ec1ffb8.era1\">mainnet-00000-5ec1ffb8.era1</a>"))
|
||||
})))
|
||||
as Box<dyn Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin>)
|
||||
}
|
||||
_ => {
|
||||
let response = Client::get(&self.0, url).send().await?;
|
||||
|
||||
Ok(Box::new(response.bytes_stream().map_err(|e| eyre::Error::new(e)))
|
||||
as Box<dyn Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin>)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,3 +3,44 @@
|
||||
mod history;
|
||||
|
||||
const fn main() {}
|
||||
|
||||
use alloy_primitives::bytes::Bytes;
|
||||
use futures_util::{Stream, TryStreamExt};
|
||||
use reqwest::{Client, IntoUrl};
|
||||
use reth_era_downloader::HttpClient;
|
||||
|
||||
// Url where the ERA1 files are hosted
|
||||
const ITHACA_ERA_INDEX_URL: &str = "https://era.ithaca.xyz/era1/index.html";
|
||||
|
||||
// The response containing one file that the fake client will return when the index Url is requested
|
||||
const GENESIS_ITHACA_INDEX_RESPONSE: &[u8] = b"<a href=\"https://era.ithaca.xyz/era1/mainnet-00000-5ec1ffb8.era1\">mainnet-00000-5ec1ffb8.era1</a>";
|
||||
|
||||
/// An HTTP client that fakes the file list to always show one known file
|
||||
///
|
||||
/// but passes all other calls including actual downloads to a real HTTP client
|
||||
///
|
||||
/// In that way, only one file is used but downloads are still performed from the original source.
|
||||
#[derive(Debug, Clone)]
|
||||
struct ClientWithFakeIndex(Client);
|
||||
|
||||
impl HttpClient for ClientWithFakeIndex {
|
||||
async fn get<U: IntoUrl + Send + Sync>(
|
||||
&self,
|
||||
url: U,
|
||||
) -> eyre::Result<impl Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin> {
|
||||
let url = url.into_url().unwrap();
|
||||
|
||||
match url.to_string().as_str() {
|
||||
ITHACA_ERA_INDEX_URL => Ok(Box::new(futures::stream::once(Box::pin(async move {
|
||||
Ok(bytes::Bytes::from_static(GENESIS_ITHACA_INDEX_RESPONSE))
|
||||
})))
|
||||
as Box<dyn Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin>),
|
||||
_ => {
|
||||
let response = Client::get(&self.0, url).send().await?;
|
||||
|
||||
Ok(Box::new(response.bytes_stream().map_err(|e| eyre::Error::new(e)))
|
||||
as Box<dyn Stream<Item = eyre::Result<Bytes>> + Send + Sync + Unpin>)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user