61
61
((msg) == WIRE_SPLICE || \
62
62
(msg) == WIRE_SPLICE_ACK)
63
63
64
+ #define SAT_MIN (a , b ) (amount_sat_less((a), (b)) ? (a) : (b))
65
+
64
66
struct peer {
65
67
struct per_peer_state * pps ;
66
68
bool channel_ready [NUM_SIDES ];
@@ -1487,7 +1489,8 @@ static u8 *send_commit_part(struct peer *peer,
1487
1489
const struct htlc * * changed_htlcs ,
1488
1490
bool notify_master ,
1489
1491
s64 splice_amnt ,
1490
- s64 remote_splice_amnt )
1492
+ s64 remote_splice_amnt ,
1493
+ u64 remote_index )
1491
1494
{
1492
1495
u8 * msg ;
1493
1496
struct bitcoin_signature commit_sig , * htlc_sigs ;
@@ -1515,14 +1518,14 @@ static u8 *send_commit_part(struct peer *peer,
1515
1518
txs = channel_splice_txs (tmpctx , funding , funding_sats , & htlc_map ,
1516
1519
direct_outputs , & funding_wscript ,
1517
1520
peer -> channel , & peer -> remote_per_commit ,
1518
- peer -> next_index [ REMOTE ] , REMOTE ,
1521
+ remote_index , REMOTE ,
1519
1522
splice_amnt , remote_splice_amnt );
1520
1523
htlc_sigs =
1521
1524
calc_commitsigs (tmpctx , peer , txs , funding_wscript , htlc_map ,
1522
- peer -> next_index [ REMOTE ] , & commit_sig );
1525
+ remote_index , & commit_sig );
1523
1526
1524
1527
if (direct_outputs [LOCAL ] != NULL ) {
1525
- pbase = penalty_base_new (tmpctx , peer -> next_index [ REMOTE ] ,
1528
+ pbase = penalty_base_new (tmpctx , remote_index ,
1526
1529
txs [0 ], direct_outputs [LOCAL ]);
1527
1530
1528
1531
/* 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,
1543
1546
status_debug ("Telling master we're about to commit..." );
1544
1547
/* Tell master to save this next commit to database, then wait.
1545
1548
*/
1546
- msg = sending_commitsig_msg (NULL , peer -> next_index [REMOTE ],
1547
- pbase ,
1549
+ msg = sending_commitsig_msg (NULL , remote_index , pbase ,
1548
1550
peer -> channel -> fee_states ,
1549
1551
peer -> channel -> blockheight_states ,
1550
1552
changed_htlcs ,
@@ -1692,7 +1694,7 @@ static void send_commit(struct peer *peer)
1692
1694
1693
1695
msgs [0 ] = send_commit_part (peer , & peer -> channel -> funding ,
1694
1696
peer -> channel -> funding_sats , changed_htlcs ,
1695
- true, 0 , 0 );
1697
+ true, 0 , 0 , peer -> next_index [ REMOTE ] );
1696
1698
1697
1699
/* Loop over current inflights
1698
1700
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
@@ -1715,7 +1717,8 @@ static void send_commit(struct peer *peer)
1715
1717
peer -> splice_state -> inflights [i ]-> amnt ,
1716
1718
changed_htlcs , false,
1717
1719
peer -> splice_state -> inflights [i ]-> splice_amnt ,
1718
- remote_splice_amnt ));
1720
+ remote_splice_amnt ,
1721
+ peer -> next_index [REMOTE ]));
1719
1722
}
1720
1723
1721
1724
peer -> next_index [REMOTE ]++ ;
@@ -2907,7 +2910,7 @@ static size_t calc_weight(enum tx_role role, const struct wally_psbt *psbt)
2907
2910
weight += psbt_input_get_weight (psbt , i );
2908
2911
2909
2912
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 )) {
2911
2914
if (role == TX_INITIATOR )
2912
2915
weight += psbt_output_get_weight (psbt , i );
2913
2916
}
@@ -2928,7 +2931,7 @@ static struct amount_sat check_balances(struct peer *peer,
2928
2931
{
2929
2932
struct amount_sat min_initiator_fee , min_accepter_fee ,
2930
2933
max_initiator_fee , max_accepter_fee ,
2931
- funding_amount_res ;
2934
+ funding_amount_res , min_multiplied ;
2932
2935
struct amount_msat funding_amount ,
2933
2936
initiator_fee , accepter_fee ;
2934
2937
struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ];
@@ -2977,45 +2980,23 @@ static struct amount_sat check_balances(struct peer *peer,
2977
2980
* While we're, here, adjust the output counts by splice amount.
2978
2981
*/
2979
2982
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." );
2999
2991
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." );
3019
3000
3020
3001
if (amount_msat_less (in [TX_INITIATOR ], out [TX_INITIATOR ])) {
3021
3002
msg = towire_channeld_splice_funding_error (NULL , in [TX_INITIATOR ],
@@ -3064,6 +3045,14 @@ static struct amount_sat check_balances(struct peer *peer,
3064
3045
max_initiator_fee = amount_tx_fee (peer -> feerate_max ,
3065
3046
calc_weight (TX_INITIATOR , psbt ));
3066
3047
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
+
3067
3056
/* Check initiator fee */
3068
3057
if (amount_msat_less_sat (initiator_fee , min_initiator_fee )) {
3069
3058
msg = towire_channeld_splice_feerate_error (NULL , initiator_fee ,
@@ -3302,11 +3291,11 @@ static void resume_splice_negotiation(struct peer *peer,
3302
3291
txsig_tlvs );
3303
3292
3304
3293
if (do_i_sign_first (peer , current_psbt , our_role )) {
3305
- status_debug ("Splice: we sign first" );
3306
3294
msg = towire_channeld_update_inflight (NULL , current_psbt ,
3307
3295
NULL , NULL );
3308
3296
wire_sync_write (MASTER_FD , take (msg ));
3309
3297
peer_write (peer -> pps , sigmsg );
3298
+ status_debug ("Splice: we signed first" );
3310
3299
}
3311
3300
3312
3301
msg = peer_read (tmpctx , peer -> pps );
@@ -3423,8 +3412,8 @@ static void resume_splice_negotiation(struct peer *peer,
3423
3412
wire_sync_write (MASTER_FD , take (msg ));
3424
3413
3425
3414
if (!do_i_sign_first (peer , current_psbt , our_role )) {
3426
- status_debug ("Splice: we sign second" );
3427
3415
peer_write (peer -> pps , sigmsg );
3416
+ status_debug ("Splice: we signed second" );
3428
3417
}
3429
3418
3430
3419
peer -> splicing = tal_free (peer -> splicing );
@@ -4263,12 +4252,8 @@ static int cmp_changed_htlc_id(const struct changed_htlc *a,
4263
4252
static void resend_commitment (struct peer * peer , struct changed_htlc * last )
4264
4253
{
4265
4254
size_t i ;
4266
- struct bitcoin_signature commit_sig , * htlc_sigs ;
4267
4255
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 );
4272
4257
4273
4258
status_debug ("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u,"
4274
4259
" blockheight LOCAL=%u REMOTE=%u" ,
@@ -4359,19 +4344,37 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
4359
4344
}
4360
4345
}
4361
4346
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 );
4366
4350
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 ;
4369
4365
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 ]));
4375
4378
4376
4379
/* If we have already received the revocation for the previous, the
4377
4380
* other side shouldn't be asking for a retransmit! */
@@ -4638,8 +4641,14 @@ static void peer_reconnect(struct peer *peer,
4638
4641
send_tlvs = tlv_channel_reestablish_tlvs_new (peer );
4639
4642
4640
4643
/* 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 ));
4642
4650
send_tlvs -> next_funding = & inflight -> outpoint .txid ;
4651
+ }
4643
4652
4644
4653
/* BOLT-upgrade_protocol #2:
4645
4654
* A node sending `channel_reestablish`, if it supports upgrading channels:
@@ -4772,9 +4781,12 @@ static void peer_reconnect(struct peer *peer,
4772
4781
tal_hex (msg , msg ));
4773
4782
}
4774
4783
4775
- status_debug ("Got reestablish commit=%" PRIu64 " revoke=%" PRIu64 ,
4784
+ status_debug ("Got reestablish commit=%" PRIu64 " revoke=%" PRIu64
4785
+ " inflights: %lu, active splices: %" PRIu32 ,
4776
4786
next_commitment_number ,
4777
- next_revocation_number );
4787
+ next_revocation_number ,
4788
+ tal_count (peer -> splice_state -> inflights ),
4789
+ peer -> splice_state -> count );
4778
4790
4779
4791
/* BOLT #2:
4780
4792
*
@@ -5079,6 +5091,7 @@ static void peer_reconnect(struct peer *peer,
5079
5091
& peer -> channel -> funding .txid ));
5080
5092
}
5081
5093
else {
5094
+ status_info ("Resuming splice negotation" );
5082
5095
resume_splice_negotiation (peer , inflight , false,
5083
5096
inflight -> i_am_initiator
5084
5097
? TX_INITIATOR
0 commit comments