From 57a761397382c07035799e127152fd5a68947e67 Mon Sep 17 00:00:00 2001 From: yancy Date: Sat, 20 Jan 2024 17:29:13 +0100 Subject: [PATCH] Rename txid to compute_txid Computing the txid is computationally expensive, so rename the method accordingly. --- bitcoin/examples/taproot-psbt.rs | 4 ++-- bitcoin/src/bip152.rs | 2 +- bitcoin/src/blockdata/block.rs | 4 ++-- bitcoin/src/blockdata/constants.rs | 2 +- bitcoin/src/blockdata/transaction.rs | 32 ++++++++++++++++++---------- bitcoin/src/merkle_tree/block.rs | 2 +- bitcoin/src/merkle_tree/mod.rs | 2 +- bitcoin/src/psbt/error.rs | 4 ++-- bitcoin/src/psbt/mod.rs | 4 ++-- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 66f6b796..6827e0af 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -425,7 +425,7 @@ impl BenefactorWallet { version: transaction::Version::TWO, lock_time, input: vec![TxIn { - previous_output: OutPoint { txid: tx.txid(), vout: 0 }, + previous_output: OutPoint { txid: tx.compute_txid(), vout: 0 }, script_sig: ScriptBuf::new(), sequence: bitcoin::Sequence(0xFFFFFFFD), // enable locktime and opt-in RBF witness: Witness::default(), @@ -568,7 +568,7 @@ impl BenefactorWallet { version: transaction::Version::TWO, lock_time, input: vec![TxIn { - previous_output: OutPoint { txid: tx.txid(), vout: 0 }, + previous_output: OutPoint { txid: tx.compute_txid(), vout: 0 }, script_sig: ScriptBuf::new(), sequence: bitcoin::Sequence(0xFFFFFFFD), // enable locktime and opt-in RBF witness: Witness::default(), diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index 376e9877..634984a7 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -219,7 +219,7 @@ impl HeaderAndShortIds { } else { short_ids.push(ShortId::with_siphash_keys( &match version { - 1 => tx.txid().to_raw_hash(), + 1 => tx.compute_txid().to_raw_hash(), 2 => tx.wtxid().to_raw_hash(), _ => unreachable!(), }, diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index b8e7f5a8..f9999c1b 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -289,7 +289,7 @@ impl Block { /// Computes the transaction merkle root. pub fn compute_merkle_root(&self) -> Option { - let hashes = self.txdata.iter().map(|obj| obj.txid().to_raw_hash()); + let hashes = self.txdata.iter().map(|obj| obj.compute_txid().to_raw_hash()); merkle_tree::calculate_root(hashes).map(|h| h.into()) } @@ -491,7 +491,7 @@ mod tests { let block: Block = deserialize(&hex!(BLOCK_HEX)).unwrap(); let cb_txid = "d574f343976d8e70d91cb278d21044dd8a396019e6db70755a0a50e4783dba38"; - assert_eq!(block.coinbase().unwrap().txid().to_string(), cb_txid); + assert_eq!(block.coinbase().unwrap().compute_txid().to_string(), cb_txid); assert_eq!(block.bip34_block_height(), Ok(100_000)); diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index a74e029a..d71a4f9a 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -88,7 +88,7 @@ fn bitcoin_genesis_tx() -> Transaction { /// Constructs and returns the genesis block. pub fn genesis_block(network: Network) -> Block { let txdata = vec![bitcoin_genesis_tx()]; - let hash: sha256d::Hash = txdata[0].txid().into(); + let hash: sha256d::Hash = txdata[0].compute_txid().into(); let merkle_root = hash.into(); match network { Network::Bitcoin => Block { diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index fa0fe508..23e33bad 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -698,15 +698,25 @@ impl Transaction { .collect(), output: self.output.clone(), }; - cloned_tx.txid().into() + cloned_tx.compute_txid().into() } + /// Computes the [`Txid`]. + /// + /// This method is deprecated. Use `compute_txid` instead. + #[deprecated( + since = "0.31.0", + note = "txid has been renamed to compute_txid to note that it's computationally expensive. use compute_txid() instead." + )] + pub fn txid(&self) -> Txid { self.compute_txid() } + /// Computes the [`Txid`]. /// /// Hashes the transaction **excluding** the segwit data (i.e. the marker, flag bytes, and the /// witness fields themselves). For non-segwit transactions which do not have any segwit data, /// this will be equal to [`Transaction::wtxid()`]. - pub fn txid(&self) -> Txid { + #[doc(alias = "txid")] + pub fn compute_txid(&self) -> Txid { let mut enc = Txid::engine(); self.version.consensus_encode(&mut enc).expect("engines don't error"); self.input.consensus_encode(&mut enc).expect("engines don't error"); @@ -1237,11 +1247,11 @@ impl Decodable for Transaction { } impl From for Txid { - fn from(tx: Transaction) -> Txid { tx.txid() } + fn from(tx: Transaction) -> Txid { tx.compute_txid() } } impl From<&Transaction> for Txid { - fn from(tx: &Transaction) -> Txid { tx.txid() } + fn from(tx: &Transaction) -> Txid { tx.compute_txid() } } impl From for Wtxid { @@ -1732,7 +1742,7 @@ mod tests { assert_eq!(realtx.lock_time, absolute::LockTime::ZERO); assert_eq!( - format!("{:x}", realtx.txid()), + format!("{:x}", realtx.compute_txid()), "a6eab3c14ab5272a58a5ba91505ba1a4b6d7a3a9fcbd187b6cd99a7b6d548cb7".to_string() ); assert_eq!( @@ -1783,7 +1793,7 @@ mod tests { assert_eq!(realtx.lock_time, absolute::LockTime::ZERO); assert_eq!( - format!("{:x}", realtx.txid()), + format!("{:x}", realtx.compute_txid()), "f5864806e3565c34d1b41e716f72609d00b55ea5eac5b924c9719a842ef42206".to_string() ); assert_eq!( @@ -1911,7 +1921,7 @@ mod tests { "d6ac4a5e61657c4c604dcde855a1db74ec6b3e54f32695d72c5e11c7761ea1b4" ); assert_eq!( - format!("{:x}", tx.txid()), + format!("{:x}", tx.compute_txid()), "9652aa62b0e748caeec40c4cb7bc17c6792435cc3dfe447dd1ca24f912a1c6ec" ); assert_eq!(tx.weight(), Weight::from_wu(2718)); @@ -1932,7 +1942,7 @@ mod tests { "971ed48a62c143bbd9c87f4bafa2ef213cfa106c6e140f111931d0be307468dd" ); assert_eq!( - format!("{:x}", tx.txid()), + format!("{:x}", tx.compute_txid()), "971ed48a62c143bbd9c87f4bafa2ef213cfa106c6e140f111931d0be307468dd" ); } @@ -2017,9 +2027,9 @@ mod tests { .as_slice()).unwrap(); let mut spent = HashMap::new(); - spent.insert(spent1.txid(), spent1); - spent.insert(spent2.txid(), spent2); - spent.insert(spent3.txid(), spent3); + spent.insert(spent1.compute_txid(), spent1); + spent.insert(spent2.compute_txid(), spent2); + spent.insert(spent3.compute_txid(), spent3); let mut spent2 = spent.clone(); let mut spent3 = spent.clone(); diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index 402ffaab..827d06d5 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -103,7 +103,7 @@ impl MerkleBlock { where F: Fn(&Txid) -> bool, { - let block_txids: Vec<_> = block.txdata.iter().map(Transaction::txid).collect(); + let block_txids: Vec<_> = block.txdata.iter().map(Transaction::compute_txid).collect(); Self::from_header_txids_with_predicate(&block.header, &block_txids, match_txids) } diff --git a/bitcoin/src/merkle_tree/mod.rs b/bitcoin/src/merkle_tree/mod.rs index 0e79117a..c3929d42 100644 --- a/bitcoin/src/merkle_tree/mod.rs +++ b/bitcoin/src/merkle_tree/mod.rs @@ -125,7 +125,7 @@ mod tests { let block: Block = deserialize(&segwit_block[..]).expect("Failed to deserialize block"); assert!(block.check_merkle_root()); // Sanity check. - let hashes_iter = block.txdata.iter().map(|obj| obj.txid().to_raw_hash()); + let hashes_iter = block.txdata.iter().map(|obj| obj.compute_txid().to_raw_hash()); let mut hashes_array: [sha256d::Hash; 15] = [Hash::all_zeros(); 15]; for (i, hash) in hashes_iter.clone().enumerate() { diff --git a/bitcoin/src/psbt/error.rs b/bitcoin/src/psbt/error.rs index 2e41332a..0243cf96 100644 --- a/bitcoin/src/psbt/error.rs +++ b/bitcoin/src/psbt/error.rs @@ -127,8 +127,8 @@ impl fmt::Display for Error { UnexpectedUnsignedTx { expected: ref e, actual: ref a } => write!( f, "different unsigned transaction: expected {}, actual {}", - e.txid(), - a.txid() + e.compute_txid(), + a.compute_txid() ), NonStandardSighashType(ref sht) => write!(f, "non-standard sighash type: {}", sht), InvalidHash(ref e) => write_err!(f, "invalid hash when parsing slice"; e), diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 26ed4993..23df1b0e 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -1597,7 +1597,7 @@ mod tests { let tx_input = &psbt.unsigned_tx.input[0]; let psbt_non_witness_utxo = psbt.inputs[0].non_witness_utxo.as_ref().unwrap(); - assert_eq!(tx_input.previous_output.txid, psbt_non_witness_utxo.txid()); + assert_eq!(tx_input.previous_output.txid, psbt_non_witness_utxo.compute_txid()); assert!(psbt_non_witness_utxo.output[tx_input.previous_output.vout as usize] .script_pubkey .is_p2pkh()); @@ -1664,7 +1664,7 @@ mod tests { let tx = &psbt.unsigned_tx; assert_eq!( - tx.txid(), + tx.compute_txid(), "75c5c9665a570569ad77dd1279e6fd4628a093c4dcbf8d41532614044c14c115".parse().unwrap(), );