@@ -131,6 +131,87 @@ def test_connect_with_alt_addr(node_factory, bitcoind):
131131 raise
132132
133133
134+ def test_connect_with_alt_addr_rpc (node_factory , bitcoind ):
135+ logging .basicConfig (level = logging .INFO )
136+
137+ # Set up nodes
138+ logging .info ("Setting up two nodes with the capability to reconnect" )
139+ l1 = node_factory .get_node (may_reconnect = True )
140+ l2 = node_factory .get_node (may_reconnect = True )
141+
142+ # Initial connection
143+ logging .info (f"Initial connection from l1 to l2 using localhost and port { l2 .port } " )
144+ l1 .rpc .connect (l2 .info ['id' ], 'localhost' , l2 .port )
145+
146+ # Checking initial connection state
147+ logging .info ("Waiting for both nodes to report they are connected..." )
148+ wait_for (lambda : only_one (l1 .rpc .listpeers (l2 .info ['id' ])['peers' ])['connected' ])
149+ wait_for (lambda : only_one (l2 .rpc .listpeers (l1 .info ['id' ])['peers' ])['connected' ])
150+
151+ # Fund channel and log the event
152+ logging .info (f"Funding channel between l1 and l2 with 10**6 satoshis" )
153+ l1 .fundchannel (l2 , 10 ** 6 )
154+
155+ # Send the alt-addr from l2 to l1
156+ alt_addr = '127.21.21.21'
157+ addr_without_bind = '127.22.22.22'
158+ l2 .rpc .alt_addr (l1 .info ['id' ], f'{ alt_addr } :{ l2 .port } ' )
159+
160+ # Modifying node configuration to use an alternative address
161+ logging .info (f"Stopping l2 to change its address to { alt_addr } :{ l2 .port } " )
162+ l2 .stop ()
163+ l2 .daemon .opts ['alt-bind-addr' ] = f'{ alt_addr } :{ l2 .port } '
164+ l2 .start ()
165+ logging .info ("Restarted l2 with bind-addr" )
166+
167+ # Verification of the alternative address setting
168+ logging .info ("Verifying bind address setting on l2" )
169+ try :
170+ binding = l2 .rpc .getinfo ()['binding' ]
171+ assert len (binding ) > 0 , "No binding found for l2"
172+ assert any (bind ['address' ] == alt_addr for bind in binding ), f"Expected bind-addr { alt_addr } , found { binding } "
173+
174+ except Exception as e :
175+ logging .error (f"Bind address not set correctly: { e } " )
176+ raise
177+
178+ # Reconnection using the alternative address
179+ logging .info ("Attempting to reconnect using the new alternative address" )
180+ try :
181+ if any (peer ['connected' ] for peer in l1 .rpc .listpeers ()['peers' ]):
182+ l1 .rpc .disconnect (l2 .info ['id' ], force = True )
183+ l1 .rpc .connect (l2 .info ['id' ], alt_addr , l2 .port )
184+ except Exception as e :
185+ logging .error (f"Error reconnecting nodes using alternative address: { e } " )
186+ raise
187+
188+ # Verify the connection using the new address
189+ logging .info ("Verifying new connection details" )
190+ try :
191+ connected_peer = l1 .rpc .getpeer (l2 .info ['id' ])
192+ assert connected_peer ['connected' ], "Peers not connected"
193+ assert connected_peer ['netaddr' ][0 ].startswith (alt_addr ), f"Connection not using alt-addr: { connected_peer ['netaddr' ][0 ]} "
194+ except Exception as e :
195+ logging .error (f"Error verifying connection using alt-addr: { e } " )
196+ raise
197+
198+ # Disconnect and attempt to connect using the addr_without_bind
199+ l2 .rpc .alt_addr (l1 .info ['id' ], f'{ addr_without_bind } :{ l2 .port } ' )
200+ l2 .stop ()
201+ l2 .start ()
202+ try :
203+ l1 .rpc .connect (l2 .info ['id' ], addr_without_bind , l2 .port )
204+ logging .error ("Connection should not be successful using addr_without_bind" )
205+ assert False , "Connection should fail using addr_without_bind"
206+ except Exception as e :
207+ logging .info (f"Expected failure connecting using addr_without_bind: { e } " )
208+
209+ # Final verification
210+ logging .info ("Verifying no connection using addr_without_bind" )
211+ connected_peer = l1 .rpc .listpeers (l2 .info ['id' ])['peers' ]
212+ assert len (connected_peer ) == 0 or not connected_peer [0 ]['connected' ], "Peer should not be connected using addr_without_bind"
213+
214+
134215def test_remote_addr (node_factory , bitcoind ):
135216 """Check address discovery (BOLT1 #917) init remote_addr works as designed:
136217
0 commit comments