Skip to content

Commit dc13dce

Browse files
committed
Add PaymentHash to Record
Adding the `payment_hash` to `Record` so we are able to track it in logs.
1 parent 68d5e88 commit dc13dce

File tree

7 files changed

+147
-127
lines changed

7 files changed

+147
-127
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ where C::Target: chain::Filter,
790790
let monitors = self.monitors.read().unwrap();
791791
match monitors.get(&funding_txo) {
792792
None => {
793-
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id));
793+
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(channel_id), None);
794794
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
795795

796796
// 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
@@ -896,25 +896,28 @@ pub(super) struct WithChannelContext<'a, L: Deref> where L::Target: Logger {
896896
pub logger: &'a L,
897897
pub peer_id: Option<PublicKey>,
898898
pub channel_id: Option<ChannelId>,
899+
pub payment_hash: Option<PaymentHash>,
899900
}
900901

901902
impl<'a, L: Deref> Logger for WithChannelContext<'a, L> where L::Target: Logger {
902903
fn log(&self, mut record: Record) {
903904
record.peer_id = self.peer_id;
904905
record.channel_id = self.channel_id;
906+
record.payment_hash = self.payment_hash;
905907
self.logger.log(record)
906908
}
907909
}
908910

909911
impl<'a, 'b, L: Deref> WithChannelContext<'a, L>
910912
where L::Target: Logger {
911-
pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>) -> Self
913+
pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>, payment_hash: Option<PaymentHash>) -> Self
912914
where S::Target: SignerProvider
913915
{
914916
WithChannelContext {
915917
logger,
916918
peer_id: Some(context.counterparty_node_id),
917919
channel_id: Some(context.channel_id),
920+
payment_hash
918921
}
919922
}
920923
}
@@ -1540,7 +1543,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
15401543
L::Target: Logger,
15411544
SP::Target: SignerProvider,
15421545
{
1543-
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(open_channel_fields.temporary_channel_id));
1546+
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(open_channel_fields.temporary_channel_id), None);
15441547
let announced_channel = if (open_channel_fields.channel_flags & 1) == 1 { true } else { false };
15451548

15461549
let channel_value_satoshis = our_funding_satoshis.saturating_add(open_channel_fields.funding_satoshis);
@@ -7703,7 +7706,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
77037706
F::Target: FeeEstimator,
77047707
L::Target: Logger,
77057708
{
7706-
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(msg.common_fields.temporary_channel_id));
7709+
let logger = WithContext::from(logger, Some(counterparty_node_id), Some(msg.common_fields.temporary_channel_id), None);
77077710

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

lightning/src/ln/channelmanager.rs

Lines changed: 70 additions & 67 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
@@ -748,13 +748,12 @@ where
748748
&self, contents: T, destination: Destination, reply_path: Option<BlindedPath>,
749749
log_suffix: fmt::Arguments
750750
) -> Result<SendSuccess, SendError> {
751-
let mut logger = WithContext::from(&self.logger, None, None);
752-
let result = self.find_path(destination)
753-
.and_then(|path| {
754-
let first_hop = path.intermediate_nodes.get(0).map(|p| *p);
755-
logger = WithContext::from(&self.logger, first_hop, None);
756-
self.enqueue_onion_message(path, contents, reply_path, log_suffix)
757-
});
751+
let mut logger = WithContext::from(&self.logger, None, None, None);
752+
let result = self.find_path(destination).and_then(|path| {
753+
let first_hop = path.intermediate_nodes.get(0).map(|p| *p);
754+
logger = WithContext::from(&self.logger, first_hop, None, None);
755+
self.enqueue_onion_message(path, contents, reply_path, log_suffix)
756+
});
758757

759758
match result.as_ref() {
760759
Err(SendError::GetNodeIdFailed) => {
@@ -932,7 +931,7 @@ where
932931
CMH::Target: CustomOnionMessageHandler,
933932
{
934933
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
935-
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None);
934+
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
936935
match self.peel_onion_message(msg) {
937936
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
938937
log_trace!(

lightning/src/util/logger.rs

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

23-
use crate::ln::ChannelId;
23+
use crate::ln::{ChannelId, PaymentHash};
2424
#[cfg(c_bindings)]
2525
use crate::prelude::*; // Needed for String
2626

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

125130
impl<$($args)?> Record<$($args)?> {
@@ -129,7 +134,8 @@ impl<$($args)?> Record<$($args)?> {
129134
#[inline]
130135
pub fn new<$($nonstruct_args)?>(
131136
level: Level, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
132-
args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32
137+
args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32,
138+
payment_hash: Option<PaymentHash>
133139
) -> Record<$($args)?> {
134140
Record {
135141
level,
@@ -142,6 +148,7 @@ impl<$($args)?> Record<$($args)?> {
142148
module_path,
143149
file,
144150
line,
151+
payment_hash,
145152
}
146153
}
147154
}
@@ -168,6 +175,8 @@ pub struct WithContext<'a, L: Deref> where L::Target: Logger {
168175
peer_id: Option<PublicKey>,
169176
/// The channel id of the channel pertaining to the logged record.
170177
channel_id: Option<ChannelId>,
178+
/// The payment hash of the payment pertaining to the logged record.
179+
payment_hash: Option<PaymentHash>
171180
}
172181

173182
impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
@@ -178,17 +187,21 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
178187
if self.channel_id.is_some() {
179188
record.channel_id = self.channel_id;
180189
}
190+
if self.payment_hash.is_some() {
191+
record.payment_hash = self.payment_hash;
192+
}
181193
self.logger.log(record)
182194
}
183195
}
184196

185197
impl<'a, L: Deref> WithContext<'a, L> where L::Target: Logger {
186198
/// 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 {
199+
pub fn from(logger: &'a L, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>, payment_hash: Option<PaymentHash>) -> Self {
188200
WithContext {
189201
logger,
190202
peer_id,
191203
channel_id,
204+
payment_hash,
192205
}
193206
}
194207
}
@@ -244,7 +257,7 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
244257
#[cfg(test)]
245258
mod tests {
246259
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
247-
use crate::ln::ChannelId;
260+
use crate::ln::{ChannelId, PaymentHash};
248261
use crate::util::logger::{Logger, Level, WithContext};
249262
use crate::util::test_utils::TestLogger;
250263
use crate::sync::Arc;
@@ -291,7 +304,8 @@ mod tests {
291304
let logger = &TestLogger::new();
292305
let secp_ctx = Secp256k1::new();
293306
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])));
307+
let payment_hash = PaymentHash([0; 32]);
308+
let context_logger = WithContext::from(&logger, Some(pk), Some(ChannelId([0; 32])), Some(payment_hash));
295309
log_error!(context_logger, "This is an error");
296310
log_warn!(context_logger, "This is an error");
297311
log_debug!(context_logger, "This is an error");
@@ -308,8 +322,9 @@ mod tests {
308322
let logger = &TestLogger::new();
309323
let secp_ctx = Secp256k1::new();
310324
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);
325+
let payment_hash = PaymentHash([0; 32]);
326+
let context_logger = &WithContext::from(&logger, None, Some(ChannelId([0; 32])), Some(payment_hash));
327+
let full_context_logger = WithContext::from(&context_logger, Some(pk), None, None);
313328
log_error!(full_context_logger, "This is an error");
314329
log_warn!(full_context_logger, "This is an error");
315330
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)