Skip to content

Commit 07e59a1

Browse files
committed
Add WaitingOnPeer/MonitorPersist spans to inbound HTLCs
1 parent c251a8e commit 07e59a1

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,48 @@ impl InboundHTLCOutput where {
322322
}
323323

324324
fn set_state<L: Deref>(&mut self, state: InboundHTLCState, logger: &L) where L::Target: Logger {
325+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
326+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
325327
mem::drop(self.state_wrapper.span.take());
326328
self.state_wrapper = InboundHTLCStateWrapper::new(
327329
state,
328330
self.span.as_user_span_ref::<L>(),
329331
logger,
330332
);
331333
}
334+
335+
fn waiting_on_peer<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
336+
self.state_wrapper.waiting_on_peer_span = Some(BoxedSpan::new(logger.start(
337+
Span::WaitingOnPeer,
338+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
339+
)));
340+
}
341+
342+
fn peer_responded(&mut self) {
343+
if self.state_wrapper.waiting_on_peer_span.is_some() {
344+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
345+
}
346+
}
347+
348+
fn waiting_on_monitor_persist<L: Deref>(&mut self, logger: &L) where L::Target: Logger {
349+
self.state_wrapper.waiting_on_monitor_persist_span = Some(BoxedSpan::new(logger.start(
350+
Span::WaitingOnMonitorPersist,
351+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
352+
)));
353+
}
354+
355+
fn monitor_persisted(&mut self) {
356+
if self.state_wrapper.waiting_on_monitor_persist_span.is_some() {
357+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
358+
}
359+
}
332360
}
333361

334362
struct InboundHTLCStateWrapper {
335363
state: InboundHTLCState,
364+
waiting_on_peer_span: Option<BoxedSpan>,
365+
waiting_on_monitor_persist_span: Option<BoxedSpan>,
366+
// Drop full span last.
336367
span: Option<BoxedSpan>,
337368
}
338369

@@ -349,6 +380,8 @@ impl InboundHTLCStateWrapper {
349380
InboundHTLCStateWrapper {
350381
state,
351382
span: Some(BoxedSpan::new(state_span)),
383+
waiting_on_peer_span: None,
384+
waiting_on_monitor_persist_span: None,
352385
}
353386
}
354387
}
@@ -5656,6 +5689,7 @@ impl<SP: Deref> FundedChannel<SP> where
56565689
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone())),
56575690
logger,
56585691
);
5692+
htlc.waiting_on_monitor_persist(logger);
56595693
}
56605694

56615695
UpdateFulfillFetch::NewClaim {
@@ -6009,7 +6043,7 @@ impl<SP: Deref> FundedChannel<SP> where
60096043

60106044
// Now update local state:
60116045
self.context.next_counterparty_htlc_id += 1;
6012-
self.context.pending_inbound_htlcs.push(InboundHTLCOutput::new(
6046+
let mut output = InboundHTLCOutput::new(
60136047
self.context.channel_id(),
60146048
InboundHTLCOutputParams {
60156049
htlc_id: msg.htlc_id,
@@ -6021,7 +6055,9 @@ impl<SP: Deref> FundedChannel<SP> where
60216055
}),
60226056
},
60236057
logger,
6024-
));
6058+
);
6059+
output.waiting_on_peer(logger);
6060+
self.context.pending_inbound_htlcs.push(output);
60256061

60266062
Ok(())
60276063
}
@@ -6241,6 +6277,9 @@ impl<SP: Deref> FundedChannel<SP> where
62416277
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(htlc_resolution.clone()),
62426278
logger,
62436279
);
6280+
if self.context.channel_state.is_awaiting_remote_revoke() {
6281+
htlc.waiting_on_peer(logger);
6282+
}
62446283
need_commitment = true;
62456284
}
62466285
}
@@ -6622,6 +6661,7 @@ impl<SP: Deref> FundedChannel<SP> where
66226661
if let InboundHTLCState::AwaitingRemoteRevokeToAnnounce(resolution) = state {
66236662
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
66246663
htlc.set_state(InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution), logger);
6664+
htlc.waiting_on_monitor_persist(logger);
66256665
require_commitment = true;
66266666
} else if let InboundHTLCState::AwaitingAnnouncedRemoteRevoke(resolution) = state {
66276667
match resolution {
@@ -7074,6 +7114,17 @@ impl<SP: Deref> FundedChannel<SP> where
70747114
assert!(self.context.channel_state.is_monitor_update_in_progress());
70757115
self.context.channel_state.clear_monitor_update_in_progress();
70767116

7117+
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
7118+
match htlc.state() {
7119+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(_) |
7120+
InboundHTLCState::LocalRemoved(_) => {
7121+
htlc.monitor_persisted();
7122+
htlc.waiting_on_peer(logger);
7123+
},
7124+
_ => {},
7125+
}
7126+
}
7127+
70777128
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
70787129
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
70797130
// first received the funding_signed.
@@ -9385,6 +9436,7 @@ impl<SP: Deref> FundedChannel<SP> where
93859436
if let Some(state) = new_state {
93869437
log_trace!(logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to AwaitingAnnouncedRemoteRevoke", &htlc.payment_hash);
93879438
htlc.set_state(state, logger);
9439+
htlc.waiting_on_monitor_persist(logger);
93889440
}
93899441
}
93909442
for htlc in self.context.pending_outbound_htlcs.iter_mut() {

lightning/src/ln/functional_tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11721,6 +11721,11 @@ pub fn test_payment_traces() {
1172111721
Span::InboundHTLCState { state: None },
1172211722
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1172311723
),
11724+
TestSpanBoundary::Start(
11725+
Span::WaitingOnPeer,
11726+
Some(Span::InboundHTLCState { state: None }),
11727+
),
11728+
TestSpanBoundary::End(Span::WaitingOnPeer),
1172411729
TestSpanBoundary::End(Span::InboundHTLCState { state: None }),
1172511730
TestSpanBoundary::Start(
1172611731
Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd) },
@@ -11731,6 +11736,16 @@ pub fn test_payment_traces() {
1173111736
Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd) },
1173211737
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1173311738
),
11739+
TestSpanBoundary::Start(
11740+
Span::WaitingOnMonitorPersist,
11741+
Some(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd) }),
11742+
),
11743+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist),
11744+
TestSpanBoundary::Start(
11745+
Span::WaitingOnPeer,
11746+
Some(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd) }),
11747+
),
11748+
TestSpanBoundary::End(Span::WaitingOnPeer),
1173411749
TestSpanBoundary::End(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd) }),
1173511750
TestSpanBoundary::Start(
1173611751
Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::Committed) },
@@ -11757,6 +11772,15 @@ pub fn test_payment_traces() {
1175711772
Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill) },
1175811773
Some(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 }),
1175911774
),
11775+
TestSpanBoundary::Start(
11776+
Span::WaitingOnMonitorPersist,
11777+
Some(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill) }),
11778+
),
11779+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist),
11780+
TestSpanBoundary::Start(
11781+
Span::WaitingOnPeer,
11782+
Some(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill) }),
11783+
),
1176011784
TestSpanBoundary::End(Span::OutboundHTLCState { state: OutboundHTLCStateDetails::Committed }),
1176111785
TestSpanBoundary::Start(
1176211786
Span::OutboundHTLCState { state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess },
@@ -11770,6 +11794,7 @@ pub fn test_payment_traces() {
1177011794
TestSpanBoundary::End(Span::OutboundHTLCState { state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess }),
1177111795
TestSpanBoundary::End(Span::OutboundHTLC { channel_id: channel_id_2, htlc_id: 0 }),
1177211796
TestSpanBoundary::End(Span::Forward),
11797+
TestSpanBoundary::End(Span::WaitingOnPeer),
1177311798
TestSpanBoundary::End(Span::InboundHTLCState { state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill) }),
1177411799
TestSpanBoundary::End(Span::InboundHTLC { channel_id: channel_id_1, htlc_id: 0 })
1177511800
]);

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)