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.)
Target does not limit its value to the maximum attainable value for any network when it
is constructed. If you need to enforce that invariant, you should compare the constructed value
against the required network’s MAX_ATTAINABLE_* target constant.
Implementations§
Source§impl Target
impl Target
Sourcepub const ZERO: Self
pub const ZERO: Self
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 maximum attainable target value on testnet.
Sourcepub const MAX_ATTAINABLE_REGTEST: Self
pub const MAX_ATTAINABLE_REGTEST: Self
The maximum attainable target value on regtest.
Sourcepub const MAX_ATTAINABLE_SIGNET: Self
pub const MAX_ATTAINABLE_SIGNET: Self
The maximum attainable target value on signet.
Sourcepub fn from_compact(c: CompactTarget) -> Result<Self, InvalidCompactTargetError>
pub fn from_compact(c: CompactTarget) -> Result<Self, InvalidCompactTargetError>
Computes the Target value from a compact representation.
Bitcoin Core parses a negative or overflowing compact value as a zero target. Use
.unwrap_or(Target::ZERO) to match that behavior.
ref: https://developer.bitcoin.org/reference/block_chain.html#target-nbits
§Errors
- If the compact value is negative or overflows 256 bits.
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 to_work(self) -> Result<Work, ZeroConversionError>
pub fn to_work(self) -> Result<Work, ZeroConversionError>
Converts this Target to Work, work = 2^256 / (target + 1).
“Work” is defined as the work done to mine a block with this target value (recorded in the
block header in compact form as nBits). This is not the same as the difficulty to mine a
block with this target (see Self::difficulty).
When validating a chain, Bitcoin Core counts a zero target as zero work. Use
.unwrap_or(Work::ZERO) to match that behavior.
§Errors
If the target is zero.
Source§impl Target
impl Target
Sourcepub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
Sourcepub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
Sourcepub fn from_be_bytes(bytes: [u8; 32]) -> Target
pub fn from_be_bytes(bytes: [u8; 32]) -> Target
Constructs Target from a big-endian byte array.
Sourcepub fn from_le_bytes(bytes: [u8; 32]) -> Target
pub fn from_le_bytes(bytes: [u8; 32]) -> Target
Constructs Target from a little-endian byte array.
Sourcepub fn to_be_bytes(self) -> [u8; 32]
pub fn to_be_bytes(self) -> [u8; 32]
Converts Target to a big-endian byte array.
Sourcepub fn to_le_bytes(self) -> [u8; 32]
pub fn to_le_bytes(self) -> [u8; 32]
Converts Target to a little-endian byte array.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Target
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Target
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read more