Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
thomask7b committed Nov 12, 2024
1 parent 9458e99 commit d435eae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/bitcoin/d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ impl SyncProgress {
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,16 @@ 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);
}
}
13 changes: 7 additions & 6 deletions src/bitcoin/d/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use miniscript::bitcoin;
/// 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,13 @@ 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.9998), 0.9998);
assert_eq!(roundup_progress(0.99998), 1.0);
assert_eq!(roundup_progress(0.99991), 1.0);
}
}

0 comments on commit d435eae

Please sign in to comment.