2023-04-30 23:19:35 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
2023-02-06 20:01:48 +00:00
|
|
|
//! Implements `FeeRate` and assoctiated features.
|
|
|
|
|
|
|
|
use core::fmt;
|
2023-02-21 23:01:26 +00:00
|
|
|
use core::ops::{Div, Mul};
|
2023-02-06 06:15:47 +00:00
|
|
|
|
2023-02-21 23:01:26 +00:00
|
|
|
use super::Weight;
|
2023-02-06 06:15:47 +00:00
|
|
|
use crate::prelude::*;
|
2023-02-06 20:01:48 +00:00
|
|
|
use crate::Amount;
|
|
|
|
|
|
|
|
/// 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.
|
2023-05-02 22:16:20 +00:00
|
|
|
pub const MIN: FeeRate = FeeRate::ZERO;
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Maximum possible value.
|
2023-05-02 22:16:20 +00:00
|
|
|
pub const MAX: FeeRate = FeeRate(u64::MAX);
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// 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.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub const fn from_sat_per_kwu(sat_kwu: u64) -> Self { FeeRate(sat_kwu) }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// 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.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { FeeRate(sat_vb * (1000 / 4)) }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Returns raw fee rate.
|
|
|
|
///
|
|
|
|
/// Can be used instead of `into()` to avoid inference issues.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub const fn to_sat_per_kwu(self) -> u64 { self.0 }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Converts to sat/vB rounding down.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub const fn to_sat_per_vb_floor(self) -> u64 { self.0 / (1000 / 4) }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Converts to sat/vB rounding up.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub const fn to_sat_per_vb_ceil(self) -> u64 { (self.0 + (1000 / 4 - 1)) / (1000 / 4) }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Checked multiplication.
|
|
|
|
///
|
|
|
|
/// Computes `self * rhs` returning `None` if overflow occurred.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub fn checked_mul(self, rhs: u64) -> Option<Self> { self.0.checked_mul(rhs).map(Self) }
|
2023-02-06 20:01:48 +00:00
|
|
|
|
|
|
|
/// Checked division.
|
|
|
|
///
|
|
|
|
/// Computes `self / rhs` returning `None` if `rhs == 0`.
|
2023-02-21 23:01:26 +00:00
|
|
|
pub fn checked_div(self, rhs: u64) -> Option<Self> { self.0.checked_div(rhs).map(Self) }
|
2023-05-18 15:46:26 +00:00
|
|
|
|
|
|
|
/// Checked weight multiplication.
|
|
|
|
///
|
2023-08-01 22:20:51 +00:00
|
|
|
/// Computes `self * rhs` where rhs is of type Weight. `None` is returned if an overflow
|
|
|
|
/// occurred.
|
2023-05-18 15:46:26 +00:00
|
|
|
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
|
|
|
|
self.0.checked_mul(rhs.to_wu()).map(Amount::from_sat)
|
|
|
|
}
|
2023-07-19 23:33:24 +00:00
|
|
|
|
|
|
|
/// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None`
|
|
|
|
/// if overflow occurred.
|
|
|
|
///
|
|
|
|
/// This is equivalent to `Self::checked_mul_by_weight()`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2023-08-18 01:17:39 +00:00
|
|
|
/// # use bitcoin::{absolute, transaction, FeeRate, Transaction};
|
2023-07-19 23:33:24 +00:00
|
|
|
/// # // Dummy transaction.
|
2023-08-18 01:17:39 +00:00
|
|
|
/// # let tx = Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], output: vec![] };
|
2023-07-19 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/vbyte is valid");
|
|
|
|
/// let fee = rate.fee_wu(tx.weight());
|
|
|
|
/// ```
|
|
|
|
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }
|
|
|
|
|
|
|
|
/// Calculates fee by multiplying this fee rate by weight, in virtual bytes, returning `None`
|
|
|
|
/// if overflow occurred.
|
|
|
|
///
|
|
|
|
/// This is equivalent to converting `vb` to `weight` using `Weight::from_vb` and then calling
|
|
|
|
/// `Self::fee_wu(weight)`.
|
|
|
|
pub fn fee_vb(self, vb: u64) -> Option<Amount> {
|
|
|
|
Weight::from_vb(vb).and_then(|w| self.fee_wu(w))
|
|
|
|
}
|
2023-02-06 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Alternative will display the unit.
|
|
|
|
impl fmt::Display for FeeRate {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
if f.alternate() {
|
2023-09-11 23:30:49 +00:00
|
|
|
write!(f, "{}.00 sat/vbyte", self.to_sat_per_vb_ceil())
|
2023-02-06 20:01:48 +00:00
|
|
|
} else {
|
|
|
|
fmt::Display::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FeeRate> for u64 {
|
2023-02-21 23:01:26 +00:00
|
|
|
fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() }
|
2023-02-06 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
2023-02-21 23:01:26 +00:00
|
|
|
fn mul(self, rhs: Weight) -> Self::Output { rhs * self }
|
2023-02-06 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Div<Weight> for Amount {
|
|
|
|
type Output = FeeRate;
|
|
|
|
|
2023-02-21 23:01:26 +00:00
|
|
|
fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) }
|
2023-02-06 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 06:15:47 +00:00
|
|
|
crate::parse::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu);
|
2023-03-04 09:42:15 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::u64;
|
|
|
|
|
2023-02-21 23:01:26 +00:00
|
|
|
use super::*;
|
|
|
|
|
2023-03-04 09:42:15 +00:00
|
|
|
#[test]
|
|
|
|
fn fee_rate_const_test() {
|
|
|
|
assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());
|
2023-05-02 22:16:20 +00:00
|
|
|
assert_eq!(u64::MIN, FeeRate::MIN.to_sat_per_kwu());
|
|
|
|
assert_eq!(u64::MAX, FeeRate::MAX.to_sat_per_kwu());
|
2023-03-04 09:42:15 +00:00
|
|
|
assert_eq!(250, FeeRate::BROADCAST_MIN.to_sat_per_kwu());
|
|
|
|
assert_eq!(750, FeeRate::DUST.to_sat_per_kwu());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fee_rate_from_sat_per_vb_test() {
|
|
|
|
let fee_rate = FeeRate::from_sat_per_vb(10).expect("expected feerate in sat/kwu");
|
|
|
|
assert_eq!(FeeRate(2500), fee_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fee_rate_from_sat_per_vb_overflow_test() {
|
|
|
|
let fee_rate = FeeRate::from_sat_per_vb(u64::MAX);
|
|
|
|
assert!(fee_rate.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_sat_per_vb_unchecked_test() {
|
|
|
|
let fee_rate = FeeRate::from_sat_per_vb_unchecked(10);
|
|
|
|
assert_eq!(FeeRate(2500), fee_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2023-02-21 23:01:26 +00:00
|
|
|
fn from_sat_per_vb_unchecked_panic_test() { FeeRate::from_sat_per_vb_unchecked(u64::MAX); }
|
2023-03-04 09:42:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_feerate_test() {
|
|
|
|
let fee_rate = FeeRate(333);
|
|
|
|
assert_eq!(333, fee_rate.to_sat_per_kwu());
|
|
|
|
assert_eq!(1, fee_rate.to_sat_per_vb_floor());
|
|
|
|
assert_eq!(2, fee_rate.to_sat_per_vb_ceil());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn checked_mul_test() {
|
|
|
|
let fee_rate = FeeRate(10).checked_mul(10).expect("expected feerate in sat/kwu");
|
|
|
|
assert_eq!(FeeRate(100), fee_rate);
|
|
|
|
|
|
|
|
let fee_rate = FeeRate(10).checked_mul(u64::MAX);
|
|
|
|
assert!(fee_rate.is_none());
|
|
|
|
}
|
|
|
|
|
2023-05-18 15:46:26 +00:00
|
|
|
#[test]
|
|
|
|
fn checked_weight_mul_test() {
|
|
|
|
let weight = Weight::from_wu(10);
|
|
|
|
let fee: Amount = FeeRate(10).checked_mul_by_weight(weight).expect("expected Amount");
|
|
|
|
assert_eq!(Amount::from_sat(100), fee);
|
|
|
|
|
|
|
|
let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX);
|
|
|
|
assert!(fee.is_none());
|
|
|
|
}
|
|
|
|
|
2023-03-04 09:42:15 +00:00
|
|
|
#[test]
|
|
|
|
fn checked_div_test() {
|
|
|
|
let fee_rate = FeeRate(10).checked_div(10).expect("expected feerate in sat/kwu");
|
|
|
|
assert_eq!(FeeRate(1), fee_rate);
|
|
|
|
|
|
|
|
let fee_rate = FeeRate(10).checked_div(0);
|
|
|
|
assert!(fee_rate.is_none());
|
|
|
|
}
|
2023-07-19 23:33:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fee_convenience_functions_agree() {
|
|
|
|
use crate::blockdata::transaction::Transaction;
|
|
|
|
use crate::consensus::Decodable;
|
|
|
|
use crate::internal_macros::hex;
|
|
|
|
|
|
|
|
const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
|
|
|
|
|
|
|
|
let raw_tx = hex!(SOME_TX);
|
|
|
|
let tx: Transaction = Decodable::consensus_decode(&mut raw_tx.as_slice()).unwrap();
|
|
|
|
|
|
|
|
let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/byte is valid");
|
|
|
|
|
|
|
|
assert_eq!(rate.fee_vb(tx.vsize() as u64), rate.fee_wu(tx.weight()));
|
|
|
|
}
|
2023-03-04 09:42:15 +00:00
|
|
|
}
|