Skip to content

Commit c44c91b

Browse files
fix: sync progress (#150)
* fix: sync progress * more work * Update dash-spv-ffi/src/client.rs Co-authored-by: PastaPastaPasta <[email protected]> * Update dash-spv-ffi/src/client.rs Co-authored-by: PastaPastaPasta <[email protected]> * Update dash-spv-ffi/src/callbacks.rs Co-authored-by: PastaPastaPasta <[email protected]> * Update dash-spv-ffi/src/callbacks.rs Co-authored-by: PastaPastaPasta <[email protected]> * fixes * fmt * fixes * more fixes --------- Co-authored-by: PastaPastaPasta <[email protected]>
1 parent fdc4411 commit c44c91b

File tree

23 files changed

+351
-286
lines changed

23 files changed

+351
-286
lines changed

dash-spv-ffi/dash_spv_ffi.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ typedef enum FFISyncStage {
2828
Downloading = 2,
2929
Validating = 3,
3030
Storing = 4,
31-
Complete = 5,
32-
Failed = 6,
31+
DownloadingFilterHeaders = 5,
32+
DownloadingFilters = 6,
33+
DownloadingBlocks = 7,
34+
Complete = 8,
35+
Failed = 9,
3336
} FFISyncStage;
3437

3538
typedef enum DashSpvValidationMode {
@@ -70,32 +73,28 @@ typedef struct FFIString {
7073
uintptr_t length;
7174
} FFIString;
7275

76+
typedef struct FFISyncProgress {
77+
uint32_t header_height;
78+
uint32_t filter_header_height;
79+
uint32_t masternode_height;
80+
uint32_t peer_count;
81+
bool filter_sync_available;
82+
uint32_t filters_downloaded;
83+
uint32_t last_synced_filter_height;
84+
} FFISyncProgress;
85+
7386
typedef struct FFIDetailedSyncProgress {
74-
uint32_t current_height;
7587
uint32_t total_height;
7688
double percentage;
7789
double headers_per_second;
7890
int64_t estimated_seconds_remaining;
7991
enum FFISyncStage stage;
8092
struct FFIString stage_message;
81-
uint32_t connected_peers;
93+
struct FFISyncProgress overview;
8294
uint64_t total_headers;
8395
int64_t sync_start_timestamp;
8496
} FFIDetailedSyncProgress;
8597

86-
typedef struct FFISyncProgress {
87-
uint32_t header_height;
88-
uint32_t filter_header_height;
89-
uint32_t masternode_height;
90-
uint32_t peer_count;
91-
bool headers_synced;
92-
bool filter_headers_synced;
93-
bool masternodes_synced;
94-
bool filter_sync_available;
95-
uint32_t filters_downloaded;
96-
uint32_t last_synced_filter_height;
97-
} FFISyncProgress;
98-
9998
typedef struct FFISpvStats {
10099
uint32_t connected_peers;
101100
uint32_t total_peers;
@@ -150,11 +149,6 @@ typedef void (*WalletTransactionCallback)(const char *wallet_id,
150149
bool is_ours,
151150
void *user_data);
152151

153-
typedef void (*FilterHeadersProgressCallback)(uint32_t filter_height,
154-
uint32_t header_height,
155-
double percentage,
156-
void *user_data);
157-
158152
typedef struct FFIEventCallbacks {
159153
BlockCallback on_block;
160154
TransactionCallback on_transaction;
@@ -164,7 +158,6 @@ typedef struct FFIEventCallbacks {
164158
MempoolRemovedCallback on_mempool_transaction_removed;
165159
CompactFilterMatchedCallback on_compact_filter_matched;
166160
WalletTransactionCallback on_wallet_transaction;
167-
FilterHeadersProgressCallback on_filter_headers_progress;
168161
void *user_data;
169162
} FFIEventCallbacks;
170163

dash-spv-ffi/include/dash_spv_ffi.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ typedef enum FFISyncStage {
2828
Downloading = 2,
2929
Validating = 3,
3030
Storing = 4,
31-
Complete = 5,
32-
Failed = 6,
31+
DownloadingFilterHeaders = 5,
32+
DownloadingFilters = 6,
33+
DownloadingBlocks = 7,
34+
Complete = 8,
35+
Failed = 9,
3336
} FFISyncStage;
3437

3538
typedef enum DashSpvValidationMode {
@@ -70,32 +73,28 @@ typedef struct FFIString {
7073
uintptr_t length;
7174
} FFIString;
7275

76+
typedef struct FFISyncProgress {
77+
uint32_t header_height;
78+
uint32_t filter_header_height;
79+
uint32_t masternode_height;
80+
uint32_t peer_count;
81+
bool filter_sync_available;
82+
uint32_t filters_downloaded;
83+
uint32_t last_synced_filter_height;
84+
} FFISyncProgress;
85+
7386
typedef struct FFIDetailedSyncProgress {
74-
uint32_t current_height;
7587
uint32_t total_height;
7688
double percentage;
7789
double headers_per_second;
7890
int64_t estimated_seconds_remaining;
7991
enum FFISyncStage stage;
8092
struct FFIString stage_message;
81-
uint32_t connected_peers;
93+
struct FFISyncProgress overview;
8294
uint64_t total_headers;
8395
int64_t sync_start_timestamp;
8496
} FFIDetailedSyncProgress;
8597

86-
typedef struct FFISyncProgress {
87-
uint32_t header_height;
88-
uint32_t filter_header_height;
89-
uint32_t masternode_height;
90-
uint32_t peer_count;
91-
bool headers_synced;
92-
bool filter_headers_synced;
93-
bool masternodes_synced;
94-
bool filter_sync_available;
95-
uint32_t filters_downloaded;
96-
uint32_t last_synced_filter_height;
97-
} FFISyncProgress;
98-
9998
typedef struct FFISpvStats {
10099
uint32_t connected_peers;
101100
uint32_t total_peers;
@@ -150,11 +149,6 @@ typedef void (*WalletTransactionCallback)(const char *wallet_id,
150149
bool is_ours,
151150
void *user_data);
152151

153-
typedef void (*FilterHeadersProgressCallback)(uint32_t filter_height,
154-
uint32_t header_height,
155-
double percentage,
156-
void *user_data);
157-
158152
typedef struct FFIEventCallbacks {
159153
BlockCallback on_block;
160154
TransactionCallback on_transaction;
@@ -164,7 +158,6 @@ typedef struct FFIEventCallbacks {
164158
MempoolRemovedCallback on_mempool_transaction_removed;
165159
CompactFilterMatchedCallback on_compact_filter_matched;
166160
WalletTransactionCallback on_wallet_transaction;
167-
FilterHeadersProgressCallback on_filter_headers_progress;
168161
void *user_data;
169162
} FFIEventCallbacks;
170163

dash-spv-ffi/src/bin/ffi_cli.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ffi::{CStr, CString};
22
use std::os::raw::{c_char, c_void};
33
use std::ptr;
4+
use std::sync::atomic::{AtomicBool, Ordering};
45
use std::thread;
56
use std::time::Duration;
67

@@ -16,17 +17,15 @@ enum NetworkOpt {
1617
Regtest,
1718
}
1819

20+
static SYNC_COMPLETED: AtomicBool = AtomicBool::new(false);
21+
1922
fn ffi_string_to_rust(s: *const c_char) -> String {
2023
if s.is_null() {
2124
return String::new();
2225
}
2326
unsafe { CStr::from_ptr(s) }.to_str().unwrap_or_default().to_owned()
2427
}
2528

26-
extern "C" fn on_filter_headers_progress(filter: u32, headers: u32, pct: f64, _ud: *mut c_void) {
27-
println!("filters: {} headers: {} progress: {:.2}%", filter, headers, pct * 100.0);
28-
}
29-
3029
extern "C" fn on_detailed_progress(progress: *const FFIDetailedSyncProgress, _ud: *mut c_void) {
3130
if progress.is_null() {
3231
return;
@@ -35,10 +34,10 @@ extern "C" fn on_detailed_progress(progress: *const FFIDetailedSyncProgress, _ud
3534
let p = &*progress;
3635
println!(
3736
"height {}/{} {:.2}% peers {} hps {:.1}",
38-
p.current_height,
37+
p.overview.header_height,
3938
p.total_height,
4039
p.percentage * 100.0,
41-
p.connected_peers,
40+
p.overview.peer_count,
4241
p.headers_per_second
4342
);
4443
}
@@ -48,6 +47,7 @@ extern "C" fn on_completion(success: bool, msg: *const c_char, _ud: *mut c_void)
4847
let m = ffi_string_to_rust(msg);
4948
if success {
5049
println!("Completed: {}", m);
50+
SYNC_COMPLETED.store(true, Ordering::SeqCst);
5151
} else {
5252
eprintln!("Failed: {}", m);
5353
}
@@ -171,7 +171,7 @@ fn main() {
171171
std::process::exit(1);
172172
}
173173

174-
// Set minimal event callbacks (progress via filter headers)
174+
// Set minimal event callbacks
175175
let callbacks = FFIEventCallbacks {
176176
on_block: None,
177177
on_transaction: None,
@@ -181,7 +181,6 @@ fn main() {
181181
on_mempool_transaction_removed: None,
182182
on_compact_filter_matched: None,
183183
on_wallet_transaction: None,
184-
on_filter_headers_progress: Some(on_filter_headers_progress),
185184
user_data: ptr::null_mut(),
186185
};
187186
let _ = dash_spv_ffi_client_set_event_callbacks(client, callbacks);
@@ -193,6 +192,9 @@ fn main() {
193192
std::process::exit(1);
194193
}
195194

195+
// Ensure completion flag is reset before starting sync
196+
SYNC_COMPLETED.store(false, Ordering::SeqCst);
197+
196198
// Run sync on this thread; detailed progress will print via callback
197199
let rc = dash_spv_ffi_client_sync_to_tip_with_progress(
198200
client,
@@ -211,10 +213,14 @@ fn main() {
211213
let prog_ptr = dash_spv_ffi_client_get_sync_progress(client);
212214
if !prog_ptr.is_null() {
213215
let prog = &*prog_ptr;
214-
let filters_complete = prog.filter_headers_synced
215-
|| !prog.filter_sync_available
216-
|| disable_filter_sync;
217-
if prog.headers_synced && filters_complete {
216+
let headers_done = SYNC_COMPLETED.load(Ordering::SeqCst);
217+
let filters_complete = if disable_filter_sync || !prog.filter_sync_available {
218+
false
219+
} else {
220+
prog.filter_header_height >= prog.header_height
221+
&& prog.last_synced_filter_height >= prog.filter_header_height
222+
};
223+
if headers_done && (filters_complete || disable_filter_sync) {
218224
dash_spv_ffi_sync_progress_destroy(prog_ptr);
219225
break;
220226
}

dash-spv-ffi/src/callbacks.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ pub type WalletTransactionCallback = Option<
135135
),
136136
>;
137137

138-
pub type FilterHeadersProgressCallback = Option<
139-
extern "C" fn(filter_height: u32, header_height: u32, percentage: f64, user_data: *mut c_void),
140-
>;
141-
142138
#[repr(C)]
143139
pub struct FFIEventCallbacks {
144140
pub on_block: BlockCallback,
@@ -149,7 +145,6 @@ pub struct FFIEventCallbacks {
149145
pub on_mempool_transaction_removed: MempoolRemovedCallback,
150146
pub on_compact_filter_matched: CompactFilterMatchedCallback,
151147
pub on_wallet_transaction: WalletTransactionCallback,
152-
pub on_filter_headers_progress: FilterHeadersProgressCallback,
153148
pub user_data: *mut c_void,
154149
}
155150

@@ -178,7 +173,6 @@ impl Default for FFIEventCallbacks {
178173
on_mempool_transaction_removed: None,
179174
on_compact_filter_matched: None,
180175
on_wallet_transaction: None,
181-
on_filter_headers_progress: None,
182176
user_data: std::ptr::null_mut(),
183177
}
184178
}
@@ -389,23 +383,3 @@ impl FFIEventCallbacks {
389383
}
390384
}
391385
}
392-
393-
impl FFIEventCallbacks {
394-
pub fn call_filter_headers_progress(
395-
&self,
396-
filter_height: u32,
397-
header_height: u32,
398-
percentage: f64,
399-
) {
400-
if let Some(callback) = self.on_filter_headers_progress {
401-
tracing::info!(
402-
"📊 Calling filter headers progress callback: filter_height={}, header_height={}, pct={:.2}",
403-
filter_height, header_height, percentage
404-
);
405-
callback(filter_height, header_height, percentage, self.user_data);
406-
tracing::info!("✅ Filter headers progress callback completed");
407-
} else {
408-
tracing::debug!("Filter headers progress callback not set");
409-
}
410-
}
411-
}

dash-spv-ffi/src/client.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,6 @@ impl FFIDashSpvClient {
249249
} => {
250250
callbacks.call_balance_update(confirmed, unconfirmed);
251251
}
252-
dash_spv::types::SpvEvent::FilterHeadersProgress {
253-
filter_header_height,
254-
header_height,
255-
percentage,
256-
} => {
257-
callbacks.call_filter_headers_progress(
258-
filter_header_height,
259-
header_height,
260-
percentage,
261-
);
262-
}
263252
dash_spv::types::SpvEvent::TransactionDetected {
264253
ref txid,
265254
confirmed,
@@ -804,7 +793,10 @@ pub unsafe extern "C" fn dash_spv_ffi_client_sync_to_tip_with_progress(
804793
match maybe_progress {
805794
Some(progress) => {
806795
// Handle callback in a thread-safe way
807-
let should_stop = matches!(progress.sync_stage, SyncStage::Complete);
796+
let should_stop = matches!(
797+
progress.sync_stage,
798+
SyncStage::Complete | SyncStage::Failed(_)
799+
);
808800

809801
// Create FFI progress
810802
let ffi_progress = Box::new(FFIDetailedSyncProgress::from(progress));
@@ -945,7 +937,7 @@ pub unsafe extern "C" fn dash_spv_ffi_client_sync_to_tip_with_progress(
945937
FFIErrorCode::Success as i32
946938
}
947939

948-
// Note: filter headers progress is forwarded via FFIEventCallbacks.on_filter_headers_progress
940+
// Filter header progress updates are included in the detailed sync progress callback.
949941

950942
/// Cancels the sync operation.
951943
///
@@ -1278,10 +1270,6 @@ pub unsafe extern "C" fn dash_spv_ffi_client_set_event_callbacks(
12781270
tracing::debug!(" Block callback: {}", callbacks.on_block.is_some());
12791271
tracing::debug!(" Transaction callback: {}", callbacks.on_transaction.is_some());
12801272
tracing::debug!(" Balance update callback: {}", callbacks.on_balance_update.is_some());
1281-
tracing::debug!(
1282-
" Filter headers progress callback: {}",
1283-
callbacks.on_filter_headers_progress.is_some()
1284-
);
12851273

12861274
let mut event_callbacks = client.event_callbacks.lock().unwrap();
12871275
*event_callbacks = callbacks;

0 commit comments

Comments
 (0)