@@ -52,7 +52,7 @@ use ln::onion_utils;
52
52
use ln:: msgs:: { ChannelMessageHandler , DecodeError , LightningError , OptionalField } ;
53
53
use chain:: keysinterface:: { Sign , KeysInterface , KeysManager , InMemorySigner } ;
54
54
use util:: config:: UserConfig ;
55
- use util:: events:: { EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
55
+ use util:: events:: { EventHandler , EventsProvider , MessageSendEvent , MessageSendEventsProvider , ClosureReason } ;
56
56
use util:: { byte_utils, events} ;
57
57
use util:: ser:: { Readable , ReadableArgs , MaybeReadable , Writeable , Writer } ;
58
58
use util:: chacha20:: { ChaCha20 , ChaChaReader } ;
@@ -835,6 +835,9 @@ macro_rules! handle_error {
835
835
msg: update
836
836
} ) ;
837
837
}
838
+ if let Some ( channel_id) = chan_id {
839
+ $self. pending_events. lock( ) . unwrap( ) . push( events:: Event :: ChannelClosed { channel_id, reason: ClosureReason :: ProcessingError { err: err. err. clone( ) } } ) ;
840
+ }
838
841
}
839
842
840
843
log_error!( $self. logger, "{}" , err. err) ;
@@ -1368,6 +1371,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1368
1371
msg : channel_update
1369
1372
} ) ;
1370
1373
}
1374
+ if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
1375
+ pending_events_lock. push ( events:: Event :: ChannelClosed {
1376
+ channel_id : * channel_id,
1377
+ reason : ClosureReason :: HolderForceClosed
1378
+ } ) ;
1379
+ }
1371
1380
}
1372
1381
break Ok ( ( ) ) ;
1373
1382
} ,
@@ -1443,7 +1452,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1443
1452
}
1444
1453
}
1445
1454
1446
- fn force_close_channel_with_peer ( & self , channel_id : & [ u8 ; 32 ] , peer_node_id : Option < & PublicKey > ) -> Result < PublicKey , APIError > {
1455
+ /// `peer_node_id` should be set when we receive a message from a peer, but not set when the
1456
+ /// user closes, which will be re-exposed as the `ChannelClosed` reason.
1457
+ fn force_close_channel_with_peer ( & self , channel_id : & [ u8 ; 32 ] , peer_node_id : Option < & PublicKey > , peer_msg : Option < & String > ) -> Result < PublicKey , APIError > {
1447
1458
let mut chan = {
1448
1459
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1449
1460
let channel_state = & mut * channel_state_lock;
@@ -1456,6 +1467,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1456
1467
if let Some ( short_id) = chan. get ( ) . get_short_channel_id ( ) {
1457
1468
channel_state. short_to_id . remove ( & short_id) ;
1458
1469
}
1470
+ let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
1471
+ if peer_node_id. is_some ( ) {
1472
+ if let Some ( peer_msg) = peer_msg {
1473
+ pending_events_lock. push ( events:: Event :: ChannelClosed { channel_id : * channel_id, reason : ClosureReason :: CounterpartyForceClosed { peer_msg : peer_msg. to_string ( ) } } ) ;
1474
+ }
1475
+ } else {
1476
+ pending_events_lock. push ( events:: Event :: ChannelClosed { channel_id : * channel_id, reason : ClosureReason :: HolderForceClosed } ) ;
1477
+ }
1459
1478
chan. remove_entry ( ) . 1
1460
1479
} else {
1461
1480
return Err ( APIError :: ChannelUnavailable { err : "No such channel" . to_owned ( ) } ) ;
@@ -1477,7 +1496,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1477
1496
/// the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager.
1478
1497
pub fn force_close_channel ( & self , channel_id : & [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
1479
1498
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1480
- match self . force_close_channel_with_peer ( channel_id, None ) {
1499
+ match self . force_close_channel_with_peer ( channel_id, None , None ) {
1481
1500
Ok ( counterparty_node_id) => {
1482
1501
self . channel_state . lock ( ) . unwrap ( ) . pending_msg_events . push (
1483
1502
events:: MessageSendEvent :: HandleError {
@@ -2421,6 +2440,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2421
2440
if let Some ( short_id) = channel. get_short_channel_id ( ) {
2422
2441
channel_state. short_to_id . remove ( & short_id) ;
2423
2442
}
2443
+ // ChannelClosed event is generated by handle_error for us.
2424
2444
Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
2425
2445
} ,
2426
2446
ChannelError :: CloseDelayBroadcast ( _) => { panic ! ( "Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here" ) ; }
@@ -3550,6 +3570,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3550
3570
msg : update
3551
3571
} ) ;
3552
3572
}
3573
+ self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: ChannelClosed { channel_id : msg. channel_id , reason : ClosureReason :: CooperativeClosure } ) ;
3553
3574
}
3554
3575
Ok ( ( ) )
3555
3576
}
@@ -3961,6 +3982,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3961
3982
msg : update
3962
3983
} ) ;
3963
3984
}
3985
+ self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
3964
3986
pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
3965
3987
node_id : chan. get_counterparty_node_id ( ) ,
3966
3988
action : msgs:: ErrorAction :: SendErrorMessage {
@@ -4022,6 +4044,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4022
4044
Err ( e) => {
4023
4045
let ( close_channel, res) = convert_chan_err ! ( self , e, short_to_id, chan, channel_id) ;
4024
4046
handle_errors. push ( ( chan. get_counterparty_node_id ( ) , Err ( res) ) ) ;
4047
+ // ChannelClosed event is generated by handle_error for us
4025
4048
!close_channel
4026
4049
}
4027
4050
}
@@ -4075,6 +4098,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4075
4098
} ) ;
4076
4099
}
4077
4100
4101
+ if let Ok ( mut pending_events_lock) = self . pending_events . lock ( ) {
4102
+ pending_events_lock. push ( events:: Event :: ChannelClosed {
4103
+ channel_id : * channel_id,
4104
+ reason : ClosureReason :: CooperativeClosure
4105
+ } ) ;
4106
+ }
4107
+
4078
4108
log_info ! ( self . logger, "Broadcasting {}" , log_tx!( tx) ) ;
4079
4109
self . tx_broadcaster . broadcast_transaction ( & tx) ;
4080
4110
false
@@ -4495,6 +4525,7 @@ where
4495
4525
msg : update
4496
4526
} ) ;
4497
4527
}
4528
+ self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: ChannelClosed { channel_id : channel. channel_id ( ) , reason : ClosureReason :: CommitmentTxConfirmed } ) ;
4498
4529
pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
4499
4530
node_id : channel. get_counterparty_node_id ( ) ,
4500
4531
action : msgs:: ErrorAction :: SendErrorMessage { msg : e } ,
@@ -4685,6 +4716,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
4685
4716
msg : update
4686
4717
} ) ;
4687
4718
}
4719
+ self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4688
4720
false
4689
4721
} else {
4690
4722
true
@@ -4699,6 +4731,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
4699
4731
if let Some ( short_id) = chan. get_short_channel_id ( ) {
4700
4732
short_to_id. remove ( & short_id) ;
4701
4733
}
4734
+ self . pending_events . lock ( ) . unwrap ( ) . push ( events:: Event :: ChannelClosed { channel_id : chan. channel_id ( ) , reason : ClosureReason :: DisconnectedPeer } ) ;
4702
4735
return false ;
4703
4736
} else {
4704
4737
no_channels_remain = false ;
@@ -4789,12 +4822,12 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
4789
4822
for chan in self . list_channels ( ) {
4790
4823
if chan. counterparty . node_id == * counterparty_node_id {
4791
4824
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
4792
- let _ = self . force_close_channel_with_peer ( & chan. channel_id , Some ( counterparty_node_id) ) ;
4825
+ let _ = self . force_close_channel_with_peer ( & chan. channel_id , Some ( counterparty_node_id) , Some ( & msg . data ) ) ;
4793
4826
}
4794
4827
}
4795
4828
} else {
4796
4829
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
4797
- let _ = self . force_close_channel_with_peer ( & msg. channel_id , Some ( counterparty_node_id) ) ;
4830
+ let _ = self . force_close_channel_with_peer ( & msg. channel_id , Some ( counterparty_node_id) , Some ( & msg . data ) ) ;
4798
4831
}
4799
4832
}
4800
4833
}
@@ -5295,6 +5328,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5295
5328
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
5296
5329
let mut by_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
5297
5330
let mut short_to_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
5331
+ let mut channel_closures = Vec :: new ( ) ;
5298
5332
for _ in 0 ..channel_count {
5299
5333
let mut channel: Channel < Signer > = Channel :: read ( reader, & args. keys_manager ) ?;
5300
5334
let funding_txo = channel. get_funding_txo ( ) . ok_or ( DecodeError :: InvalidValue ) ?;
@@ -5325,6 +5359,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5325
5359
let ( _, mut new_failed_htlcs) = channel. force_shutdown ( true ) ;
5326
5360
failed_htlcs. append ( & mut new_failed_htlcs) ;
5327
5361
monitor. broadcast_latest_holder_commitment_txn ( & args. tx_broadcaster , & args. logger ) ;
5362
+ channel_closures. push ( events:: Event :: ChannelClosed {
5363
+ channel_id : channel. channel_id ( ) ,
5364
+ reason : ClosureReason :: OutdatedChannelManager
5365
+ } ) ;
5328
5366
} else {
5329
5367
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
5330
5368
short_to_id. insert ( short_channel_id, channel. channel_id ( ) ) ;
@@ -5432,6 +5470,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
5432
5470
let mut secp_ctx = Secp256k1 :: new ( ) ;
5433
5471
secp_ctx. seeded_randomize ( & args. keys_manager . get_secure_random_bytes ( ) ) ;
5434
5472
5473
+ if !channel_closures. is_empty ( ) {
5474
+ pending_events_read. append ( & mut channel_closures) ;
5475
+ }
5476
+
5435
5477
let channel_manager = ChannelManager {
5436
5478
genesis_hash,
5437
5479
fee_estimator : args. fee_estimator ,
0 commit comments