Skip to content

Commit 8c96c5a

Browse files
committed
Add the periodic peer-store cleanup in all the examples
1 parent 1644571 commit 8c96c5a

File tree

8 files changed

+51
-16
lines changed

8 files changed

+51
-16
lines changed

examples/chat/chat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ async def run(port: int, destination: str) -> None:
4343
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
4444
host = new_host()
4545
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
46+
# Start the peer-store cleanup task
47+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
48+
4649
if not destination: # its the server
4750

4851
async def stream_handler(stream: INetStream) -> None:

examples/echo/echo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
4545
secret = secrets.token_bytes(32)
4646

4747
host = new_host(key_pair=create_new_key_pair(secret))
48-
async with host.run(listen_addrs=[listen_addr]):
48+
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
49+
# Start the peer-store cleanup task
50+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
51+
4952
print(f"I am {host.get_id().to_string()}")
5053

5154
if not destination: # its the server

examples/identify/identify.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ async def run(port: int, destination: str, use_varint_format: bool = True) -> No
7272
)
7373
host_a.set_stream_handler(IDENTIFY_PROTOCOL_ID, identify_handler)
7474

75-
async with host_a.run(listen_addrs=[listen_addr]):
75+
async with (
76+
host_a.run(listen_addrs=[listen_addr]),
77+
trio.open_nursery() as nursery,
78+
):
79+
# Start the peer-store cleanup task
80+
nursery.start_soon(host_a.get_peerstore().start_cleanup_task, 60)
81+
7682
# Get the actual address and replace 0.0.0.0 with 127.0.0.1 for client
7783
# connections
7884
server_addr = str(host_a.get_addrs()[0])
@@ -131,7 +137,13 @@ async def custom_identify_handler(stream):
131137
listen_addr = multiaddr.Multiaddr(f"/ip4/{localhost_ip}/tcp/{port}")
132138
host_b = new_host()
133139

134-
async with host_b.run(listen_addrs=[listen_addr]):
140+
async with (
141+
host_b.run(listen_addrs=[listen_addr]),
142+
trio.open_nursery() as nursery,
143+
):
144+
# Start the peer-store cleanup task
145+
nursery.start_soon(host_b.get_peerstore().start_cleanup_task, 60)
146+
135147
# Connect to the first host
136148
print(f"dialer (host_b) listening on {host_b.get_addrs()[0]}")
137149
maddr = multiaddr.Multiaddr(destination)

examples/identify_push/identify_push_demo.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ async def main() -> None:
211211
listen_addr_1 = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0")
212212
listen_addr_2 = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0")
213213

214-
async with host_1.run([listen_addr_1]), host_2.run([listen_addr_2]):
214+
async with (
215+
host_1.run([listen_addr_1]),
216+
host_2.run([listen_addr_2]),
217+
trio.open_nursery() as nursery,
218+
):
219+
# Start the peer-store cleanup task
220+
nursery.start_soon(host_1.get_peerstore().start_cleanup_task, 60)
221+
nursery.start_soon(host_2.get_peerstore().start_cleanup_task, 60)
222+
215223
# Get the addresses of both hosts
216224
addr_1 = host_1.get_addrs()[0]
217225
addr_2 = host_2.get_addrs()[0]

examples/kademlia/kademlia.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ async def run_node(
151151
host = new_host(key_pair=key_pair)
152152
listen_addr = Multiaddr(f"/ip4/127.0.0.1/tcp/{port}")
153153

154-
async with host.run(listen_addrs=[listen_addr]):
154+
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
155+
# Start the peer-store cleanup task
156+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
157+
155158
peer_id = host.get_id().pretty()
156159
addr_str = f"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}"
157160
await connect_to_bootstrap_nodes(host, bootstrap_nodes)

examples/mDNS/mDNS.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ async def run(port: int) -> None:
4646

4747
logger.info("Starting peer Discovery")
4848
host = new_host(key_pair=key_pair, enable_mDNS=True)
49-
async with host.run(listen_addrs=[listen_addr]):
49+
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
50+
# Start the peer-store cleanup task
51+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
52+
5053
await trio.sleep_forever()
5154

5255

examples/pubsub/pubsub.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ async def run(topic: str, destination: str | None, port: int | None) -> None:
144144
pubsub = Pubsub(host, gossipsub)
145145
termination_event = trio.Event() # Event to signal termination
146146
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
147+
# Start the peer-store cleanup task
148+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
149+
147150
logger.info(f"Node started with peer ID: {host.get_id()}")
148151
logger.info(f"Listening on: {listen_addr}")
149152
logger.info("Initializing PubSub and GossipSub...")

libp2p/identity/identify_push/identify_push.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,6 @@ async def _update_peerstore_from_identify(
141141
except Exception as e:
142142
logger.error("Error updating protocols for peer %s: %s", peer_id, e)
143143

144-
# Update observed address if present
145-
if identify_msg.HasField("observed_addr") and identify_msg.observed_addr:
146-
try:
147-
# Convert bytes to Multiaddr object
148-
observed_addr = Multiaddr(identify_msg.observed_addr)
149-
# Add the observed address to the peerstore
150-
# Use a default TTL of 2 hours (7200 seconds)
151-
peerstore.add_addr(peer_id, observed_addr, 7200)
152-
except Exception as e:
153-
logger.error("Error updating observed address for peer %s: %s", peer_id, e)
154144
if identify_msg.HasField("signedPeerRecord"):
155145
try:
156146
# Convert the signed-peer-record(Envelope) from prtobuf bytes
@@ -164,6 +154,16 @@ async def _update_peerstore_from_identify(
164154
logger.error(
165155
"Error updating the certified addr book for peer %s: %s", peer_id, e
166156
)
157+
# Update observed address if present
158+
if identify_msg.HasField("observed_addr") and identify_msg.observed_addr:
159+
try:
160+
# Convert bytes to Multiaddr object
161+
observed_addr = Multiaddr(identify_msg.observed_addr)
162+
# Add the observed address to the peerstore
163+
# Use a default TTL of 2 hours (7200 seconds)
164+
peerstore.add_addr(peer_id, observed_addr, 7200)
165+
except Exception as e:
166+
logger.error("Error updating observed address for peer %s: %s", peer_id, e)
167167

168168

169169
async def push_identify_to_peer(

0 commit comments

Comments
 (0)