Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blockchain sync progress #1457

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR:

Suggested change
if progress == 1.0 && self.blocks != self.headers {
if progress == 1.0 && self.blocks < self.headers {

@jp1ac4 i don't think this is likely to happen as iiuc as of now it's only used w/ a bitcoind backend, but wdyt we should do if self.blocks > self.headers? we actually return 0.999 but maybe 1.0 should be more relevant? (i'm not sure from a security POV, if returning 0.999 i guess we will stuck in the syncing process while if returning 1.0 we will assumed we are synced)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure really but I guess if they're not equal in either direction then something still needs to happen in terms of syncing.

// 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can add this test cases?:

        // 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
        );

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to include an additional test to show that numbers below 0.9999 are truncated rather than rounded up, e.g.

diff --git a/src/bitcoin/d/utils.rs b/src/bitcoin/d/utils.rs
index c317172b..ddd6d8af 100644
--- a/src/bitcoin/d/utils.rs
+++ b/src/bitcoin/d/utils.rs
@@ -376,6 +376,7 @@ mod tests {
         assert_eq!(roundup_progress(0.998), 0.998);
         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);

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