@@ -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};
@@ -5700,6 +5702,11 @@ impl<SP: Deref> FundedChannel<SP> where
5700
5702
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
5701
5703
)));
5702
5704
}
5705
+
5706
+ if msg.batch.is_some() {
5707
+ return Err(ChannelError::close("Peer sent initial commitment_signed with a batch".to_owned()));
5708
+ }
5709
+
5703
5710
let holder_commitment_point = &mut self.holder_commitment_point.clone();
5704
5711
self.context.assert_no_commitment_advancement(holder_commitment_point.transaction_number(), "initial commitment_signed");
5705
5712
@@ -5727,6 +5734,53 @@ impl<SP: Deref> FundedChannel<SP> where
5727
5734
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5728
5735
where L::Target: Logger
5729
5736
{
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> {
5730
5784
if self.context.channel_state.is_quiescent() {
5731
5785
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
5732
5786
}
@@ -5740,8 +5794,12 @@ impl<SP: Deref> FundedChannel<SP> where
5740
5794
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
5741
5795
}
5742
5796
5743
- let commitment_tx_info = self.context.validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)?;
5797
+ Ok(())
5798
+ }
5744
5799
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
+ {
5745
5803
if self.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger).is_err() {
5746
5804
// We only fail to advance our commitment point/number if we're currently
5747
5805
// waiting for our signer to unblock and provide a commitment point.
@@ -5795,18 +5853,21 @@ impl<SP: Deref> FundedChannel<SP> where
5795
5853
}
5796
5854
}
5797
5855
5798
- let LatestHolderCommitmentTXInfo {
5799
- commitment_tx, htlc_outputs, nondust_htlc_sources,
5800
- } = commitment_tx_info;
5856
+ for mut update in updates.iter_mut() {
5857
+ if let ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5858
+ claimed_htlcs: ref mut update_claimed_htlcs, ..
5859
+ } = &mut update {
5860
+ debug_assert!(update_claimed_htlcs.is_empty());
5861
+ *update_claimed_htlcs = claimed_htlcs.clone();
5862
+ } else {
5863
+ debug_assert!(false);
5864
+ }
5865
+ }
5866
+
5801
5867
self.context.latest_monitor_update_id += 1;
5802
5868
let mut monitor_update = ChannelMonitorUpdate {
5803
5869
update_id: self.context.latest_monitor_update_id,
5804
- updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5805
- commitment_tx,
5806
- htlc_outputs,
5807
- claimed_htlcs,
5808
- nondust_htlc_sources,
5809
- }],
5870
+ updates,
5810
5871
channel_id: Some(self.context.channel_id()),
5811
5872
};
5812
5873
0 commit comments