Skip to content

Commit ba369c6

Browse files
committed
channel_monitor+: Add monitor update span
Allows us to track the full lifecycle of a monitor update.
1 parent b670bd3 commit ba369c6

File tree

2 files changed

+87
-48
lines changed

2 files changed

+87
-48
lines changed

lexe-ln/src/channel_monitor.rs

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,62 @@ use anyhow::{anyhow, Context};
77
use common::{ln::channel::LxOutPoint, notify_once::NotifyOnce, task::LxTask};
88
use lightning::chain::transaction::OutPoint;
99
use tokio::sync::mpsc;
10-
use tracing::{debug, error, info, info_span};
10+
use tracing::{debug, error, info, info_span, Instrument};
1111

1212
use crate::{
1313
alias::LexeChainMonitorType, traits::LexePersister, BoxedAnyhowFuture,
1414
};
1515

16+
// `api_call_fut` is a future which makes an api call (typically with
17+
// retries) to the backend to persist the channel monitor state, returning
18+
// an `anyhow::Result<()>` once either (1) persistence succeeds or (2)
19+
// there were too many failures to keep trying. We take this future as
20+
// input (instead of e.g. a `VfsFile`) because it is the cleanest and
21+
// easiest way to abstract over the user node and LSP's differing api
22+
// clients, vfs structures, and expected error types.
23+
//
24+
// TODO(max): Add a required `upsert_monitor` method to the `LexePersister`
25+
// trait to avoid this.
26+
pub type MonitorChannelItem = (LxChannelMonitorUpdate, BoxedAnyhowFuture);
27+
1628
/// Represents a channel monitor update. See docs on each field for details.
1729
pub struct LxChannelMonitorUpdate {
18-
pub funding_txo: LxOutPoint,
30+
kind: ChannelMonitorUpdateKind,
31+
funding_txo: LxOutPoint,
1932
/// The ID of the channel monitor update, given by
2033
/// [`ChannelMonitorUpdate::update_id`] or
2134
/// [`ChannelMonitor::get_latest_update_id`].
2235
///
2336
/// [`ChannelMonitorUpdate::update_id`]: lightning::chain::channelmonitor::ChannelMonitorUpdate::update_id
2437
/// [`ChannelMonitor::get_latest_update_id`]: lightning::chain::channelmonitor::ChannelMonitor::get_latest_update_id
25-
pub update_id: u64,
26-
/// A future which makes an api call (typically with retries) to the
27-
/// backend to persist the channel monitor state, returning an
28-
/// `anyhow::Result<()>` once either (1) persistence succeeds or (2) there
29-
/// were too many failures to keep trying. We take this future as input
30-
/// (instead of e.g. a `VfsFile`) because it is the cleanest and easiest
31-
/// way to abstract over the user node and LSP's differing api clients, vfs
32-
/// structures, and expected error types.
33-
pub api_call_fut: BoxedAnyhowFuture,
34-
pub kind: ChannelMonitorUpdateKind,
38+
update_id: u64,
39+
span: tracing::Span,
40+
}
41+
42+
impl LxChannelMonitorUpdate {
43+
pub fn new(
44+
kind: ChannelMonitorUpdateKind,
45+
funding_txo: LxOutPoint,
46+
update_id: u64,
47+
) -> Self {
48+
let span =
49+
info_span!("(monitor-update)", %kind, %funding_txo, %update_id);
50+
51+
Self {
52+
kind,
53+
funding_txo,
54+
update_id,
55+
span,
56+
}
57+
}
58+
59+
/// The span for this update which includes the full monitor update context.
60+
///
61+
/// Logs related to this monitor update should be logged inside this span,
62+
/// to ensure the log information is associated with this update.
63+
pub fn span(&self) -> tracing::Span {
64+
self.span.clone()
65+
}
3566
}
3667

3768
/// Whether the [`LxChannelMonitorUpdate`] represents a new or updated channel.
@@ -56,7 +87,7 @@ impl Display for ChannelMonitorUpdateKind {
5687
/// channel monitor state.
5788
pub fn spawn_channel_monitor_persister_task<PS>(
5889
chain_monitor: Arc<LexeChainMonitorType<PS>>,
59-
mut channel_monitor_persister_rx: mpsc::Receiver<LxChannelMonitorUpdate>,
90+
mut channel_monitor_persister_rx: mpsc::Receiver<MonitorChannelItem>,
6091
mut shutdown: NotifyOnce,
6192
) -> LxTask<()>
6293
where
@@ -67,15 +98,22 @@ where
6798
LxTask::spawn_with_span(SPAN_NAME, info_span!(SPAN_NAME), async move {
6899
loop {
69100
tokio::select! {
70-
Some(update) = channel_monitor_persister_rx.recv() => {
101+
Some((update, api_call_fut))
102+
= channel_monitor_persister_rx.recv() => {
103+
let update_span = update.span();
71104

72105
let handle_result = handle_update(
73106
chain_monitor.as_ref(),
74107
update,
75-
).await;
108+
api_call_fut,
109+
)
110+
.instrument(update_span.clone())
111+
.await;
76112

77113
if let Err(e) = handle_result {
78-
error!("Monitor persist error: {e:#}");
114+
update_span.in_scope(|| {
115+
error!("Monitor persist error: {e:#}");
116+
});
79117

80118
// Channel monitor persistence errors are serious;
81119
// all errors are considered fatal.
@@ -101,20 +139,20 @@ where
101139
async fn handle_update<PS: LexePersister>(
102140
chain_monitor: &LexeChainMonitorType<PS>,
103141
update: LxChannelMonitorUpdate,
142+
api_call_fut: BoxedAnyhowFuture,
104143
) -> anyhow::Result<()> {
105144
let LxChannelMonitorUpdate {
106145
funding_txo,
107146
update_id,
108-
api_call_fut,
109-
kind,
147+
kind: _,
148+
span: _,
110149
} = update;
111150

112-
debug!(%kind, %funding_txo, %update_id, "Handling channel monitor update");
151+
debug!("Handling channel monitor update");
113152

114153
// Run the persist future.
115154
api_call_fut
116155
.await
117-
.with_context(|| format!("{kind} {funding_txo} {update_id}"))
118156
.context("Channel monitor persist API call failed")?;
119157

120158
// Notify the chain monitor that the monitor update has been persisted.
@@ -127,10 +165,9 @@ async fn handle_update<PS: LexePersister>(
127165
// monitor future.
128166
chain_monitor
129167
.channel_monitor_updated(OutPoint::from(funding_txo), update_id)
130-
.map_err(|e| anyhow!("{kind} {funding_txo} {update_id}: {e:?}"))
131-
.context("channel_monitor_updated returned Err")?;
168+
.map_err(|e| anyhow!("channel_monitor_updated returned Err: {e:?}"))?;
132169

133-
info!(%kind, %funding_txo, %update_id, "Success: persisted monitor");
170+
info!("Success: persisted monitor");
134171

135172
Ok(())
136173
}

node/src/persister/mod.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ use lexe_ln::{
3838
BroadcasterType, ChannelMonitorType, FeeEstimatorType, RouterType,
3939
SignerType,
4040
},
41-
channel_monitor::{ChannelMonitorUpdateKind, LxChannelMonitorUpdate},
41+
channel_monitor::{
42+
ChannelMonitorUpdateKind, LxChannelMonitorUpdate, MonitorChannelItem,
43+
},
4244
keys_manager::LexeKeysManager,
4345
logger::LexeTracingLogger,
4446
payments::{
@@ -91,7 +93,7 @@ pub struct NodePersister {
9193
vfs_master_key: Arc<AesMasterKey>,
9294
google_vfs: Option<Arc<GoogleVfs>>,
9395
shutdown: NotifyOnce,
94-
channel_monitor_persister_tx: mpsc::Sender<LxChannelMonitorUpdate>,
96+
channel_monitor_persister_tx: mpsc::Sender<MonitorChannelItem>,
9597
}
9698

9799
/// General helper for upserting well-formed [`VfsFile`]s.
@@ -301,7 +303,7 @@ impl NodePersister {
301303
vfs_master_key: Arc<AesMasterKey>,
302304
google_vfs: Option<Arc<GoogleVfs>>,
303305
shutdown: NotifyOnce,
304-
channel_monitor_persister_tx: mpsc::Sender<LxChannelMonitorUpdate>,
306+
channel_monitor_persister_tx: mpsc::Sender<MonitorChannelItem>,
305307
) -> Self {
306308
Self {
307309
backend_api,
@@ -778,10 +780,12 @@ impl Persist<SignerType> for NodePersister {
778780
funding_txo: OutPoint,
779781
monitor: &ChannelMonitorType,
780782
) -> ChannelMonitorUpdateStatus {
783+
let kind = ChannelMonitorUpdateKind::New;
781784
let funding_txo = LxOutPoint::from(funding_txo);
782785
let update_id = monitor.get_latest_update_id();
783-
let kind = ChannelMonitorUpdateKind::New;
784-
info!(%kind, %funding_txo, %update_id, "Persisting channel monitor");
786+
let update = LxChannelMonitorUpdate::new(kind, funding_txo, update_id);
787+
let update_span = update.span();
788+
update_span.in_scope(|| info!("Persisting channel monitor"));
785789

786790
let file_id =
787791
VfsFileId::new(CHANNEL_MONITORS_DIR, funding_txo.to_string());
@@ -810,22 +814,20 @@ impl Persist<SignerType> for NodePersister {
810814
}
811815
});
812816

813-
let update = LxChannelMonitorUpdate {
814-
funding_txo,
815-
update_id,
816-
api_call_fut,
817-
kind,
818-
};
819-
820817
// Queue up the channel monitor update for persisting. Shut down if we
821818
// can't send the update for some reason.
822-
if let Err(e) = self.channel_monitor_persister_tx.try_send(update) {
819+
if let Err(e) = self
820+
.channel_monitor_persister_tx
821+
.try_send((update, api_call_fut))
822+
{
823823
// NOTE: Although failing to send the channel monutor update to the
824824
// channel monitor persistence task is a serious error, we do not
825825
// return a PermanentFailure here because that force closes the
826826
// channel, when it is much more likely that it's simply just been
827827
// too long since the last time we synced to the chain tip.
828-
error!("Fatal error: Couldn't send channel monitor update: {e:#}");
828+
update_span.in_scope(|| {
829+
error!("Fatal: Couldn't send channel monitor update: {e:#}");
830+
});
829831
self.shutdown.send();
830832
}
831833

@@ -841,13 +843,15 @@ impl Persist<SignerType> for NodePersister {
841843
update: Option<&ChannelMonitorUpdate>,
842844
monitor: &ChannelMonitorType,
843845
) -> ChannelMonitorUpdateStatus {
846+
let kind = ChannelMonitorUpdateKind::Updated;
844847
let funding_txo = LxOutPoint::from(funding_txo);
845848
let update_id = update
846849
.as_ref()
847850
.map(|u| u.update_id)
848851
.unwrap_or_else(|| monitor.get_latest_update_id());
849-
let kind = ChannelMonitorUpdateKind::Updated;
850-
info!(%kind, %funding_txo, %update_id, "Persisting channel monitor");
852+
let update = LxChannelMonitorUpdate::new(kind, funding_txo, update_id);
853+
let update_span = update.span();
854+
update_span.in_scope(|| info!("Persisting channel monitor"));
851855

852856
let file_id =
853857
VfsFileId::new(CHANNEL_MONITORS_DIR, funding_txo.to_string());
@@ -871,22 +875,20 @@ impl Persist<SignerType> for NodePersister {
871875
}
872876
});
873877

874-
let update = LxChannelMonitorUpdate {
875-
funding_txo,
876-
update_id,
877-
api_call_fut,
878-
kind,
879-
};
880-
881878
// Queue up the channel monitor update for persisting. Shut down if we
882879
// can't send the update for some reason.
883-
if let Err(e) = self.channel_monitor_persister_tx.try_send(update) {
880+
if let Err(e) = self
881+
.channel_monitor_persister_tx
882+
.try_send((update, api_call_fut))
883+
{
884884
// NOTE: Although failing to send the channel monutor update to the
885885
// channel monitor persistence task is a serious error, we do not
886886
// return a PermanentFailure here because that force closes the
887887
// channel, when it is much more likely that it's simply just been
888888
// too long since the last time we synced to the chain tip.
889-
error!("Fatal error: Couldn't send channel monitor update: {e:#}");
889+
update_span.in_scope(|| {
890+
error!("Fatal: Couldn't send channel monitor update: {e:#}");
891+
});
890892
self.shutdown.send();
891893
}
892894

0 commit comments

Comments
 (0)