Skip to content

Commit 742b4e0

Browse files
committed
Remove from_witness_data_size and from_non_witness_data_size
Weight::from_non_witness_data_size has an overflow bug. These methods duplicate the functionality of `Weight::from_vb` and `Weight::from_wu`, respectively. Furthermore, they are less discoverable than the methods they duplicate, so they will be removed in this PR.
1 parent 674ac57 commit 742b4e0

File tree

3 files changed

+7
-32
lines changed

3 files changed

+7
-32
lines changed

bitcoin/src/blockdata/transaction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ internal_macros::define_extension_trait! {
134134
/// Keep in mind that when adding a TxIn to a transaction, the total weight of the transaction
135135
/// might increase more than `TxIn::legacy_weight`. This happens when the new input added causes
136136
/// the input length `CompactSize` to increase its encoding length.
137-
fn legacy_weight(&self) -> Weight {
138-
Weight::from_non_witness_data_size(self.base_size().to_u64())
137+
fn legacy_weight(&self) -> Option<Weight> {
138+
Weight::from_vb(self.base_size().to_u64())
139139
}
140140

141141
/// The weight of the TxIn when it's included in a SegWit transaction (i.e., a transaction
@@ -149,9 +149,9 @@ internal_macros::define_extension_trait! {
149149
/// - the new input added causes the input length `CompactSize` to increase its encoding length
150150
/// - the new input is the first segwit input added - this will add an additional 2WU to the
151151
/// transaction weight to take into account the SegWit marker
152-
fn segwit_weight(&self) -> Weight {
153-
Weight::from_non_witness_data_size(self.base_size().to_u64())
154-
+ Weight::from_witness_data_size(self.witness.size().to_u64())
152+
fn segwit_weight(&self) -> Option<Weight> {
153+
Weight::from_vb(self.base_size().to_u64())
154+
.map(|w| w + Weight::from_wu(self.witness.size().to_u64()))
155155
}
156156

157157
/// Returns the base size of this input.
@@ -1805,7 +1805,7 @@ mod tests {
18051805
assert_eq!(*is_segwit, tx.uses_segwit_serialization());
18061806

18071807
let mut calculated_weight = empty_transaction_weight
1808-
+ tx.inputs.iter().fold(Weight::ZERO, |sum, i| sum + txin_weight(i))
1808+
+ tx.inputs.iter().fold(Weight::ZERO, |sum, i| sum + txin_weight(i).expect("valid weight"))
18091809
+ tx.outputs.iter().fold(Weight::ZERO, |sum, o| sum + o.weight());
18101810

18111811
// The empty tx uses SegWit serialization but a legacy tx does not.

fuzz/fuzz_targets/units/arbitrary_weight.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ fn do_test(data: &[u8]) {
3333
}
3434

3535
// Constructors that return a Weight
36-
for constructor in
37-
[Weight::from_wu, Weight::from_witness_data_size, Weight::from_non_witness_data_size]
36+
for constructor in [Weight::from_wu]
3837
{
3938
if let Ok(val) = u.arbitrary() {
4039
constructor(val);

units/src/weight.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ impl Weight {
9595
Weight::from_wu(vb * Self::WITNESS_SCALE_FACTOR)
9696
}
9797

98-
/// Constructs a new [`Weight`] from witness size.
99-
pub const fn from_witness_data_size(witness_size: u64) -> Self { Weight::from_wu(witness_size) }
100-
101-
/// Constructs a new [`Weight`] from non-witness size.
102-
pub const fn from_non_witness_data_size(non_witness_size: u64) -> Self {
103-
Weight::from_wu(non_witness_size * Self::WITNESS_SCALE_FACTOR)
104-
}
105-
10698
/// Converts to kilo weight units rounding down.
10799
pub const fn to_kwu_floor(self) -> u64 { self.to_wu() / 1000 }
108100

@@ -361,22 +353,6 @@ mod tests {
361353
#[should_panic = "attempt to multiply with overflow"]
362354
fn from_vb_unchecked_panic() { Weight::from_vb_unchecked(u64::MAX); }
363355

364-
#[test]
365-
fn from_witness_data_size() {
366-
let witness_data_size = 1;
367-
let got = Weight::from_witness_data_size(witness_data_size);
368-
let want = Weight::from_wu(witness_data_size);
369-
assert_eq!(got, want);
370-
}
371-
372-
#[test]
373-
fn from_non_witness_data_size() {
374-
let non_witness_data_size = 1;
375-
let got = Weight::from_non_witness_data_size(non_witness_data_size);
376-
let want = Weight::from_wu(non_witness_data_size * 4);
377-
assert_eq!(got, want);
378-
}
379-
380356
#[test]
381357
fn to_kwu_floor() {
382358
assert_eq!(Weight::from_wu(5_000).to_kwu_floor(), 5);

0 commit comments

Comments
 (0)