Skip to content

Commit 825c9e5

Browse files
refactor(key-wallet): add all_funding_accounts(_mut) and drop in-loop as_funds() filters
Five funds-only callsites in `wallet_info_interface.rs` were iterating `all_accounts()` and filtering each ref via `as_funds()` / `as_funds_mut()` inside the loop body — pure noise, since balance / UTXO state only ever lives on Standard / CoinJoin / DashPay accounts. Add focused accessors on `ManagedAccountCollection`: - `all_funding_accounts(&self) -> Vec<&ManagedCoreFundsAccount>` - `all_funding_accounts_mut(&mut self) -> Vec<&mut ManagedCoreFundsAccount>` Migrate `account_balances`, `utxos`, `update_balance`, `immature_transactions`, and `matured_coinbase_records` to use them. `monitored_addresses`, `transaction_history`, `monitor_revision`, and `mark_instant_send_utxos` keep using `all_accounts(_mut)` — they legitimately span both variants. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1e7edbf commit 825c9e5

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

key-wallet/src/managed_account/managed_account_collection.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,34 @@ impl ManagedAccountCollection {
11521152
accounts
11531153
}
11541154

1155+
/// Get all funds-bearing accounts (Standard BIP44/32, CoinJoin, DashPay).
1156+
///
1157+
/// Use this from callsites that operate on balance / UTXO state — keys-only
1158+
/// accounts (identity, asset-lock, provider) don't track those, so iterating
1159+
/// [`Self::all_accounts`] and filtering via [`ManagedAccountRef::as_funds`]
1160+
/// in those callsites is just noise.
1161+
pub fn all_funding_accounts(&self) -> Vec<&ManagedCoreFundsAccount> {
1162+
let mut accounts = Vec::new();
1163+
accounts.extend(self.standard_bip44_accounts.values());
1164+
accounts.extend(self.standard_bip32_accounts.values());
1165+
accounts.extend(self.coinjoin_accounts.values());
1166+
accounts.extend(self.dashpay_receival_accounts.values());
1167+
accounts.extend(self.dashpay_external_accounts.values());
1168+
accounts
1169+
}
1170+
1171+
/// Get all funds-bearing accounts mutably. See [`Self::all_funding_accounts`]
1172+
/// for which account types are visited.
1173+
pub fn all_funding_accounts_mut(&mut self) -> Vec<&mut ManagedCoreFundsAccount> {
1174+
let mut accounts = Vec::new();
1175+
accounts.extend(self.standard_bip44_accounts.values_mut());
1176+
accounts.extend(self.standard_bip32_accounts.values_mut());
1177+
accounts.extend(self.coinjoin_accounts.values_mut());
1178+
accounts.extend(self.dashpay_receival_accounts.values_mut());
1179+
accounts.extend(self.dashpay_external_accounts.values_mut());
1180+
accounts
1181+
}
1182+
11551183
/// Get the count of accounts
11561184
pub fn count(&self) -> usize {
11571185
self.all_accounts().len()

key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,9 @@ pub trait WalletInfoInterface: Sized + WalletTransactionChecker + ManagedAccount
7878
/// balance.
7979
fn account_balances(&self) -> BTreeMap<AccountType, WalletCoreBalance> {
8080
self.accounts()
81-
.all_accounts()
81+
.all_funding_accounts()
8282
.iter()
83-
.filter_map(|acc| {
84-
let funds = acc.as_funds()?;
85-
Some((funds.managed_account_type().to_account_type(), funds.balance))
86-
})
83+
.map(|funds| (funds.managed_account_type().to_account_type(), funds.balance))
8784
.collect()
8885
}
8986

@@ -196,10 +193,8 @@ impl WalletInfoInterface for ManagedWalletInfo {
196193

197194
fn utxos(&self) -> BTreeSet<&Utxo> {
198195
let mut utxos = BTreeSet::new();
199-
for account in self.accounts.all_accounts() {
200-
if let Some(funds) = account.as_funds() {
201-
utxos.extend(funds.utxos.values());
202-
}
196+
for account in self.accounts.all_funding_accounts() {
197+
utxos.extend(account.utxos.values());
203198
}
204199
utxos
205200
}
@@ -215,14 +210,12 @@ impl WalletInfoInterface for ManagedWalletInfo {
215210
}
216211

217212
fn update_balance(&mut self) {
213+
// Only funds-bearing accounts contribute to the wallet balance.
218214
let mut balance = WalletCoreBalance::default();
219215
let last_processed_height = self.last_processed_height();
220-
for mut account in self.accounts.all_accounts_mut() {
221-
// Only funds-bearing accounts contribute to the wallet balance.
222-
if let Some(funds) = account.as_funds_mut() {
223-
funds.update_balance(last_processed_height);
224-
balance += funds.balance;
225-
}
216+
for funds in self.accounts.all_funding_accounts_mut() {
217+
funds.update_balance(last_processed_height);
218+
balance += funds.balance;
226219
}
227220
self.balance = balance;
228221
}
@@ -244,14 +237,10 @@ impl WalletInfoInterface for ManagedWalletInfo {
244237
}
245238

246239
fn immature_transactions(&self) -> Vec<Transaction> {
247-
let mut immature_txids: BTreeSet<Txid> = BTreeSet::new();
248-
249240
// Coinbase UTXOs only live on funds-bearing accounts.
250-
for account in self.accounts.all_accounts() {
251-
let Some(funds) = account.as_funds() else {
252-
continue;
253-
};
254-
for utxo in funds.utxos.values() {
241+
let mut immature_txids: BTreeSet<Txid> = BTreeSet::new();
242+
for account in self.accounts.all_funding_accounts() {
243+
for utxo in account.utxos.values() {
255244
if utxo.is_coinbase && !utxo.is_mature(self.last_processed_height()) {
256245
immature_txids.insert(utxo.outpoint.txid);
257246
}
@@ -260,11 +249,8 @@ impl WalletInfoInterface for ManagedWalletInfo {
260249

261250
// Look up the matching transaction records on the same funds accounts.
262251
let mut transactions = Vec::new();
263-
for account in self.accounts.all_accounts() {
264-
let Some(funds) = account.as_funds() else {
265-
continue;
266-
};
267-
for (txid, record) in funds.transactions() {
252+
for account in self.accounts.all_funding_accounts() {
253+
for (txid, record) in account.transactions() {
268254
if immature_txids.contains(txid) {
269255
transactions.push(record.transaction.clone());
270256
}
@@ -291,13 +277,10 @@ impl WalletInfoInterface for ManagedWalletInfo {
291277
if new_height <= old_height {
292278
return Vec::new();
293279
}
280+
// Coinbase records only land on funds-bearing accounts.
294281
let mut matured = Vec::new();
295-
for account in self.accounts.all_accounts() {
296-
// Coinbase records only land on funds-bearing accounts.
297-
let Some(funds) = account.as_funds() else {
298-
continue;
299-
};
300-
for record in funds.transactions().values() {
282+
for account in self.accounts.all_funding_accounts() {
283+
for record in account.transactions().values() {
301284
if !record.transaction.is_coin_base() {
302285
continue;
303286
}

0 commit comments

Comments
 (0)