Skip to content

Commit 3fb329b

Browse files
committed
Add PaymentHash to Record
Adding the `payment_hash` to `Record` so we are able to track it in logs.
1 parent 38690bf commit 3fb329b

File tree

7 files changed

+148
-126
lines changed

7 files changed

+148
-126
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ where C::Target: chain::Filter,
760760
let monitors = self.monitors.read().unwrap();
761761
match monitors.get(&funding_txo) {
762762
None => {
763-
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id));
763+
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id), None);
764764
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
765765

766766
// We should never ever trigger this from within ChannelManager. Technically a

lightning/src/ln/channel.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,25 +923,28 @@ pub(super) struct WithChannelContext<'a, L: Deref> where L::Target: Logger {
923923
pub logger: &'a L,
924924
pub peer_id: Option<PublicKey>,
925925
pub channel_id: Option<ChannelId>,
926+
pub payment_hash: Option<PaymentHash>,
926927
}
927928

928929
impl<'a, L: Deref> Logger for WithChannelContext<'a, L> where L::Target: Logger {
929930
fn log(&self, mut record: Record) {
930931
record.peer_id = self.peer_id;
931932
record.channel_id = self.channel_id;
933+
record.payment_hash = self.payment_hash;
932934
self.logger.log(record)
933935
}
934936
}
935937

936938
impl<'a, 'b, L: Deref> WithChannelContext<'a, L>
937939
where L::Target: Logger {
938-
pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>) -> Self
940+
pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>, payment_hash: Option<PaymentHash>) -> Self
939941
where S::Target: SignerProvider
940942
{
941943
WithChannelContext {
942944
logger,
943945
peer_id: Some(context.counterparty_node_id),
944946
channel_id: Some(context.channel_id),
947+
payment_hash
945948
}
946949
}
947950
}
@@ -1571,7 +1574,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
15711574
L::Target: Logger,
15721575
SP::Target: SignerProvider,
15731576
{
1574-
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(open_channel_fields.temporary_channel_id));
1577+
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(open_channel_fields.temporary_channel_id), None);
15751578
let announced_channel = if (open_channel_fields.channel_flags & 1) == 1 { true } else { false };
15761579

15771580
let channel_value_satoshis = our_funding_satoshis.saturating_add(open_channel_fields.funding_satoshis);
@@ -7859,7 +7862,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
78597862
F::Target: FeeEstimator,
78607863
L::Target: Logger,
78617864
{
7862-
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(msg.common_fields.temporary_channel_id));
7865+
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(msg.common_fields.temporary_channel_id), None);
78637866

78647867
// First check the channel type is known, failing before we do anything else if we don't
78657868
// support this channel type.

lightning/src/ln/channelmanager.rs

Lines changed: 71 additions & 68 deletions
Large diffs are not rendered by default.

lightning/src/ln/peer_handler.rs

Lines changed: 40 additions & 40 deletions
Large diffs are not rendered by default.

lightning/src/onion_message/messenger.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -877,13 +877,12 @@ where
877877
&self, contents: T, destination: Destination, reply_path: Option<BlindedPath>,
878878
log_suffix: fmt::Arguments
879879
) -> Result<SendSuccess, SendError> {
880-
let mut logger = WithContext::from(&self.logger, None, None);
881-
let result = self.find_path(destination)
882-
.and_then(|path| {
883-
let first_hop = path.intermediate_nodes.get(0).map(|p| *p);
884-
logger = WithContext::from(&self.logger, first_hop, None);
885-
self.enqueue_onion_message(path, contents, reply_path, log_suffix)
886-
});
880+
let mut logger = WithContext::from(&self.logger, None, None, None);
881+
let result = self.find_path(destination).and_then(|path| {
882+
let first_hop = path.intermediate_nodes.get(0).map(|p| *p);
883+
logger = WithContext::from(&self.logger, first_hop, None, None);
884+
self.enqueue_onion_message(path, contents, reply_path, log_suffix)
885+
});
887886

888887
match result.as_ref() {
889888
Err(SendError::GetNodeIdFailed) => {
@@ -1063,7 +1062,7 @@ where
10631062
CMH::Target: CustomOnionMessageHandler,
10641063
{
10651064
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
1066-
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None);
1065+
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
10671066
match self.peel_onion_message(msg) {
10681067
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
10691068
log_trace!(

lightning/src/util/logger.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::fmt;
2121
use core::ops::Deref;
2222

2323
use crate::ln::types::ChannelId;
24+
use crate::ln::PaymentHash;
2425
#[cfg(c_bindings)]
2526
use crate::prelude::*; // Needed for String
2627

@@ -120,6 +121,11 @@ pub struct Record<$($args)?> {
120121
pub file: &'static str,
121122
/// The line containing the message.
122123
pub line: u32,
124+
/// The payment hash.
125+
///
126+
/// Note that this is only filled in for logs pertaining to a specific payment, and will be
127+
/// `None` for logs which are not directly related to a payment.
128+
pub payment_hash: Option<PaymentHash>,
123129
}
124130

125131
impl<$($args)?> Record<$($args)?> {
@@ -129,7 +135,8 @@ impl<$($args)?> Record<$($args)?> {
129135
#[inline]
130136
pub fn new<$($nonstruct_args)?>(
131137
level: Level, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
132-
args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32
138+
args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32,
139+
payment_hash: Option<PaymentHash>
133140
) -> Record<$($args)?> {
134141
Record {
135142
level,
@@ -142,6 +149,7 @@ impl<$($args)?> Record<$($args)?> {
142149
module_path,
143150
file,
144151
line,
152+
payment_hash,
145153
}
146154
}
147155
}
@@ -168,6 +176,8 @@ pub struct WithContext<'a, L: Deref> where L::Target: Logger {
168176
peer_id: Option<PublicKey>,
169177
/// The channel id of the channel pertaining to the logged record.
170178
channel_id: Option<ChannelId>,
179+
/// The payment hash of the payment pertaining to the logged record.
180+
payment_hash: Option<PaymentHash>
171181
}
172182

173183
impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
@@ -178,17 +188,21 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
178188
if self.channel_id.is_some() {
179189
record.channel_id = self.channel_id;
180190
}
191+
if self.payment_hash.is_some() {
192+
record.payment_hash = self.payment_hash;
193+
}
181194
self.logger.log(record)
182195
}
183196
}
184197

185198
impl<'a, L: Deref> WithContext<'a, L> where L::Target: Logger {
186199
/// Wraps the given logger, providing additional context to any logged records.
187-
pub fn from(logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>) -> Self {
200+
pub fn from(logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>, payment_hash: Option<PaymentHash>) -> Self {
188201
WithContext {
189202
logger,
190203
peer_id,
191204
channel_id,
205+
payment_hash,
192206
}
193207
}
194208
}
@@ -245,6 +259,7 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
245259
mod tests {
246260
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
247261
use crate::ln::types::ChannelId;
262+
use crate::ln::PaymentHash;
248263
use crate::util::logger::{Logger, Level, WithContext};
249264
use crate::util::test_utils::TestLogger;
250265
use crate::sync::Arc;
@@ -291,7 +306,8 @@ mod tests {
291306
let logger = &TestLogger::new();
292307
let secp_ctx = Secp256k1::new();
293308
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
294-
let context_logger = WithContext::from(&logger, Some(pk), Some(ChannelId([0; 32])));
309+
let payment_hash = PaymentHash([0; 32]);
310+
let context_logger = WithContext::from(&logger, Some(pk), Some(ChannelId([0; 32])), Some(payment_hash));
295311
log_error!(context_logger, "This is an error");
296312
log_warn!(context_logger, "This is an error");
297313
log_debug!(context_logger, "This is an error");
@@ -308,8 +324,9 @@ mod tests {
308324
let logger = &TestLogger::new();
309325
let secp_ctx = Secp256k1::new();
310326
let pk = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
311-
let context_logger = &WithContext::from(&logger, None, Some(ChannelId([0; 32])));
312-
let full_context_logger = WithContext::from(&context_logger, Some(pk), None);
327+
let payment_hash = PaymentHash([0; 32]);
328+
let context_logger = &WithContext::from(&logger, None, Some(ChannelId([0; 32])), Some(payment_hash));
329+
let full_context_logger = WithContext::from(&context_logger, Some(pk), None, None);
313330
log_error!(full_context_logger, "This is an error");
314331
log_warn!(full_context_logger, "This is an error");
315332
log_debug!(full_context_logger, "This is an error");

lightning/src/util/macro_logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ macro_rules! log_spendable {
148148
#[macro_export]
149149
macro_rules! log_internal {
150150
($logger: expr, $lvl:expr, $($arg:tt)+) => (
151-
$logger.log($crate::util::logger::Record::new($lvl, None, None, format_args!($($arg)+), module_path!(), file!(), line!()))
151+
$logger.log($crate::util::logger::Record::new($lvl, None, None, format_args!($($arg)+), module_path!(), file!(), line!(), None))
152152
);
153153
}
154154

0 commit comments

Comments
 (0)