Skip to content

Commit 9ed44f5

Browse files
authored
Merge pull request #753 from lla-dane/feat/certified-addrbook
WIP: Certified-Address-Book interface for Peer-Store
2 parents 8352d19 + 8786f06 commit 9ed44f5

33 files changed

+1590
-71
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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
identify_handler_for,
1515
parse_identify_response,
1616
)
17+
from libp2p.identity.identify.pb.identify_pb2 import Identify
18+
from libp2p.peer.envelope import debug_dump_envelope, unmarshal_envelope
1719
from libp2p.peer.peerinfo import (
1820
info_from_p2p_addr,
1921
)
@@ -32,10 +34,11 @@ def decode_multiaddrs(raw_addrs):
3234
return decoded_addrs
3335

3436

35-
def print_identify_response(identify_response):
37+
def print_identify_response(identify_response: Identify):
3638
"""Pretty-print Identify response."""
3739
public_key_b64 = base64.b64encode(identify_response.public_key).decode("utf-8")
3840
listen_addrs = decode_multiaddrs(identify_response.listen_addrs)
41+
signed_peer_record = unmarshal_envelope(identify_response.signedPeerRecord)
3942
try:
4043
observed_addr_decoded = decode_multiaddrs([identify_response.observed_addr])
4144
except Exception:
@@ -51,6 +54,8 @@ def print_identify_response(identify_response):
5154
f" Agent Version: {identify_response.agent_version}"
5255
)
5356

57+
debug_dump_envelope(signed_peer_record)
58+
5459

5560
async def run(port: int, destination: str, use_varint_format: bool = True) -> None:
5661
localhost_ip = "0.0.0.0"
@@ -61,12 +66,19 @@ async def run(port: int, destination: str, use_varint_format: bool = True) -> No
6166
host_a = new_host()
6267

6368
# Set up identify handler with specified format
69+
# Set use_varint_format = False, if want to checkout the Signed-PeerRecord
6470
identify_handler = identify_handler_for(
6571
host_a, use_varint_format=use_varint_format
6672
)
6773
host_a.set_stream_handler(IDENTIFY_PROTOCOL_ID, identify_handler)
6874

69-
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+
7082
# Get the actual address and replace 0.0.0.0 with 127.0.0.1 for client
7183
# connections
7284
server_addr = str(host_a.get_addrs()[0])
@@ -125,7 +137,13 @@ async def custom_identify_handler(stream):
125137
listen_addr = multiaddr.Multiaddr(f"/ip4/{localhost_ip}/tcp/{port}")
126138
host_b = new_host()
127139

128-
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+
129147
# Connect to the first host
130148
print(f"dialer (host_b) listening on {host_b.get_addrs()[0]}")
131149
maddr = multiaddr.Multiaddr(destination)
@@ -238,9 +256,9 @@ def main() -> None:
238256

239257
args = parser.parse_args()
240258

241-
# Determine format: raw format if --raw-format is specified, otherwise
242-
# length-prefixed
243-
use_varint_format = not args.raw_format
259+
# Determine format: use varint (length-prefixed) if --raw-format is specified,
260+
# otherwise use raw protobuf format (old format)
261+
use_varint_format = args.raw_format
244262

245263
try:
246264
if args.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/ping/ping.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ async def run(port: int, destination: str) -> None:
5959
host = new_host(listen_addrs=[listen_addr])
6060

6161
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
62+
# Start the peer-store cleanup task
63+
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
64+
6265
if not destination:
6366
host.set_stream_handler(PING_PROTOCOL_ID, handle_ping)
6467

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...")

0 commit comments

Comments
 (0)