What changed, and why it matters
This change is a performance optimization for the Monero wallet's balance-reporting RPC. When a user asks for balances across many subaddresses, the code now pre-counts unspent outputs in one pass instead of re-scanning the entire transaction list once per subaddress. The functional test was updated to check that a multi-subaddress balance query returns the same results as before. There is no security vulnerability here.
No security action needed. Treat as a normal performance improvement and review for correctness during routine code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies wallet_rpc_server.cpp’s get_balance handler. Previously, num_unspent_outputs was computed with std::count_if over all transfers for every requested address index, giving O(n*m) behavior. The new code builds a single std::map
Changed components
src/wallet/wallet_rpc_server.cpptests/functional_tests/transfer.pyInspect captured patch +18 / −1
### src/wallet/wallet_rpc_server.cpp
@@ -754,6 +754,12 @@ namespace tools
for (const auto& i : balance_per_subaddress)
address_indices.insert(i.first);
}
+ std::map<uint32_t, uint64_t> unspent_counts;
+ if (address_indices.size() > 1)
+ for (const auto& td : transfers)
+ if (!td.m_spent && td.m_subaddr_index.major == account_index &&
+ address_indices.count(td.m_subaddr_index.minor) != 0)
+ ++unspent_counts[td.m_subaddr_index.minor];
for (uint32_t i : address_indices)
{
wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info;
@@ -766,7 +772,13 @@ namespace tools
info.blocks_to_unlock = unlocked_balance_per_subaddress[i].second.first;
info.time_to_unlock = unlocked_balance_per_subaddress[i].second.second;
info.label = m_wallet->get_subaddress_label(index);
- info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; });
+ if (address_indices.size() == 1)
+ info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; });
+ else
+ {
+ const auto count = unspent_counts.find(i);
+ info.num_unspent_outputs = count == unspent_counts.end() ? 0 : count->second;
+ }
res.per_subaddress.emplace_back(std::move(info));
}
}
### tests/functional_tests/transfer.py
@@ -1598,6 +1598,11 @@ def check_subaddress_lookahead(self):
assert balance_info_0_999['blocks_to_unlock'] == 9
assert balance_info_0_999['time_to_unlock'] == 0
+ selected = self.wallet[0].get_balance(address_indices = [0, 998, 999])['per_subaddress']
+ selected = {info['address_index']: info for info in selected}
+ assert selected[998]['balance'] == selected[998]['num_unspent_outputs'] == 0
+ assert selected[999] == balance_info_0_999
+
def check_pool_scanner(self):
daemon = Daemon()
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.