Introduce CompactTargetExt trait
In preparation for moving the `CompactTarget` type to `primitives` introduce an extension trait for code that will be left behind in `bitcoin`.
This commit is contained in:
parent
100ce03643
commit
a00bd7cc4d
|
@ -15,6 +15,7 @@ use units::parse::{self, ParseIntError, PrefixedHexError, UnprefixedHexError};
|
||||||
|
|
||||||
use crate::block::{BlockHash, Header};
|
use crate::block::{BlockHash, Header};
|
||||||
use crate::consensus::encode::{self, Decodable, Encodable};
|
use crate::consensus::encode::{self, Decodable, Encodable};
|
||||||
|
use crate::internal_macros::define_extension_trait;
|
||||||
use crate::network::Params;
|
use crate::network::Params;
|
||||||
|
|
||||||
/// Implement traits and methods shared by `Target` and `Work`.
|
/// Implement traits and methods shared by `Target` and `Work`.
|
||||||
|
@ -355,18 +356,17 @@ impl CompactTarget {
|
||||||
pub fn to_consensus(self) -> u32 { self.0 }
|
pub fn to_consensus(self) -> u32 { self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
mod tmp {
|
define_extension_trait! {
|
||||||
use super::*;
|
/// Extension functionality for the [`CompactTarget`] type.
|
||||||
|
pub trait CompactTargetExt impl for CompactTarget {
|
||||||
impl CompactTarget {
|
|
||||||
/// Creates a `CompactTarget` from a prefixed hex string.
|
/// Creates a `CompactTarget` from a prefixed hex string.
|
||||||
pub fn from_hex(s: &str) -> Result<Self, PrefixedHexError> {
|
fn from_hex(s: &str) -> Result<CompactTarget, PrefixedHexError> {
|
||||||
let target = parse::hex_u32_prefixed(s)?;
|
let target = parse::hex_u32_prefixed(s)?;
|
||||||
Ok(Self::from_consensus(target))
|
Ok(Self::from_consensus(target))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a `CompactTarget` from an unprefixed hex string.
|
/// Creates a `CompactTarget` from an unprefixed hex string.
|
||||||
pub fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError> {
|
fn from_unprefixed_hex(s: &str) -> Result<CompactTarget, UnprefixedHexError> {
|
||||||
let target = parse::hex_u32_unprefixed(s)?;
|
let target = parse::hex_u32_unprefixed(s)?;
|
||||||
Ok(Self::from_consensus(target))
|
Ok(Self::from_consensus(target))
|
||||||
}
|
}
|
||||||
|
@ -393,7 +393,7 @@ mod tmp {
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// The expected [`CompactTarget`] recalculation.
|
/// The expected [`CompactTarget`] recalculation.
|
||||||
pub fn from_next_work_required(
|
fn from_next_work_required(
|
||||||
last: CompactTarget,
|
last: CompactTarget,
|
||||||
timespan: u64,
|
timespan: u64,
|
||||||
params: impl AsRef<Params>,
|
params: impl AsRef<Params>,
|
||||||
|
@ -437,7 +437,7 @@ mod tmp {
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// The expected [`CompactTarget`] recalculation.
|
/// The expected [`CompactTarget`] recalculation.
|
||||||
pub fn from_header_difficulty_adjustment(
|
fn from_header_difficulty_adjustment(
|
||||||
last_epoch_boundary: Header,
|
last_epoch_boundary: Header,
|
||||||
current: Header,
|
current: Header,
|
||||||
params: impl AsRef<Params>,
|
params: impl AsRef<Params>,
|
||||||
|
|
Loading…
Reference in New Issue