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 ];
@@ -756,6 +758,9 @@ static void check_mutual_splice_locked(struct peer *peer)
756
758
& inflight -> outpoint .txid );
757
759
wire_sync_write (MASTER_FD , take (msg ));
758
760
761
+ /* We must regossip the scid since it has changed */
762
+ peer -> gossip_scid_announced = false;
763
+
759
764
channel_announcement_negotiate (peer );
760
765
billboard_update (peer );
761
766
send_channel_update (peer , 0 );
@@ -1487,7 +1492,8 @@ static u8 *send_commit_part(struct peer *peer,
1487
1492
const struct htlc * * changed_htlcs ,
1488
1493
bool notify_master ,
1489
1494
s64 splice_amnt ,
1490
- s64 remote_splice_amnt )
1495
+ s64 remote_splice_amnt ,
1496
+ u64 remote_index )
1491
1497
{
1492
1498
u8 * msg ;
1493
1499
struct bitcoin_signature commit_sig , * htlc_sigs ;
@@ -1515,14 +1521,14 @@ static u8 *send_commit_part(struct peer *peer,
1515
1521
txs = channel_splice_txs (tmpctx , funding , funding_sats , & htlc_map ,
1516
1522
direct_outputs , & funding_wscript ,
1517
1523
peer -> channel , & peer -> remote_per_commit ,
1518
- peer -> next_index [ REMOTE ] , REMOTE ,
1524
+ remote_index , REMOTE ,
1519
1525
splice_amnt , remote_splice_amnt );
1520
1526
htlc_sigs =
1521
1527
calc_commitsigs (tmpctx , peer , txs , funding_wscript , htlc_map ,
1522
- peer -> next_index [ REMOTE ] , & commit_sig );
1528
+ remote_index , & commit_sig );
1523
1529
1524
1530
if (direct_outputs [LOCAL ] != NULL ) {
1525
- pbase = penalty_base_new (tmpctx , peer -> next_index [ REMOTE ] ,
1531
+ pbase = penalty_base_new (tmpctx , remote_index ,
1526
1532
txs [0 ], direct_outputs [LOCAL ]);
1527
1533
1528
1534
/* Add the penalty_base to our in-memory list as well, so we
@@ -1543,8 +1549,7 @@ static u8 *send_commit_part(struct peer *peer,
1543
1549
status_debug ("Telling master we're about to commit..." );
1544
1550
/* Tell master to save this next commit to database, then wait.
1545
1551
*/
1546
- msg = sending_commitsig_msg (NULL , peer -> next_index [REMOTE ],
1547
- pbase ,
1552
+ msg = sending_commitsig_msg (NULL , remote_index , pbase ,
1548
1553
peer -> channel -> fee_states ,
1549
1554
peer -> channel -> blockheight_states ,
1550
1555
changed_htlcs ,
@@ -1692,7 +1697,7 @@ static void send_commit(struct peer *peer)
1692
1697
1693
1698
msgs [0 ] = send_commit_part (peer , & peer -> channel -> funding ,
1694
1699
peer -> channel -> funding_sats , changed_htlcs ,
1695
- true, 0 , 0 );
1700
+ true, 0 , 0 , peer -> next_index [ REMOTE ] );
1696
1701
1697
1702
/* Loop over current inflights
1698
1703
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
@@ -1715,7 +1720,8 @@ static void send_commit(struct peer *peer)
1715
1720
peer -> splice_state -> inflights [i ]-> amnt ,
1716
1721
changed_htlcs , false,
1717
1722
peer -> splice_state -> inflights [i ]-> splice_amnt ,
1718
- remote_splice_amnt ));
1723
+ remote_splice_amnt ,
1724
+ peer -> next_index [REMOTE ]));
1719
1725
}
1720
1726
1721
1727
peer -> next_index [REMOTE ]++ ;
@@ -2907,7 +2913,7 @@ static size_t calc_weight(enum tx_role role, const struct wally_psbt *psbt)
2907
2913
weight += psbt_input_get_weight (psbt , i );
2908
2914
2909
2915
for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ )
2910
- if (is_initiators_serial (& psbt -> inputs [i ].unknowns )) {
2916
+ if (is_initiators_serial (& psbt -> outputs [i ].unknowns )) {
2911
2917
if (role == TX_INITIATOR )
2912
2918
weight += psbt_output_get_weight (psbt , i );
2913
2919
}
@@ -2928,7 +2934,7 @@ static struct amount_sat check_balances(struct peer *peer,
2928
2934
{
2929
2935
struct amount_sat min_initiator_fee , min_accepter_fee ,
2930
2936
max_initiator_fee , max_accepter_fee ,
2931
- funding_amount_res ;
2937
+ funding_amount_res , min_multiplied ;
2932
2938
struct amount_msat funding_amount ,
2933
2939
initiator_fee , accepter_fee ;
2934
2940
struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ];
@@ -2977,45 +2983,23 @@ static struct amount_sat check_balances(struct peer *peer,
2977
2983
* While we're, here, adjust the output counts by splice amount.
2978
2984
*/
2979
2985
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
- }
2986
+ if (!amount_msat_add_sat_s64 (& funding_amount , funding_amount ,
2987
+ peer -> splicing -> opener_relative ))
2988
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
2989
+ "Unable to add opener funding" );
2990
+ if (!amount_msat_add_sat_s64 (& out [TX_INITIATOR ], out [TX_INITIATOR ],
2991
+ peer -> splicing -> opener_relative ))
2992
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
2993
+ "Unable to add opener funding to out amnt." );
2999
2994
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
- }
2995
+ if (!amount_msat_add_sat_s64 (& funding_amount , funding_amount ,
2996
+ peer -> splicing -> accepter_relative ))
2997
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
2998
+ "Unable to add accepter funding" );
2999
+ if (!amount_msat_add_sat_s64 (& out [TX_ACCEPTER ], out [TX_ACCEPTER ],
3000
+ peer -> splicing -> accepter_relative ))
3001
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
3002
+ "Unable to add accepter funding to out amnt." );
3019
3003
3020
3004
if (amount_msat_less (in [TX_INITIATOR ], out [TX_INITIATOR ])) {
3021
3005
msg = towire_channeld_splice_funding_error (NULL , in [TX_INITIATOR ],
@@ -3064,6 +3048,14 @@ static struct amount_sat check_balances(struct peer *peer,
3064
3048
max_initiator_fee = amount_tx_fee (peer -> feerate_max ,
3065
3049
calc_weight (TX_INITIATOR , psbt ));
3066
3050
3051
+ /* Sometimes feerate_max is some absurdly high value, in that case we
3052
+ * give a fee warning based of a multiple of the min value. */
3053
+ amount_sat_mul (& min_multiplied , min_accepter_fee , 5 );
3054
+ max_accepter_fee = SAT_MIN (min_multiplied , max_accepter_fee );
3055
+
3056
+ amount_sat_mul (& min_multiplied , min_initiator_fee , 5 );
3057
+ max_initiator_fee = SAT_MIN (min_multiplied , max_initiator_fee );
3058
+
3067
3059
/* Check initiator fee */
3068
3060
if (amount_msat_less_sat (initiator_fee , min_initiator_fee )) {
3069
3061
msg = towire_channeld_splice_feerate_error (NULL , initiator_fee ,
@@ -3302,11 +3294,11 @@ static void resume_splice_negotiation(struct peer *peer,
3302
3294
txsig_tlvs );
3303
3295
3304
3296
if (do_i_sign_first (peer , current_psbt , our_role )) {
3305
- status_debug ("Splice: we sign first" );
3306
3297
msg = towire_channeld_update_inflight (NULL , current_psbt ,
3307
3298
NULL , NULL );
3308
3299
wire_sync_write (MASTER_FD , take (msg ));
3309
3300
peer_write (peer -> pps , sigmsg );
3301
+ status_debug ("Splice: we signed first" );
3310
3302
}
3311
3303
3312
3304
msg = peer_read (tmpctx , peer -> pps );
@@ -3423,8 +3415,8 @@ static void resume_splice_negotiation(struct peer *peer,
3423
3415
wire_sync_write (MASTER_FD , take (msg ));
3424
3416
3425
3417
if (!do_i_sign_first (peer , current_psbt , our_role )) {
3426
- status_debug ("Splice: we sign second" );
3427
3418
peer_write (peer -> pps , sigmsg );
3419
+ status_debug ("Splice: we signed second" );
3428
3420
}
3429
3421
3430
3422
peer -> splicing = tal_free (peer -> splicing );
@@ -4263,12 +4255,8 @@ static int cmp_changed_htlc_id(const struct changed_htlc *a,
4263
4255
static void resend_commitment (struct peer * peer , struct changed_htlc * last )
4264
4256
{
4265
4257
size_t i ;
4266
- struct bitcoin_signature commit_sig , * htlc_sigs ;
4267
4258
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 ];
4259
+ u8 * * msgs = tal_arr (tmpctx , u8 * , 1 );
4272
4260
4273
4261
status_debug ("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u,"
4274
4262
" blockheight LOCAL=%u REMOTE=%u" ,
@@ -4359,19 +4347,37 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
4359
4347
}
4360
4348
}
4361
4349
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 );
4350
+ msgs [0 ] = send_commit_part (peer , & peer -> channel -> funding ,
4351
+ peer -> channel -> funding_sats , NULL ,
4352
+ false, 0 , 0 , peer -> next_index [REMOTE ] - 1 );
4366
4353
4367
- htlc_sigs = calc_commitsigs (tmpctx , peer , txs , funding_wscript , htlc_map , peer -> next_index [REMOTE ]- 1 ,
4368
- & commit_sig );
4354
+ /* Loop over current inflights
4355
+ * BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
4356
+ *
4357
+ * A sending node:
4358
+ *...
4359
+ * - MUST first send a `commitment_signed` for the active channel then immediately
4360
+ * send a `commitment_signed` for each splice awaiting confirmation, in increasing
4361
+ * feerate order.
4362
+ */
4363
+ for (i = 0 ; i < tal_count (peer -> splice_state -> inflights ); i ++ ) {
4364
+ s64 funding_diff = sats_diff (peer -> splice_state -> inflights [i ]-> amnt ,
4365
+ peer -> channel -> funding_sats );
4366
+ s64 remote_splice_amnt = funding_diff
4367
+ - peer -> splice_state -> inflights [i ]-> splice_amnt ;
4369
4368
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 ));
4369
+ tal_arr_expand (& msgs ,
4370
+ send_commit_part (peer ,
4371
+ & peer -> splice_state -> inflights [i ]-> outpoint ,
4372
+ peer -> splice_state -> inflights [i ]-> amnt ,
4373
+ NULL , false,
4374
+ peer -> splice_state -> inflights [i ]-> splice_amnt ,
4375
+ remote_splice_amnt ,
4376
+ peer -> next_index [REMOTE ] - 1 ));
4377
+ }
4378
+
4379
+ for (i = 0 ; i < tal_count (msgs ); i ++ )
4380
+ peer_write (peer -> pps , take (msgs [i ]));
4375
4381
4376
4382
/* If we have already received the revocation for the previous, the
4377
4383
* other side shouldn't be asking for a retransmit! */
@@ -4638,8 +4644,14 @@ static void peer_reconnect(struct peer *peer,
4638
4644
send_tlvs = tlv_channel_reestablish_tlvs_new (peer );
4639
4645
4640
4646
/* If inflight with no sigs on it, send next_funding */
4641
- if (inflight && !inflight -> last_tx )
4647
+ if (inflight && !inflight -> last_tx ) {
4648
+ status_debug ("Reestablish with an inflight but missing"
4649
+ " last_tx, will send next_funding %s" ,
4650
+ type_to_string (tmpctx ,
4651
+ struct bitcoin_txid ,
4652
+ & inflight -> outpoint .txid ));
4642
4653
send_tlvs -> next_funding = & inflight -> outpoint .txid ;
4654
+ }
4643
4655
4644
4656
/* BOLT-upgrade_protocol #2:
4645
4657
* A node sending `channel_reestablish`, if it supports upgrading channels:
@@ -4772,9 +4784,12 @@ static void peer_reconnect(struct peer *peer,
4772
4784
tal_hex (msg , msg ));
4773
4785
}
4774
4786
4775
- status_debug ("Got reestablish commit=%" PRIu64 " revoke=%" PRIu64 ,
4787
+ status_debug ("Got reestablish commit=%" PRIu64 " revoke=%" PRIu64
4788
+ " inflights: %lu, active splices: %" PRIu32 ,
4776
4789
next_commitment_number ,
4777
- next_revocation_number );
4790
+ next_revocation_number ,
4791
+ tal_count (peer -> splice_state -> inflights ),
4792
+ peer -> splice_state -> count );
4778
4793
4779
4794
/* BOLT #2:
4780
4795
*
@@ -5079,6 +5094,7 @@ static void peer_reconnect(struct peer *peer,
5079
5094
& peer -> channel -> funding .txid ));
5080
5095
}
5081
5096
else {
5097
+ status_info ("Resuming splice negotation" );
5082
5098
resume_splice_negotiation (peer , inflight , false,
5083
5099
inflight -> i_am_initiator
5084
5100
? TX_INITIATOR
0 commit comments