Skip to content

Commit fb1fd4c

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

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

lightning/src/ln/channel.rs

Lines changed: 47 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 { sha256_of_onion: [u8; 32], failure_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
}
@@ -4594,8 +4594,8 @@ where
45944594
.pending_inbound_htlcs
45954595
.iter()
45964596
.filter(|InboundHTLCOutput { state, .. }| match (state, local) {
4597-
(InboundHTLCState::LocalRemoved(Fulfill(_, _)), true) => false,
4598-
(InboundHTLCState::LocalRemoved(Fulfill(_, _)), false) => true,
4597+
(InboundHTLCState::LocalRemoved(Fulfill { .. }), true) => false,
4598+
(InboundHTLCState::LocalRemoved(Fulfill { .. }), false) => true,
45994599
_ => false,
46004600
})
46014601
.map(|InboundHTLCOutput { amount_msat, .. }| amount_msat)
@@ -6809,7 +6809,10 @@ impl FailHTLCContents for ([u8; 32], u16) {
68096809
}
68106810
}
68116811
fn to_inbound_htlc_state(self) -> InboundHTLCState {
6812-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(self))
6812+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed {
6813+
sha256_of_onion: self.0,
6814+
failure_code: self.1,
6815+
})
68136816
}
68146817
fn to_htlc_update_awaiting_ack(self, htlc_id: u64) -> HTLCUpdateAwaitingACK {
68156818
HTLCUpdateAwaitingACK::FailMalformedHTLC {
@@ -7308,7 +7311,7 @@ where
73087311
match htlc.state {
73097312
InboundHTLCState::Committed => {},
73107313
InboundHTLCState::LocalRemoved(ref reason) => {
7311-
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
7314+
if let &InboundHTLCRemovalReason::Fulfill { .. } = reason {
73127315
} else {
73137316
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());
73147317
debug_assert!(
@@ -7416,10 +7419,10 @@ where
74167419
"Upgrading HTLC {} to LocalRemoved with a Fulfill!",
74177420
&htlc.payment_hash,
74187421
);
7419-
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(
7420-
payment_preimage_arg.clone(),
7422+
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill {
7423+
preimage: payment_preimage_arg.clone(),
74217424
attribution_data,
7422-
));
7425+
});
74237426
}
74247427

74257428
UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, update_blocked: false }
@@ -8645,7 +8648,7 @@ where
86458648
pending_inbound_htlcs.retain(|htlc| {
86468649
if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
86478650
log_trace!(logger, " ...removing inbound LocalRemoved {}", &htlc.payment_hash);
8648-
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
8651+
if let &InboundHTLCRemovalReason::Fulfill { .. } = reason {
86498652
value_to_self_msat_diff += htlc.amount_msat as i64;
86508653
}
86518654
*expecting_peer_commitment_signed = true;
@@ -8720,10 +8723,10 @@ where
87208723
},
87218724
HTLCFailureMsg::Malformed(msg) => {
87228725
htlc.state = InboundHTLCState::LocalRemoved(
8723-
InboundHTLCRemovalReason::FailMalformed((
8724-
msg.sha256_of_onion,
8725-
msg.failure_code,
8726-
)),
8726+
InboundHTLCRemovalReason::FailMalformed {
8727+
sha256_of_onion: msg.sha256_of_onion,
8728+
failure_code: msg.failure_code,
8729+
},
87278730
);
87288731
update_fail_malformed_htlcs.push(msg)
87298732
},
@@ -9708,25 +9711,22 @@ where
97089711
attribution_data: err_packet.attribution_data.clone(),
97099712
});
97109713
},
9711-
&InboundHTLCRemovalReason::FailMalformed((
9712-
ref sha256_of_onion,
9713-
ref failure_code,
9714-
)) => {
9714+
&InboundHTLCRemovalReason::FailMalformed {
9715+
sha256_of_onion: ref hash,
9716+
failure_code: ref code,
9717+
} => {
97159718
update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
97169719
channel_id: self.context.channel_id(),
97179720
htlc_id: htlc.htlc_id,
9718-
sha256_of_onion: sha256_of_onion.clone(),
9719-
failure_code: failure_code.clone(),
9721+
sha256_of_onion: hash.clone(),
9722+
failure_code: code.clone(),
97209723
});
97219724
},
9722-
&InboundHTLCRemovalReason::Fulfill(
9723-
ref payment_preimage,
9724-
ref attribution_data,
9725-
) => {
9725+
&InboundHTLCRemovalReason::Fulfill { ref preimage, ref attribution_data } => {
97269726
update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
97279727
channel_id: self.context.channel_id(),
97289728
htlc_id: htlc.htlc_id,
9729-
payment_preimage: payment_preimage.clone(),
9729+
payment_preimage: preimage.clone(),
97309730
attribution_data: attribution_data.clone(),
97319731
});
97329732
},
@@ -14505,11 +14505,14 @@ where
1450514505
data.write(writer)?;
1450614506
removed_htlc_attribution_data.push(&attribution_data);
1450714507
},
14508-
InboundHTLCRemovalReason::FailMalformed((hash, code)) => {
14508+
InboundHTLCRemovalReason::FailMalformed {
14509+
sha256_of_onion: hash,
14510+
failure_code: code,
14511+
} => {
1450914512
1u8.write(writer)?;
1451014513
(hash, code).write(writer)?;
1451114514
},
14512-
InboundHTLCRemovalReason::Fulfill(preimage, attribution_data) => {
14515+
InboundHTLCRemovalReason::Fulfill { preimage, attribution_data } => {
1451314516
2u8.write(writer)?;
1451414517
preimage.write(writer)?;
1451514518
removed_htlc_attribution_data.push(&attribution_data);
@@ -14955,8 +14958,17 @@ where
1495514958
data: Readable::read(reader)?,
1495614959
attribution_data: None,
1495714960
}),
14958-
1 => InboundHTLCRemovalReason::FailMalformed(Readable::read(reader)?),
14959-
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?, None),
14961+
1 => {
14962+
let (hash, code) = Readable::read(reader)?;
14963+
InboundHTLCRemovalReason::FailMalformed {
14964+
sha256_of_onion: hash,
14965+
failure_code: code,
14966+
}
14967+
},
14968+
2 => InboundHTLCRemovalReason::Fulfill {
14969+
preimage: Readable::read(reader)?,
14970+
attribution_data: None,
14971+
},
1496014972
_ => return Err(DecodeError::InvalidValue),
1496114973
};
1496214974
InboundHTLCState::LocalRemoved(reason)
@@ -15414,7 +15426,7 @@ where
1541415426
InboundHTLCRemovalReason::FailRelay(ref mut packet) => {
1541515427
Some(&mut packet.attribution_data)
1541615428
},
15417-
InboundHTLCRemovalReason::Fulfill(_, ref mut attribution_data) => {
15429+
InboundHTLCRemovalReason::Fulfill { ref mut attribution_data, .. } => {
1541815430
Some(attribution_data)
1541915431
},
1542015432
_ => None,

0 commit comments

Comments
 (0)