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) -> Self
pub fn from_compact(c: CompactTarget) -> Self
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.
Source§impl Target
impl Target
Sourcepub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
Constructs a new Target from a prefixed hex string.
§Errors
- If the input string does not contain a
0x(or0X) prefix. - If the input string is not a valid hex encoding of a
Target.
Sourcepub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
Constructs a new Target from an unprefixed hex string.
§Errors
- If the input string contains a
0x(or0X) prefix. - If the input string is not a valid hex encoding of a
Target.
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.