You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
key-wallet: add AddressPool::next_unused_index and drop the duplicated index math #1352
AddressPool has no method that returns the index of the next address to hand out, so every caller works it out by hand.
key-wallet/src/managed_account/address_pool.rs repeats self.highest_generated.map(|h| h + 1).unwrap_or(0) (or the map_or form) nine times: generate_addresses, generate_up_to, next_unused, next_unused_with_info, next_unused_and_reserve, next_unused_multiple, next_unused_multiple_with_info, unused_addresses_count and the gap-limit maintenance loop.
The index getters on ManagedAccountTrait (get_next_receive_address_index, get_next_change_address_index, get_next_address_index) find the first free address by calling unused_addresses(), which clones every available address into a Vec, keeps only the first and then looks its index up in the address_index hash map. They also call stats(), which counts reserved addresses across the whole pool, only to read highest_generated.
Each copy can drift from the "first available address" rule that next_unused applies, and the getters do work proportional to the pool size for a single index.
Fix
Add AddressPool::next_unused_index(&self) -> u32. It returns the key of the first entry in the ordered addresses map whose info is_available(), or highest_generated + 1 (0 for an empty pool) when none is free. Route the trait getters through it, and have the next_unused* and generation paths reuse the same "next index to derive" expression instead of restating it.
Context: #1341 shares one helper across the three trait getters but keeps the Vec based lookup.
Activity
refactor(key-wallet)!: derive pool addresses in one place