Skip to content

Commit 3c9bbca

Browse files
wpaulinotcharding
authored andcommitted
Expose valid (min, max) difficulty transition thresholds
Once `U256` was made private, we lost the ability to check whether a valid difficulty transition was made in the chain, since `Target` no longer exposes any arithmetic operations.
1 parent 4642021 commit 3c9bbca

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

bitcoin/src/pow.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ impl Target {
235235
/// [`difficulty`]: Target::difficulty
236236
#[cfg_attr(all(test, mutate), mutate)]
237237
pub fn difficulty_float(&self) -> f64 { TARGET_MAX_F64 / self.0.to_f64() }
238+
239+
/// Computes the minimum valid [`Target`] threshold allowed for a block in which a difficulty
240+
/// adjustment occurs.
241+
///
242+
/// The difficulty can only decrease or increase by a factor of 4 max on each difficulty
243+
/// adjustment period.
244+
pub fn min_difficulty_transition_threshold(&self) -> Self { Self(self.0 >> 2) }
245+
246+
/// Computes the maximum valid [`Target`] threshold allowed for a block in which a difficulty
247+
/// adjustment occurs.
248+
///
249+
/// The difficulty can only decrease or increase by a factor of 4 max on each difficulty
250+
/// adjustment period.
251+
pub fn max_difficulty_transition_threshold(&self) -> Self { Self(self.0 << 2) }
238252
}
239253
do_impl!(Target);
240254

0 commit comments

Comments
 (0)