Skip to content

Commit 938c062

Browse files
committed
Convert InboundHTLCRemovalReason fields to struct
Preparation for serializing the enum. The serialization macros do not support multiple unnamed fields.
1 parent d4d0879 commit 938c062

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

lightning/src/ln/channel.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ enum FeeUpdateState {
141141
#[derive(Debug)]
142142
enum InboundHTLCRemovalReason {
143143
FailRelay(msgs::OnionErrorPacket),
144-
FailMalformed(([u8; 32], u16)),
145-
Fulfill(PaymentPreimage, Option<AttributionData>),
144+
FailMalformed { hash: [u8; 32], code: u16 },
145+
Fulfill { preimage: PaymentPreimage, attribution_data: Option<AttributionData> },
146146
}
147147

148148
/// Represents the resolution status of an inbound HTLC.
@@ -238,9 +238,9 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
238238
Some(InboundHTLCStateDetails::Committed),
239239
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(_)) =>
240240
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail),
241-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(_)) =>
241+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed{..}) =>
242242
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail),
243-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_, _)) =>
243+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill{..}) =>
244244
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill),
245245
}
246246
}
@@ -272,9 +272,9 @@ impl InboundHTLCState {
272272

273273
fn preimage(&self) -> Option<PaymentPreimage> {
274274
match self {
275-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(preimage, _)) => {
276-
Some(*preimage)
277-
},
275+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill {
276+
preimage, ..
277+
}) => Some(*preimage),
278278
_ => None,
279279
}
280280
}
@@ -4569,8 +4569,8 @@ where
45694569
.pending_inbound_htlcs
45704570
.iter()
45714571
.filter(|InboundHTLCOutput { state, .. }| match (state, local) {
4572-
(InboundHTLCState::LocalRemoved(Fulfill(_, _)), true) => false,
4573-
(InboundHTLCState::LocalRemoved(Fulfill(_, _)), false) => true,
4572+
(InboundHTLCState::LocalRemoved(Fulfill { .. }), true) => false,
4573+
(InboundHTLCState::LocalRemoved(Fulfill { .. }), false) => true,
45744574
_ => false,
45754575
})
45764576
.map(|InboundHTLCOutput { amount_msat, .. }| amount_msat)
@@ -6786,7 +6786,10 @@ impl FailHTLCContents for ([u8; 32], u16) {
67866786
}
67876787
}
67886788
fn to_inbound_htlc_state(self) -> InboundHTLCState {
6789-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(self))
6789+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed {
6790+
hash: self.0,
6791+
code: self.1,
6792+
})
67906793
}
67916794
fn to_htlc_update_awaiting_ack(self, htlc_id: u64) -> HTLCUpdateAwaitingACK {
67926795
HTLCUpdateAwaitingACK::FailMalformedHTLC {
@@ -7282,7 +7285,7 @@ where
72827285
match htlc.state {
72837286
InboundHTLCState::Committed => {},
72847287
InboundHTLCState::LocalRemoved(ref reason) => {
7285-
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
7288+
if let &InboundHTLCRemovalReason::Fulfill { .. } = reason {
72867289
} else {
72877290
log_warn!(logger, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", &htlc.payment_hash, &self.context.channel_id());
72887291
debug_assert!(
@@ -7392,10 +7395,10 @@ where
73927395
"Upgrading HTLC {} to LocalRemoved with a Fulfill!",
73937396
&htlc.payment_hash,
73947397
);
7395-
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(
7396-
payment_preimage_arg.clone(),
7398+
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill {
7399+
preimage: payment_preimage_arg.clone(),
73977400
attribution_data,
7398-
));
7401+
});
73997402
}
74007403

74017404
UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, update_blocked: false }
@@ -8656,7 +8659,7 @@ where
86568659
pending_inbound_htlcs.retain(|htlc| {
86578660
if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
86588661
log_trace!(logger, " ...removing inbound LocalRemoved {}", &htlc.payment_hash);
8659-
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
8662+
if let &InboundHTLCRemovalReason::Fulfill { .. } = reason {
86608663
value_to_self_msat_diff += htlc.amount_msat as i64;
86618664
}
86628665
*expecting_peer_commitment_signed = true;
@@ -8731,10 +8734,10 @@ where
87318734
},
87328735
HTLCFailureMsg::Malformed(msg) => {
87338736
htlc.state = InboundHTLCState::LocalRemoved(
8734-
InboundHTLCRemovalReason::FailMalformed((
8735-
msg.sha256_of_onion,
8736-
msg.failure_code,
8737-
)),
8737+
InboundHTLCRemovalReason::FailMalformed {
8738+
hash: msg.sha256_of_onion,
8739+
code: msg.failure_code,
8740+
},
87388741
);
87398742
update_fail_malformed_htlcs.push(msg)
87408743
},
@@ -9710,25 +9713,19 @@ where
97109713
attribution_data: err_packet.attribution_data.clone(),
97119714
});
97129715
},
9713-
&InboundHTLCRemovalReason::FailMalformed((
9714-
ref sha256_of_onion,
9715-
ref failure_code,
9716-
)) => {
9716+
&InboundHTLCRemovalReason::FailMalformed { ref hash, ref code } => {
97179717
update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
97189718
channel_id: self.context.channel_id(),
97199719
htlc_id: htlc.htlc_id,
9720-
sha256_of_onion: sha256_of_onion.clone(),
9721-
failure_code: failure_code.clone(),
9720+
sha256_of_onion: hash.clone(),
9721+
failure_code: code.clone(),
97229722
});
97239723
},
9724-
&InboundHTLCRemovalReason::Fulfill(
9725-
ref payment_preimage,
9726-
ref attribution_data,
9727-
) => {
9724+
&InboundHTLCRemovalReason::Fulfill { ref preimage, ref attribution_data } => {
97289725
update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
97299726
channel_id: self.context.channel_id(),
97309727
htlc_id: htlc.htlc_id,
9731-
payment_preimage: payment_preimage.clone(),
9728+
payment_preimage: preimage.clone(),
97329729
attribution_data: attribution_data.clone(),
97339730
});
97349731
},
@@ -14493,11 +14490,11 @@ where
1449314490
data.write(writer)?;
1449414491
removed_htlc_attribution_data.push(&attribution_data);
1449514492
},
14496-
InboundHTLCRemovalReason::FailMalformed((hash, code)) => {
14493+
InboundHTLCRemovalReason::FailMalformed { hash, code } => {
1449714494
1u8.write(writer)?;
1449814495
(hash, code).write(writer)?;
1449914496
},
14500-
InboundHTLCRemovalReason::Fulfill(preimage, attribution_data) => {
14497+
InboundHTLCRemovalReason::Fulfill { preimage, attribution_data } => {
1450114498
2u8.write(writer)?;
1450214499
preimage.write(writer)?;
1450314500
removed_htlc_attribution_data.push(&attribution_data);
@@ -14937,8 +14934,14 @@ where
1493714934
data: Readable::read(reader)?,
1493814935
attribution_data: None,
1493914936
}),
14940-
1 => InboundHTLCRemovalReason::FailMalformed(Readable::read(reader)?),
14941-
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?, None),
14937+
1 => {
14938+
let (hash, code) = Readable::read(reader)?;
14939+
InboundHTLCRemovalReason::FailMalformed { hash, code }
14940+
},
14941+
2 => InboundHTLCRemovalReason::Fulfill {
14942+
preimage: Readable::read(reader)?,
14943+
attribution_data: None,
14944+
},
1494214945
_ => return Err(DecodeError::InvalidValue),
1494314946
};
1494414947
InboundHTLCState::LocalRemoved(reason)
@@ -15392,7 +15395,7 @@ where
1539215395
InboundHTLCRemovalReason::FailRelay(ref mut packet) => {
1539315396
Some(&mut packet.attribution_data)
1539415397
},
15395-
InboundHTLCRemovalReason::Fulfill(_, ref mut attribution_data) => {
15398+
InboundHTLCRemovalReason::Fulfill { ref mut attribution_data, .. } => {
1539615399
Some(attribution_data)
1539715400
},
1539815401
_ => None,

0 commit comments

Comments
 (0)