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