Skip to content

key-wallet: add AddressPool::next_unused_index and drop the duplicated index math #1352

Closed

Description

@xdustinface

Problem

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

  1. added a commit that references this issue on Nov 11, 2026
    606888e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions