Skip to content

Commit

Permalink
allow Store to report to Swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
drHuangMHT committed Jan 8, 2025
1 parent cb294c6 commit a9dc9aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
18 changes: 12 additions & 6 deletions misc/peer-store/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ use libp2p_swarm::{dummy, FromSwarm, NetworkBehaviour};
use crate::store::{AddressSource, Store};

/// Events generated by [`Behaviour`] and emitted back to the [`libp2p_swarm::Swarm`].
pub enum Event {
pub enum Event<S: Store> {
/// The peer's record has been updated.
/// Manually updating a record will always emit this event
/// even if it provides no new information.
RecordUpdated { peer: PeerId },
RecordUpdated {
peer: PeerId,
},
Store(S::ToSwarm),
}

pub struct Behaviour<S> {
pub struct Behaviour<S: Store> {
store: S,
/// Peers that are currently connected.
connected_peers: HashSet<PeerId>,
/// Pending Events to be emitted back to the [`libp2p_swarm::Swarm`].
pending_events: VecDeque<Event>,
pending_events: VecDeque<Event<S>>,
}

impl<'a, S> Behaviour<S>
Expand Down Expand Up @@ -115,10 +118,11 @@ where
impl<S> NetworkBehaviour for Behaviour<S>
where
S: Store + 'static,
<S as Store>::ToSwarm: Send + Sync,
{
type ConnectionHandler = dummy::ConnectionHandler;

type ToSwarm = Event;
type ToSwarm = Event<S>;

fn handle_established_inbound_connection(
&mut self,
Expand Down Expand Up @@ -197,7 +201,9 @@ where
if let Some(ev) = self.pending_events.pop_front() {
return Poll::Ready(libp2p_swarm::ToSwarm::GenerateEvent(ev));
}
self.store.poll(cx);
if let Some(ev) = self.store.poll(cx) {
self.pending_events.push_back(Event::Store(ev));
};
Poll::Pending
}
}
2 changes: 1 addition & 1 deletion misc/peer-store/src/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl MemoryStore {
}

impl Store for MemoryStore {
type Event = ();
type ToSwarm = ();

fn update_address(
&mut self,
Expand Down
7 changes: 5 additions & 2 deletions misc/peer-store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use libp2p_swarm::FromSwarm;
/// A store that
/// - contains all observed addresses of peers;
pub trait Store {
type Event;

/// Event generated by the store and emitted to [`Swarm`](libp2p_swarm::Swarm).
/// The behaviour cannot handle this event.
type ToSwarm;

/// Update an address record.
/// Returns `true` when the address is new.
Expand Down Expand Up @@ -38,7 +41,7 @@ pub trait Store {
fn addresses_of_peer(&self, peer: &PeerId) -> Option<impl Iterator<Item = &Multiaddr>>;

/// Trigger grabage collection for records.
fn poll(&mut self, cx: &mut Context<'_>) -> Option<Self::Event>;
fn poll(&mut self, cx: &mut Context<'_>) -> Option<Self::ToSwarm>;
}

pub enum Event {
Expand Down

0 comments on commit a9dc9aa

Please sign in to comment.