Skip to content

Commit

Permalink
f Account for ConfirmationTarget::MempoolMinimum
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 21, 2023
1 parent 3910de8 commit 83cd7fb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/bitcoind_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::convert::{BlockchainInfo, FeeResponse, FundedTx, NewAddress, RawTx, SignedTx};
use crate::convert::{BlockchainInfo, FeeResponse, FundedTx, NewAddress, RawTx, SignedTx, MempoolMinFeeResponse};
use crate::disk::FilesystemLogger;
use base64;
use bitcoin::blockdata::transaction::Transaction;
Expand Down Expand Up @@ -32,6 +32,7 @@ pub struct BitcoindClient {

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum Target {
MempoolMinimum,
Background,
Normal,
HighPriority,
Expand Down Expand Up @@ -75,6 +76,7 @@ impl BitcoindClient {
"Failed to make initial call to bitcoind - please check your RPC user/password and access settings")
})?;
let mut fees: HashMap<Target, AtomicU32> = 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));
Expand Down Expand Up @@ -102,6 +104,19 @@ impl BitcoindClient {
) {
handle.spawn(async move {
loop {
let mempoolmin_estimate = {
let resp = rpc_client
.call_method::<MempoolMinFeeResponse>(
"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");
Expand Down Expand Up @@ -151,6 +166,9 @@ impl BitcoindClient {
}
};

fees.get(&Target::MempoolMinimum)
.unwrap()
.store(mempoolmin_estimate, Ordering::Release);
fees.get(&Target::Background)
.unwrap()
.store(background_estimate, Ordering::Release);
Expand Down Expand Up @@ -238,6 +256,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)
}
Expand Down
27 changes: 26 additions & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl TryInto<NewAddress> for JsonResponse {
Ok(NewAddress(self.0.as_str().unwrap().to_string()))
}
}

pub struct FeeResponse {
pub feerate_sat_per_kw: Option<u32>,
pub errored: bool,
Expand All @@ -74,6 +73,32 @@ impl TryInto<FeeResponse> for JsonResponse {
}
}

#[derive(Debug)]
pub struct MempoolMinFeeResponse {
pub feerate_sat_per_kw: Option<u32>,
pub errored: bool,
}

impl TryInto<MempoolMinFeeResponse> for JsonResponse {
type Error = std::io::Error;
fn try_into(self) -> std::io::Result<MempoolMinFeeResponse> {
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,
Expand Down

0 comments on commit 83cd7fb

Please sign in to comment.