Skip to content

Commit bb3b4de

Browse files
committed
Finetune balance check, use expected balance
1 parent 3bfcb23 commit bb3b4de

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

lightning/src/ln/channel.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -3423,7 +3423,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34233423
/// The channel value is an input as opposed to using from self, so that this can be used in case of splicing
34243424
/// to checks with new channel value (before being comitted to it).
34253425
#[cfg(any(dual_funding, splicing))]
3426-
pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> {
3426+
pub fn check_balance_meets_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> {
3427+
if balance == 0 {
3428+
return Ok(());
3429+
}
34273430
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
34283431
channel_value, self.holder_dust_limit_satoshis);
34293432
if balance < holder_selected_channel_reserve_satoshis {
@@ -7485,9 +7488,11 @@ impl<SP: Deref> Channel<SP> where
74857488

74867489
/// Handle splice_init
74877490
#[cfg(splicing)]
7488-
pub fn splice_init(
7489-
&mut self, their_funding_contribution_satoshis: i64, our_funding_contribution_satoshis: i64,
7490-
) -> Result<msgs::SpliceAck, ChannelError> {
7491+
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
7492+
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
7493+
// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
7494+
let our_funding_contribution_satoshis = 0i64;
7495+
74917496
// Check if a splice has been initiated already.
74927497
// Note: this could be handled more nicely, and support multiple outstanding splice's, the incoming splice_ack matters anyways.
74937498
if let Some(splice_info) = &self.context.pending_splice_pre {
@@ -7520,11 +7525,12 @@ impl<SP: Deref> Channel<SP> where
75207525
}
75217526

75227527
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, their_funding_contribution_satoshis, our_funding_contribution_satoshis);
7523-
7528+
let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution_satoshis);
75247529
// Early check for reserve requirement, assuming maximum balance of full channel value
75257530
// This will also be checked later at tx_complete
7526-
let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?;
7531+
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
75277532

7533+
// TODO(splicing): Store msg.funding_pubkey
75287534
// TODO(splicing): Apply start of splice (splice_start)
75297535

75307536
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis);
@@ -7534,22 +7540,24 @@ impl<SP: Deref> Channel<SP> where
75347540

75357541
/// Handle splice_ack
75367542
#[cfg(splicing)]
7537-
pub fn splice_ack(
7538-
&mut self, their_funding_contribution_satoshis: i64,
7539-
) -> Result<(), ChannelError> {
7543+
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
7544+
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
7545+
75407546
// check if splice is pending
75417547
let pending_splice = if let Some(pending_splice) = &self.context.pending_splice_pre {
75427548
pending_splice
75437549
} else {
75447550
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
75457551
};
75467552

7547-
let pre_channel_value = self.context.get_value_satoshis();
7548-
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, pending_splice.our_funding_contribution, their_funding_contribution_satoshis);
7553+
let our_funding_contribution = pending_splice.our_funding_contribution;
75497554

7555+
let pre_channel_value = self.context.get_value_satoshis();
7556+
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
7557+
let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution);
75507558
// Early check for reserve requirement, assuming maximum balance of full channel value
75517559
// This will also be checked later at tx_complete
7552-
let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?;
7560+
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
75537561
Ok(())
75547562
}
75557563

lightning/src/ln/channelmanager.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9002,8 +9002,6 @@ where
90029002
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
90039003
let peer_state = &mut *peer_state_lock;
90049004

9005-
let our_funding_contribution = 0i64;
9006-
90079005
// Look for the channel
90089006
match peer_state.channel_by_id.entry(msg.channel_id) {
90099007
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
@@ -9012,7 +9010,7 @@ where
90129010
), msg.channel_id)),
90139011
hash_map::Entry::Occupied(mut chan_entry) => {
90149012
if let ChannelPhase::Funded(chan) = chan_entry.get_mut() {
9015-
match chan.splice_init(msg.funding_contribution_satoshis, our_funding_contribution) {
9013+
match chan.splice_init(msg) {
90169014
Ok(splice_ack_msg) => {
90179015
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck {
90189016
node_id: *counterparty_node_id,
@@ -9060,7 +9058,7 @@ where
90609058
), msg.channel_id)),
90619059
hash_map::Entry::Occupied(mut chan) => {
90629060
if let ChannelPhase::Funded(chan) = chan.get_mut() {
9063-
match chan.splice_ack(msg.funding_contribution_satoshis) {
9061+
match chan.splice_ack(msg) {
90649062
Ok(_) => {}
90659063
Err(err) => {
90669064
return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));

0 commit comments

Comments
 (0)