Skip to content

Commit 37e57dd

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

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
}
@@ -7763,9 +7744,7 @@ where
77637744
amount_msat: msg.amount_msat,
77647745
payment_hash: msg.payment_hash,
77657746
cltv_expiry: msg.cltv_expiry,
7766-
state: InboundHTLCState::RemoteAnnounced(InboundHTLCResolution::Pending {
7767-
update_add_htlc: msg.clone(),
7768-
}),
7747+
state: InboundHTLCState::RemoteAnnounced(msg.clone())
77697748
});
77707749
Ok(())
77717750
}
@@ -8244,11 +8223,10 @@ where
82448223
}
82458224

82468225
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
8247-
if let &InboundHTLCState::RemoteAnnounced(ref htlc_resolution) = &htlc.state {
8226+
if let &InboundHTLCState::RemoteAnnounced(ref update_add) = &htlc.state {
82488227
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToAnnounce due to commitment_signed in channel {}.",
82498228
&htlc.payment_hash, &self.context.channel_id);
8250-
htlc.state =
8251-
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(htlc_resolution.clone());
8229+
htlc.state = InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add.clone());
82528230
need_commitment = true;
82538231
}
82548232
}
@@ -8760,22 +8738,21 @@ where
87608738
let mut state = InboundHTLCState::Committed { update_add_htlc_opt: None };
87618739
mem::swap(&mut state, &mut htlc.state);
87628740

8763-
if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution) = state {
8741+
if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add) = state {
87648742
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
8765-
htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution);
8743+
htlc.state = InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add);
87668744
require_commitment = true;
8767-
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution) =
8745+
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add) =
87688746
state
87698747
{
8770-
match resolution {
8771-
InboundHTLCResolution::Pending { update_add_htlc } => {
8772-
log_trace!(logger, " ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed", &htlc.payment_hash);
8773-
pending_update_adds.push(update_add_htlc.clone());
8774-
htlc.state = InboundHTLCState::Committed {
8775-
update_add_htlc_opt: Some(update_add_htlc),
8776-
};
8777-
},
8778-
}
8748+
log_trace!(
8749+
logger,
8750+
" ...promoting inbound AwaitingAnnouncedRemoteRevoke {} to Committed",
8751+
&htlc.payment_hash
8752+
);
8753+
pending_update_adds.push(update_add.clone());
8754+
htlc.state =
8755+
InboundHTLCState::Committed { update_add_htlc_opt: Some(update_add) };
87798756
}
87808757
}
87818758
}
@@ -12778,10 +12755,10 @@ where
1277812755
// is acceptable.
1277912756
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
1278012757
let new_state =
12781-
if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref forward_info) =
12758+
if let &InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref update_add) =
1278212759
&htlc.state
1278312760
{
12784-
Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(forward_info.clone()))
12761+
Some(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add.clone()))
1278512762
} else {
1278612763
None
1278712764
};
@@ -14553,6 +14530,41 @@ impl Readable for AnnouncementSigsState {
1455314530
}
1455414531
}
1455514532

14533+
/// Represents the resolution status of an inbound HTLC. Previously we had multiple variants here,
14534+
/// now we only use it for backwards compatibility when (de)serializing [`InboundHTLCState`]s.
14535+
enum WriteableLegacyHTLCResolution<'a> {
14536+
Pending { update_add_htlc: &'a msgs::UpdateAddHTLC },
14537+
}
14538+
14539+
// We can't use `impl_writeable_tlv_based_enum` due to the lifetime.
14540+
impl<'a> Writeable for WriteableLegacyHTLCResolution<'a> {
14541+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
14542+
match self {
14543+
Self::Pending { update_add_htlc } => {
14544+
2u8.write(writer)?;
14545+
crate::_encode_varint_length_prefixed_tlv!(writer, {
14546+
(0, update_add_htlc, required)
14547+
});
14548+
},
14549+
}
14550+
14551+
Ok(())
14552+
}
14553+
}
14554+
14555+
/// Represents the resolution status of an inbound HTLC. Previously we had multiple variants here,
14556+
/// now we only use it for backwards compatibility when (de)serializing [`InboundHTLCState`]s.
14557+
enum ReadableLegacyHTLCResolution {
14558+
Pending { update_add_htlc: msgs::UpdateAddHTLC },
14559+
}
14560+
14561+
impl_writeable_tlv_based_enum!(ReadableLegacyHTLCResolution,
14562+
// 0 used to be used for the ::Resolved variant in 0.2 and below.
14563+
(2, Pending) => {
14564+
(0, update_add_htlc, required),
14565+
},
14566+
);
14567+
1455614568
impl<SP: Deref> Writeable for FundedChannel<SP>
1455714569
where
1455814570
SP::Target: SignerProvider,
@@ -14640,13 +14652,13 @@ where
1464014652
htlc.payment_hash.write(writer)?;
1464114653
match &htlc.state {
1464214654
&InboundHTLCState::RemoteAnnounced(_) => unreachable!(),
14643-
&InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref htlc_resolution) => {
14655+
&InboundHTLCState::AwaitingRemoteRevokeToAnnounce(ref update_add_htlc) => {
1464414656
1u8.write(writer)?;
14645-
htlc_resolution.write(writer)?;
14657+
WriteableLegacyHTLCResolution::Pending { update_add_htlc }.write(writer)?;
1464614658
},
14647-
&InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref htlc_resolution) => {
14659+
&InboundHTLCState::AwaitingAnnouncedRemoteRevoke(ref update_add_htlc) => {
1464814660
2u8.write(writer)?;
14649-
htlc_resolution.write(writer)?;
14661+
WriteableLegacyHTLCResolution::Pending { update_add_htlc }.write(writer)?;
1465014662
},
1465114663
&InboundHTLCState::Committed { ref update_add_htlc_opt } => {
1465214664
3u8.write(writer)?;
@@ -15093,18 +15105,20 @@ where
1509315105
payment_hash: Readable::read(reader)?,
1509415106
state: match <u8 as Readable>::read(reader)? {
1509515107
1 => {
15096-
let resolution = Readable::read(reader).map_err(|e| {
15097-
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");
15098-
e
15099-
})?;
15100-
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution)
15108+
let ReadableLegacyHTLCResolution::Pending { update_add_htlc } =
15109+
Readable::read(reader).map_err(|e| {
15110+
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");
15111+
e
15112+
})?;
15113+
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(update_add_htlc)
1510115114
},
1510215115
2 => {
15103-
let resolution = Readable::read(reader).map_err(|e| {
15104-
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");
15105-
e
15106-
})?;
15107-
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution)
15116+
let ReadableLegacyHTLCResolution::Pending { update_add_htlc } =
15117+
Readable::read(reader).map_err(|e| {
15118+
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");
15119+
e
15120+
})?;
15121+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(update_add_htlc)
1510815122
},
1510915123
3 => InboundHTLCState::Committed { update_add_htlc_opt: None },
1511015124
4 => {

0 commit comments

Comments
 (0)