Skip to content

Commit 4a861b1

Browse files
committed
f Update fees upon sync, rather than every request
1 parent 71ff2e5 commit 4a861b1

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/wallet.rs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ where
2626
// A BDK on-chain wallet.
2727
inner: Mutex<bdk::Wallet<D>>,
2828
// A cache storing the most recently retrieved fee rate estimations.
29-
fee_rate_cache: Mutex<HashMap<ConfirmationTarget, FeeRate>>,
29+
fee_rate_cache: RwLock<HashMap<ConfirmationTarget, FeeRate>>,
3030
tokio_runtime: RwLock<Option<Arc<tokio::runtime::Runtime>>>,
3131
logger: Arc<FilesystemLogger>,
3232
}
@@ -39,18 +39,26 @@ where
3939
blockchain: EsploraBlockchain, wallet: bdk::Wallet<D>, logger: Arc<FilesystemLogger>,
4040
) -> Self {
4141
let inner = Mutex::new(wallet);
42-
let fee_rate_cache = Mutex::new(HashMap::new());
42+
let fee_rate_cache = RwLock::new(HashMap::new());
4343
let tokio_runtime = RwLock::new(None);
4444
Self { blockchain, inner, fee_rate_cache, tokio_runtime, logger }
4545
}
4646

4747
pub(crate) async fn sync(&self) -> Result<(), Error> {
48+
match self.update_fee_estimates().await {
49+
Ok(()) => (),
50+
Err(e) => {
51+
log_error!(self.logger, "Fee estimation error: {}", e);
52+
return Err(e);
53+
}
54+
}
55+
4856
let sync_options = SyncOptions { progress: None };
4957
match self.inner.lock().unwrap().sync(&self.blockchain, sync_options).await {
5058
Ok(()) => Ok(()),
5159
Err(e) => {
5260
log_error!(self.logger, "Wallet sync error: {}", e);
53-
Err(e)?
61+
Err(From::from(e))
5462
}
5563
}
5664
}
@@ -63,6 +71,36 @@ where
6371
*self.tokio_runtime.write().unwrap() = None;
6472
}
6573

74+
pub(crate) async fn update_fee_estimates(&self) -> Result<(), Error> {
75+
let mut locked_fee_rate_cache = self.fee_rate_cache.write().unwrap();
76+
77+
let confirmation_targets = vec![
78+
ConfirmationTarget::Background,
79+
ConfirmationTarget::Normal,
80+
ConfirmationTarget::HighPriority,
81+
];
82+
for target in confirmation_targets {
83+
let num_blocks = num_blocks_from_conf_target(target);
84+
85+
let est_fee_rate = self.blockchain.estimate_fee(num_blocks).await;
86+
87+
match est_fee_rate {
88+
Ok(rate) => {
89+
locked_fee_rate_cache.insert(target, rate);
90+
log_trace!(
91+
self.logger,
92+
"Fee rate estimation updated: {} sats/kwu",
93+
rate.fee_wu(1000)
94+
);
95+
}
96+
Err(e) => {
97+
log_error!(self.logger, "Failed to update fee rate estimation: {}", e);
98+
}
99+
}
100+
}
101+
Ok(())
102+
}
103+
66104
pub(crate) fn create_funding_transaction(
67105
&self, output_script: &Script, value_sats: u64, confirmation_target: ConfirmationTarget,
68106
) -> Result<Transaction, Error> {
@@ -111,41 +149,12 @@ where
111149
}
112150

113151
fn estimate_fee_rate(&self, confirmation_target: ConfirmationTarget) -> FeeRate {
114-
let mut locked_fee_rate_cache = self.fee_rate_cache.lock().unwrap();
115-
let num_blocks = num_blocks_from_conf_target(confirmation_target);
152+
let locked_fee_rate_cache = self.fee_rate_cache.read().unwrap();
116153

117154
// We'll fall back on this, if we really don't have any other information.
118155
let fallback_rate = fallback_fee_from_conf_target(confirmation_target);
119156

120-
let locked_runtime = self.tokio_runtime.read().unwrap();
121-
if locked_runtime.as_ref().is_none() {
122-
log_error!(self.logger, "Failed to update fee rate estimation: No runtime.");
123-
unreachable!("Failed to broadcast transaction: No runtime.");
124-
}
125-
126-
let est_fee_rate = tokio::task::block_in_place(move || {
127-
locked_runtime
128-
.as_ref()
129-
.unwrap()
130-
.handle()
131-
.block_on(async move { self.blockchain.estimate_fee(num_blocks).await })
132-
});
133-
134-
match est_fee_rate {
135-
Ok(rate) => {
136-
locked_fee_rate_cache.insert(confirmation_target, rate);
137-
log_trace!(
138-
self.logger,
139-
"Fee rate estimation updated: {} sats/kwu",
140-
rate.fee_wu(1000)
141-
);
142-
rate
143-
}
144-
Err(e) => {
145-
log_error!(self.logger, "Failed to update fee rate estimation: {}", e);
146-
*locked_fee_rate_cache.get(&confirmation_target).unwrap_or(&fallback_rate)
147-
}
148-
}
157+
*locked_fee_rate_cache.get(&confirmation_target).unwrap_or(&fallback_rate)
149158
}
150159
}
151160

0 commit comments

Comments
 (0)