@@ -142,7 +142,7 @@ use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};
142142
143143use lightning:: chain:: { BestBlock , Confirm } ;
144144use lightning:: events:: bump_transaction:: Wallet as LdkWallet ;
145- use lightning:: ln:: channelmanager:: PaymentId ;
145+ use lightning:: ln:: channelmanager:: { ChannelShutdownState , PaymentId } ;
146146use lightning:: ln:: msgs:: SocketAddress ;
147147
148148use lightning:: util:: config:: { ChannelHandshakeConfig , UserConfig } ;
@@ -886,6 +886,8 @@ impl Node {
886886 OnchainPayment :: new (
887887 Arc :: clone ( & self . runtime ) ,
888888 Arc :: clone ( & self . wallet ) ,
889+ Arc :: clone ( & self . channel_manager ) ,
890+ Arc :: clone ( & self . config ) ,
889891 Arc :: clone ( & self . logger ) ,
890892 )
891893 }
@@ -896,6 +898,8 @@ impl Node {
896898 Arc :: new ( OnchainPayment :: new (
897899 Arc :: clone ( & self . runtime ) ,
898900 Arc :: clone ( & self . wallet ) ,
901+ Arc :: clone ( & self . channel_manager ) ,
902+ Arc :: clone ( & self . config ) ,
899903 Arc :: clone ( & self . logger ) ,
900904 ) )
901905 }
@@ -971,6 +975,10 @@ impl Node {
971975 /// channel counterparty on channel open. This can be useful to start out with the balance not
972976 /// entirely shifted to one side, therefore allowing to receive payments from the getgo.
973977 ///
978+ /// If Anchor channels are enabled, this will ensure the configured
979+ /// [`AnchorChannelsConfig::per_channel_reserve_sats`] is available and will be retained before
980+ /// opening the channel.
981+ ///
974982 /// Returns a [`UserChannelId`] allowing to locally keep track of the channel.
975983 pub fn connect_open_channel (
976984 & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
@@ -983,18 +991,26 @@ impl Node {
983991 }
984992 let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
985993
986- let cur_balance = self . wallet . get_balance ( ) ?;
987- if cur_balance. get_spendable ( ) < channel_amount_sats {
988- log_error ! ( self . logger, "Unable to create channel due to insufficient funds." ) ;
989- return Err ( Error :: InsufficientFunds ) ;
990- }
991-
992994 let peer_info = PeerInfo { node_id, address } ;
993995
994996 let con_node_id = peer_info. node_id ;
995997 let con_addr = peer_info. address . clone ( ) ;
996998 let con_cm = Arc :: clone ( & self . connection_manager ) ;
997999
1000+ let cur_anchor_reserve_sats =
1001+ total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1002+ let spendable_amount_sats =
1003+ self . wallet . get_balances ( cur_anchor_reserve_sats) . map ( |( _, s) | s) . unwrap_or ( 0 ) ;
1004+
1005+ // Fail early if we have less than the channel value available.
1006+ if spendable_amount_sats < channel_amount_sats {
1007+ log_error ! ( self . logger,
1008+ "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1009+ spendable_amount_sats, channel_amount_sats
1010+ ) ;
1011+ return Err ( Error :: InsufficientFunds ) ;
1012+ }
1013+
9981014 // We need to use our main runtime here as a local runtime might not be around to poll
9991015 // connection futures going forward.
10001016 tokio:: task:: block_in_place ( move || {
@@ -1003,11 +1019,37 @@ impl Node {
10031019 } )
10041020 } ) ?;
10051021
1022+ // Fail if we have less than the channel value + anchor reserve available (if applicable).
1023+ let init_features = self
1024+ . peer_manager
1025+ . peer_by_node_id ( & node_id)
1026+ . ok_or ( Error :: ConnectionFailed ) ?
1027+ . init_features ;
1028+ let required_funds_sats = channel_amount_sats
1029+ + self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1030+ if init_features. requires_anchors_zero_fee_htlc_tx ( )
1031+ && !c. trusted_peers_no_reserve . contains ( & node_id)
1032+ {
1033+ c. per_channel_reserve_sats
1034+ } else {
1035+ 0
1036+ }
1037+ } ) ;
1038+
1039+ if spendable_amount_sats < required_funds_sats {
1040+ log_error ! ( self . logger,
1041+ "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1042+ spendable_amount_sats, required_funds_sats
1043+ ) ;
1044+ return Err ( Error :: InsufficientFunds ) ;
1045+ }
1046+
10061047 let channel_config = ( * ( channel_config. unwrap_or_default ( ) ) ) . clone ( ) . into ( ) ;
10071048 let user_config = UserConfig {
10081049 channel_handshake_limits : Default :: default ( ) ,
10091050 channel_handshake_config : ChannelHandshakeConfig {
10101051 announced_channel : announce_channel,
1052+ negotiate_anchors_zero_fee_htlc_tx : self . config . anchor_channels_config . is_some ( ) ,
10111053 ..Default :: default ( )
10121054 } ,
10131055 channel_config,
@@ -1166,11 +1208,13 @@ impl Node {
11661208
11671209 /// Retrieves an overview of all known balances.
11681210 pub fn list_balances ( & self ) -> BalanceDetails {
1169- let ( total_onchain_balance_sats, spendable_onchain_balance_sats) = self
1170- . wallet
1171- . get_balance ( )
1172- . map ( |bal| ( bal. get_total ( ) , bal. get_spendable ( ) ) )
1173- . unwrap_or ( ( 0 , 0 ) ) ;
1211+ let cur_anchor_reserve_sats =
1212+ total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1213+ let ( total_onchain_balance_sats, spendable_onchain_balance_sats) =
1214+ self . wallet . get_balances ( cur_anchor_reserve_sats) . unwrap_or ( ( 0 , 0 ) ) ;
1215+
1216+ let total_anchor_channels_reserve_sats =
1217+ std:: cmp:: min ( cur_anchor_reserve_sats, total_onchain_balance_sats) ;
11741218
11751219 let mut total_lightning_balance_sats = 0 ;
11761220 let mut lightning_balances = Vec :: new ( ) ;
@@ -1203,6 +1247,7 @@ impl Node {
12031247 BalanceDetails {
12041248 total_onchain_balance_sats,
12051249 spendable_onchain_balance_sats,
1250+ total_anchor_channels_reserve_sats,
12061251 total_lightning_balance_sats,
12071252 lightning_balances,
12081253 pending_balances_from_channel_closures,
@@ -1335,3 +1380,23 @@ pub struct NodeStatus {
13351380 /// Will be `None` if we have no public channels or we haven't broadcasted since the [`Node`] was initialized.
13361381 pub latest_node_announcement_broadcast_timestamp : Option < u64 > ,
13371382}
1383+
1384+ pub ( crate ) fn total_anchor_channels_reserve_sats (
1385+ channel_manager : & ChannelManager , config : & Config ,
1386+ ) -> u64 {
1387+ config. anchor_channels_config . as_ref ( ) . map_or ( 0 , |anchor_channels_config| {
1388+ channel_manager
1389+ . list_channels ( )
1390+ . into_iter ( )
1391+ . filter ( |c| {
1392+ !anchor_channels_config. trusted_peers_no_reserve . contains ( & c. counterparty . node_id )
1393+ && c. channel_shutdown_state
1394+ . map_or ( true , |s| s != ChannelShutdownState :: ShutdownComplete )
1395+ && c. channel_type
1396+ . as_ref ( )
1397+ . map_or ( false , |t| t. requires_anchors_zero_fee_htlc_tx ( ) )
1398+ } )
1399+ . count ( ) as u64
1400+ * anchor_channels_config. per_channel_reserve_sats
1401+ } )
1402+ }
0 commit comments