fn sync<T: BlockTree>(&mut self, now: LocalTime, tree: &T) {
        if self.peers.is_empty() {
            return;
        }
        if self.is_synced(now, tree) {
            let (tip, _) = tree.tip();
            let height = tree.height();

            self.upstream.event(Event::Synced(tip, height));

            // If we think we're in sync and we haven't asked other peers in a while, then
            // sample their headers just to make sure we're on the right chain.
            if self
                .last_peer_sample
                .map(|t| now.duration_since(t) >= PEER_SAMPLE_INTERVAL)
                .unwrap_or(true)
            {
                self.last_peer_sample = Some(now);

                self.sample_peers(now, tree);
            }
            return;
        }
        // It looks like we're out of sync...

        let locators = (tree.locator_hashes(tree.height()), BlockHash::default());

        // If we're already fetching these headers, just wait.
        if self.syncing(&locators) {
            return;
        }

        if let Some(peer) = self.random_sync_candidate(&locators.0, tree) {
            let timeout = self.config.request_timeout;
            let addr = peer.id;

            self.request(addr, locators, now, timeout, OnTimeout::Ignore);
            self.upstream.event(Event::Syncing(addr));
        } else {
            // TODO: No peer found to sync.. emit event.
        }
    }
/// Start syncing if we're out of sync.
Start syncing if we're out of sync.
Start syncing if we ' re out of sync .
fn sync<T: BlockTree>(&mut self, now: LocalTime, tree: &T) {
        if self.peers.is_empty() {
            return;
        }
        if self.is_synced(now, tree) {
            let (tip, _) = tree.tip();
            let height = tree.height();
            self.upstream.event(Event::Synced(tip, height));
            // If we think we're in sync and we haven't asked other peers in a while, then
            // sample their headers just to make sure we're on the right chain.
            if self
                .last_peer_sample
                .map(|t| now.duration_since(t) >= PEER_SAMPLE_INTERVAL)
                .unwrap_or(true)
            {
                self.last_peer_sample = Some(now);
                self.sample_peers(now, tree);
            }
            return;
        }
        // It looks like we're out of sync...
        let locators = (tree.locator_hashes(tree.height()), BlockHash::default());
        // If we're already fetching these headers, just wait.
        if self.syncing(&locators) {
            return;
        }
        if let Some(peer) = self.random_sync_candidate(&locators.0, tree) {
            let timeout = self.config.request_timeout;
            let addr = peer.id;
            self.request(addr, locators, now, timeout, OnTimeout::Ignore);
            self.upstream.event(Event::Syncing(addr));
        } else {
            // TODO: No peer found to sync.. emit event.
        }
    }
fn sync < T : BlockTree > ( & mut self , now : LocalTime , tree : & T ) { if self . peers . is_empty ( ) { return ; } if self . is_synced ( now , tree ) { let ( tip , _ ) = tree . tip ( ) ; let height = tree . height ( ) ; self . upstream . event ( Event :: Synced ( tip , height ) ) ; // If we think we're in sync and we haven't asked other peers in a while, then // sample their headers just to make sure we're on the right chain. if self . last_peer_sample . map ( | t | now . duration_since ( t ) >= PEER_SAMPLE_INTERVAL ) . unwrap_or ( true ) { self . last_peer_sample = Some ( now ) ; self . sample_peers ( now , tree ) ; } return ; } // It looks like we're out of sync... let locators = ( tree . locator_hashes ( tree . height ( ) ) , BlockHash :: default ( ) ) ; // If we're already fetching these headers, just wait. if self . syncing ( & locators ) { return ; } if let Some ( peer ) = self . random_sync_candidate ( & locators . 0 , tree ) { let timeout = self . config . request_timeout ; let addr = peer . id ; self . request ( addr , locators , now , timeout , OnTimeout :: Ignore ) ; self . upstream . event ( Event :: Syncing ( addr ) ) ; } else { // TODO: No peer found to sync.. emit event. } }
Start syncing if we're out of sync.
Start syncing if we ' re out of sync .
&mut self
now
LocalTime
tree
&T