libp2p_peer_store/store.rs
1use std::{fmt::Debug, task::Context};
2
3use libp2p_core::{Multiaddr, PeerId};
4use libp2p_swarm::FromSwarm;
5
6/// A store that contains all observed addresses of peers.
7pub trait Store {
8 /// Event generated by the store and emitted to [`Swarm`](libp2p_swarm::Swarm).
9 /// [`Behaviour`](crate::Behaviour) cannot handle this event.
10 type FromStore: Debug + Send;
11
12 /// How this store handles events from [`Swarm`](libp2p_swarm::Swarm).
13 fn on_swarm_event(&mut self, event: &FromSwarm);
14
15 /// Get all stored addresses of the peer.
16 fn addresses_of_peer(&self, peer: &PeerId) -> Option<impl Iterator<Item = &Multiaddr>>;
17
18 /// Polls for things that the store should do.
19 /// The task should be waked up to emit events to [`Behaviour`](crate::Behaviour) and
20 /// [`Swarm`](libp2p_swarm::Swarm).
21 fn poll(&mut self, cx: &mut Context<'_>) -> Option<Event<Self::FromStore>>;
22}
23
24/// Event that will be handled by [`Behaviour`](crate::Behaviour).
25pub enum Event<T> {
26 /// An address record has been updated.
27 RecordUpdated(PeerId),
28 /// Event generated by the store.
29 /// [`Behaviour`](crate::Behaviour) can only forward the event to swarm.
30 Store(T),
31}