@@ -66,6 +66,8 @@ use crate::util::errors::APIError;
66
66
use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, MaxDustHTLCExposure};
67
67
use crate::util::scid_utils::scid_from_parts;
68
68
69
+ use alloc::collections::BTreeMap;
70
+
69
71
use crate::io;
70
72
use crate::prelude::*;
71
73
use core::{cmp,mem,fmt};
@@ -5778,6 +5780,11 @@ impl<SP: Deref> FundedChannel<SP> where
5778
5780
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
5779
5781
)));
5780
5782
}
5783
+
5784
+ if msg.batch.is_some() {
5785
+ return Err(ChannelError::close("Peer sent initial commitment_signed with a batch".to_owned()));
5786
+ }
5787
+
5781
5788
let holder_commitment_point = &mut self.holder_commitment_point.clone();
5782
5789
self.context.assert_no_commitment_advancement(holder_commitment_point.transaction_number(), "initial commitment_signed");
5783
5790
@@ -5805,6 +5812,53 @@ impl<SP: Deref> FundedChannel<SP> where
5805
5812
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5806
5813
where L::Target: Logger
5807
5814
{
5815
+ self.commitment_signed_check_state()?;
5816
+
5817
+ if !self.pending_funding.is_empty() {
5818
+ return Err(ChannelError::close("Peer sent commitment_signed without a batch when there's a pending splice".to_owned()));
5819
+ }
5820
+
5821
+ let updates = self
5822
+ .context
5823
+ .validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)
5824
+ .map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5825
+ vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5826
+ commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5827
+ }]
5828
+ )?;
5829
+
5830
+ self.commitment_signed_update_monitor(updates, logger)
5831
+ }
5832
+
5833
+ pub fn commitment_signed_batch<L: Deref>(&mut self, batch: &BTreeMap<Txid, msgs::CommitmentSigned>, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5834
+ where L::Target: Logger
5835
+ {
5836
+ self.commitment_signed_check_state()?;
5837
+
5838
+ // Any commitment_signed not associated with a FundingScope is ignored below if a
5839
+ // pending splice transaction has confirmed since receiving the batch.
5840
+ let updates = core::iter::once(&self.funding)
5841
+ .chain(self.pending_funding.iter())
5842
+ .map(|funding| {
5843
+ let funding_txid = funding.get_funding_txo().unwrap().txid;
5844
+ let msg = batch
5845
+ .get(&funding_txid)
5846
+ .ok_or_else(|| ChannelError::close(format!("Peer did not send a commitment_signed for pending splice transaction: {}", funding_txid)))?;
5847
+ self.context
5848
+ .validate_commitment_signed(funding, &self.holder_commitment_point, msg, logger)
5849
+ .map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
5850
+ ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5851
+ commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
5852
+ }
5853
+ )
5854
+ }
5855
+ )
5856
+ .collect::<Result<Vec<_>, ChannelError>>()?;
5857
+
5858
+ self.commitment_signed_update_monitor(updates, logger)
5859
+ }
5860
+
5861
+ fn commitment_signed_check_state(&self) -> Result<(), ChannelError> {
5808
5862
if self.context.channel_state.is_quiescent() {
5809
5863
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
5810
5864
}
@@ -5818,8 +5872,12 @@ impl<SP: Deref> FundedChannel<SP> where
5818
5872
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
5819
5873
}
5820
5874
5821
- let commitment_tx_info = self.context.validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)?;
5875
+ Ok(())
5876
+ }
5822
5877
5878
+ fn commitment_signed_update_monitor<L: Deref>(&mut self, mut updates: Vec<ChannelMonitorUpdateStep>, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5879
+ where L::Target: Logger
5880
+ {
5823
5881
if self.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
5824
5882
// We only fail to advance our commitment point/number if we're currently
5825
5883
// waiting for our signer to unblock and provide a commitment point.
@@ -5873,18 +5931,21 @@ impl<SP: Deref> FundedChannel<SP> where
5873
5931
}
5874
5932
}
5875
5933
5876
- let LatestHolderCommitmentTXInfo {
5877
- commitment_tx, htlc_outputs, nondust_htlc_sources,
5878
- } = commitment_tx_info;
5934
+ for mut update in updates.iter_mut() {
5935
+ if let ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5936
+ claimed_htlcs: ref mut update_claimed_htlcs, ..
5937
+ } = &mut update {
5938
+ debug_assert!(update_claimed_htlcs.is_empty());
5939
+ *update_claimed_htlcs = claimed_htlcs.clone();
5940
+ } else {
5941
+ debug_assert!(false);
5942
+ }
5943
+ }
5944
+
5879
5945
self.context.latest_monitor_update_id += 1;
5880
5946
let mut monitor_update = ChannelMonitorUpdate {
5881
5947
update_id: self.context.latest_monitor_update_id,
5882
- updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5883
- commitment_tx,
5884
- htlc_outputs,
5885
- claimed_htlcs,
5886
- nondust_htlc_sources,
5887
- }],
5948
+ updates,
5888
5949
channel_id: Some(self.context.channel_id()),
5889
5950
};
5890
5951
0 commit comments