5454//!
5555//! let node_id = PublicKey::from_str("NODE_ID").unwrap();
5656//! let node_addr = SocketAddress::from_str("IP_ADDR:PORT").unwrap();
57- //! node.open_channel(node_id, node_addr, 10000, None, None).unwrap();
57+ //! node.open_channel(node_id, Some( node_addr) , 10000, None, None).unwrap();
5858//!
5959//! let event = node.wait_next_event();
6060//! println!("EVENT: {:?}", event);
@@ -1126,39 +1126,47 @@ impl Node {
11261126 }
11271127
11281128 fn open_channel_inner (
1129- & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : FundingAmount ,
1130- push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
1131- announce_for_forwarding : bool ,
1129+ & self , node_id : PublicKey , address : Option < SocketAddress > ,
1130+ channel_amount_sats : FundingAmount , push_to_counterparty_msat : Option < u64 > ,
1131+ channel_config : Option < ChannelConfig > , announce_for_forwarding : bool ,
11321132 ) -> Result < UserChannelId , Error > {
11331133 if !* self . is_running . read ( ) . unwrap ( ) {
11341134 return Err ( Error :: NotRunning ) ;
11351135 }
11361136
1137- let peer_info = PeerInfo { node_id, address } ;
1138-
1139- let con_node_id = peer_info. node_id ;
1140- let con_addr = peer_info. address . clone ( ) ;
1141- let con_cm = Arc :: clone ( & self . connection_manager ) ;
1142-
1143- // We need to use our main runtime here as a local runtime might not be around to poll
1144- // connection futures going forward.
1145- self . runtime . block_on ( async move {
1146- con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
1147- } ) ?;
1137+ // if we don't have the socket address, check if we are already connected
1138+ let address = match address {
1139+ Some ( address) => {
1140+ // We need to use our main runtime here as a local runtime might not be around to poll
1141+ // connection futures going forward.
1142+ let con_cm = Arc :: clone ( & self . connection_manager ) ;
1143+ let con_addr = address. clone ( ) ;
1144+ self . runtime . block_on ( async move {
1145+ con_cm. connect_peer_if_necessary ( node_id, con_addr) . await
1146+ } ) ?;
1147+ Some ( address)
1148+ } ,
1149+ None => {
1150+ // If we are connected, grab the socket address as we need to make sure we have it persisted
1151+ // in our peer storage for future reconnections.
1152+ let peer =
1153+ self . peer_manager . peer_by_node_id ( & node_id) . ok_or ( Error :: NotConnected ) ?;
1154+ peer. socket_address
1155+ } ,
1156+ } ;
11481157
11491158 let channel_amount_sats = match channel_amount_sats {
11501159 FundingAmount :: Exact { amount_sats } => {
11511160 // Check funds availability after connection (includes anchor reserve
11521161 // calculation).
1153- self . check_sufficient_funds_for_channel ( amount_sats, & peer_info . node_id ) ?;
1162+ self . check_sufficient_funds_for_channel ( amount_sats, & node_id) ?;
11541163 amount_sats
11551164 } ,
11561165 FundingAmount :: Max => {
11571166 // Determine max funding amount from all available on-chain funds.
11581167 let cur_anchor_reserve_sats =
11591168 total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1160- let new_channel_reserve =
1161- self . new_channel_anchor_reserve_sats ( & peer_info. node_id ) ?;
1169+ let new_channel_reserve = self . new_channel_anchor_reserve_sats ( & node_id) ?;
11621170 let total_anchor_reserve_sats = cur_anchor_reserve_sats + new_channel_reserve;
11631171
11641172 let fee_rate =
@@ -1197,20 +1205,21 @@ impl Node {
11971205 ) ;
11981206
11991207 match self . channel_manager . create_channel (
1200- peer_info . node_id ,
1208+ node_id,
12011209 channel_amount_sats,
12021210 push_msat,
12031211 user_channel_id,
12041212 None ,
12051213 Some ( user_config) ,
12061214 ) {
12071215 Ok ( _) => {
1208- log_info ! (
1209- self . logger,
1210- "Initiated channel creation with peer {}. " ,
1211- peer_info. node_id
1212- ) ;
1213- self . peer_store . add_peer ( peer_info) ?;
1216+ log_info ! ( self . logger, "Initiated channel creation with peer {}. " , node_id) ;
1217+
1218+ if let Some ( address) = address {
1219+ let peer_info = PeerInfo { node_id, address } ;
1220+ self . peer_store . add_peer ( peer_info) ?;
1221+ }
1222+
12141223 Ok ( UserChannelId ( user_channel_id) )
12151224 } ,
12161225 Err ( e) => {
@@ -1224,7 +1233,7 @@ impl Node {
12241233 let init_features = self
12251234 . peer_manager
12261235 . peer_by_node_id ( peer_node_id)
1227- . ok_or ( Error :: ConnectionFailed ) ?
1236+ . ok_or ( Error :: NotConnected ) ?
12281237 . init_features ;
12291238 let anchor_channel = init_features. requires_anchors_zero_fee_htlc_tx ( ) ;
12301239 Ok ( new_channel_anchor_reserve_sats ( & self . config , peer_node_id, anchor_channel) )
@@ -1280,7 +1289,7 @@ impl Node {
12801289 ///
12811290 /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
12821291 pub fn open_channel (
1283- & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
1292+ & self , node_id : PublicKey , address : Option < SocketAddress > , channel_amount_sats : u64 ,
12841293 push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
12851294 ) -> Result < UserChannelId , Error > {
12861295 self . open_channel_inner (
@@ -1315,7 +1324,7 @@ impl Node {
13151324 ///
13161325 /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
13171326 pub fn open_announced_channel (
1318- & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
1327+ & self , node_id : PublicKey , address : Option < SocketAddress > , channel_amount_sats : u64 ,
13191328 push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
13201329 ) -> Result < UserChannelId , Error > {
13211330 if let Err ( err) = may_announce_channel ( & self . config ) {
@@ -1348,8 +1357,8 @@ impl Node {
13481357 ///
13491358 /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
13501359 pub fn open_channel_with_all (
1351- & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1352- channel_config : Option < ChannelConfig > ,
1360+ & self , node_id : PublicKey , address : Option < SocketAddress > ,
1361+ push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
13531362 ) -> Result < UserChannelId , Error > {
13541363 self . open_channel_inner (
13551364 node_id,
@@ -1380,8 +1389,8 @@ impl Node {
13801389 ///
13811390 /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
13821391 pub fn open_announced_channel_with_all (
1383- & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1384- channel_config : Option < ChannelConfig > ,
1392+ & self , node_id : PublicKey , address : Option < SocketAddress > ,
1393+ push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
13851394 ) -> Result < UserChannelId , Error > {
13861395 if let Err ( err) = may_announce_channel ( & self . config ) {
13871396 log_error ! ( self . logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node: {err}" ) ;
0 commit comments