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§
source§impl Target
impl Target
sourcepub const ZERO: Target = _
pub const ZERO: Target = _
When parsing nBits, Bitcoin Core converts a negative target threshold into a target of zero.
sourcepub const MAX: Self = _
pub const MAX: Self = _
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.
sourcepub const MAX_ATTAINABLE_MAINNET: Self = _
pub const MAX_ATTAINABLE_MAINNET: Self = _
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
).
sourcepub const MAX_ATTAINABLE_TESTNET: Self = _
pub const MAX_ATTAINABLE_TESTNET: Self = _
The proof of work limit on testnet.
sourcepub const MAX_ATTAINABLE_REGTEST: Self = _
pub const MAX_ATTAINABLE_REGTEST: Self = _
The proof of work limit on regtest.
sourcepub const MAX_ATTAINABLE_SIGNET: Self = _
pub const MAX_ATTAINABLE_SIGNET: Self = _
The proof of work limit on signet.
sourcepub const fn max_value() -> Self
👎Deprecated since 0.31.0: Use Self::MAX instead
pub const fn max_value() -> Self
The maximum possible target (see Target::MAX
).
This is provided for consistency with Rust 1.41.1, newer code should use Target::MAX
.
sourcepub 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
sourcepub 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.
sourcepub 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.
sourcepub fn difficulty(&self) -> u128
pub fn difficulty(&self) -> 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
.
sourcepub 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.
sourcepub fn min_difficulty_transition_threshold(&self) -> Self
pub fn min_difficulty_transition_threshold(&self) -> Self
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.
sourcepub fn max_difficulty_transition_threshold(&self) -> Self
pub fn max_difficulty_transition_threshold(&self) -> Self
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.
source§impl Target
impl Target
sourcepub fn from_be_bytes(bytes: [u8; 32]) -> Target
pub fn from_be_bytes(bytes: [u8; 32]) -> Target
Creates Self
from a big-endian byte array.
sourcepub fn from_le_bytes(bytes: [u8; 32]) -> Target
pub fn from_le_bytes(bytes: [u8; 32]) -> Target
Creates Self
from a little-endian byte array.
sourcepub fn to_be_bytes(self) -> [u8; 32]
pub fn to_be_bytes(self) -> [u8; 32]
Converts self
to a big-endian byte array.
sourcepub fn to_le_bytes(self) -> [u8; 32]
pub fn to_le_bytes(self) -> [u8; 32]
Converts self
to a little-endian byte array.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Target
impl<'de> Deserialize<'de> for Target
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl From<CompactTarget> for Target
impl From<CompactTarget> for Target
source§fn from(c: CompactTarget) -> Self
fn from(c: CompactTarget) -> Self
source§impl Ord for Target
impl Ord for Target
source§impl PartialEq for Target
impl PartialEq for Target
source§impl PartialOrd for Target
impl PartialOrd for Target
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more