Skip to content

Commit 29dd036

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 ade0cc0 commit 29dd036

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
@@ -1036,50 +1036,14 @@ impl Node {
10361036
let con_addr = peer_info.address.clone();
10371037
let con_cm = Arc::clone(&self.connection_manager);
10381038

1039-
let cur_anchor_reserve_sats =
1040-
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
1041-
let spendable_amount_sats =
1042-
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
1043-
1044-
// Fail early if we have less than the channel value available.
1045-
if spendable_amount_sats < channel_amount_sats {
1046-
log_error!(self.logger,
1047-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1048-
spendable_amount_sats, channel_amount_sats
1049-
);
1050-
return Err(Error::InsufficientFunds);
1051-
}
1052-
10531039
// We need to use our main runtime here as a local runtime might not be around to poll
10541040
// connection futures going forward.
10551041
self.runtime.block_on(async move {
10561042
con_cm.connect_peer_if_necessary(con_node_id, con_addr).await
10571043
})?;
10581044

1059-
// Fail if we have less than the channel value + anchor reserve available (if applicable).
1060-
let init_features = self
1061-
.peer_manager
1062-
.peer_by_node_id(&node_id)
1063-
.ok_or(Error::ConnectionFailed)?
1064-
.init_features;
1065-
let required_funds_sats = channel_amount_sats
1066-
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
1067-
if init_features.requires_anchors_zero_fee_htlc_tx()
1068-
&& !c.trusted_peers_no_reserve.contains(&node_id)
1069-
{
1070-
c.per_channel_reserve_sats
1071-
} else {
1072-
0
1073-
}
1074-
});
1075-
1076-
if spendable_amount_sats < required_funds_sats {
1077-
log_error!(self.logger,
1078-
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1079-
spendable_amount_sats, required_funds_sats
1080-
);
1081-
return Err(Error::InsufficientFunds);
1082-
}
1045+
// Check funds availability after connection (includes anchor reserve calculation)
1046+
self.check_sufficient_funds_for_channel(channel_amount_sats, &node_id)?;
10831047

10841048
let mut user_config = default_user_config(&self.config);
10851049
user_config.channel_handshake_config.announce_for_forwarding = announce_for_forwarding;
@@ -1120,6 +1084,51 @@ impl Node {
11201084
}
11211085
}
11221086

1087+
fn check_sufficient_funds_for_channel(
1088+
&self, amount_sats: u64, peer_node_id: &PublicKey,
1089+
) -> Result<(), Error> {
1090+
let cur_anchor_reserve_sats =
1091+
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
1092+
let spendable_amount_sats =
1093+
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats).unwrap_or(0);
1094+
1095+
// Fail early if we have less than the channel value available.
1096+
if spendable_amount_sats < amount_sats {
1097+
log_error!(self.logger,
1098+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1099+
spendable_amount_sats, amount_sats
1100+
);
1101+
return Err(Error::InsufficientFunds);
1102+
}
1103+
1104+
// Fail if we have less than the channel value + anchor reserve available (if applicable).
1105+
let init_features = self
1106+
.peer_manager
1107+
.peer_by_node_id(peer_node_id)
1108+
.ok_or(Error::ConnectionFailed)?
1109+
.init_features;
1110+
let required_funds_sats = amount_sats
1111+
+ self.config.anchor_channels_config.as_ref().map_or(0, |c| {
1112+
if init_features.requires_anchors_zero_fee_htlc_tx()
1113+
&& !c.trusted_peers_no_reserve.contains(peer_node_id)
1114+
{
1115+
c.per_channel_reserve_sats
1116+
} else {
1117+
0
1118+
}
1119+
});
1120+
1121+
if spendable_amount_sats < required_funds_sats {
1122+
log_error!(self.logger,
1123+
"Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats",
1124+
spendable_amount_sats, required_funds_sats
1125+
);
1126+
return Err(Error::InsufficientFunds);
1127+
}
1128+
1129+
Ok(())
1130+
}
1131+
11231132
/// Connect to a node and open a new unannounced channel.
11241133
///
11251134
/// To open an announced channel, see [`Node::open_announced_channel`].

0 commit comments

Comments
 (0)