mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
doc: adding identify push protocol examples and doc
This commit is contained in:
@ -7,6 +7,11 @@ from libp2p.custom_types import (
|
||||
from libp2p.host.exceptions import (
|
||||
StreamFailure,
|
||||
)
|
||||
from libp2p.identity.identify_push import (
|
||||
ID_PUSH,
|
||||
identify_push_handler_for,
|
||||
push_identify_to_peer,
|
||||
)
|
||||
from libp2p.peer.peerinfo import (
|
||||
info_from_p2p_addr,
|
||||
)
|
||||
@ -246,6 +251,32 @@ async def pubsub_demo(host_a, host_b):
|
||||
assert b_received.is_set()
|
||||
|
||||
|
||||
async def identify_push_demo(host_a, host_b):
|
||||
# Set up the identify/push handlers
|
||||
host_b.set_stream_handler(ID_PUSH, identify_push_handler_for(host_b))
|
||||
|
||||
# Push identify information from host_a to host_b
|
||||
success = await push_identify_to_peer(host_a, host_b.get_id())
|
||||
assert success is True
|
||||
|
||||
# Check that host_b's peerstore has been updated with host_a's information
|
||||
peer_id = host_a.get_id()
|
||||
peerstore = host_b.get_peerstore()
|
||||
|
||||
# Check that the peer is in the peerstore
|
||||
assert peer_id in peerstore.peer_ids()
|
||||
|
||||
# Check that the protocols were updated
|
||||
host_a_protocols = set(host_a.get_mux().get_protocols())
|
||||
peerstore_protocols = set(peerstore.get_protocols(peer_id))
|
||||
assert all(protocol in peerstore_protocols for protocol in host_a_protocols)
|
||||
|
||||
# Check that the addresses were updated
|
||||
host_a_addrs = set(host_a.get_addrs())
|
||||
peerstore_addrs = set(peerstore.addrs(peer_id))
|
||||
assert all(addr in peerstore_addrs for addr in host_a_addrs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test",
|
||||
[
|
||||
@ -257,6 +288,7 @@ async def pubsub_demo(host_a, host_b):
|
||||
echo_demo,
|
||||
ping_demo,
|
||||
pubsub_demo,
|
||||
identify_push_demo,
|
||||
],
|
||||
)
|
||||
@pytest.mark.trio
|
||||
|
||||
Reference in New Issue
Block a user