@@ -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 , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
26
+ use ln:: channelmonitor:: { ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
29
29
use ln:: msgs:: { ChannelMessageHandler , HandleError , RAACommitmentOrder } ;
@@ -586,6 +586,33 @@ impl ChannelManager {
586
586
}
587
587
}
588
588
589
+ fn handle_monitor_update_fail ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , channel_id : & [ u8 ; 32 ] , err : ChannelMonitorUpdateErr , reason : RAACommitmentOrder ) {
590
+ match err {
591
+ ChannelMonitorUpdateErr :: PermanentFailure => {
592
+ let mut chan = {
593
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
594
+ let chan = channel_state. by_id . remove ( channel_id) . expect ( "monitor_update_failed must be called within the same lock as the channel get!" ) ;
595
+ if let Some ( short_id) = chan. get_short_channel_id ( ) {
596
+ channel_state. short_to_id . remove ( & short_id) ;
597
+ }
598
+ chan
599
+ } ;
600
+ mem:: drop ( channel_state_lock) ;
601
+ self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
602
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
603
+ if let Ok ( update) = self . get_channel_update ( & chan) {
604
+ events. push ( events:: Event :: BroadcastChannelUpdate {
605
+ msg : update
606
+ } ) ;
607
+ }
608
+ } ,
609
+ ChannelMonitorUpdateErr :: TemporaryFailure => {
610
+ 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!" ) ;
611
+ channel. monitor_update_failed ( reason) ;
612
+ } ,
613
+ }
614
+ }
615
+
589
616
#[ inline]
590
617
fn gen_rho_mu_from_shared_secret ( shared_secret : & SharedSecret ) -> ( [ u8 ; 32 ] , [ u8 ; 32 ] ) {
591
618
( {
@@ -984,6 +1011,11 @@ impl ChannelManager {
984
1011
if let Some ( ( err, code, chan_update) ) = loop {
985
1012
let chan = channel_state. as_mut ( ) . unwrap ( ) . by_id . get_mut ( & forwarding_id) . unwrap ( ) ;
986
1013
1014
+ // Note that we could technically not return an error yet here and just hope
1015
+ // that the connection is reestablished or monitor updated by the time we get
1016
+ // around to doing the actual forward, but better to fail early if we can and
1017
+ // hopefully an attacker trying to path-trace payments cannot make this occur
1018
+ // on a small/per-node/per-channel scale.
987
1019
if !chan. is_live ( ) { // channel_disabled
988
1020
break Some ( ( "Forwarding channel is not in a ready state." , 0x1000 | 20 , Some ( self . get_channel_update ( chan) . unwrap ( ) ) ) ) ;
989
1021
}
@@ -1027,6 +1059,7 @@ impl ChannelManager {
1027
1059
}
1028
1060
1029
1061
/// only fails if the channel does not yet have an assigned short_id
1062
+ /// May be called with channel_state already locked!
1030
1063
fn get_channel_update ( & self , chan : & Channel ) -> Result < msgs:: ChannelUpdate , HandleError > {
1031
1064
let short_channel_id = match chan. get_short_channel_id ( ) {
1032
1065
None => return Err ( HandleError { err : "Channel not yet established" , action : None } ) ,
@@ -1097,30 +1130,36 @@ impl ChannelManager {
1097
1130
let onion_packet = ChannelManager :: construct_onion_packet ( onion_payloads, onion_keys, & payment_hash) ;
1098
1131
1099
1132
let ( first_hop_node_id, update_add, commitment_signed) = {
1100
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1101
- let channel_state = channel_state_lock. borrow_parts ( ) ;
1133
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1102
1134
1103
1135
let id = match channel_state. short_to_id . get ( & route. hops . first ( ) . unwrap ( ) . short_channel_id ) {
1104
1136
None => return Err ( APIError :: ChannelUnavailable { err : "No channel available with first hop!" } ) ,
1105
1137
Some ( id) => id. clone ( ) ,
1106
1138
} ;
1107
1139
1108
1140
let res = {
1109
- let chan = channel_state. by_id . get_mut ( & id) . unwrap ( ) ;
1110
- if chan. get_their_node_id ( ) != route. hops . first ( ) . unwrap ( ) . pubkey {
1111
- return Err ( APIError :: RouteError { err : "Node ID mismatch on first hop!" } ) ;
1112
- }
1113
- if !chan. is_live ( ) {
1114
- return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected!" } ) ;
1115
- }
1116
- match chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1117
- route : route. clone ( ) ,
1118
- session_priv : session_priv. clone ( ) ,
1119
- first_hop_htlc_msat : htlc_msat,
1120
- } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ? {
1141
+ let res = {
1142
+ let chan = channel_state. by_id . get_mut ( & id) . unwrap ( ) ;
1143
+ if chan. get_their_node_id ( ) != route. hops . first ( ) . unwrap ( ) . pubkey {
1144
+ return Err ( APIError :: RouteError { err : "Node ID mismatch on first hop!" } ) ;
1145
+ }
1146
+ if chan. is_awaiting_monitor_update ( ) {
1147
+ return Err ( APIError :: MonitorUpdateFailed ) ;
1148
+ }
1149
+ if !chan. is_live ( ) {
1150
+ return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected!" } ) ;
1151
+ }
1152
+ chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1153
+ route : route. clone ( ) ,
1154
+ session_priv : session_priv. clone ( ) ,
1155
+ first_hop_htlc_msat : htlc_msat,
1156
+ } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ?
1157
+ } ;
1158
+ match res {
1121
1159
Some ( ( update_add, commitment_signed, chan_monitor) ) => {
1122
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1123
- unimplemented ! ( ) ;
1160
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1161
+ self . handle_monitor_update_fail ( channel_state, & id, e, RAACommitmentOrder :: CommitmentFirst ) ;
1162
+ return Err ( APIError :: MonitorUpdateFailed ) ;
1124
1163
}
1125
1164
Some ( ( update_add, commitment_signed) )
1126
1165
} ,
@@ -1540,7 +1579,83 @@ impl ChannelManager {
1540
1579
/// ChannelMonitorUpdateErr::TemporaryFailure was returned from a channel monitor update
1541
1580
/// operation.
1542
1581
pub fn test_restore_channel_monitor ( & self ) {
1543
- unimplemented ! ( ) ;
1582
+ let mut new_events = Vec :: new ( ) ;
1583
+ let mut close_results = Vec :: new ( ) ;
1584
+ let mut htlc_forwards = Vec :: new ( ) ;
1585
+ let mut htlc_failures = Vec :: new ( ) ;
1586
+
1587
+ {
1588
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1589
+ let channel_state = channel_lock. borrow_parts ( ) ;
1590
+ let short_to_id = channel_state. short_to_id ;
1591
+ channel_state. by_id . retain ( |_, channel| {
1592
+ if channel. is_awaiting_monitor_update ( ) {
1593
+ let chan_monitor = channel. channel_monitor ( ) ;
1594
+ if let Err ( e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1595
+ match e {
1596
+ ChannelMonitorUpdateErr :: PermanentFailure => {
1597
+ if let Some ( short_id) = channel. get_short_channel_id ( ) {
1598
+ short_to_id. remove ( & short_id) ;
1599
+ }
1600
+ close_results. push ( channel. force_shutdown ( ) ) ;
1601
+ if let Ok ( update) = self . get_channel_update ( & channel) {
1602
+ new_events. push ( events:: Event :: BroadcastChannelUpdate {
1603
+ msg : update
1604
+ } ) ;
1605
+ }
1606
+ false
1607
+ } ,
1608
+ ChannelMonitorUpdateErr :: TemporaryFailure => true ,
1609
+ }
1610
+ } else {
1611
+ let ( raa, commitment_update, order, pending_forwards, mut pending_failures) = channel. monitor_updating_restored ( ) ;
1612
+ if !pending_forwards. is_empty ( ) {
1613
+ htlc_forwards. push ( ( channel. get_short_channel_id ( ) . expect ( "We can't have pending forwards before funding confirmation" ) , pending_forwards) ) ;
1614
+ }
1615
+ htlc_failures. append ( & mut pending_failures) ;
1616
+
1617
+ macro_rules! handle_cs { ( ) => {
1618
+ if let Some ( update) = commitment_update {
1619
+ new_events. push( events:: Event :: UpdateHTLCs {
1620
+ node_id: channel. get_their_node_id( ) ,
1621
+ updates: update,
1622
+ } ) ;
1623
+ }
1624
+ } }
1625
+ macro_rules! handle_raa { ( ) => {
1626
+ if let Some ( revoke_and_ack) = raa {
1627
+ new_events. push( events:: Event :: SendRevokeAndACK {
1628
+ node_id: channel. get_their_node_id( ) ,
1629
+ msg: revoke_and_ack,
1630
+ } ) ;
1631
+ }
1632
+ } }
1633
+ match order {
1634
+ RAACommitmentOrder :: CommitmentFirst => {
1635
+ handle_cs ! ( ) ;
1636
+ handle_raa ! ( ) ;
1637
+ } ,
1638
+ RAACommitmentOrder :: RevokeAndACKFirst => {
1639
+ handle_raa ! ( ) ;
1640
+ handle_cs ! ( ) ;
1641
+ } ,
1642
+ }
1643
+ true
1644
+ }
1645
+ } else { true }
1646
+ } ) ;
1647
+ }
1648
+
1649
+ for failure in htlc_failures. drain ( ..) {
1650
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1651
+ }
1652
+ self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1653
+
1654
+ for res in close_results. drain ( ..) {
1655
+ self . finish_force_close_channel ( res) ;
1656
+ }
1657
+
1658
+ self . pending_events . lock ( ) . unwrap ( ) . append ( & mut new_events) ;
1544
1659
}
1545
1660
1546
1661
fn internal_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < msgs:: AcceptChannel , MsgHandleErrInternal > {
@@ -2211,6 +2326,9 @@ impl ChannelManager {
2211
2326
if !chan. is_outbound ( ) {
2212
2327
return Err ( APIError :: APIMisuseError { err : "update_fee cannot be sent for an inbound channel" } ) ;
2213
2328
}
2329
+ if chan. is_awaiting_monitor_update ( ) {
2330
+ return Err ( APIError :: MonitorUpdateFailed ) ;
2331
+ }
2214
2332
if !chan. is_live ( ) {
2215
2333
return Err ( APIError :: ChannelUnavailable { err : "Channel is either not yet fully established or peer is currently disconnected" } ) ;
2216
2334
}
0 commit comments