@@ -339,6 +339,29 @@ pub enum UpdateFulfillCommitFetch {
339339 DuplicateClaim { } ,
340340}
341341
342+ /// The return value of `revoke_and_ack` on success, primarily updates to other channels or HTLC
343+ /// state.
344+ pub ( super ) struct RAAUpdates {
345+ pub commitment_update : Option < msgs:: CommitmentUpdate > ,
346+ pub accepted_htlcs : Vec < ( PendingHTLCInfo , u64 ) > ,
347+ pub failed_htlcs : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
348+ pub finalized_claimed_htlcs : Vec < HTLCSource > ,
349+ pub monitor_update : ChannelMonitorUpdate ,
350+ pub holding_cell_failed_htlcs : Vec < ( HTLCSource , PaymentHash ) > ,
351+ }
352+
353+ /// The return value of `monitor_updating_restored`
354+ pub ( super ) struct MonitorRestoreUpdates {
355+ pub raa : Option < msgs:: RevokeAndACK > ,
356+ pub commitment_update : Option < msgs:: CommitmentUpdate > ,
357+ pub order : RAACommitmentOrder ,
358+ pub accepted_htlcs : Vec < ( PendingHTLCInfo , u64 ) > ,
359+ pub failed_htlcs : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
360+ pub finalized_claimed_htlcs : Vec < HTLCSource > ,
361+ pub funding_broadcastable : Option < Transaction > ,
362+ pub funding_locked : Option < msgs:: FundingLocked > ,
363+ }
364+
342365/// If the majority of the channels funds are to the fundee and the initiator holds only just
343366/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
344367/// initiator controls the feerate, if they then go to increase the channel fee, they may have no
@@ -406,6 +429,7 @@ pub(super) struct Channel<Signer: Sign> {
406429 monitor_pending_commitment_signed : bool ,
407430 monitor_pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
408431 monitor_pending_failures : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
432+ monitor_pending_finalized_fulfills : Vec < HTLCSource > ,
409433
410434 // pending_update_fee is filled when sending and receiving update_fee.
411435 //
@@ -692,6 +716,7 @@ impl<Signer: Sign> Channel<Signer> {
692716 monitor_pending_commitment_signed : false ,
693717 monitor_pending_forwards : Vec :: new ( ) ,
694718 monitor_pending_failures : Vec :: new ( ) ,
719+ monitor_pending_finalized_fulfills : Vec :: new ( ) ,
695720
696721 #[ cfg( debug_assertions) ]
697722 holder_max_commitment_tx_output : Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
@@ -955,6 +980,7 @@ impl<Signer: Sign> Channel<Signer> {
955980 monitor_pending_commitment_signed : false ,
956981 monitor_pending_forwards : Vec :: new ( ) ,
957982 monitor_pending_failures : Vec :: new ( ) ,
983+ monitor_pending_finalized_fulfills : Vec :: new ( ) ,
958984
959985 #[ cfg( debug_assertions) ]
960986 holder_max_commitment_tx_output : Mutex :: new ( ( msg. push_msat , msg. funding_satoshis * 1000 - msg. push_msat ) ) ,
@@ -2711,7 +2737,7 @@ impl<Signer: Sign> Channel<Signer> {
27112737 /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
27122738 /// generating an appropriate error *after* the channel state has been updated based on the
27132739 /// revoke_and_ack message.
2714- pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Option < msgs :: CommitmentUpdate > , Vec < ( PendingHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , ChannelMonitorUpdate , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError >
2740+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < RAAUpdates , ChannelError >
27152741 where L :: Target : Logger ,
27162742 {
27172743 if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
@@ -2777,6 +2803,7 @@ impl<Signer: Sign> Channel<Signer> {
27772803 log_trace ! ( logger, "Updating HTLCs on receipt of RAA in channel {}..." , log_bytes!( self . channel_id( ) ) ) ;
27782804 let mut to_forward_infos = Vec :: new ( ) ;
27792805 let mut revoked_htlcs = Vec :: new ( ) ;
2806+ let mut finalized_claimed_htlcs = Vec :: new ( ) ;
27802807 let mut update_fail_htlcs = Vec :: new ( ) ;
27812808 let mut update_fail_malformed_htlcs = Vec :: new ( ) ;
27822809 let mut require_commitment = false ;
@@ -2803,6 +2830,7 @@ impl<Signer: Sign> Channel<Signer> {
28032830 if let Some ( reason) = fail_reason. clone ( ) { // We really want take() here, but, again, non-mut ref :(
28042831 revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
28052832 } else {
2833+ finalized_claimed_htlcs. push ( htlc. source . clone ( ) ) ;
28062834 // They fulfilled, so we sent them money
28072835 value_to_self_msat_diff -= htlc. amount_msat as i64 ;
28082836 }
@@ -2899,8 +2927,14 @@ impl<Signer: Sign> Channel<Signer> {
28992927 }
29002928 self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
29012929 self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
2930+ self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
29022931 log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply." , log_bytes!( self . channel_id( ) ) ) ;
2903- return Ok ( ( None , Vec :: new ( ) , Vec :: new ( ) , monitor_update, Vec :: new ( ) ) )
2932+ return Ok ( RAAUpdates {
2933+ commitment_update : None , finalized_claimed_htlcs : Vec :: new ( ) ,
2934+ accepted_htlcs : Vec :: new ( ) , failed_htlcs : Vec :: new ( ) ,
2935+ monitor_update,
2936+ holding_cell_failed_htlcs : Vec :: new ( )
2937+ } ) ;
29042938 }
29052939
29062940 match self . free_holding_cell_htlcs ( logger) ? {
@@ -2919,7 +2953,14 @@ impl<Signer: Sign> Channel<Signer> {
29192953 self . latest_monitor_update_id = monitor_update. update_id ;
29202954 monitor_update. updates . append ( & mut additional_update. updates ) ;
29212955
2922- Ok ( ( Some ( commitment_update) , to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail) )
2956+ Ok ( RAAUpdates {
2957+ commitment_update : Some ( commitment_update) ,
2958+ finalized_claimed_htlcs,
2959+ accepted_htlcs : to_forward_infos,
2960+ failed_htlcs : revoked_htlcs,
2961+ monitor_update,
2962+ holding_cell_failed_htlcs : htlcs_to_fail
2963+ } )
29232964 } ,
29242965 ( None , htlcs_to_fail) => {
29252966 if require_commitment {
@@ -2932,17 +2973,27 @@ impl<Signer: Sign> Channel<Signer> {
29322973
29332974 log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
29342975 log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
2935- Ok ( ( Some ( msgs:: CommitmentUpdate {
2936- update_add_htlcs : Vec :: new ( ) ,
2937- update_fulfill_htlcs : Vec :: new ( ) ,
2938- update_fail_htlcs,
2939- update_fail_malformed_htlcs,
2940- update_fee : None ,
2941- commitment_signed
2942- } ) , to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail) )
2976+ Ok ( RAAUpdates {
2977+ commitment_update : Some ( msgs:: CommitmentUpdate {
2978+ update_add_htlcs : Vec :: new ( ) ,
2979+ update_fulfill_htlcs : Vec :: new ( ) ,
2980+ update_fail_htlcs,
2981+ update_fail_malformed_htlcs,
2982+ update_fee : None ,
2983+ commitment_signed
2984+ } ) ,
2985+ finalized_claimed_htlcs,
2986+ accepted_htlcs : to_forward_infos, failed_htlcs : revoked_htlcs,
2987+ monitor_update, holding_cell_failed_htlcs : htlcs_to_fail
2988+ } )
29432989 } else {
29442990 log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
2945- Ok ( ( None , to_forward_infos, revoked_htlcs, monitor_update, htlcs_to_fail) )
2991+ Ok ( RAAUpdates {
2992+ commitment_update : None ,
2993+ finalized_claimed_htlcs,
2994+ accepted_htlcs : to_forward_infos, failed_htlcs : revoked_htlcs,
2995+ monitor_update, holding_cell_failed_htlcs : htlcs_to_fail
2996+ } )
29462997 }
29472998 }
29482999 }
@@ -3057,18 +3108,23 @@ impl<Signer: Sign> Channel<Signer> {
30573108 /// which failed. The messages which were generated from that call which generated the
30583109 /// monitor update failure must *not* have been sent to the remote end, and must instead
30593110 /// have been dropped. They will be regenerated when monitor_updating_restored is called.
3060- pub fn monitor_update_failed ( & mut self , resend_raa : bool , resend_commitment : bool , mut pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > , mut pending_fails : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ) {
3111+ pub fn monitor_update_failed ( & mut self , resend_raa : bool , resend_commitment : bool ,
3112+ mut pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
3113+ mut pending_fails : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
3114+ mut pending_finalized_claimed_htlcs : Vec < HTLCSource >
3115+ ) {
30613116 self . monitor_pending_revoke_and_ack |= resend_raa;
30623117 self . monitor_pending_commitment_signed |= resend_commitment;
30633118 self . monitor_pending_forwards . append ( & mut pending_forwards) ;
30643119 self . monitor_pending_failures . append ( & mut pending_fails) ;
3120+ self . monitor_pending_finalized_fulfills . append ( & mut pending_finalized_claimed_htlcs) ;
30653121 self . channel_state |= ChannelState :: MonitorUpdateFailed as u32 ;
30663122 }
30673123
30683124 /// Indicates that the latest ChannelMonitor update has been committed by the client
30693125 /// successfully and we should restore normal operation. Returns messages which should be sent
30703126 /// to the remote side.
3071- pub fn monitor_updating_restored < L : Deref > ( & mut self , logger : & L ) -> ( Option < msgs :: RevokeAndACK > , Option < msgs :: CommitmentUpdate > , RAACommitmentOrder , Vec < ( PendingHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , Option < Transaction > , Option < msgs :: FundingLocked > ) where L :: Target : Logger {
3127+ pub fn monitor_updating_restored < L : Deref > ( & mut self , logger : & L ) -> MonitorRestoreUpdates where L :: Target : Logger {
30723128 assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
30733129 self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
30743130
@@ -3091,15 +3147,20 @@ impl<Signer: Sign> Channel<Signer> {
30913147 } )
30923148 } else { None } ;
30933149
3094- let mut forwards = Vec :: new ( ) ;
3095- mem:: swap ( & mut forwards, & mut self . monitor_pending_forwards ) ;
3096- let mut failures = Vec :: new ( ) ;
3097- mem:: swap ( & mut failures, & mut self . monitor_pending_failures ) ;
3150+ let mut accepted_htlcs = Vec :: new ( ) ;
3151+ mem:: swap ( & mut accepted_htlcs, & mut self . monitor_pending_forwards ) ;
3152+ let mut failed_htlcs = Vec :: new ( ) ;
3153+ mem:: swap ( & mut failed_htlcs, & mut self . monitor_pending_failures ) ;
3154+ let mut finalized_claimed_htlcs = Vec :: new ( ) ;
3155+ mem:: swap ( & mut finalized_claimed_htlcs, & mut self . monitor_pending_finalized_fulfills ) ;
30983156
30993157 if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
31003158 self . monitor_pending_revoke_and_ack = false ;
31013159 self . monitor_pending_commitment_signed = false ;
3102- return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures, funding_broadcastable, funding_locked) ;
3160+ return MonitorRestoreUpdates {
3161+ raa : None , commitment_update : None , order : RAACommitmentOrder :: RevokeAndACKFirst ,
3162+ accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, funding_locked
3163+ } ;
31033164 }
31043165
31053166 let raa = if self . monitor_pending_revoke_and_ack {
@@ -3116,7 +3177,9 @@ impl<Signer: Sign> Channel<Signer> {
31163177 log_bytes!( self . channel_id( ) ) , if funding_broadcastable. is_some( ) { "a funding broadcastable, " } else { "" } ,
31173178 if commitment_update. is_some( ) { "a" } else { "no" } , if raa. is_some( ) { "an" } else { "no" } ,
31183179 match order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
3119- ( raa, commitment_update, order, forwards, failures, funding_broadcastable, funding_locked)
3180+ MonitorRestoreUpdates {
3181+ raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, funding_locked
3182+ }
31203183 }
31213184
31223185 pub fn update_fee < F : Deref > ( & mut self , fee_estimator : & F , msg : & msgs:: UpdateFee ) -> Result < ( ) , ChannelError >
@@ -5176,6 +5239,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
51765239 ( 5 , self . config, required) ,
51775240 ( 7 , self . shutdown_scriptpubkey, option) ,
51785241 ( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5242+ ( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
51795243 } ) ;
51805244
51815245 Ok ( ( ) )
@@ -5409,13 +5473,15 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54095473
54105474 let mut announcement_sigs = None ;
54115475 let mut target_closing_feerate_sats_per_kw = None ;
5476+ let mut monitor_pending_finalized_fulfills = Some ( Vec :: new ( ) ) ;
54125477 read_tlv_fields ! ( reader, {
54135478 ( 0 , announcement_sigs, option) ,
54145479 ( 1 , minimum_depth, option) ,
54155480 ( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
54165481 ( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
54175482 ( 7 , shutdown_scriptpubkey, option) ,
54185483 ( 9 , target_closing_feerate_sats_per_kw, option) ,
5484+ ( 11 , monitor_pending_finalized_fulfills, vec_type) ,
54195485 } ) ;
54205486
54215487 let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -5451,6 +5517,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
54515517 monitor_pending_commitment_signed,
54525518 monitor_pending_forwards,
54535519 monitor_pending_failures,
5520+ monitor_pending_finalized_fulfills : monitor_pending_finalized_fulfills. unwrap ( ) ,
54545521
54555522 pending_update_fee,
54565523 holding_cell_update_fee,
@@ -5700,6 +5767,7 @@ mod tests {
57005767 session_priv : SecretKey :: from_slice ( & hex:: decode ( "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ) . unwrap ( ) [ ..] ) . unwrap ( ) ,
57015768 first_hop_htlc_msat : 548 ,
57025769 payment_id : PaymentId ( [ 42 ; 32 ] ) ,
5770+ payment_secret : None ,
57035771 }
57045772 } ) ;
57055773
0 commit comments