Skip to content

Commit 52e5a9a

Browse files
committed
Add timeout for fee rate cache updates
.. even though we don't expect this to block, we're better safe than sorry and start to introduce timeouts for any calls we make to remote servers.
1 parent 87c939b commit 52e5a9a

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ enum NodeError {
157157
"ChannelConfigUpdateFailed",
158158
"PersistenceFailed",
159159
"FeerateEstimationUpdateFailed",
160+
"FeerateEstimationUpdateTimeout",
160161
"WalletOperationFailed",
161162
"WalletOperationTimeout",
162163
"OnchainTxSigningFailed",

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub(crate) const BDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 90;
5252
// The timeout after which we abort a wallet syncing operation.
5353
pub(crate) const LDK_WALLET_SYNC_TIMEOUT_SECS: u64 = 30;
5454

55+
// The timeout after which we abort a fee rate cache update operation.
56+
pub(crate) const FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS: u64 = 5;
57+
5558
// The length in bytes of our wallets' keys seed.
5659
pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
5760

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum Error {
3333
PersistenceFailed,
3434
/// A fee rate estimation update failed.
3535
FeerateEstimationUpdateFailed,
36+
/// A fee rate estimation update timed out.
37+
FeerateEstimationUpdateTimeout,
3638
/// A wallet operation failed.
3739
WalletOperationFailed,
3840
/// A wallet operation timed out.
@@ -113,6 +115,9 @@ impl fmt::Display for Error {
113115
Self::FeerateEstimationUpdateFailed => {
114116
write!(f, "Failed to update fee rate estimates.")
115117
},
118+
Self::FeerateEstimationUpdateTimeout => {
119+
write!(f, "Updating fee rate estimates timed out.")
120+
},
116121
Self::WalletOperationFailed => write!(f, "Failed to conduct wallet operation."),
117122
Self::WalletOperationTimeout => write!(f, "A wallet operation timed out."),
118123
Self::OnchainTxSigningFailed => write!(f, "Failed to sign given transaction."),

src/fee_estimator.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::config::FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS;
12
use crate::logger::{log_error, log_trace, Logger};
23
use crate::{Config, Error};
34

@@ -14,6 +15,7 @@ use bitcoin::Network;
1415
use std::collections::HashMap;
1516
use std::ops::Deref;
1617
use std::sync::{Arc, RwLock};
18+
use std::time::Duration;
1719

1820
pub(crate) struct OnchainFeeEstimator<L: Deref>
1921
where
@@ -55,7 +57,21 @@ where
5557
ConfirmationTarget::OutputSpendingFee => 12,
5658
};
5759

58-
let estimates = self.esplora_client.get_fee_estimates().await.map_err(|e| {
60+
let estimates = tokio::time::timeout(
61+
Duration::from_secs(FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS),
62+
self.esplora_client.get_fee_estimates(),
63+
)
64+
.await
65+
.map_err(|e| {
66+
log_error!(
67+
self.logger,
68+
"Updating fee rate estimates for {:?} timed out: {}",
69+
target,
70+
e
71+
);
72+
Error::FeerateEstimationUpdateTimeout
73+
})?
74+
.map_err(|e| {
5975
log_error!(
6076
self.logger,
6177
"Failed to retrieve fee rate estimates for {:?}: {}",

0 commit comments

Comments
 (0)