@@ -365,6 +365,47 @@ impl ChannelManager {
365
365
Ok ( ( ) )
366
366
}
367
367
368
+ #[ inline]
369
+ fn finish_force_close_channel ( & self , shutdown_res : ( Vec < Transaction > , Vec < [ u8 ; 32 ] > ) ) {
370
+ let ( local_txn, failed_htlcs) = shutdown_res;
371
+ for payment_hash in failed_htlcs {
372
+ // unknown_next_peer...I dunno who that is anymore....
373
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & payment_hash, HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ) ;
374
+ }
375
+ for tx in local_txn {
376
+ self . tx_broadcaster . broadcast_transaction ( & tx) ;
377
+ }
378
+ //TODO: We need to have a way where outbound HTLC claims can result in us claiming the
379
+ //now-on-chain HTLC output for ourselves (and, thereafter, passing the HTLC backwards).
380
+ //TODO: We need to handle monitoring of pending offered HTLCs which just hit the chain and
381
+ //may be claimed, resulting in us claiming the inbound HTLCs (and back-failing after
382
+ //timeouts are hit and our claims confirm).
383
+ }
384
+
385
+ /// Force closes a channel, immediately broadcasting the latest local commitment transaction to
386
+ /// the chain and rejecting new HTLCs on the given channel.
387
+ pub fn force_close_channel ( & self , channel_id : & [ u8 ; 32 ] ) {
388
+ let mut chan = {
389
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
390
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
391
+ if let Some ( mut chan) = channel_state. by_id . remove ( channel_id) {
392
+ if let Some ( short_id) = chan. get_short_channel_id ( ) {
393
+ channel_state. short_to_id . remove ( & short_id) ;
394
+ }
395
+ chan
396
+ } else {
397
+ return ;
398
+ }
399
+ } ;
400
+ self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
401
+ let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
402
+ if let Ok ( update) = self . get_channel_update ( & chan) {
403
+ events. push ( events:: Event :: BroadcastChannelUpdate {
404
+ msg : update
405
+ } ) ;
406
+ }
407
+ }
408
+
368
409
#[ inline]
369
410
fn gen_rho_mu_from_shared_secret ( shared_secret : & SharedSecret ) -> ( [ u8 ; 32 ] , [ u8 ; 32 ] ) {
370
411
( {
@@ -1092,6 +1133,7 @@ impl events::EventsProvider for ChannelManager {
1092
1133
impl ChainListener for ChannelManager {
1093
1134
fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , indexes_of_txn_matched : & [ u32 ] ) {
1094
1135
let mut new_events = Vec :: new ( ) ;
1136
+ let mut failed_channels = Vec :: new ( ) ;
1095
1137
{
1096
1138
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1097
1139
let mut short_to_ids_to_insert = Vec :: new ( ) ;
@@ -1119,7 +1161,10 @@ impl ChainListener for ChannelManager {
1119
1161
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1120
1162
short_to_ids_to_remove. push ( short_id) ;
1121
1163
}
1122
- channel. force_shutdown ( ) ;
1164
+ // It looks like our counterparty went on-chain. We go ahead and
1165
+ // broadcast our latest local state as well here, just in case its
1166
+ // some kind of SPV attack, though we expect these to be dropped.
1167
+ failed_channels. push ( channel. force_shutdown ( ) ) ;
1123
1168
if let Ok ( update) = self . get_channel_update ( & channel) {
1124
1169
new_events. push ( events:: Event :: BroadcastChannelUpdate {
1125
1170
msg : update
@@ -1134,7 +1179,11 @@ impl ChainListener for ChannelManager {
1134
1179
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1135
1180
short_to_ids_to_remove. push ( short_id) ;
1136
1181
}
1137
- channel. force_shutdown ( ) ;
1182
+ failed_channels. push ( channel. force_shutdown ( ) ) ;
1183
+ // If would_broadcast_at_height() is true, the channel_monitor will broadcast
1184
+ // the latest local tx for us, so we should skip that here (it doesn't really
1185
+ // hurt anything, but does make tests a bit simpler).
1186
+ failed_channels. last_mut ( ) . unwrap ( ) . 0 = Vec :: new ( ) ;
1138
1187
if let Ok ( update) = self . get_channel_update ( & channel) {
1139
1188
new_events. push ( events:: Event :: BroadcastChannelUpdate {
1140
1189
msg : update
@@ -1151,6 +1200,9 @@ impl ChainListener for ChannelManager {
1151
1200
channel_state. short_to_id . insert ( to_insert. 0 , to_insert. 1 ) ;
1152
1201
}
1153
1202
}
1203
+ for failure in failed_channels. drain ( ..) {
1204
+ self . finish_force_close_channel ( failure) ;
1205
+ }
1154
1206
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1155
1207
for funding_locked in new_events. drain ( ..) {
1156
1208
pending_events. push ( funding_locked) ;
@@ -1160,23 +1212,38 @@ impl ChainListener for ChannelManager {
1160
1212
1161
1213
/// We force-close the channel without letting our counterparty participate in the shutdown
1162
1214
fn block_disconnected ( & self , header : & BlockHeader ) {
1163
- let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1164
- let channel_state = channel_lock. borrow_parts ( ) ;
1165
- let short_to_id = channel_state. short_to_id ;
1166
- channel_state. by_id . retain ( |_, v| {
1167
- if v. block_disconnected ( header) {
1168
- let tx = v. force_shutdown ( ) ;
1169
- for broadcast_tx in tx {
1170
- self . tx_broadcaster . broadcast_transaction ( & broadcast_tx) ;
1171
- }
1172
- if let Some ( short_id) = v. get_short_channel_id ( ) {
1173
- short_to_id. remove ( & short_id) ;
1215
+ let mut new_events = Vec :: new ( ) ;
1216
+ let mut failed_channels = Vec :: new ( ) ;
1217
+ {
1218
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1219
+ let channel_state = channel_lock. borrow_parts ( ) ;
1220
+ let short_to_id = channel_state. short_to_id ;
1221
+ channel_state. by_id . retain ( |_, v| {
1222
+ if v. block_disconnected ( header) {
1223
+ if let Some ( short_id) = v. get_short_channel_id ( ) {
1224
+ short_to_id. remove ( & short_id) ;
1225
+ }
1226
+ failed_channels. push ( v. force_shutdown ( ) ) ;
1227
+ if let Ok ( update) = self . get_channel_update ( & v) {
1228
+ new_events. push ( events:: Event :: BroadcastChannelUpdate {
1229
+ msg : update
1230
+ } ) ;
1231
+ }
1232
+ false
1233
+ } else {
1234
+ true
1174
1235
}
1175
- false
1176
- } else {
1177
- true
1236
+ } ) ;
1237
+ }
1238
+ for failure in failed_channels. drain ( ..) {
1239
+ self . finish_force_close_channel ( failure) ;
1240
+ }
1241
+ if !new_events. is_empty ( ) {
1242
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1243
+ for funding_locked in new_events. drain ( ..) {
1244
+ pending_events. push ( funding_locked) ;
1178
1245
}
1179
- } ) ;
1246
+ }
1180
1247
self . latest_block_height . fetch_sub ( 1 , Ordering :: AcqRel ) ;
1181
1248
}
1182
1249
}
@@ -1855,6 +1922,7 @@ impl ChannelMessageHandler for ChannelManager {
1855
1922
1856
1923
fn peer_disconnected ( & self , their_node_id : & PublicKey , no_connection_possible : bool ) {
1857
1924
let mut new_events = Vec :: new ( ) ;
1925
+ let mut failed_channels = Vec :: new ( ) ;
1858
1926
{
1859
1927
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1860
1928
let channel_state = channel_state_lock. borrow_parts ( ) ;
@@ -1865,10 +1933,7 @@ impl ChannelMessageHandler for ChannelManager {
1865
1933
if let Some ( short_id) = chan. get_short_channel_id ( ) {
1866
1934
short_to_id. remove ( & short_id) ;
1867
1935
}
1868
- let txn_to_broadcast = chan. force_shutdown ( ) ;
1869
- for tx in txn_to_broadcast {
1870
- self . tx_broadcaster . broadcast_transaction ( & tx) ;
1871
- }
1936
+ failed_channels. push ( chan. force_shutdown ( ) ) ;
1872
1937
if let Ok ( update) = self . get_channel_update ( & chan) {
1873
1938
new_events. push ( events:: Event :: BroadcastChannelUpdate {
1874
1939
msg : update
@@ -1889,6 +1954,9 @@ impl ChannelMessageHandler for ChannelManager {
1889
1954
}
1890
1955
}
1891
1956
}
1957
+ for failure in failed_channels. drain ( ..) {
1958
+ self . finish_force_close_channel ( failure) ;
1959
+ }
1892
1960
if !new_events. is_empty ( ) {
1893
1961
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
1894
1962
for event in new_events. drain ( ..) {
@@ -2880,7 +2948,7 @@ mod tests {
2880
2948
let mut node_txn = test_txn_broadcast ( & nodes[ 1 ] , & chan_1, None , HTLCType :: NONE ) ;
2881
2949
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
2882
2950
nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ node_txn. drain( ..) . next( ) . unwrap( ) ] } , 1 ) ;
2883
- assert_eq ! ( nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . len ( ) , 0 ) ;
2951
+ test_txn_broadcast ( & nodes[ 0 ] , & chan_1 , None , HTLCType :: NONE ) ;
2884
2952
}
2885
2953
get_announce_close_broadcast_events ( & nodes, 0 , 1 ) ;
2886
2954
assert_eq ! ( nodes[ 0 ] . node. list_channels( ) . len( ) , 0 ) ;
@@ -2895,7 +2963,7 @@ mod tests {
2895
2963
let mut node_txn = test_txn_broadcast ( & nodes[ 1 ] , & chan_2, None , HTLCType :: TIMEOUT ) ;
2896
2964
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
2897
2965
nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ node_txn. drain( ..) . next( ) . unwrap( ) ] } , 1 ) ;
2898
- assert_eq ! ( nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . len ( ) , 0 ) ;
2966
+ test_txn_broadcast ( & nodes[ 2 ] , & chan_2 , None , HTLCType :: NONE ) ;
2899
2967
}
2900
2968
get_announce_close_broadcast_events ( & nodes, 1 , 2 ) ;
2901
2969
assert_eq ! ( nodes[ 1 ] . node. list_channels( ) . len( ) , 0 ) ;
@@ -2990,14 +3058,15 @@ mod tests {
2990
3058
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ revoked_local_txn[ 0 ] . clone( ) ] } , 1 ) ;
2991
3059
{
2992
3060
let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
2993
- assert_eq ! ( node_txn. len( ) , 1 ) ;
3061
+ assert_eq ! ( node_txn. len( ) , 2 ) ;
2994
3062
assert_eq ! ( node_txn[ 0 ] . input. len( ) , 1 ) ;
2995
3063
2996
3064
let mut funding_tx_map = HashMap :: new ( ) ;
2997
3065
funding_tx_map. insert ( revoked_local_txn[ 0 ] . txid ( ) , revoked_local_txn[ 0 ] . clone ( ) ) ;
2998
3066
node_txn[ 0 ] . verify ( & funding_tx_map) . unwrap ( ) ;
2999
- node_txn. clear ( ) ;
3067
+ node_txn. swap_remove ( 0 ) ;
3000
3068
}
3069
+ test_txn_broadcast ( & nodes[ 1 ] , & chan_5, None , HTLCType :: NONE ) ;
3001
3070
3002
3071
nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ revoked_local_txn[ 0 ] . clone( ) ] } , 1 ) ;
3003
3072
let node_txn = test_txn_broadcast ( & nodes[ 0 ] , & chan_5, Some ( revoked_local_txn[ 0 ] . clone ( ) ) , HTLCType :: TIMEOUT ) ;
0 commit comments