Skip to content

Commit 04f7037

Browse files
committed
Add WaitingOnPeer/MonitorPersist spans to outbound HTLCs
1 parent fd35c5f commit 04f7037

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,52 @@ impl OutboundHTLCOutput {
593593
where
594594
L::Target: Logger,
595595
{
596+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
597+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
596598
mem::drop(self.state_wrapper.span.take());
597599
self.state_wrapper =
598600
OutboundHTLCStateWrapper::new(state, self.span.as_user_span_ref::<L>(), logger);
599601
}
602+
603+
fn waiting_on_peer<L: Deref>(&mut self, logger: &L)
604+
where
605+
L::Target: Logger,
606+
{
607+
self.state_wrapper.waiting_on_peer_span = Some(BoxedSpan::new(logger.start(
608+
Span::WaitingOnPeer,
609+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
610+
)));
611+
}
612+
613+
fn peer_responded(&mut self) {
614+
if self.state_wrapper.waiting_on_peer_span.is_some() {
615+
mem::drop(self.state_wrapper.waiting_on_peer_span.take());
616+
}
617+
}
618+
619+
fn waiting_on_monitor_persist<L: Deref>(&mut self, logger: &L)
620+
where
621+
L::Target: Logger,
622+
{
623+
self.state_wrapper.waiting_on_monitor_persist_span = Some(BoxedSpan::new(logger.start(
624+
Span::WaitingOnMonitorPersist,
625+
self.state_wrapper.span.as_ref().map(|s| s.as_user_span_ref::<L>()).flatten(),
626+
)));
627+
}
628+
629+
fn monitor_persisted(&mut self) {
630+
if self.state_wrapper.waiting_on_monitor_persist_span.is_some() {
631+
mem::drop(self.state_wrapper.waiting_on_monitor_persist_span.take());
632+
}
633+
}
600634
}
601635

602636
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
603637
struct OutboundHTLCStateWrapper {
604638
state: OutboundHTLCState,
639+
waiting_on_peer_span: Option<BoxedSpan>,
640+
waiting_on_monitor_persist_span: Option<BoxedSpan>,
641+
// Drop full span last.
605642
span: Option<BoxedSpan>,
606643
}
607644

@@ -615,7 +652,12 @@ impl OutboundHTLCStateWrapper {
615652
{
616653
let state_span =
617654
logger.start(Span::OutboundHTLCState { state: (&state).into() }, parent_span);
618-
OutboundHTLCStateWrapper { state, span: Some(BoxedSpan::new(state_span)) }
655+
OutboundHTLCStateWrapper {
656+
state,
657+
span: Some(BoxedSpan::new(state_span)),
658+
waiting_on_peer_span: None,
659+
waiting_on_monitor_persist_span: None,
660+
}
619661
}
620662
}
621663

@@ -6866,6 +6908,7 @@ where
68666908
return Err(ChannelError::close(format!("Remote tried to fulfill/fail HTLC ({}) before it had been committed", htlc_id))),
68676909
OutboundHTLCState::Committed => {
68686910
htlc.set_state(OutboundHTLCState::RemoteRemoved(outcome), logger);
6911+
htlc.waiting_on_peer(logger);
68696912
},
68706913
OutboundHTLCState::AwaitingRemoteRevokeToRemove(_) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) | OutboundHTLCState::RemoteRemoved(_) =>
68716914
return Err(ChannelError::close(format!("Remote tried to fulfill/fail HTLC ({}) that they'd already fulfilled/failed", htlc_id))),
@@ -7284,6 +7327,9 @@ where
72847327
claimed_htlcs.push((SentHTLCId::from_source(&htlc.source), preimage));
72857328
}
72867329
htlc.set_state(OutboundHTLCState::AwaitingRemoteRevokeToRemove(reason), logger);
7330+
if self.context.channel_state.is_awaiting_remote_revoke() {
7331+
htlc.waiting_on_peer(logger);
7332+
}
72877333
need_commitment = true;
72887334
}
72897335
}
@@ -7849,6 +7895,7 @@ where
78497895
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
78507896
mem::swap(outcome, &mut reason);
78517897
htlc.set_state(OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason), logger);
7898+
htlc.waiting_on_monitor_persist(logger);
78527899
require_commitment = true;
78537900
}
78547901
}
@@ -8288,6 +8335,16 @@ where
82888335
_ => {},
82898336
}
82908337
}
8338+
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
8339+
match htlc.state() {
8340+
OutboundHTLCState::LocalAnnounced(_) |
8341+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(_) => {
8342+
htlc.monitor_persisted();
8343+
htlc.waiting_on_peer(logger);
8344+
},
8345+
_ => {},
8346+
}
8347+
}
82918348

82928349
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
82938350
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
@@ -10964,7 +11021,7 @@ where
1096411021
// that are simple to implement, and we do it on the outgoing side because then the failure message that encodes
1096511022
// the hold time still needs to be built in channel manager.
1096611023
let send_timestamp = duration_since_epoch();
10967-
self.context.pending_outbound_htlcs.push(OutboundHTLCOutput::new(
11024+
let mut htlc = OutboundHTLCOutput::new(
1096811025
self.context.channel_id(),
1096911026
OutboundHTLCOutputParams {
1097011027
htlc_id: self.context.next_holder_htlc_id,
@@ -10979,7 +11036,9 @@ where
1097911036
},
1098011037
forward_span,
1098111038
logger,
10982-
));
11039+
);
11040+
htlc.waiting_on_monitor_persist(logger);
11041+
self.context.pending_outbound_htlcs.push(htlc);
1098311042
self.context.next_holder_htlc_id += 1;
1098411043

1098511044
Ok(true)
@@ -11029,6 +11088,7 @@ where
1102911088
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
1103011089
mem::swap(outcome, &mut reason);
1103111090
htlc.set_state(OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason), logger);
11091+
htlc.waiting_on_monitor_persist(logger);
1103211092
}
1103311093
}
1103411094
if let Some((feerate, update_state)) = self.context.pending_update_fee {

lightning/src/ln/functional_tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11797,6 +11797,20 @@ pub fn test_payment_traces() {
1179711797
},
1179811798
Some(Span::OutboundHTLC { channel_id: channel_id_2, htlc_id: 0 }),
1179911799
),
11800+
TestSpanBoundary::Start(
11801+
Span::WaitingOnMonitorPersist,
11802+
Some(Span::OutboundHTLCState {
11803+
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd
11804+
}),
11805+
),
11806+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist,),
11807+
TestSpanBoundary::Start(
11808+
Span::WaitingOnPeer,
11809+
Some(Span::OutboundHTLCState {
11810+
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd
11811+
}),
11812+
),
11813+
TestSpanBoundary::End(Span::WaitingOnPeer,),
1180011814
TestSpanBoundary::End(Span::OutboundHTLCState {
1180111815
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd
1180211816
}),
@@ -11811,6 +11825,10 @@ pub fn test_payment_traces() {
1181111825
Span::OutboundHTLCState { state: OutboundHTLCStateDetails::Committed },
1181211826
Some(Span::OutboundHTLC { channel_id: channel_id_2, htlc_id: 0 }),
1181311827
),
11828+
TestSpanBoundary::Start(
11829+
Span::WaitingOnPeer,
11830+
Some(Span::OutboundHTLCState { state: OutboundHTLCStateDetails::Committed }),
11831+
),
1181411832
TestSpanBoundary::End(Span::InboundHTLCState {
1181511833
state: Some(InboundHTLCStateDetails::Committed)
1181611834
}),
@@ -11833,6 +11851,7 @@ pub fn test_payment_traces() {
1183311851
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill)
1183411852
}),
1183511853
),
11854+
TestSpanBoundary::End(Span::WaitingOnPeer),
1183611855
TestSpanBoundary::End(Span::OutboundHTLCState {
1183711856
state: OutboundHTLCStateDetails::Committed
1183811857
}),
@@ -11851,6 +11870,20 @@ pub fn test_payment_traces() {
1185111870
},
1185211871
Some(Span::OutboundHTLC { channel_id: channel_id_2, htlc_id: 0 }),
1185311872
),
11873+
TestSpanBoundary::Start(
11874+
Span::WaitingOnMonitorPersist,
11875+
Some(Span::OutboundHTLCState {
11876+
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess
11877+
}),
11878+
),
11879+
TestSpanBoundary::End(Span::WaitingOnMonitorPersist),
11880+
TestSpanBoundary::Start(
11881+
Span::WaitingOnPeer,
11882+
Some(Span::OutboundHTLCState {
11883+
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess
11884+
}),
11885+
),
11886+
TestSpanBoundary::End(Span::WaitingOnPeer),
1185411887
TestSpanBoundary::End(Span::OutboundHTLCState {
1185511888
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess
1185611889
}),

0 commit comments

Comments
 (0)