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.
This commit is contained in:
Wilmer Paulino 2023-05-01 14:50:25 -07:00
parent cce8512d41
commit 8e6f953aa7
No known key found for this signature in database
GPG Key ID: 634FE5FC544DCA31
1 changed files with 14 additions and 0 deletions

View File

@ -234,6 +234,20 @@ impl Target {
/// [`difficulty`]: Target::difficulty
#[cfg_attr(all(test, mutate), mutate)]
pub fn difficulty_float(&self) -> f64 { TARGET_MAX_F64 / self.0.to_f64() }
/// Computes the minimum valid [`Target`] threshold allowed for a block in which a difficulty
/// adjustment occurs.
///
/// The difficulty can only decrease or increase by a factor of 4 max on each difficulty
/// adjustment period.
pub fn min_difficulty_transition_threshold(&self) -> Self { Self(self.0 >> 2) }
/// Computes the maximum valid [`Target`] threshold allowed for a block in which a difficulty
/// adjustment occurs.
///
/// The difficulty can only decrease or increase by a factor of 4 max on each difficulty
/// adjustment period.
pub fn max_difficulty_transition_threshold(&self) -> Self { Self(self.0 << 2) }
}
do_impl!(Target);