Merge rust-bitcoin/rust-bitcoin#6678: units: Add `Weight::to_vb_*` functions, deprecating `to_vbytes_*`
What changed, and why it matters
This is a routine API cleanup in a Rust Bitcoin library. It adds new method names (to_vb_floor, to_vb_ceil) for converting transaction 'weight' to 'virtual bytes' and marks the old names (to_vbytes_floor, to_vbytes_ceil) as deprecated. The actual math is unchanged, and existing callers are updated to use the new names. There is no security bug or fix here.
No security action needed. Treat as a normal API-consistency refactor. If maintaining downstream code, plan to migrate from to_vbytes_* to to_vb_* before the deprecated methods are removed in a future release.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces Weight::to_vb_floor and Weight::to_vb_ceil as aliases for the existing to_vbytes_floor/to_vbytes_ceil logic, deprecates the old names for consistency with FeeRate/from_vb_* naming, and updates internal callers (Transaction::vsize, fuzz target, tests) and API snapshot files. The implementation is identical: to_vb_floor divides weight units by WITNESS_SCALE_FACTOR; to_vb_ceil uses div_ceil. No behavioral or security-relevant change is present.
Changed components
bitcoin/src/blockdata/transaction.rsunits/src/weight.rsfuzz/fuzz_targets/units/arbitrary_weight.rsunits/api/*Inspect captured patch +30 / −8
### bitcoin/src/blockdata/transaction.rs
@@ -400,7 +400,7 @@ impl TransactionExt for Transaction {
#[inline]
fn vsize(&self) -> usize {
// No overflow because it's computed from data in memory
- self.weight().to_vbytes_ceil() as usize
+ self.weight().to_vb_ceil() as usize
}
fn is_explicitly_rbf(&self) -> bool { self.inputs.iter().any(|input| input.sequence.is_rbf()) }
### fuzz/fuzz_targets/units/arbitrary_weight.rs
@@ -16,8 +16,8 @@ fn do_test(data: &[u8]) {
weight.to_wu();
weight.to_kwu_ceil();
weight.to_kwu_floor();
- weight.to_vbytes_ceil();
- weight.to_vbytes_floor();
+ weight.to_vb_ceil();
+ weight.to_vb_floor();
// Operations that take u64 as the rhs
for operation in [Weight::checked_mul, Weight::checked_div] {
### units/api/all-features.txt
@@ -9328,6 +9328,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
@@ -11763,6 +11765,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
### units/api/alloc-only.txt
@@ -7573,6 +7573,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
@@ -9914,6 +9916,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
### units/api/no-features.txt
@@ -6729,6 +6729,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
@@ -8936,6 +8938,8 @@ pub const fn bitcoin_units::Weight::from_vb_unchecked(vb: u64) -> Self
pub const fn bitcoin_units::Weight::mul_by_fee_rate(self, fee_rate: bitcoin_units::FeeRate) -> bitcoin_units::result::NumOpResult<bitcoin_units::Amount>
pub const fn bitcoin_units::Weight::to_kwu_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_kwu_floor(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_ceil(self) -> u64
+pub const fn bitcoin_units::Weight::to_vb_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_ceil(self) -> u64
pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
impl bitcoin_units::Weight
### units/src/weight.rs
@@ -120,10 +120,20 @@ impl Weight {
/// Converts to vB rounding down.
#[inline]
+ pub const fn to_vb_floor(self) -> u64 { self.to_wu() / Self::WITNESS_SCALE_FACTOR }
+
+ /// Converts to vB rounding down.
+ #[inline]
+ #[deprecated(since = "TBD", note = "use `to_vb_floor()` instead")]
pub const fn to_vbytes_floor(self) -> u64 { self.to_wu() / Self::WITNESS_SCALE_FACTOR }
/// Converts to vB rounding up.
#[inline]
+ pub const fn to_vb_ceil(self) -> u64 { self.to_wu().div_ceil(Self::WITNESS_SCALE_FACTOR) }
+
+ /// Converts to vB rounding up.
+ #[inline]
+ #[deprecated(since = "TBD", note = "use `to_vbytes_ceil()` instead")]
pub const fn to_vbytes_ceil(self) -> u64 { self.to_wu().div_ceil(Self::WITNESS_SCALE_FACTOR) }
/// Checked addition.
@@ -418,15 +428,15 @@ mod tests {
#[test]
fn to_vb_floor() {
- assert_eq!(Weight::from_wu(8).to_vbytes_floor(), 2);
- assert_eq!(Weight::from_wu(9).to_vbytes_floor(), 2);
+ assert_eq!(Weight::from_wu(8).to_vb_floor(), 2);
+ assert_eq!(Weight::from_wu(9).to_vb_floor(), 2);
}
#[test]
fn to_vb_ceil() {
- assert_eq!(Weight::from_wu(4).to_vbytes_ceil(), 1);
- assert_eq!(Weight::from_wu(5).to_vbytes_ceil(), 2);
- assert_eq!(Weight::MAX.to_vbytes_ceil(), u64::MAX / Weight::WITNESS_SCALE_FACTOR + 1);
+ assert_eq!(Weight::from_wu(4).to_vb_ceil(), 1);
+ assert_eq!(Weight::from_wu(5).to_vb_ceil(), 2);
+ assert_eq!(Weight::MAX.to_vb_ceil(), u64::MAX / Weight::WITNESS_SCALE_FACTOR + 1);
}
#[test]Why this scored 20/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.