Skip to content

Commit 5fa1e16

Browse files
committed
splice: Add test for splice-out and fix errors
Added a test for splicing out that exposed some behavior and code glitches that are addressed in this commit. Added tests around invalid splices and forced restarts mid-splice and fixed related functionality. Also added documentation for how to do a splice out . ChangeLog-Fixed: Added docs, testing, and some fixes related to splicing out, insufficent balance handling, and restarting during a splice.
1 parent fe7a91c commit 5fa1e16

10 files changed

+329
-108
lines changed

channeld/channeld.c

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
((msg) == WIRE_SPLICE || \
6262
(msg) == WIRE_SPLICE_ACK)
6363

64+
#define SAT_MIN(a, b) (amount_sat_less((a), (b)) ? (a) : (b))
65+
6466
struct peer {
6567
struct per_peer_state *pps;
6668
bool channel_ready[NUM_SIDES];
@@ -1487,7 +1489,8 @@ static u8 *send_commit_part(struct peer *peer,
14871489
const struct htlc **changed_htlcs,
14881490
bool notify_master,
14891491
s64 splice_amnt,
1490-
s64 remote_splice_amnt)
1492+
s64 remote_splice_amnt,
1493+
u64 remote_index)
14911494
{
14921495
u8 *msg;
14931496
struct bitcoin_signature commit_sig, *htlc_sigs;
@@ -1515,14 +1518,14 @@ static u8 *send_commit_part(struct peer *peer,
15151518
txs = channel_splice_txs(tmpctx, funding, funding_sats, &htlc_map,
15161519
direct_outputs, &funding_wscript,
15171520
peer->channel, &peer->remote_per_commit,
1518-
peer->next_index[REMOTE], REMOTE,
1521+
remote_index, REMOTE,
15191522
splice_amnt, remote_splice_amnt);
15201523
htlc_sigs =
15211524
calc_commitsigs(tmpctx, peer, txs, funding_wscript, htlc_map,
1522-
peer->next_index[REMOTE], &commit_sig);
1525+
remote_index, &commit_sig);
15231526

15241527
if (direct_outputs[LOCAL] != NULL) {
1525-
pbase = penalty_base_new(tmpctx, peer->next_index[REMOTE],
1528+
pbase = penalty_base_new(tmpctx, remote_index,
15261529
txs[0], direct_outputs[LOCAL]);
15271530

15281531
/* Add the penalty_base to our in-memory list as well, so we
@@ -1543,8 +1546,7 @@ static u8 *send_commit_part(struct peer *peer,
15431546
status_debug("Telling master we're about to commit...");
15441547
/* Tell master to save this next commit to database, then wait.
15451548
*/
1546-
msg = sending_commitsig_msg(NULL, peer->next_index[REMOTE],
1547-
pbase,
1549+
msg = sending_commitsig_msg(NULL, remote_index, pbase,
15481550
peer->channel->fee_states,
15491551
peer->channel->blockheight_states,
15501552
changed_htlcs,
@@ -1692,7 +1694,7 @@ static void send_commit(struct peer *peer)
16921694

16931695
msgs[0] = send_commit_part(peer, &peer->channel->funding,
16941696
peer->channel->funding_sats, changed_htlcs,
1695-
true, 0, 0);
1697+
true, 0, 0, peer->next_index[REMOTE]);
16961698

16971699
/* Loop over current inflights
16981700
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
@@ -1715,7 +1717,8 @@ static void send_commit(struct peer *peer)
17151717
peer->splice_state->inflights[i]->amnt,
17161718
changed_htlcs, false,
17171719
peer->splice_state->inflights[i]->splice_amnt,
1718-
remote_splice_amnt));
1720+
remote_splice_amnt,
1721+
peer->next_index[REMOTE]));
17191722
}
17201723

17211724
peer->next_index[REMOTE]++;
@@ -2907,7 +2910,7 @@ static size_t calc_weight(enum tx_role role, const struct wally_psbt *psbt)
29072910
weight += psbt_input_get_weight(psbt, i);
29082911

29092912
for (size_t i = 0; i < psbt->num_outputs; i++)
2910-
if (is_initiators_serial(&psbt->inputs[i].unknowns)) {
2913+
if (is_initiators_serial(&psbt->outputs[i].unknowns)) {
29112914
if (role == TX_INITIATOR)
29122915
weight += psbt_output_get_weight(psbt, i);
29132916
}
@@ -2928,7 +2931,7 @@ static struct amount_sat check_balances(struct peer *peer,
29282931
{
29292932
struct amount_sat min_initiator_fee, min_accepter_fee,
29302933
max_initiator_fee, max_accepter_fee,
2931-
funding_amount_res;
2934+
funding_amount_res, min_multiplied;
29322935
struct amount_msat funding_amount,
29332936
initiator_fee, accepter_fee;
29342937
struct amount_msat in[NUM_TX_ROLES], out[NUM_TX_ROLES];
@@ -2977,45 +2980,23 @@ static struct amount_sat check_balances(struct peer *peer,
29772980
* While we're, here, adjust the output counts by splice amount.
29782981
*/
29792982

2980-
if(peer->splicing->opener_relative > 0) {
2981-
if (!amount_msat_add_sat(&funding_amount, funding_amount,
2982-
amount_sat((u64)peer->splicing->opener_relative)))
2983-
peer_failed_warn(peer->pps, &peer->channel_id,
2984-
"Unable to add opener funding");
2985-
if (!amount_msat_add_sat(&out[TX_INITIATOR], out[TX_INITIATOR],
2986-
amount_sat((u64)peer->splicing->opener_relative)))
2987-
peer_failed_warn(peer->pps, &peer->channel_id,
2988-
"Unable to add opener funding to out amnt.");
2989-
} else {
2990-
if (!amount_msat_sub_sat(&funding_amount, funding_amount,
2991-
amount_sat((u64)-peer->splicing->opener_relative)))
2992-
peer_failed_warn(peer->pps, &peer->channel_id,
2993-
"Unable to sub opener funding");
2994-
if (!amount_msat_sub_sat(&out[TX_INITIATOR], out[TX_INITIATOR],
2995-
amount_sat((u64)peer->splicing->opener_relative)))
2996-
peer_failed_warn(peer->pps, &peer->channel_id,
2997-
"Unable to sub opener funding from out amnt.");
2998-
}
2983+
if (!amount_msat_add_sat_s64(&funding_amount, funding_amount,
2984+
peer->splicing->opener_relative))
2985+
peer_failed_warn(peer->pps, &peer->channel_id,
2986+
"Unable to add opener funding");
2987+
if (!amount_msat_add_sat_s64(&out[TX_INITIATOR], out[TX_INITIATOR],
2988+
peer->splicing->opener_relative))
2989+
peer_failed_warn(peer->pps, &peer->channel_id,
2990+
"Unable to add opener funding to out amnt.");
29992991

3000-
if(peer->splicing->accepter_relative > 0) {
3001-
if (!amount_msat_add_sat(&funding_amount, funding_amount,
3002-
amount_sat((u64)peer->splicing->accepter_relative)))
3003-
peer_failed_warn(peer->pps, &peer->channel_id,
3004-
"Unable to add accepter funding");
3005-
if (!amount_msat_add_sat(&out[TX_ACCEPTER], out[TX_ACCEPTER],
3006-
amount_sat((u64)peer->splicing->accepter_relative)))
3007-
peer_failed_warn(peer->pps, &peer->channel_id,
3008-
"Unable to add accepter funding to out amnt.");
3009-
} else {
3010-
if (!amount_msat_sub_sat(&funding_amount, funding_amount,
3011-
amount_sat((u64)-peer->splicing->accepter_relative)))
3012-
peer_failed_warn(peer->pps, &peer->channel_id,
3013-
"Unable to subtract accepter funding");
3014-
if (!amount_msat_sub_sat(&out[TX_ACCEPTER], out[TX_ACCEPTER],
3015-
amount_sat((u64)-peer->splicing->accepter_relative)))
3016-
peer_failed_warn(peer->pps, &peer->channel_id,
3017-
"Unable to sub accepter funding from out amnt.");
3018-
}
2992+
if (!amount_msat_add_sat_s64(&funding_amount, funding_amount,
2993+
peer->splicing->accepter_relative))
2994+
peer_failed_warn(peer->pps, &peer->channel_id,
2995+
"Unable to add accepter funding");
2996+
if (!amount_msat_add_sat_s64(&out[TX_ACCEPTER], out[TX_ACCEPTER],
2997+
peer->splicing->accepter_relative))
2998+
peer_failed_warn(peer->pps, &peer->channel_id,
2999+
"Unable to add accepter funding to out amnt.");
30193000

30203001
if (amount_msat_less(in[TX_INITIATOR], out[TX_INITIATOR])) {
30213002
msg = towire_channeld_splice_funding_error(NULL, in[TX_INITIATOR],
@@ -3064,6 +3045,14 @@ static struct amount_sat check_balances(struct peer *peer,
30643045
max_initiator_fee = amount_tx_fee(peer->feerate_max,
30653046
calc_weight(TX_INITIATOR, psbt));
30663047

3048+
/* Sometimes feerate_max is some absurdly high value, in that case we
3049+
* give a fee warning based of a multiple of the min value. */
3050+
amount_sat_mul(&min_multiplied, min_accepter_fee, 5);
3051+
max_accepter_fee = SAT_MIN(min_multiplied, max_accepter_fee);
3052+
3053+
amount_sat_mul(&min_multiplied, min_initiator_fee, 5);
3054+
max_initiator_fee = SAT_MIN(min_multiplied, max_initiator_fee);
3055+
30673056
/* Check initiator fee */
30683057
if (amount_msat_less_sat(initiator_fee, min_initiator_fee)) {
30693058
msg = towire_channeld_splice_feerate_error(NULL, initiator_fee,
@@ -3302,11 +3291,11 @@ static void resume_splice_negotiation(struct peer *peer,
33023291
txsig_tlvs);
33033292

33043293
if (do_i_sign_first(peer, current_psbt, our_role)) {
3305-
status_debug("Splice: we sign first");
33063294
msg = towire_channeld_update_inflight(NULL, current_psbt,
33073295
NULL, NULL);
33083296
wire_sync_write(MASTER_FD, take(msg));
33093297
peer_write(peer->pps, sigmsg);
3298+
status_debug("Splice: we signed first");
33103299
}
33113300

33123301
msg = peer_read(tmpctx, peer->pps);
@@ -3423,8 +3412,8 @@ static void resume_splice_negotiation(struct peer *peer,
34233412
wire_sync_write(MASTER_FD, take(msg));
34243413

34253414
if (!do_i_sign_first(peer, current_psbt, our_role)) {
3426-
status_debug("Splice: we sign second");
34273415
peer_write(peer->pps, sigmsg);
3416+
status_debug("Splice: we signed second");
34283417
}
34293418

34303419
peer->splicing = tal_free(peer->splicing);
@@ -4263,12 +4252,8 @@ static int cmp_changed_htlc_id(const struct changed_htlc *a,
42634252
static void resend_commitment(struct peer *peer, struct changed_htlc *last)
42644253
{
42654254
size_t i;
4266-
struct bitcoin_signature commit_sig, *htlc_sigs;
42674255
u8 *msg;
4268-
struct bitcoin_tx **txs;
4269-
const u8 *funding_wscript;
4270-
const struct htlc **htlc_map;
4271-
struct wally_tx_output *direct_outputs[NUM_SIDES];
4256+
u8 **msgs = tal_arr(tmpctx, u8*, 1);
42724257

42734258
status_debug("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u,"
42744259
" blockheight LOCAL=%u REMOTE=%u",
@@ -4359,19 +4344,37 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
43594344
}
43604345
}
43614346

4362-
/* Re-send the commitment_signed itself. */
4363-
txs = channel_txs(tmpctx, &htlc_map, direct_outputs,
4364-
&funding_wscript, peer->channel, &peer->remote_per_commit,
4365-
peer->next_index[REMOTE]-1, REMOTE);
4347+
msgs[0] = send_commit_part(peer, &peer->channel->funding,
4348+
peer->channel->funding_sats, NULL,
4349+
false, 0, 0, peer->next_index[REMOTE] - 1);
43664350

4367-
htlc_sigs = calc_commitsigs(tmpctx, peer, txs, funding_wscript, htlc_map, peer->next_index[REMOTE]-1,
4368-
&commit_sig);
4351+
/* Loop over current inflights
4352+
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
4353+
*
4354+
* A sending node:
4355+
*...
4356+
* - MUST first send a `commitment_signed` for the active channel then immediately
4357+
* send a `commitment_signed` for each splice awaiting confirmation, in increasing
4358+
* feerate order.
4359+
*/
4360+
for (i = 0; i < tal_count(peer->splice_state->inflights); i++) {
4361+
s64 funding_diff = sats_diff(peer->splice_state->inflights[i]->amnt,
4362+
peer->channel->funding_sats);
4363+
s64 remote_splice_amnt = funding_diff
4364+
- peer->splice_state->inflights[i]->splice_amnt;
43694365

4370-
msg = towire_commitment_signed(NULL, &peer->channel_id,
4371-
&commit_sig.s,
4372-
raw_sigs(tmpctx, htlc_sigs),
4373-
NULL);
4374-
peer_write(peer->pps, take(msg));
4366+
tal_arr_expand(&msgs,
4367+
send_commit_part(peer,
4368+
&peer->splice_state->inflights[i]->outpoint,
4369+
peer->splice_state->inflights[i]->amnt,
4370+
NULL, false,
4371+
peer->splice_state->inflights[i]->splice_amnt,
4372+
remote_splice_amnt,
4373+
peer->next_index[REMOTE] - 1));
4374+
}
4375+
4376+
for(i = 0; i < tal_count(msgs); i++)
4377+
peer_write(peer->pps, take(msgs[i]));
43754378

43764379
/* If we have already received the revocation for the previous, the
43774380
* other side shouldn't be asking for a retransmit! */
@@ -4638,8 +4641,14 @@ static void peer_reconnect(struct peer *peer,
46384641
send_tlvs = tlv_channel_reestablish_tlvs_new(peer);
46394642

46404643
/* If inflight with no sigs on it, send next_funding */
4641-
if (inflight && !inflight->last_tx)
4644+
if (inflight && !inflight->last_tx) {
4645+
status_debug("Reestablish with an inflight but missing"
4646+
" last_tx, will send next_funding %s",
4647+
type_to_string(tmpctx,
4648+
struct bitcoin_txid,
4649+
&inflight->outpoint.txid));
46424650
send_tlvs->next_funding = &inflight->outpoint.txid;
4651+
}
46434652

46444653
/* BOLT-upgrade_protocol #2:
46454654
* A node sending `channel_reestablish`, if it supports upgrading channels:
@@ -4772,9 +4781,12 @@ static void peer_reconnect(struct peer *peer,
47724781
tal_hex(msg, msg));
47734782
}
47744783

4775-
status_debug("Got reestablish commit=%"PRIu64" revoke=%"PRIu64,
4784+
status_debug("Got reestablish commit=%"PRIu64" revoke=%"PRIu64
4785+
" inflights: %lu, active splices: %"PRIu32,
47764786
next_commitment_number,
4777-
next_revocation_number);
4787+
next_revocation_number,
4788+
tal_count(peer->splice_state->inflights),
4789+
peer->splice_state->count);
47784790

47794791
/* BOLT #2:
47804792
*
@@ -5079,6 +5091,7 @@ static void peer_reconnect(struct peer *peer,
50795091
&peer->channel->funding.txid));
50805092
}
50815093
else {
5094+
status_info("Resuming splice negotation");
50825095
resume_splice_negotiation(peer, inflight, false,
50835096
inflight->i_am_initiator
50845097
? TX_INITIATOR

channeld/inflight.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ struct inflight *fromwire_inflight(const tal_t *ctx, const u8 **cursor, size_t *
1212
inflight->amnt = fromwire_amount_sat(cursor, max);
1313
inflight->psbt = fromwire_wally_psbt(inflight, cursor, max);
1414
inflight->splice_amnt = fromwire_s64(cursor, max);
15-
inflight->last_tx = fromwire_bitcoin_tx(inflight, cursor, max);
16-
fromwire_bitcoin_signature(cursor, max, &inflight->last_sig);
15+
int has_tx = fromwire_u8(cursor, max);
16+
if(has_tx) {
17+
inflight->last_tx = fromwire_bitcoin_tx(inflight, cursor, max);
18+
fromwire_bitcoin_signature(cursor, max, &inflight->last_sig);
19+
}
20+
else {
21+
inflight->last_tx = NULL;
22+
memset(&inflight->last_sig, 0, sizeof(inflight->last_sig));
23+
}
1724
inflight->i_am_initiator = fromwire_bool(cursor, max);
1825

1926
return inflight;
@@ -25,8 +32,11 @@ void towire_inflight(u8 **pptr, const struct inflight *inflight)
2532
towire_amount_sat(pptr, inflight->amnt);
2633
towire_wally_psbt(pptr, inflight->psbt);
2734
towire_s64(pptr, inflight->splice_amnt);
28-
towire_bitcoin_tx(pptr, inflight->last_tx);
29-
towire_bitcoin_signature(pptr, &inflight->last_sig);
35+
towire_u8(pptr, inflight->last_tx ? 1 : 0);
36+
if(inflight->last_tx) {
37+
towire_bitcoin_tx(pptr, inflight->last_tx);
38+
towire_bitcoin_signature(pptr, &inflight->last_sig);
39+
}
3040
towire_bool(pptr, inflight->i_am_initiator);
3141
}
3242

0 commit comments

Comments
 (0)