Skip to content

Commit fd35c5f

Browse files
committed
Add WaitingOnPeer/MonitorPersist spans to inbound HTLCs
1 parent 480a669 commit fd35c5f

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,51 @@ impl InboundHTLCOutput {
336336
where
337337
L::Target: Logger,
338338
{
339+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
340+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
339341
mem::drop(self.state_wrapper.span.take());
340342
self.state_wrapper =
341343
InboundHTLCStateWrapper::new(state, self.span.as_user_span_ref::<L>(), logger);
342344
}
345+
346+
fn waiting_on_peer<L: Deref>(&mut self, logger: &L)
347+
where
348+
L::Target: Logger,
349+
{
350+
self.state_wrapper.waiting_on_peer_span = Some(BoxedSpan::new(logger.start(
351+
Span::WaitingOnPeer,
352+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
353+
)));
354+
}
355+
356+
fn peer_responded(&mut self) {
357+
if self.state_wrapper.waiting_on_peer_span.is_some() {
358+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
359+
}
360+
}
361+
362+
fn waiting_on_monitor_persist<L: Deref>(&mut self, logger: &L)
363+
where
364+
L::Target: Logger,
365+
{
366+
self.state_wrapper.waiting_on_monitor_persist_span = Some(BoxedSpan::new(logger.start(
367+
Span::WaitingOnMonitorPersist,
368+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
369+
)));
370+
}
371+
372+
fn monitor_persisted(&mut self) {
373+
if self.state_wrapper.waiting_on_monitor_persist_span.is_some() {
374+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
375+
}
376+
}
343377
}
344378

345379
struct InboundHTLCStateWrapper {
346380
state: InboundHTLCState,
381+
waiting_on_peer_span: Option<BoxedSpan>,
382+
waiting_on_monitor_persist_span: Option<BoxedSpan>,
383+
// Drop full span last.
347384
span: Option<BoxedSpan>,
348385
}
349386

@@ -357,7 +394,12 @@ impl InboundHTLCStateWrapper {
357394
{
358395
let state_span =
359396
logger.start(Span::InboundHTLCState { state: (&state).into() }, parent_span);
360-
InboundHTLCStateWrapper { state, span: Some(BoxedSpan::new(state_span)) }
397+
InboundHTLCStateWrapper {
398+
state,
399+
span: Some(BoxedSpan::new(state_span)),
400+
waiting_on_peer_span: None,
401+
waiting_on_monitor_persist_span: None,
402+
}
361403
}
362404
}
363405

@@ -6474,6 +6516,7 @@ where
64746516
)),
64756517
logger,
64766518
);
6519+
htlc.waiting_on_monitor_persist(logger);
64776520
}
64786521

64796522
UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, update_blocked: false }
@@ -6785,7 +6828,7 @@ where
67856828

67866829
// Now update local state:
67876830
self.context.next_counterparty_htlc_id += 1;
6788-
self.context.pending_inbound_htlcs.push(InboundHTLCOutput::new(
6831+
let mut output = InboundHTLCOutput::new(
67896832
self.context.channel_id(),
67906833
InboundHTLCOutputParams {
67916834
htlc_id: msg.htlc_id,
@@ -6797,7 +6840,9 @@ where
67976840
}),
67986841
},
67996842
logger,
6800-
));
6843+
);
6844+
output.waiting_on_peer(logger);
6845+
self.context.pending_inbound_htlcs.push(output);
68016846

68026847
Ok(())
68036848
}
@@ -7213,6 +7258,9 @@ where
72137258
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(htlc_resolution.clone()),
72147259
logger,
72157260
);
7261+
if self.context.channel_state.is_awaiting_remote_revoke() {
7262+
htlc.waiting_on_peer(logger);
7263+
}
72167264
need_commitment = true;
72177265
}
72187266
}
@@ -7727,6 +7775,7 @@ where
77277775
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution),
77287776
logger,
77297777
);
7778+
htlc.waiting_on_monitor_persist(logger);
77307779
require_commitment = true;
77317780
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution) =
77327781
state
@@ -8229,6 +8278,17 @@ where
82298278
self.context.channel_state.clear_monitor_update_in_progress();
82308279
assert_eq!(self.blocked_monitor_updates_pending(), 0);
82318280

8281+
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
8282+
match htlc.state() {
8283+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) |
8284+
InboundHTLCState::LocalRemoved(_) => {
8285+
htlc.monitor_persisted();
8286+
htlc.waiting_on_peer(logger);
8287+
},
8288+
_ => {},
8289+
}
8290+
}
8291+
82328292
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
82338293
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
82348294
// first received the funding_signed.
@@ -10959,6 +11019,7 @@ where
1095911019
if let Some(state) = new_state {
1096011020
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
1096111021
htlc.set_state(state, logger);
11022+
htlc.waiting_on_monitor_persist(logger);
1096211023
}
1096311024
}
1096411025
for htlc in self.context.pending_outbound_htlcs.iter_mut() {

lightning/src/ln/functional_tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,6 +11741,11 @@ pub fn test_payment_traces() {
1174111741
Span::InboundHTLCState { state: None },
1174211742
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1174311743
),
11744+
TestSpanBoundary::Start(
11745+
Span::WaitingOnPeer,
11746+
Some(Span::InboundHTLCState { state: None }),
11747+
),
11748+
TestSpanBoundary::End(Span::WaitingOnPeer),
1174411749
TestSpanBoundary::End(Span::InboundHTLCState { state: None }),
1174511750
TestSpanBoundary::Start(
1174611751
Span::InboundHTLCState {
@@ -11757,6 +11762,20 @@ pub fn test_payment_traces() {
1175711762
},
1175811763
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1175911764
),
11765+
TestSpanBoundary::Start(
11766+
Span::WaitingOnMonitorPersist,
11767+
Some(Span::InboundHTLCState {
11768+
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd)
11769+
}),
11770+
),
11771+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist),
11772+
TestSpanBoundary::Start(
11773+
Span::WaitingOnPeer,
11774+
Some(Span::InboundHTLCState {
11775+
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd)
11776+
}),
11777+
),
11778+
TestSpanBoundary::End(Span::WaitingOnPeer),
1176011779
TestSpanBoundary::End(Span::InboundHTLCState {
1176111780
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd)
1176211781
}),
@@ -11801,6 +11820,19 @@ pub fn test_payment_traces() {
1180111820
},
1180211821
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1180311822
),
11823+
TestSpanBoundary::Start(
11824+
Span::WaitingOnMonitorPersist,
11825+
Some(Span::InboundHTLCState {
11826+
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill)
11827+
}),
11828+
),
11829+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist),
11830+
TestSpanBoundary::Start(
11831+
Span::WaitingOnPeer,
11832+
Some(Span::InboundHTLCState {
11833+
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill)
11834+
}),
11835+
),
1180411836
TestSpanBoundary::End(Span::OutboundHTLCState {
1180511837
state: OutboundHTLCStateDetails::Committed
1180611838
}),
@@ -11824,6 +11856,7 @@ pub fn test_payment_traces() {
1182411856
}),
1182511857
TestSpanBoundary::End(Span::OutboundHTLC { channel_id: channel_id_2, htlc_id: 0 }),
1182611858
TestSpanBoundary::End(Span::Forward),
11859+
TestSpanBoundary::End(Span::WaitingOnPeer),
1182711860
TestSpanBoundary::End(Span::InboundHTLCState {
1182811861
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill)
1182911862
}),

lightning/src/util/logger.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ pub enum Span {
204204
/// The state.
205205
state: OutboundHTLCStateDetails,
206206
},
207+
/// Span representing the wait time until a peer responds.
208+
WaitingOnPeer,
209+
/// Span representing the wait time until a channel monitor persists.
210+
WaitingOnMonitorPersist,
207211
/// Span representing sending an outbound Ping and receiving an inbound Pong.
208212
PingPong {
209213
/// The node id of the counterparty.

0 commit comments

Comments
 (0)