1414from libp2p .pubsub .gossipsub import GossipSub
1515from libp2p .pubsub .pubsub import Pubsub
1616from libp2p .records .validator import Validator
17- from libp2p .utils .paths import get_script_dir , join_paths
1817
1918GOSSIPSUB_PROTOCOL_ID = TProtocol ("/meshsub/1.0.0" )
2019COMMANDS = """
2423- join <topic> - Subscribe to a topic
2524- leave <topic> - Unsubscribe to a topic
2625- publish <topic> <message> - Publish a message
27- - put <key> <value> - Execute PUT_VALUE in DHT
26+ - put <key> <value> - Execute PUT_VALUE in DHT
2827- get <key> - Execute GET_VALUE in DHT
2928- advertize <content-id> - Execute ADD_PROVIDER in DHT
3029- get_provider <content-id> - Execute GET_PROVIDERS in DHT
@@ -44,9 +43,7 @@ def select(self, key: str, values: list[bytes]) -> int:
4443
4544
4645class Node :
47- def __init__ (
48- self , listen_addrs : list [multiaddr .Multiaddr ], dht_role : str
49- ):
46+ def __init__ (self , listen_addrs : list [multiaddr .Multiaddr ], dht_role : str ):
5047 # Create a libp2p-host
5148 self .host = new_host (listen_addrs = listen_addrs , enable_metrics = True )
5249
@@ -89,6 +86,11 @@ async def receive_loop(self, subsription):
8986 while not self .termination_event .is_set ():
9087 try :
9188 message = await subsription .get ()
89+
90+ from_peer_id = ID (message .from_id ).to_base58 ()
91+ if from_peer_id == self .host .get_id ().pretty ():
92+ continue
93+
9294 print (f"From: { ID (message .from_id ).to_base58 ()} " )
9395 print (f"Received: { message .data .decode ('utf-8' )} " )
9496 except Exception :
@@ -131,35 +133,35 @@ async def command_executor(self, nursery):
131133 if cmd == "publish" and len (parts ) > 2 :
132134 await self .pubsub .publish (parts [1 ], parts [2 ].encode ())
133135 print (f"Published: { parts [2 ]} " )
134-
136+
135137 if cmd == "put" and len (parts ) > 2 :
136138 key = parts [1 ]
137139 value = parts [2 ].encode ()
138-
140+
139141 await self .dht .put_value (key , value )
140142 print (f"Stored value: { value .decode ()} with key: { key } " )
141-
143+
142144 if cmd == "get" and len (parts ) > 1 :
143145 key = parts [1 ]
144-
146+
145147 retrieved_value = await self .dht .get_value (key )
146148 if retrieved_value :
147149 print (f"Retrieved value: { retrieved_value .decode ()} " )
148150 else :
149151 print ("Failed to retrieve" )
150-
152+
151153 if cmd == "advertize" and len (parts ) > 1 :
152154 content_id = parts [1 ]
153-
155+
154156 success = await self .dht .provide (content_id )
155157 if success :
156158 print (f"Advertised as provider for content: { content_id } " )
157159 else :
158160 print ("Failed to advertise as provider" )
159-
161+
160162 if cmd == "get_provider" and len (parts ) > 1 :
161163 content_id = parts [1 ]
162-
164+
163165 providers = await self .dht .find_providers (content_id )
164166 if providers :
165167 print (
@@ -168,7 +170,7 @@ async def command_executor(self, nursery):
168170 )
169171 else :
170172 print ("No providers found" )
171-
173+
172174 if cmd == "local" :
173175 maddr = self .host .get_addrs ()[0 ]
174176 print (maddr )
0 commit comments