8000 Add kilo weight unit conversion by yancyribbens · Pull Request #1735 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add kilo weight unit conversion #1735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bitcoin/src/blockdata/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ impl Weight {
/// Directly constructs `Weight` from weight units.
pub const fn from_wu(wu: u64) -> Self { Weight(wu) }

/// Constructs `Weight` from kilo weight units returning `None` if overflow occurred.
pub fn from_kwu(wu: u64) -> Option<Self> { wu.checked_mul(1000).map(Weight) }

/// Constructs `Weight` from virtual bytes.
8000 ///
/// # Errors
Expand All @@ -55,6 +58,9 @@ impl Weight {
/// Can be used instead of `into()` to avoid inference issues.
pub const fn to_wu(self) -> u64 { self.0 }

/// Converts to kilo weight units rounding down.
pub const fn to_kwu_floor(self) -> u64 { self.0 / 1000 }

/// Converts to vB rounding down.
pub const fn to_vbytes_floor(self) -> u64 { self.0 / 4 }

Expand Down Expand Up @@ -102,6 +108,17 @@ mod tests {
assert_eq!(Weight::ZERO, Weight::from_wu(0));
}

#[test]
fn kilo_weight_constructor_test() {
assert_eq!(Weight(1_000), Weight::from_kwu(1).expect("expected weight unit"));
}

#[test]
#[should_panic]
fn kilo_weight_constructor_panic_test() {
Weight::from_kwu(u64::max_value()).expect("expected weight unit");
}

#[test]
fn from_vb_test() {
let vb = Weight::from_vb(1).expect("expected weight unit");
Expand Down Expand Up @@ -132,6 +149,11 @@ mod tests {
assert_eq!(Weight(4), Weight::from_non_witness_data_size(1));
}

#[test]
fn to_kwu_floor_test() {
assert_eq!(1, Weight(1_000).to_kwu_floor());
}

#[test]
fn to_vb_floor_test() {
assert_eq!(1, Weight(4).to_vbytes_floor());
Expand Down
0