Skip to content

Commit dfbe771

Browse files
committed
move, document and test the FeeRate struct
1 parent e4a5f71 commit dfbe771

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

integration_test/src/main.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use bitcoin::hashes::hex::{FromHex, ToHex};
2727
use bitcoin::hashes::Hash;
2828
use bitcoin::secp256k1;
2929
use bitcoin::{
30-
Address, Amount, Network, OutPoint, PrivateKey, Script, EcdsaSighashType, SignedAmount, Transaction,
31-
TxIn, TxOut, Txid, Witness,
30+
Address, Amount, EcdsaSighashType, Network, OutPoint, PrivateKey, Script, SignedAmount,
31+
Transaction, TxIn, TxOut, Txid, Witness,
3232
};
3333
use bitcoincore_rpc::bitcoincore_rpc_json::{
3434
GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest,
@@ -597,8 +597,9 @@ fn test_sign_raw_transaction_with_send_raw_transaction(cl: &Client) {
597597
}],
598598
};
599599

600-
let res =
601-
cl.sign_raw_transaction_with_key(&tx, &[sk], None, Some(EcdsaSighashType::All.into())).unwrap();
600+
let res = cl
601+
.sign_raw_transaction_with_key(&tx, &[sk], None, Some(EcdsaSighashType::All.into()))
602+
.unwrap();
602603
assert!(res.complete);
603604
let _ = cl.send_raw_transaction(&res.transaction().unwrap()).unwrap();
604605
}
@@ -687,7 +688,7 @@ fn test_bump_fee(cl: &Client) {
687688

688689
// bump with explicit fee rate
689690
let amount_per_vbyte = Amount::from_sat(500);
690-
let new_fee_rate = json::FeeRate::new(amount_per_vbyte);
691+
let new_fee_rate = json::FeeRate::per_vbyte(amount_per_vbyte);
691692
let options = json::BumpFeeOptions {
692693
fee_rate: Some(new_fee_rate),
693694
replaceable: Some(true),
@@ -1103,11 +1104,7 @@ fn test_add_ban(cl: &Client) {
11031104
let res = cl.list_banned().unwrap();
11041105
assert_eq!(res.len(), 0);
11051106

1106-
assert_error_message!(
1107-
cl.add_ban("INVALID_STRING", 0, false),
1108-
-30,
1109-
"Error: Invalid IP/Subnet"
1110-
);
1107+
assert_error_message!(cl.add_ban("INVALID_STRING", 0, false), -30, "Error: Invalid IP/Subnet");
11111108
}
11121109

11131110
fn test_set_network_active(cl: &Client) {

json/src/lib.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@ use std::fmt;
3535

3636
//TODO(stevenroose) consider using a Time type
3737

38+
/// A representation of a fee rate. Bitcoin Core uses different units in different
39+
/// versions. To avoid burdening the user with using the correct unit, this struct
40+
/// provides an umambiguous way to represent the fee rate, and the lib will perform
41+
/// the necessary conversions.
42+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
43+
pub struct FeeRate(Amount);
44+
45+
impl FeeRate {
46+
/// Construct FeeRate from the amount per vbyte
47+
pub fn per_vbyte(amount_per_vbyte: Amount) -> Self {
48+
// internal representation is amount per vbyte
49+
Self(amount_per_vbyte)
50+
}
51+
52+
/// Construct FeeRate from the amount per kilo-vbyte
53+
pub fn per_kvbyte(amount_per_kvbyte: Amount) -> Self {
54+
// internal representation is amount per vbyte, so divide by 1000
55+
Self::per_vbyte(amount_per_kvbyte / 1000)
56+
}
57+
58+
pub fn as_sat_per_vbyte(&self) -> f64 {
59+
// multiply by the number of decimals to get sat
60+
self.0.as_sat() as f64
61+
}
62+
63+
pub fn as_btc_per_kvbyte(&self) -> f64 {
64+
// divide by 10^8 to get btc/vbyte, then multiply by 10^3 to get btc/kbyte
65+
self.0.as_sat() as f64 / 100_000.0
66+
}
67+
}
68+
3869
/// A module used for serde serialization of bytes in hexadecimal format.
3970
///
4071
/// The module is compatible with the serde attribute.
@@ -1827,9 +1858,9 @@ impl BumpFeeOptions {
18271858
pub fn to_serializable(&self, version: usize) -> SerializableBumpFeeOptions {
18281859
let fee_rate = self.fee_rate.map(|x| {
18291860
if version < 210000 {
1830-
x.btc_per_kvbyte()
1861+
x.as_btc_per_kvbyte()
18311862
} else {
1832-
x.sat_per_vbyte()
1863+
x.as_sat_per_vbyte()
18331864
}
18341865
});
18351866

@@ -1842,23 +1873,6 @@ impl BumpFeeOptions {
18421873
}
18431874
}
18441875

1845-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
1846-
pub struct FeeRate(Amount);
1847-
1848-
impl FeeRate {
1849-
pub fn new(amount_per_vbyte: Amount) -> Self {
1850-
Self(amount_per_vbyte)
1851-
}
1852-
pub fn sat_per_vbyte(&self) -> f64 {
1853-
// multiply by the number of decimals to get sat
1854-
self.0.as_sat() as f64
1855-
}
1856-
pub fn btc_per_kvbyte(&self) -> f64 {
1857-
// divide by 10^8 to get btc/vbyte, then multiply by 10^3 to get btc/kbyte
1858-
self.0.as_sat() as f64 / 100_000.0
1859-
}
1860-
}
1861-
18621876
#[derive(Serialize, Clone, PartialEq, Debug, Default)]
18631877
#[serde(rename_all = "camelCase")]
18641878
pub struct SerializableBumpFeeOptions {
@@ -2072,3 +2086,18 @@ where
20722086
}
20732087
Ok(Some(res))
20742088
}
2089+
2090+
#[cfg(test)]
2091+
mod tests {
2092+
use super::*;
2093+
2094+
#[test]
2095+
fn test_fee_rate_conversion() {
2096+
let rate_1 = FeeRate::per_kvbyte(Amount::from_sat(10_000));
2097+
let rate_2 = FeeRate::per_vbyte(Amount::from_sat(10));
2098+
assert_eq!(rate_1, rate_2);
2099+
2100+
assert_eq!(rate_1.as_sat_per_vbyte(), 10.0);
2101+
assert_eq!(rate_1.as_btc_per_kvbyte(), 10.0 * 1e3 / 1e8);
2102+
}
2103+
}

0 commit comments

Comments
 (0)