Repository: rust-bitcoin/rust-bitcoin
Pull request #4219 (2025-03-07 21:34:13)

Rename then new `Timestamp` type
Done in two patches so we can bikeshed the name of the type and separately the name of the module.

- Rename type: `Timestamp` to `BlockTime`
- Rename module: `timestamp` to `time`


Patch:
diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs
index c7b69526..e602a313 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 @@ fn dummy_block() -> Block<BlockChecked> {
             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 dfd039e5..a8dae5e4 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -13,7 +13,7 @@
 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 @@ fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Erro
     }
 }
 
-impl Encodable for Timestamp {
+impl Encodable for BlockTime {
     fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
         self.to_u32().consensus_encode(w)
     }
 }
 
-impl Decodable for Timestamp {
+impl Decodable for BlockTime {
     fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
-        Decodable::consensus_decode(r).map(Timestamp::from_u32)
+        Decodable::consensus_decode(r).map(BlockTime::from_u32)
     }
 }
 
@@ -609,7 +609,7 @@ fn block() {
             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 @@ fn segwit_block() {
             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 2c5cb4e1..3d8dc256 100644
--- a/bitcoin/src/blockdata/constants.rs
+++ b/bitcoin/src/blockdata/constants.rs
@@ -16,7 +16,7 @@
 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;
@@ -132,7 +132,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
                 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,
             },
@@ -144,7 +144,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
                 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,
             },
@@ -156,7 +156,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
                 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,
             },
@@ -168,7 +168,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
                 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,
             },
@@ -180,7 +180,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
                 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,
             },
@@ -320,7 +320,7 @@ fn bitcoin_genesis_full_block() {
             "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!(
@@ -338,7 +338,7 @@ fn testnet_genesis_full_block() {
             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!(
@@ -356,7 +356,7 @@ fn signet_genesis_full_block() {
             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 30824831..792f23df 100644
--- a/bitcoin/src/lib.rs
+++ b/bitcoin/src/lib.rs
@@ -135,7 +135,7 @@
     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 09929f64..bb59edb5 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -1078,7 +1078,7 @@ pub fn u64_to_target(u: u64) -> Target { Target(U256::from(u)) }
 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 @@ fn compact_target_from_upwards_difficulty_adjustment_using_headers() {
             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 @@ fn compact_target_from_downwards_difficulty_adjustment_using_headers() {
             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 @@ fn compact_target_from_downwards_difficulty_adjustment_using_headers() {
             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 88eefad9..18cbdc09 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -14,7 +14,7 @@
 #[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.
@@ -402,7 +402,7 @@ fn header_size() {
             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 abca776b..31fb210a 100644
--- a/primitives/src/lib.rs
+++ b/primitives/src/lib.rs
@@ -58,7 +58,7 @@ pub mod _core {
     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 7654d672..4a166ebb 100644
--- a/units/src/lib.rs
+++ b/units/src/lib.rs
@@ -34,7 +34,7 @@ pub mod _core {
 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 mod _core {
     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 35e59d8b..41faec70 100644
--- a/units/src/timestamp.rs
+++ b/units/src/time.rs
@@ -26,33 +26,33 @@
 /// ref: <https://en.bitcoin.it/wiki/Block_timestamp>
 #[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<u32> for Timestamp {
+impl From<u32> for BlockTime {
     #[inline]
     fn from(t: u32) -> Self { Self::from_u32(t) }
 }
 
-impl From<Timestamp> for u32 {
+impl From<BlockTime> 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<Self> {
         let t: u32 = u.arbitrary()?;
-        Ok(Timestamp::from(t))
+        Ok(BlockTime::from(t))
     }
 }


Test patch:
diff --git a/units/tests/api.rs b/units/tests/api.rs
index 97a20e2a..b8ec96c3 100644
--- a/units/tests/api.rs
+++ b/units/tests/api.rs
@@ -15,7 +15,7 @@
 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 @@ fn max() -> Self {
             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 @@ fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
             i: relative::Height::arbitrary(u)?,
             j: relative::Time::arbitrary(u)?,
             k: Weight::arbitrary(u)?,
-            l: Timestamp::arbitrary(u)?,
+            l: BlockTime::arbitrary(u)?,
         };
         Ok(a)
     }