bad-addresses 0.0.1

Stores recent active addresses in Badchain
Documentation
#![no_std]
extern crate alloc;
use alloc::collections::BTreeMap; // replace with HashMap if std is allowed
use alloc::vec::Vec;

use solana_sdk::{clock::Slot, pubkey::Pubkey};

pub const MAX_ADDRESSES: usize = 512;

pub mod sysvar;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct BadAddresses {
    map: BTreeMap<Pubkey, Slot>,
}

impl Default for BadAddresses {
    fn default() -> Self {
        Self {
            map: BTreeMap::new(),
        }
    }
}

impl BadAddresses {
    pub fn insert(&mut self, address: Pubkey, slot: Slot) {
        if self.map.len() >= MAX_ADDRESSES && !self.map.contains_key(&address) {
            // Evict the oldest (lowest slot)
            if let Some((oldest_key, _)) = self
                .map
                .iter()
                .min_by_key(|(_, s)| *s)
                .map(|(k, _)| (k.clone(), ()))
            {
                self.map.remove(&oldest_key);
            }
        }
        self.map.insert(address, slot);
    }

    pub fn contains(&self, address: &Pubkey) -> bool {
        self.map.contains_key(address)
    }

    pub fn get_slot(&self, address: &Pubkey) -> Option<Slot> {
        self.map.get(address).cloned()
    }

    pub fn len(&self) -> usize {
        self.map.len()
    }

    pub fn size_hint() -> usize {
        // Estimate: 32 (Pubkey) + 8 (Slot) per entry
        MAX_ADDRESSES * (32 + 8) + 8
    }
}