Skip to content

Commit 2ec901f

Browse files
committed
Move the CompactTarget type to primitives
Potentially the whole `pow` module will move to `primitives` but this is not possible easily right now. However, we would like to be able to move the `BlockHash` and `block::Header` types over to `primitives` and doing so requires the `CompactTarget` to be there. Move the `CompactTarget` type to `primitives` and re-export it from the `primitives` crate root. Note also, we re-export the type publicly from `bitcoin::pow`.
1 parent a00bd7c commit 2ec901f

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

bitcoin/src/pow.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use crate::consensus::encode::{self, Decodable, Encodable};
1818
use crate::internal_macros::define_extension_trait;
1919
use crate::network::Params;
2020

21+
#[rustfmt::skip] // Keep public re-exports separate.
22+
#[doc(inline)]
23+
pub use primitives::CompactTarget;
24+
2125
/// Implement traits and methods shared by `Target` and `Work`.
2226
macro_rules! do_impl {
2327
($ty:ident) => {
@@ -330,32 +334,6 @@ impl Target {
330334
}
331335
do_impl!(Target);
332336

333-
/// Encoding of 256-bit target as 32-bit float.
334-
///
335-
/// This is used to encode a target into the block header. Satoshi made this part of consensus code
336-
/// in the original version of Bitcoin, likely copying an idea from OpenSSL.
337-
///
338-
/// OpenSSL's bignum (BN) type has an encoding, which is even called "compact" as in bitcoin, which
339-
/// is exactly this format.
340-
///
341-
/// # Note on order/equality
342-
///
343-
/// Usage of the ordering and equality traits for this type may be surprising. Converting between
344-
/// `CompactTarget` and `Target` is lossy *in both directions* (there are multiple `CompactTarget`
345-
/// values that map to the same `Target` value). Ordering and equality for this type are defined in
346-
/// terms of the underlying `u32`.
347-
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
348-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
349-
pub struct CompactTarget(u32);
350-
351-
impl CompactTarget {
352-
/// Creates a [`CompactTarget`] from a consensus encoded `u32`.
353-
pub fn from_consensus(bits: u32) -> Self { Self(bits) }
354-
355-
/// Returns the consensus encoded `u32` representation of this [`CompactTarget`].
356-
pub fn to_consensus(self) -> u32 { self.0 }
357-
}
358-
359337
define_extension_trait! {
360338
/// Extension functionality for the [`CompactTarget`] type.
361339
pub trait CompactTargetExt impl for CompactTarget {
@@ -467,16 +445,6 @@ impl Decodable for CompactTarget {
467445
}
468446
}
469447

470-
impl fmt::LowerHex for CompactTarget {
471-
#[inline]
472-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
473-
}
474-
475-
impl fmt::UpperHex for CompactTarget {
476-
#[inline]
477-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
478-
}
479-
480448
/// Big-endian 256 bit integer type.
481449
// (high, low): u.0 contains the high bits, u.1 contains the low bits.
482450
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]

primitives/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate serde;
3131
#[cfg(feature = "alloc")]
3232
pub mod locktime;
3333
pub mod opcodes;
34+
pub mod pow;
3435
pub mod sequence;
3536

3637
#[doc(inline)]
@@ -40,7 +41,10 @@ pub use units::*;
4041
#[cfg(feature = "alloc")]
4142
pub use self::locktime::{absolute, relative};
4243
#[doc(inline)]
43-
pub use self::sequence::Sequence;
44+
pub use self::{
45+
pow::CompactTarget,
46+
sequence::Sequence,
47+
};
4448

4549
#[rustfmt::skip]
4650
#[allow(unused_imports)]

primitives/src/pow.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Proof-of-work related integer types.
4+
5+
use core::fmt;
6+
7+
/// Encoding of 256-bit target as 32-bit float.
8+
///
9+
/// This is used to encode a target into the block header. Satoshi made this part of consensus code
10+
/// in the original version of Bitcoin, likely copying an idea from OpenSSL.
11+
///
12+
/// OpenSSL's bignum (BN) type has an encoding, which is even called "compact" as in bitcoin, which
13+
/// is exactly this format.
14+
///
15+
/// # Note on order/equality
16+
///
17+
/// Usage of the ordering and equality traits for this type may be surprising. Converting between
18+
/// `CompactTarget` and `Target` is lossy *in both directions* (there are multiple `CompactTarget`
19+
/// values that map to the same `Target` value). Ordering and equality for this type are defined in
20+
/// terms of the underlying `u32`.
21+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23+
pub struct CompactTarget(u32);
24+
25+
impl CompactTarget {
26+
/// Creates a [`CompactTarget`] from a consensus encoded `u32`.
27+
pub fn from_consensus(bits: u32) -> Self { Self(bits) }
28+
29+
/// Returns the consensus encoded `u32` representation of this [`CompactTarget`].
30+
pub fn to_consensus(self) -> u32 { self.0 }
31+
}
32+
33+
impl fmt::LowerHex for CompactTarget {
34+
#[inline]
35+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
36+
}
37+
38+
impl fmt::UpperHex for CompactTarget {
39+
#[inline]
40+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
41+
}

0 commit comments

Comments
 (0)