Update `segwit_weight` and `legacy_weight` to use non-deprecated functions
What changed, and why it matters
This commit is a routine internal cleanup. It replaces deprecated function names with their newer equivalents in two helper methods that calculate Bitcoin transaction weight. The actual math and behavior remain the same, and the old functions are still kept (just marked as deprecated). There is no security issue here.
No security action needed. This is a normal refactoring/deprecation maintenance commit. Reviewers may verify that the new `unwrap()` calls are acceptable given the prior panic-on-overflow behavior.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch updates legacy_weight and segwit_weight in bitcoin/src/blockdata/transaction.rs to call Weight::from_vb/from_wu instead of the now-deprecated Weight::from_non_witness_data_size/from_witness_data_size. It also updates a test in block.rs to use from_vb_unchecked. The deprecated functions are retained in units/src/weight.rs with #[deprecated] attributes, and their tests are annotated with #[allow(deprecated)]. The computations are semantically equivalent: from_non_witness_data_size(x) equals from_vb(x).unwrap() (both multiply by WITNESS_SCALE_FACTOR), and from_witness_data_size(x) equals from_wu(x). The only functional change is that legacy_weight and segwit_weight now use checked_add/unwrap and from_vb/unwrap, which can panic on overflow, but the prior implementation using from_non_witness_data_size also panicked on overflow (documented in its doc comment), so behavior is unchanged.
Changed components
bitcoin/src/blockdata/transaction.rsbitcoin/src/blockdata/block.rsunits/src/weight.rsInspect captured patch +10 / −4
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index 5725d05b..7558beef 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -659,7 +659,7 @@ mod tests {
assert_eq!(block_base_size(real_decode.transactions()), some_block.len());
assert_eq!(
real_decode.weight(),
- Weight::from_non_witness_data_size(some_block.len().to_u64())
+ Weight::from_vb_unchecked(some_block.len().to_u64())
);
assert_eq!(serialize(&real_decode), some_block);
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index 9b79cbf5..51210477 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -139,7 +139,7 @@ internal_macros::define_extension_trait! {
///
/// If the coversion overflows.
fn legacy_weight(&self) -> Weight {
- Weight::from_non_witness_data_size(self.base_size().to_u64())
+ Weight::from_vb(self.base_size().to_u64()).unwrap()
}
/// The weight of the TxIn when it's included in a SegWit transaction (i.e., a transaction
@@ -158,8 +158,8 @@ internal_macros::define_extension_trait! {
///
/// If the coversion overflows.
fn segwit_weight(&self) -> Weight {
- Weight::from_non_witness_data_size(self.base_size().to_u64())
- + Weight::from_witness_data_size(self.witness.size().to_u64())
+ Weight::from_vb(self.base_size().to_u64())
+ .and_then(|w| w.checked_add(Weight::from_wu(self.witness.size().to_u64()))).unwrap()
}
/// Returns the base size of this input.
diff --git a/units/src/weight.rs b/units/src/weight.rs
index 3d8d257d..4b67a2eb 100644
--- a/units/src/weight.rs
+++ b/units/src/weight.rs
@@ -96,6 +96,7 @@ impl Weight {
}
/// Constructs a new [`Weight`] from witness size.
+ #[deprecated(since = "TBD", note = "use `from_wu` instead")]
pub const fn from_witness_data_size(witness_size: u64) -> Self { Weight::from_wu(witness_size) }
/// Constructs a new [`Weight`] from non-witness size.
@@ -103,6 +104,7 @@ impl Weight {
/// # Panics
///
/// If the conversion from virtual bytes overflows.
+ #[deprecated(since = "TBD", note = "use `from_vb` or `from_vb_unchecked` instead")]
pub const fn from_non_witness_data_size(non_witness_size: u64) -> Self {
Weight::from_wu(non_witness_size * Self::WITNESS_SCALE_FACTOR)
}
@@ -366,6 +368,8 @@ mod tests {
fn from_vb_unchecked_panic() { Weight::from_vb_unchecked(u64::MAX); }
#[test]
+ #[allow(deprecated)] // tests the deprecated function
+ #[allow(deprecated_in_future)]
fn from_witness_data_size() {
let witness_data_size = 1;
let got = Weight::from_witness_data_size(witness_data_size);
@@ -374,6 +378,8 @@ mod tests {
}
#[test]
+ #[allow(deprecated)] // tests the deprecated function
+ #[allow(deprecated_in_future)]
fn from_non_witness_data_size() {
let non_witness_data_size = 1;
let got = Weight::from_non_witness_data_size(non_witness_data_size);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.