11use crate :: types:: { DynStore , Sweeper , Wallet } ;
22
33use crate :: {
4- hex_utils, ChannelManager , Config , Error , NetworkGraph , PeerInfo , PeerStore , UserChannelId ,
4+ hex_utils, BumpTransactionEventHandler , ChannelManager , Config , Error , NetworkGraph , PeerInfo ,
5+ PeerStore , UserChannelId ,
56} ;
67
78use crate :: connection:: ConnectionManager ;
@@ -15,9 +16,10 @@ use crate::io::{
1516 EVENT_QUEUE_PERSISTENCE_KEY , EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE ,
1617 EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE ,
1718} ;
18- use crate :: logger:: { log_error, log_info, Logger } ;
19+ use crate :: logger:: { log_debug , log_error, log_info, Logger } ;
1920
2021use lightning:: chain:: chaininterface:: ConfirmationTarget ;
22+ use lightning:: events:: bump_transaction:: BumpTransactionEvent ;
2123use lightning:: events:: { ClosureReason , PaymentPurpose } ;
2224use lightning:: events:: { Event as LdkEvent , PaymentFailureReason } ;
2325use lightning:: impl_writeable_tlv_based_enum;
@@ -317,6 +319,7 @@ where
317319{
318320 event_queue : Arc < EventQueue < L > > ,
319321 wallet : Arc < Wallet > ,
322+ bump_tx_event_handler : Arc < BumpTransactionEventHandler > ,
320323 channel_manager : Arc < ChannelManager > ,
321324 connection_manager : Arc < ConnectionManager < L > > ,
322325 output_sweeper : Arc < Sweeper > ,
@@ -333,15 +336,17 @@ where
333336 L :: Target : Logger ,
334337{
335338 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 > ,
341345 ) -> Self {
342346 Self {
343347 event_queue,
344348 wallet,
349+ bump_tx_event_handler,
345350 channel_manager,
346351 connection_manager,
347352 output_sweeper,
@@ -815,9 +820,67 @@ where
815820 temporary_channel_id,
816821 counterparty_node_id,
817822 funding_satoshis,
818- channel_type : _ ,
823+ channel_type,
819824 push_msat : _,
820825 } => {
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+
821884 let user_channel_id: u128 = rand:: thread_rng ( ) . gen :: < u128 > ( ) ;
822885 let allow_0conf = self . config . trusted_peers_0conf . contains ( & counterparty_node_id) ;
823886 let res = if allow_0conf {
@@ -838,8 +901,9 @@ where
838901 Ok ( ( ) ) => {
839902 log_info ! (
840903 self . logger,
841- "Accepting inbound{} channel of {}sats from{} peer {}" ,
904+ "Accepting inbound{}{} channel of {}sats from{} peer {}" ,
842905 if allow_0conf { " 0conf" } else { "" } ,
906+ if anchor_channel { " Anchor" } else { "" } ,
843907 funding_satoshis,
844908 if allow_0conf { " trusted" } else { "" } ,
845909 counterparty_node_id,
@@ -848,8 +912,9 @@ where
848912 Err ( e) => {
849913 log_error ! (
850914 self . logger,
851- "Error while accepting inbound{} channel from{} peer {}: {:?}" ,
915+ "Error while accepting inbound{}{} channel from{} peer {}: {:?}" ,
852916 if allow_0conf { " 0conf" } else { "" } ,
917+ if anchor_channel { " Anchor" } else { "" } ,
853918 counterparty_node_id,
854919 if allow_0conf { " trusted" } else { "" } ,
855920 e,
@@ -1018,7 +1083,6 @@ where
10181083 } ,
10191084 LdkEvent :: DiscardFunding { .. } => { } ,
10201085 LdkEvent :: HTLCIntercepted { .. } => { } ,
1021- LdkEvent :: BumpTransaction ( _) => { } ,
10221086 LdkEvent :: InvoiceRequestFailed { payment_id } => {
10231087 log_error ! (
10241088 self . logger,
@@ -1062,6 +1126,35 @@ where
10621126 } ) ;
10631127 }
10641128 } ,
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+ } ,
10651158 }
10661159 }
10671160}
0 commit comments