Skip to content

Commit

Permalink
Merge #1457: Fix blockchain sync progress
Browse files Browse the repository at this point in the history
439af75 Fix blockchain sync precision, add tests (thomas)

Pull request description:

  I've merged `roundup_progress` in `rounded_up_progress` since was in utils and was no longer used by any other method.
  I also added some tests to verify the precision's update but also for specific cases that we manage. By the way tests has been moved in the related file.

  Solves #907

ACKs for top commit:
  pythcoiner:
    ACK 439af75
  jp1ac4:
    Tested ACK 439af75.

Tree-SHA512: a87998d1e25c47d751dabc38f641f05f2544bf17cbdcf95d24e829a0aa9dd8ce2de3bdc72a52aced04ffd777412ca206641f914d9a083241d98ceea33d4c10cc
  • Loading branch information
edouardparis committed Nov 13, 2024
2 parents 387f431 + 439af75 commit b46efc6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
43 changes: 41 additions & 2 deletions src/bitcoin/d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
);
}
}
16 changes: 9 additions & 7 deletions src/bitcoin/d/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b46efc6

Please sign in to comment.