doc: adding identify push protocol examples and doc

This commit is contained in:
acul71
2025-04-19 03:19:48 +02:00
committed by Paul Robinson
parent cef73519d3
commit 66707364a7
8 changed files with 205 additions and 63 deletions

View File

@ -43,6 +43,8 @@ logger = logging.getLogger(__name__)
async def main() -> None:
print("\n==== Starting Identify-Push Example ====\n")
# Create key pairs for the two hosts
key_pair_1 = create_new_key_pair()
key_pair_2 = create_new_key_pair()
@ -70,27 +72,49 @@ async def main() -> None:
async with host_1.run([listen_addr_1]), host_2.run([listen_addr_2]):
# Get the addresses of both hosts
addr_1 = host_1.get_addrs()[0]
logger.info("Host 1 listening on %s", addr_1)
logger.info(f"Host 1 listening on {addr_1}")
print(f"Host 1 listening on {addr_1}")
print(f"Peer ID: {host_1.get_id().pretty()}")
addr_2 = host_2.get_addrs()[0]
logger.info("Host 2 listening on %s", addr_2)
logger.info(f"Host 2 listening on {addr_2}")
print(f"Host 2 listening on {addr_2}")
print(f"Peer ID: {host_2.get_id().pretty()}")
print("\nConnecting Host 2 to Host 1...")
# Connect host_2 to host_1
peer_info = info_from_p2p_addr(addr_1)
await host_2.connect(peer_info)
logger.info("Host 2 connected to Host 1")
# Wait a bit for the connection to establish
await trio.sleep(1)
print("Host 2 successfully connected to Host 1")
# Push identify information from host_1 to host_2
logger.info("Host 1 pushing identify information to Host 2")
await push_identify_to_peer(host_1, host_2.get_id())
print("\nHost 1 pushing identify information to Host 2...")
# Wait a bit for the push to complete
await trio.sleep(1)
try:
# Call push_identify_to_peer which now returns a boolean
success = await push_identify_to_peer(host_1, host_2.get_id())
logger.info("Example completed successfully")
if success:
logger.info("Identify push completed successfully")
print("Identify push completed successfully!")
logger.info("Example completed successfully")
print("\nExample completed successfully!")
else:
logger.warning("Identify push didn't complete successfully")
print("\nWarning: Identify push didn't complete successfully")
logger.warning("Example completed with warnings")
print("Example completed with warnings")
except Exception as e:
logger.error(f"Error during identify push: {str(e)}")
print(f"\nError during identify push: {str(e)}")
logger.error("Example completed with errors")
print("Example completed with errors")
if __name__ == "__main__":

View File

@ -31,17 +31,15 @@ from libp2p import (
from libp2p.crypto.secp256k1 import (
create_new_key_pair,
)
from libp2p.custom_types import (
TProtocol,
)
from libp2p.identity.identify import (
identify_handler_for,
)
from libp2p.identity.identify import ID as ID_IDENTIFY
from libp2p.identity.identify_push import (
ID_PUSH,
identify_push_handler_for,
push_identify_to_peer,
)
from libp2p.identity.identify_push import ID_PUSH as ID_IDENTIFY_PUSH
from libp2p.peer.peerinfo import (
info_from_p2p_addr,
)
@ -53,6 +51,9 @@ logging.basicConfig(
)
logger = logging.getLogger("libp2p.identity.identify-push-example")
# Default port configuration
DEFAULT_PORT = 8888
async def run_listener(port: int) -> None:
"""Run a host in listener mode."""
@ -65,22 +66,23 @@ async def run_listener(port: int) -> None:
host = new_host(key_pair=key_pair)
# Set up the identify and identify/push handlers
host.set_stream_handler(TProtocol("/ipfs/id/1.0.0"), identify_handler_for(host))
host.set_stream_handler(ID_PUSH, identify_push_handler_for(host))
host.set_stream_handler(ID_IDENTIFY, identify_handler_for(host))
host.set_stream_handler(ID_IDENTIFY_PUSH, identify_push_handler_for(host))
# Start listening
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
async with host.run([listen_addr]):
addr = host.get_addrs()[0]
logger.info("Listener host ready")
logger.info("Listening on: %s", addr)
logger.info("Peer ID: %s", host.get_id().pretty())
# Print user-friendly information
logger.info("Listener host ready!")
print("Listener host ready!")
logger.info(f"Listening on: {addr}")
print(f"Listening on: {addr}")
logger.info(f"Peer ID: {host.get_id().pretty()}")
print(f"Peer ID: {host.get_id().pretty()}")
print("\nRun dialer with command:")
print(f"python identify_push_listener_dialer.py -d {addr}")
print("\nWaiting for incoming connections... (Ctrl+C to exit)")
@ -100,49 +102,60 @@ async def run_dialer(port: int, destination: str) -> None:
host = new_host(key_pair=key_pair)
# Set up the identify and identify/push handlers
host.set_stream_handler(TProtocol("/ipfs/id/1.0.0"), identify_handler_for(host))
host.set_stream_handler(ID_PUSH, identify_push_handler_for(host))
host.set_stream_handler(ID_IDENTIFY, identify_handler_for(host))
host.set_stream_handler(ID_IDENTIFY_PUSH, identify_push_handler_for(host))
# Start listening on a different port
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}")
async with host.run([listen_addr]):
logger.info("Dialer host ready")
logger.info("Listening on: %s", host.get_addrs()[0])
logger.info("Dialer host ready!")
print("Dialer host ready!")
logger.info(f"Listening on: {host.get_addrs()[0]}")
print(f"Listening on: {host.get_addrs()[0]}")
# Parse the destination multiaddress and connect to the listener
maddr = multiaddr.Multiaddr(destination)
peer_info = info_from_p2p_addr(maddr)
logger.info("Connecting to peer: %s", peer_info.peer_id)
logger.info(f"Connecting to peer: {peer_info.peer_id}")
print(f"\nConnecting to peer: {peer_info.peer_id}")
try:
await host.connect(peer_info)
logger.info("Successfully connected to listener")
logger.info("Successfully connected to listener!")
print("Successfully connected to listener!")
# Wait briefly for the connection to establish
await trio.sleep(1)
# Push identify information to the listener
logger.info("Pushing identify information to listener")
logger.info("Pushing identify information to listener...")
print("\nPushing identify information to listener...")
await push_identify_to_peer(host, peer_info.peer_id)
logger.info("Identify push completed. Waiting a moment...")
print("Identify push completed successfully!")
try:
# Call push_identify_to_peer which returns a boolean
success = await push_identify_to_peer(host, peer_info.peer_id)
# Keep the connection open for a bit to observe what happens
print("Waiting a moment to keep connection open...")
await trio.sleep(5)
if success:
logger.info("Identify push completed successfully!")
print("Identify push completed successfully!")
logger.info("Example completed successfully!")
print("\nExample completed successfully!")
else:
logger.warning("Identify push didn't complete successfully.")
print("\nWarning: Identify push didn't complete successfully.")
logger.warning("Example completed with warnings.")
print("Example completed with warnings.")
except Exception as e:
logger.error(f"Error during identify push: {str(e)}")
print(f"\nError during identify push: {str(e)}")
logger.error("Example completed with errors.")
print("Example completed with errors.")
# Continue execution despite the push error
logger.info("Example completed successfully")
print("\nExample completed successfully!")
except Exception as e:
logger.error("Error during dialer operation: %s", str(e))
logger.error(f"Error during dialer operation: {str(e)}")
print(f"\nError during dialer operation: {str(e)}")
raise
@ -156,7 +169,8 @@ def main() -> None:
"""
example = (
"/ip4/127.0.0.1/tcp/8888/p2p/QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
f"/ip4/127.0.0.1/tcp/{DEFAULT_PORT}/p2p/"
"QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
)
parser = argparse.ArgumentParser(description=description)
@ -164,7 +178,10 @@ def main() -> None:
"-p",
"--port",
type=int,
help="port to listen on (default: 8888 for listener, 8889 for dialer)",
help=(
f"port to listen on (default: {DEFAULT_PORT} for listener, "
f"{DEFAULT_PORT + 1} for dialer)"
),
)
parser.add_argument(
"-d",
@ -176,12 +193,12 @@ def main() -> None:
try:
if args.destination:
# Run in dialer mode with default port 8889 if not specified
port = args.port if args.port is not None else 8889
# Run in dialer mode with default port DEFAULT_PORT + 1 if not specified
port = args.port if args.port is not None else DEFAULT_PORT + 1
trio.run(run_dialer, port, args.destination)
else:
# Run in listener mode with default port 8888 if not specified
port = args.port if args.port is not None else 8888
# Run in listener mode with default port DEFAULT_PORT if not specified
port = args.port if args.port is not None else DEFAULT_PORT
trio.run(run_listener, port)
except KeyboardInterrupt:
print("\nInterrupted by user")