mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-08 12:21:09 +03:00
introduce some minimal sync state (#978)
* commit * rustfmt * only fast sync once then treat as full sync * commit * add some debug logging so we can track progress when verifying rangeproofs * rustfmt
This commit is contained in:
parent
a8cc58bb89
commit
b7e29fee55
3 changed files with 64 additions and 29 deletions
|
@ -792,6 +792,13 @@ impl<'a> Extension<'a> {
|
||||||
return Err(Error::OutputNotFound);
|
return Err(Error::OutputNotFound);
|
||||||
}
|
}
|
||||||
proof_count += 1;
|
proof_count += 1;
|
||||||
|
|
||||||
|
if proof_count % 500 == 0 {
|
||||||
|
debug!(
|
||||||
|
LOGGER,
|
||||||
|
"txhashset: verify_rangeproofs: verified {} rangeproofs", proof_count,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,7 +199,9 @@ impl Server {
|
||||||
Some(b) => b,
|
Some(b) => b,
|
||||||
};
|
};
|
||||||
|
|
||||||
sync::run_sync(
|
let syncer = sync::Syncer::new();
|
||||||
|
|
||||||
|
syncer.run_sync(
|
||||||
currently_syncing.clone(),
|
currently_syncing.clone(),
|
||||||
awaiting_peers.clone(),
|
awaiting_peers.clone(),
|
||||||
p2p_server.peers.clone(),
|
p2p_server.peers.clone(),
|
||||||
|
|
|
@ -19,13 +19,43 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use time;
|
use time;
|
||||||
|
|
||||||
use chain;
|
use chain;
|
||||||
|
use common::types::Error;
|
||||||
use core::core::hash::{Hash, Hashed};
|
use core::core::hash::{Hash, Hashed};
|
||||||
use core::core::target::Difficulty;
|
use core::core::target::Difficulty;
|
||||||
use core::global;
|
use core::global;
|
||||||
|
use grin::sync;
|
||||||
use p2p::{self, Peer, Peers};
|
use p2p::{self, Peer, Peers};
|
||||||
use common::types::Error;
|
|
||||||
use util::LOGGER;
|
use util::LOGGER;
|
||||||
|
|
||||||
|
pub struct Syncer {}
|
||||||
|
|
||||||
|
impl Syncer {
|
||||||
|
pub fn new() -> Syncer {
|
||||||
|
Syncer {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_sync(
|
||||||
|
&self,
|
||||||
|
currently_syncing: Arc<AtomicBool>,
|
||||||
|
awaiting_peers: Arc<AtomicBool>,
|
||||||
|
peers: Arc<p2p::Peers>,
|
||||||
|
chain: Arc<chain::Chain>,
|
||||||
|
skip_sync_wait: bool,
|
||||||
|
archive_mode: bool,
|
||||||
|
stop: Arc<AtomicBool>,
|
||||||
|
) {
|
||||||
|
sync::run_sync(
|
||||||
|
currently_syncing,
|
||||||
|
awaiting_peers,
|
||||||
|
peers,
|
||||||
|
chain,
|
||||||
|
skip_sync_wait,
|
||||||
|
archive_mode,
|
||||||
|
stop,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Starts the syncing loop, just spawns two threads that loop forever
|
/// Starts the syncing loop, just spawns two threads that loop forever
|
||||||
pub fn run_sync(
|
pub fn run_sync(
|
||||||
currently_syncing: Arc<AtomicBool>,
|
currently_syncing: Arc<AtomicBool>,
|
||||||
|
@ -75,30 +105,28 @@ pub fn run_sync(
|
||||||
si.highest_height = most_work_height;
|
si.highest_height = most_work_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
// in archival nodes (no fast sync) we just consider we have the whole
|
if syncing {
|
||||||
// state already, then fast sync triggers if other peers are much
|
|
||||||
// further ahead
|
|
||||||
let fast_sync_enabled =
|
let fast_sync_enabled =
|
||||||
!archive_mode && si.highest_height.saturating_sub(head.height) > horizon;
|
!archive_mode && si.highest_height.saturating_sub(head.height) > horizon;
|
||||||
|
|
||||||
if syncing {
|
|
||||||
// run the header sync every 10s
|
// run the header sync every 10s
|
||||||
if si.header_sync_due(&header_head) {
|
if si.header_sync_due(&header_head) {
|
||||||
header_sync(peers.clone(), chain.clone());
|
header_sync(peers.clone(), chain.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// run the body_sync every 5s
|
if fast_sync_enabled {
|
||||||
if !fast_sync_enabled && si.body_sync_due(&head) {
|
|
||||||
body_sync(peers.clone(), chain.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
// run fast sync if applicable, every 5 min
|
// run fast sync if applicable, every 5 min
|
||||||
if fast_sync_enabled && header_head.height == si.highest_height
|
if header_head.height == si.highest_height && si.fast_sync_due() {
|
||||||
&& si.fast_sync_due()
|
|
||||||
{
|
|
||||||
fast_sync(peers.clone(), chain.clone(), &header_head);
|
fast_sync(peers.clone(), chain.clone(), &header_head);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// run the body_sync every 5s
|
||||||
|
if si.body_sync_due(&head) {
|
||||||
|
body_sync(peers.clone(), chain.clone());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
currently_syncing.store(syncing, Ordering::Relaxed);
|
currently_syncing.store(syncing, Ordering::Relaxed);
|
||||||
|
|
||||||
thread::sleep(Duration::from_secs(1));
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
@ -265,10 +293,6 @@ fn needs_syncing(
|
||||||
if is_syncing {
|
if is_syncing {
|
||||||
if let Some(peer) = peer {
|
if let Some(peer) = peer {
|
||||||
if let Ok(peer) = peer.try_read() {
|
if let Ok(peer) = peer.try_read() {
|
||||||
debug!(
|
|
||||||
LOGGER,
|
|
||||||
"needs_syncing {} {}", local_diff, peer.info.total_difficulty
|
|
||||||
);
|
|
||||||
most_work_height = peer.info.height;
|
most_work_height = peer.info.height;
|
||||||
|
|
||||||
if peer.info.total_difficulty <= local_diff {
|
if peer.info.total_difficulty <= local_diff {
|
||||||
|
@ -360,7 +384,7 @@ fn get_locator_heights(height: u64) -> Vec<u64> {
|
||||||
struct SyncInfo {
|
struct SyncInfo {
|
||||||
prev_body_sync: (time::Tm, u64),
|
prev_body_sync: (time::Tm, u64),
|
||||||
prev_header_sync: (time::Tm, u64),
|
prev_header_sync: (time::Tm, u64),
|
||||||
prev_fast_sync: time::Tm,
|
prev_fast_sync: Option<time::Tm>,
|
||||||
highest_height: u64,
|
highest_height: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,7 +394,7 @@ impl SyncInfo {
|
||||||
SyncInfo {
|
SyncInfo {
|
||||||
prev_body_sync: (now.clone(), 0),
|
prev_body_sync: (now.clone(), 0),
|
||||||
prev_header_sync: (now.clone(), 0),
|
prev_header_sync: (now.clone(), 0),
|
||||||
prev_fast_sync: now.clone() - time::Duration::seconds(5 * 60),
|
prev_fast_sync: None,
|
||||||
highest_height: 0,
|
highest_height: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -399,15 +423,17 @@ impl SyncInfo {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For now this is a one-time thing (it can be slow) at initial startup.
|
||||||
fn fast_sync_due(&mut self) -> bool {
|
fn fast_sync_due(&mut self) -> bool {
|
||||||
|
if let None = self.prev_fast_sync {
|
||||||
let now = time::now_utc();
|
let now = time::now_utc();
|
||||||
if now - self.prev_fast_sync > time::Duration::seconds(5 * 60) {
|
self.prev_fast_sync = Some(now);
|
||||||
self.prev_fast_sync = now;
|
true
|
||||||
return true;
|
} else {
|
||||||
}
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
Loading…
Reference in a new issue