Skip to content

Commit 41ab89b

Browse files
committed
Fold anchor amounts in update_add_htlc
1 parent 346c962 commit 41ab89b

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

lightning/src/ln/channel.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,20 +5755,27 @@ impl<SP: Deref> FundedChannel<SP> where
57555755
// violate the reserve value if we do not do this (as we forget inbound HTLCs from the
57565756
// Channel state once they will not be present in the next received commitment
57575757
// transaction).
5758-
let mut removed_outbound_total_msat = 0;
5759-
for ref htlc in self.context.pending_outbound_htlcs.iter() {
5760-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
5761-
removed_outbound_total_msat += htlc.amount_msat;
5762-
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
5763-
removed_outbound_total_msat += htlc.amount_msat;
5758+
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = {
5759+
let mut removed_outbound_total_msat = 0;
5760+
for ref htlc in self.context.pending_outbound_htlcs.iter() {
5761+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
5762+
removed_outbound_total_msat += htlc.amount_msat;
5763+
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
5764+
removed_outbound_total_msat += htlc.amount_msat;
5765+
}
57645766
}
5765-
}
5766-
5767-
let pending_value_to_self_msat =
5768-
self.funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
5769-
let pending_remote_value_msat =
5770-
self.funding.get_value_satoshis() * 1000 - pending_value_to_self_msat;
5771-
if pending_remote_value_msat < msg.amount_msat {
5767+
let pending_value_to_self_msat =
5768+
self.funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
5769+
let pending_remote_value_msat =
5770+
self.funding.get_value_satoshis() * 1000 - pending_value_to_self_msat;
5771+
SpecTxBuilder {}.balances_excluding_tx_fee(
5772+
self.funding.is_outbound(),
5773+
self.funding.get_channel_type(),
5774+
self.funding.value_to_self_msat,
5775+
pending_remote_value_msat,
5776+
)
5777+
};
5778+
if remote_balance_before_fee_msat < msg.amount_msat {
57725779
return Err(ChannelError::close("Remote HTLC add would overdraw remaining funds".to_owned()));
57735780
}
57745781

@@ -5779,29 +5786,19 @@ impl<SP: Deref> FundedChannel<SP> where
57795786
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
57805787
self.context.next_remote_commit_tx_fee_msat(&self.funding, Some(htlc_candidate), None) // Don't include the extra fee spike buffer HTLC in calculations
57815788
};
5782-
let anchor_outputs_value_msat = if !self.funding.is_outbound() && self.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
5783-
ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
5784-
} else {
5785-
0
5786-
};
5787-
if pending_remote_value_msat.saturating_sub(msg.amount_msat).saturating_sub(anchor_outputs_value_msat) < remote_commit_tx_fee_msat {
5789+
if remote_balance_before_fee_msat.saturating_sub(msg.amount_msat) < remote_commit_tx_fee_msat {
57885790
return Err(ChannelError::close("Remote HTLC add would not leave enough to pay for fees".to_owned()));
57895791
};
5790-
if pending_remote_value_msat.saturating_sub(msg.amount_msat).saturating_sub(remote_commit_tx_fee_msat).saturating_sub(anchor_outputs_value_msat) < self.funding.holder_selected_channel_reserve_satoshis * 1000 {
5792+
if remote_balance_before_fee_msat.saturating_sub(msg.amount_msat).saturating_sub(remote_commit_tx_fee_msat) < self.funding.holder_selected_channel_reserve_satoshis * 1000 {
57915793
return Err(ChannelError::close("Remote HTLC add would put them under remote reserve value".to_owned()));
57925794
}
57935795
}
57945796

5795-
let anchor_outputs_value_msat = if self.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
5796-
ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000
5797-
} else {
5798-
0
5799-
};
58005797
if self.funding.is_outbound() {
58015798
// Check that they won't violate our local required channel reserve by adding this HTLC.
58025799
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
58035800
let local_commit_tx_fee_msat = self.context.next_local_commit_tx_fee_msat(&self.funding, htlc_candidate, None);
5804-
if self.funding.value_to_self_msat < self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat + anchor_outputs_value_msat {
5801+
if local_balance_before_fee_msat < self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat {
58055802
return Err(ChannelError::close("Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_owned()));
58065803
}
58075804
}

0 commit comments

Comments
 (0)