Skip to content

💡 [REQUEST] - Remove account from AddressStorage #436

@SteMak

Description

@SteMak

Summary

AddressStorage does not provide methods for accounts removal

This limits experience of validating PDA create/close flow

Basic Example

    #[flow]
    fn flow1(&mut self) {
        let address = self.new_account();
        self.fuzz_accounts.account.insert_with_address(address);
    }

    #[flow]
    fn flow2(&mut self) {
        let address_opt = self.fuzz_accounts.account.get(&mut self.trident);
        if address_opt.is_none() {
            return;
        }
        let address = address_opt.unwrap();

        self.fuzz_accounts.account.remove(address);
        self.close_account(address);
    }

Drawbacks

Vec<Pubkey> is O(n) complexity for element search

Unresolved questions

Let me propose the patch to address_storage.rs file

  • remove function implemented
  • insert functions are modified to add address to storage only once
    /// Inserts a new address into storage
    ///
    /// Generates a new address (either a PDA or random keypair) and stores it.
    /// If PDA seeds are provided, attempts to derive a PDA. If derivation fails
    /// or no seeds are provided, generates a random keypair address.
    ///
    /// # Arguments
    /// * `trident` - The Trident instance for random number generation
    /// * `seeds` - Optional PDA seeds for deriving a program-derived address
    ///
    /// # Returns
    /// The newly created and stored address
    pub fn insert(&mut self, trident: &mut Trident, seeds: Option<PdaSeeds>) -> Pubkey {
        let address = self.get_or_create_address(seeds, trident);

        if !self.addresses.iter().any(|x| *x == address) {
            self.addresses.push(address);
        }

        address
    }

    /// Inserts an existing address into storage
    ///
    /// Stores a pre-existing address without generating a new one.
    /// Useful when you need to track addresses created elsewhere.
    ///
    /// # Arguments
    /// * `address` - The address to store
    pub fn insert_with_address(&mut self, address: Pubkey) {
        if !self.addresses.iter().any(|x| *x == address) {
            self.addresses.push(address);
        }
    }

    /// Removes an existing address from storage
    ///
    /// Removes specified address from the storage.
    /// Useful when you closed account.
    ///
    /// # Arguments
    /// * `address` - The address to remove
    pub fn remove(&mut self, address: Pubkey) {
        if let Some(i) = self.addresses.iter().position(|x| *x == address) {
            self.addresses.swap_remove(i);
        }
    }

Implementation PR

No response

Reference Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions