11
11
PROTOCOL_VERSION ,
12
12
_mk_identify_protobuf ,
13
13
_multiaddr_to_bytes ,
14
+ _remote_address_to_multiaddr ,
14
15
)
15
16
from libp2p .identity .identify .pb .identify_pb2 import (
16
17
Identify ,
19
20
host_pair_factory ,
20
21
)
21
22
22
- logger = logging .getLogger ("libp2p.identity.identify-test" )
23
+
24
+ def clean_multiaddr (addr : Multiaddr ) -> Multiaddr :
25
+ """
26
+ Clean a multiaddr by removing the '/p2p/' part if it exists.
27
+
28
+ Args:
29
+ addr: The multiaddr to clean
30
+
31
+ Returns:
32
+ The cleaned multiaddr
33
+ """
34
+ return Multiaddr .join (
35
+ * (
36
+ addr .split ()[:- 1 ]
37
+ if str (addr .split ()[- 1 ]).startswith ("/p2p/" )
38
+ else addr .split ()
39
+ )
40
+ )
41
+
42
+
43
+ # logger = logging.getLogger("libp2p.identity.identify-test")
44
+ logger = logging .getLogger (__name__ )
23
45
24
46
25
47
@pytest .mark .trio
@@ -29,8 +51,8 @@ async def test_identify_protocol(security_protocol):
29
51
host_b ,
30
52
):
31
53
# Here, host_b is the requester and host_a is the responder.
32
- # observed_addr represent host_b’ s address as observed by host_a
33
- # (i.e., the address from which host_b’ s request was received).
54
+ # observed_addr represent host_b' s address as observed by host_a
55
+ # (i.e., the address from which host_b' s request was received).
34
56
stream = await host_b .new_stream (host_a .get_id (), (ID ,))
35
57
response = await stream .read ()
36
58
await stream .close ()
@@ -59,13 +81,7 @@ async def test_identify_protocol(security_protocol):
59
81
# TODO: use decapsulateCode(protocols('p2p').code)
60
82
# when the Multiaddr class will implement it
61
83
host_b_addr = host_b .get_addrs ()[0 ]
62
- cleaned_addr = Multiaddr .join (
63
- * (
64
- host_b_addr .split ()[:- 1 ]
65
- if str (host_b_addr .split ()[- 1 ]).startswith ("/p2p/" )
66
- else host_b_addr .split ()
67
- )
68
- )
84
+ cleaned_addr = clean_multiaddr (host_b_addr )
69
85
70
86
logger .debug ("observed_addr: %s" , Multiaddr (identify_response .observed_addr ))
71
87
logger .debug ("host_b.get_addrs()[0]: %s" , host_b .get_addrs ()[0 ])
@@ -77,3 +93,66 @@ async def test_identify_protocol(security_protocol):
77
93
78
94
# sanity check
79
95
assert identify_response == _mk_identify_protobuf (host_a , cleaned_addr )
96
+
97
+
98
+ @pytest .mark .trio
99
+ async def test_complete_remote_address_delegation_chain (security_protocol ):
100
+ async with host_pair_factory (security_protocol = security_protocol ) as (
101
+ host_a ,
102
+ host_b ,
103
+ ):
104
+ logger .debug (
105
+ "test_complete_remote_address_delegation_chain security_protocol: %s" ,
106
+ security_protocol ,
107
+ )
108
+
109
+ # Create a stream from host_a to host_b
110
+ stream = await host_a .new_stream (host_b .get_id (), (ID ,))
111
+
112
+ # Get references to all components in the delegation chain
113
+ mplex_stream = stream .muxed_stream
114
+ swarm_conn = host_a .get_network ().connections [host_b .get_id ()]
115
+ mplex = swarm_conn .muxed_conn
116
+ secure_session = mplex .secured_conn
117
+ raw_connection = secure_session .conn
118
+ trio_tcp_stream = raw_connection .stream
119
+
120
+ # Get remote addresses at each layer
121
+ stream_addr = stream .get_remote_address ()
122
+ muxed_stream_addr = stream .muxed_stream .get_remote_address ()
123
+ mplex_addr = mplex_stream .muxed_conn .get_remote_address ()
124
+ secure_session_addr = mplex .secured_conn .get_remote_address ()
125
+ raw_connection_addr = secure_session .conn .get_remote_address ()
126
+ trio_tcp_stream_addr = raw_connection .stream .get_remote_address ()
127
+ socket_addr = trio_tcp_stream .stream .socket .getpeername ()
128
+
129
+ # Log all addresses
130
+ logger .debug ("NetStream address: %s" , stream_addr )
131
+ logger .debug ("MplexStream address: %s" , muxed_stream_addr )
132
+ logger .debug ("Mplex address: %s" , mplex_addr )
133
+ logger .debug ("SecureSession address: %s" , secure_session_addr )
134
+ logger .debug ("RawConnection address: %s" , raw_connection_addr )
135
+ logger .debug ("TrioTCPStream address: %s" , trio_tcp_stream_addr )
136
+ logger .debug ("Socket address: %s" , socket_addr )
137
+
138
+ # Verify complete delegation chain
139
+ assert (
140
+ stream_addr
141
+ == muxed_stream_addr
142
+ == mplex_addr
143
+ == secure_session_addr
144
+ == raw_connection_addr
145
+ == trio_tcp_stream_addr
146
+ == socket_addr
147
+ )
148
+
149
+ # Convert to multiaddr and verify it matches host_b's cleaned address
150
+ remote_address_multiaddr = _remote_address_to_multiaddr (stream_addr )
151
+ host_b_addr = clean_multiaddr (host_b .get_addrs ()[0 ])
152
+
153
+ logger .debug ("Remote address multiaddr: %s" , remote_address_multiaddr )
154
+ logger .debug ("Host B cleaned address: %s" , host_b_addr )
155
+
156
+ assert remote_address_multiaddr == host_b_addr
157
+
158
+ await stream .close ()
0 commit comments