Skip to content

Commit

Permalink
Merge #2382: Improve private function use_segwit_serialization
Browse files Browse the repository at this point in the history
dae16f0 Use any method on iterator (Tobin C. Harding)
671dc0e Use better predicate name (Tobin C. Harding)

Pull request description:

  - Patch 1: Improve the name.
  - Patch 2: Use `any` instead of manual loop.

ACKs for top commit:
  Kixunil:
    ACK dae16f0
  apoelstra:
    ACK dae16f0

Tree-SHA512: 9b98c21cbb5fa93c011ba8bae88ab0dd2efa807d7cb220f121c56b31fbe5a9e9cd6f29badeca092e22949dc278c1a9d3dd676cd2919a11f13101d0dd83ec9313
  • Loading branch information
apoelstra committed Jan 23, 2024
2 parents e003336 + dae16f0 commit ccbc976
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,9 @@ impl Transaction {
#[inline]
pub fn total_size(&self) -> usize {
let mut size: usize = 4; // Serialized length of a u32 for the version number.
let use_segwit = self.use_segwit_serialization();
let uses_segwit = self.uses_segwit_serialization();

if use_segwit {
if uses_segwit {
size += 2; // 1 byte for the marker and 1 for the flag.
}

Expand All @@ -784,7 +784,7 @@ impl Transaction {
.input
.iter()
.map(|input| {
if use_segwit {
if uses_segwit {
input.total_size()
} else {
input.base_size()
Expand Down Expand Up @@ -984,11 +984,9 @@ impl Transaction {
}

/// Returns whether or not to serialize transaction as specified in BIP-144.
fn use_segwit_serialization(&self) -> bool {
for input in &self.input {
if !input.witness.is_empty() {
return true;
}
fn uses_segwit_serialization(&self) -> bool {
if self.input.iter().any(|input| !input.witness.is_empty()) {
return true;
}
// To avoid serialization ambiguity, no inputs means we use BIP141 serialization (see
// `Transaction` docs for full explanation).
Expand Down Expand Up @@ -1175,7 +1173,7 @@ impl Encodable for Transaction {
len += self.version.consensus_encode(w)?;

// Legacy transaction serialization format only includes inputs and outputs.
if !self.use_segwit_serialization() {
if !self.uses_segwit_serialization() {
len += self.input.consensus_encode(w)?;
len += self.output.consensus_encode(w)?;
} else {
Expand Down Expand Up @@ -2167,14 +2165,14 @@ mod tests {
for (is_segwit, tx, expected_weight) in &txs {
let txin_weight = if *is_segwit { TxIn::segwit_weight } else { TxIn::legacy_weight };
let tx: Transaction = deserialize(Vec::from_hex(tx).unwrap().as_slice()).unwrap();
assert_eq!(*is_segwit, tx.use_segwit_serialization());
assert_eq!(*is_segwit, tx.uses_segwit_serialization());

let mut calculated_weight = empty_transaction_weight
+ tx.input.iter().fold(Weight::ZERO, |sum, i| sum + txin_weight(i))
+ tx.output.iter().fold(Weight::ZERO, |sum, o| sum + o.weight());

// The empty tx uses segwit serialization but a legacy tx does not.
if !tx.use_segwit_serialization() {
if !tx.uses_segwit_serialization() {
calculated_weight -= Weight::from_wu(2);
}

Expand Down

0 comments on commit ccbc976

Please sign in to comment.