Struct bdk_chain::bitcoin::CompactTarget
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§
§impl CompactTarget
impl CompactTarget
pub fn from_hex(s: &str) -> Result<CompactTarget, PrefixedHexError>
pub fn from_hex(s: &str) -> Result<CompactTarget, PrefixedHexError>
Creates a CompactTarget
from an prefixed hex string.
pub fn from_unprefixed_hex(s: &str) -> Result<CompactTarget, UnprefixedHexError>
pub fn from_unprefixed_hex(s: &str) -> Result<CompactTarget, UnprefixedHexError>
Creates a CompactTarget
from an unprefixed hex string.
pub 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.
pub 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.
pub fn from_consensus(bits: u32) -> CompactTarget
pub fn from_consensus(bits: u32) -> CompactTarget
Creates a CompactTarget
from a consensus encoded u32
.
pub fn to_consensus(self) -> u32
pub fn to_consensus(self) -> u32
Returns the consensus encoded u32
representation of this CompactTarget
.
Trait Implementations§
§impl Clone for CompactTarget
impl Clone for CompactTarget
§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§impl Debug for CompactTarget
impl Debug for CompactTarget
§impl Decodable for CompactTarget
impl Decodable for CompactTarget
§impl Default for CompactTarget
impl Default for CompactTarget
§fn default() -> CompactTarget
fn default() -> CompactTarget
§impl<'de> Deserialize<'de> for CompactTarget
impl<'de> Deserialize<'de> for CompactTarget
§fn deserialize<__D>(
__deserializer: __D
) -> Result<CompactTarget, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<CompactTarget, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl Encodable for CompactTarget
impl Encodable for CompactTarget
§impl From<CompactTarget> for Target
impl From<CompactTarget> for Target
§fn from(c: CompactTarget) -> Target
fn from(c: CompactTarget) -> Target
§impl Hash for CompactTarget
impl Hash for CompactTarget
§impl LowerHex for CompactTarget
impl LowerHex for CompactTarget
§impl Ord for CompactTarget
impl Ord for CompactTarget
§impl PartialEq for CompactTarget
impl PartialEq for CompactTarget
§fn eq(&self, other: &CompactTarget) -> bool
fn eq(&self, other: &CompactTarget) -> bool
self
and other
values to be equal, and is used
by ==
.§impl PartialOrd for CompactTarget
impl PartialOrd for CompactTarget
§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