Skip to main content

BootstrapCache

Struct BootstrapCache 

Source
pub struct BootstrapCache { /* private fields */ }
Expand description

Greedy bootstrap cache with quality-based peer selection.

This cache stores peer information with quality metrics and provides epsilon-greedy selection to balance exploitation (using known-good peers) with exploration (trying new peers to discover potentially better ones).

Implementations§

Source§

impl BootstrapCache

Source

pub async fn open(config: BootstrapCacheConfig) -> Result<Self>

Open or create a bootstrap cache.

Loads existing cache data from disk if available, otherwise starts fresh.

Source

pub fn subscribe(&self) -> Receiver<CacheEvent>

Subscribe to cache events

Source

pub async fn peer_count(&self) -> usize

Get the number of cached peers

Source

pub async fn get_peer(&self, peer_id: &PeerId) -> Option<CachedPeer>

Get a specific peer from the cache

Source

pub async fn select_peers(&self, count: usize) -> Vec<CachedPeer>

Select peers for bootstrap using epsilon-greedy strategy.

Returns up to count peers, balancing exploitation of known-good peers with exploration of untested peers based on the configured epsilon.

Source

pub async fn select_relay_peers(&self, count: usize) -> Vec<CachedPeer>

Select peers that support relay functionality.

Returns peers sorted by quality score, preferring observed relay capability.

Source

pub async fn select_coordinators(&self, count: usize) -> Vec<CachedPeer>

Select peers that support NAT coordination.

Returns peers sorted by quality score, preferring observed coordination capability.

Source

pub async fn select_relays_for_target( &self, count: usize, target: &SocketAddr, prefer_dual_stack: bool, ) -> Vec<CachedPeer>

Select relay peers that can reach a target IP version.

Returns relays sorted by quality that can bridge traffic to the target. Dual-stack relays are preferred as they can reach any target.

§Arguments
  • count - Maximum number of relays to return
  • target - The target address to reach
  • prefer_dual_stack - If true, prioritize dual-stack relays
Source

pub async fn select_dual_stack_relays(&self, count: usize) -> Vec<CachedPeer>

Select relay peers that support dual-stack (IPv4 + IPv6) bridging.

These peers are valuable for bridging between IPv4-only and IPv6-only networks.

Source

pub async fn upsert(&self, peer: CachedPeer)

Add or update a peer in the cache.

If the cache is at capacity, evicts the lowest quality peers.

Source

pub async fn add_seed(&self, peer_id: PeerId, addresses: Vec<SocketAddr>)

Add a seed peer (user-provided bootstrap node).

Source

pub async fn add_from_connection( &self, peer_id: PeerId, addresses: Vec<SocketAddr>, caps: Option<PeerCapabilities>, )

Add a peer discovered from an active connection.

Source

pub async fn record_outcome(&self, peer_id: &PeerId, outcome: ConnectionOutcome)

Record a connection attempt result.

Source

pub async fn record_success(&self, peer_id: &PeerId, rtt_ms: u32)

Record successful connection.

Source

pub async fn record_failure(&self, peer_id: &PeerId)

Record failed connection.

Source

pub async fn update_capabilities( &self, peer_id: &PeerId, caps: PeerCapabilities, )

Update peer capabilities.

Source

pub async fn get(&self, peer_id: &PeerId) -> Option<CachedPeer>

Get a specific peer.

Source

pub async fn update_token(&self, peer_id: PeerId, token: Vec<u8>)

Update the address validation token for a peer

Source

pub async fn get_all_tokens(&self) -> HashMap<PeerId, Vec<u8>>

Get all tokens from cached peers (for initializing TokenStore)

Source

pub async fn contains(&self, peer_id: &PeerId) -> bool

Check if peer exists in cache.

Source

pub async fn remove(&self, peer_id: &PeerId) -> Option<CachedPeer>

Remove a peer from cache.

Source

pub async fn save(&self) -> Result<()>

Save cache to disk.

Source

pub async fn cleanup_stale(&self) -> usize

Cleanup stale peers.

Removes peers that haven’t been seen within the stale threshold. Returns the number of peers removed.

Source

pub async fn recalculate_quality(&self)

Recalculate quality scores for all peers.

Source

pub async fn stats(&self) -> CacheStats

Get cache statistics.

Source

pub fn start_maintenance(self: Arc<Self>) -> JoinHandle<()>

Start background maintenance tasks.

Spawns a task that periodically:

  • Saves the cache to disk
  • Cleans up stale peers
  • Recalculates quality scores

Returns a handle that can be used to cancel the task.

Source

pub async fn all_peers(&self) -> Vec<CachedPeer>

Get all cached peers (for export/debug).

Source

pub fn config(&self) -> &BootstrapCacheConfig

Get the configuration.

Trait Implementations§

Source§

impl Debug for BootstrapCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more