@@ -104,10 +104,35 @@ enum InboundHTLCRemovalReason {
104
104
Fulfill(PaymentPreimage),
105
105
}
106
106
107
+ /// Represents the resolution status of an inbound HTLC.
108
+ #[derive(Clone)]
109
+ enum InboundHTLCResolution {
110
+ /// Resolved implies the action we must take with the inbound HTLC has already been determined,
111
+ /// i.e., we already know whether it must be failed back or forwarded.
112
+ Resolved {
113
+ pending_htlc_status: PendingHTLCStatus,
114
+ },
115
+ /// Pending implies we will attempt to resolve the inbound HTLC once it has been fully committed
116
+ /// to by both sides of the channel, i.e., once a `revoke_and_ack` has been processed by both
117
+ /// nodes for the state update in which it was proposed.
118
+ Pending {
119
+ update_add_htlc: msgs::UpdateAddHTLC,
120
+ },
121
+ }
122
+
123
+ impl_writeable_tlv_based_enum!(InboundHTLCResolution,
124
+ (0, Resolved) => {
125
+ (0, pending_htlc_status, required),
126
+ },
127
+ (2, Pending) => {
128
+ (0, update_add_htlc, required),
129
+ };
130
+ );
131
+
107
132
enum InboundHTLCState {
108
133
/// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
109
134
/// update_add_htlc message for this HTLC.
110
- RemoteAnnounced(PendingHTLCStatus ),
135
+ RemoteAnnounced(InboundHTLCResolution ),
111
136
/// Included in a received commitment_signed message (implying we've
112
137
/// revoke_and_ack'd it), but the remote hasn't yet revoked their previous
113
138
/// state (see the example below). We have not yet included this HTLC in a
@@ -137,13 +162,13 @@ enum InboundHTLCState {
137
162
/// Implies AwaitingRemoteRevoke.
138
163
///
139
164
/// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
140
- AwaitingRemoteRevokeToAnnounce(PendingHTLCStatus ),
165
+ AwaitingRemoteRevokeToAnnounce(InboundHTLCResolution ),
141
166
/// Included in a received commitment_signed message (implying we've revoke_and_ack'd it).
142
167
/// We have also included this HTLC in our latest commitment_signed and are now just waiting
143
168
/// on the remote's revoke_and_ack to make this HTLC an irrevocable part of the state of the
144
169
/// channel (before it can then get forwarded and/or removed).
145
170
/// Implies AwaitingRemoteRevoke.
146
- AwaitingAnnouncedRemoteRevoke(PendingHTLCStatus ),
171
+ AwaitingAnnouncedRemoteRevoke(InboundHTLCResolution ),
147
172
Committed,
148
173
/// Removed by us and a new commitment_signed was sent (if we were AwaitingRemoteRevoke when we
149
174
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -1291,6 +1316,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1291
1316
monitor_pending_forwards: Vec<(PendingHTLCInfo, u64)>,
1292
1317
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
1293
1318
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
1319
+ monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
1294
1320
1295
1321
/// If we went to send a commitment update (ie some messages then [`msgs::CommitmentSigned`])
1296
1322
/// but our signer (initially) refused to give us a signature, we should retry at some point in
@@ -1755,6 +1781,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1755
1781
monitor_pending_forwards: Vec::new(),
1756
1782
monitor_pending_failures: Vec::new(),
1757
1783
monitor_pending_finalized_fulfills: Vec::new(),
1784
+ monitor_pending_update_adds: Vec::new(),
1758
1785
1759
1786
signer_pending_commitment_update: false,
1760
1787
signer_pending_funding: false,
@@ -1976,6 +2003,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1976
2003
monitor_pending_forwards: Vec::new(),
1977
2004
monitor_pending_failures: Vec::new(),
1978
2005
monitor_pending_finalized_fulfills: Vec::new(),
2006
+ monitor_pending_update_adds: Vec::new(),
1979
2007
1980
2008
signer_pending_commitment_update: false,
1981
2009
signer_pending_funding: false,
@@ -4255,7 +4283,9 @@ impl<SP: Deref> Channel<SP> where
4255
4283
amount_msat: msg.amount_msat,
4256
4284
payment_hash: msg.payment_hash,
4257
4285
cltv_expiry: msg.cltv_expiry,
4258
- state: InboundHTLCState::RemoteAnnounced(pending_forward_status),
4286
+ state: InboundHTLCState::RemoteAnnounced(InboundHTLCResolution::Resolved {
4287
+ pending_htlc_status: pending_forward_status
4288
+ }),
4259
4289
});
4260
4290
Ok(())
4261
4291
}
@@ -4461,13 +4491,13 @@ impl<SP: Deref> Channel<SP> where
4461
4491
}
4462
4492
4463
4493
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
4464
- let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info ) = &htlc.state {
4465
- Some(forward_info .clone())
4494
+ let htlc_resolution = if let &InboundHTLCState::RemoteAnnounced(ref resolution ) = &htlc.state {
4495
+ Some(resolution .clone())
4466
4496
} else { None };
4467
- if let Some(forward_info ) = new_forward {
4497
+ if let Some(htlc_resolution ) = htlc_resolution {
4468
4498
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToAnnounce due to commitment_signed in channel {}.",
4469
4499
&htlc.payment_hash, &self.context.channel_id);
4470
- htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info );
4500
+ htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(htlc_resolution );
4471
4501
need_commitment = true;
4472
4502
}
4473
4503
}
@@ -4777,6 +4807,7 @@ impl<SP: Deref> Channel<SP> where
4777
4807
4778
4808
log_trace!(logger, "Updating HTLCs on receipt of RAA in channel {}...", &self.context.channel_id());
4779
4809
let mut to_forward_infos = Vec::new();
4810
+ let mut pending_update_adds = Vec::new();
4780
4811
let mut revoked_htlcs = Vec::new();
4781
4812
let mut finalized_claimed_htlcs = Vec::new();
4782
4813
let mut update_fail_htlcs = Vec::new();
@@ -4824,29 +4855,37 @@ impl<SP: Deref> Channel<SP> where
4824
4855
let mut state = InboundHTLCState::Committed;
4825
4856
mem::swap(&mut state, &mut htlc.state);
4826
4857
4827
- if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(forward_info ) = state {
4858
+ if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution ) = state {
4828
4859
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
4829
- htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info );
4860
+ htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution );
4830
4861
require_commitment = true;
4831
- } else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info) = state {
4832
- match forward_info {
4833
- PendingHTLCStatus::Fail(fail_msg) => {
4834
- log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to LocalRemoved due to PendingHTLCStatus indicating failure", &htlc.payment_hash);
4835
- require_commitment = true;
4836
- match fail_msg {
4837
- HTLCFailureMsg::Relay(msg) => {
4838
- htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
4839
- update_fail_htlcs.push(msg)
4840
- },
4841
- HTLCFailureMsg::Malformed(msg) => {
4842
- htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed((msg.sha256_of_onion, msg.failure_code)));
4843
- update_fail_malformed_htlcs.push(msg)
4862
+ } else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution) = state {
4863
+ match resolution {
4864
+ InboundHTLCResolution::Resolved { pending_htlc_status } =>
4865
+ match pending_htlc_status {
4866
+ PendingHTLCStatus::Fail(fail_msg) => {
4867
+ log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to LocalRemoved due to PendingHTLCStatus indicating failure", &htlc.payment_hash);
4868
+ require_commitment = true;
4869
+ match fail_msg {
4870
+ HTLCFailureMsg::Relay(msg) => {
4871
+ htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(msg.reason.clone()));
4872
+ update_fail_htlcs.push(msg)
4873
+ },
4874
+ HTLCFailureMsg::Malformed(msg) => {
4875
+ htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed((msg.sha256_of_onion, msg.failure_code)));
4876
+ update_fail_malformed_htlcs.push(msg)
4877
+ },
4878
+ }
4844
4879
},
4880
+ PendingHTLCStatus::Forward(forward_info) => {
4881
+ log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed, attempting to forward", &htlc.payment_hash);
4882
+ to_forward_infos.push((forward_info, htlc.htlc_id));
4883
+ htlc.state = InboundHTLCState::Committed;
4884
+ }
4845
4885
}
4846
- },
4847
- PendingHTLCStatus::Forward(forward_info) => {
4886
+ InboundHTLCResolution::Pending { update_add_htlc } => {
4848
4887
log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed", &htlc.payment_hash);
4849
- to_forward_infos .push((forward_info, htlc.htlc_id) );
4888
+ pending_update_adds .push(update_add_htlc );
4850
4889
htlc.state = InboundHTLCState::Committed;
4851
4890
}
4852
4891
}
@@ -4907,6 +4946,8 @@ impl<SP: Deref> Channel<SP> where
4907
4946
}
4908
4947
}
4909
4948
4949
+ self.context.monitor_pending_update_adds.append(&mut pending_update_adds);
4950
+
4910
4951
if self.context.channel_state.is_monitor_update_in_progress() {
4911
4952
// We can't actually generate a new commitment transaction (incl by freeing holding
4912
4953
// cells) while we can't update the monitor, so we just return what we have.
@@ -8232,7 +8273,7 @@ fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures)
8232
8273
ret
8233
8274
}
8234
8275
8235
- const SERIALIZATION_VERSION: u8 = 3 ;
8276
+ const SERIALIZATION_VERSION: u8 = 4 ;
8236
8277
const MIN_SERIALIZATION_VERSION: u8 = 3;
8237
8278
8238
8279
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -8294,7 +8335,18 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8294
8335
// Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
8295
8336
// called.
8296
8337
8297
- write_ver_prefix!(writer, MIN_SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
8338
+ let version_to_write = if self.context.pending_inbound_htlcs.iter().any(|htlc| match htlc.state {
8339
+ InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_resolution)|
8340
+ InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_resolution) => {
8341
+ matches!(htlc_resolution, InboundHTLCResolution::Pending { .. })
8342
+ },
8343
+ _ => false,
8344
+ }) {
8345
+ SERIALIZATION_VERSION
8346
+ } else {
8347
+ MIN_SERIALIZATION_VERSION
8348
+ };
8349
+ write_ver_prefix!(writer, version_to_write, MIN_SERIALIZATION_VERSION);
8298
8350
8299
8351
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
8300
8352
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
@@ -8350,13 +8402,29 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8350
8402
htlc.payment_hash.write(writer)?;
8351
8403
match &htlc.state {
8352
8404
&InboundHTLCState::RemoteAnnounced(_) => unreachable!(),
8353
- &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_state ) => {
8405
+ &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_resolution ) => {
8354
8406
1u8.write(writer)?;
8355
- htlc_state.write(writer)?;
8407
+ if version_to_write <= 3 {
8408
+ if let InboundHTLCResolution::Resolved { pending_htlc_status } = htlc_resolution {
8409
+ pending_htlc_status.write(writer)?;
8410
+ } else {
8411
+ panic!();
8412
+ }
8413
+ } else {
8414
+ htlc_resolution.write(writer)?;
8415
+ }
8356
8416
},
8357
- &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_state ) => {
8417
+ &InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_resolution ) => {
8358
8418
2u8.write(writer)?;
8359
- htlc_state.write(writer)?;
8419
+ if version_to_write <= 3 {
8420
+ if let InboundHTLCResolution::Resolved { pending_htlc_status } = htlc_resolution {
8421
+ pending_htlc_status.write(writer)?;
8422
+ } else {
8423
+ panic!();
8424
+ }
8425
+ } else {
8426
+ htlc_resolution.write(writer)?;
8427
+ }
8360
8428
},
8361
8429
&InboundHTLCState::Committed => {
8362
8430
3u8.write(writer)?;
@@ -8582,6 +8650,11 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8582
8650
8583
8651
let holder_max_accepted_htlcs = if self.context.holder_max_accepted_htlcs == DEFAULT_MAX_HTLCS { None } else { Some(self.context.holder_max_accepted_htlcs) };
8584
8652
8653
+ let mut monitor_pending_update_adds = None;
8654
+ if !self.context.monitor_pending_update_adds.is_empty() {
8655
+ monitor_pending_update_adds = Some(&self.context.monitor_pending_update_adds);
8656
+ }
8657
+
8585
8658
write_tlv_fields!(writer, {
8586
8659
(0, self.context.announcement_sigs, option),
8587
8660
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -8599,6 +8672,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
8599
8672
(7, self.context.shutdown_scriptpubkey, option),
8600
8673
(8, self.context.blocked_monitor_updates, optional_vec),
8601
8674
(9, self.context.target_closing_feerate_sats_per_kw, option),
8675
+ (10, monitor_pending_update_adds, option), // Added in 0.0.122
8602
8676
(11, self.context.monitor_pending_finalized_fulfills, required_vec),
8603
8677
(13, self.context.channel_creation_height, required),
8604
8678
(15, preimages, required_vec),
@@ -8693,8 +8767,22 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
8693
8767
cltv_expiry: Readable::read(reader)?,
8694
8768
payment_hash: Readable::read(reader)?,
8695
8769
state: match <u8 as Readable>::read(reader)? {
8696
- 1 => InboundHTLCState::AwaitingRemoteRevokeToAnnounce(Readable::read(reader)?),
8697
- 2 => InboundHTLCState::AwaitingAnnouncedRemoteRevoke(Readable::read(reader)?),
8770
+ 1 => {
8771
+ let resolution = if ver <= 3 {
8772
+ InboundHTLCResolution::Resolved { pending_htlc_status: Readable::read(reader)? }
8773
+ } else {
8774
+ Readable::read(reader)?
8775
+ };
8776
+ InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution)
8777
+ },
8778
+ 2 => {
8779
+ let resolution = if ver <= 3 {
8780
+ InboundHTLCResolution::Resolved { pending_htlc_status: Readable::read(reader)? }
8781
+ } else {
8782
+ Readable::read(reader)?
8783
+ };
8784
+ InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution)
8785
+ },
8698
8786
3 => InboundHTLCState::Committed,
8699
8787
4 => InboundHTLCState::LocalRemoved(Readable::read(reader)?),
8700
8788
_ => return Err(DecodeError::InvalidValue),
@@ -8911,6 +8999,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
8911
8999
let mut holding_cell_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
8912
9000
8913
9001
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
9002
+ let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
8914
9003
8915
9004
read_tlv_fields!(reader, {
8916
9005
(0, announcement_sigs, option),
@@ -8923,6 +9012,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
8923
9012
(7, shutdown_scriptpubkey, option),
8924
9013
(8, blocked_monitor_updates, optional_vec),
8925
9014
(9, target_closing_feerate_sats_per_kw, option),
9015
+ (10, monitor_pending_update_adds, option), // Added in 0.0.122
8926
9016
(11, monitor_pending_finalized_fulfills, optional_vec),
8927
9017
(13, channel_creation_height, option),
8928
9018
(15, preimages_opt, optional_vec),
@@ -9094,6 +9184,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9094
9184
monitor_pending_forwards,
9095
9185
monitor_pending_failures,
9096
9186
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
9187
+ monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or(Vec::new()),
9097
9188
9098
9189
signer_pending_commitment_update: false,
9099
9190
signer_pending_funding: false,
0 commit comments