Merge rust-bitcoin/rust-bitcoin#2816: make difficulty_float general to all network
fcf27a62a2
update api (Divyansh Gupta)531aba0cf1
make `difficulty_float` general to all network (Divyansh Gupta) Pull request description: Fix #2783 ACKs for top commit: tcharding: ACKfcf27a62a2
apoelstra: ACKfcf27a62a2
Tree-SHA512: aec9648b057677b89f4f397e1fa703ed6496436bd6e1e8052d5b7f52ef52c95b286babd425d97fe7d4f903cd0a3d68ae589d8fb255fc68f423714918cd485873
This commit is contained in:
commit
c4f2f2454f
|
@ -7602,7 +7602,7 @@ pub fn bitcoin::blockdata::block::Header::consensus_decode_from_finite_reader<R:
|
|||
pub fn bitcoin::blockdata::block::Header::consensus_encode<R: bitcoin_io::Write + core::marker::Sized>(&self, r: &mut R) -> core::result::Result<usize, bitcoin_io::error::Error>
|
||||
pub fn bitcoin::blockdata::block::Header::deserialize<__D>(__deserializer: __D) -> core::result::Result<Self, <__D as serde::de::Deserializer>::Error> where __D: serde::de::Deserializer<'de>
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::eq(&self, other: &bitcoin::blockdata::block::Header) -> bool
|
||||
pub fn bitcoin::blockdata::block::Header::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::blockdata::block::Header::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
|
||||
|
@ -9087,7 +9087,7 @@ pub fn bitcoin::pow::Target::clone(&self) -> bitcoin::pow::Target
|
|||
pub fn bitcoin::pow::Target::cmp(&self, other: &bitcoin::pow::Target) -> core::cmp::Ordering
|
||||
pub fn bitcoin::pow::Target::deserialize<__D>(__deserializer: __D) -> core::result::Result<Self, <__D as serde::de::Deserializer>::Error> where __D: serde::de::Deserializer<'de>
|
||||
pub fn bitcoin::pow::Target::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::pow::Target::eq(&self, other: &bitcoin::pow::Target) -> bool
|
||||
pub fn bitcoin::pow::Target::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::pow::Target::from(c: bitcoin::pow::CompactTarget) -> Self
|
||||
|
|
|
@ -7239,7 +7239,7 @@ pub fn bitcoin::blockdata::block::Header::consensus_decode<R: bitcoin_io::BufRea
|
|||
pub fn bitcoin::blockdata::block::Header::consensus_decode_from_finite_reader<R: bitcoin_io::BufRead + core::marker::Sized>(r: &mut R) -> core::result::Result<bitcoin::blockdata::block::Header, bitcoin::consensus::encode::Error>
|
||||
pub fn bitcoin::blockdata::block::Header::consensus_encode<R: bitcoin_io::Write + core::marker::Sized>(&self, r: &mut R) -> core::result::Result<usize, bitcoin_io::error::Error>
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::eq(&self, other: &bitcoin::blockdata::block::Header) -> bool
|
||||
pub fn bitcoin::blockdata::block::Header::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::blockdata::block::Header::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
|
||||
|
@ -8618,7 +8618,7 @@ pub fn bitcoin::pow::CompactTarget::to_consensus(self) -> u32
|
|||
pub fn bitcoin::pow::Target::clone(&self) -> bitcoin::pow::Target
|
||||
pub fn bitcoin::pow::Target::cmp(&self, other: &bitcoin::pow::Target) -> core::cmp::Ordering
|
||||
pub fn bitcoin::pow::Target::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::pow::Target::eq(&self, other: &bitcoin::pow::Target) -> bool
|
||||
pub fn bitcoin::pow::Target::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::pow::Target::from(c: bitcoin::pow::CompactTarget) -> Self
|
||||
|
|
|
@ -6600,7 +6600,7 @@ pub fn bitcoin::blockdata::block::Header::consensus_decode<R: bitcoin_io::BufRea
|
|||
pub fn bitcoin::blockdata::block::Header::consensus_decode_from_finite_reader<R: bitcoin_io::BufRead + core::marker::Sized>(r: &mut R) -> core::result::Result<bitcoin::blockdata::block::Header, bitcoin::consensus::encode::Error>
|
||||
pub fn bitcoin::blockdata::block::Header::consensus_encode<R: bitcoin_io::Write + core::marker::Sized>(&self, r: &mut R) -> core::result::Result<usize, bitcoin_io::error::Error>
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::blockdata::block::Header::eq(&self, other: &bitcoin::blockdata::block::Header) -> bool
|
||||
pub fn bitcoin::blockdata::block::Header::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::blockdata::block::Header::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
|
||||
|
@ -7763,7 +7763,7 @@ pub fn bitcoin::pow::CompactTarget::to_consensus(self) -> u32
|
|||
pub fn bitcoin::pow::Target::clone(&self) -> bitcoin::pow::Target
|
||||
pub fn bitcoin::pow::Target::cmp(&self, other: &bitcoin::pow::Target) -> core::cmp::Ordering
|
||||
pub fn bitcoin::pow::Target::difficulty(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> u128
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self) -> f64
|
||||
pub fn bitcoin::pow::Target::difficulty_float(&self, params: impl core::convert::AsRef<bitcoin::consensus::params::Params>) -> f64
|
||||
pub fn bitcoin::pow::Target::eq(&self, other: &bitcoin::pow::Target) -> bool
|
||||
pub fn bitcoin::pow::Target::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
|
||||
pub fn bitcoin::pow::Target::from(c: bitcoin::pow::CompactTarget) -> Self
|
||||
|
|
|
@ -98,7 +98,9 @@ impl Header {
|
|||
}
|
||||
|
||||
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
|
||||
pub fn difficulty_float(&self) -> f64 { self.target().difficulty_float() }
|
||||
pub fn difficulty_float(&self, params: impl AsRef<Params>) -> f64 {
|
||||
self.target().difficulty_float(params)
|
||||
}
|
||||
|
||||
/// Checks that the proof-of-work for the block is valid, returning the block hash.
|
||||
pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, ValidationError> {
|
||||
|
@ -541,7 +543,7 @@ mod tests {
|
|||
real_decode.block_hash()
|
||||
);
|
||||
assert_eq!(real_decode.header.difficulty(¶ms), 1);
|
||||
assert_eq!(real_decode.header.difficulty_float(), 1.0);
|
||||
assert_eq!(real_decode.header.difficulty_float(¶ms), 1.0);
|
||||
|
||||
assert_eq!(real_decode.total_size(), some_block.len());
|
||||
assert_eq!(real_decode.base_size(), some_block.len());
|
||||
|
@ -583,7 +585,7 @@ mod tests {
|
|||
real_decode.block_hash()
|
||||
);
|
||||
assert_eq!(real_decode.header.difficulty(¶ms), 2456598);
|
||||
assert_eq!(real_decode.header.difficulty_float(), 2456598.4399242126);
|
||||
assert_eq!(real_decode.header.difficulty_float(¶ms), 2456598.4399242126);
|
||||
|
||||
assert_eq!(real_decode.total_size(), segwit_block.len());
|
||||
assert_eq!(real_decode.base_size(), 4283);
|
||||
|
|
|
@ -267,13 +267,20 @@ impl Target {
|
|||
///
|
||||
/// See [`difficulty`] for details.
|
||||
///
|
||||
/// # Returns
|
||||
/// # Panics
|
||||
///
|
||||
/// Returns [`f64::INFINITY`] if `self` is zero (caused by divide by zero).
|
||||
/// Panics if `self` is zero (divide by zero).
|
||||
///
|
||||
/// [`difficulty`]: Target::difficulty
|
||||
#[cfg_attr(all(test, mutate), mutate)]
|
||||
pub fn difficulty_float(&self) -> f64 { TARGET_MAX_F64 / self.0.to_f64() }
|
||||
pub fn difficulty_float(&self, params: impl AsRef<Params>) -> f64 {
|
||||
// We want to explicitly panic to be uniform with `difficulty()`
|
||||
// (float division by zero does not panic).
|
||||
// Note, target 0 is basically impossible to obtain by any "normal" means.
|
||||
assert_ne!(self.0, U256::ZERO, "divide by zero");
|
||||
let max = params.as_ref().max_attainable_target;
|
||||
max.0.to_f64() / self.0.to_f64()
|
||||
}
|
||||
|
||||
/// Computes the minimum valid [`Target`] threshold allowed for a block in which a difficulty
|
||||
/// adjustment occurs.
|
||||
|
@ -899,10 +906,6 @@ impl U256 {
|
|||
}
|
||||
}
|
||||
|
||||
// Target::MAX as a float value. Calculated with U256::to_f64.
|
||||
// This is validated in the unit tests as well.
|
||||
const TARGET_MAX_F64: f64 = 2.695953529101131e67;
|
||||
|
||||
impl<T: Into<u128>> From<T> for U256 {
|
||||
fn from(x: T) -> Self { U256(0, x.into()) }
|
||||
}
|
||||
|
@ -1916,17 +1919,22 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn target_difficulty_float() {
|
||||
assert_eq!(Target::MAX.difficulty_float(), 1.0_f64);
|
||||
let params = Params::new(crate::Network::Bitcoin);
|
||||
|
||||
assert_eq!(Target::MAX.difficulty_float(¶ms), 1.0_f64);
|
||||
assert_eq!(
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1c00ffff_u32)).difficulty_float(),
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1c00ffff_u32))
|
||||
.difficulty_float(¶ms),
|
||||
256.0_f64
|
||||
);
|
||||
assert_eq!(
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1b00ffff_u32)).difficulty_float(),
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1b00ffff_u32))
|
||||
.difficulty_float(¶ms),
|
||||
65536.0_f64
|
||||
);
|
||||
assert_eq!(
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1a00f3a2_u32)).difficulty_float(),
|
||||
Target::from_compact(CompactTarget::from_consensus(0x1a00f3a2_u32))
|
||||
.difficulty_float(¶ms),
|
||||
17628585.065897066_f64
|
||||
);
|
||||
}
|
||||
|
@ -2041,8 +2049,6 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn u256_to_f64() {
|
||||
// Validate that the Target::MAX value matches the constant also used in difficulty calculation.
|
||||
assert_eq!(Target::MAX.0.to_f64(), TARGET_MAX_F64);
|
||||
assert_eq!(U256::ZERO.to_f64(), 0.0_f64);
|
||||
assert_eq!(U256::ONE.to_f64(), 1.0_f64);
|
||||
assert_eq!(U256::MAX.to_f64(), 1.157920892373162e77_f64);
|
||||
|
|
Loading…
Reference in New Issue