Skip to content

Commit

Permalink
Refactor ChainSync ctor. to return SyncStatus
Browse files Browse the repository at this point in the history
Change the constructor API so that it returns a higher level construct.
  • Loading branch information
jvff committed Aug 27, 2021
1 parent 444171d commit b0b0f75
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl StartCmd {
.map_err(|_| eyre!("could not send setup data to inbound service"))?;

info!("initializing syncer");
// TODO: use sync_length_receiver to activate the mempool (#2592)
let (syncer, _sync_length_receiver) =
// TODO: use sync_status to activate the mempool (#2592)
let (syncer, _sync_status) =
ChainSync::new(&config, peer_set.clone(), state, chain_verifier);

select! {
Expand Down
15 changes: 5 additions & 10 deletions zebrad/src/components/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::{
future::FutureExt,
stream::{FuturesUnordered, StreamExt},
};
use tokio::{sync::watch, time::sleep};
use tokio::time::sleep;
use tower::{
builder::ServiceBuilder, hedge::Hedge, limit::ConcurrencyLimit, retry::Retry, timeout::Timeout,
Service, ServiceExt,
Expand Down Expand Up @@ -224,13 +224,8 @@ where
/// - state: the zebra-state that stores the chain
/// - verifier: the zebra-consensus verifier that checks the chain
///
/// Also returns a [`watch::Receiver`] endpoint for receiving recent sync lengths.
pub fn new(
config: &ZebradConfig,
peers: ZN,
state: ZS,
verifier: ZV,
) -> (Self, watch::Receiver<Vec<usize>>) {
/// Also returns a [`SyncStatus`] to check if the syncer has likely reached the chain tip.
pub fn new(config: &ZebradConfig, peers: ZN, state: ZS, verifier: ZV) -> (Self, SyncStatus) {
let tip_network = Timeout::new(peers.clone(), TIPS_RESPONSE_TIMEOUT);
// The Hedge middleware is the outermost layer, hedging requests
// between two retry-wrapped networks. The innermost timeout
Expand Down Expand Up @@ -266,7 +261,7 @@ where
MIN_LOOKAHEAD_LIMIT
);

let (recent_syncs, sync_length_receiver) = RecentSyncLengths::new();
let (sync_status, recent_syncs) = SyncStatus::new();

let new_syncer = Self {
genesis_hash: genesis_hash(config.network.network),
Expand All @@ -278,7 +273,7 @@ where
recent_syncs,
};

(new_syncer, sync_length_receiver)
(new_syncer, sync_status)
}

#[instrument(skip(self))]
Expand Down
9 changes: 7 additions & 2 deletions zebrad/src/components/sync/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use tokio::sync::watch;

use super::RecentSyncLengths;

/// A helper type to determine if the synchronizer has likely reached the chain tip.
///
/// This type can be used as a handle, so cloning it is cheap.
Expand All @@ -13,8 +15,11 @@ impl SyncStatus {
///
/// The status is determined based on the latest counts of synchronized blocks, observed
/// through `latest_sync_length`.
pub fn new(latest_sync_length: watch::Receiver<Vec<usize>>) -> Self {
SyncStatus { latest_sync_length }
pub fn new() -> (Self, RecentSyncLengths) {
let (recent_sync_lengths, latest_sync_length) = RecentSyncLengths::new();
let status = SyncStatus { latest_sync_length };

(status, recent_sync_lengths)
}

/// Wait until the synchronization is likely close to the tip.
Expand Down

0 comments on commit b0b0f75

Please sign in to comment.