Skip to content

Commit

Permalink
Rename Params::pow_limit to max_attainable_target
Browse files Browse the repository at this point in the history
The maximum "attainable" target is a `rust-bitcoin` thing, Core use max
unattainable.

Deprecated the `Params::pow_limit` field and add a new field
`max_attainable_target`.

The `Params` type is `non_exhaustive` so this is not an API breaking
change.
  • Loading branch information
tcharding committed Apr 2, 2024
1 parent f0f6d3f commit 4121c9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion bitcoin/src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//!

use crate::network::Network;
#[cfg(doc)]
use crate::pow::CompactTarget;
use crate::pow::Target;

/// Parameters that influence chain consensus.
Expand All @@ -30,14 +32,20 @@ pub struct Params {
/// Number of blocks with the same set of rules.
pub miner_confirmation_window: u32,
/// Proof of work limit value. It contains the lowest possible difficulty.
#[deprecated(since = "TBD", note = "field renamed to max_attainable_target")]
pub pow_limit: Target,
/// The maximum **attainable** target value for these params.
///
/// Not all target values are attainable because consensus code uses the compact format to
/// represent targets (see [`CompactTarget`]).
///
/// Note that this value differs from Bitcoin Core's powLimit field in that this value is
/// attainable, but Bitcoin Core's is not. Specifically, because targets in Bitcoin are always
/// rounded to the nearest float expressible in "compact form", not all targets are attainable.
/// Still, this should not affect consensus as the only place where the non-compact form of
/// this is used in Bitcoin Core's consensus algorithm is in comparison and there are no
/// compact-expressible values between Bitcoin Core's and the limit expressed here.
pub pow_limit: Target,
pub max_attainable_target: Target,
/// Expected amount of time to mine one block.
pub pow_target_spacing: u64,
/// Difficulty recalculation interval.
Expand All @@ -62,6 +70,7 @@ impl Params {
rule_change_activation_threshold: 1916, // 95%
miner_confirmation_window: 2016,
pow_limit: Target::MAX_ATTAINABLE_MAINNET,
max_attainable_target: Target::MAX_ATTAINABLE_MAINNET,
pow_target_spacing: 10 * 60, // 10 minutes.
pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
allow_min_difficulty_blocks: false,
Expand All @@ -78,6 +87,7 @@ impl Params {
rule_change_activation_threshold: 1512, // 75%
miner_confirmation_window: 2016,
pow_limit: Target::MAX_ATTAINABLE_TESTNET,
max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
pow_target_spacing: 10 * 60, // 10 minutes.
pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
allow_min_difficulty_blocks: true,
Expand All @@ -94,6 +104,7 @@ impl Params {
rule_change_activation_threshold: 1916, // 95%
miner_confirmation_window: 2016,
pow_limit: Target::MAX_ATTAINABLE_SIGNET,
max_attainable_target: Target::MAX_ATTAINABLE_SIGNET,
pow_target_spacing: 10 * 60, // 10 minutes.
pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
allow_min_difficulty_blocks: false,
Expand All @@ -110,6 +121,7 @@ impl Params {
rule_change_activation_threshold: 108, // 75%
miner_confirmation_window: 144,
pow_limit: Target::MAX_ATTAINABLE_REGTEST,
max_attainable_target: Target::MAX_ATTAINABLE_REGTEST,
pow_target_spacing: 10 * 60, // 10 minutes.
pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
allow_min_difficulty_blocks: true,
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl Target {
// Panic here may be eaiser to debug than during the actual division.
assert_ne!(self.0, U256::ZERO, "divide by zero");

let max = params.as_ref().pow_limit;
let max = params.as_ref().max_attainable_target;
let d = max.0 / self.0;
d.saturating_to_u128()
}
Expand Down

0 comments on commit 4121c9a

Please sign in to comment.