1
1
use crate :: types:: { DynStore , Sweeper , Wallet } ;
2
2
3
3
use crate :: {
4
- hex_utils, ChannelManager , Config , Error , NetworkGraph , PeerInfo , PeerStore , UserChannelId ,
4
+ hex_utils, BumpTransactionEventHandler , ChannelManager , Config , Error , NetworkGraph , PeerInfo ,
5
+ PeerStore , UserChannelId ,
5
6
} ;
6
7
7
8
use crate :: connection:: ConnectionManager ;
@@ -15,9 +16,10 @@ use crate::io::{
15
16
EVENT_QUEUE_PERSISTENCE_KEY , EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE ,
16
17
EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE ,
17
18
} ;
18
- use crate :: logger:: { log_error, log_info, Logger } ;
19
+ use crate :: logger:: { log_debug , log_error, log_info, Logger } ;
19
20
20
21
use lightning:: chain:: chaininterface:: ConfirmationTarget ;
22
+ use lightning:: events:: bump_transaction:: BumpTransactionEvent ;
21
23
use lightning:: events:: { ClosureReason , PaymentPurpose } ;
22
24
use lightning:: events:: { Event as LdkEvent , PaymentFailureReason } ;
23
25
use lightning:: impl_writeable_tlv_based_enum;
@@ -317,6 +319,7 @@ where
317
319
{
318
320
event_queue : Arc < EventQueue < L > > ,
319
321
wallet : Arc < Wallet > ,
322
+ bump_tx_event_handler : Arc < BumpTransactionEventHandler > ,
320
323
channel_manager : Arc < ChannelManager > ,
321
324
connection_manager : Arc < ConnectionManager < L > > ,
322
325
output_sweeper : Arc < Sweeper > ,
@@ -333,15 +336,17 @@ where
333
336
L :: Target : Logger ,
334
337
{
335
338
pub fn new (
336
- event_queue : Arc < EventQueue < L > > , wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > ,
337
- connection_manager : Arc < ConnectionManager < L > > , output_sweeper : Arc < Sweeper > ,
338
- network_graph : Arc < NetworkGraph > , payment_store : Arc < PaymentStore < L > > ,
339
- peer_store : Arc < PeerStore < L > > , runtime : Arc < RwLock < Option < tokio:: runtime:: Runtime > > > ,
340
- logger : L , config : Arc < Config > ,
339
+ event_queue : Arc < EventQueue < L > > , wallet : Arc < Wallet > ,
340
+ bump_tx_event_handler : Arc < BumpTransactionEventHandler > ,
341
+ channel_manager : Arc < ChannelManager > , connection_manager : Arc < ConnectionManager < L > > ,
342
+ output_sweeper : Arc < Sweeper > , network_graph : Arc < NetworkGraph > ,
343
+ payment_store : Arc < PaymentStore < L > > , peer_store : Arc < PeerStore < L > > ,
344
+ runtime : Arc < RwLock < Option < tokio:: runtime:: Runtime > > > , logger : L , config : Arc < Config > ,
341
345
) -> Self {
342
346
Self {
343
347
event_queue,
344
348
wallet,
349
+ bump_tx_event_handler,
345
350
channel_manager,
346
351
connection_manager,
347
352
output_sweeper,
@@ -815,9 +820,67 @@ where
815
820
temporary_channel_id,
816
821
counterparty_node_id,
817
822
funding_satoshis,
818
- channel_type : _ ,
823
+ channel_type,
819
824
push_msat : _,
820
825
} => {
826
+ let anchor_channel = channel_type. requires_anchors_zero_fee_htlc_tx ( ) ;
827
+
828
+ if anchor_channel {
829
+ if let Some ( anchor_channels_config) =
830
+ self . config . anchor_channels_config . as_ref ( )
831
+ {
832
+ let cur_anchor_reserve_sats = crate :: total_anchor_channels_reserve_sats (
833
+ & self . channel_manager ,
834
+ & self . config ,
835
+ ) ;
836
+ let spendable_amount_sats = self
837
+ . wallet
838
+ . get_spendable_amount_sats ( cur_anchor_reserve_sats)
839
+ . unwrap_or ( 0 ) ;
840
+
841
+ let required_amount_sats = if anchor_channels_config
842
+ . trusted_peers_no_reserve
843
+ . contains ( & counterparty_node_id)
844
+ {
845
+ 0
846
+ } else {
847
+ anchor_channels_config. per_channel_reserve_sats
848
+ } ;
849
+
850
+ if spendable_amount_sats < required_amount_sats {
851
+ log_error ! (
852
+ self . logger,
853
+ "Rejecting inbound Anchor channel from peer {} due to insufficient available on-chain reserves." ,
854
+ counterparty_node_id,
855
+ ) ;
856
+ self . channel_manager
857
+ . force_close_without_broadcasting_txn (
858
+ & temporary_channel_id,
859
+ & counterparty_node_id,
860
+ )
861
+ . unwrap_or_else ( |e| {
862
+ log_error ! ( self . logger, "Failed to reject channel: {:?}" , e)
863
+ } ) ;
864
+ return ;
865
+ }
866
+ } else {
867
+ log_error ! (
868
+ self . logger,
869
+ "Rejecting inbound channel from peer {} due to Anchor channels being disabled." ,
870
+ counterparty_node_id,
871
+ ) ;
872
+ self . channel_manager
873
+ . force_close_without_broadcasting_txn (
874
+ & temporary_channel_id,
875
+ & counterparty_node_id,
876
+ )
877
+ . unwrap_or_else ( |e| {
878
+ log_error ! ( self . logger, "Failed to reject channel: {:?}" , e)
879
+ } ) ;
880
+ return ;
881
+ }
882
+ }
883
+
821
884
let user_channel_id: u128 = rand:: thread_rng ( ) . gen :: < u128 > ( ) ;
822
885
let allow_0conf = self . config . trusted_peers_0conf . contains ( & counterparty_node_id) ;
823
886
let res = if allow_0conf {
@@ -838,8 +901,9 @@ where
838
901
Ok ( ( ) ) => {
839
902
log_info ! (
840
903
self . logger,
841
- "Accepting inbound{} channel of {}sats from{} peer {}" ,
904
+ "Accepting inbound{}{} channel of {}sats from{} peer {}" ,
842
905
if allow_0conf { " 0conf" } else { "" } ,
906
+ if anchor_channel { " Anchor" } else { "" } ,
843
907
funding_satoshis,
844
908
if allow_0conf { " trusted" } else { "" } ,
845
909
counterparty_node_id,
@@ -848,8 +912,9 @@ where
848
912
Err ( e) => {
849
913
log_error ! (
850
914
self . logger,
851
- "Error while accepting inbound{} channel from{} peer {}: {:?}" ,
915
+ "Error while accepting inbound{}{} channel from{} peer {}: {:?}" ,
852
916
if allow_0conf { " 0conf" } else { "" } ,
917
+ if anchor_channel { " Anchor" } else { "" } ,
853
918
counterparty_node_id,
854
919
if allow_0conf { " trusted" } else { "" } ,
855
920
e,
@@ -1018,7 +1083,6 @@ where
1018
1083
} ,
1019
1084
LdkEvent :: DiscardFunding { .. } => { } ,
1020
1085
LdkEvent :: HTLCIntercepted { .. } => { } ,
1021
- LdkEvent :: BumpTransaction ( _) => { } ,
1022
1086
LdkEvent :: InvoiceRequestFailed { payment_id } => {
1023
1087
log_error ! (
1024
1088
self . logger,
@@ -1062,6 +1126,35 @@ where
1062
1126
} ) ;
1063
1127
}
1064
1128
} ,
1129
+ LdkEvent :: BumpTransaction ( bte) => {
1130
+ let ( channel_id, counterparty_node_id) = match bte {
1131
+ BumpTransactionEvent :: ChannelClose {
1132
+ ref channel_id,
1133
+ ref counterparty_node_id,
1134
+ ..
1135
+ } => ( channel_id, counterparty_node_id) ,
1136
+ BumpTransactionEvent :: HTLCResolution {
1137
+ ref channel_id,
1138
+ ref counterparty_node_id,
1139
+ ..
1140
+ } => ( channel_id, counterparty_node_id) ,
1141
+ } ;
1142
+
1143
+ if let Some ( anchor_channels_config) = self . config . anchor_channels_config . as_ref ( ) {
1144
+ if anchor_channels_config
1145
+ . trusted_peers_no_reserve
1146
+ . contains ( counterparty_node_id)
1147
+ {
1148
+ log_debug ! ( self . logger,
1149
+ "Ignoring BumpTransactionEvent for channel {} due to trusted counterparty {}" ,
1150
+ channel_id, counterparty_node_id
1151
+ ) ;
1152
+ return ;
1153
+ }
1154
+ }
1155
+
1156
+ self . bump_tx_event_handler . handle_event ( & bte) ;
1157
+ } ,
1065
1158
}
1066
1159
}
1067
1160
}
0 commit comments