Struct bitcoin::Target

source ·
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§

source§

impl Target

source

pub const ZERO: Target = _

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

source

pub const MAX: Self = _

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

source

pub const MAX_ATTAINABLE_MAINNET: Self = _

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

source

pub const MAX_ATTAINABLE_TESTNET: Self = _

The proof of work limit on testnet.

source

pub const MAX_ATTAINABLE_REGTEST: Self = _

The proof of work limit on regtest.

source

pub const MAX_ATTAINABLE_SIGNET: Self = _

The proof of work limit on signet.

source

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

source

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.

source

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.

source

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

source

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

source

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

source

pub fn min_difficulty_transition_threshold(&self) -> Self

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

source

pub fn max_difficulty_transition_threshold(&self) -> Self

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

source

pub fn min_transition_threshold(&self) -> Self

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.

source

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

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.

source

pub fn max_transition_threshold_unchecked(&self) -> Self

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.

source§

impl Target

source

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

Creates Target from a prefixed hex string.

source

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

Creates Target from an unprefixed hex string.

source

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

Creates Target from a big-endian byte array.

source

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

Creates Target from a little-endian byte array.

source

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

Converts Target to a big-endian byte array.

source

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

Converts Target to a little-endian byte array.

Trait Implementations§

source§

impl Clone for Target

source§

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
source§

impl Debug for Target

source§

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

Formats the value using the given formatter. Read more
source§

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

source§

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

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

impl Display for Target

source§

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

Formats the value using the given formatter. Read more
source§

impl From<CompactTarget> for Target

source§

fn from(c: CompactTarget) -> Self

Converts to this type from the input type.
source§

impl Hash for Target

source§

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

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
source§

impl LowerHex for Target

source§

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

Formats the value using the given formatter.
source§

impl Ord for Target

source§

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 + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Target

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl PartialOrd for Target

source§

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

This method 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

This method 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

This method 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

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

impl Serialize for Target

source§

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

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

impl UpperHex for Target

source§

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

Formats the value using the given formatter.
source§

impl Copy for Target

source§

impl Eq for Target

source§

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> 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,

§

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§

default 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>,

§

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>,

§

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.
source§

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

source§

fn vzip(self) -> V

source§

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