Struct bitcoin::CompactTarget
source · 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.
Implementations§
source§impl CompactTarget
impl CompactTarget
sourcepub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError>
Creates a CompactTarget
from an prefixed hex string.
sourcepub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError>
Creates a CompactTarget
from an unprefixed hex string.
sourcepub fn from_next_work_required(
last: CompactTarget,
timespan: u64,
params: impl AsRef<Params>
) -> CompactTarget
pub fn from_next_work_required( last: CompactTarget, timespan: u64, params: impl AsRef<Params> ) -> CompactTarget
Computes the CompactTarget
from a difficulty adjustment.
ref: https://github.com/bitcoin/bitcoin/blob/0503cbea9aab47ec0a87d34611e5453158727169/src/pow.cpp
Given the previous Target, represented as a CompactTarget
, the difficulty is adjusted
by taking the timespan between them, and multipling the current CompactTarget
by a factor
of the net timespan and expected timespan. The CompactTarget
may not adjust by more than
a factor of 4, or adjust beyond the maximum threshold for the network.
Note
Under the consensus rules, the difference in the number of blocks between the headers does
not equate to the difficulty_adjustment_interval
of Params
. This is due to an off-by-one
error, and, the expected number of blocks in between headers is difficulty_adjustment_interval - 1
when calculating the difficulty adjustment.
Take the example of the first difficulty adjustment. Block 2016 introduces a new CompactTarget
,
which takes the net timespan between Block 2015 and Block 0, and recomputes the difficulty.
Returns
The expected CompactTarget
recalculation.
sourcepub fn from_header_difficulty_adjustment(
last_epoch_boundary: Header,
current: Header,
params: impl AsRef<Params>
) -> CompactTarget
pub fn from_header_difficulty_adjustment( last_epoch_boundary: Header, current: Header, params: impl AsRef<Params> ) -> CompactTarget
Computes the CompactTarget
from a difficulty adjustment,
assuming these are the relevant block headers.
Given two headers, representing the start and end of a difficulty adjustment epoch,
compute the CompactTarget
based on the net time between them and the current
CompactTarget
.
Note
See CompactTarget::from_next_work_required
For example, to successfully compute the first difficulty adjustment on the Bitcoin network,
one would pass the header for Block 2015 as current
and the header for Block 0 as
last_epoch_boundary
.
Returns
The expected CompactTarget
recalculation.
sourcepub fn from_consensus(bits: u32) -> Self
pub fn from_consensus(bits: u32) -> Self
Creates a CompactTarget
from a consensus encoded u32
.
sourcepub fn to_consensus(self) -> u32
pub fn to_consensus(self) -> u32
Returns the consensus encoded u32
representation of this CompactTarget
.
Trait Implementations§
source§impl Clone for CompactTarget
impl Clone for CompactTarget
source§fn clone(&self) -> CompactTarget
fn clone(&self) -> CompactTarget
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for CompactTarget
impl Debug for CompactTarget
source§impl Decodable for CompactTarget
impl Decodable for CompactTarget
source§impl Default for CompactTarget
impl Default for CompactTarget
source§fn default() -> CompactTarget
fn default() -> CompactTarget
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 Encodable for CompactTarget
impl Encodable for CompactTarget
source§impl From<CompactTarget> for Target
impl From<CompactTarget> for Target
source§fn from(c: CompactTarget) -> Self
fn from(c: CompactTarget) -> Self
source§impl Hash for CompactTarget
impl Hash for CompactTarget
source§impl LowerHex for CompactTarget
impl LowerHex for CompactTarget
source§impl Ord for CompactTarget
impl Ord for CompactTarget
source§fn cmp(&self, other: &CompactTarget) -> Ordering
fn cmp(&self, other: &CompactTarget) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq<CompactTarget> for CompactTarget
impl PartialEq<CompactTarget> for CompactTarget
source§fn eq(&self, other: &CompactTarget) -> bool
fn eq(&self, other: &CompactTarget) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<CompactTarget> for CompactTarget
impl PartialOrd<CompactTarget> for CompactTarget
source§fn partial_cmp(&self, other: &CompactTarget) -> Option<Ordering>
fn partial_cmp(&self, other: &CompactTarget) -> Option<Ordering>
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