Skip to content

Commit 8e5e331

Browse files
committed
Finetune balance check, use expected balance
1 parent cd372c9 commit 8e5e331

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
@@ -3652,7 +3652,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36523652
/// The channel value is an input as opposed to using from self, so that this can be used in case of splicing
36533653
/// to checks with new channel value (before being comitted to it).
36543654
#[cfg(any(dual_funding, splicing))]
3655-
pub fn check_balance_meets_reserve_requirements(&self, channel_value: u64, balance: u64) -> Result<(), ChannelError> {
3655+
pub fn check_balance_meets_reserve_requirements(&self, balance: u64, channel_value: u64) -> Result<(), ChannelError> {
3656+
if balance == 0 {
3657+
return Ok(());
3658+
}
36563659
let holder_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
36573660
channel_value, self.holder_dust_limit_satoshis);
36583661
if balance < holder_selected_channel_reserve_satoshis {
@@ -7954,9 +7957,11 @@ impl<SP: Deref> Channel<SP> where
79547957

79557958
/// Handle splice_init
79567959
#[cfg(splicing)]
7957-
pub fn splice_init(
7958-
&mut self, their_funding_contribution_satoshis: i64, our_funding_contribution_satoshis: i64,
7959-
) -> Result<msgs::SpliceAck, ChannelError> {
7960+
pub fn splice_init(&mut self, msg: &msgs::SpliceInit) -> Result<msgs::SpliceAck, ChannelError> {
7961+
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
7962+
// TODO(splicing): Currently not possible to contribute on the splicing-acceptor side
7963+
let our_funding_contribution_satoshis = 0i64;
7964+
79607965
// Check if a splice has been initiated already.
79617966
// Note: this could be handled more nicely, and support multiple outstanding splice's, the incoming splice_ack matters anyways.
79627967
if let Some(splice_info) = &self.context.pending_splice_pre {
@@ -7989,11 +7994,12 @@ impl<SP: Deref> Channel<SP> where
79897994
}
79907995

79917996
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, their_funding_contribution_satoshis, our_funding_contribution_satoshis);
7992-
7997+
let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution_satoshis);
79937998
// Early check for reserve requirement, assuming maximum balance of full channel value
79947999
// This will also be checked later at tx_complete
7995-
let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?;
8000+
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
79968001

8002+
// TODO(splicing): Store msg.funding_pubkey
79978003
// TODO(splicing): Apply start of splice (splice_start)
79988004

79998005
let splice_ack_msg = self.context.get_splice_ack(our_funding_contribution_satoshis);
@@ -8003,22 +8009,24 @@ impl<SP: Deref> Channel<SP> where
80038009

80048010
/// Handle splice_ack
80058011
#[cfg(splicing)]
8006-
pub fn splice_ack(
8007-
&mut self, their_funding_contribution_satoshis: i64,
8008-
) -> Result<(), ChannelError> {
8012+
pub fn splice_ack(&mut self, msg: &msgs::SpliceAck) -> Result<(), ChannelError> {
8013+
let their_funding_contribution_satoshis = msg.funding_contribution_satoshis;
8014+
80098015
// check if splice is pending
80108016
let pending_splice = if let Some(pending_splice) = &self.context.pending_splice_pre {
80118017
pending_splice
80128018
} else {
80138019
return Err(ChannelError::Warn(format!("Channel is not in pending splice")));
80148020
};
80158021

8016-
let pre_channel_value = self.context.get_value_satoshis();
8017-
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, pending_splice.our_funding_contribution, their_funding_contribution_satoshis);
8022+
let our_funding_contribution = pending_splice.our_funding_contribution;
80188023

8024+
let pre_channel_value = self.context.get_value_satoshis();
8025+
let post_channel_value = PendingSpliceInfoPre::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution_satoshis);
8026+
let post_balance = PendingSpliceInfoPre::add_checked(self.context.value_to_self_msat, our_funding_contribution);
80198027
// Early check for reserve requirement, assuming maximum balance of full channel value
80208028
// This will also be checked later at tx_complete
8021-
let _res = self.context.check_balance_meets_reserve_requirements(post_channel_value, post_channel_value)?;
8029+
let _res = self.context.check_balance_meets_reserve_requirements(post_balance, post_channel_value)?;
80228030
Ok(())
80238031
}
80248032

lightning/src/ln/channelmanager.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9366,8 +9366,6 @@ where
93669366
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
93679367
let peer_state = &mut *peer_state_lock;
93689368

9369-
let our_funding_contribution = 0i64;
9370-
93719369
// Look for the channel
93729370
match peer_state.channel_by_id.entry(msg.channel_id) {
93739371
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
@@ -9376,7 +9374,7 @@ where
93769374
), msg.channel_id)),
93779375
hash_map::Entry::Occupied(mut chan_entry) => {
93789376
if let ChannelPhase::Funded(chan) = chan_entry.get_mut() {
9379-
match chan.splice_init(msg.funding_contribution_satoshis, our_funding_contribution) {
9377+
match chan.splice_init(msg) {
93809378
Ok(splice_ack_msg) => {
93819379
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceAck {
93829380
node_id: *counterparty_node_id,
@@ -9424,7 +9422,7 @@ where
94249422
), msg.channel_id)),
94259423
hash_map::Entry::Occupied(mut chan) => {
94269424
if let ChannelPhase::Funded(chan) = chan.get_mut() {
9427-
match chan.splice_ack(msg.funding_contribution_satoshis) {
9425+
match chan.splice_ack(msg) {
94289426
Ok(_) => {}
94299427
Err(err) => {
94309428
return Err(MsgHandleErrInternal::from_chan_no_close(err, msg.channel_id));

0 commit comments

Comments
 (0)