From b07b101f13d0836db12aa59dd0f914275f9edeb4 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Fri, 21 Jul 2023 09:47:37 +0200 Subject: [PATCH] f Account for `ConfirmationTarget::MempoolMinimum` This is part of the upgrade to LDK 0.0.116. --- src/bitcoind_client.rs | 22 +++++++++++++++++++++- src/convert.rs | 25 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/bitcoind_client.rs b/src/bitcoind_client.rs index e07d6200..228f27a2 100644 --- a/src/bitcoind_client.rs +++ b/src/bitcoind_client.rs @@ -1,4 +1,6 @@ -use crate::convert::{BlockchainInfo, FeeResponse, FundedTx, NewAddress, RawTx, SignedTx}; +use crate::convert::{ + BlockchainInfo, FeeResponse, FundedTx, MempoolMinFeeResponse, NewAddress, RawTx, SignedTx, +}; use crate::disk::FilesystemLogger; use base64; use bitcoin::blockdata::transaction::Transaction; @@ -32,6 +34,7 @@ pub struct BitcoindClient { #[derive(Clone, Eq, Hash, PartialEq)] pub enum Target { + MempoolMinimum, Background, Normal, HighPriority, @@ -75,6 +78,7 @@ impl BitcoindClient { "Failed to make initial call to bitcoind - please check your RPC user/password and access settings") })?; let mut fees: HashMap = HashMap::new(); + fees.insert(Target::MempoolMinimum, AtomicU32::new(MIN_FEERATE)); fees.insert(Target::Background, AtomicU32::new(MIN_FEERATE)); fees.insert(Target::Normal, AtomicU32::new(2000)); fees.insert(Target::HighPriority, AtomicU32::new(5000)); @@ -102,6 +106,16 @@ impl BitcoindClient { ) { handle.spawn(async move { loop { + let mempoolmin_estimate = { + let resp = rpc_client + .call_method::("getmempoolinfo", &vec![]) + .await + .unwrap(); + match resp.feerate_sat_per_kw { + Some(feerate) => std::cmp::max(feerate, MIN_FEERATE), + None => MIN_FEERATE, + } + }; let background_estimate = { let background_conf_target = serde_json::json!(144); let background_estimate_mode = serde_json::json!("ECONOMICAL"); @@ -151,6 +165,9 @@ impl BitcoindClient { } }; + fees.get(&Target::MempoolMinimum) + .unwrap() + .store(mempoolmin_estimate, Ordering::Release); fees.get(&Target::Background) .unwrap() .store(background_estimate, Ordering::Release); @@ -238,6 +255,9 @@ impl BitcoindClient { impl FeeEstimator for BitcoindClient { fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 { match confirmation_target { + ConfirmationTarget::MempoolMinimum => { + self.fees.get(&Target::MempoolMinimum).unwrap().load(Ordering::Acquire) + } ConfirmationTarget::Background => { self.fees.get(&Target::Background).unwrap().load(Ordering::Acquire) } diff --git a/src/convert.rs b/src/convert.rs index 64039233..9676a9fb 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -49,7 +49,6 @@ impl TryInto for JsonResponse { Ok(NewAddress(self.0.as_str().unwrap().to_string())) } } - pub struct FeeResponse { pub feerate_sat_per_kw: Option, pub errored: bool, @@ -74,6 +73,30 @@ impl TryInto for JsonResponse { } } +pub struct MempoolMinFeeResponse { + pub feerate_sat_per_kw: Option, + pub errored: bool, +} + +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + let errored = !self.0["errors"].is_null(); + Ok(MempoolMinFeeResponse { + errored, + feerate_sat_per_kw: match self.0["mempoolminfee"].as_f64() { + // Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to + // satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4 + // to convert virtual-bytes into weight units. + Some(feerate_btc_per_kvbyte) => { + Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32) + } + None => None, + }, + }) + } +} + pub struct BlockchainInfo { pub latest_height: usize, pub latest_blockhash: BlockHash,