pub struct CompactTarget(/* private fields */);Expand description
Encoding of 256-bit target as 32-bit float.
This is used to encode a target into the block header. Satoshi made this part of consensus code in the original version of Bitcoin, likely copying an idea from OpenSSL.
OpenSSL’s bignum (BN) type has an encoding, which is even called “compact” as in Bitcoin, which is exactly this format.
§Note on equality
Equality is on the consensus encoded u32. Conversion to Target is lossy, so this type is
not ordered. To compare two targets convert them first.
Implementations§
Source§impl CompactTarget
impl CompactTarget
Sourcepub fn from_consensus(bits: u32) -> Self
pub fn from_consensus(bits: u32) -> Self
Constructs a new CompactTarget from a consensus encoded u32.
Sourcepub const fn to_consensus(self) -> u32
👎Deprecated since 0.6.0: use ‘to_consensus_u32()’ instead
pub const fn to_consensus(self) -> u32
use ‘to_consensus_u32()’ instead
Returns the consensus encoded u32 representation of this CompactTarget.
Sourcepub const fn to_consensus_u32(self) -> u32
pub const fn to_consensus_u32(self) -> u32
Returns the consensus encoded u32 representation of this CompactTarget.
Sourcepub fn to_target(self) -> Result<Target, InvalidCompactTargetError>
pub fn to_target(self) -> Result<Target, InvalidCompactTargetError>
Computes the Target value from this compact representation.
ref: https://developer.bitcoin.org/reference/block_chain.html#target-nbits
§Errors
- If the compact value is negative or overflows 256 bits.
Sourcepub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>where
Self: Sized,
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>where
Self: Sized,
Constructs a new CompactTarget 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
u32.
Sourcepub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>where
Self: Sized,
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>where
Self: Sized,
Constructs a new CompactTarget 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
u32.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for CompactTarget
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for CompactTarget
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 moreSource§impl Binary for CompactTarget
impl Binary for CompactTarget
Source§impl Clone for CompactTarget
impl Clone for CompactTarget
impl Copy for CompactTarget
Source§impl Debug for CompactTarget
impl Debug for CompactTarget
Source§impl Decode for CompactTarget
Available on crate feature encoding only.
impl Decode for CompactTarget
encoding only.Source§impl<'de> Deserialize<'de> for CompactTarget
impl<'de> Deserialize<'de> for CompactTarget
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 Display for CompactTarget
impl Display for CompactTarget
Source§impl Encode for CompactTarget
Available on crate feature encoding only.
impl Encode for CompactTarget
encoding only.impl Eq for CompactTarget
Source§impl FromStr for CompactTarget
impl FromStr for CompactTarget
Source§impl Hash for CompactTarget
impl Hash for CompactTarget
Source§impl LowerHex for CompactTarget
impl LowerHex for CompactTarget
Source§impl Octal for CompactTarget
impl Octal for CompactTarget
Source§impl PartialEq for CompactTarget
impl PartialEq for CompactTarget
Source§impl Serialize for CompactTarget
impl Serialize for CompactTarget
impl StructuralPartialEq for CompactTarget
Source§impl TryFrom<&str> for CompactTarget
impl TryFrom<&str> for CompactTarget
Source§impl TryFrom<CompactTarget> for Target
impl TryFrom<CompactTarget> for Target
Source§type Error = InvalidCompactTargetError
type Error = InvalidCompactTargetError
Source§impl TryFrom<String> for CompactTarget
Available on crate feature alloc only.
impl TryFrom<String> for CompactTarget
alloc only.