bitcoind: move roundup_progress() to utils and unit test it
This commit is contained in:
parent
88c807d599
commit
4560bc9ed2
@ -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)]
|
||||
|
||||
@ -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<Fh, Fs>(
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user