@@ -410,17 +410,6 @@ pub enum UpdateFulfillCommitFetch {
410
410
DuplicateClaim { } ,
411
411
}
412
412
413
- /// The return value of `revoke_and_ack` on success, primarily updates to other channels or HTLC
414
- /// state.
415
- pub ( super ) struct RAAUpdates {
416
- pub commitment_update : Option < msgs:: CommitmentUpdate > ,
417
- pub accepted_htlcs : Vec < ( PendingHTLCInfo , u64 ) > ,
418
- pub failed_htlcs : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
419
- pub finalized_claimed_htlcs : Vec < HTLCSource > ,
420
- pub monitor_update : ChannelMonitorUpdate ,
421
- pub holding_cell_failed_htlcs : Vec < ( HTLCSource , PaymentHash ) > ,
422
- }
423
-
424
413
/// The return value of `monitor_updating_restored`
425
414
pub ( super ) struct MonitorRestoreUpdates {
426
415
pub raa : Option < msgs:: RevokeAndACK > ,
@@ -3001,17 +2990,17 @@ impl<Signer: Sign> Channel<Signer> {
3001
2990
Ok ( ( ) )
3002
2991
}
3003
2992
3004
- pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < ( msgs :: RevokeAndACK , Option < msgs :: CommitmentSigned > , ChannelMonitorUpdate ) , ( Option < ChannelMonitorUpdate > , ChannelError ) >
2993
+ pub fn commitment_signed < L : Deref > ( & mut self , msg : & msgs:: CommitmentSigned , logger : & L ) -> Result < & ChannelMonitorUpdate , ChannelError >
3005
2994
where L :: Target : Logger
3006
2995
{
3007
2996
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
3008
- return Err ( ( None , ChannelError :: Close ( "Got commitment signed message when channel was not in an operational state" . to_owned ( ) ) ) ) ;
2997
+ return Err ( ChannelError :: Close ( "Got commitment signed message when channel was not in an operational state" . to_owned ( ) ) ) ;
3009
2998
}
3010
2999
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == ChannelState :: PeerDisconnected as u32 {
3011
- return Err ( ( None , ChannelError :: Close ( "Peer sent commitment_signed when we needed a channel_reestablish" . to_owned ( ) ) ) ) ;
3000
+ return Err ( ChannelError :: Close ( "Peer sent commitment_signed when we needed a channel_reestablish" . to_owned ( ) ) ) ;
3012
3001
}
3013
3002
if self . channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self . last_sent_closing_fee . is_some ( ) {
3014
- return Err ( ( None , ChannelError :: Close ( "Peer sent commitment_signed after we'd started exchanging closing_signeds" . to_owned ( ) ) ) ) ;
3003
+ return Err ( ChannelError :: Close ( "Peer sent commitment_signed after we'd started exchanging closing_signeds" . to_owned ( ) ) ) ;
3015
3004
}
3016
3005
3017
3006
let funding_script = self . get_funding_redeemscript ( ) ;
@@ -3029,7 +3018,7 @@ impl<Signer: Sign> Channel<Signer> {
3029
3018
log_bytes!( self . counterparty_funding_pubkey( ) . serialize( ) ) , encode:: serialize_hex( & bitcoin_tx. transaction) ,
3030
3019
log_bytes!( sighash[ ..] ) , encode:: serialize_hex( & funding_script) , log_bytes!( self . channel_id( ) ) ) ;
3031
3020
if let Err ( _) = self . secp_ctx . verify_ecdsa ( & sighash, & msg. signature , & self . counterparty_funding_pubkey ( ) ) {
3032
- return Err ( ( None , ChannelError :: Close ( "Invalid commitment tx signature from peer" . to_owned ( ) ) ) ) ;
3021
+ return Err ( ChannelError :: Close ( "Invalid commitment tx signature from peer" . to_owned ( ) ) ) ;
3033
3022
}
3034
3023
bitcoin_tx. txid
3035
3024
} ;
@@ -3044,7 +3033,7 @@ impl<Signer: Sign> Channel<Signer> {
3044
3033
debug_assert ! ( !self . is_outbound( ) ) ;
3045
3034
let counterparty_reserve_we_require_msat = self . holder_selected_channel_reserve_satoshis * 1000 ;
3046
3035
if commitment_stats. remote_balance_msat < commitment_stats. total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
3047
- return Err ( ( None , ChannelError :: Close ( "Funding remote cannot afford proposed new fee" . to_owned ( ) ) ) ) ;
3036
+ return Err ( ChannelError :: Close ( "Funding remote cannot afford proposed new fee" . to_owned ( ) ) ) ;
3048
3037
}
3049
3038
}
3050
3039
#[ cfg( any( test, fuzzing) ) ]
@@ -3066,7 +3055,7 @@ impl<Signer: Sign> Channel<Signer> {
3066
3055
}
3067
3056
3068
3057
if msg. htlc_signatures . len ( ) != commitment_stats. num_nondust_htlcs {
3069
- return Err ( ( None , ChannelError :: Close ( format ! ( "Got wrong number of HTLC signatures ({}) from remote. It must be {}" , msg. htlc_signatures. len( ) , commitment_stats. num_nondust_htlcs) ) ) ) ;
3058
+ return Err ( ChannelError :: Close ( format ! ( "Got wrong number of HTLC signatures ({}) from remote. It must be {}" , msg. htlc_signatures. len( ) , commitment_stats. num_nondust_htlcs) ) ) ;
3070
3059
}
3071
3060
3072
3061
// TODO: Sadly, we pass HTLCs twice to ChannelMonitor: once via the HolderCommitmentTransaction and once via the update
@@ -3084,7 +3073,7 @@ impl<Signer: Sign> Channel<Signer> {
3084
3073
log_bytes!( msg. htlc_signatures[ idx] . serialize_compact( ) [ ..] ) , log_bytes!( keys. countersignatory_htlc_key. serialize( ) ) ,
3085
3074
encode:: serialize_hex( & htlc_tx) , log_bytes!( htlc_sighash[ ..] ) , encode:: serialize_hex( & htlc_redeemscript) , log_bytes!( self . channel_id( ) ) ) ;
3086
3075
if let Err ( _) = self . secp_ctx . verify_ecdsa ( & htlc_sighash, & msg. htlc_signatures [ idx] , & keys. countersignatory_htlc_key ) {
3087
- return Err ( ( None , ChannelError :: Close ( "Invalid HTLC tx signature from peer" . to_owned ( ) ) ) ) ;
3076
+ return Err ( ChannelError :: Close ( "Invalid HTLC tx signature from peer" . to_owned ( ) ) ) ;
3088
3077
}
3089
3078
htlcs_and_sigs. push ( ( htlc, Some ( msg. htlc_signatures [ idx] ) , source) ) ;
3090
3079
} else {
@@ -3100,10 +3089,8 @@ impl<Signer: Sign> Channel<Signer> {
3100
3089
self . counterparty_funding_pubkey ( )
3101
3090
) ;
3102
3091
3103
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number - 1 , & self . secp_ctx ) ;
3104
3092
self . holder_signer . validate_holder_commitment ( & holder_commitment_tx, commitment_stats. preimages )
3105
- . map_err ( |_| ( None , ChannelError :: Close ( "Failed to validate our commitment" . to_owned ( ) ) ) ) ?;
3106
- let per_commitment_secret = self . holder_signer . release_commitment_secret ( self . cur_holder_commitment_transaction_number + 1 ) ;
3093
+ . map_err ( |_| ChannelError :: Close ( "Failed to validate our commitment" . to_owned ( ) ) ) ?;
3107
3094
3108
3095
// Update state now that we've passed all the can-fail calls...
3109
3096
let mut need_commitment = false ;
@@ -3148,7 +3135,7 @@ impl<Signer: Sign> Channel<Signer> {
3148
3135
3149
3136
self . cur_holder_commitment_transaction_number -= 1 ;
3150
3137
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
3151
- // send_commitment_no_status_check () next which will reset this to RAAFirst.
3138
+ // build_commitment_no_status_check () next which will reset this to RAAFirst.
3152
3139
self . resend_order = RAACommitmentOrder :: CommitmentFirst ;
3153
3140
3154
3141
if ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 ) != 0 {
@@ -3160,37 +3147,35 @@ impl<Signer: Sign> Channel<Signer> {
3160
3147
// the corresponding HTLC status updates so that get_last_commitment_update
3161
3148
// includes the right HTLCs.
3162
3149
self . monitor_pending_commitment_signed = true ;
3163
- let ( _ , mut additional_update) = self . send_commitment_no_status_check ( logger) . map_err ( |e| ( None , e ) ) ? ;
3164
- // send_commitment_no_status_check may bump latest_monitor_id but we want them to be
3150
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3151
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
3165
3152
// strictly increasing by one, so decrement it here.
3166
3153
self . latest_monitor_update_id = monitor_update. update_id ;
3167
3154
monitor_update. updates . append ( & mut additional_update. updates ) ;
3168
3155
}
3169
3156
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply." ,
3170
3157
log_bytes!( self . channel_id) ) ;
3171
- return Err ( ( Some ( monitor_update) , ChannelError :: Ignore ( "Previous monitor update failure prevented generation of RAA" . to_owned ( ) ) ) ) ;
3158
+ self . pending_monitor_updates . push ( monitor_update) ;
3159
+ return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3172
3160
}
3173
3161
3174
- let commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
3162
+ let need_commitment_signed = if need_commitment && ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) ) == 0 {
3175
3163
// If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
3176
3164
// we'll send one right away when we get the revoke_and_ack when we
3177
3165
// free_holding_cell_htlcs().
3178
- let ( msg , mut additional_update) = self . send_commitment_no_status_check ( logger) . map_err ( |e| ( None , e ) ) ? ;
3179
- // send_commitment_no_status_check may bump latest_monitor_id but we want them to be
3166
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3167
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
3180
3168
// strictly increasing by one, so decrement it here.
3181
3169
self . latest_monitor_update_id = monitor_update. update_id ;
3182
3170
monitor_update. updates . append ( & mut additional_update. updates ) ;
3183
- Some ( msg )
3184
- } else { None } ;
3171
+ true
3172
+ } else { false } ;
3185
3173
3186
3174
log_debug ! ( logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack." ,
3187
- log_bytes!( self . channel_id( ) ) , if commitment_signed. is_some( ) { " our own commitment_signed and" } else { "" } ) ;
3188
-
3189
- Ok ( ( msgs:: RevokeAndACK {
3190
- channel_id : self . channel_id ,
3191
- per_commitment_secret,
3192
- next_per_commitment_point,
3193
- } , commitment_signed, monitor_update) )
3175
+ log_bytes!( self . channel_id( ) ) , if need_commitment_signed { " our own commitment_signed and" } else { "" } ) ;
3176
+ self . pending_monitor_updates . push ( monitor_update) ;
3177
+ self . monitor_updating_paused ( true , need_commitment_signed, false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3178
+ return Ok ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) ;
3194
3179
}
3195
3180
3196
3181
/// Public version of the below, checking relevant preconditions first.
@@ -3322,7 +3307,7 @@ impl<Signer: Sign> Channel<Signer> {
3322
3307
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
3323
3308
/// generating an appropriate error *after* the channel state has been updated based on the
3324
3309
/// revoke_and_ack message.
3325
- pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < RAAUpdates , ChannelError >
3310
+ pub fn revoke_and_ack < L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , logger : & L ) -> Result < ( Vec < ( HTLCSource , PaymentHash ) > , & ChannelMonitorUpdate ) , ChannelError >
3326
3311
where L :: Target : Logger ,
3327
3312
{
3328
3313
if ( self . channel_state & ( ChannelState :: ChannelReady as u32 ) ) != ( ChannelState :: ChannelReady as u32 ) {
@@ -3509,8 +3494,8 @@ impl<Signer: Sign> Channel<Signer> {
3509
3494
// When the monitor updating is restored we'll call get_last_commitment_update(),
3510
3495
// which does not update state, but we're definitely now awaiting a remote revoke
3511
3496
// before we can step forward any more, so set it here.
3512
- let ( _ , mut additional_update) = self . send_commitment_no_status_check ( logger) ? ;
3513
- // send_commitment_no_status_check may bump latest_monitor_id but we want them to be
3497
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3498
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
3514
3499
// strictly increasing by one, so decrement it here.
3515
3500
self . latest_monitor_update_id = monitor_update. update_id ;
3516
3501
monitor_update. updates . append ( & mut additional_update. updates ) ;
@@ -3519,12 +3504,8 @@ impl<Signer: Sign> Channel<Signer> {
3519
3504
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
3520
3505
self . monitor_pending_finalized_fulfills . append ( & mut finalized_claimed_htlcs) ;
3521
3506
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} but awaiting a monitor update resolution to reply." , log_bytes!( self . channel_id( ) ) ) ;
3522
- return Ok ( RAAUpdates {
3523
- commitment_update : None , finalized_claimed_htlcs : Vec :: new ( ) ,
3524
- accepted_htlcs : Vec :: new ( ) , failed_htlcs : Vec :: new ( ) ,
3525
- monitor_update,
3526
- holding_cell_failed_htlcs : Vec :: new ( )
3527
- } ) ;
3507
+ self . pending_monitor_updates . push ( monitor_update) ;
3508
+ return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3528
3509
}
3529
3510
3530
3511
match self . free_holding_cell_htlcs ( logger) ? {
@@ -3543,47 +3524,29 @@ impl<Signer: Sign> Channel<Signer> {
3543
3524
self . latest_monitor_update_id = monitor_update. update_id ;
3544
3525
monitor_update. updates . append ( & mut additional_update. updates ) ;
3545
3526
3546
- Ok ( RAAUpdates {
3547
- commitment_update : Some ( commitment_update) ,
3548
- finalized_claimed_htlcs,
3549
- accepted_htlcs : to_forward_infos,
3550
- failed_htlcs : revoked_htlcs,
3551
- monitor_update,
3552
- holding_cell_failed_htlcs : htlcs_to_fail
3553
- } )
3527
+ self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3528
+ self . pending_monitor_updates . push ( monitor_update) ;
3529
+ Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3554
3530
} ,
3555
3531
( None , htlcs_to_fail) => {
3556
3532
if require_commitment {
3557
- let ( commitment_signed , mut additional_update) = self . send_commitment_no_status_check ( logger) ? ;
3533
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3558
3534
3559
- // send_commitment_no_status_check may bump latest_monitor_id but we want them to be
3535
+ // build_commitment_no_status_check may bump latest_monitor_id but we want them to be
3560
3536
// strictly increasing by one, so decrement it here.
3561
3537
self . latest_monitor_update_id = monitor_update. update_id ;
3562
3538
monitor_update. updates . append ( & mut additional_update. updates ) ;
3563
3539
3564
3540
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {}. Responding with a commitment update with {} HTLCs failed." ,
3565
3541
log_bytes!( self . channel_id( ) ) , update_fail_htlcs. len( ) + update_fail_malformed_htlcs. len( ) ) ;
3566
- Ok ( RAAUpdates {
3567
- commitment_update : Some ( msgs:: CommitmentUpdate {
3568
- update_add_htlcs : Vec :: new ( ) ,
3569
- update_fulfill_htlcs : Vec :: new ( ) ,
3570
- update_fail_htlcs,
3571
- update_fail_malformed_htlcs,
3572
- update_fee : None ,
3573
- commitment_signed
3574
- } ) ,
3575
- finalized_claimed_htlcs,
3576
- accepted_htlcs : to_forward_infos, failed_htlcs : revoked_htlcs,
3577
- monitor_update, holding_cell_failed_htlcs : htlcs_to_fail
3578
- } )
3542
+ self . monitor_updating_paused ( false , true , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3543
+ self . pending_monitor_updates . push ( monitor_update) ;
3544
+ Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3579
3545
} else {
3580
3546
log_debug ! ( logger, "Received a valid revoke_and_ack for channel {} with no reply necessary." , log_bytes!( self . channel_id( ) ) ) ;
3581
- Ok ( RAAUpdates {
3582
- commitment_update : None ,
3583
- finalized_claimed_htlcs,
3584
- accepted_htlcs : to_forward_infos, failed_htlcs : revoked_htlcs,
3585
- monitor_update, holding_cell_failed_htlcs : htlcs_to_fail
3586
- } )
3547
+ self . monitor_updating_paused ( false , false , false , to_forward_infos, revoked_htlcs, finalized_claimed_htlcs) ;
3548
+ self . pending_monitor_updates . push ( monitor_update) ;
3549
+ Ok ( ( htlcs_to_fail, self . pending_monitor_updates . last ( ) . unwrap ( ) ) )
3587
3550
}
3588
3551
}
3589
3552
}
@@ -3763,6 +3726,7 @@ impl<Signer: Sign> Channel<Signer> {
3763
3726
pub fn monitor_updating_restored < L : Deref > ( & mut self , logger : & L , node_pk : PublicKey , genesis_block_hash : BlockHash , best_block_height : u32 ) -> MonitorRestoreUpdates where L :: Target : Logger {
3764
3727
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , ChannelState :: MonitorUpdateInProgress as u32 ) ;
3765
3728
self . channel_state &= !( ChannelState :: MonitorUpdateInProgress as u32 ) ;
3729
+ self . pending_monitor_updates . clear ( ) ;
3766
3730
3767
3731
// If we're past (or at) the FundingSent stage on an outbound channel, try to
3768
3732
// (re-)broadcast the funding transaction as we may have declined to broadcast it when we
0 commit comments