Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dash-spv/src/sync/blocks/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
header_storage: Arc<RwLock<H>>,
block_storage: Arc<RwLock<B>>,
) -> Self {
let synced_height = wallet.read().await.synced_height();
let last_processed_height = wallet.read().await.last_processed_height();

let mut initial_progress = BlocksProgress::default();
initial_progress.update_last_processed(synced_height);
initial_progress.update_last_processed(last_processed_height);

Self {
progress: initial_progress,
Expand Down
18 changes: 9 additions & 9 deletions dash-spv/src/sync/filters/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<H: BlockHeaderStorage, FH: FilterHeaderStorage, F: FilterStorage, W: Wallet
filter_header_storage: Arc<RwLock<FH>>,
filter_storage: Arc<RwLock<F>>,
) -> Self {
let committed_height = wallet.read().await.filter_committed_height();
let committed_height = wallet.read().await.synced_height();
let stored_height = filter_storage.read().await.filter_tip_height().await.unwrap_or(0);
let target_height =
header_storage.read().await.get_tip().await.map(|t| t.height()).unwrap_or(0);
Expand Down Expand Up @@ -158,11 +158,11 @@ impl<H: BlockHeaderStorage, FH: FilterHeaderStorage, F: FilterStorage, W: Wallet
) -> SyncResult<Vec<SyncEvent>> {
debug_assert!(self.is_idle(), "manager should have no in-flight state on start");

// Use filter_committed_height for restart recovery instead of
// synced_height, which advances per-block and may exceed committed scan progress.
// Use synced_height for restart recovery instead of
// last_processed_height, which advances per-block and may exceed committed scan progress.
let (wallet_birth_height, wallet_committed_height) = {
let wallet = self.wallet.read().await;
(wallet.earliest_required_height().await, wallet.filter_committed_height())
(wallet.earliest_required_height().await, wallet.synced_height())
};

// Get stored filters tip
Expand Down Expand Up @@ -495,7 +495,7 @@ impl<H: BlockHeaderStorage, FH: FilterHeaderStorage, F: FilterStorage, W: Wallet
let end = batch.end_height();
if end > self.progress.committed_height() {
self.progress.update_committed_height(end);
self.wallet.write().await.update_filter_committed_height(end);
self.wallet.write().await.update_synced_height(end);
}
self.processing_height = end + 1;

Expand Down Expand Up @@ -814,9 +814,9 @@ mod tests {
async fn test_filters_manager_new_restores_from_storage() {
let storage = DiskStorageManager::with_temp_dir().await.unwrap();

// Set wallet committed height via synced_height (MockWallet default delegates)
// Set wallet committed height via last_processed_height (MockWallet default delegates)
let mut wallet = MockWallet::new();
wallet.update_synced_height(50);
wallet.update_last_processed_height(50);
let wallet = Arc::new(RwLock::new(wallet));

// Pre-populate filter storage with filters at heights 1..=100
Expand Down Expand Up @@ -1044,7 +1044,7 @@ mod tests {
assert_eq!(manager.state(), SyncState::WaitForEvents);

// Wallet committed to height 100, so scan_start will be 101
manager.wallet.write().await.update_synced_height(100);
manager.wallet.write().await.update_last_processed_height(100);
// Filter headers only reached 50, so its below scan_start
manager.progress.update_filter_header_tip_height(50);
// Chain tip higher so the Synced early-return is not taken
Expand Down Expand Up @@ -1121,7 +1121,7 @@ mod tests {
// Simulate restart where everything is already synced but state is WaitForEvents.
// committed == stored == filter_header_tip — start_download detects synced state.
manager.set_state(SyncState::WaitForEvents);
manager.wallet.write().await.update_synced_height(100);
manager.wallet.write().await.update_last_processed_height(100);
manager.progress.update_committed_height(100);
manager.progress.update_stored_height(100);
manager.progress.update_filter_header_tip_height(100);
Expand Down
30 changes: 15 additions & 15 deletions key-wallet-ffi/FFI_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ Functions: 64
| `managed_wallet_get_top_up_account_with_registration_index` | Get a managed IdentityTopUp account with a specific registration index This... | managed_account |
| `managed_wallet_get_utxos` | Get all UTXOs from managed wallet info # Safety - `managed_info` must be a... | utxo |
| `managed_wallet_info_free` | Free managed wallet info returned by wallet_manager_get_managed_wallet_info ... | managed_wallet |
| `managed_wallet_last_processed_height` | Get current last processed height from wallet info # Safety -... | managed_wallet |
| `managed_wallet_mark_address_used` | Mark an address as used in the pool This updates the pool's tracking of... | address_pool |
| `managed_wallet_set_gap_limit` | Set the gap limit for an address pool The gap limit determines how many... | address_pool |
| `managed_wallet_synced_height` | Get current synced height from wallet info # Safety - `managed_wallet`... | managed_wallet |
| `wallet_add_account` | Add an account to the wallet without xpub # Safety This function... | wallet |
| `wallet_add_account_with_string_xpub` | Add an account to the wallet with xpub as string # Safety This function... | wallet |
| `wallet_add_account_with_xpub_bytes` | Add an account to the wallet with xpub as byte array # Safety This... | wallet |
Expand Down Expand Up @@ -1140,51 +1140,51 @@ Free managed wallet info returned by wallet_manager_get_managed_wallet_info # S

---

#### `managed_wallet_mark_address_used`
#### `managed_wallet_last_processed_height`

```c
managed_wallet_mark_address_used(managed_wallet: *mut FFIManagedWalletInfo, address: *const c_char, error: *mut FFIError,) -> bool
managed_wallet_last_processed_height(managed_wallet: *const FFIManagedWalletInfo, error: *mut FFIError,) -> c_uint
```

**Description:**
Mark an address as used in the pool This updates the pool's tracking of which addresses have been used, which is important for gap limit management and wallet recovery. # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `address` must be a valid C string - `error` must be a valid pointer to an FFIError
Get current last processed height from wallet info # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call

**Safety:**
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `address` must be a valid C string - `error` must be a valid pointer to an FFIError
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call

**Module:** `address_pool`
**Module:** `managed_wallet`

---

#### `managed_wallet_set_gap_limit`
#### `managed_wallet_mark_address_used`

```c
managed_wallet_set_gap_limit(managed_wallet: *mut FFIManagedWalletInfo, account_type: FFIAccountType, account_index: c_uint, pool_type: FFIAddressPoolType, gap_limit: c_uint, error: *mut FFIError,) -> bool
managed_wallet_mark_address_used(managed_wallet: *mut FFIManagedWalletInfo, address: *const c_char, error: *mut FFIError,) -> bool
```

**Description:**
Set the gap limit for an address pool The gap limit determines how many unused addresses to maintain at the end of the pool. This is important for wallet recovery and address discovery. # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError
Mark an address as used in the pool This updates the pool's tracking of which addresses have been used, which is important for gap limit management and wallet recovery. # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `address` must be a valid C string - `error` must be a valid pointer to an FFIError

**Safety:**
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `address` must be a valid C string - `error` must be a valid pointer to an FFIError

**Module:** `address_pool`

---

#### `managed_wallet_synced_height`
#### `managed_wallet_set_gap_limit`

```c
managed_wallet_synced_height(managed_wallet: *const FFIManagedWalletInfo, error: *mut FFIError,) -> c_uint
managed_wallet_set_gap_limit(managed_wallet: *mut FFIManagedWalletInfo, account_type: FFIAccountType, account_index: c_uint, pool_type: FFIAddressPoolType, gap_limit: c_uint, error: *mut FFIError,) -> bool
```

**Description:**
Get current synced height from wallet info # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call
Set the gap limit for an address pool The gap limit determines how many unused addresses to maintain at the end of the pool. This is important for wallet recovery and address discovery. # Safety - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError

**Safety:**
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call
- `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo - `error` must be a valid pointer to an FFIError

**Module:** `managed_wallet`
**Module:** `address_pool`

---

Expand Down
6 changes: 3 additions & 3 deletions key-wallet-ffi/src/managed_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,20 @@ pub unsafe extern "C" fn managed_wallet_get_balance(
true
}

/// Get current synced height from wallet info
/// Get current last processed height from wallet info
///
/// # Safety
///
/// - `managed_wallet` must be a valid pointer to an FFIManagedWalletInfo
/// - `error` must be a valid pointer to an FFIError structure
/// - The caller must ensure all pointers remain valid for the duration of this call
#[no_mangle]
pub unsafe extern "C" fn managed_wallet_synced_height(
pub unsafe extern "C" fn managed_wallet_last_processed_height(
managed_wallet: *const FFIManagedWalletInfo,
error: *mut FFIError,
) -> c_uint {
let managed_wallet = deref_ptr!(managed_wallet, error);
managed_wallet.inner().synced_height()
managed_wallet.inner().last_processed_height()
}

/// Free managed wallet info
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub unsafe extern "C" fn wallet_build_and_sign_transaction(
let mut tx_builder_with_inputs = match tx_builder.select_inputs(
&utxos,
SelectionStrategy::BranchAndBound,
managed_wallet.synced_height(),
managed_wallet.last_processed_height(),
|utxo| {
// Look up the derivation path for this UTXO's address
let path = address_to_path.get(&utxo.address)?;
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub unsafe extern "C" fn managed_wallet_get_utxos(
// Get script bytes
let script_bytes = utxo.txout.script_pubkey.as_bytes().to_vec();

let current_height = managed_info.inner().synced_height();
let current_height = managed_info.inner().last_processed_height();
let confirmations = utxo.confirmations(current_height);

let ffi_utxo = FFIUTXO::new(
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod utxo_tests {
managed_info.accounts.insert(bip44_account).unwrap();

let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));
unsafe { (*ffi_managed_info).inner_mut() }.update_synced_height(300);
unsafe { (*ffi_managed_info).inner_mut() }.update_last_processed_height(300);
let result = unsafe {
managed_wallet_get_utxos(&*ffi_managed_info, &mut utxos_out, &mut count_out, error)
};
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/wallet_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ pub unsafe extern "C" fn wallet_manager_current_height(
let manager_ref = deref_ptr!(manager, error);
manager_ref.runtime.block_on(async {
let manager_guard = manager_ref.manager.read().await;
manager_guard.synced_height()
manager_guard.last_processed_height()
})
}

Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/wallet_manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ mod tests {
let manager_ref = &*manager;
manager_ref.runtime.block_on(async {
let mut manager_guard = manager_ref.manager.write().await;
manager_guard.update_synced_height(new_height);
manager_guard.update_last_processed_height(new_height);
});
}

Expand Down
6 changes: 3 additions & 3 deletions key-wallet-manager/examples/wallet_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ fn main() {
// Example 7: Block height tracking
println!("\n7. Block height tracking...");

println!(" Current height (Testnet): {:?}", manager.synced_height());
println!(" Current height (Testnet): {:?}", manager.last_processed_height());

// Update height
manager.update_synced_height(850_000);
println!(" Updated height to: {:?}", manager.synced_height());
manager.update_last_processed_height(850_000);
println!(" Updated height to: {:?}", manager.last_processed_height());

println!("\n=== Summary ===");
println!("Total wallets created: {}", manager.wallet_count());
Expand Down
14 changes: 7 additions & 7 deletions key-wallet-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ pub struct WalletManager<T: WalletInfoInterface = ManagedWalletInfo> {
/// Network the managed wallets are used for
network: Network,
/// Last fully processed block height.
synced_height: CoreBlockHeight,
last_processed_height: CoreBlockHeight,
/// Height at which filter scanning was last committed.
filter_committed_height: CoreBlockHeight,
synced_height: CoreBlockHeight,
/// Immutable wallets indexed by wallet ID
wallets: BTreeMap<WalletId, Wallet>,
/// Mutable wallet info indexed by wallet ID
Expand All @@ -111,8 +111,8 @@ impl<T: WalletInfoInterface> WalletManager<T> {
pub fn new(network: Network) -> Self {
Self {
network,
last_processed_height: 0,
synced_height: 0,
filter_committed_height: 0,
wallets: BTreeMap::new(),
wallet_infos: BTreeMap::new(),
structural_revision: 0,
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.synced_height);
managed_info.set_birth_height(self.last_processed_height);
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.synced_height);
managed_info.set_birth_height(self.last_processed_height);
managed_info.set_first_loaded_at(current_timestamp());
Comment thread
xdustinface marked this conversation as resolved.

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.synced_height);
managed_info.set_birth_height(self.last_processed_height);
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -438,7 +438,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
let mut managed_info = T::from_wallet(&wallet);

// Use the current height as the birth height since we don't know when it was originally created
managed_info.set_birth_height(self.synced_height);
managed_info.set_birth_height(self.last_processed_height);
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down
52 changes: 26 additions & 26 deletions key-wallet-manager/src/process_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
result.new_addresses.extend(check_result.new_addresses);
}

self.update_synced_height(height);
self.update_last_processed_height(height);

result
}
Expand Down Expand Up @@ -97,30 +97,30 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
self.wallet_infos.values().map(|info| info.birth_height()).min().unwrap_or(0)
}

fn synced_height(&self) -> CoreBlockHeight {
self.synced_height
fn last_processed_height(&self) -> CoreBlockHeight {
self.last_processed_height
}

fn update_synced_height(&mut self, height: CoreBlockHeight) {
self.synced_height = height;
fn update_last_processed_height(&mut self, height: CoreBlockHeight) {
self.last_processed_height = height;

let snapshot = self.snapshot_balances();

for (_wallet_id, info) in self.wallet_infos.iter_mut() {
info.update_synced_height(height);
info.update_last_processed_height(height);
}

self.emit_balance_changes(&snapshot);
}

fn filter_committed_height(&self) -> CoreBlockHeight {
self.filter_committed_height
fn synced_height(&self) -> CoreBlockHeight {
self.synced_height
}

fn update_filter_committed_height(&mut self, height: CoreBlockHeight) {
self.filter_committed_height = height;
if height > self.synced_height {
self.update_synced_height(height);
fn update_synced_height(&mut self, height: CoreBlockHeight) {
self.synced_height = height;
if height > self.last_processed_height {
self.update_last_processed_height(height);
}
}

Expand Down Expand Up @@ -216,19 +216,19 @@ mod tests {
}

#[tokio::test]
async fn test_synced_height() {
async fn test_last_processed_height() {
let mut manager: WalletManager<ManagedWalletInfo> = WalletManager::new(Network::Testnet);
// Initial state
assert_eq!(manager.synced_height(), 0);
// Inrease synced height
manager.update_synced_height(1000);
assert_eq!(manager.synced_height(), 1000);
//Increase synced height again
manager.update_synced_height(5000);
assert_eq!(manager.synced_height(), 5000);
// Decrease synced height
manager.update_synced_height(10);
assert_eq!(manager.synced_height(), 10);
assert_eq!(manager.last_processed_height(), 0);
// Increase last processed height
manager.update_last_processed_height(1000);
assert_eq!(manager.last_processed_height(), 1000);
// Increase last processed height again
manager.update_last_processed_height(5000);
assert_eq!(manager.last_processed_height(), 5000);
// Decrease last processed height
manager.update_last_processed_height(10);
assert_eq!(manager.last_processed_height(), 10);
}

#[tokio::test]
Expand Down Expand Up @@ -386,9 +386,9 @@ mod tests {
assert_eq!(manager.monitor_revision(), expected_rev, "after get_change_address");
}

// update_synced_height does NOT bump
manager.update_synced_height(1000);
assert_eq!(manager.monitor_revision(), expected_rev, "after update_synced_height");
// update_last_processed_height does NOT bump
manager.update_last_processed_height(1000);
assert_eq!(manager.monitor_revision(), expected_rev, "after update_last_processed_height");

// process_mempool_transaction bumps from UTXO changes and possibly
// new addresses generated via gap limit maintenance
Expand Down
Loading
Loading