Skip to content

Commit ec64e77

Browse files
committed
protocol: pkt_reconnect becomes pkt_init, and always send.
Make this always the first packet after auth. That means there's no reliance on whether a node remembers an aborted connection. It also gives us a place to put version bits. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent e5994ad commit ec64e77

File tree

6 files changed

+156
-154
lines changed

6 files changed

+156
-154
lines changed

daemon/packets.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ Pkt *pkt_err(struct peer *peer, const char *msg, ...)
236236
return make_pkt(peer, PKT__PKT_ERROR, e);
237237
}
238238

239-
Pkt *pkt_reconnect(struct peer *peer, u64 ack)
239+
Pkt *pkt_init(struct peer *peer, u64 ack)
240240
{
241-
Reconnect *r = tal(peer, Reconnect);
242-
reconnect__init(r);
243-
r->ack = ack;
244-
return make_pkt(peer, PKT__PKT_RECONNECT, r);
241+
Init *i = tal(peer, Init);
242+
init__init(i);
243+
i->ack = ack;
244+
return make_pkt(peer, PKT__PKT_INIT, i);
245245
}
246246

247247
void queue_pkt_err(struct peer *peer, Pkt *err)

daemon/packets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void queue_pkt_close_shutdown(struct peer *peer);
2626
void queue_pkt_close_signature(struct peer *peer);
2727

2828
Pkt *pkt_err(struct peer *peer, const char *msg, ...);
29-
Pkt *pkt_reconnect(struct peer *peer, u64 ack);
29+
Pkt *pkt_init(struct peer *peer, u64 ack);
3030
void queue_pkt_err(struct peer *peer, Pkt *err);
3131
Pkt *pkt_err_unexpected(struct peer *peer, const Pkt *pkt);
3232

daemon/peer.c

Lines changed: 84 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,86 @@ static void peer_has_connected(struct peer *peer)
20912091
}
20922092
}
20932093

2094+
static struct io_plan *init_pkt_in(struct io_conn *conn, struct peer *peer)
2095+
{
2096+
if (peer->inpkt->pkt_case != PKT__PKT_INIT) {
2097+
peer_received_unexpected_pkt(peer, peer->inpkt, __func__);
2098+
return pkt_out(conn, peer);
2099+
}
2100+
2101+
/* They might have missed the error, tell them before hanging up */
2102+
if (state_is_error(peer->state)) {
2103+
queue_pkt_err(peer, pkt_err(peer, "In error state %s",
2104+
state_name(peer->state)));
2105+
return pkt_out(conn, peer);
2106+
}
2107+
2108+
/* Send any packets they missed. */
2109+
retransmit_pkts(peer, peer->inpkt->init->ack);
2110+
2111+
/* We let the conversation go this far in case they missed the
2112+
* close packets. But now we can close if we're done. */
2113+
if (!state_can_io(peer->state)) {
2114+
log_debug(peer->log, "State %s, closing immediately",
2115+
state_name(peer->state));
2116+
return pkt_out(conn, peer);
2117+
}
2118+
2119+
/* Back into normal mode. */
2120+
peer_has_connected(peer);
2121+
return io_duplex(conn,
2122+
peer_read_packet(conn, peer, pkt_in),
2123+
pkt_out(conn, peer));
2124+
}
2125+
2126+
static struct io_plan *read_init_pkt(struct io_conn *conn,
2127+
struct peer *peer)
2128+
{
2129+
return peer_read_packet(conn, peer, init_pkt_in);
2130+
}
2131+
2132+
static u64 peer_commitsigs_received(struct peer *peer)
2133+
{
2134+
return peer->their_commitsigs;
2135+
}
2136+
2137+
static u64 peer_revocations_received(struct peer *peer)
2138+
{
2139+
/* How many preimages we've received. */
2140+
return -peer->their_preimages.min_index;
2141+
}
2142+
2143+
static struct io_plan *peer_send_init(struct io_conn *conn, struct peer *peer)
2144+
{
2145+
u64 sigs, revokes, shutdown, closing;
2146+
2147+
sigs = peer_commitsigs_received(peer);
2148+
revokes = peer_revocations_received(peer);
2149+
shutdown = peer->closing.their_script ? 1 : 0;
2150+
closing = peer->closing.sigs_in;
2151+
log_debug(peer->log,
2152+
"Init with ack %"PRIu64" sigs + %"PRIu64" revokes"
2153+
" + %"PRIu64" shutdown + %"PRIu64" closing",
2154+
sigs, revokes, shutdown, closing);
2155+
2156+
/* BOLT #2:
2157+
*
2158+
* A node reconnecting after receiving or sending an `open_channel`
2159+
* message SHOULD send a `reconnect` message on the new connection
2160+
* immediately after it has validated the `authenticate` message. */
2161+
2162+
/* BOLT #2:
2163+
*
2164+
* A node MUST set the `ack` field in the `reconnect` message to the
2165+
* the sum of previously-processed messages of types
2166+
* `open_commit_sig`, `update_commit`, `update_revocation`,
2167+
* `close_shutdown` and `close_signature`. */
2168+
return peer_write_packet(conn, peer,
2169+
pkt_init(peer, sigs + revokes
2170+
+ shutdown + closing),
2171+
read_init_pkt);
2172+
}
2173+
20942174
/* Crypto is on, we are live. */
20952175
static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
20962176
{
@@ -2107,10 +2187,7 @@ static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
21072187

21082188
state_event(peer, peer->local.offer_anchor, NULL);
21092189

2110-
peer_has_connected(peer);
2111-
return io_duplex(conn,
2112-
peer_read_packet(conn, peer, pkt_in),
2113-
pkt_out(conn, peer));
2190+
return peer_send_init(conn,peer);
21142191
}
21152192

21162193
static void destroy_peer(struct peer *peer)
@@ -2484,17 +2561,6 @@ static bool peer_first_connected(struct peer *peer,
24842561
return true;
24852562
}
24862563

2487-
static u64 peer_commitsigs_received(struct peer *peer)
2488-
{
2489-
return peer->their_commitsigs;
2490-
}
2491-
2492-
static u64 peer_revocations_received(struct peer *peer)
2493-
{
2494-
/* How many preimages we've received. */
2495-
return -peer->their_preimages.min_index;
2496-
}
2497-
24982564
static void htlc_destroy(struct htlc *htlc)
24992565
{
25002566
if (!htlc_map_del(&htlc->peer->htlcs, htlc))
@@ -2540,86 +2606,22 @@ struct htlc *peer_new_htlc(struct peer *peer,
25402606
return h;
25412607
}
25422608

2543-
2544-
static struct io_plan *reconnect_pkt_in(struct io_conn *conn, struct peer *peer)
2545-
{
2546-
if (peer->inpkt->pkt_case != PKT__PKT_RECONNECT) {
2547-
peer_received_unexpected_pkt(peer, peer->inpkt, __func__);
2548-
return pkt_out(conn, peer);
2549-
}
2550-
2551-
/* We need to eliminate queue now. */
2552-
clear_output_queue(peer);
2553-
2554-
/* They might have missed the error, tell them before hanging up */
2555-
if (state_is_error(peer->state)) {
2556-
queue_pkt_err(peer, pkt_err(peer, "In error state %s",
2557-
state_name(peer->state)));
2558-
return pkt_out(conn, peer);
2559-
}
2560-
2561-
/* Send any packets they missed. */
2562-
retransmit_pkts(peer, peer->inpkt->reconnect->ack);
2563-
2564-
/* We let the conversation go this far in case they missed the
2565-
* close packets. But now we can close if we're done. */
2566-
if (!state_can_io(peer->state)) {
2567-
log_debug(peer->log, "State %s, closing immediately",
2568-
state_name(peer->state));
2569-
return pkt_out(conn, peer);
2570-
}
2571-
2572-
/* Back into normal mode. */
2573-
peer_has_connected(peer);
2574-
return io_duplex(conn,
2575-
peer_read_packet(conn, peer, pkt_in),
2576-
pkt_out(conn, peer));
2577-
}
2578-
2579-
static struct io_plan *read_reconnect_pkt(struct io_conn *conn,
2580-
struct peer *peer)
2581-
{
2582-
return peer_read_packet(conn, peer, reconnect_pkt_in);
2583-
}
2584-
25852609
static struct io_plan *crypto_on_reconnect(struct io_conn *conn,
25862610
struct lightningd_state *dstate,
25872611
struct io_data *iod,
25882612
const struct pubkey *id,
25892613
struct peer *peer,
25902614
bool we_connected)
25912615
{
2592-
u64 sigs, revokes, shutdown, closing;
2593-
25942616
/* Setup peer->conn and peer->io_data */
25952617
if (!peer_reconnected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
25962618
iod, id, we_connected))
25972619
return io_close(conn);
25982620

2599-
sigs = peer_commitsigs_received(peer);
2600-
revokes = peer_revocations_received(peer);
2601-
shutdown = peer->closing.their_script ? 1 : 0;
2602-
closing = peer->closing.sigs_in;
2603-
log_debug(peer->log,
2604-
"Reconnecting with ack %"PRIu64" sigs + %"PRIu64" revokes"
2605-
" + %"PRIu64" shutdown + %"PRIu64" closing",
2606-
sigs, revokes, shutdown, closing);
2607-
/* BOLT #2:
2608-
*
2609-
* A node reconnecting after receiving or sending an `open_channel`
2610-
* message SHOULD send a `reconnect` message on the new connection
2611-
* immediately after it has validated the `authenticate` message. */
2621+
/* We need to eliminate queue now. */
2622+
clear_output_queue(peer);
26122623

2613-
/* BOLT #2:
2614-
*
2615-
* A node MUST set the `ack` field in the `reconnect` message to the
2616-
* the sum of previously-processed messages of types
2617-
* `open_commit_sig`, `update_commit`, `update_revocation`,
2618-
* `close_shutdown` and `close_signature`. */
2619-
return peer_write_packet(conn, peer,
2620-
pkt_reconnect(peer, sigs + revokes
2621-
+ shutdown + closing),
2622-
read_reconnect_pkt);
2624+
return peer_send_init(conn, peer);
26232625
}
26242626

26252627
static struct io_plan *crypto_on_reconnect_in(struct io_conn *conn,

lightning.pb-c.c

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -308,47 +308,47 @@ void authenticate__free_unpacked
308308
assert(message->base.descriptor == &authenticate__descriptor);
309309
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
310310
}
311-
void reconnect__init
312-
(Reconnect *message)
311+
void init__init
312+
(Init *message)
313313
{
314-
static Reconnect init_value = RECONNECT__INIT;
314+
static Init init_value = INIT__INIT;
315315
*message = init_value;
316316
}
317-
size_t reconnect__get_packed_size
318-
(const Reconnect *message)
317+
size_t init__get_packed_size
318+
(const Init *message)
319319
{
320-
assert(message->base.descriptor == &reconnect__descriptor);
320+
assert(message->base.descriptor == &init__descriptor);
321321
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
322322
}
323-
size_t reconnect__pack
324-
(const Reconnect *message,
323+
size_t init__pack
324+
(const Init *message,
325325
uint8_t *out)
326326
{
327-
assert(message->base.descriptor == &reconnect__descriptor);
327+
assert(message->base.descriptor == &init__descriptor);
328328
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
329329
}
330-
size_t reconnect__pack_to_buffer
331-
(const Reconnect *message,
330+
size_t init__pack_to_buffer
331+
(const Init *message,
332332
ProtobufCBuffer *buffer)
333333
{
334-
assert(message->base.descriptor == &reconnect__descriptor);
334+
assert(message->base.descriptor == &init__descriptor);
335335
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
336336
}
337-
Reconnect *
338-
reconnect__unpack
337+
Init *
338+
init__unpack
339339
(ProtobufCAllocator *allocator,
340340
size_t len,
341341
const uint8_t *data)
342342
{
343-
return (Reconnect *)
344-
protobuf_c_message_unpack (&reconnect__descriptor,
343+
return (Init *)
344+
protobuf_c_message_unpack (&init__descriptor,
345345
allocator, len, data);
346346
}
347-
void reconnect__free_unpacked
348-
(Reconnect *message,
347+
void init__free_unpacked
348+
(Init *message,
349349
ProtobufCAllocator *allocator)
350350
{
351-
assert(message->base.descriptor == &reconnect__descriptor);
351+
assert(message->base.descriptor == &init__descriptor);
352352
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
353353
}
354354
void open_channel__init
@@ -1644,42 +1644,42 @@ const ProtobufCMessageDescriptor authenticate__descriptor =
16441644
(ProtobufCMessageInit) authenticate__init,
16451645
NULL,NULL,NULL /* reserved[123] */
16461646
};
1647-
static const ProtobufCFieldDescriptor reconnect__field_descriptors[1] =
1647+
static const ProtobufCFieldDescriptor init__field_descriptors[1] =
16481648
{
16491649
{
16501650
"ack",
16511651
1,
16521652
PROTOBUF_C_LABEL_REQUIRED,
16531653
PROTOBUF_C_TYPE_UINT64,
16541654
0, /* quantifier_offset */
1655-
offsetof(Reconnect, ack),
1655+
offsetof(Init, ack),
16561656
NULL,
16571657
NULL,
16581658
0, /* flags */
16591659
0,NULL,NULL /* reserved1,reserved2, etc */
16601660
},
16611661
};
1662-
static const unsigned reconnect__field_indices_by_name[] = {
1662+
static const unsigned init__field_indices_by_name[] = {
16631663
0, /* field[0] = ack */
16641664
};
1665-
static const ProtobufCIntRange reconnect__number_ranges[1 + 1] =
1665+
static const ProtobufCIntRange init__number_ranges[1 + 1] =
16661666
{
16671667
{ 1, 0 },
16681668
{ 0, 1 }
16691669
};
1670-
const ProtobufCMessageDescriptor reconnect__descriptor =
1670+
const ProtobufCMessageDescriptor init__descriptor =
16711671
{
16721672
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
1673-
"reconnect",
1674-
"Reconnect",
1675-
"Reconnect",
1673+
"init",
1674+
"Init",
1675+
"Init",
16761676
"",
1677-
sizeof(Reconnect),
1677+
sizeof(Init),
16781678
1,
1679-
reconnect__field_descriptors,
1680-
reconnect__field_indices_by_name,
1681-
1, reconnect__number_ranges,
1682-
(ProtobufCMessageInit) reconnect__init,
1679+
init__field_descriptors,
1680+
init__field_indices_by_name,
1681+
1, init__number_ranges,
1682+
(ProtobufCMessageInit) init__init,
16831683
NULL,NULL,NULL /* reserved[123] */
16841684
};
16851685
static const ProtobufCEnumValue open_channel__anchor_offer__enum_values_by_number[2] =
@@ -2840,13 +2840,13 @@ static const ProtobufCFieldDescriptor pkt__field_descriptors[15] =
28402840
0,NULL,NULL /* reserved1,reserved2, etc */
28412841
},
28422842
{
2843-
"reconnect",
2843+
"init",
28442844
51,
28452845
PROTOBUF_C_LABEL_OPTIONAL,
28462846
PROTOBUF_C_TYPE_MESSAGE,
28472847
offsetof(Pkt, pkt_case),
2848-
offsetof(Pkt, reconnect),
2849-
&reconnect__descriptor,
2848+
offsetof(Pkt, init),
2849+
&init__descriptor,
28502850
NULL,
28512851
0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */
28522852
0,NULL,NULL /* reserved1,reserved2, etc */
@@ -2857,11 +2857,11 @@ static const unsigned pkt__field_indices_by_name[] = {
28572857
10, /* field[10] = close_shutdown */
28582858
11, /* field[11] = close_signature */
28592859
12, /* field[12] = error */
2860+
14, /* field[14] = init */
28602861
6, /* field[6] = open */
28612862
7, /* field[7] = open_anchor */
28622863
8, /* field[8] = open_commit_sig */
28632864
9, /* field[9] = open_complete */
2864-
14, /* field[14] = reconnect */
28652865
0, /* field[0] = update_add_htlc */
28662866
4, /* field[4] = update_commit */
28672867
2, /* field[2] = update_fail_htlc */

0 commit comments

Comments
 (0)