Skip to content

Commit ed56faa

Browse files
committed
Create the peer msg for peer_alt_address
Signed-off-by: Max Rantil <[email protected]>
1 parent 81726c0 commit ed56faa

File tree

11 files changed

+132
-17
lines changed

11 files changed

+132
-17
lines changed

channeld/channeld.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,30 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
535535
check_mutual_splice_locked(peer);
536536
}
537537

538+
static void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
539+
{
540+
struct pubkey peer_id;
541+
u8 *alt_addr;
542+
// u32 *timestamp = NULL;
543+
fprintf(stderr, "handle_peer_alt_addr: called with message\n");
544+
if (!fromwire_peer_alt_address(peer, (u8 *)msg, &peer_id, &alt_addr/* , timestamp */)) {
545+
master_badmsg(WIRE_PEER_ALT_ADDRESS, (void *)msg);
546+
}
547+
fprintf(stderr, "Alternative address for peer %s received.\n", type_to_string(tmpctx, struct pubkey, &peer_id));
548+
549+
// struct peer *peer = peer_htable_get(daemon->peers, &peer_id);
550+
// if (!peer)
551+
// return; // Peer not found
552+
553+
// updating peer information in a database:
554+
// update_peer_address(peer, &alt_addr);
555+
556+
// Optionally, trigger actions that use the new address immediately,
557+
// such as attempting a new connection:
558+
// try_connect_to_peer(peer, &alt_addr);
559+
560+
}
561+
538562
static void handle_peer_channel_ready(struct peer *peer, const u8 *msg)
539563
{
540564
struct channel_id chanid;
@@ -4254,6 +4278,9 @@ static void peer_in(struct peer *peer, const u8 *msg)
42544278
case WIRE_SPLICE_LOCKED:
42554279
handle_peer_splice_locked(peer, msg);
42564280
return;
4281+
case WIRE_PEER_ALT_ADDRESS:
4282+
handle_peer_alt_addr(peer, msg);
4283+
return;
42574284
case WIRE_INIT:
42584285
case WIRE_OPEN_CHANNEL:
42594286
case WIRE_ACCEPT_CHANNEL:

common/interactivetx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static u8 *read_next_msg(const tal_t *ctx,
186186
case WIRE_SPLICE:
187187
case WIRE_SPLICE_ACK:
188188
case WIRE_SPLICE_LOCKED:
189+
case WIRE_PEER_ALT_ADDRESS:
189190
*error = tal_fmt(ctx,
190191
"Received invalid message from peer: %d", t);
191192
return NULL;
@@ -736,6 +737,7 @@ char *process_interactivetx_updates(const tal_t *ctx,
736737
case WIRE_SPLICE_ACK:
737738
case WIRE_STFU:
738739
case WIRE_SPLICE_LOCKED:
740+
case WIRE_PEER_ALT_ADDRESS:
739741
return tal_fmt(ctx, "Unexpected wire message %s",
740742
tal_hex(ctx, msg));
741743
}

connectd/connectd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
20862086
goto out;
20872087

20882088
case WIRE_CONNECTD_CONNECT_TO_PEER:
2089+
// case WIRE_PEER_ALT_ADDRESS:
20892090
connect_to_peer(daemon, msg);
20902091
goto out;
20912092

connectd/gossip_rcvd_filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static bool is_msg_gossip_broadcast(const u8 *cursor)
9595
case WIRE_SPLICE:
9696
case WIRE_SPLICE_ACK:
9797
case WIRE_SPLICE_LOCKED:
98+
case WIRE_PEER_ALT_ADDRESS:
9899
break;
99100
}
100101
return false;

connectd/gossip_store.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static bool public_msg_type(enum peer_wire type)
9898
case WIRE_SPLICE:
9999
case WIRE_SPLICE_ACK:
100100
case WIRE_SPLICE_LOCKED:
101+
case WIRE_PEER_ALT_ADDRESS:
101102
return false;
102103
case WIRE_CHANNEL_ANNOUNCEMENT:
103104
case WIRE_NODE_ANNOUNCEMENT:

connectd/multiplex.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ static bool is_urgent(enum peer_wire type)
394394
case WIRE_SPLICE:
395395
case WIRE_SPLICE_ACK:
396396
case WIRE_SPLICE_LOCKED:
397+
case WIRE_PEER_ALT_ADDRESS:
397398
return false;
398399

399400
/* These are time-sensitive, and so send without delay. */

gossipd/gossipd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ static void handle_recv_gossip(struct daemon *daemon, const u8 *outermsg)
277277
case WIRE_SPLICE:
278278
case WIRE_SPLICE_ACK:
279279
case WIRE_SPLICE_LOCKED:
280+
case WIRE_PEER_ALT_ADDRESS:
280281
break;
281282
}
282283

openingd/dualopend.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ static u8 *opening_negotiate_msg(const tal_t *ctx, struct state *state)
17021702
case WIRE_SPLICE:
17031703
case WIRE_SPLICE_ACK:
17041704
case WIRE_SPLICE_LOCKED:
1705+
case WIRE_PEER_ALT_ADDRESS:
17051706
break;
17061707
}
17071708

@@ -2079,6 +2080,7 @@ static bool run_tx_interactive(struct state *state,
20792080
case WIRE_SPLICE:
20802081
case WIRE_SPLICE_ACK:
20812082
case WIRE_SPLICE_LOCKED:
2083+
case WIRE_PEER_ALT_ADDRESS:
20822084
open_abort(state, "Unexpected wire message %s",
20832085
tal_hex(tmpctx, msg));
20842086
return false;
@@ -4284,6 +4286,7 @@ static u8 *handle_peer_in(struct state *state)
42844286
case WIRE_SPLICE:
42854287
case WIRE_SPLICE_ACK:
42864288
case WIRE_SPLICE_LOCKED:
4289+
case WIRE_PEER_ALT_ADDRESS:
42874290
break;
42884291
}
42894292

tests/test_connection.py

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
73129
def 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"

wire/peer_wire.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static bool unknown_type(enum peer_wire t)
5353
case WIRE_SPLICE:
5454
case WIRE_SPLICE_ACK:
5555
case WIRE_SPLICE_LOCKED:
56+
case WIRE_PEER_ALT_ADDRESS:
5657
return false;
5758
}
5859
return true;
@@ -110,6 +111,7 @@ bool is_msg_for_gossipd(const u8 *cursor)
110111
case WIRE_SPLICE:
111112
case WIRE_SPLICE_ACK:
112113
case WIRE_SPLICE_LOCKED:
114+
case WIRE_PEER_ALT_ADDRESS:
113115
break;
114116
}
115117
return false;
@@ -162,6 +164,7 @@ bool extract_channel_id(const u8 *in_pkt, struct channel_id *channel_id)
162164
case WIRE_ONION_MESSAGE:
163165
case WIRE_PEER_STORAGE:
164166
case WIRE_YOUR_PEER_STORAGE:
167+
case WIRE_PEER_ALT_ADDRESS:
165168
return false;
166169

167170
/* Special cases: */

0 commit comments

Comments
 (0)