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
1719from 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
5560async 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 :
0 commit comments