|
54 | 54 | //! |
55 | 55 | //! let node_id = PublicKey::from_str("NODE_ID").unwrap(); |
56 | 56 | //! 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(); |
58 | 58 | //! |
59 | 59 | //! let event = node.wait_next_event(); |
60 | 60 | //! println!("EVENT: {:?}", event); |
@@ -1126,39 +1126,47 @@ impl Node { |
1126 | 1126 | } |
1127 | 1127 |
|
1128 | 1128 | 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, |
1132 | 1132 | ) -> Result<UserChannelId, Error> { |
1133 | 1133 | if !*self.is_running.read().unwrap() { |
1134 | 1134 | return Err(Error::NotRunning); |
1135 | 1135 | } |
1136 | 1136 |
|
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::ConnectionFailed)?; |
| 1154 | + peer.socket_address |
| 1155 | + }, |
| 1156 | + }; |
1148 | 1157 |
|
1149 | 1158 | let channel_amount_sats = match channel_amount_sats { |
1150 | 1159 | FundingAmount::Exact { amount_sats } => { |
1151 | 1160 | // Check funds availability after connection (includes anchor reserve |
1152 | 1161 | // 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)?; |
1154 | 1163 | amount_sats |
1155 | 1164 | }, |
1156 | 1165 | FundingAmount::Max => { |
1157 | 1166 | // Determine max funding amount from all available on-chain funds. |
1158 | 1167 | let cur_anchor_reserve_sats = |
1159 | 1168 | 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)?; |
1162 | 1170 | let total_anchor_reserve_sats = cur_anchor_reserve_sats + new_channel_reserve; |
1163 | 1171 |
|
1164 | 1172 | let fee_rate = |
@@ -1197,20 +1205,21 @@ impl Node { |
1197 | 1205 | ); |
1198 | 1206 |
|
1199 | 1207 | match self.channel_manager.create_channel( |
1200 | | - peer_info.node_id, |
| 1208 | + node_id, |
1201 | 1209 | channel_amount_sats, |
1202 | 1210 | push_msat, |
1203 | 1211 | user_channel_id, |
1204 | 1212 | None, |
1205 | 1213 | Some(user_config), |
1206 | 1214 | ) { |
1207 | 1215 | 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 | + |
1214 | 1223 | Ok(UserChannelId(user_channel_id)) |
1215 | 1224 | }, |
1216 | 1225 | Err(e) => { |
@@ -1280,7 +1289,7 @@ impl Node { |
1280 | 1289 | /// |
1281 | 1290 | /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats |
1282 | 1291 | 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, |
1284 | 1293 | push_to_counterparty_msat: Option<u64>, channel_config: Option<ChannelConfig>, |
1285 | 1294 | ) -> Result<UserChannelId, Error> { |
1286 | 1295 | self.open_channel_inner( |
@@ -1315,7 +1324,7 @@ impl Node { |
1315 | 1324 | /// |
1316 | 1325 | /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats |
1317 | 1326 | 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, |
1319 | 1328 | push_to_counterparty_msat: Option<u64>, channel_config: Option<ChannelConfig>, |
1320 | 1329 | ) -> Result<UserChannelId, Error> { |
1321 | 1330 | if let Err(err) = may_announce_channel(&self.config) { |
@@ -1348,8 +1357,8 @@ impl Node { |
1348 | 1357 | /// |
1349 | 1358 | /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats |
1350 | 1359 | 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>, |
1353 | 1362 | ) -> Result<UserChannelId, Error> { |
1354 | 1363 | self.open_channel_inner( |
1355 | 1364 | node_id, |
@@ -1380,8 +1389,8 @@ impl Node { |
1380 | 1389 | /// |
1381 | 1390 | /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats |
1382 | 1391 | 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>, |
1385 | 1394 | ) -> Result<UserChannelId, Error> { |
1386 | 1395 | if let Err(err) = may_announce_channel(&self.config) { |
1387 | 1396 | 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