|
1 | 1 | use crate::logger::{log_error, log_info, log_trace, Logger}; |
2 | 2 |
|
| 3 | +use crate::config::BDK_WALLET_SYNC_TIMEOUT_SECS; |
3 | 4 | use crate::Error; |
4 | 5 |
|
5 | 6 | use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; |
@@ -36,6 +37,7 @@ use bitcoin::{ScriptBuf, Transaction, TxOut, Txid}; |
36 | 37 | use std::mem; |
37 | 38 | use std::ops::{Deref, DerefMut}; |
38 | 39 | use std::sync::{Arc, Mutex, RwLock}; |
| 40 | +use std::time::Duration; |
39 | 41 |
|
40 | 42 | enum WalletSyncStatus { |
41 | 43 | Completed, |
@@ -100,34 +102,46 @@ where |
100 | 102 | let res = { |
101 | 103 | let sync_options = SyncOptions { progress: None }; |
102 | 104 | let wallet_lock = self.inner.lock().unwrap(); |
103 | | - match wallet_lock.sync(&self.blockchain, sync_options).await { |
104 | | - Ok(()) => { |
105 | | - // TODO: Drop this workaround after BDK 1.0 upgrade. |
106 | | - // Update balance cache after syncing. |
107 | | - if let Ok(balance) = wallet_lock.get_balance() { |
108 | | - *self.balance_cache.write().unwrap() = balance; |
109 | | - } |
110 | | - Ok(()) |
111 | | - }, |
112 | | - Err(e) => match e { |
113 | | - bdk::Error::Esplora(ref be) => match **be { |
114 | | - bdk::blockchain::esplora::EsploraError::Reqwest(_) => { |
115 | | - log_error!( |
116 | | - self.logger, |
117 | | - "Sync failed due to HTTP connection error: {}", |
118 | | - e |
119 | | - ); |
120 | | - Err(From::from(e)) |
| 105 | + |
| 106 | + let timeout_fut = tokio::time::timeout( |
| 107 | + Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS), |
| 108 | + wallet_lock.sync(&self.blockchain, sync_options), |
| 109 | + ); |
| 110 | + |
| 111 | + match timeout_fut.await { |
| 112 | + Ok(res) => match res { |
| 113 | + Ok(()) => { |
| 114 | + // TODO: Drop this workaround after BDK 1.0 upgrade. |
| 115 | + // Update balance cache after syncing. |
| 116 | + if let Ok(balance) = wallet_lock.get_balance() { |
| 117 | + *self.balance_cache.write().unwrap() = balance; |
| 118 | + } |
| 119 | + Ok(()) |
| 120 | + }, |
| 121 | + Err(e) => match e { |
| 122 | + bdk::Error::Esplora(ref be) => match **be { |
| 123 | + bdk::blockchain::esplora::EsploraError::Reqwest(_) => { |
| 124 | + log_error!( |
| 125 | + self.logger, |
| 126 | + "Sync failed due to HTTP connection error: {}", |
| 127 | + e |
| 128 | + ); |
| 129 | + Err(From::from(e)) |
| 130 | + }, |
| 131 | + _ => { |
| 132 | + log_error!(self.logger, "Sync failed due to Esplora error: {}", e); |
| 133 | + Err(From::from(e)) |
| 134 | + }, |
121 | 135 | }, |
122 | 136 | _ => { |
123 | | - log_error!(self.logger, "Sync failed due to Esplora error: {}", e); |
| 137 | + log_error!(self.logger, "Wallet sync error: {}", e); |
124 | 138 | Err(From::from(e)) |
125 | 139 | }, |
126 | 140 | }, |
127 | | - _ => { |
128 | | - log_error!(self.logger, "Wallet sync error: {}", e); |
129 | | - Err(From::from(e)) |
130 | | - }, |
| 141 | + }, |
| 142 | + Err(e) => { |
| 143 | + log_error!(self.logger, "On-chain wallet sync timed out: {}", e); |
| 144 | + Err(Error::WalletOperationTimeout) |
131 | 145 | }, |
132 | 146 | } |
133 | 147 | }; |
|
0 commit comments