Skip to content

Commit

Permalink
Store a SyncStatus handle in the Crawler
Browse files Browse the repository at this point in the history
The helper type will make it easier to determine if the crawler is
enabled or not.
  • Loading branch information
jvff committed Aug 30, 2021
1 parent 2e1d857 commit faa40e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down
6 changes: 5 additions & 1 deletion zebrad/src/components/mempool/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +33,7 @@ const PEER_RESPONSE_TIMEOUT: Duration = Duration::from_secs(6);
/// The mempool transaction crawler.
pub struct Crawler<S> {
peer_set: Mutex<Timeout<S>>,
status: SyncStatus,
}

impl<S> Crawler<S>
Expand All @@ -39,9 +42,10 @@ where
S::Future: Send,
{
/// Spawn an asynchronous task to run the mempool crawler.
pub fn spawn(peer_set: S) -> JoinHandle<Result<(), BoxError>> {
pub fn spawn(peer_set: S, status: SyncStatus) -> JoinHandle<Result<(), BoxError>> {
let crawler = Crawler {
peer_set: Mutex::new(Timeout::new(peer_set, PEER_RESPONSE_TIMEOUT)),
status,
};

tokio::spawn(crawler.run())
Expand Down
10 changes: 8 additions & 2 deletions zebrad/src/components/mempool/crawler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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();

Expand Down

0 comments on commit faa40e0

Please sign in to comment.