MCPcopy Create free account
hub / github.com/ElementsProject/lightning / send_message_batch

Function send_message_batch

channeld/channeld.c:1365–1435  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1363}
1364
1365static void send_message_batch(struct peer *peer, u8 **msgs)
1366{
1367 size_t size;
1368 size_t hdr_size = tal_bytelen(towire_protocol_batch_element(tmpctx,
1369 &peer->channel_id,
1370 0));
1371 u8 *batch_msg, *final_msg, *final_msg_ptr;
1372 struct tlv_start_batch_tlvs *tlvs;
1373
1374 assert(tal_count(msgs) > 0);
1375
1376 /* When sending one message, no batching is required */
1377 if (tal_count(msgs) == 1) {
1378 peer_write(peer->pps, msgs[0]);
1379 return;
1380 }
1381
1382 /* We prefix each message with an interal wire type,
1383 * protocol_batch_element. connectd will eat each message so they don't
1384 * actually go out to the peer. It's just so connectd can chop up the
1385 * message batch back out into individual messages. */
1386
1387 /* We start by calculating the total size */
1388 size = 0;
1389
1390 /* Build the `start_batch` msg now so know it's size */
1391 tlvs = tlv_start_batch_tlvs_new(tmpctx);
1392 tlvs->batch_info = tal(tlvs, u16);
1393 *tlvs->batch_info = WIRE_COMMITMENT_SIGNED;
1394 batch_msg = towire_start_batch(tmpctx, &peer->channel_id,
1395 tal_count(msgs), tlvs);
1396 size += tal_bytelen(batch_msg) + hdr_size;
1397
1398 /* Count the size of all the messages in the batch */
1399 for(u32 i = 0; i < tal_count(msgs); i++)
1400 size += tal_bytelen(msgs[i]) + hdr_size;
1401
1402 /* Now we know the size of our `final_msg` so we allocate */
1403 final_msg = tal_arr(tmpctx, u8, size);
1404 final_msg_ptr = final_msg;
1405
1406 status_debug("proto_batch Building batch with %zu bytes, msgs: %zu",
1407 size, tal_count(msgs));
1408
1409 /* Copy the bytes for `start_batch` prefix */
1410 memcpy(final_msg_ptr,
1411 towire_protocol_batch_element(tmpctx,
1412 &peer->channel_id,
1413 tal_bytelen(batch_msg)),
1414 hdr_size);
1415 final_msg_ptr += hdr_size;
1416
1417 memcpy(final_msg_ptr, batch_msg, tal_bytelen(batch_msg));
1418 final_msg_ptr += tal_bytelen(batch_msg);
1419
1420 /* Now copy the bytes from all messages in `msgs` */
1421 for(u32 i = 0; i < tal_count(msgs); i++) {
1422 memcpy(final_msg_ptr,

Callers 2

send_commitFunction · 0.85
resend_commitmentFunction · 0.85

Calls 2

tal_bytelenFunction · 0.85
peer_writeFunction · 0.85

Tested by

no test coverage detected