Skip to content

Commit 0cbb078

Browse files
authored
refactor: Make wallet birth height non-optional (#300)
1 parent 0903286 commit 0cbb078

File tree

14 files changed

+42
-57
lines changed

14 files changed

+42
-57
lines changed

dash-spv-ffi/tests/test_wallet_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod tests {
6464
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
6565
"",
6666
Network::Dash,
67-
None,
67+
0,
6868
WalletAccountCreationOptions::Default,
6969
false,
7070
false,

dash-spv/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
307307
mnemonic_phrase.as_str(),
308308
"",
309309
network,
310-
None,
310+
0,
311311
key_wallet::wallet::initialization::WalletAccountCreationOptions::default(),
312312
)?;
313313
let wallet = Arc::new(tokio::sync::RwLock::new(wallet_manager));

dash-spv/src/sync/manager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::storage::StorageManager;
99
use crate::sync::{FilterSyncManager, HeaderSyncManager, MasternodeSyncManager, ReorgConfig};
1010
use crate::types::{SharedFilterHeights, SyncProgress};
1111
use crate::{SpvStats, SyncError};
12+
use dashcore::prelude::CoreBlockHeight;
1213
use dashcore::BlockHash;
1314
use key_wallet_manager::{wallet_interface::WalletInterface, Network as WalletNetwork};
1415
use std::sync::Arc;
@@ -158,14 +159,14 @@ impl<
158159
}
159160

160161
/// Get the earliest wallet birth height hint for the configured network, if available.
161-
pub async fn wallet_birth_height_hint(&self) -> Option<u32> {
162+
pub async fn wallet_birth_height_hint(&self) -> CoreBlockHeight {
162163
// Map the dashcore network to wallet network, returning None for unknown variants
163164
let wallet_network = match self.config.network {
164165
dashcore::Network::Dash => WalletNetwork::Dash,
165166
dashcore::Network::Testnet => WalletNetwork::Testnet,
166167
dashcore::Network::Devnet => WalletNetwork::Devnet,
167168
dashcore::Network::Regtest => WalletNetwork::Regtest,
168-
_ => return None, // Unknown network variant - return None instead of defaulting
169+
_ => return 0, // Unknown network variant - return None instead of defaulting
169170
};
170171

171172
// Only acquire the wallet lock if we have a valid network mapping

key-wallet-ffi/include/key_wallet_ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3941,7 +3941,7 @@ bool wallet_manager_add_wallet_from_mnemonic(FFIWalletManager *manager,
39413941
- `manager` must be a valid pointer to an FFIWalletManager instance
39423942
- `mnemonic` must be a valid pointer to a null-terminated C string
39433943
- `passphrase` must be a valid pointer to a null-terminated C string or null
3944-
- `birth_height` is optional, pass 0 for default
3944+
- `birth_height` is the block height to start syncing from (0 = sync from genesis)
39453945
- `account_options` must be a valid pointer to FFIWalletAccountCreationOptions or null
39463946
- `downgrade_to_pubkey_wallet` if true, creates a watch-only or externally signable wallet
39473947
- `allow_external_signing` if true AND downgrade_to_pubkey_wallet is true, creates an externally signable wallet

key-wallet-ffi/src/wallet_manager.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic_with_options(
215215
mnemonic_str,
216216
passphrase_str,
217217
network_rust,
218-
None, // birth_height
218+
0,
219219
creation_options,
220220
)
221221
});
@@ -274,7 +274,7 @@ pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic(
274274
/// - `manager` must be a valid pointer to an FFIWalletManager instance
275275
/// - `mnemonic` must be a valid pointer to a null-terminated C string
276276
/// - `passphrase` must be a valid pointer to a null-terminated C string or null
277-
/// - `birth_height` is optional, pass 0 for default
277+
/// - `birth_height` is the block height to start syncing from (0 = sync from genesis)
278278
/// - `account_options` must be a valid pointer to FFIWalletAccountCreationOptions or null
279279
/// - `downgrade_to_pubkey_wallet` if true, creates a watch-only or externally signable wallet
280280
/// - `allow_external_signing` if true AND downgrade_to_pubkey_wallet is true, creates an externally signable wallet
@@ -369,13 +369,6 @@ pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic_return_serializ
369369
// Get the manager and call the proper method
370370
let manager_ref = unsafe { &*manager };
371371

372-
// Convert birth_height: 0 means None, any other value means Some(value)
373-
let birth_height = if birth_height == 0 {
374-
None
375-
} else {
376-
Some(birth_height)
377-
};
378-
379372
let result = manager_ref.runtime.block_on(async {
380373
let mut manager_guard = manager_ref.manager.write().await;
381374

key-wallet-manager/examples/wallet_creation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() {
4747
test_mnemonic,
4848
"", // No passphrase
4949
Network::Testnet,
50-
Some(100_000), // Birth height
50+
100_000,
5151
key_wallet::wallet::initialization::WalletAccountCreationOptions::Default,
5252
);
5353

key-wallet-manager/src/wallet_interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ pub trait WalletInterface: Send + Sync {
5858
///
5959
/// The default implementation returns `None`, which signals that the caller should
6060
/// fall back to its existing behaviour.
61-
async fn earliest_required_height(&self, _network: Network) -> Option<CoreBlockHeight> {
62-
None
61+
async fn earliest_required_height(&self, _network: Network) -> CoreBlockHeight {
62+
0
6363
}
6464

6565
/// Provide a human-readable description of the wallet implementation.

key-wallet-manager/src/wallet_manager/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use alloc::collections::BTreeMap;
1111
use alloc::string::String;
1212
use alloc::vec::Vec;
1313
use dashcore::blockdata::transaction::Transaction;
14+
use dashcore::prelude::CoreBlockHeight;
1415
use dashcore::{BlockHash, Txid};
1516
use key_wallet::account::AccountCollection;
1617
use key_wallet::transaction_checking::TransactionContext;
@@ -121,7 +122,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
121122
mnemonic: &str,
122123
passphrase: &str,
123124
network: Network,
124-
birth_height: Option<u32>,
125+
birth_height: CoreBlockHeight,
125126
account_creation_options: key_wallet::wallet::initialization::WalletAccountCreationOptions,
126127
) -> Result<WalletId, WalletError> {
127128
let mnemonic_obj = Mnemonic::from_phrase(mnemonic, key_wallet::mnemonic::Language::English)
@@ -178,7 +179,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
178179
/// * `mnemonic` - The mnemonic phrase
179180
/// * `passphrase` - Optional BIP39 passphrase (empty string for no passphrase)
180181
/// * `network` - The network for the wallet
181-
/// * `birth_height` - Optional birth height for wallet scanning
182+
/// * `birth_height` - Birth height for wallet scanning (0 to sync from genesis)
182183
/// * `account_creation_options` - Which accounts to create initially
183184
/// * `downgrade_to_pubkey_wallet` - If true, creates a wallet without private keys
184185
/// * `allow_external_signing` - If true and downgraded, creates an externally signable wallet (e.g., for hardware wallets)
@@ -198,7 +199,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
198199
mnemonic: &str,
199200
passphrase: &str,
200201
network: Network,
201-
birth_height: Option<u32>,
202+
birth_height: CoreBlockHeight,
202203
account_creation_options: key_wallet::wallet::initialization::WalletAccountCreationOptions,
203204
downgrade_to_pubkey_wallet: bool,
204205
allow_external_signing: bool,
@@ -302,7 +303,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
302303
// Create managed wallet info
303304
let mut managed_info = T::from_wallet(&wallet);
304305
let network_state = self.get_or_create_network_state(network);
305-
managed_info.set_birth_height(Some(network_state.current_height));
306+
managed_info.set_birth_height(network_state.current_height);
306307
managed_info.set_first_loaded_at(current_timestamp());
307308

308309
self.wallets.insert(wallet_id, wallet);
@@ -396,8 +397,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
396397

397398
// Create managed wallet info
398399
let mut managed_info = T::from_wallet(&wallet);
399-
managed_info
400-
.set_birth_height(Some(self.get_or_create_network_state(network).current_height));
400+
managed_info.set_birth_height(self.get_or_create_network_state(network).current_height);
401401
managed_info.set_first_loaded_at(current_timestamp());
402402

403403
self.wallets.insert(wallet_id, wallet);
@@ -446,8 +446,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
446446

447447
// Create managed wallet info
448448
let mut managed_info = T::from_wallet(&wallet);
449-
managed_info
450-
.set_birth_height(Some(self.get_or_create_network_state(network).current_height));
449+
managed_info.set_birth_height(self.get_or_create_network_state(network).current_height);
451450
managed_info.set_first_loaded_at(current_timestamp());
452451

453452
self.wallets.insert(wallet_id, wallet);
@@ -492,7 +491,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
492491

493492
// Use the current network's height as the birth height since we don't know when it was originally created
494493
let network_state = self.get_or_create_network_state(wallet.network);
495-
managed_info.set_birth_height(Some(network_state.current_height));
494+
managed_info.set_birth_height(network_state.current_height);
496495
managed_info.set_first_loaded_at(current_timestamp());
497496

498497
self.wallets.insert(wallet_id, wallet);

key-wallet-manager/src/wallet_manager/process_block.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,13 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
174174
}
175175
}
176176

177-
async fn earliest_required_height(&self, network: Network) -> Option<CoreBlockHeight> {
178-
let mut earliest: Option<CoreBlockHeight> = None;
179-
180-
for info in self.wallet_infos.values() {
181-
// Only consider wallets that actually track this network AND have a known birth height
182-
if info.network() == network {
183-
if let Some(birth_height) = info.birth_height() {
184-
earliest = Some(match earliest {
185-
Some(current) => current.min(birth_height),
186-
None => birth_height,
187-
});
188-
}
189-
}
190-
}
191-
192-
// Return None if no wallets with known birth heights were found for this network
193-
earliest
177+
async fn earliest_required_height(&self, network: Network) -> CoreBlockHeight {
178+
self.wallet_infos
179+
.values()
180+
.filter(|info| info.network() == network)
181+
.map(|info| info.birth_height())
182+
.min()
183+
.unwrap_or(0)
194184
}
195185

196186
async fn describe(&self, network: Network) -> String {

key-wallet-manager/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn test_wallet_manager_from_mnemonic() {
3030
&mnemonic.to_string(),
3131
"",
3232
Network::Testnet,
33-
None, // birth_height
33+
0,
3434
WalletAccountCreationOptions::Default,
3535
);
3636
assert!(wallet_result.is_ok(), "Failed to create wallet: {:?}", wallet_result);

0 commit comments

Comments
 (0)