Skip to content

Commit 14ae753

Browse files
committed
Refactor funds checking logic into reusable method
Extract the funds availability checking logic from open_channel_inner into a separate method so that it can be reused for channel splicing.
1 parent 16d43cd commit 14ae753

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

src/lib.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,50 +1072,14 @@ impl Node {
10721072
let con_addr = peer_info.address.clone();
10731073
let con_cm = Arc::clone(&self.connection_manager);
10741074

1075-
let cur_anchor_reserve_sats =
1076-
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
1077-
let spendable_amount_sats =
1078-
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
1079-
1080-
// Fail early if we have less than the channel value available.
1081-
if spendable_amount_sats < channel_amount_sats {
1082-
log_error!(self.logger,
1083-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1084-
spendable_amount_sats, channel_amount_sats
1085-
);
1086-
return Err(Error::InsufficientFunds);
1087-
}
1088-
10891075
// We need to use our main runtime here as a local runtime might not be around to poll
10901076
// connection futures going forward.
10911077
self.runtime.block_on(async move {
10921078
con_cm.connect_peer_if_necessary(con_node_id, con_addr).await
10931079
})?;
10941080

1095-
// Fail if we have less than the channel value + anchor reserve available (if applicable).
1096-
let init_features = self
1097-
.peer_manager
1098-
.peer_by_node_id(&node_id)
1099-
.ok_or(Error::ConnectionFailed)?
1100-
.init_features;
1101-
let required_funds_sats = channel_amount_sats
1102-
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
1103-
if init_features.requires_anchors_zero_fee_htlc_tx()
1104-
&& !c.trusted_peers_no_reserve.contains(&node_id)
1105-
{
1106-
c.per_channel_reserve_sats
1107-
} else {
1108-
0
1109-
}
1110-
});
1111-
1112-
if spendable_amount_sats < required_funds_sats {
1113-
log_error!(self.logger,
1114-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1115-
spendable_amount_sats, required_funds_sats
1116-
);
1117-
return Err(Error::InsufficientFunds);
1118-
}
1081+
// Check funds availability after connection (includes anchor reserve calculation)
1082+
self.check_sufficient_funds_for_channel(channel_amount_sats, &node_id)?;
11191083

11201084
let mut user_config = default_user_config(&self.config);
11211085
user_config.channel_handshake_config.announce_for_forwarding = announce_for_forwarding;
@@ -1156,6 +1120,51 @@ impl Node {
11561120
}
11571121
}
11581122

1123+
fn check_sufficient_funds_for_channel(
1124+
&self, amount_sats: u64, peer_node_id: &PublicKey,
1125+
) -> Result<(), Error> {
1126+
let cur_anchor_reserve_sats =
1127+
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
1128+
let spendable_amount_sats =
1129+
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
1130+
1131+
// Fail early if we have less than the channel value available.
1132+
if spendable_amount_sats < amount_sats {
1133+
log_error!(self.logger,
1134+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1135+
spendable_amount_sats, amount_sats
1136+
);
1137+
return Err(Error::InsufficientFunds);
1138+
}
1139+
1140+
// Fail if we have less than the channel value + anchor reserve available (if applicable).
1141+
let init_features = self
1142+
.peer_manager
1143+
.peer_by_node_id(peer_node_id)
1144+
.ok_or(Error::ConnectionFailed)?
1145+
.init_features;
1146+
let required_funds_sats = amount_sats
1147+
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
1148+
if init_features.requires_anchors_zero_fee_htlc_tx()
1149+
&& !c.trusted_peers_no_reserve.contains(peer_node_id)
1150+
{
1151+
c.per_channel_reserve_sats
1152+
} else {
1153+
0
1154+
}
1155+
});
1156+
1157+
if spendable_amount_sats < required_funds_sats {
1158+
log_error!(self.logger,
1159+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1160+
spendable_amount_sats, required_funds_sats
1161+
);
1162+
return Err(Error::InsufficientFunds);
1163+
}
1164+
1165+
Ok(())
1166+
}
1167+
11591168
/// Connect to a node and open a new unannounced channel.
11601169
///
11611170
/// To open an announced channel, see [`Node::open_announced_channel`].

0 commit comments

Comments
 (0)