@@ -70,54 +70,124 @@ def test_connect_basic(node_factory):
7070 assert l1 .rpc .listpeers (l2id )['peers' ][0 ]['num_channels' ] == 2
7171
7272
73+ # def test_connect_with_alt_addr(node_factory, bitcoind):
74+ # # Step 1: Set up two nodes and open a channel
75+ # l1 = node_factory.get_node(may_reconnect=True)
76+ # l2 = node_factory.get_node(may_reconnect=True)
77+ # l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
78+
79+ # # Should reconnect.
80+ # wait_for(lambda: only_one(l1.rpc.listpeers(l2.info['id'])['peers'])['connected'])
81+ # wait_for(lambda: only_one(l2.rpc.listpeers(l1.info['id'])['peers'])['connected'])
82+ # l1.fundchannel(l2, 10**6)
83+
84+ # # Step 2: Modify configuration to use alt-addr
85+ # alt_addr = '127.0.0.21'
86+ # l2.stop()
87+ # l2.daemon.opts['alt-addr'] = f'{alt_addr}:{l2.port}'
88+ # # l2.daemon.opts['bind-addr'] = f'{alt_addr}:{l2.port}'
89+ # l2.start()
90+
91+ # # Verify the alt-addr setting
92+ # try:
93+ # binding = l2.rpc.getinfo()['binding']
94+ # assert len(binding) > 0, "No binding found for l2"
95+ # assert any(bind['address'] == alt_addr for bind in binding), f"Expected alt-addr {alt_addr}, found {binding}"
96+ # except Exception as e:
97+ # logging.error(f"alt-addr not set correctly: {e}")
98+ # raise
99+
100+ # # Step 3: Reconnect using alt-addr
101+ # try:
102+ # # Disconnect the peers first if they are connected
103+ # if any(peer['connected'] for peer in l1.rpc.listpeers()['peers']):
104+ # l1.rpc.disconnect(l2.info['id'], force=True)
105+
106+ # l1.rpc.connect(l2.info['id'], alt_addr, l2.port)
107+ # except Exception as e:
108+ # logging.error(f"Error reconnecting nodes: {e}")
109+ # raise
110+
111+ # # Verify the connection is using the new alt-addr
112+ # try:
113+ # connected_peer = l1.rpc.getpeer(l2.info['id'])
114+ # assert connected_peer['connected'], "Peers not connected"
115+ # assert connected_peer['netaddr'][0].startswith(alt_addr), f"Connection not using alt-addr: {connected_peer['netaddr'][0]}"
116+ # except Exception as e:
117+ # logging.error(f"Error verifying connection using alt-addr: {e}")
118+ # raise
119+
120+ # # Ensure the channel is still active
121+ # try:
122+ # channel_state = l1.rpc.listpeerchannels(l2.info['id'])['channels'][0]['state']
123+ # assert channel_state == 'CHANNELD_NORMAL', f"Channel state is {channel_state}, expected CHANNELD_NORMAL"
124+ # except Exception as e:
125+ # logging.error(f"Channel state not normal: {e}")
126+ # raise
127+
128+
73129def test_connect_with_alt_addr (node_factory , bitcoind ):
74- # Step 1: Set up two nodes and open a channel
130+ logging .basicConfig (level = logging .INFO )
131+
132+ # Set up nodes
133+ logging .info ("Setting up two nodes with the capability to reconnect" )
75134 l1 = node_factory .get_node (may_reconnect = True )
76135 l2 = node_factory .get_node (may_reconnect = True )
136+
137+ # Initial connection
138+ logging .info (f"Initial connection from l1 to l2 using localhost and port { l2 .port } " )
77139 l1 .rpc .connect (l2 .info ['id' ], 'localhost' , l2 .port )
78140
79- # Should reconnect.
141+ # Checking initial connection state
142+ logging .info ("Waiting for both nodes to report they are connected..." )
80143 wait_for (lambda : only_one (l1 .rpc .listpeers (l2 .info ['id' ])['peers' ])['connected' ])
81144 wait_for (lambda : only_one (l2 .rpc .listpeers (l1 .info ['id' ])['peers' ])['connected' ])
145+
146+ # Fund channel and log the event
147+ logging .info (f"Funding channel between l1 and l2 with 10**6 satoshis" )
82148 l1 .fundchannel (l2 , 10 ** 6 )
83149
84- # Step 2: Modify configuration to use alt-addr
85- alt_addr = '127.0.0.21'
150+ # Modifying node configuration to use an alternative address
151+ alt_addr = '127.21.21.21'
152+ logging .info (f"Stopping l2 to change its address to { alt_addr } :{ l2 .port } " )
86153 l2 .stop ()
87- l2 .daemon .opts ['alt-addr' ] = f'{ alt_addr } :{ l2 .port } '
88154 # l2.daemon.opts['bind-addr'] = f'{alt_addr}:{l2.port}'
155+ l2 .daemon .opts ['alt-addr' ] = f'{ alt_addr } :{ l2 .port } '
89156 l2 .start ()
90-
91- # Verify the alt-addr setting
157+ logging .info ("Restarted l2 with alternative address" )
158+
159+ # Verification of the alternative address setting
160+ logging .info ("Verifying alternative address setting on l2" )
92161 try :
93162 binding = l2 .rpc .getinfo ()['binding' ]
94163 assert len (binding ) > 0 , "No binding found for l2"
95164 assert any (bind ['address' ] == alt_addr for bind in binding ), f"Expected alt-addr { alt_addr } , found { binding } "
96165 except Exception as e :
97- logging .error (f"alt-addr not set correctly: { e } " )
166+ logging .error (f"Alternative address not set correctly: { e } " )
98167 raise
99-
100- # Step 3: Reconnect using alt-addr
168+
169+ # Reconnection using the alternative address
170+ logging .info ("Attempting to reconnect using the new alternative address" )
101171 try :
102- # Disconnect the peers first if they are connected
103172 if any (peer ['connected' ] for peer in l1 .rpc .listpeers ()['peers' ]):
104173 l1 .rpc .disconnect (l2 .info ['id' ], force = True )
105-
106174 l1 .rpc .connect (l2 .info ['id' ], alt_addr , l2 .port )
107175 except Exception as e :
108- logging .error (f"Error reconnecting nodes: { e } " )
176+ logging .error (f"Error reconnecting nodes using alternative address : { e } " )
109177 raise
110178
111- # Verify the connection is using the new alt-addr
179+ # Verify the connection using the new address
180+ logging .info ("Verifying new connection details" )
112181 try :
113182 connected_peer = l1 .rpc .getpeer (l2 .info ['id' ])
114183 assert connected_peer ['connected' ], "Peers not connected"
115184 assert connected_peer ['netaddr' ][0 ].startswith (alt_addr ), f"Connection not using alt-addr: { connected_peer ['netaddr' ][0 ]} "
116185 except Exception as e :
117186 logging .error (f"Error verifying connection using alt-addr: { e } " )
118187 raise
119-
120- # Ensure the channel is still active
188+
189+ # Verify the channel state
190+ logging .info ("Checking the channel state after reconnection" )
121191 try :
122192 channel_state = l1 .rpc .listpeerchannels (l2 .info ['id' ])['channels' ][0 ]['state' ]
123193 assert channel_state == 'CHANNELD_NORMAL' , f"Channel state is { channel_state } , expected CHANNELD_NORMAL"
0 commit comments