Remove quadratic algorithm

Currently we loop over transaction inputs within a loop over transaction
inputs - ouch.

Cache the `use_segwit_serialization` outside the iteration loop.

Fix: #2357
This commit is contained in:
Tobin C. Harding 2024-01-19 06:57:31 +11:00
parent 9eec1082ec
commit a338a61cc3
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 3 additions and 2 deletions

View File

@ -773,8 +773,9 @@ impl Transaction {
#[inline] #[inline]
pub fn total_size(&self) -> usize { pub fn total_size(&self) -> usize {
let mut size: usize = 4; // Serialized length of a u32 for the version number. let mut size: usize = 4; // Serialized length of a u32 for the version number.
let use_segwit = self.use_segwit_serialization();
if self.use_segwit_serialization() { if use_segwit {
size += 2; // 1 byte for the marker and 1 for the flag. size += 2; // 1 byte for the marker and 1 for the flag.
} }
@ -783,7 +784,7 @@ impl Transaction {
.input .input
.iter() .iter()
.map(|input| { .map(|input| {
if self.use_segwit_serialization() { if use_segwit {
input.total_size() input.total_size()
} else { } else {
input.base_size() input.base_size()