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 more