@@ -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