Skip to content

Commit 8af34e4

Browse files
committed
feat: add start and stop observing topics with topic observation state function completions
1 parent f978b38 commit 8af34e4

2 files changed

Lines changed: 27 additions & 49 deletions

File tree

libp2p/pubsub/extensions.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -475,38 +475,19 @@ def remove_peer(self, peer_id: ID) -> None:
475475
if not self._observing[topic]:
476476
del self._observing[topic]
477477

478-
# ------------------------------------------------------------------
479-
# TODO for contributor:
480-
# Implement the following methods to complete the outbound observer path.
481-
# ------------------------------------------------------------------
482-
483478
def get_observing_topics(self) -> set[str]:
484479
"""
485480
Return the set of topics this node is currently observing (outbound).
486481
487-
Implementation hint:
488-
Return ``set(self._observing.keys())``.
489-
490482
:return: set of topic strings we sent OBSERVE for.
491483
"""
492-
# TODO: Return the set of topics from self._observing.
493-
raise NotImplementedError(
494-
"get_observing_topics() is left as an easy task for contributors. "
495-
"Hint: return set(self._observing.keys())"
496-
)
484+
return set(self._observing.keys())
497485

498486
def get_subscriber_peers_for_topic(self, topic: str) -> set[ID]:
499487
"""
500488
Return the set of subscriber peers we sent OBSERVE to for *topic*.
501489
502-
Implementation hint:
503-
Return a copy of ``self._observing.get(topic, set())``.
504-
505490
:param topic: the topic to query.
506491
:return: set of subscriber peer IDs we are observing through.
507492
"""
508-
# TODO: Return a copy of self._observing.get(topic, set()).
509-
raise NotImplementedError(
510-
"get_subscriber_peers_for_topic() is left as an easy task for "
511-
"contributors. Hint: return set(self._observing.get(topic, set()))"
512-
)
493+
return set(self._observing.get(topic, set()))

libp2p/pubsub/gossipsub.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,44 +1683,41 @@ async def start_observing_topic(self, topic: str) -> None:
16831683
Internally it picks suitable subscriber peers and calls
16841684
:meth:`emit_observe` for each of them.
16851685
1686-
Implementation hints:
1687-
1. Check ``self.pubsub`` is not None.
1688-
2. Get the peers subscribed to *topic* from
1689-
``self.pubsub.peer_topics.get(topic, set())``.
1690-
3. For each peer: only send OBSERVE if
1691-
``self.supports_v13_features(peer)`` is True AND
1692-
``self.extensions_state.both_support_topic_observation(peer)``
1693-
is True.
1694-
4. Await ``self.emit_observe(topic, peer)`` for each qualifying peer.
1695-
16961686
:param topic: The topic to start observing.
16971687
"""
1698-
# TODO (contributor): implement start_observing_topic.
1699-
# See implementation hints in the docstring above.
1700-
raise NotImplementedError(
1701-
"start_observing_topic() is left as an easy task for contributors. "
1702-
"See the docstring for step-by-step implementation hints."
1703-
)
1688+
if self.pubsub is None:
1689+
raise NoPubsubAttached
1690+
1691+
peers_subscribed = self.pubsub.peer_topics.get(topic, set())
1692+
for peer in peers_subscribed:
1693+
if self.supports_v13_features(
1694+
peer
1695+
) and self.extensions_state.both_support_topic_observation(peer):
1696+
await self.emit_observe(topic, peer)
1697+
logger.debug(
1698+
"Started observing topic '%s' via peer %s.",
1699+
topic,
1700+
peer,
1701+
)
17041702

17051703
async def stop_observing_topic(self, topic: str) -> None:
17061704
"""
17071705
Stop observing *topic* by sending UNOBSERVE to all peers we previously
17081706
sent OBSERVE to for *topic*.
17091707
1710-
Implementation hints:
1711-
1. Get the set of peers we sent OBSERVE to via
1712-
``self.topic_observation.get_subscriber_peers_for_topic(topic)``.
1713-
(Note: this calls the TODO method in TopicObservationState.)
1714-
2. Await ``self.emit_unobserve(topic, peer)`` for each of them.
1715-
17161708
:param topic: The topic to stop observing.
17171709
"""
1718-
# TODO (contributor): implement stop_observing_topic.
1719-
# See implementation hints in the docstring above.
1720-
raise NotImplementedError(
1721-
"stop_observing_topic() is left as an easy task for contributors. "
1722-
"See the docstring for step-by-step implementation hints."
1723-
)
1710+
if self.pubsub is None:
1711+
raise NoPubsubAttached
1712+
1713+
subscriber_peers = self.topic_observation.get_subscriber_peers_for_topic(topic)
1714+
for peer in subscriber_peers:
1715+
await self.emit_unobserve(topic, peer)
1716+
logger.debug(
1717+
"Stopped observing topic '%s' via peer %s.",
1718+
topic,
1719+
peer,
1720+
)
17241721

17251722
def _track_peer_ip(self, peer_id: ID) -> None:
17261723
"""

0 commit comments

Comments
 (0)