@@ -1048,7 +1048,7 @@ impl ChannelManager {
1048
1048
let ( onion_payloads, htlc_msat, htlc_cltv) = ChannelManager :: build_onion_payloads ( & route, cur_height) ?;
1049
1049
let onion_packet = ChannelManager :: construct_onion_packet ( onion_payloads, onion_keys, & payment_hash) ;
1050
1050
1051
- let ( first_hop_node_id, ( update_add, commitment_signed, chan_monitor ) ) = {
1051
+ let ( first_hop_node_id, update_add, commitment_signed) = {
1052
1052
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1053
1053
let channel_state = channel_state_lock. borrow_parts ( ) ;
1054
1054
@@ -1065,24 +1065,30 @@ impl ChannelManager {
1065
1065
if !chan. is_live ( ) {
1066
1066
return Err ( APIError :: ChannelUnavailable { err : "Peer for first hop currently disconnected!" } ) ;
1067
1067
}
1068
- chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1068
+ match chan. send_htlc_and_commit ( htlc_msat, payment_hash. clone ( ) , htlc_cltv, HTLCSource :: OutboundRoute {
1069
1069
route : route. clone ( ) ,
1070
1070
session_priv : session_priv. clone ( ) ,
1071
- } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ?
1071
+ } , onion_packet) . map_err ( |he| APIError :: ChannelUnavailable { err : he. err } ) ? {
1072
+ 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 ! ( ) ;
1075
+ }
1076
+ Some ( ( update_add, commitment_signed) )
1077
+ } ,
1078
+ None => None ,
1079
+ }
1072
1080
} ;
1073
1081
1074
1082
let first_hop_node_id = route. hops . first ( ) . unwrap ( ) . pubkey ;
1075
1083
1076
1084
match res {
1077
- Some ( msgs) => ( first_hop_node_id, msgs) ,
1085
+ Some ( ( update_add, commitment_signed) ) => {
1086
+ ( first_hop_node_id, update_add, commitment_signed)
1087
+ } ,
1078
1088
None => return Ok ( ( ) ) ,
1079
1089
}
1080
1090
} ;
1081
1091
1082
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1083
- unimplemented ! ( ) ;
1084
- }
1085
-
1086
1092
let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
1087
1093
events. push ( events:: Event :: UpdateHTLCs {
1088
1094
node_id : first_hop_node_id,
@@ -1135,7 +1141,9 @@ impl ChannelManager {
1135
1141
} ,
1136
1142
None => return
1137
1143
}
1138
- } ; // Release channel lock for install_watch_outpoint call,
1144
+ } ;
1145
+ // Because we have exclusive ownership of the channel here we can release the channel_state
1146
+ // lock before add_update_monitor
1139
1147
if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1140
1148
unimplemented ! ( ) ;
1141
1149
}
@@ -1250,7 +1258,10 @@ impl ChannelManager {
1250
1258
continue ;
1251
1259
} ,
1252
1260
} ;
1253
- new_events. push ( ( Some ( monitor) , events:: Event :: UpdateHTLCs {
1261
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1262
+ unimplemented ! ( ) ; // but def dont push the event...
1263
+ }
1264
+ new_events. push ( events:: Event :: UpdateHTLCs {
1254
1265
node_id : forward_chan. get_their_node_id ( ) ,
1255
1266
updates : msgs:: CommitmentUpdate {
1256
1267
update_add_htlcs : add_htlc_msgs,
@@ -1260,7 +1271,7 @@ impl ChannelManager {
1260
1271
update_fee : None ,
1261
1272
commitment_signed : commitment_msg,
1262
1273
} ,
1263
- } ) ) ;
1274
+ } ) ;
1264
1275
}
1265
1276
} else {
1266
1277
for HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info } in pending_forwards. drain ( ..) {
@@ -1273,10 +1284,10 @@ impl ChannelManager {
1273
1284
hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( prev_hop_data) ,
1274
1285
hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ prev_hop_data] ) ; } ,
1275
1286
} ;
1276
- new_events. push ( ( None , events:: Event :: PaymentReceived {
1287
+ new_events. push ( events:: Event :: PaymentReceived {
1277
1288
payment_hash : forward_info. payment_hash ,
1278
1289
amt : forward_info. amt_to_forward ,
1279
- } ) ) ;
1290
+ } ) ;
1280
1291
}
1281
1292
}
1282
1293
}
@@ -1290,21 +1301,8 @@ impl ChannelManager {
1290
1301
}
1291
1302
1292
1303
if new_events. is_empty ( ) { return }
1293
-
1294
- new_events. retain ( |event| {
1295
- if let & Some ( ref monitor) = & event. 0 {
1296
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor. clone ( ) ) {
1297
- unimplemented ! ( ) ; // but def dont push the event...
1298
- }
1299
- }
1300
- true
1301
- } ) ;
1302
-
1303
1304
let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
1304
- events. reserve ( new_events. len ( ) ) ;
1305
- for event in new_events. drain ( ..) {
1306
- events. push ( event. 1 ) ;
1307
- }
1305
+ events. append ( & mut new_events) ;
1308
1306
}
1309
1307
1310
1308
/// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event.
@@ -1355,7 +1353,13 @@ impl ChannelManager {
1355
1353
1356
1354
let chan = channel_state. by_id . get_mut ( & chan_id) . unwrap ( ) ;
1357
1355
match chan. get_update_fail_htlc_and_commit ( htlc_id, err_packet) {
1358
- Ok ( msg) => ( chan. get_their_node_id ( ) , msg) ,
1356
+ Ok ( Some ( ( msg, commitment_msg, chan_monitor) ) ) => {
1357
+ if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1358
+ unimplemented ! ( ) ;
1359
+ }
1360
+ ( chan. get_their_node_id ( ) , Some ( ( msg, commitment_msg) ) )
1361
+ } ,
1362
+ Ok ( None ) => ( chan. get_their_node_id ( ) , None ) ,
1359
1363
Err ( _e) => {
1360
1364
//TODO: Do something with e?
1361
1365
return ;
@@ -1364,13 +1368,9 @@ impl ChannelManager {
1364
1368
} ;
1365
1369
1366
1370
match fail_msgs {
1367
- Some ( ( msg, commitment_msg, chan_monitor ) ) => {
1371
+ Some ( ( msg, commitment_msg) ) => {
1368
1372
mem:: drop ( channel_state) ;
1369
1373
1370
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1371
- unimplemented ! ( ) ; // but def dont push the event...
1372
- }
1373
-
1374
1374
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1375
1375
pending_events. push ( events:: Event :: UpdateHTLCs {
1376
1376
node_id,
@@ -1435,7 +1435,13 @@ impl ChannelManager {
1435
1435
1436
1436
let chan = channel_state. by_id . get_mut ( & chan_id) . unwrap ( ) ;
1437
1437
match chan. get_update_fulfill_htlc_and_commit ( htlc_id, payment_preimage) {
1438
- Ok ( msg) => ( chan. get_their_node_id ( ) , msg) ,
1438
+ Ok ( ( msgs, Some ( chan_monitor) ) ) => {
1439
+ if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1440
+ unimplemented ! ( ) ; // but def dont push the event...
1441
+ }
1442
+ ( chan. get_their_node_id ( ) , msgs)
1443
+ } ,
1444
+ Ok ( ( msgs, None ) ) => ( chan. get_their_node_id ( ) , msgs) ,
1439
1445
Err ( _e) => {
1440
1446
// TODO: There is probably a channel manager somewhere that needs to
1441
1447
// learn the preimage as the channel may be about to hit the chain.
@@ -1446,13 +1452,7 @@ impl ChannelManager {
1446
1452
} ;
1447
1453
1448
1454
mem:: drop ( channel_state) ;
1449
- if let Some ( chan_monitor) = fulfill_msgs. 1 {
1450
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1451
- unimplemented ! ( ) ; // but def dont push the event...
1452
- }
1453
- }
1454
-
1455
- if let Some ( ( msg, commitment_msg) ) = fulfill_msgs. 0 {
1455
+ if let Some ( ( msg, commitment_msg) ) = fulfill_msgs {
1456
1456
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1457
1457
pending_events. push ( events:: Event :: UpdateHTLCs {
1458
1458
node_id : node_id,
@@ -1565,10 +1565,9 @@ impl ChannelManager {
1565
1565
} ,
1566
1566
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. temporary_channel_id ) )
1567
1567
}
1568
- } ; // Release channel lock for install_watch_outpoint call,
1569
- // note that this means if the remote end is misbehaving and sends a message for the same
1570
- // channel back-to-back with funding_created, we'll end up thinking they sent a message
1571
- // for a bogus channel.
1568
+ } ;
1569
+ // Because we have exclusive ownership of the channel here we can release the channel_state
1570
+ // lock before add_update_monitor
1572
1571
if let Err ( _e) = self . monitor . add_update_monitor ( monitor_update. get_funding_txo ( ) . unwrap ( ) , monitor_update) {
1573
1572
unimplemented ! ( ) ;
1574
1573
}
@@ -1585,7 +1584,7 @@ impl ChannelManager {
1585
1584
}
1586
1585
1587
1586
fn internal_funding_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
1588
- let ( funding_txo, user_id, monitor ) = {
1587
+ let ( funding_txo, user_id) = {
1589
1588
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1590
1589
match channel_state. by_id . get_mut ( & msg. channel_id ) {
1591
1590
Some ( chan) => {
@@ -1594,14 +1593,14 @@ impl ChannelManager {
1594
1593
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1595
1594
}
1596
1595
let chan_monitor = chan. funding_signed ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1597
- ( chan. get_funding_txo ( ) . unwrap ( ) , chan. get_user_id ( ) , chan_monitor)
1596
+ if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1597
+ unimplemented ! ( ) ;
1598
+ }
1599
+ ( chan. get_funding_txo ( ) . unwrap ( ) , chan. get_user_id ( ) )
1598
1600
} ,
1599
1601
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1600
1602
}
1601
1603
} ;
1602
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1603
- unimplemented ! ( ) ;
1604
- }
1605
1604
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1606
1605
pending_events. push ( events:: Event :: FundingBroadcastSafe {
1607
1606
funding_txo : funding_txo,
@@ -1841,43 +1840,44 @@ impl ChannelManager {
1841
1840
}
1842
1841
1843
1842
fn internal_commitment_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: CommitmentSigned ) -> Result < ( msgs:: RevokeAndACK , Option < msgs:: CommitmentSigned > ) , MsgHandleErrInternal > {
1844
- let ( revoke_and_ack, commitment_signed, chan_monitor ) = {
1843
+ let ( revoke_and_ack, commitment_signed) = {
1845
1844
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1846
1845
match channel_state. by_id . get_mut ( & msg. channel_id ) {
1847
1846
Some ( chan) => {
1848
1847
if chan. get_their_node_id ( ) != * their_node_id {
1849
1848
//TODO: here and below MsgHandleErrInternal, #153 case
1850
1849
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1851
1850
}
1852
- chan. commitment_signed ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?
1851
+ let ( revoke_and_ack, commitment_signed, chan_monitor) = chan. commitment_signed ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1852
+ if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1853
+ unimplemented ! ( ) ;
1854
+ }
1855
+ ( revoke_and_ack, commitment_signed)
1853
1856
} ,
1854
1857
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1855
1858
}
1856
1859
} ;
1857
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1858
- unimplemented ! ( ) ;
1859
- }
1860
-
1861
1860
Ok ( ( revoke_and_ack, commitment_signed) )
1862
1861
}
1863
1862
1864
1863
fn internal_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < Option < msgs:: CommitmentUpdate > , MsgHandleErrInternal > {
1865
- let ( ( res, mut pending_forwards, mut pending_failures, chan_monitor ) , short_channel_id) = {
1864
+ let ( ( res, mut pending_forwards, mut pending_failures) , short_channel_id) = {
1866
1865
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1867
1866
match channel_state. by_id . get_mut ( & msg. channel_id ) {
1868
1867
Some ( chan) => {
1869
1868
if chan. get_their_node_id ( ) != * their_node_id {
1870
1869
//TODO: here and below MsgHandleErrInternal, #153 case
1871
1870
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1872
1871
}
1873
- ( chan. revoke_and_ack ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?, chan. get_short_channel_id ( ) . expect ( "RAA should only work on a short-id-available channel" ) )
1872
+ let ( res, pending_forwards, pending_failures, chan_monitor) = chan. revoke_and_ack ( & msg) . map_err ( |e| MsgHandleErrInternal :: from_maybe_close ( e) ) ?;
1873
+ if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1874
+ unimplemented ! ( ) ;
1875
+ }
1876
+ ( ( res, pending_forwards, pending_failures) , chan. get_short_channel_id ( ) . expect ( "RAA should only work on a short-id-available channel" ) )
1874
1877
} ,
1875
1878
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1876
1879
}
1877
1880
} ;
1878
- if let Err ( _e) = self . monitor . add_update_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1879
- unimplemented ! ( ) ;
1880
- }
1881
1881
for failure in pending_failures. drain ( ..) {
1882
1882
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
1883
1883
}
@@ -1968,7 +1968,7 @@ impl ChannelManager {
1968
1968
}
1969
1969
1970
1970
fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , MsgHandleErrInternal > {
1971
- let ( res, chan_monitor ) = {
1971
+ let res = {
1972
1972
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1973
1973
match channel_state. by_id . get_mut ( & msg. channel_id ) {
1974
1974
Some ( chan) => {
@@ -1977,16 +1977,17 @@ impl ChannelManager {
1977
1977
}
1978
1978
let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order) = chan. channel_reestablish ( msg)
1979
1979
. map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
1980
- ( Ok ( ( funding_locked, revoke_and_ack, commitment_update, order) ) , channel_monitor)
1980
+ if let Some ( monitor) = channel_monitor {
1981
+ if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1982
+ unimplemented ! ( ) ;
1983
+ }
1984
+ }
1985
+ Ok ( ( funding_locked, revoke_and_ack, commitment_update, order) )
1981
1986
} ,
1982
1987
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1983
1988
}
1984
1989
} ;
1985
- if let Some ( monitor) = chan_monitor {
1986
- if let Err ( _e) = self . monitor . add_update_monitor ( monitor. get_funding_txo ( ) . unwrap ( ) , monitor) {
1987
- unimplemented ! ( ) ;
1988
- }
1989
- }
1990
+
1990
1991
res
1991
1992
}
1992
1993
0 commit comments