@@ -23,7 +23,7 @@ use secp256k1;
23
23
use chain:: chaininterface:: { BroadcasterInterface , ChainListener , ChainWatchInterface , FeeEstimator } ;
24
24
use chain:: transaction:: OutPoint ;
25
25
use ln:: channel:: { Channel , ChannelError , ChannelKeys } ;
26
- use ln:: channelmonitor:: ManyChannelMonitor ;
26
+ use ln:: channelmonitor:: { ChannelMonitorUpdateErr , ManyChannelMonitor } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
29
29
use ln:: msgs:: { ChannelMessageHandler , HandleError , RAACommitmentOrder } ;
@@ -562,6 +562,33 @@ impl ChannelManager {
562
562
}
563
563
}
564
564
565
+ fn handle_monitor_update_fail ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , channel_id : & [ u8 ; 32 ] , err : ChannelMonitorUpdateErr , reason : RAACommitmentOrder ) {
566
+ match err {
567
+ ChannelMonitorUpdateErr :: PermanentFailure => {
568
+ let mut chan = {
569
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
570
+ let chan = channel_state. by_id . remove ( channel_id) . expect ( "monitor_update_failed must be called within the same lock as the channel get!" ) ;
571
+ if let Some ( short_id) = chan. get_short_channel_id ( ) {
572
+ channel_state. short_to_id . remove ( & short_id) ;
573
+ }
574
+ chan
575
+ } ;
576
+ mem:: drop ( channel_state_lock) ;
577
+ self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
578
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
579
+ if let Ok ( update) = self . get_channel_update ( & chan) {
580
+ events. push ( events:: Event :: BroadcastChannelUpdate {
581
+ msg : update
582
+ } ) ;
583
+ }
584
+ } ,
585
+ ChannelMonitorUpdateErr :: TemporaryFailure => {
586
+ let channel = channel_state_lock. by_id . get_mut ( channel_id) . expect ( "monitor_update_failed must be called within the same lock as the channel get!" ) ;
587
+ channel. monitor_update_failed ( reason) ;
588
+ } ,
589
+ }
590
+ }
591
+
565
592
#[ inline]
566
593
fn gen_rho_mu_from_shared_secret ( shared_secret : & SharedSecret ) -> ( [ u8 ; 32 ] , [ u8 ; 32 ] ) {
567
594
( {
@@ -955,6 +982,11 @@ impl ChannelManager {
955
982
} ;
956
983
if let Some ( ( err, code, chan_update) ) = {
957
984
let chan = channel_state. as_mut ( ) . unwrap ( ) . by_id . get_mut ( & forwarding_id) . unwrap ( ) ;
985
+ // Note that we could technically not return an error yet here and just hope
986
+ // that the connection is reestablished or monitor updated by the time we get
987
+ // around to doing the actual forward, but better to fail early if we can and
988
+ // hopefully an attacker trying to path-trace payments cannot make this occur
989
+ // on a small/per-node/per-channel scale.
958
990
if !chan. is_live ( ) {
959
991
Some ( ( "Forwarding channel is not in a ready state." , 0x1000 | 7 , self . get_channel_update ( chan) . unwrap ( ) ) )
960
992
} else {
@@ -979,6 +1011,7 @@ impl ChannelManager {
979
1011
}
980
1012
981
1013
/// only fails if the channel does not yet have an assigned short_id
1014
+ /// May be called with channel_state already locked!
982
1015
fn get_channel_update ( & self , chan : & Channel ) -> Result < msgs:: ChannelUpdate , HandleError > {
983
1016
let short_channel_id = match chan. get_short_channel_id ( ) {
984
1017
None => return Err ( HandleError { err : "Channel not yet established" , action : None } ) ,
@@ -1049,29 +1082,35 @@ impl ChannelManager {
1049
1082
let onion_packet = ChannelManager :: construct_onion_packet ( onion_payloads, onion_keys, & payment_hash) ;
1050
1083
1051
1084
let ( first_hop_node_id, update_add, commitment_signed) = {
1052
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1053
- let channel_state = channel_state_lock. borrow_parts ( ) ;
1085
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1054
1086
1055
1087
let id = match channel_state. short_to_id . get ( & route. hops . first ( ) . unwrap ( ) . short_channel_id ) {
1056
1088
None => return Err ( APIError :: ChannelUnavailable { err : "No channel available with first hop!" } ) ,
1057
1089
Some ( id) => id. clone ( ) ,
1058
1090
} ;
1059
1091
1060
1092
let res = {
1061
- let chan = channel_state. by_id . get_mut ( & id) . unwrap ( ) ;
1062
- if chan. get_their_node_id ( ) != route. hops . first ( ) . unwrap ( ) . pubkey {
1063
- return Err ( APIError :: RouteError { err : "Node ID mismatch on first hop!" } ) ;
1064
- }
1065
- if !chan. is_live ( ) {
1066
- return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected!" } ) ;
1067
- }
1068
- match chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1069
- route : route. clone ( ) ,
1070
- session_priv : session_priv. clone ( ) ,
1071
- } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ? {
1093
+ let res = {
1094
+ let chan = channel_state. by_id . get_mut ( & id) . unwrap ( ) ;
1095
+ if chan. get_their_node_id ( ) != route. hops . first ( ) . unwrap ( ) . pubkey {
1096
+ return Err ( APIError :: RouteError { err : "Node ID mismatch on first hop!" } ) ;
1097
+ }
1098
+ if chan. is_awaiting_monitor_update ( ) {
1099
+ return Err ( APIError :: MonitorUpdateFailed ) ;
1100
+ }
1101
+ if !chan. is_live ( ) {
1102
+ return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected!" } ) ;
1103
+ }
1104
+ chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1105
+ route : route. clone ( ) ,
1106
+ session_priv : session_priv. clone ( ) ,
1107
+ } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ?
1108
+ } ;
1109
+ match res {
1072
1110
Some ( ( update_add, commitment_signed, chan_monitor) ) => {
1073
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1074
- unimplemented ! ( ) ;
1111
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1112
+ self . handle_monitor_update_fail ( channel_state, & id, e, RAACommitmentOrder :: CommitmentFirst ) ;
1113
+ return Err ( APIError :: MonitorUpdateFailed ) ;
1075
1114
}
1076
1115
Some ( ( update_add, commitment_signed) )
1077
1116
} ,
@@ -1479,7 +1518,81 @@ impl ChannelManager {
1479
1518
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1480
1519
/// operation.
1481
1520
pub fn test_restore_channel_monitor ( & self ) {
1482
- unimplemented ! ( ) ;
1521
+ let mut new_events = Vec :: new ( ) ;
1522
+ let mut close_results = Vec :: new ( ) ;
1523
+ let mut htlc_forwards = Vec :: new ( ) ;
1524
+ let mut htlc_failures = Vec :: new ( ) ;
1525
+
1526
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1527
+ let channel_state = channel_lock. borrow_parts ( ) ;
1528
+ let short_to_id = channel_state. short_to_id ;
1529
+ channel_state. by_id . retain ( |_, channel| {
1530
+ if channel. is_awaiting_monitor_update ( ) {
1531
+ let chan_monitor = channel. channel_monitor ( ) ;
1532
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1533
+ match e {
1534
+ ChannelMonitorUpdateErr :: PermanentFailure => {
1535
+ if let Some ( short_id) = channel. get_short_channel_id ( ) {
1536
+ short_to_id. remove ( & short_id) ;
1537
+ }
1538
+ close_results. push ( channel. force_shutdown ( ) ) ;
1539
+ if let Ok ( update) = self . get_channel_update ( & channel) {
1540
+ new_events. push ( events:: Event :: BroadcastChannelUpdate {
1541
+ msg : update
1542
+ } ) ;
1543
+ }
1544
+ false
1545
+ } ,
1546
+ ChannelMonitorUpdateErr :: TemporaryFailure => true ,
1547
+ }
1548
+ } else {
1549
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures) = channel. monitor_updating_restored ( ) ;
1550
+ if !pending_forwards. is_empty ( ) {
1551
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
1552
+ }
1553
+ htlc_failures. append ( & mut pending_failures) ;
1554
+
1555
+ macro_rules! handle_cs { ( ) => {
1556
+ if let Some ( update) = commitment_update {
1557
+ new_events. push( events:: Event :: UpdateHTLCs {
1558
+ node_id: channel. get_their_node_id( ) ,
1559
+ updates: update,
1560
+ } ) ;
1561
+ }
1562
+ } }
1563
+ macro_rules! handle_raa { ( ) => {
1564
+ if let Some ( revoke_and_ack) = raa {
1565
+ new_events. push( events:: Event :: SendRevokeAndACK {
1566
+ node_id: channel. get_their_node_id( ) ,
1567
+ msg: revoke_and_ack,
1568
+ } ) ;
1569
+ }
1570
+ } }
1571
+ match order {
1572
+ RAACommitmentOrder :: CommitmentFirst => {
1573
+ handle_cs ! ( ) ;
1574
+ handle_raa ! ( ) ;
1575
+ } ,
1576
+ RAACommitmentOrder :: RevokeAndACKFirst => {
1577
+ handle_raa ! ( ) ;
1578
+ handle_cs ! ( ) ;
1579
+ } ,
1580
+ }
1581
+ true
1582
+ }
1583
+ } else { true }
1584
+ } ) ;
1585
+
1586
+ for failure in htlc_failures. drain ( ..) {
1587
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1588
+ }
1589
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1590
+
1591
+ for res in close_results. drain ( ..) {
1592
+ self . finish_force_close_channel ( res) ;
1593
+ }
1594
+
1595
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut new_events) ;
1483
1596
}
1484
1597
1485
1598
fn internal_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < msgs:: AcceptChannel , MsgHandleErrInternal > {
@@ -2010,6 +2123,9 @@ impl ChannelManager {
2010
2123
if !chan. is_outbound ( ) {
2011
2124
return Err ( APIError :: APIMisuseError { err : "update_fee cannot be sent for an inbound channel" } ) ;
2012
2125
}
2126
+ if chan. is_awaiting_monitor_update ( ) {
2127
+ return Err ( APIError :: MonitorUpdateFailed ) ;
2128
+ }
2013
2129
if !chan. is_live ( ) {
2014
2130
return Err ( APIError :: ChannelUnavailable { err : "Channel is either not yet fully established or peer is currently disconnected" } ) ;
2015
2131
}
0 commit comments