From faa40e01778163e689a04b681604f08ad9f998cf Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Fri, 27 Aug 2021 02:05:58 +0000 Subject: [PATCH] Store a `SyncStatus` handle in the `Crawler` The helper type will make it easier to determine if the crawler is enabled or not. --- zebrad/src/commands/start.rs | 4 ++-- zebrad/src/components/mempool/crawler.rs | 6 +++++- zebrad/src/components/mempool/crawler/tests.rs | 10 ++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index 8a998445ff6..145a152995b 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -85,12 +85,12 @@ impl StartCmd { info!("initializing syncer"); // TODO: use sync_status to activate the mempool (#2592) - let (syncer, _sync_status) = + let (syncer, sync_status) = ChainSync::new(&config, peer_set.clone(), state, chain_verifier); select! { result = syncer.sync().fuse() => result, - _ = mempool::Crawler::spawn(peer_set).fuse() => { + _ = mempool::Crawler::spawn(peer_set, sync_status).fuse() => { unreachable!("The mempool crawler only stops if it panics"); } } diff --git a/zebrad/src/components/mempool/crawler.rs b/zebrad/src/components/mempool/crawler.rs index 22b509a6590..e924d6382df 100644 --- a/zebrad/src/components/mempool/crawler.rs +++ b/zebrad/src/components/mempool/crawler.rs @@ -10,6 +10,8 @@ use tower::{timeout::Timeout, BoxError, Service, ServiceExt}; use zebra_network::{Request, Response}; +use super::super::sync::SyncStatus; + #[cfg(test)] mod tests; @@ -31,6 +33,7 @@ const PEER_RESPONSE_TIMEOUT: Duration = Duration::from_secs(6); /// The mempool transaction crawler. pub struct Crawler { peer_set: Mutex>, + status: SyncStatus, } impl Crawler @@ -39,9 +42,10 @@ where S::Future: Send, { /// Spawn an asynchronous task to run the mempool crawler. - pub fn spawn(peer_set: S) -> JoinHandle> { + pub fn spawn(peer_set: S, status: SyncStatus) -> JoinHandle> { let crawler = Crawler { peer_set: Mutex::new(Timeout::new(peer_set, PEER_RESPONSE_TIMEOUT)), + status, }; tokio::spawn(crawler.run()) diff --git a/zebrad/src/components/mempool/crawler/tests.rs b/zebrad/src/components/mempool/crawler/tests.rs index 35b71d04233..afdf28944c4 100644 --- a/zebrad/src/components/mempool/crawler/tests.rs +++ b/zebrad/src/components/mempool/crawler/tests.rs @@ -8,7 +8,7 @@ use tower::{buffer::Buffer, util::BoxService, BoxError}; use zebra_network::{Request, Response}; -use super::{Crawler, FANOUT, RATE_LIMIT_DELAY}; +use super::{Crawler, SyncStatus, FANOUT, RATE_LIMIT_DELAY}; /// The number of iterations to crawl while testing. /// @@ -30,7 +30,13 @@ const ERROR_MARGIN: Duration = Duration::from_millis(100); async fn crawler_requests_for_transaction_ids() { let (peer_set, mut requests) = mock_peer_set(); - Crawler::spawn(peer_set); + // Mock the latest sync length in a state that enables the mempool. + let (sync_status, mut recent_sync_lengths) = SyncStatus::new(); + for _ in 0..5 { + recent_sync_lengths.push_extend_tips_length(0); + } + + Crawler::spawn(peer_set, sync_status); time::pause();