@@ -2328,6 +2328,26 @@ impl Channel {
2328
2328
}
2329
2329
}
2330
2330
2331
+ fn propose_first_closing_signed ( & mut self , fee_estimator : & FeeEstimator ) -> msgs:: ClosingSigned {
2332
+ let mut proposed_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
2333
+ if self . feerate_per_kw > proposed_feerate {
2334
+ proposed_feerate = self . feerate_per_kw ;
2335
+ }
2336
+ let tx_weight = Self :: get_closing_transaction_weight ( & self . get_closing_scriptpubkey ( ) , self . their_shutdown_scriptpubkey . as_ref ( ) . unwrap ( ) ) ;
2337
+ let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000 ;
2338
+
2339
+ let ( closing_tx, total_fee_satoshis) = self . build_closing_transaction ( proposed_total_fee_satoshis, false ) ;
2340
+ let funding_redeemscript = self . get_funding_redeemscript ( ) ;
2341
+ let sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & closing_tx) . sighash_all ( & closing_tx. input [ 0 ] , & funding_redeemscript, self . channel_value_satoshis ) [ ..] ) . unwrap ( ) ;
2342
+
2343
+ self . last_sent_closing_fee = Some ( ( proposed_feerate, total_fee_satoshis) ) ;
2344
+ msgs:: ClosingSigned {
2345
+ channel_id : self . channel_id ,
2346
+ fee_satoshis : total_fee_satoshis,
2347
+ signature : self . secp_ctx . sign ( & sighash, & self . local_keys . funding_key ) ,
2348
+ }
2349
+ }
2350
+
2331
2351
pub fn shutdown ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: Shutdown ) -> Result < ( Option < msgs:: Shutdown > , Option < msgs:: ClosingSigned > , Vec < ( HTLCSource , [ u8 ; 32 ] ) > ) , ChannelError > {
2332
2352
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
2333
2353
return Err ( ChannelError :: Close ( "Peer sent shutdown when we needed a channel_reestablish" ) ) ;
@@ -2367,23 +2387,6 @@ impl Channel {
2367
2387
self . their_shutdown_scriptpubkey = Some ( msg. scriptpubkey . clone ( ) ) ;
2368
2388
}
2369
2389
2370
- let our_closing_script = self . get_closing_scriptpubkey ( ) ;
2371
-
2372
- let ( proposed_feerate, proposed_fee, our_sig) = if self . channel_outbound && self . pending_inbound_htlcs . is_empty ( ) && self . pending_outbound_htlcs . is_empty ( ) {
2373
- let mut proposed_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
2374
- if self . feerate_per_kw > proposed_feerate {
2375
- proposed_feerate = self . feerate_per_kw ;
2376
- }
2377
- let tx_weight = Self :: get_closing_transaction_weight ( & our_closing_script, & msg. scriptpubkey ) ;
2378
- let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000 ;
2379
-
2380
- let ( closing_tx, total_fee_satoshis) = self . build_closing_transaction ( proposed_total_fee_satoshis, false ) ;
2381
- let funding_redeemscript = self . get_funding_redeemscript ( ) ;
2382
- let sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & closing_tx) . sighash_all ( & closing_tx. input [ 0 ] , & funding_redeemscript, self . channel_value_satoshis ) [ ..] ) . unwrap ( ) ;
2383
-
2384
- ( Some ( proposed_feerate) , Some ( total_fee_satoshis) , Some ( self . secp_ctx . sign ( & sighash, & self . local_keys . funding_key ) ) )
2385
- } else { ( None , None , None ) } ;
2386
-
2387
2390
// From here on out, we may not fail!
2388
2391
2389
2392
self . channel_state |= ChannelState :: RemoteShutdownSent as u32 ;
@@ -2413,21 +2416,16 @@ impl Channel {
2413
2416
} else {
2414
2417
Some ( msgs:: Shutdown {
2415
2418
channel_id : self . channel_id ,
2416
- scriptpubkey : our_closing_script ,
2419
+ scriptpubkey : self . get_closing_scriptpubkey ( ) ,
2417
2420
} )
2418
2421
} ;
2419
2422
2420
2423
self . channel_state |= ChannelState :: LocalShutdownSent as u32 ;
2421
2424
self . channel_update_count += 1 ;
2422
- if self . pending_inbound_htlcs . is_empty ( ) && self . pending_outbound_htlcs . is_empty ( ) && self . channel_outbound {
2425
+ if self . channel_outbound && self . pending_inbound_htlcs . is_empty ( ) && self . pending_outbound_htlcs . is_empty ( ) {
2423
2426
// There are no more HTLCs and we're the funder, this means we start the closing_signed
2424
2427
// dance with an initial fee proposal!
2425
- self . last_sent_closing_fee = Some ( ( proposed_feerate. unwrap ( ) , proposed_fee. unwrap ( ) ) ) ;
2426
- Ok ( ( our_shutdown, Some ( msgs:: ClosingSigned {
2427
- channel_id : self . channel_id ,
2428
- fee_satoshis : proposed_fee. unwrap ( ) ,
2429
- signature : our_sig. unwrap ( ) ,
2430
- } ) , dropped_outbound_htlcs) )
2428
+ Ok ( ( our_shutdown, Some ( self . propose_first_closing_signed ( fee_estimator) ) , dropped_outbound_htlcs) )
2431
2429
} else {
2432
2430
Ok ( ( our_shutdown, None , dropped_outbound_htlcs) )
2433
2431
}
0 commit comments