diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index c7b69526b..e602a3133 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -413,7 +413,7 @@ mod test { use crate::merkle_tree::TxMerkleNode; use crate::transaction::OutPointExt; use crate::{ - transaction, Amount, BlockChecked, CompactTarget, OutPoint, ScriptBuf, Sequence, Timestamp, + transaction, Amount, BlockChecked, CompactTarget, OutPoint, ScriptBuf, Sequence, BlockTime, TxIn, TxOut, Txid, Witness, }; @@ -437,7 +437,7 @@ mod test { version: block::Version::ONE, prev_blockhash: BlockHash::from_byte_array([0x99; 32]), merkle_root: TxMerkleNode::from_byte_array([0x77; 32]), - time: Timestamp::from_u32(2), + time: BlockTime::from_u32(2), bits: CompactTarget::from_consensus(3), nonce: 4, }; diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index dfd039e55..a8dae5e4b 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -13,7 +13,7 @@ use core::fmt; use hashes::{sha256d, HashEngine}; use internals::{compact_size, ToU64}; use io::{BufRead, Write}; -use units::Timestamp; +use units::BlockTime; use super::Weight; use crate::consensus::encode::WriteExt as _; @@ -86,15 +86,15 @@ impl Decodable for Version { } } -impl Encodable for Timestamp { +impl Encodable for BlockTime { fn consensus_encode(&self, w: &mut W) -> Result { self.to_u32().consensus_encode(w) } } -impl Decodable for Timestamp { +impl Decodable for BlockTime { fn consensus_decode(r: &mut R) -> Result { - Decodable::consensus_decode(r).map(Timestamp::from_u32) + Decodable::consensus_decode(r).map(BlockTime::from_u32) } } @@ -609,7 +609,7 @@ mod tests { block::compute_merkle_root(&transactions).unwrap() ); assert_eq!(serialize(&real_decode.header().merkle_root), merkle); - assert_eq!(real_decode.header().time, Timestamp::from_u32(1231965655)); + assert_eq!(real_decode.header().time, BlockTime::from_u32(1231965655)); assert_eq!(real_decode.header().bits, CompactTarget::from_consensus(486604799)); assert_eq!(real_decode.header().nonce, 2067413810); assert_eq!(real_decode.header().work(), work); @@ -660,7 +660,7 @@ mod tests { real_decode.header().merkle_root, block::compute_merkle_root(&transactions).unwrap() ); - assert_eq!(real_decode.header().time, Timestamp::from_u32(1472004949)); + assert_eq!(real_decode.header().time, BlockTime::from_u32(1472004949)); assert_eq!(real_decode.header().bits, CompactTarget::from_consensus(0x1a06d450)); assert_eq!(real_decode.header().nonce, 1879759182); assert_eq!(real_decode.header().work(), work); diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index ae0d48df8..8d48609aa 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -16,7 +16,7 @@ use crate::opcodes::all::*; use crate::pow::CompactTarget; use crate::transaction::{self, OutPoint, Transaction, TxIn, TxOut}; use crate::witness::Witness; -use crate::{script, Amount, BlockHash, Sequence, TestnetVersion, Timestamp}; +use crate::{script, Amount, BlockHash, Sequence, TestnetVersion, BlockTime}; /// How many seconds between blocks we expect on average. pub const TARGET_BLOCK_SPACING: u32 = 600; @@ -134,7 +134,7 @@ pub fn genesis_block(params: impl AsRef) -> Block { version: block::Version::ONE, prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH, merkle_root, - time: Timestamp::from_u32(1231006505), + time: BlockTime::from_u32(1231006505), bits: CompactTarget::from_consensus(0x1d00ffff), nonce: 2083236893, }, @@ -146,7 +146,7 @@ pub fn genesis_block(params: impl AsRef) -> Block { version: block::Version::ONE, prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH, merkle_root, - time: Timestamp::from_u32(1296688602), + time: BlockTime::from_u32(1296688602), bits: CompactTarget::from_consensus(0x1d00ffff), nonce: 414098458, }, @@ -158,7 +158,7 @@ pub fn genesis_block(params: impl AsRef) -> Block { version: block::Version::ONE, prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH, merkle_root, - time: Timestamp::from_u32(1714777860), + time: BlockTime::from_u32(1714777860), bits: CompactTarget::from_consensus(0x1d00ffff), nonce: 393743547, }, @@ -170,7 +170,7 @@ pub fn genesis_block(params: impl AsRef) -> Block { version: block::Version::ONE, prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH, merkle_root, - time: Timestamp::from_u32(1598918400), + time: BlockTime::from_u32(1598918400), bits: CompactTarget::from_consensus(0x1e0377ae), nonce: 52613770, }, @@ -182,7 +182,7 @@ pub fn genesis_block(params: impl AsRef) -> Block { version: block::Version::ONE, prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH, merkle_root, - time: Timestamp::from_u32(1296688602), + time: BlockTime::from_u32(1296688602), bits: CompactTarget::from_consensus(0x207fffff), nonce: 2, }, @@ -322,7 +322,7 @@ mod test { "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" ); - assert_eq!(gen.header().time, Timestamp::from_u32(1231006505)); + assert_eq!(gen.header().time, BlockTime::from_u32(1231006505)); assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header().nonce, 2083236893); assert_eq!( @@ -340,7 +340,7 @@ mod test { gen.header().merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" ); - assert_eq!(gen.header().time, Timestamp::from_u32(1296688602)); + assert_eq!(gen.header().time, BlockTime::from_u32(1296688602)); assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header().nonce, 414098458); assert_eq!( @@ -358,7 +358,7 @@ mod test { gen.header().merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" ); - assert_eq!(gen.header().time, Timestamp::from_u32(1598918400)); + assert_eq!(gen.header().time, BlockTime::from_u32(1598918400)); assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x1e0377ae)); assert_eq!(gen.header().nonce, 52613770); assert_eq!( diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 308248313..792f23df1 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -135,7 +135,7 @@ pub use units::{ amount::{Amount, Denomination, SignedAmount}, block::{BlockHeight, BlockInterval}, fee_rate::FeeRate, - timestamp::{self, Timestamp}, + time::{self, BlockTime}, weight::Weight, }; diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index 09929f64f..bb59edb50 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -1078,7 +1078,7 @@ pub mod test_utils { mod tests { use super::*; use crate::pow::test_utils::{u128_to_work, u32_to_target, u64_to_target}; - use crate::Timestamp; + use crate::BlockTime; impl U256 { fn bit_at(&self, index: usize) -> bool { @@ -1763,7 +1763,7 @@ mod tests { version: Version::ONE, prev_blockhash: BlockHash::from_byte_array([0; 32]), merkle_root: TxMerkleNode::from_byte_array([0; 32]), - time: Timestamp::from_u32(1599332177), + time: BlockTime::from_u32(1599332177), bits: epoch_start.bits, nonce: epoch_start.nonce, }; @@ -1785,7 +1785,7 @@ mod tests { version: Version::ONE, prev_blockhash: BlockHash::from_byte_array([0; 32]), merkle_root: TxMerkleNode::from_byte_array([0; 32]), - time: Timestamp::from_u32(1599332844), + time: BlockTime::from_u32(1599332844), bits: starting_bits, nonce: 0, }; @@ -1795,7 +1795,7 @@ mod tests { version: Version::ONE, prev_blockhash: BlockHash::from_byte_array([0; 32]), merkle_root: TxMerkleNode::from_byte_array([0; 32]), - time: Timestamp::from_u32(1600591200), + time: BlockTime::from_u32(1600591200), bits: starting_bits, nonce: 0, }; diff --git a/primitives/src/block.rs b/primitives/src/block.rs index 56673fa41..a4d49b0ae 100644 --- a/primitives/src/block.rs +++ b/primitives/src/block.rs @@ -14,7 +14,7 @@ use core::marker::PhantomData; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; use hashes::{sha256d, HashEngine as _}; -use units::Timestamp; +use units::BlockTime; use crate::merkle_tree::TxMerkleNode; #[cfg(feature = "alloc")] @@ -180,7 +180,7 @@ pub struct Header { /// The root hash of the Merkle tree of transactions in the block. pub merkle_root: TxMerkleNode, /// The timestamp of the block, as claimed by the miner. - pub time: Timestamp, + pub time: BlockTime, /// The target value below which the blockhash must lie. pub bits: CompactTarget, /// The nonce, selected to obtain a low enough blockhash. @@ -366,7 +366,7 @@ mod tests { version: Version::ONE, prev_blockhash: BlockHash::from_byte_array([0x99; 32]), merkle_root: TxMerkleNode::from_byte_array([0x77; 32]), - time: Timestamp::from(2), + time: BlockTime::from(2), bits: CompactTarget::from_consensus(3), nonce: 4, } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index d16c58f91..f9dafc05a 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -52,7 +52,7 @@ pub use units::{ amount::{self, Amount, SignedAmount}, block::{BlockHeight, BlockInterval}, fee_rate::{self, FeeRate}, - timestamp::{self, Timestamp}, + time::{self, BlockTime}, weight::{self, Weight}, }; diff --git a/units/src/lib.rs b/units/src/lib.rs index 7654d6723..4a166ebb2 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -34,7 +34,7 @@ pub mod block; pub mod fee_rate; pub mod locktime; pub mod parse; -pub mod timestamp; +pub mod time; pub mod weight; #[doc(inline)] @@ -43,6 +43,6 @@ pub use self::{ amount::{Amount, SignedAmount}, block::{BlockHeight, BlockInterval}, fee_rate::FeeRate, - timestamp::Timestamp, + time::BlockTime, weight::Weight }; diff --git a/units/src/timestamp.rs b/units/src/time.rs similarity index 82% rename from units/src/timestamp.rs rename to units/src/time.rs index 35e59d8be..41faec706 100644 --- a/units/src/timestamp.rs +++ b/units/src/time.rs @@ -26,33 +26,33 @@ use serde::{Deserialize, Serialize}; /// ref: #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Timestamp(u32); +pub struct BlockTime(u32); -impl Timestamp { - /// Constructs a new [`Timestamp`] from an unsigned 32 bit integer value. +impl BlockTime { + /// Constructs a new [`BlockTime`] from an unsigned 32 bit integer value. #[inline] - pub const fn from_u32(t: u32) -> Self { Timestamp(t) } + pub const fn from_u32(t: u32) -> Self { BlockTime(t) } /// Returns the inner `u32` value. #[inline] pub const fn to_u32(self) -> u32 { self.0 } } -impl From for Timestamp { +impl From for BlockTime { #[inline] fn from(t: u32) -> Self { Self::from_u32(t) } } -impl From for u32 { +impl From for u32 { #[inline] - fn from(t: Timestamp) -> Self { t.to_u32() } + fn from(t: BlockTime) -> Self { t.to_u32() } } #[cfg(feature = "arbitrary")] -impl<'a> Arbitrary<'a> for Timestamp { +impl<'a> Arbitrary<'a> for BlockTime { #[inline] fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { let t: u32 = u.arbitrary()?; - Ok(Timestamp::from(t)) + Ok(BlockTime::from(t)) } } diff --git a/units/tests/api.rs b/units/tests/api.rs index 97a20e2a6..b8ec96c3c 100644 --- a/units/tests/api.rs +++ b/units/tests/api.rs @@ -15,7 +15,7 @@ use arbitrary::{Arbitrary, Unstructured}; use bitcoin_units::locktime::{absolute, relative}; // Typical usage is `absolute::Height`. use bitcoin_units::{ amount, block, fee_rate, locktime, parse, weight, Amount, BlockHeight, BlockInterval, FeeRate, - SignedAmount, Timestamp, Weight, + SignedAmount, BlockTime, Weight, }; /// A struct that includes all public non-error enums. @@ -42,7 +42,7 @@ struct Structs { i: relative::Height, j: relative::Time, k: Weight, - l: Timestamp, + l: BlockTime, } impl Structs { @@ -59,7 +59,7 @@ impl Structs { i: relative::Height::MAX, j: relative::Time::MAX, k: Weight::MAX, - l: Timestamp::from_u32(u32::MAX), + l: BlockTime::from_u32(u32::MAX), } } } @@ -89,7 +89,7 @@ struct CommonTraits { i: relative::Height, j: relative::Time, k: Weight, - l: Timestamp, + l: BlockTime, } /// A struct that includes all types that implement `Default`. @@ -141,13 +141,13 @@ struct Errors { #[test] fn api_can_use_modules_from_crate_root() { - use bitcoin_units::{amount, block, fee_rate, locktime, parse, timestamp, weight}; + use bitcoin_units::{amount, block, fee_rate, locktime, parse, time, weight}; } #[test] fn api_can_use_types_from_crate_root() { use bitcoin_units::{ - Amount, BlockHeight, BlockInterval, FeeRate, SignedAmount, Timestamp, Weight, + Amount, BlockHeight, BlockInterval, FeeRate, SignedAmount, BlockTime, Weight, }; } @@ -189,8 +189,8 @@ fn api_can_use_all_types_from_module_parse() { } #[test] -fn api_can_use_all_types_from_module_timestamp() { - use bitcoin_units::timestamp::Timestamp; +fn api_can_use_all_types_from_module_time() { + use bitcoin_units::time::BlockTime; } #[test] @@ -295,7 +295,7 @@ impl<'a> Arbitrary<'a> for Structs { i: relative::Height::arbitrary(u)?, j: relative::Time::arbitrary(u)?, k: Weight::arbitrary(u)?, - l: Timestamp::arbitrary(u)?, + l: BlockTime::arbitrary(u)?, }; Ok(a) }