@@ -28,6 +28,7 @@ use bitcoin::{secp256k1, sighash, TxIn};
28
28
#[cfg(splicing)]
29
29
use bitcoin::{FeeRate, Sequence};
30
30
31
+ use crate::blinded_path::message::BlindedMessagePath;
31
32
use crate::chain::chaininterface::{
32
33
fee_for_weight, ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator,
33
34
};
@@ -282,6 +283,29 @@ impl InboundHTLCState {
282
283
_ => None,
283
284
}
284
285
}
286
+
287
+ /// Whether we need to hold onto this HTLC until receipt of a corresponding [`ReleaseHeldHtlc`]
288
+ /// onion message.
289
+ ///
290
+ ///[`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc
291
+ fn should_hold_htlc(&self) -> bool {
292
+ match self {
293
+ InboundHTLCState::RemoteAnnounced(res)
294
+ | InboundHTLCState::AwaitingRemoteRevokeToAnnounce(res)
295
+ | InboundHTLCState::AwaitingAnnouncedRemoteRevoke(res) => match res {
296
+ InboundHTLCResolution::Resolved { pending_htlc_status } => {
297
+ match pending_htlc_status {
298
+ PendingHTLCStatus::Forward(info) => info.routing.should_hold_htlc(),
299
+ _ => false,
300
+ }
301
+ },
302
+ InboundHTLCResolution::Pending { update_add_htlc } => {
303
+ update_add_htlc.hold_htlc.is_some()
304
+ },
305
+ },
306
+ InboundHTLCState::Committed | InboundHTLCState::LocalRemoved(_) => false,
307
+ }
308
+ }
285
309
}
286
310
287
311
struct InboundHTLCOutput {
@@ -1602,12 +1626,12 @@ where
1602
1626
}
1603
1627
1604
1628
#[rustfmt::skip]
1605
- pub fn signer_maybe_unblocked<L: Deref>(
1606
- &mut self, chain_hash: ChainHash, logger: &L,
1607
- ) -> Option<SignerResumeUpdates> where L::Target: Logger {
1629
+ pub fn signer_maybe_unblocked<L: Deref, CBP >(
1630
+ &mut self, chain_hash: ChainHash, logger: &L, path_for_release_htlc: CBP
1631
+ ) -> Option<SignerResumeUpdates> where L::Target: Logger, CBP: Fn(u64, u64) -> Result<BlindedMessagePath, ()> {
1608
1632
match &mut self.phase {
1609
1633
ChannelPhase::Undefined => unreachable!(),
1610
- ChannelPhase::Funded(chan) => Some(chan.signer_maybe_unblocked(logger)),
1634
+ ChannelPhase::Funded(chan) => Some(chan.signer_maybe_unblocked(logger, path_for_release_htlc )),
1611
1635
ChannelPhase::UnfundedOutboundV1(chan) => {
1612
1636
let (open_channel, funding_created) = chan.signer_maybe_unblocked(chain_hash, logger);
1613
1637
Some(SignerResumeUpdates {
@@ -8707,13 +8731,14 @@ where
8707
8731
/// successfully and we should restore normal operation. Returns messages which should be sent
8708
8732
/// to the remote side.
8709
8733
#[rustfmt::skip]
8710
- pub fn monitor_updating_restored<L: Deref, NS: Deref>(
8734
+ pub fn monitor_updating_restored<L: Deref, NS: Deref, CBP >(
8711
8735
&mut self, logger: &L, node_signer: &NS, chain_hash: ChainHash,
8712
- user_config: &UserConfig, best_block_height: u32
8736
+ user_config: &UserConfig, best_block_height: u32, path_for_release_htlc: CBP
8713
8737
) -> MonitorRestoreUpdates
8714
8738
where
8715
8739
L::Target: Logger,
8716
- NS::Target: NodeSigner
8740
+ NS::Target: NodeSigner,
8741
+ CBP: Fn(u64, u64) -> Result<BlindedMessagePath, ()>
8717
8742
{
8718
8743
assert!(self.context.channel_state.is_monitor_update_in_progress());
8719
8744
self.context.channel_state.clear_monitor_update_in_progress();
@@ -8770,7 +8795,7 @@ where
8770
8795
}
8771
8796
8772
8797
let mut raa = if self.context.monitor_pending_revoke_and_ack {
8773
- self.get_last_revoke_and_ack(logger)
8798
+ self.get_last_revoke_and_ack(path_for_release_htlc, logger)
8774
8799
} else { None };
8775
8800
let mut commitment_update = if self.context.monitor_pending_commitment_signed {
8776
8801
self.get_last_commitment_update_for_send(logger).ok()
@@ -8859,7 +8884,9 @@ where
8859
8884
/// Indicates that the signer may have some signatures for us, so we should retry if we're
8860
8885
/// blocked.
8861
8886
#[rustfmt::skip]
8862
- pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
8887
+ pub fn signer_maybe_unblocked<L: Deref, CBP>(
8888
+ &mut self, logger: &L, path_for_release_htlc: CBP
8889
+ ) -> SignerResumeUpdates where L::Target: Logger, CBP: Fn(u64, u64) -> Result<BlindedMessagePath, ()> {
8863
8890
if !self.holder_commitment_point.can_advance() {
8864
8891
log_trace!(logger, "Attempting to update holder per-commitment point...");
8865
8892
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
@@ -8887,7 +8914,7 @@ where
8887
8914
} else { None };
8888
8915
let mut revoke_and_ack = if self.context.signer_pending_revoke_and_ack {
8889
8916
log_trace!(logger, "Attempting to generate pending revoke and ack...");
8890
- self.get_last_revoke_and_ack(logger)
8917
+ self.get_last_revoke_and_ack(path_for_release_htlc, logger)
8891
8918
} else { None };
8892
8919
8893
8920
if self.context.resend_order == RAACommitmentOrder::CommitmentFirst
@@ -8958,9 +8985,12 @@ where
8958
8985
}
8959
8986
}
8960
8987
8961
- fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK>
8988
+ fn get_last_revoke_and_ack<CBP, L: Deref>(
8989
+ &mut self, path_for_release_htlc: CBP, logger: &L,
8990
+ ) -> Option<msgs::RevokeAndACK>
8962
8991
where
8963
8992
L::Target: Logger,
8993
+ CBP: Fn(u64, u64) -> Result<BlindedMessagePath, ()>,
8964
8994
{
8965
8995
debug_assert!(
8966
8996
self.holder_commitment_point.next_transaction_number() <= INITIAL_COMMITMENT_NUMBER - 2
@@ -8973,14 +9003,32 @@ where
8973
9003
.ok();
8974
9004
if let Some(per_commitment_secret) = per_commitment_secret {
8975
9005
if self.holder_commitment_point.can_advance() {
9006
+ let mut release_htlc_message_paths = Vec::new();
9007
+ for htlc in &self.context.pending_inbound_htlcs {
9008
+ // TODO: how to handle the errors here
9009
+ let held_htlc = htlc.state.should_hold_htlc();
9010
+ if held_htlc {
9011
+ let scid = match self.funding.short_channel_id {
9012
+ Some(scid) => scid,
9013
+ None => {
9014
+ continue;
9015
+ },
9016
+ };
9017
+ match path_for_release_htlc(scid, htlc.htlc_id) {
9018
+ Ok(path) => release_htlc_message_paths.push((htlc.htlc_id, path)),
9019
+ Err(()) => continue,
9020
+ }
9021
+ }
9022
+ }
9023
+
8976
9024
self.context.signer_pending_revoke_and_ack = false;
8977
9025
return Some(msgs::RevokeAndACK {
8978
9026
channel_id: self.context.channel_id,
8979
9027
per_commitment_secret,
8980
9028
next_per_commitment_point: self.holder_commitment_point.next_point(),
8981
9029
#[cfg(taproot)]
8982
9030
next_local_nonce: None,
8983
- release_htlc_message_paths: Vec::new() ,
9031
+ release_htlc_message_paths,
8984
9032
});
8985
9033
}
8986
9034
}
@@ -9128,13 +9176,15 @@ where
9128
9176
/// May panic if some calls other than message-handling calls (which will all Err immediately)
9129
9177
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
9130
9178
#[rustfmt::skip]
9131
- pub fn channel_reestablish<L: Deref, NS: Deref>(
9179
+ pub fn channel_reestablish<L: Deref, NS: Deref, CBP >(
9132
9180
&mut self, msg: &msgs::ChannelReestablish, logger: &L, node_signer: &NS,
9133
- chain_hash: ChainHash, user_config: &UserConfig, best_block: &BestBlock
9181
+ chain_hash: ChainHash, user_config: &UserConfig, best_block: &BestBlock,
9182
+ path_for_release_htlc: CBP,
9134
9183
) -> Result<ReestablishResponses, ChannelError>
9135
9184
where
9136
9185
L::Target: Logger,
9137
- NS::Target: NodeSigner
9186
+ NS::Target: NodeSigner,
9187
+ CBP: Fn(u64, u64) -> Result<BlindedMessagePath, ()>
9138
9188
{
9139
9189
if !self.context.channel_state.is_peer_disconnected() {
9140
9190
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
@@ -9235,7 +9285,7 @@ where
9235
9285
self.context.monitor_pending_revoke_and_ack = true;
9236
9286
None
9237
9287
} else {
9238
- self.get_last_revoke_and_ack(logger)
9288
+ self.get_last_revoke_and_ack(path_for_release_htlc, logger)
9239
9289
}
9240
9290
} else {
9241
9291
debug_assert!(false, "All values should have been handled in the four cases above");
@@ -16433,6 +16483,7 @@ mod tests {
16433
16483
chain_hash,
16434
16484
&config,
16435
16485
0,
16486
+ |_, _| Err(())
16436
16487
);
16437
16488
16438
16489
// Receive funding_signed, but the channel will be configured to hold sending channel_ready and
@@ -16447,6 +16498,7 @@ mod tests {
16447
16498
chain_hash,
16448
16499
&config,
16449
16500
0,
16501
+ |_, _| Err(())
16450
16502
);
16451
16503
// Our channel_ready shouldn't be sent yet, even with trust_own_funding_0conf set,
16452
16504
// as the funding transaction depends on all channels in the batch becoming ready.
0 commit comments