Skip to content

Commit

Permalink
Rename txid to compute_txid
Browse files Browse the repository at this point in the history
Computing the txid is computationally expensive, so rename the method
accordingly.
  • Loading branch information
yancy committed Jan 20, 2024
1 parent 78ea5a1 commit b81f8fc
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bitcoin/src/bip152.rs
Expand Up @@ -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!(),
},
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/block.rs
Expand Up @@ -289,7 +289,7 @@ impl Block {

/// Computes the transaction merkle root.
pub fn compute_merkle_root(&self) -> Option<TxMerkleNode> {
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())
}

Expand Down Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/constants.rs
Expand Up @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions bitcoin/src/blockdata/transaction.rs
Expand Up @@ -698,15 +698,20 @@ impl Transaction {
.collect(),
output: self.output.clone(),
};
cloned_tx.txid().into()
cloned_tx.compute_txid().into()
}

#[deprecated(since = "0.31.0", note = "use compute_txid() instead")]
#[doc(alias = "compute_txid")]
#[doc(hidden)]
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 {
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");
Expand Down Expand Up @@ -1231,11 +1236,11 @@ impl Decodable for Transaction {
}

impl From<Transaction> 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<Transaction> for Wtxid {
Expand Down Expand Up @@ -1726,7 +1731,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!(
Expand Down Expand Up @@ -1766,7 +1771,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!(
Expand Down Expand Up @@ -1894,7 +1899,7 @@ mod tests {
"d6ac4a5e61657c4c604dcde855a1db74ec6b3e54f32695d72c5e11c7761ea1b4"
);
assert_eq!(
format!("{:x}", tx.txid()),
format!("{:x}", tx.compute_txid()),
"9652aa62b0e748caeec40c4cb7bc17c6792435cc3dfe447dd1ca24f912a1c6ec"
);
assert_eq!(tx.weight(), Weight::from_wu(2718));
Expand All @@ -1915,7 +1920,7 @@ mod tests {
"971ed48a62c143bbd9c87f4bafa2ef213cfa106c6e140f111931d0be307468dd"
);
assert_eq!(
format!("{:x}", tx.txid()),
format!("{:x}", tx.compute_txid()),
"971ed48a62c143bbd9c87f4bafa2ef213cfa106c6e140f111931d0be307468dd"
);
}
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/merkle_tree/block.rs
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/merkle_tree/mod.rs
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/psbt/error.rs
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/psbt/mod.rs
Expand Up @@ -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());
Expand Down Expand Up @@ -1664,7 +1664,7 @@ mod tests {

let tx = &psbt.unsigned_tx;
assert_eq!(
tx.txid(),
tx.compute_txid(),
"75c5c9665a570569ad77dd1279e6fd4628a093c4dcbf8d41532614044c14c115".parse().unwrap(),
);

Expand Down

0 comments on commit b81f8fc

Please sign in to comment.