Skip to content

Commit ba0a2df

Browse files
committed
Use transaction timestamp for syncing token payments
1 parent c601788 commit ba0a2df

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

crates/breez-sdk/core/src/persist/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
22
pub(crate) mod sqlite;
33

4-
use std::{collections::HashMap, sync::Arc};
4+
use std::{collections::HashMap, sync::Arc, time::SystemTime};
55

66
use macros::async_trait;
77
use serde::{Deserialize, Serialize};
@@ -273,7 +273,7 @@ pub(crate) struct CachedAccountInfo {
273273
pub(crate) struct CachedSyncInfo {
274274
pub(crate) offset: u64,
275275
#[serde(default)]
276-
pub(crate) token_offset: u64,
276+
pub(crate) last_synced_token_timestamp: Option<SystemTime>,
277277
}
278278

279279
#[derive(Serialize, Deserialize, Default)]

crates/breez-sdk/core/src/sdk.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl BreezSdk {
382382
let save_res = object_repository
383383
.save_sync_info(&CachedSyncInfo {
384384
offset: next_offset.saturating_sub(pending_payments),
385-
token_offset: cached_sync_info.token_offset,
385+
last_synced_token_timestamp: cached_sync_info.last_synced_token_timestamp,
386386
})
387387
.await;
388388

@@ -404,14 +404,18 @@ impl BreezSdk {
404404
.fetch_sync_info()
405405
.await?
406406
.unwrap_or_default();
407-
let current_token_offset = cached_sync_info.token_offset;
407+
let last_synced_token_timestamp = cached_sync_info.last_synced_token_timestamp;
408408

409409
let our_public_key = self.spark_wallet.get_identity_public_key();
410410

411+
let mut latest_token_transaction_timestamp = None;
412+
411413
// We'll keep querying in batches until we have all transfers
412-
let mut next_offset = current_token_offset;
414+
let mut next_offset = 0;
413415
let mut has_more = true;
414-
info!("Syncing token payments to storage, offset = {next_offset}");
416+
info!(
417+
"Syncing token payments to storage, last synced token timestamp = {last_synced_token_timestamp:?}"
418+
);
415419
while has_more {
416420
// Get batch of token transactions starting from current offset
417421
let token_transactions = self
@@ -420,12 +424,18 @@ impl BreezSdk {
420424
paging: Some(PagingFilter::new(
421425
Some(next_offset),
422426
Some(PAYMENT_SYNC_BATCH_SIZE),
423-
Some(Order::Ascending),
427+
None,
424428
)),
425429
..Default::default()
426430
})
427431
.await?;
428432

433+
// On first iteration, set the latest token transaction timestamp to the first transaction timestamp
434+
if next_offset == 0 {
435+
latest_token_transaction_timestamp =
436+
token_transactions.first().map(|tx| tx.created_timestamp);
437+
}
438+
429439
// Get prev out hashes of first input of each token transaction
430440
// Assumes all inputs of a tx share the same owner public key
431441
let token_transactions_prevout_hashes = token_transactions
@@ -459,6 +469,13 @@ impl BreezSdk {
459469
);
460470
// Process transfers in this batch
461471
for transaction in &token_transactions {
472+
// Stop syncing if we have reached the last synced token transaction timestamp
473+
if let Some(last_synced_token_timestamp) = last_synced_token_timestamp {
474+
if transaction.created_timestamp <= last_synced_token_timestamp {
475+
break;
476+
}
477+
}
478+
462479
let tx_inputs_are_ours = match &transaction.inputs {
463480
spark_wallet::TokenInputs::Transfer(token_transfer_input) => {
464481
let Some(first_input) = token_transfer_input.outputs_to_spend.first()
@@ -504,18 +521,21 @@ impl BreezSdk {
504521

505522
// Check if we have more transfers to fetch
506523
next_offset = next_offset.saturating_add(u64::try_from(token_transactions.len())?);
507-
// Update our last processed offset in the storage
524+
has_more = token_transactions.len() as u64 == PAYMENT_SYNC_BATCH_SIZE;
525+
}
526+
527+
// Update our last processed transaction timestamp in the storage
528+
if let Some(latest_token_transaction_timestamp) = latest_token_transaction_timestamp {
508529
let save_res = object_repository
509530
.save_sync_info(&CachedSyncInfo {
510531
offset: cached_sync_info.offset,
511-
token_offset: next_offset,
532+
last_synced_token_timestamp: Some(latest_token_transaction_timestamp),
512533
})
513534
.await;
514535

515536
if let Err(err) = save_res {
516-
error!("Failed to update last sync token offset: {err:?}");
537+
error!("Failed to update last sync token timestamp: {err:?}");
517538
}
518-
has_more = token_transactions.len() as u64 == PAYMENT_SYNC_BATCH_SIZE;
519539
}
520540

521541
Ok(())

0 commit comments

Comments
 (0)