Rename txid to compute_txid

Computing the txid is computationally expensive, so rename the method
accordingly.
This commit is contained in:
yancy 2024-01-20 17:29:13 +01:00
parent 8b552c7706
commit 57a7613973
9 changed files with 33 additions and 23 deletions

View File

@ -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(),

View File

@ -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!(),
},

View File

@ -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())
}
@ -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));

View File

@ -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 {

View File

@ -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<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 {
@ -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();

View File

@ -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)
}

View File

@ -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() {

View File

@ -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),

View File

@ -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(),
);