Skip to content

Commit ad05fc8

Browse files
Simplify InboundHTLCStates' htlc resolution type
Previously, several variants of InboundHTLCState contained an InboundHTLCResolution enum. Now that that the resolution enum only has one variant, we can get rid of it and simplify the parent InboundHTLCState type.
1 parent ab24eca commit ad05fc8

File tree

1 file changed

+74
-60
lines changed

1 file changed

+74
-60
lines changed

lightning/src/ln/channel.rs

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,11 @@ enum InboundHTLCRemovalReason {
144144
Fulfill { preimage: PaymentPreimage, attribution_data: Option<AttributionData> },
145145
}
146146

147-
/// Represents the resolution status of an inbound HTLC.
148-
#[cfg_attr(test, derive(Debug))]
149-
#[derive(Clone)]
150-
enum InboundHTLCResolution {
151-
/// Pending implies we will attempt to resolve the inbound HTLC once it has been fully committed
152-
/// to by both sides of the channel, i.e., once a `revoke_and_ack` has been processed by both
153-
/// nodes for the state update in which it was proposed.
154-
Pending { update_add_htlc: msgs::UpdateAddHTLC },
155-
}
156-
157-
impl_writeable_tlv_based_enum!(InboundHTLCResolution,
158-
// 0 used to be used for InboundHTLCResolution::Resolved in 0.2 and below.
159-
(2, Pending) => {
160-
(0, update_add_htlc, required),
161-
},
162-
);
163-
164147
#[cfg_attr(test, derive(Debug))]
165148
enum InboundHTLCState {
166149
/// Offered by remote, to be included in next local commitment tx. I.e., the remote sent an
167150
/// update_add_htlc message for this HTLC.
168-
RemoteAnnounced(InboundHTLCResolution),
151+
RemoteAnnounced(msgs::UpdateAddHTLC),
169152
/// Included in a received commitment_signed message (implying we've
170153
/// revoke_and_ack'd it), but the remote hasn't yet revoked their previous
171154
/// state (see the example below). We have not yet included this HTLC in a
@@ -195,13 +178,13 @@ enum InboundHTLCState {
195178
/// Implies AwaitingRemoteRevoke.
196179
///
197180
/// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
198-
AwaitingRemoteRevokeToAnnounce(InboundHTLCResolution),
181+
AwaitingRemoteRevokeToAnnounce(msgs::UpdateAddHTLC),
199182
/// Included in a received commitment_signed message (implying we've revoke_and_ack'd it).
200183
/// We have also included this HTLC in our latest commitment_signed and are now just waiting
201184
/// on the remote's revoke_and_ack to make this HTLC an irrevocable part of the state of the
202185
/// channel (before it can then get forwarded and/or removed).
203186
/// Implies AwaitingRemoteRevoke.
204-
AwaitingAnnouncedRemoteRevoke(InboundHTLCResolution),
187+
AwaitingAnnouncedRemoteRevoke(msgs::UpdateAddHTLC),
205188
/// An HTLC irrevocably committed in the latest commitment transaction, ready to be forwarded or
206189
/// removed.
207190
Committed {
@@ -286,12 +269,10 @@ impl InboundHTLCState {
286269
/// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc
287270
fn should_hold_htlc(&self) -> bool {
288271
match self {
289-
InboundHTLCState::RemoteAnnounced(res)
290-
| InboundHTLCState::AwaitingRemoteRevokeToAnnounce(res)
291-
| InboundHTLCState::AwaitingAnnouncedRemoteRevoke(res) => match res {
292-
InboundHTLCResolution::Pending { update_add_htlc } => {
293-
update_add_htlc.hold_htlc.is_some()
294-
},
272+
InboundHTLCState::RemoteAnnounced(update_add_htlc)
273+
| InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add_htlc)
274+
| InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add_htlc) => {
275+
update_add_htlc.hold_htlc.is_some()
295276
},
296277
InboundHTLCState::Committed { .. } | InboundHTLCState::LocalRemoved(_) => false,
297278
}
@@ -7761,9 +7742,7 @@ where
77617742
amount_msat: msg.amount_msat,
77627743
payment_hash: msg.payment_hash,
77637744
cltv_expiry: msg.cltv_expiry,
7764-
state: InboundHTLCState::RemoteAnnounced(InboundHTLCResolution::Pending {
7765-
update_add_htlc: msg.clone(),
7766-
}),
7745+
state: InboundHTLCState::RemoteAnnounced(msg.clone())
77677746
});
77687747
Ok(())
77697748
}
@@ -8223,11 +8202,10 @@ where
82238202
}
82248203

82258204
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
8226-
if let &InboundHTLCState::RemoteAnnounced(ref htlc_resolution) = &htlc.state {
8205+
if let &InboundHTLCState::RemoteAnnounced(ref update_add) = &htlc.state {
82278206
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToAnnounce due to commitment_signed in channel {}.",
82288207
&htlc.payment_hash, &self.context.channel_id);
8229-
htlc.state =
8230-
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(htlc_resolution.clone());
8208+
htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add.clone());
82318209
need_commitment = true;
82328210
}
82338211
}
@@ -8732,22 +8710,21 @@ where
87328710
let mut state = InboundHTLCState::Committed { update_add_htlc_opt: None };
87338711
mem::swap(&mut state, &mut htlc.state);
87348712

8735-
if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution) = state {
8713+
if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add) = state {
87368714
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
8737-
htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution);
8715+
htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add);
87388716
require_commitment = true;
8739-
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution) =
8717+
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add) =
87408718
state
87418719
{
8742-
match resolution {
8743-
InboundHTLCResolution::Pending { update_add_htlc } => {
8744-
log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed", &htlc.payment_hash);
8745-
pending_update_adds.push(update_add_htlc.clone());
8746-
htlc.state = InboundHTLCState::Committed {
8747-
update_add_htlc_opt: Some(update_add_htlc),
8748-
};
8749-
},
8750-
}
8720+
log_trace!(
8721+
logger,
8722+
" ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed",
8723+
&htlc.payment_hash
8724+
);
8725+
pending_update_adds.push(update_add.clone());
8726+
htlc.state =
8727+
InboundHTLCState::Committed { update_add_htlc_opt: Some(update_add) };
87518728
}
87528729
}
87538730
}
@@ -12741,10 +12718,10 @@ where
1274112718
// is acceptable.
1274212719
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
1274312720
let new_state =
12744-
if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref forward_info) =
12721+
if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref update_add) =
1274512722
&htlc.state
1274612723
{
12747-
Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info.clone()))
12724+
Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add.clone()))
1274812725
} else {
1274912726
None
1275012727
};
@@ -14450,6 +14427,41 @@ impl Readable for AnnouncementSigsState {
1445014427
}
1445114428
}
1445214429

14430+
/// Represents the resolution status of an inbound HTLC. Previously we had multiple variants here,
14431+
/// now we only use it for backwards compatibility when (de)serializing [`InboundHTLCState`]s.
14432+
enum WriteableLegacyHTLCResolution<'a> {
14433+
Pending { update_add_htlc: &'a msgs::UpdateAddHTLC },
14434+
}
14435+
14436+
// We can't use `impl_writeable_tlv_based_enum` due to the lifetime.
14437+
impl<'a> Writeable for WriteableLegacyHTLCResolution<'a> {
14438+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
14439+
match self {
14440+
Self::Pending { update_add_htlc } => {
14441+
2u8.write(writer)?;
14442+
crate::_encode_varint_length_prefixed_tlv!(writer, {
14443+
(0, update_add_htlc, required)
14444+
});
14445+
},
14446+
}
14447+
14448+
Ok(())
14449+
}
14450+
}
14451+
14452+
/// Represents the resolution status of an inbound HTLC. Previously we had multiple variants here,
14453+
/// now we only use it for backwards compatibility when (de)serializing [`InboundHTLCState`]s.
14454+
enum ReadableLegacyHTLCResolution {
14455+
Pending { update_add_htlc: msgs::UpdateAddHTLC },
14456+
}
14457+
14458+
impl_writeable_tlv_based_enum!(ReadableLegacyHTLCResolution,
14459+
// 0 used to be used for the ::Resolved variant in 0.2 and below.
14460+
(2, Pending) => {
14461+
(0, update_add_htlc, required),
14462+
},
14463+
);
14464+
1445314465
impl<SP: Deref> Writeable for FundedChannel<SP>
1445414466
where
1445514467
SP::Target: SignerProvider,
@@ -14537,13 +14549,13 @@ where
1453714549
htlc.payment_hash.write(writer)?;
1453814550
match &htlc.state {
1453914551
&InboundHTLCState::RemoteAnnounced(_) => unreachable!(),
14540-
&InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_resolution) => {
14552+
&InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref update_add_htlc) => {
1454114553
1u8.write(writer)?;
14542-
htlc_resolution.write(writer)?;
14554+
WriteableLegacyHTLCResolution::Pending { update_add_htlc }.write(writer)?;
1454314555
},
14544-
&InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_resolution) => {
14556+
&InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref update_add_htlc) => {
1454514557
2u8.write(writer)?;
14546-
htlc_resolution.write(writer)?;
14558+
WriteableLegacyHTLCResolution::Pending { update_add_htlc }.write(writer)?;
1454714559
},
1454814560
&InboundHTLCState::Committed { ref update_add_htlc_opt } => {
1454914561
3u8.write(writer)?;
@@ -14990,18 +15002,20 @@ where
1499015002
payment_hash: Readable::read(reader)?,
1499115003
state: match <u8 as Readable>::read(reader)? {
1499215004
1 => {
14993-
let resolution = Readable::read(reader).map_err(|e| {
14994-
log_error!(logger, "Found deprecated HTLC received on LDK 0.0.123 or earlier. HTLC must be resolved before upgrading to LDK 0.3+, see CHANGELOG.md");
14995-
e
14996-
})?;
14997-
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution)
15005+
let ReadableLegacyHTLCResolution::Pending { update_add_htlc } =
15006+
Readable::read(reader).map_err(|e| {
15007+
log_error!(logger, "Found deprecated HTLC received on LDK 0.0.123 or earlier. HTLC must be resolved before upgrading to LDK 0.3+, see CHANGELOG.md");
15008+
e
15009+
})?;
15010+
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add_htlc)
1499815011
},
1499915012
2 => {
15000-
let resolution = Readable::read(reader).map_err(|e| {
15001-
log_error!(logger, "Found deprecated HTLC received on LDK 0.0.123 or earlier. HTLC must be resolved before upgrading to LDK 0.3+, see CHANGELOG.md");
15002-
e
15003-
})?;
15004-
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution)
15013+
let ReadableLegacyHTLCResolution::Pending { update_add_htlc } =
15014+
Readable::read(reader).map_err(|e| {
15015+
log_error!(logger, "Found deprecated HTLC received on LDK 0.0.123 or earlier. HTLC must be resolved before upgrading to LDK 0.3+, see CHANGELOG.md");
15016+
e
15017+
})?;
15018+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add_htlc)
1500515019
},
1500615020
3 => InboundHTLCState::Committed { update_add_htlc_opt: None },
1500715021
4 => {

0 commit comments

Comments
 (0)