delete PackedLockTime by aliasing it to LockTime

The next commit will be a mechanical s/PackedLockTime/LockTime/; this commit
seemed like the easiest way to facilitate that.
This commit is contained in:
Andrew Poelstra 2022-12-11 18:50:23 +00:00
parent fa81568fb6
commit 4dee116b8a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 13 additions and 126 deletions

View File

@ -412,7 +412,7 @@ impl BenefactorWallet {
// CREATOR + UPDATER // CREATOR + UPDATER
let next_tx = Transaction { let next_tx = Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(lock_time), lock_time: absolute::PackedLockTime::from_consensus(lock_time),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { txid: tx.txid(), vout: 0 }, previous_output: OutPoint { txid: tx.txid(), vout: 0 },
script_sig: Script::new(), script_sig: Script::new(),
@ -482,7 +482,7 @@ impl BenefactorWallet {
self.beneficiary_xpub.derive_pub(&self.secp, &new_derivation_path)?.to_x_only_pub(); self.beneficiary_xpub.derive_pub(&self.secp, &new_derivation_path)?.to_x_only_pub();
// Build up the leaf script and combine with internal key into a taproot commitment // Build up the leaf script and combine with internal key into a taproot commitment
let lock_time = psbt.unsigned_tx.lock_time.to_u32() + lock_time_delta; let lock_time = psbt.unsigned_tx.lock_time.to_consensus_u32() + lock_time_delta;
let script = Self::time_lock_script(lock_time, beneficiary_key); let script = Self::time_lock_script(lock_time, beneficiary_key);
let leaf_hash = TapLeafHash::from_script(&script, LeafVersion::TapScript); let leaf_hash = TapLeafHash::from_script(&script, LeafVersion::TapScript);
@ -557,7 +557,7 @@ impl BenefactorWallet {
let next_tx = Transaction { let next_tx = Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(lock_time), lock_time: absolute::PackedLockTime::from_consensus(lock_time),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { txid: tx.txid(), vout: 0 }, previous_output: OutPoint { txid: tx.txid(), vout: 0 },
script_sig: Script::new(), script_sig: Script::new(),
@ -632,7 +632,7 @@ impl BeneficiaryWallet {
let input_value = psbt.inputs[0].witness_utxo.as_ref().unwrap().value; let input_value = psbt.inputs[0].witness_utxo.as_ref().unwrap().value;
let input_script_pubkey = let input_script_pubkey =
psbt.inputs[0].witness_utxo.as_ref().unwrap().script_pubkey.clone(); psbt.inputs[0].witness_utxo.as_ref().unwrap().script_pubkey.clone();
psbt.unsigned_tx.lock_time = absolute::PackedLockTime(lock_time); psbt.unsigned_tx.lock_time = absolute::PackedLockTime::from_consensus(lock_time);
psbt.unsigned_tx.output = vec![TxOut { psbt.unsigned_tx.output = vec![TxOut {
script_pubkey: to_address.script_pubkey(), script_pubkey: to_address.script_pubkey(),
value: input_value - ABSOLUTE_FEES_IN_SATS, value: input_value - ABSOLUTE_FEES_IN_SATS,

View File

@ -37,122 +37,8 @@ use crate::absolute;
/// [Bitcoin Core]: https://github.com/bitcoin/bitcoin/blob/9ccaee1d5e2e4b79b0a7c29aadb41b97e4741332/src/script/script.h#L39 /// [Bitcoin Core]: https://github.com/bitcoin/bitcoin/blob/9ccaee1d5e2e4b79b0a7c29aadb41b97e4741332/src/script/script.h#L39
pub const LOCK_TIME_THRESHOLD: u32 = 500_000_000; pub const LOCK_TIME_THRESHOLD: u32 = 500_000_000;
/// Packed lock time wraps a [`LockTime`] consensus value i.e., the raw `u32` used by the network. /// Will be deleted in next commit
/// pub type PackedLockTime = LockTime;
/// This struct may be preferred in performance-critical applications because it's slightly smaller
/// than [`LockTime`] and has a bit more performant (de)serialization. In particular, this may be
/// relevant when the value is not processed, just passed around. Note however that the difference
/// is super-small, so unless you do something extreme you shouldn't worry about it.
///
/// This type implements a naive ordering based on the `u32`, this is _not_ a semantically correct
/// ordering for a lock time, hence [`LockTime`] does not implement `Ord`. This type is useful if
/// you want to use a lock time as part of a struct and wish to derive `Ord`. For all other uses,
/// consider using [`LockTime`] directly.
///
/// # Examples
/// ```
/// # use bitcoin::Amount;
/// # use bitcoin::absolute::{PackedLockTime, LockTime};
/// #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
/// struct S {
/// lock_time: PackedLockTime,
/// amount: Amount,
/// }
///
/// let _ = S {
/// lock_time: LockTime::from_consensus(741521).into(),
/// amount: Amount::from_sat(10_000_000),
/// };
/// ```
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
pub struct PackedLockTime(pub u32);
impl PackedLockTime {
/// If [`crate::Transaction::lock_time`] is set to zero it is ignored, in other words a
/// transaction with nLocktime==0 is able to be included immediately in any block.
pub const ZERO: PackedLockTime = PackedLockTime(0);
/// Returns the inner `u32`.
#[inline]
pub fn to_u32(self) -> u32 {
self.0
}
}
impl fmt::Display for PackedLockTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl FromHexStr for PackedLockTime {
type Error = Error;
fn from_hex_str_no_prefix<S: AsRef<str> + Into<String>>(s: S) -> Result<Self, Self::Error> {
let packed_lock_time = crate::parse::hex_u32(s)?;
Ok(Self(packed_lock_time))
}
}
impl Encodable for PackedLockTime {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
self.0.consensus_encode(w)
}
}
impl Decodable for PackedLockTime {
#[inline]
fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
u32::consensus_decode(r).map(PackedLockTime)
}
}
impl From<LockTime> for PackedLockTime {
fn from(n: LockTime) -> Self {
PackedLockTime(n.to_consensus_u32())
}
}
impl From<PackedLockTime> for LockTime {
fn from(n: PackedLockTime) -> Self {
LockTime::from_consensus(n.0)
}
}
impl From<&LockTime> for PackedLockTime {
fn from(n: &LockTime) -> Self {
PackedLockTime(n.to_consensus_u32())
}
}
impl From<&PackedLockTime> for LockTime {
fn from(n: &PackedLockTime) -> Self {
LockTime::from_consensus(n.0)
}
}
impl From<PackedLockTime> for u32 {
fn from(p: PackedLockTime) -> Self {
p.0
}
}
impl_parse_str_through_int!(PackedLockTime);
impl fmt::LowerHex for PackedLockTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
impl fmt::UpperHex for PackedLockTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", self.0)
}
}
/// An absolute lock time value, representing either a block height or a UNIX timestamp (seconds /// An absolute lock time value, representing either a block height or a UNIX timestamp (seconds
/// since epoch). /// since epoch).
@ -179,6 +65,7 @@ impl fmt::UpperHex for PackedLockTime {
/// ``` /// ```
#[allow(clippy::derive_ord_xor_partial_ord)] #[allow(clippy::derive_ord_xor_partial_ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Ord)] // will be removed in next commit
pub enum LockTime { pub enum LockTime {
/// A block height lock time value. /// A block height lock time value.
/// ///
@ -848,14 +735,14 @@ mod tests {
#[test] #[test]
fn packed_lock_time_from_str_hex_happy_path() { fn packed_lock_time_from_str_hex_happy_path() {
let actual = PackedLockTime::from_hex_str("0xBA70D").unwrap(); let actual = PackedLockTime::from_hex_str("0xBA70D").unwrap();
let expected = PackedLockTime(0xBA70D); let expected = PackedLockTime::from_consensus(0xBA70D);
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
fn packed_lock_time_from_str_hex_no_prefix_happy_path() { fn packed_lock_time_from_str_hex_no_prefix_happy_path() {
let lock_time = PackedLockTime::from_hex_str_no_prefix("BA70D").unwrap(); let lock_time = PackedLockTime::from_hex_str_no_prefix("BA70D").unwrap();
assert_eq!(lock_time, PackedLockTime(0xBA70D)); assert_eq!(lock_time, PackedLockTime::from_consensus(0xBA70D));
} }
#[test] #[test]

View File

@ -977,7 +977,7 @@ mod tests {
let expected = PartiallySignedTransaction { let expected = PartiallySignedTransaction {
unsigned_tx: Transaction { unsigned_tx: Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(1257139), lock_time: absolute::PackedLockTime::from_consensus(1257139),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { previous_output: OutPoint {
txid: Txid::from_hex( txid: Txid::from_hex(
@ -1237,7 +1237,7 @@ mod tests {
let unserialized = PartiallySignedTransaction { let unserialized = PartiallySignedTransaction {
unsigned_tx: Transaction { unsigned_tx: Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(1257139), lock_time: absolute::PackedLockTime::from_consensus(1257139),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { previous_output: OutPoint {
txid: Txid::from_hex( txid: Txid::from_hex(
@ -1549,7 +1549,7 @@ mod tests {
let mut unserialized = PartiallySignedTransaction { let mut unserialized = PartiallySignedTransaction {
unsigned_tx: Transaction { unsigned_tx: Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(1257139), lock_time: absolute::PackedLockTime::from_consensus(1257139),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { previous_output: OutPoint {
txid: Txid::from_hex( txid: Txid::from_hex(
@ -1719,7 +1719,7 @@ mod tests {
let mut t = PartiallySignedTransaction { let mut t = PartiallySignedTransaction {
unsigned_tx: Transaction { unsigned_tx: Transaction {
version: 2, version: 2,
lock_time: absolute::PackedLockTime(1257139), lock_time: absolute::PackedLockTime::from_consensus(1257139),
input: vec![TxIn { input: vec![TxIn {
previous_output: OutPoint { previous_output: OutPoint {
txid: Txid::from_hex( txid: Txid::from_hex(