@@ -442,6 +442,43 @@ macro_rules! try_chan_entry {
442
442
}
443
443
}
444
444
445
+ macro_rules! return_monitor_err {
446
+ ( $self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path) => {
447
+ return_monitor_err!( $self, $err, $channel_state, $entry, $action_type, Vec :: new( ) , Vec :: new( ) )
448
+ } ;
449
+ ( $self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $raa_first_dropped_cs: expr) => {
450
+ if $action_type != RAACommitmentOrder :: RevokeAndACKFirst { panic!( "Bad return_monitor_err call!" ) ; }
451
+ return_monitor_err!( $self, $err, $channel_state, $entry, $action_type, Vec :: new( ) , Vec :: new( ) , $raa_first_dropped_cs)
452
+ } ;
453
+ ( $self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $failed_forwards: expr, $failed_fails: expr) => {
454
+ return_monitor_err!( $self, $err, $channel_state, $entry, $action_type, $failed_forwards, $failed_fails, false )
455
+ } ;
456
+ ( $self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $failed_forwards: expr, $failed_fails: expr, $raa_first_dropped_cs: expr) => {
457
+ match $err {
458
+ ChannelMonitorUpdateErr :: PermanentFailure => {
459
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
460
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
461
+ $channel_state. short_to_id. remove( & short_id) ;
462
+ }
463
+ // TODO: $failed_fails is dropped here, which will cause other channels to hit the
464
+ // chain in a confused state! We need to move them into the ChannelMonitor which
465
+ // will be responsible for failing backwards once things confirm on-chain.
466
+ // It's ok that we drop $failed_forwards here - at this point we'd rather they
467
+ // broadcast HTLC-Timeout and pay the associated fees to get their funds back than
468
+ // us bother trying to claim it just to forward on to another peer. If we're
469
+ // splitting hairs we'd prefer to claim things that went *to us*, but we haven't
470
+ // given up the preimage yet, so might as well just wait until the payment is
471
+ // retried, avoiding the on-chain fees.
472
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
473
+ } ,
474
+ ChannelMonitorUpdateErr :: TemporaryFailure => {
475
+ $entry. get_mut( ) . monitor_update_failed( $action_type, $failed_forwards, $failed_fails, $raa_first_dropped_cs) ;
476
+ return Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( "Failed to update ChannelMonitor" ) , * $entry. key( ) ) ) ;
477
+ } ,
478
+ }
479
+ }
480
+ }
481
+
445
482
// Does not break in case of TemporaryFailure!
446
483
macro_rules! maybe_break_monitor_err {
447
484
( $self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path) => {
@@ -454,7 +491,7 @@ macro_rules! maybe_break_monitor_err {
454
491
break Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
455
492
} ,
456
493
ChannelMonitorUpdateErr :: TemporaryFailure => {
457
- $entry. get_mut( ) . monitor_update_failed( $action_type) ;
494
+ $entry. get_mut( ) . monitor_update_failed( $action_type, Vec :: new ( ) , Vec :: new ( ) , false ) ;
458
495
} ,
459
496
}
460
497
}
@@ -1681,6 +1718,13 @@ impl ChannelManager {
1681
1718
if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1682
1719
match e {
1683
1720
ChannelMonitorUpdateErr :: PermanentFailure => {
1721
+ // TODO: There may be some pending HTLCs that we intended to fail
1722
+ // backwards when a monitor update failed. We should make sure
1723
+ // knowledge of those gets moved into the appropriate in-memory
1724
+ // ChannelMonitor and they get failed backwards once we get
1725
+ // on-chain confirmations.
1726
+ // Note I think #198 addresses this, so once its merged a test
1727
+ // should be written.
1684
1728
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1685
1729
short_to_id. remove ( & short_id) ;
1686
1730
}
@@ -2283,8 +2327,9 @@ impl ChannelManager {
2283
2327
}
2284
2328
let ( revoke_and_ack, commitment_signed, closing_signed, chan_monitor) =
2285
2329
try_chan_entry ! ( self , chan. get_mut( ) . commitment_signed( & msg, & * self . fee_estimator) , channel_state, chan) ;
2286
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
2287
- unimplemented ! ( ) ;
2330
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
2331
+ return_monitor_err ! ( self , e, channel_state, chan, RAACommitmentOrder :: RevokeAndACKFirst , commitment_signed. is_some( ) ) ;
2332
+ //TODO: Rebroadcast closing_signed if present on monitor update restoration
2288
2333
}
2289
2334
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendRevokeAndACK {
2290
2335
node_id : their_node_id. clone ( ) ,
@@ -2360,8 +2405,8 @@ impl ChannelManager {
2360
2405
}
2361
2406
let ( commitment_update, pending_forwards, pending_failures, closing_signed, chan_monitor) =
2362
2407
try_chan_entry ! ( self , chan. get_mut( ) . revoke_and_ack( & msg, & * self . fee_estimator) , channel_state, chan) ;
2363
- if let Err ( _e ) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
2364
- unimplemented ! ( ) ;
2408
+ if let Err ( e ) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
2409
+ return_monitor_err ! ( self , e , channel_state , chan , RAACommitmentOrder :: CommitmentFirst , pending_forwards , pending_failures ) ;
2365
2410
}
2366
2411
if let Some ( updates) = commitment_update {
2367
2412
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: UpdateHTLCs {
@@ -2455,11 +2500,21 @@ impl ChannelManager {
2455
2500
if chan. get ( ) . get_their_node_id ( ) != * their_node_id {
2456
2501
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2457
2502
}
2458
- let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order, shutdown) =
2503
+ let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, mut order, shutdown) =
2459
2504
try_chan_entry ! ( self , chan. get_mut( ) . channel_reestablish( msg) , channel_state, chan) ;
2460
2505
if let Some ( monitor) = channel_monitor {
2461
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2462
- unimplemented ! ( ) ;
2506
+ if let Err ( e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
2507
+ // channel_reestablish doesn't guarantee the order it returns is sensical
2508
+ // for the messages it returns, but if we're setting what messages to
2509
+ // re-transmit on monitor update success, we need to make sure it is sane.
2510
+ if revoke_and_ack. is_none ( ) {
2511
+ order = RAACommitmentOrder :: CommitmentFirst ;
2512
+ }
2513
+ if commitment_update. is_none ( ) {
2514
+ order = RAACommitmentOrder :: RevokeAndACKFirst ;
2515
+ }
2516
+ return_monitor_err ! ( self , e, channel_state, chan, order) ;
2517
+ //TODO: Resend the funding_locked if needed once we get the monitor running again
2463
2518
}
2464
2519
}
2465
2520
if let Some ( msg) = funding_locked {
0 commit comments