Struct Target
pub struct Target(/* private fields */);
Expand description
A 256 bit integer representing target.
The SHA-256 hash of a block’s header must be lower than or equal to the current target for the
block to be accepted by the network. The lower the target, the more difficult it is to generate
a block. (See also Work
.)
Implementations§
§impl Target
impl Target
pub const ZERO: Target = _
pub const ZERO: Target = _
When parsing nBits, Bitcoin Core converts a negative target threshold into a target of zero.
pub const MAX: Target = _
pub const MAX: Target = _
The maximum possible target.
This value is used to calculate difficulty, which is defined as how difficult the current target makes it to find a block relative to how difficult it would be at the highest possible target. Remember highest target == lowest difficulty.
pub const MAX_ATTAINABLE_MAINNET: Target = _
pub const MAX_ATTAINABLE_MAINNET: Target = _
The maximum attainable target value on mainnet.
Not all target values are attainable because consensus code uses the compact format to
represent targets (see CompactTarget
).
pub const MAX_ATTAINABLE_TESTNET: Target = _
pub const MAX_ATTAINABLE_TESTNET: Target = _
The proof of work limit on testnet.
pub const MAX_ATTAINABLE_REGTEST: Target = _
pub const MAX_ATTAINABLE_REGTEST: Target = _
The proof of work limit on regtest.
pub const MAX_ATTAINABLE_SIGNET: Target = _
pub const MAX_ATTAINABLE_SIGNET: Target = _
The proof of work limit on signet.
pub fn from_compact(c: CompactTarget) -> Target
pub fn from_compact(c: CompactTarget) -> Target
Computes the Target
value from a compact representation.
ref: https://developer.bitcoin.org/reference/block_chain.html#target-nbits
pub fn to_compact_lossy(self) -> CompactTarget
pub fn to_compact_lossy(self) -> CompactTarget
Computes the compact value from a Target
representation.
The compact form is by definition lossy, this means that
t == Target::from_compact(t.to_compact_lossy())
does not always hold.
pub fn is_met_by(&self, hash: BlockHash) -> bool
pub fn is_met_by(&self, hash: BlockHash) -> bool
Returns true if block hash is less than or equal to this Target
.
Proof-of-work validity for a block requires the hash of the block to be less than or equal to the target.
pub fn difficulty(&self, params: impl AsRef<Params>) -> u128
pub fn difficulty(&self, params: impl AsRef<Params>) -> u128
Computes the popular “difficulty” measure for mining.
Difficulty represents how difficult the current target makes it to find a block, relative to how difficult it would be at the highest possible target (highest target == lowest difficulty).
For example, a difficulty of 6,695,826 means that at a given hash rate, it will, on average, take ~6.6 million times as long to find a valid block as it would at a difficulty of 1, or alternatively, it will take, again on average, ~6.6 million times as many hashes to find a valid block
§Note
Difficulty is calculated using the following algorithm max / current
where max is
defined for the Bitcoin network and current
is the current target for this block. As
such, a low target implies a high difficulty. Since Target
is represented as a 256 bit
integer but difficulty()
returns only 128 bits this means for targets below approximately
0xffff_ffff_ffff_ffff_ffff_ffff
difficulty()
will saturate at u128::MAX
.
§Panics
Panics if self
is zero (divide by zero).
pub fn difficulty_float(&self) -> f64
pub fn difficulty_float(&self) -> f64
Computes the popular “difficulty” measure for mining and returns a float value of f64.
See difficulty
for details.
§Returns
Returns f64::INFINITY
if self
is zero (caused by divide by zero).
pub fn min_difficulty_transition_threshold(&self) -> Target
👎Deprecated since 0.32.0: use min_transition_threshold instead
pub fn min_difficulty_transition_threshold(&self) -> Target
Computes the minimum valid Target
threshold allowed for a block in which a difficulty
adjustment occurs.
pub fn max_difficulty_transition_threshold(&self) -> Target
👎Deprecated since 0.32.0: use max_transition_threshold instead
pub fn max_difficulty_transition_threshold(&self) -> Target
Computes the maximum valid Target
threshold allowed for a block in which a difficulty
adjustment occurs.
pub fn min_transition_threshold(&self) -> Target
pub fn min_transition_threshold(&self) -> Target
pub fn max_transition_threshold(&self, params: impl AsRef<Params>) -> Target
pub fn max_transition_threshold(&self, params: impl AsRef<Params>) -> Target
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.
We also check that the calculated target is not greater than the maximum allowed target,
this value is network specific - hence the params
parameter.
pub fn max_transition_threshold_unchecked(&self) -> Target
pub fn max_transition_threshold_unchecked(&self) -> Target
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.
§Returns
This function may return a value greater than the maximum allowed target for this network.
The return value should be checked against Params::max_attainable_target
or use one of
the Target::MAX_ATTAINABLE_FOO
constants.
§impl Target
impl Target
pub fn from_hex(s: &str) -> Result<Target, PrefixedHexError>
pub fn from_hex(s: &str) -> Result<Target, PrefixedHexError>
Creates Target
from a prefixed hex string.
pub fn from_unprefixed_hex(s: &str) -> Result<Target, UnprefixedHexError>
pub fn from_unprefixed_hex(s: &str) -> Result<Target, UnprefixedHexError>
Creates Target
from an unprefixed hex string.
pub fn from_be_bytes(bytes: [u8; 32]) -> Target
pub fn from_be_bytes(bytes: [u8; 32]) -> Target
Creates Target
from a big-endian byte array.
pub fn from_le_bytes(bytes: [u8; 32]) -> Target
pub fn from_le_bytes(bytes: [u8; 32]) -> Target
Creates Target
from a little-endian byte array.
pub fn to_be_bytes(self) -> [u8; 32]
pub fn to_be_bytes(self) -> [u8; 32]
Converts Target
to a big-endian byte array.
pub fn to_le_bytes(self) -> [u8; 32]
pub fn to_le_bytes(self) -> [u8; 32]
Converts Target
to a little-endian byte array.