Skip to content

Commit facecf0

Browse files
committed
f - impl handle_commitment_signed_batch
1 parent 4bd6079 commit facecf0

File tree

4 files changed

+104
-58
lines changed

4 files changed

+104
-58
lines changed

lightning/src/ln/channel.rs

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::util::errors::APIError;
6666
use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, MaxDustHTLCExposure};
6767
use crate::util::scid_utils::scid_from_parts;
6868

69-
use alloc::collections::{btree_map, BTreeMap};
69+
use alloc::collections::BTreeMap;
7070

7171
use crate::io;
7272
use crate::prelude::*;
@@ -1520,7 +1520,6 @@ impl<SP: Deref> Channel<SP> where
15201520
let mut funded_channel = FundedChannel {
15211521
funding: chan.funding,
15221522
pending_funding: vec![],
1523-
commitment_signed_batch: BTreeMap::new(),
15241523
context: chan.context,
15251524
interactive_tx_signing_session: chan.interactive_tx_signing_session,
15261525
holder_commitment_point,
@@ -4920,7 +4919,6 @@ pub(super) struct DualFundingChannelContext {
49204919
pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
49214920
pub funding: FundingScope,
49224921
pending_funding: Vec<FundingScope>,
4923-
commitment_signed_batch: BTreeMap<Txid, msgs::CommitmentSigned>,
49244922
pub context: ChannelContext<SP>,
49254923
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
49264924
holder_commitment_point: HolderCommitmentPoint,
@@ -5736,6 +5734,53 @@ impl<SP: Deref> FundedChannel<SP> where
57365734
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
57375735
where L::Target: Logger
57385736
{
5737+
self.commitment_signed_check_state()?;
5738+
5739+
if !self.pending_funding.is_empty() {
5740+
return Err(ChannelError::close("Peer sent commitment_signed without a batch when there's a pending splice".to_owned()));
5741+
}
5742+
5743+
let updates = self
5744+
.context
5745+
.validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)
5746+
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5747+
vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5748+
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5749+
}]
5750+
)?;
5751+
5752+
self.commitment_signed_update_monitor(updates, logger)
5753+
}
5754+
5755+
pub fn commitment_signed_batch<L: Deref>(&mut self, batch: &BTreeMap<Txid, msgs::CommitmentSigned>, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5756+
where L::Target: Logger
5757+
{
5758+
self.commitment_signed_check_state()?;
5759+
5760+
// Any commitment_signed not associated with a FundingScope is ignored below if a
5761+
// pending splice transaction has confirmed since receiving the batch.
5762+
let updates = core::iter::once(&self.funding)
5763+
.chain(self.pending_funding.iter())
5764+
.map(|funding| {
5765+
let funding_txid = funding.get_funding_txo().unwrap().txid;
5766+
let msg = batch
5767+
.get(&funding_txid)
5768+
.ok_or_else(|| ChannelError::close(format!("Peer did not send a commitment_signed for pending splice transaction: {}", funding_txid)))?;
5769+
self.context
5770+
.validate_commitment_signed(funding, &self.holder_commitment_point, msg, logger)
5771+
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5772+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5773+
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5774+
}
5775+
)
5776+
}
5777+
)
5778+
.collect::<Result<Vec<_>, ChannelError>>()?;
5779+
5780+
self.commitment_signed_update_monitor(updates, logger)
5781+
}
5782+
5783+
fn commitment_signed_check_state(&self) -> Result<(), ChannelError> {
57395784
if self.context.channel_state.is_quiescent() {
57405785
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
57415786
}
@@ -5749,59 +5794,12 @@ impl<SP: Deref> FundedChannel<SP> where
57495794
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
57505795
}
57515796

5752-
if msg.batch.is_none() && !self.pending_funding.is_empty() {
5753-
return Err(ChannelError::close("Peer sent commitment_signed without a batch when there's a pending splice".to_owned()));
5754-
}
5755-
5756-
let mut updates = match &msg.batch {
5757-
// No pending splice
5758-
None => {
5759-
debug_assert!(self.pending_funding.is_empty());
5760-
debug_assert!(self.commitment_signed_batch.is_empty());
5761-
self.context
5762-
.validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)
5763-
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5764-
vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5765-
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5766-
}]
5767-
)?
5768-
},
5769-
// May or may not have a pending splice
5770-
Some(batch) => {
5771-
match self.commitment_signed_batch.entry(batch.funding_txid) {
5772-
btree_map::Entry::Vacant(entry) => { entry.insert(msg.clone()); },
5773-
btree_map::Entry::Occupied(entry) => {
5774-
return Err(ChannelError::close(format!("Peer sent commitment_signed with duplicate funding_txid {} in a batch", entry.key())));
5775-
},
5776-
}
5777-
5778-
if self.commitment_signed_batch.len() < batch.batch_size as usize {
5779-
return Ok(None);
5780-
}
5781-
5782-
// Any commitment_signed not associated with a FundingScope is ignored below if a
5783-
// pending splice transaction has confirmed since receiving the batch.
5784-
core::iter::once(&self.funding)
5785-
.chain(self.pending_funding.iter())
5786-
.map(|funding| {
5787-
let funding_txid = funding.get_funding_txo().unwrap().txid;
5788-
let msg = self.commitment_signed_batch
5789-
.get(&funding_txid)
5790-
.ok_or_else(|| ChannelError::close(format!("Peer did not send a commitment_signed for pending splice transaction: {}", funding_txid)))?;
5791-
self.context
5792-
.validate_commitment_signed(funding, &self.holder_commitment_point, msg, logger)
5793-
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5794-
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5795-
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5796-
}
5797-
)
5798-
}
5799-
)
5800-
.collect::<Result<Vec<_>, ChannelError>>()?
5801-
},
5802-
};
5803-
self.commitment_signed_batch.clear();
5797+
Ok(())
5798+
}
58045799

5800+
fn commitment_signed_update_monitor<L: Deref>(&mut self, mut updates: Vec<ChannelMonitorUpdateStep>, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5801+
where L::Target: Logger
5802+
{
58055803
if self.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
58065804
// We only fail to advance our commitment point/number if we're currently
58075805
// waiting for our signer to unblock and provide a commitment point.
@@ -9606,7 +9604,6 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
96069604
let mut channel = FundedChannel {
96079605
funding: self.funding,
96089606
pending_funding: vec![],
9609-
commitment_signed_batch: BTreeMap::new(),
96109607
context: self.context,
96119608
interactive_tx_signing_session: None,
96129609
is_v2_established: false,
@@ -9884,7 +9881,6 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
98849881
let mut channel = FundedChannel {
98859882
funding: self.funding,
98869883
pending_funding: vec![],
9887-
commitment_signed_batch: BTreeMap::new(),
98889884
context: self.context,
98899885
interactive_tx_signing_session: None,
98909886
is_v2_established: false,
@@ -11151,7 +11147,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1115111147
funding_transaction,
1115211148
},
1115311149
pending_funding: pending_funding.unwrap(),
11154-
commitment_signed_batch: BTreeMap::new(),
1115511150
context: ChannelContext {
1115611151
user_id,
1115711152

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9039,6 +9039,38 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
90399039
}
90409040
}
90419041

9042+
fn internal_commitment_signed_batch(&self, counterparty_node_id: &PublicKey, channel_id: ChannelId, batch: &BTreeMap<Txid, msgs::CommitmentSigned>) -> Result<(), MsgHandleErrInternal> {
9043+
let per_peer_state = self.per_peer_state.read().unwrap();
9044+
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
9045+
.ok_or_else(|| {
9046+
debug_assert!(false);
9047+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), channel_id)
9048+
})?;
9049+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
9050+
let peer_state = &mut *peer_state_lock;
9051+
match peer_state.channel_by_id.entry(channel_id) {
9052+
hash_map::Entry::Occupied(mut chan_entry) => {
9053+
let chan = chan_entry.get_mut();
9054+
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
9055+
let funding_txo = chan.funding().get_funding_txo();
9056+
if let Some(chan) = chan.as_funded_mut() {
9057+
let monitor_update_opt = try_channel_entry!(
9058+
self, peer_state, chan.commitment_signed_batch(batch, &&logger), chan_entry
9059+
);
9060+
9061+
if let Some(monitor_update) = monitor_update_opt {
9062+
handle_new_monitor_update!(
9063+
self, funding_txo.unwrap(), monitor_update, peer_state_lock, peer_state,
9064+
per_peer_state, chan
9065+
);
9066+
}
9067+
}
9068+
Ok(())
9069+
},
9070+
hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), channel_id))
9071+
}
9072+
}
9073+
90429074
fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
90439075
let mut push_forward_event = self.forward_htlcs.lock().unwrap().is_empty();
90449076
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
@@ -12165,6 +12197,11 @@ where
1216512197
let _ = handle_error!(self, self.internal_commitment_signed(&counterparty_node_id, msg), counterparty_node_id);
1216612198
}
1216712199

12200+
fn handle_commitment_signed_batch(&self, counterparty_node_id: PublicKey, channel_id: ChannelId, batch: &BTreeMap<Txid, msgs::CommitmentSigned>) {
12201+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12202+
let _ = handle_error!(self, self.internal_commitment_signed_batch(&counterparty_node_id, channel_id, batch), counterparty_node_id);
12203+
}
12204+
1216812205
fn handle_revoke_and_ack(&self, counterparty_node_id: PublicKey, msg: &msgs::RevokeAndACK) {
1216912206
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
1217012207
let _ = handle_error!(self, self.internal_revoke_and_ack(&counterparty_node_id, msg), counterparty_node_id);

lightning/src/ln/peer_handler.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ impl ChannelMessageHandler for ErroringMessageHandler {
334334
fn handle_commitment_signed(&self, their_node_id: PublicKey, msg: &msgs::CommitmentSigned) {
335335
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
336336
}
337+
fn handle_commitment_signed_batch(
338+
&self, their_node_id: PublicKey, channel_id: ChannelId,
339+
_batch: &BTreeMap<Txid, msgs::CommitmentSigned>,
340+
) {
341+
ErroringMessageHandler::push_error(self, their_node_id, channel_id);
342+
}
337343
fn handle_revoke_and_ack(&self, their_node_id: PublicKey, msg: &msgs::RevokeAndACK) {
338344
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
339345
}

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
7979

8080
use lightning_invoice::RawBolt11Invoice;
8181

82+
use alloc::collections::BTreeMap;
83+
8284
use crate::io;
8385
use crate::prelude::*;
8486
use crate::sign::{EntropySource, NodeSigner, RandomBytes, Recipient, SignerProvider};
@@ -1022,6 +1024,12 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
10221024
fn handle_commitment_signed(&self, _their_node_id: PublicKey, msg: &msgs::CommitmentSigned) {
10231025
self.received_msg(wire::Message::CommitmentSigned(msg.clone()));
10241026
}
1027+
fn handle_commitment_signed_batch(
1028+
&self, _their_node_id: PublicKey, _channel_id: ChannelId,
1029+
_batch: &BTreeMap<Txid, msgs::CommitmentSigned>,
1030+
) {
1031+
unreachable!()
1032+
}
10251033
fn handle_revoke_and_ack(&self, _their_node_id: PublicKey, msg: &msgs::RevokeAndACK) {
10261034
self.received_msg(wire::Message::RevokeAndACK(msg.clone()));
10271035
}

0 commit comments

Comments
 (0)