From 4560bc9ed2e41e81797cadfdecb5a97a6d351850 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Wed, 14 Dec 2022 16:40:05 +0100 Subject: [PATCH 1/2] bitcoind: move roundup_progress() to utils and unit test it --- src/bitcoin/d/mod.rs | 14 +------------- src/bitcoin/d/utils.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/bitcoin/d/mod.rs b/src/bitcoin/d/mod.rs index 2978564e..6679edd8 100644 --- a/src/bitcoin/d/mod.rs +++ b/src/bitcoin/d/mod.rs @@ -7,7 +7,7 @@ use crate::{ config, descriptors::MultipathDescriptor, }; -use utils::block_before_date; +use utils::{block_before_date, roundup_progress}; use std::{ cmp, collections::HashSet, convert::TryInto, fs, io, str::FromStr, thread, time::Duration, @@ -959,18 +959,6 @@ impl BitcoinD { ) } } -// Bitcoind uses a guess for the value of verificationprogress. It will eventually get to -// be 1, and we want to be less conservative. -fn roundup_progress(progress: f64) -> f64 { - let precision = 10u64.pow(5) as f64; - let progress_rounded = (progress * precision + 1.0) as u64; - - if progress_rounded >= precision as u64 { - 1.0 - } else { - (progress_rounded as f64 / precision) as f64 - } -} /// An entry in the 'listdescriptors' result. #[derive(Debug, Clone)] diff --git a/src/bitcoin/d/utils.rs b/src/bitcoin/d/utils.rs index b6e2a330..05b4fbd3 100644 --- a/src/bitcoin/d/utils.rs +++ b/src/bitcoin/d/utils.rs @@ -2,6 +2,22 @@ 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 +/// 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(5) as f64; + let progress_rounded = (progress * precision + 1.0) as u64; + + if progress_rounded >= precision as u64 { + 1.0 + } else { + (progress * precision) as u64 as f64 / precision + } +} + // As a standalone function to unit test it. /// Get the last block of the chain before the given date by performing a binary search. pub fn block_before_date( @@ -346,4 +362,16 @@ mod tests { } ); } + + #[test] + fn bitcoind_roundup_progress() { + assert_eq!(roundup_progress(0.6), 0.6); + assert_eq!(roundup_progress(0.67891), 0.67891); + assert_eq!(roundup_progress(0.98), 0.98); + assert_eq!(roundup_progress(0.998), 0.998); + assert_eq!(roundup_progress(0.9997), 0.9997); + assert_eq!(roundup_progress(0.9476), 0.9476); + assert_eq!(roundup_progress(0.99998), 0.99998); + assert_eq!(roundup_progress(0.999998), 1.0); + } } From 030bfadaf7ef5e88500f6f64ea814d3656b07fba Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Wed, 14 Dec 2022 16:47:21 +0100 Subject: [PATCH 2/2] bitcoind: decrease the sync progress roundup threshold --- src/bitcoin/d/utils.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bitcoin/d/utils.rs b/src/bitcoin/d/utils.rs index 05b4fbd3..2bc957cf 100644 --- a/src/bitcoin/d/utils.rs +++ b/src/bitcoin/d/utils.rs @@ -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(5) as f64; + let precision = 10u64.pow(3) as f64; let progress_rounded = (progress * precision + 1.0) as u64; if progress_rounded >= precision as u64 { @@ -366,12 +366,12 @@ mod tests { #[test] fn bitcoind_roundup_progress() { assert_eq!(roundup_progress(0.6), 0.6); - assert_eq!(roundup_progress(0.67891), 0.67891); + assert_eq!(roundup_progress(0.67891), 0.678); assert_eq!(roundup_progress(0.98), 0.98); assert_eq!(roundup_progress(0.998), 0.998); - assert_eq!(roundup_progress(0.9997), 0.9997); - assert_eq!(roundup_progress(0.9476), 0.9476); - assert_eq!(roundup_progress(0.99998), 0.99998); - assert_eq!(roundup_progress(0.999998), 1.0); + 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); } }