From 100ce03643efc51f83341825b1b726c8b3084aa3 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 12 Aug 2024 20:17:43 +1000 Subject: [PATCH] Run cargo +nightly fmt No manual changes. --- bitcoin/src/pow.rs | 172 ++++++++++++++++++++++----------------------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index 32a47a9b3..2afc5b8c2 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -358,96 +358,96 @@ impl CompactTarget { mod tmp { use super::*; -impl CompactTarget { - /// Creates a `CompactTarget` from a prefixed hex string. - pub fn from_hex(s: &str) -> Result { - let target = parse::hex_u32_prefixed(s)?; - Ok(Self::from_consensus(target)) - } - - /// Creates a `CompactTarget` from an unprefixed hex string. - pub fn from_unprefixed_hex(s: &str) -> Result { - let target = parse::hex_u32_unprefixed(s)?; - Ok(Self::from_consensus(target)) - } - - /// Computes the [`CompactTarget`] from a difficulty adjustment. - /// - /// ref: - /// - /// Given the previous Target, represented as a [`CompactTarget`], the difficulty is adjusted - /// by taking the timespan between them, and multipling the current [`CompactTarget`] by a factor - /// of the net timespan and expected timespan. The [`CompactTarget`] may not adjust by more than - /// a factor of 4, or adjust beyond the maximum threshold for the network. - /// - /// # Note - /// - /// Under the consensus rules, the difference in the number of blocks between the headers does - /// not equate to the `difficulty_adjustment_interval` of [`Params`]. This is due to an off-by-one - /// error, and, the expected number of blocks in between headers is `difficulty_adjustment_interval - 1` - /// when calculating the difficulty adjustment. - /// - /// Take the example of the first difficulty adjustment. Block 2016 introduces a new [`CompactTarget`], - /// which takes the net timespan between Block 2015 and Block 0, and recomputes the difficulty. - /// - /// # Returns - /// - /// The expected [`CompactTarget`] recalculation. - pub fn from_next_work_required( - last: CompactTarget, - timespan: u64, - params: impl AsRef, - ) -> CompactTarget { - let params = params.as_ref(); - if params.no_pow_retargeting { - return last; + impl CompactTarget { + /// Creates a `CompactTarget` from a prefixed hex string. + pub fn from_hex(s: &str) -> Result { + let target = parse::hex_u32_prefixed(s)?; + Ok(Self::from_consensus(target)) } - // Comments relate to the `pow.cpp` file from Core. - // ref: - let min_timespan = params.pow_target_timespan >> 2; // Lines 56/57 - let max_timespan = params.pow_target_timespan << 2; // Lines 58/59 - let actual_timespan = timespan.clamp(min_timespan, max_timespan); - let prev_target: Target = last.into(); - let maximum_retarget = prev_target.max_transition_threshold(params); // bnPowLimit - let retarget = prev_target.0; // bnNew - let retarget = retarget.mul(actual_timespan.into()); - let retarget = retarget.div(params.pow_target_timespan.into()); - let retarget = Target(retarget); - if retarget.ge(&maximum_retarget) { - return maximum_retarget.to_compact_lossy(); - } - retarget.to_compact_lossy() - } - /// Computes the [`CompactTarget`] from a difficulty adjustment, - /// assuming these are the relevant block headers. - /// - /// Given two headers, representing the start and end of a difficulty adjustment epoch, - /// compute the [`CompactTarget`] based on the net time between them and the current - /// [`CompactTarget`]. - /// - /// # Note - /// - /// See [`CompactTarget::from_next_work_required`] - /// - /// For example, to successfully compute the first difficulty adjustment on the Bitcoin network, - /// one would pass the header for Block 2015 as `current` and the header for Block 0 as - /// `last_epoch_boundary`. - /// - /// # Returns - /// - /// The expected [`CompactTarget`] recalculation. - pub fn from_header_difficulty_adjustment( - last_epoch_boundary: Header, - current: Header, - params: impl AsRef, - ) -> CompactTarget { - let timespan = current.time - last_epoch_boundary.time; - let bits = current.bits; - CompactTarget::from_next_work_required(bits, timespan.into(), params) + /// Creates a `CompactTarget` from an unprefixed hex string. + pub fn from_unprefixed_hex(s: &str) -> Result { + let target = parse::hex_u32_unprefixed(s)?; + Ok(Self::from_consensus(target)) + } + + /// Computes the [`CompactTarget`] from a difficulty adjustment. + /// + /// ref: + /// + /// Given the previous Target, represented as a [`CompactTarget`], the difficulty is adjusted + /// by taking the timespan between them, and multipling the current [`CompactTarget`] by a factor + /// of the net timespan and expected timespan. The [`CompactTarget`] may not adjust by more than + /// a factor of 4, or adjust beyond the maximum threshold for the network. + /// + /// # Note + /// + /// Under the consensus rules, the difference in the number of blocks between the headers does + /// not equate to the `difficulty_adjustment_interval` of [`Params`]. This is due to an off-by-one + /// error, and, the expected number of blocks in between headers is `difficulty_adjustment_interval - 1` + /// when calculating the difficulty adjustment. + /// + /// Take the example of the first difficulty adjustment. Block 2016 introduces a new [`CompactTarget`], + /// which takes the net timespan between Block 2015 and Block 0, and recomputes the difficulty. + /// + /// # Returns + /// + /// The expected [`CompactTarget`] recalculation. + pub fn from_next_work_required( + last: CompactTarget, + timespan: u64, + params: impl AsRef, + ) -> CompactTarget { + let params = params.as_ref(); + if params.no_pow_retargeting { + return last; + } + // Comments relate to the `pow.cpp` file from Core. + // ref: + let min_timespan = params.pow_target_timespan >> 2; // Lines 56/57 + let max_timespan = params.pow_target_timespan << 2; // Lines 58/59 + let actual_timespan = timespan.clamp(min_timespan, max_timespan); + let prev_target: Target = last.into(); + let maximum_retarget = prev_target.max_transition_threshold(params); // bnPowLimit + let retarget = prev_target.0; // bnNew + let retarget = retarget.mul(actual_timespan.into()); + let retarget = retarget.div(params.pow_target_timespan.into()); + let retarget = Target(retarget); + if retarget.ge(&maximum_retarget) { + return maximum_retarget.to_compact_lossy(); + } + retarget.to_compact_lossy() + } + + /// Computes the [`CompactTarget`] from a difficulty adjustment, + /// assuming these are the relevant block headers. + /// + /// Given two headers, representing the start and end of a difficulty adjustment epoch, + /// compute the [`CompactTarget`] based on the net time between them and the current + /// [`CompactTarget`]. + /// + /// # Note + /// + /// See [`CompactTarget::from_next_work_required`] + /// + /// For example, to successfully compute the first difficulty adjustment on the Bitcoin network, + /// one would pass the header for Block 2015 as `current` and the header for Block 0 as + /// `last_epoch_boundary`. + /// + /// # Returns + /// + /// The expected [`CompactTarget`] recalculation. + pub fn from_header_difficulty_adjustment( + last_epoch_boundary: Header, + current: Header, + params: impl AsRef, + ) -> CompactTarget { + let timespan = current.time - last_epoch_boundary.time; + let bits = current.bits; + CompactTarget::from_next_work_required(bits, timespan.into(), params) + } } } -} impl From for Target { fn from(c: CompactTarget) -> Self { Target::from_compact(c) }