Skip to content

Commit 07fa3ad

Browse files
committed
splice: Implement start_batch
Implement the sending of `start_batch` as per t-bast’s spec. We ignoring it when receving, as we don’t need it. Changelog-Added: support for `start_batch`
1 parent 9d160eb commit 07fa3ad

File tree

11 files changed

+121
-44
lines changed

11 files changed

+121
-44
lines changed

channeld/channeld.c

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,12 +1197,7 @@ static u8 *send_commit_part(const tal_t *ctx,
11971197
(int)splice_amnt, (int)remote_splice_amnt,
11981198
remote_index);
11991199

1200-
if (batch_size > 1) {
1201-
cs_tlv->splice_info = tal(cs_tlv, struct tlv_commitment_signed_tlvs_splice_info);
1202-
1203-
cs_tlv->splice_info->batch_size = batch_size;
1204-
cs_tlv->splice_info->funding_txid = funding->txid;
1205-
}
1200+
cs_tlv->splice_info = tal_dup(cs_tlv, struct bitcoin_txid, &funding->txid);
12061201
}
12071202

12081203
txs = channel_txs(tmpctx, funding, funding_sats, &htlc_map,
@@ -1274,6 +1269,52 @@ static s64 sats_diff(struct amount_sat a, struct amount_sat b)
12741269
return (s64)a.satoshis - (s64)b.satoshis; /* Raw: splicing numbers can wrap! */
12751270
}
12761271

1272+
static void send_message_batch(struct peer *peer, u8 **msgs)
1273+
{
1274+
size_t size;
1275+
u8 *batch_msg, *final_msg, *final_msg_ptr;
1276+
struct tlv_start_batch_tlvs *tlvs;
1277+
1278+
size = 0;
1279+
for(u32 i = 0; i < tal_count(msgs); i++)
1280+
size += tal_bytelen(msgs[i]);
1281+
1282+
/* Compiler mistakenly thinks batch_msg may be used uninitialized */
1283+
batch_msg = NULL;
1284+
1285+
/* Only append `start_batch` if batch is more than 1 */
1286+
if (tal_count(msgs) > 1) {
1287+
tlvs = tlv_start_batch_tlvs_new(NULL);
1288+
tlvs->batch_info = tal(tlvs, u16);
1289+
*tlvs->batch_info = WIRE_COMMITMENT_SIGNED;
1290+
batch_msg = towire_start_batch(NULL, &peer->channel_id,
1291+
tal_count(msgs), tlvs);
1292+
tal_steal(batch_msg, tlvs);
1293+
size += tal_bytelen(batch_msg);
1294+
}
1295+
1296+
/* Now we know the size of our `final_msg` so we allocate. */
1297+
final_msg = tal_arr(NULL, u8, size);
1298+
final_msg_ptr = final_msg;
1299+
1300+
/* Append the `start_batch` here, again if batch is more than 1 */
1301+
if (tal_count(msgs) > 1) {
1302+
memcpy(final_msg_ptr, batch_msg, tal_bytelen(batch_msg));
1303+
final_msg_ptr += tal_bytelen(batch_msg);
1304+
tal_free(batch_msg);
1305+
}
1306+
1307+
/* Now copy the bytes from all messages in `msgs` */
1308+
for(u32 i = 0; i < tal_count(msgs); i++) {
1309+
memcpy(final_msg_ptr, msgs[i], tal_bytelen(msgs[i]));
1310+
final_msg_ptr += tal_bytelen(msgs[i]);
1311+
}
1312+
1313+
assert(final_msg + size == final_msg_ptr);
1314+
peer_write(peer->pps, final_msg);
1315+
tal_free(final_msg);
1316+
}
1317+
12771318
static void send_commit(struct peer *peer)
12781319
{
12791320
const struct htlc **changed_htlcs;
@@ -1440,8 +1481,7 @@ static void send_commit(struct peer *peer)
14401481

14411482
peer->next_index[REMOTE]++;
14421483

1443-
for(u32 i = 0; i < tal_count(msgs); i++)
1444-
peer_write(peer->pps, take(msgs[i]));
1484+
send_message_batch(peer, msgs);
14451485

14461486
maybe_send_shutdown(peer);
14471487

@@ -1963,11 +2003,11 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
19632003
if (peer->splice_state->await_commitment_succcess
19642004
&& !tal_count(peer->splice_state->inflights) && cs_tlv && cs_tlv->splice_info) {
19652005
if (!bitcoin_txid_eq(&peer->channel->funding.txid,
1966-
&cs_tlv->splice_info->funding_txid)) {
2006+
cs_tlv->splice_info)) {
19672007
status_info("Ignoring stale commit_sig for channel_id"
19682008
" %s, as %s is locked in now.",
19692009
fmt_bitcoin_txid(tmpctx,
1970-
&cs_tlv->splice_info->funding_txid),
2010+
cs_tlv->splice_info),
19712011
fmt_bitcoin_txid(tmpctx,
19722012
&peer->channel->funding.txid));
19732013
return NULL;
@@ -2017,22 +2057,17 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20172057
outpoint = peer->splice_state->inflights[commit_index - 1]->outpoint;
20182058
funding_sats = peer->splice_state->inflights[commit_index - 1]->amnt;
20192059

2020-
if (cs_tlv && cs_tlv->splice_info
2021-
&& cs_tlv->splice_info->batch_size == 1)
2022-
peer_failed_err(peer->pps, &peer->channel_id,
2023-
"batch_size can never be 1");
2024-
20252060
status_debug("handle_peer_commit_sig for inflight outpoint %s",
20262061
fmt_bitcoin_txid(tmpctx, &outpoint.txid));
20272062

20282063
if (cs_tlv->splice_info
20292064
&& !bitcoin_txid_eq(&outpoint.txid,
2030-
&cs_tlv->splice_info->funding_txid))
2065+
cs_tlv->splice_info))
20312066
peer_failed_err(peer->pps, &peer->channel_id,
20322067
"Expected commit sig message for %s but"
20332068
" got %s",
20342069
fmt_bitcoin_txid(tmpctx, &outpoint.txid),
2035-
fmt_bitcoin_txid(tmpctx, &cs_tlv->splice_info->funding_txid));
2070+
fmt_bitcoin_txid(tmpctx, cs_tlv->splice_info));
20362071
}
20372072
else {
20382073
outpoint = peer->channel->funding;
@@ -2089,7 +2124,7 @@ static struct commitsig_info *handle_peer_commit_sig(struct peer *peer,
20892124
fmt_amount_sat(tmpctx, funding_sats),
20902125
cs_tlv && cs_tlv->splice_info
20912126
? fmt_bitcoin_txid(tmpctx,
2092-
&cs_tlv->splice_info->funding_txid)
2127+
cs_tlv->splice_info)
20932128
: "N/A",
20942129
peer->splice_state->await_commitment_succcess ? "yes"
20952130
: "no",
@@ -2253,7 +2288,7 @@ static int commit_index_from_msg(const u8 *msg, struct peer *peer)
22532288
if (!cs_tlv || !cs_tlv->splice_info)
22542289
return -1;
22552290

2256-
funding_txid = cs_tlv->splice_info->funding_txid;
2291+
funding_txid = *cs_tlv->splice_info;
22572292

22582293
if (bitcoin_txid_eq(&funding_txid, &peer->channel->funding.txid))
22592294
return 0;
@@ -2306,28 +2341,30 @@ static struct commitsig_info *handle_peer_commit_sig_batch(struct peer *peer,
23062341
s64 remote_splice_amnt,
23072342
u64 local_index,
23082343
const struct pubkey *local_per_commit,
2309-
bool allow_empty_commit)
2344+
bool allow_empty_commit,
2345+
u16 batch_size)
23102346
{
23112347
struct channel_id channel_id;
23122348
struct bitcoin_signature commit_sig;
23132349
secp256k1_ecdsa_signature *raw_sigs;
2314-
u16 batch_size;
23152350
const u8 **msg_batch;
23162351
enum peer_wire type;
23172352
struct tlv_commitment_signed_tlvs *cs_tlv
23182353
= tlv_commitment_signed_tlvs_new(tmpctx);
23192354
status_debug("fromwire_commitment_signed(%p) primary", msg);
2355+
check_tx_abort(peer, msg, NULL);
2356+
type = fromwire_peektype(msg);
2357+
if (type != WIRE_COMMITMENT_SIGNED)
2358+
peer_failed_err(peer->pps, &peer->channel_id,
2359+
"Expected splice related "
2360+
"WIRE_COMMITMENT_SIGNED but got %s.",
2361+
peer_wire_name(type));
23202362
if (!fromwire_commitment_signed(tmpctx, msg,
23212363
&channel_id, &commit_sig.s, &raw_sigs,
23222364
&cs_tlv))
23232365
peer_failed_warn(peer->pps, &peer->channel_id,
23242366
"Bad commit_sig %s", tal_hex(msg, msg));
23252367

2326-
/* Default batch_size is 1 */
2327-
batch_size = 1;
2328-
if (cs_tlv->splice_info && cs_tlv->splice_info->batch_size)
2329-
batch_size = cs_tlv->splice_info->batch_size;
2330-
23312368
msg_batch = tal_arr(tmpctx, const u8*, batch_size);
23322369
msg_batch[0] = msg;
23332370
status_debug("msg_batch[0]: %p", msg_batch[0]);
@@ -2363,14 +2400,6 @@ static struct commitsig_info *handle_peer_commit_sig_batch(struct peer *peer,
23632400
" splice_info",
23642401
tal_hex(sub_msg, sub_msg), i, batch_size);
23652402

2366-
if (!sub_cs_tlv->splice_info
2367-
|| sub_cs_tlv->splice_info->batch_size != batch_size)
2368-
peer_failed_err(peer->pps, &peer->channel_id,
2369-
"batch_size value mismatch in"
2370-
" commit_sig bundle, item [%"PRIu16
2371-
"/%"PRIu16"] %s", i, batch_size,
2372-
tal_hex(sub_msg, sub_msg));
2373-
23742403
msg_batch[i] = sub_msg;
23752404
status_debug("msg_batch[%d]: %p", (int)i, msg_batch[i]);
23762405
}
@@ -2385,6 +2414,24 @@ static struct commitsig_info *handle_peer_commit_sig_batch(struct peer *peer,
23852414
allow_empty_commit, msg_batch);
23862415
}
23872416

2417+
static void handle_peer_start_batch(struct peer *peer, const u8 *msg)
2418+
{
2419+
u16 batch_size;
2420+
struct channel_id channel_id;
2421+
if (!fromwire_start_batch(tmpctx, msg, &channel_id, &batch_size, NULL))
2422+
peer_failed_warn(peer->pps, &peer->channel_id,
2423+
"Bad start_batch %s", tal_hex(msg, msg));
2424+
2425+
handle_peer_commit_sig_batch(peer, peer_read(tmpctx, peer->pps), 0,
2426+
peer->channel->funding_pubkey[REMOTE],
2427+
NULL, 0, 0,
2428+
peer->next_index[LOCAL],
2429+
&peer->next_local_per_commit,
2430+
false,
2431+
batch_size);
2432+
}
2433+
2434+
23882435
/* Pops the penalty base for the given commitnum from our internal list. There
23892436
* may not be one, in which case we return NULL and leave the list
23902437
* unmodified. */
@@ -4850,13 +4897,17 @@ static void peer_in(struct peer *peer, const u8 *msg)
48504897
case WIRE_UPDATE_ADD_HTLC:
48514898
handle_peer_add_htlc(peer, msg);
48524899
return;
4900+
case WIRE_START_BATCH:
4901+
handle_peer_start_batch(peer, msg);
4902+
return;
48534903
case WIRE_COMMITMENT_SIGNED:
48544904
handle_peer_commit_sig_batch(peer, msg, 0,
48554905
peer->channel->funding_pubkey[REMOTE],
48564906
NULL, 0, 0,
48574907
peer->next_index[LOCAL],
48584908
&peer->next_local_per_commit,
4859-
false);
4909+
false,
4910+
1);
48604911
return;
48614912
case WIRE_UPDATE_FEE:
48624913
handle_peer_feechange(peer, msg);

common/gossmap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ const void *gossmap_stream_next(const tal_t *ctx,
17991799
case WIRE_UPDATE_FULFILL_HTLC:
18001800
case WIRE_UPDATE_FAIL_HTLC:
18011801
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
1802+
case WIRE_START_BATCH:
18021803
case WIRE_COMMITMENT_SIGNED:
18031804
case WIRE_REVOKE_AND_ACK:
18041805
case WIRE_UPDATE_FEE:

common/interactivetx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static u8 *read_next_msg(const tal_t *ctx,
163163
case WIRE_UPDATE_FULFILL_HTLC:
164164
case WIRE_UPDATE_FAIL_HTLC:
165165
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
166+
case WIRE_START_BATCH:
166167
case WIRE_COMMITMENT_SIGNED:
167168
case WIRE_REVOKE_AND_ACK:
168169
case WIRE_UPDATE_FEE:
@@ -771,6 +772,7 @@ char *process_interactivetx_updates(const tal_t *ctx,
771772
case WIRE_UPDATE_FULFILL_HTLC:
772773
case WIRE_UPDATE_FAIL_HTLC:
773774
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
775+
case WIRE_START_BATCH:
774776
case WIRE_COMMITMENT_SIGNED:
775777
case WIRE_REVOKE_AND_ACK:
776778
case WIRE_UPDATE_FEE:

connectd/gossip_rcvd_filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static bool is_msg_gossip_broadcast(const u8 *cursor)
7373
case WIRE_UPDATE_FULFILL_HTLC:
7474
case WIRE_UPDATE_FAIL_HTLC:
7575
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
76+
case WIRE_START_BATCH:
7677
case WIRE_COMMITMENT_SIGNED:
7778
case WIRE_REVOKE_AND_ACK:
7879
case WIRE_UPDATE_FEE:

connectd/gossip_store.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static bool public_msg_type(enum peer_wire type)
8282
case WIRE_UPDATE_FULFILL_HTLC:
8383
case WIRE_UPDATE_FAIL_HTLC:
8484
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
85+
case WIRE_START_BATCH:
8586
case WIRE_COMMITMENT_SIGNED:
8687
case WIRE_REVOKE_AND_ACK:
8788
case WIRE_UPDATE_FEE:

connectd/multiplex.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ static bool is_urgent(enum peer_wire type)
382382
/* These are time-sensitive, and so send without delay. */
383383
case WIRE_PING:
384384
case WIRE_PONG:
385+
case WIRE_START_BATCH:
385386
case WIRE_COMMITMENT_SIGNED:
386387
case WIRE_REVOKE_AND_ACK:
387388
return true;
@@ -440,6 +441,10 @@ static struct io_plan *encrypt_and_send(struct peer *peer,
440441

441442
set_urgent_flag(peer, is_urgent(type));
442443

444+
/* DTODO: If needed, split start_batch message back into multiple
445+
* individual write events. Currently we send all messages in the batch
446+
* in a single write event. */
447+
443448
/* We free this and the encrypted version in next write_to_peer */
444449
peer->sent_to_peer = cryptomsg_encrypt_msg(peer, &peer->cs, msg);
445450
return io_write(peer->to_peer,

gossipd/gossipd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ static void handle_recv_gossip(struct daemon *daemon, const u8 *outermsg)
244244
case WIRE_UPDATE_FULFILL_HTLC:
245245
case WIRE_UPDATE_FAIL_HTLC:
246246
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
247+
case WIRE_START_BATCH:
247248
case WIRE_COMMITMENT_SIGNED:
248249
case WIRE_REVOKE_AND_ACK:
249250
case WIRE_UPDATE_FEE:

openingd/dualopend.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,7 @@ static u8 *opening_negotiate_msg(const tal_t *ctx, struct state *state)
16731673
case WIRE_UPDATE_FULFILL_HTLC:
16741674
case WIRE_UPDATE_FAIL_HTLC:
16751675
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
1676+
case WIRE_START_BATCH:
16761677
case WIRE_COMMITMENT_SIGNED:
16771678
case WIRE_REVOKE_AND_ACK:
16781679
case WIRE_UPDATE_FEE:
@@ -2057,6 +2058,7 @@ static bool run_tx_interactive(struct state *state,
20572058
case WIRE_UPDATE_FULFILL_HTLC:
20582059
case WIRE_UPDATE_FAIL_HTLC:
20592060
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
2061+
case WIRE_START_BATCH:
20602062
case WIRE_COMMITMENT_SIGNED:
20612063
case WIRE_REVOKE_AND_ACK:
20622064
case WIRE_UPDATE_FEE:
@@ -4209,6 +4211,9 @@ static u8 *handle_peer_in(struct state *state)
42094211
}
42104212
accepter_start(state, msg);
42114213
return NULL;
4214+
case WIRE_START_BATCH:
4215+
/* We ignore batch messages as we dont need them */
4216+
return NULL;
42124217
case WIRE_COMMITMENT_SIGNED:
42134218
handle_commit_signed(state, msg);
42144219
return NULL;

wire/peer_wire.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static bool unknown_type(enum peer_wire t)
2121
case WIRE_UPDATE_FULFILL_HTLC:
2222
case WIRE_UPDATE_FAIL_HTLC:
2323
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
24+
case WIRE_START_BATCH:
2425
case WIRE_COMMITMENT_SIGNED:
2526
case WIRE_REVOKE_AND_ACK:
2627
case WIRE_UPDATE_FEE:
@@ -89,6 +90,7 @@ bool is_msg_for_gossipd(const u8 *cursor)
8990
case WIRE_UPDATE_FULFILL_HTLC:
9091
case WIRE_UPDATE_FAIL_HTLC:
9192
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
93+
case WIRE_START_BATCH:
9294
case WIRE_COMMITMENT_SIGNED:
9395
case WIRE_REVOKE_AND_ACK:
9496
case WIRE_UPDATE_FEE:
@@ -345,6 +347,11 @@ bool extract_channel_id(const u8 *in_pkt, struct channel_id *channel_id)
345347
* 2. data:
346348
* * [`channel_id`:`channel_id`]
347349
*/
350+
case WIRE_START_BATCH:
351+
/* 1. type: 127 (`start_batch`)
352+
* 2. data:
353+
* * [`channel_id`:`channel_id`]
354+
*/
348355
case WIRE_COMMITMENT_SIGNED:
349356
/* BOLT #2:
350357
* 1. type: 132 (`commitment_signed`)

wire/peer_wire.csv

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,19 @@ msgdata,update_fail_malformed_htlc,channel_id,channel_id,
293293
msgdata,update_fail_malformed_htlc,id,u64,
294294
msgdata,update_fail_malformed_htlc,sha256_of_onion,sha256,
295295
msgdata,update_fail_malformed_htlc,failure_code,u16,
296+
msgtype,start_batch,127
297+
msgdata,start_batch,channel_id,channel_id,
298+
msgdata,start_batch,batch_size,u16,
299+
msgdata,start_batch,batch_info,start_batch_tlvs,
300+
tlvtype,start_batch_tlvs,batch_info,1
301+
tlvdata,start_batch_tlvs,batch_info,message_type,u16,
296302
msgtype,commitment_signed,132
297303
msgdata,commitment_signed,channel_id,channel_id,
298304
msgdata,commitment_signed,signature,signature,
299305
msgdata,commitment_signed,num_htlcs,u16,
300306
msgdata,commitment_signed,htlc_signature,signature,num_htlcs
301307
msgdata,commitment_signed,splice_channel_id,commitment_signed_tlvs,
302308
tlvtype,commitment_signed_tlvs,splice_info,0
303-
tlvdata,commitment_signed_tlvs,splice_info,batch_size,u16,
304309
tlvdata,commitment_signed_tlvs,splice_info,funding_txid,sha256,
305310
msgtype,revoke_and_ack,133
306311
msgdata,revoke_and_ack,channel_id,channel_id,

0 commit comments

Comments
 (0)