@@ -339,6 +339,29 @@ pub enum UpdateFulfillCommitFetch {
339
339
DuplicateClaim { } ,
340
340
}
341
341
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
+
342
365
/// If the majority of the channels funds are to the fundee and the initiator holds only just
343
366
/// enough funds to cover their reserve value, channels are at risk of getting "stuck". Because the
344
367
/// 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> {
406
429
monitor_pending_commitment_signed : bool ,
407
430
monitor_pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
408
431
monitor_pending_failures : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
432
+ monitor_pending_finalized_fulfills : Vec < HTLCSource > ,
409
433
410
434
// pending_update_fee is filled when sending and receiving update_fee.
411
435
//
@@ -692,6 +716,7 @@ impl<Signer: Sign> Channel<Signer> {
692
716
monitor_pending_commitment_signed : false ,
693
717
monitor_pending_forwards : Vec :: new ( ) ,
694
718
monitor_pending_failures : Vec :: new ( ) ,
719
+ monitor_pending_finalized_fulfills : Vec :: new ( ) ,
695
720
696
721
#[ cfg( debug_assertions) ]
697
722
holder_max_commitment_tx_output : Mutex :: new ( ( channel_value_satoshis * 1000 - push_msat, push_msat) ) ,
@@ -955,6 +980,7 @@ impl<Signer: Sign> Channel<Signer> {
955
980
monitor_pending_commitment_signed : false ,
956
981
monitor_pending_forwards : Vec :: new ( ) ,
957
982
monitor_pending_failures : Vec :: new ( ) ,
983
+ monitor_pending_finalized_fulfills : Vec :: new ( ) ,
958
984
959
985
#[ cfg( debug_assertions) ]
960
986
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> {
2711
2737
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
2712
2738
/// generating an appropriate error *after* the channel state has been updated based on the
2713
2739
/// 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 >
2715
2741
where L :: Target : Logger ,
2716
2742
{
2717
2743
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
@@ -2777,6 +2803,7 @@ impl<Signer: Sign> Channel<Signer> {
2777
2803
log_trace ! ( logger, "Updating HTLCs on receipt of RAA in channel {}..." , log_bytes!( self . channel_id( ) ) ) ;
2778
2804
let mut to_forward_infos = Vec :: new ( ) ;
2779
2805
let mut revoked_htlcs = Vec :: new ( ) ;
2806
+ let mut finalized_claimed_htlcs = Vec :: new ( ) ;
2780
2807
let mut update_fail_htlcs = Vec :: new ( ) ;
2781
2808
let mut update_fail_malformed_htlcs = Vec :: new ( ) ;
2782
2809
let mut require_commitment = false ;
@@ -2803,6 +2830,7 @@ impl<Signer: Sign> Channel<Signer> {
2803
2830
if let Some ( reason) = fail_reason. clone ( ) { // We really want take() here, but, again, non-mut ref :(
2804
2831
revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
2805
2832
} else {
2833
+ finalized_claimed_htlcs. push ( htlc. source . clone ( ) ) ;
2806
2834
// They fulfilled, so we sent them money
2807
2835
value_to_self_msat_diff -= htlc. amount_msat as i64 ;
2808
2836
}
@@ -2899,8 +2927,14 @@ impl<Signer: Sign> Channel<Signer> {
2899
2927
}
2900
2928
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2901
2929
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
2930
+ self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
2902
2931
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
+ } ) ;
2904
2938
}
2905
2939
2906
2940
match self . free_holding_cell_htlcs ( logger) ? {
@@ -2919,7 +2953,14 @@ impl<Signer: Sign> Channel<Signer> {
2919
2953
self . latest_monitor_update_id = monitor_update. update_id ;
2920
2954
monitor_update. updates . append ( & mut additional_update. updates ) ;
2921
2955
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
+ } )
2923
2964
} ,
2924
2965
( None , htlcs_to_fail) => {
2925
2966
if require_commitment {
@@ -2932,17 +2973,27 @@ impl<Signer: Sign> Channel<Signer> {
2932
2973
2933
2974
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
2934
2975
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
+ } )
2943
2989
} else {
2944
2990
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
+ } )
2946
2997
}
2947
2998
}
2948
2999
}
@@ -3057,18 +3108,23 @@ impl<Signer: Sign> Channel<Signer> {
3057
3108
/// which failed. The messages which were generated from that call which generated the
3058
3109
/// monitor update failure must *not* have been sent to the remote end, and must instead
3059
3110
/// 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
+ ) {
3061
3116
self . monitor_pending_revoke_and_ack |= resend_raa;
3062
3117
self . monitor_pending_commitment_signed |= resend_commitment;
3063
3118
self . monitor_pending_forwards . append ( & mut pending_forwards) ;
3064
3119
self . monitor_pending_failures . append ( & mut pending_fails) ;
3120
+ self . monitor_pending_finalized_fulfills . append ( & mut pending_finalized_claimed_htlcs) ;
3065
3121
self . channel_state |= ChannelState :: MonitorUpdateFailed as u32 ;
3066
3122
}
3067
3123
3068
3124
/// Indicates that the latest ChannelMonitor update has been committed by the client
3069
3125
/// successfully and we should restore normal operation. Returns messages which should be sent
3070
3126
/// 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 {
3072
3128
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
3073
3129
self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
3074
3130
@@ -3091,15 +3147,20 @@ impl<Signer: Sign> Channel<Signer> {
3091
3147
} )
3092
3148
} else { None } ;
3093
3149
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 ) ;
3098
3156
3099
3157
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
3100
3158
self . monitor_pending_revoke_and_ack = false ;
3101
3159
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
+ } ;
3103
3164
}
3104
3165
3105
3166
let raa = if self . monitor_pending_revoke_and_ack {
@@ -3116,7 +3177,9 @@ impl<Signer: Sign> Channel<Signer> {
3116
3177
log_bytes!( self . channel_id( ) ) , if funding_broadcastable. is_some( ) { "a funding broadcastable, " } else { "" } ,
3117
3178
if commitment_update. is_some( ) { "a" } else { "no" } , if raa. is_some( ) { "an" } else { "no" } ,
3118
3179
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
+ }
3120
3183
}
3121
3184
3122
3185
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> {
5176
5239
( 5 , self . config, required) ,
5177
5240
( 7 , self . shutdown_scriptpubkey, option) ,
5178
5241
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5242
+ ( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
5179
5243
} ) ;
5180
5244
5181
5245
Ok ( ( ) )
@@ -5409,13 +5473,15 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5409
5473
5410
5474
let mut announcement_sigs = None ;
5411
5475
let mut target_closing_feerate_sats_per_kw = None ;
5476
+ let mut monitor_pending_finalized_fulfills = Some ( Vec :: new ( ) ) ;
5412
5477
read_tlv_fields ! ( reader, {
5413
5478
( 0 , announcement_sigs, option) ,
5414
5479
( 1 , minimum_depth, option) ,
5415
5480
( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
5416
5481
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
5417
5482
( 7 , shutdown_scriptpubkey, option) ,
5418
5483
( 9 , target_closing_feerate_sats_per_kw, option) ,
5484
+ ( 11 , monitor_pending_finalized_fulfills, vec_type) ,
5419
5485
} ) ;
5420
5486
5421
5487
let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -5451,6 +5517,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5451
5517
monitor_pending_commitment_signed,
5452
5518
monitor_pending_forwards,
5453
5519
monitor_pending_failures,
5520
+ monitor_pending_finalized_fulfills : monitor_pending_finalized_fulfills. unwrap ( ) ,
5454
5521
5455
5522
pending_update_fee,
5456
5523
holding_cell_update_fee,
@@ -5700,6 +5767,7 @@ mod tests {
5700
5767
session_priv : SecretKey :: from_slice ( & hex:: decode ( "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ) . unwrap ( ) [ ..] ) . unwrap ( ) ,
5701
5768
first_hop_htlc_msat : 548 ,
5702
5769
payment_id : PaymentId ( [ 42 ; 32 ] ) ,
5770
+ payment_secret : None ,
5703
5771
}
5704
5772
} ) ;
5705
5773
0 commit comments