Fix blockchain sync precision, add tests
This commit is contained in:
parent
f3206f9dfa
commit
439af752a5
@ -1256,13 +1256,13 @@ impl SyncProgress {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the verification progress, roundup up to to three decimal places. This will not return
|
||||
/// Get the verification progress, roundup up to four decimal places. This will not return
|
||||
/// 1.0 (ie 100% verification progress) until the verification is complete.
|
||||
pub fn rounded_up_progress(&self) -> f64 {
|
||||
let progress = roundup_progress(self.percentage);
|
||||
if progress == 1.0 && self.blocks != self.headers {
|
||||
// Don't return a 100% progress until we are actually done syncing.
|
||||
0.999
|
||||
0.9999
|
||||
} else {
|
||||
progress
|
||||
}
|
||||
@ -1554,3 +1554,42 @@ impl From<&&Json> for MempoolEntryFees {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_rounded_up_progress() {
|
||||
assert_eq!(
|
||||
SyncProgress::new(0.6, 1_000, 1_000).rounded_up_progress(),
|
||||
0.6
|
||||
);
|
||||
assert_eq!(
|
||||
SyncProgress::new(0.67891, 1_000, 1_000).rounded_up_progress(),
|
||||
0.6789
|
||||
);
|
||||
assert_eq!(
|
||||
SyncProgress::new(0.99991, 1_000, 1_000).rounded_up_progress(),
|
||||
1.0
|
||||
);
|
||||
assert_eq!(
|
||||
SyncProgress::new(1.2, 1_000, 1_000).rounded_up_progress(),
|
||||
1.0
|
||||
);
|
||||
assert_eq!(
|
||||
SyncProgress::new(1.0, 1_000, 999).rounded_up_progress(),
|
||||
0.9999
|
||||
);
|
||||
// approximatively year 2198
|
||||
assert_eq!(
|
||||
SyncProgress::new(1.0, 9999999, 9999998).rounded_up_progress(),
|
||||
0.9999
|
||||
);
|
||||
//bug or corrupted bitcond (blocks > headers)
|
||||
assert_eq!(
|
||||
SyncProgress::new(1.0, 999998, 999999).rounded_up_progress(),
|
||||
0.9999
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,13 +2,13 @@ use crate::bitcoin::{d::BlockStats, BlockChainTip};
|
||||
|
||||
use miniscript::bitcoin;
|
||||
|
||||
/// Truncate the sync progress, rounding it up if it gets above 0.999. Note this also caps the
|
||||
/// Truncate the sync progress, rounding it up if it gets above 0.9999. Note this also caps the
|
||||
/// progress to 1.0, as bitcoind could temporarily return value >1.0 in getblockchaininfo's
|
||||
/// "verificationprogress" field.
|
||||
/// Bitcoind uses a guess for the value of verificationprogress. It will eventually get to
|
||||
/// be 1, and we want to be less conservative.
|
||||
pub fn roundup_progress(progress: f64) -> f64 {
|
||||
let precision = 10u64.pow(3) as f64;
|
||||
let precision = 10u64.pow(4) as f64;
|
||||
let progress_rounded = (progress * precision + 1.0) as u64;
|
||||
|
||||
if progress_rounded >= precision as u64 {
|
||||
@ -371,12 +371,14 @@ mod tests {
|
||||
#[test]
|
||||
fn bitcoind_roundup_progress() {
|
||||
assert_eq!(roundup_progress(0.6), 0.6);
|
||||
assert_eq!(roundup_progress(0.67891), 0.678);
|
||||
assert_eq!(roundup_progress(0.67891), 0.6789);
|
||||
assert_eq!(roundup_progress(0.98), 0.98);
|
||||
assert_eq!(roundup_progress(0.998), 0.998);
|
||||
assert_eq!(roundup_progress(0.9476), 0.947);
|
||||
assert_eq!(roundup_progress(0.998), 0.998);
|
||||
assert_eq!(roundup_progress(0.9998), 1.0);
|
||||
assert_eq!(roundup_progress(0.9991), 1.0);
|
||||
assert_eq!(roundup_progress(0.9476), 0.9476);
|
||||
assert_eq!(roundup_progress(0.94761), 0.9476);
|
||||
assert_eq!(roundup_progress(0.94769), 0.9476);
|
||||
assert_eq!(roundup_progress(0.9998), 0.9998);
|
||||
assert_eq!(roundup_progress(0.99998), 1.0);
|
||||
assert_eq!(roundup_progress(0.99991), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user