bdk_chain::bitcoin

Struct Target

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.)

ref: https://en.bitcoin.it/wiki/Target

Implementations§

§

impl Target

pub const ZERO: Target = _

When parsing nBits, Bitcoin Core converts a negative target threshold into a target of zero.

pub const MAX: Target = _

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.

ref: https://en.bitcoin.it/wiki/Target

pub const MAX_ATTAINABLE_MAINNET: Target = _

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).

pub const MAX_ATTAINABLE_TESTNET: Target = _

The proof of work limit on testnet.

pub const MAX_ATTAINABLE_REGTEST: Target = _

The proof of work limit on regtest.

pub const MAX_ATTAINABLE_SIGNET: Target = _

The proof of work limit on signet.

pub fn from_compact(c: CompactTarget) -> Target

Computes the Target value from a compact representation.

ref: https://developer.bitcoin.org/reference/block_chain.html#target-nbits

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.

pub fn is_met_by(&self, hash: BlockHash) -> bool

Returns true if block hash is less than or equal to this Target.

Proof-of-work validity for a block requires the hash of the block to be less than or equal to the target.

pub fn to_work(self) -> Work

Converts this Target to Work.

“Work” is defined as the work done to mine a block with this target value (recorded in the block header in compact form as nBits). This is not the same as the difficulty to mine a block with this target (see Self::difficulty).

pub fn difficulty(&self, params: impl AsRef<Params>) -> u128

Computes the popular “difficulty” measure for mining.

Difficulty represents how difficult the current target makes it to find a block, relative to how difficult it would be at the highest possible target (highest target == lowest difficulty).

For example, a difficulty of 6,695,826 means that at a given hash rate, it will, on average, take ~6.6 million times as long to find a valid block as it would at a difficulty of 1, or alternatively, it will take, again on average, ~6.6 million times as many hashes to find a valid block

§Note

Difficulty is calculated using the following algorithm max / current where max is defined for the Bitcoin network and current is the current target for this block. As such, a low target implies a high difficulty. Since Target is represented as a 256 bit integer but difficulty() returns only 128 bits this means for targets below approximately 0xffff_ffff_ffff_ffff_ffff_ffff difficulty() will saturate at u128::MAX.

§Panics

Panics if self is zero (divide by zero).

pub fn difficulty_float(&self) -> f64

Computes the popular “difficulty” measure for mining and returns a float value of f64.

See difficulty for details.

§Returns

Returns f64::INFINITY if self is zero (caused by divide by zero).

pub fn min_difficulty_transition_threshold(&self) -> Target

👎Deprecated since 0.32.0: use min_transition_threshold instead

Computes the minimum valid Target threshold allowed for a block in which a difficulty adjustment occurs.

pub fn max_difficulty_transition_threshold(&self) -> Target

👎Deprecated since 0.32.0: use max_transition_threshold instead

Computes the maximum valid Target threshold allowed for a block in which a difficulty adjustment occurs.

pub fn min_transition_threshold(&self) -> Target

Computes the minimum valid Target threshold allowed for a block in which a difficulty adjustment occurs.

The difficulty can only decrease or increase by a factor of 4 max on each difficulty adjustment period.

§Returns

In line with Bitcoin Core this function may return a target value of zero.

pub fn max_transition_threshold(&self, params: impl AsRef<Params>) -> Target

Computes the maximum valid Target threshold allowed for a block in which a difficulty adjustment occurs.

The difficulty can only decrease or increase by a factor of 4 max on each difficulty adjustment period.

We also check that the calculated target is not greater than the maximum allowed target, this value is network specific - hence the params parameter.

pub fn max_transition_threshold_unchecked(&self) -> Target

Computes the maximum valid Target threshold allowed for a block in which a difficulty adjustment occurs.

The difficulty can only decrease or increase by a factor of 4 max on each difficulty adjustment period.

§Returns

This function may return a value greater than the maximum allowed target for this network.

The return value should be checked against Params::max_attainable_target or use one of the Target::MAX_ATTAINABLE_FOO constants.

§

impl Target

pub fn from_hex(s: &str) -> Result<Target, PrefixedHexError>

Creates Target from a prefixed hex string.

pub fn from_unprefixed_hex(s: &str) -> Result<Target, UnprefixedHexError>

Creates Target from an unprefixed hex string.

pub fn from_be_bytes(bytes: [u8; 32]) -> Target

Creates Target from a big-endian byte array.

pub fn from_le_bytes(bytes: [u8; 32]) -> Target

Creates Target from a little-endian byte array.

pub fn to_be_bytes(self) -> [u8; 32]

Converts Target to a big-endian byte array.

pub fn to_le_bytes(self) -> [u8; 32]

Converts Target to a little-endian byte array.

Trait Implementations§

§

impl Clone for Target

§

fn clone(&self) -> Target

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Target

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for Target

§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Target, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
§

impl Display for Target

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl From<CompactTarget> for Target

§

fn from(c: CompactTarget) -> Target

Converts to this type from the input type.
§

impl Hash for Target

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl LowerHex for Target

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Ord for Target

§

fn cmp(&self, other: &Target) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

impl PartialEq for Target

§

fn eq(&self, other: &Target) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl PartialOrd for Target

§

fn partial_cmp(&self, other: &Target) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl Serialize for Target

§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl UpperHex for Target

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Copy for Target

§

impl Eq for Target

§

impl StructuralPartialEq for Target

Auto Trait Implementations§

§

impl Freeze for Target

§

impl RefUnwindSafe for Target

§

impl Send for Target

§

impl Sync for Target

§

impl Unpin for Target

§

impl UnwindSafe for Target

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,