Skip to content

Commit 4d1dcb6

Browse files
committed
Add hold times to update_fulfill_htlc
1 parent db180e2 commit 4d1dcb6

File tree

7 files changed

+221
-38
lines changed

7 files changed

+221
-38
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,8 +2887,12 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
28872887
as_raa = Some(get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, node_b_id));
28882888
}
28892889

2890-
let fulfill_msg =
2891-
msgs::UpdateFulfillHTLC { channel_id: chan_id_2, htlc_id: 0, payment_preimage };
2890+
let fulfill_msg = msgs::UpdateFulfillHTLC {
2891+
channel_id: chan_id_2,
2892+
htlc_id: 0,
2893+
payment_preimage,
2894+
attribution_data: None,
2895+
};
28922896
if second_fails {
28932897
nodes[2].node.fail_htlc_backwards(&payment_hash);
28942898
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(
@@ -2904,8 +2908,14 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
29042908

29052909
let cs_updates = get_htlc_update_msgs!(nodes[2], node_b_id);
29062910
assert_eq!(cs_updates.update_fulfill_htlcs.len(), 1);
2907-
// Check that the message we're about to deliver matches the one generated:
2908-
assert_eq!(fulfill_msg, cs_updates.update_fulfill_htlcs[0]);
2911+
2912+
// Check that the message we're about to deliver matches the one generated. Ignore attribution data.
2913+
assert_eq!(fulfill_msg.channel_id, cs_updates.update_fulfill_htlcs[0].channel_id);
2914+
assert_eq!(fulfill_msg.htlc_id, cs_updates.update_fulfill_htlcs[0].htlc_id);
2915+
assert_eq!(
2916+
fulfill_msg.payment_preimage,
2917+
cs_updates.update_fulfill_htlcs[0].payment_preimage
2918+
);
29092919
}
29102920
nodes[1].node.handle_update_fulfill_htlc(node_c_id, &fulfill_msg);
29112921
expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);

lightning/src/ln/channel.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ enum FeeUpdateState {
138138
enum InboundHTLCRemovalReason {
139139
FailRelay(msgs::OnionErrorPacket),
140140
FailMalformed(([u8; 32], u16)),
141-
Fulfill(PaymentPreimage),
141+
Fulfill(PaymentPreimage, Option<AttributionData>),
142142
}
143143

144144
/// Represents the resolution status of an inbound HTLC.
@@ -234,7 +234,7 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
234234
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail),
235235
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(_)) =>
236236
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail),
237-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) =>
237+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_, _)) =>
238238
Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill),
239239
}
240240
}
@@ -266,7 +266,7 @@ impl InboundHTLCState {
266266

267267
fn preimage(&self) -> Option<PaymentPreimage> {
268268
match self {
269-
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(preimage)) => {
269+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(preimage, _)) => {
270270
Some(*preimage)
271271
},
272272
_ => None,
@@ -466,6 +466,7 @@ enum HTLCUpdateAwaitingACK {
466466
},
467467
ClaimHTLC {
468468
payment_preimage: PaymentPreimage,
469+
attribution_data: AttributionData,
469470
htlc_id: u64,
470471
},
471472
FailHTLC {
@@ -6212,7 +6213,7 @@ where
62126213
// (see equivalent if condition there).
62136214
assert!(!self.context.channel_state.can_generate_new_commitment());
62146215
let mon_update_id = self.context.latest_monitor_update_id; // Forget the ChannelMonitor update
6215-
let fulfill_resp = self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, None, logger);
6216+
let fulfill_resp = self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, None, AttributionData::new(), logger);
62166217
self.context.latest_monitor_update_id = mon_update_id;
62176218
if let UpdateFulfillFetch::NewClaim { update_blocked, .. } = fulfill_resp {
62186219
assert!(update_blocked); // The HTLC must have ended up in the holding cell.
@@ -6222,7 +6223,7 @@ where
62226223
#[rustfmt::skip]
62236224
fn get_update_fulfill_htlc<L: Deref>(
62246225
&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage,
6225-
payment_info: Option<PaymentClaimDetails>, logger: &L,
6226+
payment_info: Option<PaymentClaimDetails>, attribution_data: AttributionData, logger: &L,
62266227
) -> UpdateFulfillFetch where L::Target: Logger {
62276228
// Either ChannelReady got set (which means it won't be unset) or there is no way any
62286229
// caller thought we could have something claimed (cause we wouldn't have accepted in an
@@ -6246,7 +6247,7 @@ where
62466247
match htlc.state {
62476248
InboundHTLCState::Committed => {},
62486249
InboundHTLCState::LocalRemoved(ref reason) => {
6249-
if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
6250+
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
62506251
} else {
62516252
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());
62526253
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
@@ -6312,6 +6313,7 @@ where
63126313
log_trace!(logger, "Adding HTLC claim to holding_cell in channel {}! Current state: {}", &self.context.channel_id(), self.context.channel_state.to_u32());
63136314
self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
63146315
payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
6316+
attribution_data,
63156317
});
63166318
return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, update_blocked: true };
63176319
}
@@ -6324,7 +6326,7 @@ where
63246326
return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, update_blocked: true };
63256327
}
63266328
log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", &htlc.payment_hash, &self.context.channel_id);
6327-
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
6329+
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone(), Some(attribution_data)));
63286330
}
63296331

63306332
UpdateFulfillFetch::NewClaim {
@@ -6337,10 +6339,10 @@ where
63376339
#[rustfmt::skip]
63386340
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(
63396341
&mut self, htlc_id: u64, payment_preimage: PaymentPreimage,
6340-
payment_info: Option<PaymentClaimDetails>, logger: &L,
6342+
payment_info: Option<PaymentClaimDetails>, attribution_data: AttributionData, logger: &L,
63416343
) -> UpdateFulfillCommitFetch where L::Target: Logger {
63426344
let release_cs_monitor = self.context.blocked_monitor_updates.is_empty();
6343-
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, payment_info, logger) {
6345+
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, payment_info, attribution_data, logger) {
63446346
UpdateFulfillFetch::NewClaim { mut monitor_update, htlc_value_msat, update_blocked } => {
63456347
// Even if we aren't supposed to let new monitor updates with commitment state
63466348
// updates run, we still need to push the preimage ChannelMonitorUpdateStep no
@@ -6658,7 +6660,7 @@ where
66586660
}
66596661

66606662
#[rustfmt::skip]
6661-
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64, Option<u64>), ChannelError> {
6663+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64, Option<u64>, Option<Duration>), ChannelError> {
66626664
if self.context.channel_state.is_remote_stfu_sent() || self.context.channel_state.is_quiescent() {
66636665
return Err(ChannelError::WarnAndDisconnect("Got fulfill HTLC message while quiescent".to_owned()));
66646666
}
@@ -6669,7 +6671,7 @@ where
66696671
return Err(ChannelError::close("Peer sent update_fulfill_htlc when we needed a channel_reestablish".to_owned()));
66706672
}
66716673

6672-
self.mark_outbound_htlc_removed(msg.htlc_id, OutboundHTLCOutcome::Success(msg.payment_preimage)).map(|htlc| (htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat))
6674+
self.mark_outbound_htlc_removed(msg.htlc_id, OutboundHTLCOutcome::Success(msg.payment_preimage)).map(|htlc| (htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp))
66736675
}
66746676

66756677
#[rustfmt::skip]
@@ -7160,7 +7162,7 @@ where
71607162
}
71617163
None
71627164
},
7163-
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
7165+
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, ref attribution_data } => {
71647166
// If an HTLC claim was previously added to the holding cell (via
71657167
// `get_update_fulfill_htlc`, then generating the claim message itself must
71667168
// not fail - any in between attempts to claim the HTLC will have resulted
@@ -7173,7 +7175,7 @@ where
71737175
// We do not bother to track and include `payment_info` here, however.
71747176
let mut additional_monitor_update =
71757177
if let UpdateFulfillFetch::NewClaim { monitor_update, .. } =
7176-
self.get_update_fulfill_htlc(htlc_id, *payment_preimage, None, logger)
7178+
self.get_update_fulfill_htlc(htlc_id, *payment_preimage, None, attribution_data.clone(), logger)
71777179
{ monitor_update } else { unreachable!() };
71787180
update_fulfill_count += 1;
71797181
monitor_update.updates.append(&mut additional_monitor_update.updates);
@@ -7376,7 +7378,7 @@ where
73767378
pending_inbound_htlcs.retain(|htlc| {
73777379
if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
73787380
log_trace!(logger, " ...removing inbound LocalRemoved {}", &htlc.payment_hash);
7379-
if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
7381+
if let &InboundHTLCRemovalReason::Fulfill(_, _) = reason {
73807382
value_to_self_msat_diff += htlc.amount_msat as i64;
73817383
}
73827384
*expecting_peer_commitment_signed = true;
@@ -8242,11 +8244,12 @@ where
82428244
failure_code: failure_code.clone(),
82438245
});
82448246
},
8245-
&InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
8247+
&InboundHTLCRemovalReason::Fulfill(ref payment_preimage, ref attribution_data) => {
82468248
update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
82478249
channel_id: self.context.channel_id(),
82488250
htlc_id: htlc.htlc_id,
82498251
payment_preimage: payment_preimage.clone(),
8252+
attribution_data: attribution_data.clone(),
82508253
});
82518254
},
82528255
}
@@ -12338,7 +12341,7 @@ where
1233812341
1u8.write(writer)?;
1233912342
(hash, code).write(writer)?;
1234012343
},
12341-
InboundHTLCRemovalReason::Fulfill(preimage) => {
12344+
InboundHTLCRemovalReason::Fulfill(preimage, _) => { // TODO: Persistence
1234212345
2u8.write(writer)?;
1234312346
preimage.write(writer)?;
1234412347
},
@@ -12417,7 +12420,7 @@ where
1241712420
holding_cell_skimmed_fees.push(skimmed_fee_msat);
1241812421
holding_cell_blinding_points.push(blinding_point);
1241912422
},
12420-
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id } => {
12423+
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, ref htlc_id, .. } => {
1242112424
1u8.write(writer)?;
1242212425
payment_preimage.write(writer)?;
1242312426
htlc_id.write(writer)?;
@@ -12704,7 +12707,7 @@ where
1270412707
attribution_data: None,
1270512708
}),
1270612709
1 => InboundHTLCRemovalReason::FailMalformed(Readable::read(reader)?),
12707-
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
12710+
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?, None), // TODO: Persistence
1270812711
_ => return Err(DecodeError::InvalidValue),
1270912712
};
1271012713
InboundHTLCState::LocalRemoved(reason)
@@ -12777,6 +12780,7 @@ where
1277712780
1 => HTLCUpdateAwaitingACK::ClaimHTLC {
1277812781
payment_preimage: Readable::read(reader)?,
1277912782
htlc_id: Readable::read(reader)?,
12783+
attribution_data: AttributionData::new(), // TODO: Persistence
1278012784
},
1278112785
2 => HTLCUpdateAwaitingACK::FailHTLC {
1278212786
htlc_id: Readable::read(reader)?,
@@ -13282,7 +13286,7 @@ where
1328213286
}
1328313287
}
1328413288

13285-
fn duration_since_epoch() -> Option<Duration> {
13289+
pub(crate) fn duration_since_epoch() -> Option<Duration> {
1328613290
#[cfg(not(feature = "std"))]
1328713291
let now = None;
1328813292

@@ -14003,6 +14007,7 @@ mod tests {
1400314007
let dummy_holding_cell_claim_htlc = HTLCUpdateAwaitingACK::ClaimHTLC {
1400414008
payment_preimage: PaymentPreimage([42; 32]),
1400514009
htlc_id: 0,
14010+
attribution_data: AttributionData::new(),
1400614011
};
1400714012
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
1400814013
htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some(AttributionData::new()) }

0 commit comments

Comments
 (0)