forked from rust-bitcoin/rust-bitcoin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use of general-purpose integers is often error-prone and annoying. We're working towards improving it by introducing newtypes. This adds newtypes for weight and fee rate to make fee computation easier and more readable. Note however that this dosn't change the type for individual parts of the transaction since computing the total weight is not as simple as summing them up and we want to avoid such confusion. Part of rust-bitcoin#630
- Loading branch information
Showing
7 changed files
with
363 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
//! Implements `FeeRate` and assoctiated features. | ||
|
||
use core::fmt; | ||
use core::ops::{Mul, Div}; | ||
use crate::Amount; | ||
use super::Weight; | ||
|
||
/// Represents fee rate. | ||
/// | ||
/// This is an integer newtype representing fee rate in `sat/kwu`. It provides protection against mixing | ||
/// up the types as well as basic formatting features. | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] | ||
#[cfg_attr(feature = "serde", serde(transparent))] | ||
pub struct FeeRate(u64); | ||
|
||
impl FeeRate { | ||
/// 0 sat/kwu. | ||
/// | ||
/// Equivalent to [`MIN`](Self::MIN), may better express intent in some contexts. | ||
pub const ZERO: FeeRate = FeeRate(0); | ||
|
||
/// Minimum possible value (0 sat/kwu). | ||
/// | ||
/// Equivalent to [`ZERO`](Self::ZERO), may better express intent in some contexts. | ||
pub const MIN: FeeRate = FeeRate(u64::min_value()); | ||
|
||
/// Maximum possible value. | ||
pub const MAX: FeeRate = FeeRate(u64::max_value()); | ||
|
||
/// Minimum fee rate required to broadcast a transaction. | ||
/// | ||
/// The value matches the default Bitcoin Core policy at the time of library release. | ||
pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_unchecked(1); | ||
|
||
/// Fee rate used to compute dust amount. | ||
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_unchecked(3); | ||
|
||
/// Constructs `FeeRate` from satoshis per 1000 weight units. | ||
pub const fn from_sat_per_kwu(sat_kwu: u64) -> Self { | ||
FeeRate(sat_kwu) | ||
} | ||
|
||
/// Constructs `FeeRate` from satoshis per virtual bytes. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns `None` on arithmetic overflow. | ||
pub fn from_sat_per_vb(sat_vb: u64) -> Option<Self> { | ||
// 1 vb == 4 wu | ||
// 1 sat/vb == 1/4 sat/wu | ||
// sat_vb sat/vb * 1000 / 4 == sat/kwu | ||
Some(FeeRate(sat_vb.checked_mul(1000 / 4)?)) | ||
} | ||
|
||
/// Constructs `FeeRate` from satoshis per virtual bytes without overflow check. | ||
pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { | ||
FeeRate(sat_vb * (1000 / 4)) | ||
} | ||
|
||
/// Returns raw fee rate. | ||
/// | ||
/// Can be used instead of `into()` to avoid inference issues. | ||
pub const fn to_sat_per_kwu(self) -> u64 { | ||
self.0 | ||
} | ||
|
||
/// Converts to sat/vB rounding down. | ||
pub const fn to_sat_per_vb_floor(self) -> u64 { | ||
self.0 / (1000 / 4) | ||
} | ||
|
||
/// Converts to sat/vB rounding up. | ||
pub const fn to_sat_per_vb_ceil(self) -> u64 { | ||
(self.0 + (1000 / 4 - 1)) / (1000 / 4) | ||
} | ||
|
||
/// Checked multiplication. | ||
/// | ||
/// Computes `self * rhs` returning `None` if overflow occurred. | ||
pub fn checked_mul(self, rhs: u64) -> Option<Self> { | ||
self.0.checked_mul(rhs).map(Self) | ||
} | ||
|
||
/// Checked division. | ||
/// | ||
/// Computes `self / rhs` returning `None` if `rhs == 0`. | ||
pub fn checked_div(self, rhs: u64) -> Option<Self> { | ||
self.0.checked_div(rhs).map(Self) | ||
} | ||
} | ||
|
||
/// Alternative will display the unit. | ||
impl fmt::Display for FeeRate { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
if f.alternate() { | ||
write!(f, "{} sat/kwu", self.0) | ||
} else { | ||
fmt::Display::fmt(&self.0, f) | ||
} | ||
} | ||
} | ||
|
||
impl From<FeeRate> for u64 { | ||
fn from(value: FeeRate) -> Self { | ||
value.to_sat_per_kwu() | ||
} | ||
} | ||
|
||
/// Computes ceiling so that fee computation is conservative. | ||
impl Mul<FeeRate> for Weight { | ||
type Output = Amount; | ||
|
||
fn mul(self, rhs: FeeRate) -> Self::Output { | ||
Amount::from_sat((rhs.to_sat_per_kwu() * self.to_wu() + 999) / 1000) | ||
} | ||
} | ||
|
||
impl Mul<Weight> for FeeRate { | ||
type Output = Amount; | ||
|
||
fn mul(self, rhs: Weight) -> Self::Output { | ||
rhs * self | ||
} | ||
} | ||
|
||
impl Div<Weight> for Amount { | ||
type Output = FeeRate; | ||
|
||
fn div(self, rhs: Weight) -> Self::Output { | ||
FeeRate(self.to_sat() * 1000 / rhs.to_wu()) | ||
} | ||
} | ||
|
||
crate::parse::impl_parse_str_through_int!(FeeRate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.