Skip to content

Commit 7a6a1f1

Browse files
committed
Update sync status details
1 parent 3a55106 commit 7a6a1f1

File tree

3 files changed

+144
-83
lines changed

3 files changed

+144
-83
lines changed

client/src/cbf.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use spaces_wallet::bdk_wallet::{KeychainKind, Update};
1111
use spaces_wallet::bitcoin::bip158::BlockFilter;
1212
use spaces_wallet::bitcoin::ScriptBuf;
1313
use spaces_wallet::SpacesWallet;
14+
use crate::calc_progress;
1415
use crate::client::{BlockSource, BlockchainInfo};
1516
use crate::source::BitcoinBlockSource;
16-
use crate::wallets::WalletProgressUpdate;
17+
use crate::wallets::{WalletStatus, WalletProgressUpdate};
1718

1819
pub struct CompactFilterSync {
1920
graph: IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<KeychainKind>>,
@@ -29,7 +30,7 @@ pub struct CompactFilterSync {
2930
total_filters: u32,
3031
wait: Option<Instant>,
3132
state: SyncState,
32-
filters_queued: bool
33+
filters_queued: bool,
3334
}
3435

3536
enum SyncState {
@@ -136,7 +137,7 @@ impl CompactFilterSync {
136137
}
137138
if info.headers != info.blocks {
138139
info!("Source still syncing, retrying...");
139-
*progress = WalletProgressUpdate::Syncing;
140+
*progress = WalletProgressUpdate::new(WalletStatus::Syncing, None);
140141
self.wait = Some(Instant::now());
141142
return Ok(());
142143
}
@@ -147,10 +148,16 @@ impl CompactFilterSync {
147148
}
148149

149150
info!("Filters syncing, retrying...");
150-
*progress = WalletProgressUpdate::CbfFilterSync {
151-
total: info.filter_headers.unwrap_or(0),
152-
completed: info.filters.unwrap_or(0),
153-
};
151+
*progress = WalletProgressUpdate::new(WalletStatus::CbfFilterSync, Some(
152+
calc_progress(
153+
info.checkpoint.map(|c| c.height).unwrap_or(0),
154+
info.filters.unwrap_or(0),
155+
std::cmp::max(
156+
info.prune_height.unwrap_or(0),
157+
info.filter_headers.unwrap_or(0)
158+
),
159+
)
160+
));
154161
self.wait = Some(Instant::now());
155162
return Ok(());
156163
}
@@ -205,10 +212,12 @@ impl CompactFilterSync {
205212
} else {
206213
info!("wallet({}) processed block filter {} - no match", wallet.name(), height);
207214
}
208-
*progress = WalletProgressUpdate::CbfProcessFilters {
209-
total: self.total_filters,
210-
completed: self.total_filters - self.queued_filters.len() as u32,
211-
};
215+
216+
let completed = self.total_filters as f32 - self.queued_filters.len() as f32;
217+
*progress = WalletProgressUpdate::new(
218+
WalletStatus::CbfProcessFilters,
219+
Some(completed / self.total_filters as f32)
220+
);
212221
}
213222
SyncState::QueueBlocks => {
214223
if !self.queued_blocks.is_empty() {
@@ -231,12 +240,12 @@ impl CompactFilterSync {
231240
// The client has a global state for pending blocks in the queue
232241
// so we cap it just in case other things are queuing blocks
233242
// at the same time
234-
let pending = std::cmp::min(status.pending, self.block_matches);
235-
*progress = WalletProgressUpdate::CbfDownloadMatchingBlocks {
236-
total: self.block_matches,
237-
completed: self.block_matches - pending,
238-
};
239-
243+
let pending = std::cmp::min(status.pending, self.block_matches) as f32;
244+
let completed = self.block_matches as f32 - pending;
245+
*progress = WalletProgressUpdate::new(
246+
WalletStatus::CbfDownloadMatchingBlocks,
247+
Some(completed / self.block_matches as f32)
248+
);
240249
self.wait = Some(Instant::now());
241250
return Ok(());
242251
}
@@ -251,7 +260,7 @@ impl CompactFilterSync {
251260
SyncState::ProcessBlocks => {
252261
let (height, hash) = match self.queued_blocks.pop_first() {
253262
None => {
254-
*progress = WalletProgressUpdate::CbfApplyUpdate;
263+
*progress = WalletProgressUpdate::new(WalletStatus::CbfApplyUpdate, None);
255264
self.state = SyncState::ApplyUpdate;
256265
return Ok(());
257266
}
@@ -262,10 +271,11 @@ impl CompactFilterSync {
262271
.ok_or(anyhow!("block {} {} not found", height, hash))?;
263272
self.chain_changeset.insert(height, Some(hash));
264273
let _ = self.graph.apply_block_relevant(&block, height);
265-
*progress = WalletProgressUpdate::CbfProcessMatchingBlocks {
266-
total: self.block_matches,
267-
completed: self.block_matches - self.queued_blocks.len() as u32 ,
268-
};
274+
let completed = self.block_matches - self.queued_blocks.len() as u32;
275+
*progress = WalletProgressUpdate::new(
276+
WalletStatus::CbfProcessMatchingBlocks,
277+
Some(completed as f32 / self.block_matches as f32)
278+
);
269279
}
270280
SyncState::ApplyUpdate => {
271281
info!("wallet({}): updating wallet tip to {}", wallet.name(), self.filters_tip);
@@ -280,9 +290,9 @@ impl CompactFilterSync {
280290
info!("wallet({}): compact filter sync portion complete at {}", wallet.name(), self.filters_tip);
281291
self.state = SyncState::Synced;
282292
// Only CBF portion is done
283-
*progress = WalletProgressUpdate::Syncing
293+
*progress = WalletProgressUpdate::new(WalletStatus::Syncing, None);
284294
}
285-
SyncState::Synced => {},
295+
SyncState::Synced => {}
286296
}
287297
Ok(())
288298
}

client/src/format.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
rpc::ServerInfo,
2323
wallets::{ListSpacesResponse, TxInfo, TxResponse, WalletResponse},
2424
};
25-
use crate::wallets::{WalletInfoWithProgress, WalletProgressUpdate};
25+
use crate::wallets::{WalletInfoWithProgress, WalletStatus};
2626

2727
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum, Serialize, Deserialize)]
2828
#[serde(rename_all = "lowercase")]
@@ -188,36 +188,39 @@ pub fn print_wallet_info(prog: WalletInfoWithProgress, format: Format) {
188188

189189
// Print sync status
190190
println!(" Sync Status:");
191-
match prog.status {
192-
WalletProgressUpdate::SourceSync { total, completed } => {
193-
println!(" Source Syncing: {}/{} ({:.1}%)", completed, total,
194-
(completed as f64 / total as f64) * 100.0);
191+
let p = prog.sync.progress.unwrap_or(0.0);
192+
match prog.sync.status {
193+
WalletStatus::HeadersSync => {
194+
println!(" Syncing block headers");
195195
}
196-
WalletProgressUpdate::CbfFilterSync { total, completed } => {
197-
println!(" Filters Syncing: {}/{} ({:.1}%)", completed, total,
198-
(completed as f64 / total as f64) * 100.0);
196+
WalletStatus::ChainSync => {
197+
println!(" Chain Syncing: {:.1}%", p * 100.0);
199198
}
200-
WalletProgressUpdate::CbfProcessFilters { total, completed } => {
201-
println!(" Processing Filters: {}/{} ({:.1}%)", completed, total,
202-
(completed as f64 / total as f64) * 100.0);
199+
WalletStatus::SpacesSync => {
200+
println!(" Spaces Syncing: {:.1}%", p * 100.0);
203201
}
204-
WalletProgressUpdate::CbfDownloadMatchingBlocks { total, completed } => {
205-
println!(" Downloading Matching Blocks: {}/{} ({:.1}%)", completed, total,
206-
(completed as f64 / total as f64) * 100.0);
202+
WalletStatus::CbfFilterSync => {
203+
println!(" Filters Syncing: {:.1}%", p * 100.0);
207204
}
208-
WalletProgressUpdate::CbfProcessMatchingBlocks { total, completed } => {
209-
println!(" Processing Matching Blocks: {}/{} ({:.1}%)", completed, total,
210-
(completed as f64 / total as f64) * 100.0);
205+
WalletStatus::CbfProcessFilters => {
206+
println!(" Processing Filters: {:.1}%", p* 100.0);
211207
}
212-
WalletProgressUpdate::Syncing => {
208+
WalletStatus::CbfDownloadMatchingBlocks => {
209+
println!(" Downloading Matching Blocks: {:.1}%", p * 100.0);
210+
}
211+
WalletStatus::CbfProcessMatchingBlocks => {
212+
println!(" Processing Matching Blocks: {:.1}%",p * 100.0);
213+
}
214+
WalletStatus::Syncing => {
213215
println!(" Syncing: In progress ({:.1}%):", prog.info.progress * 100.0);
214216
}
215-
WalletProgressUpdate::CbfApplyUpdate => {
217+
WalletStatus::CbfApplyUpdate => {
216218
println!(" Applying compact filters update");
217219
}
218-
WalletProgressUpdate::Complete => {
220+
WalletStatus::Complete => {
219221
println!(" Complete");
220222
}
223+
221224
}
222225

223226
println!();

0 commit comments

Comments
 (0)