Skip to content

Commit 0f253c0

Browse files
committed
Use a single WithContext wrapper rather than several log-wrappers
In `ChannelMonitor` logging, we often wrap a logger with `WithChannelMonitor` to automatically include metadata in our structured logging. That's great, except having too many logger wrapping types flying around makes for less compatibility if we have methods that want to require a wrapped-logger. Here we change the `WithChannelMonitor` "constructors" to actually return a `WithContext` instead, making things more consistent.
1 parent 7fe3268 commit 0f253c0

1 file changed

Lines changed: 29 additions & 48 deletions

File tree

lightning/src/chain/channelmonitor.rs

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::sign::{
6666
use crate::types::features::ChannelTypeFeatures;
6767
use crate::types::payment::{PaymentHash, PaymentPreimage};
6868
use crate::util::byte_utils;
69-
use crate::util::logger::{Logger, Record};
69+
use crate::util::logger::{Logger, WithContext};
7070
use crate::util::persist::MonitorName;
7171
use crate::util::ser::{
7272
MaybeReadable, Readable, ReadableArgs, RequiredWrapper, UpgradableRequired, Writeable, Writer,
@@ -1825,45 +1825,27 @@ macro_rules! _process_events_body {
18251825
}
18261826
pub(super) use _process_events_body as process_events_body;
18271827

1828-
pub(crate) struct WithChannelMonitor<'a, L: Deref>
1829-
where
1830-
L::Target: Logger,
1831-
{
1832-
logger: &'a L,
1833-
peer_id: Option<PublicKey>,
1834-
channel_id: Option<ChannelId>,
1835-
payment_hash: Option<PaymentHash>,
1836-
}
1828+
pub(crate) struct WithChannelMonitor;
18371829

1838-
impl<'a, L: Deref> Logger for WithChannelMonitor<'a, L>
1839-
where
1840-
L::Target: Logger,
1841-
{
1842-
fn log(&self, mut record: Record) {
1843-
record.peer_id = self.peer_id;
1844-
record.channel_id = self.channel_id;
1845-
record.payment_hash = self.payment_hash;
1846-
self.logger.log(record)
1847-
}
1848-
}
1849-
1850-
impl<'a, L: Deref> WithChannelMonitor<'a, L>
1851-
where
1852-
L::Target: Logger,
1853-
{
1854-
pub(crate) fn from<S: EcdsaChannelSigner>(
1830+
impl WithChannelMonitor {
1831+
pub(crate) fn from<'a, L: Deref, S: EcdsaChannelSigner>(
18551832
logger: &'a L, monitor: &ChannelMonitor<S>, payment_hash: Option<PaymentHash>,
1856-
) -> Self {
1833+
) -> WithContext<'a, L>
1834+
where
1835+
L::Target: Logger,
1836+
{
18571837
Self::from_impl(logger, &*monitor.inner.lock().unwrap(), payment_hash)
18581838
}
18591839

1860-
#[rustfmt::skip]
1861-
pub(crate) fn from_impl<S: EcdsaChannelSigner>(logger: &'a L, monitor_impl: &ChannelMonitorImpl<S>, payment_hash: Option<PaymentHash>) -> Self {
1840+
pub(crate) fn from_impl<'a, L: Deref, S: EcdsaChannelSigner>(
1841+
logger: &'a L, monitor_impl: &ChannelMonitorImpl<S>, payment_hash: Option<PaymentHash>,
1842+
) -> WithContext<'a, L>
1843+
where
1844+
L::Target: Logger,
1845+
{
18621846
let peer_id = Some(monitor_impl.counterparty_node_id);
18631847
let channel_id = Some(monitor_impl.channel_id());
1864-
WithChannelMonitor {
1865-
logger, peer_id, channel_id, payment_hash,
1866-
}
1848+
WithContext::from(logger, peer_id, channel_id, payment_hash)
18671849
}
18681850
}
18691851

@@ -3829,7 +3811,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38293811
fn provide_payment_preimage<B: Deref, F: Deref, L: Deref>(
38303812
&mut self, payment_hash: &PaymentHash, payment_preimage: &PaymentPreimage,
38313813
payment_info: &Option<PaymentClaimDetails>, broadcaster: &B,
3832-
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithChannelMonitor<L>)
3814+
fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithContext<L>)
38333815
where B::Target: BroadcasterInterface,
38343816
F::Target: FeeEstimator,
38353817
L::Target: Logger,
@@ -4006,7 +3988,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40063988
///
40073989
/// [`ChannelMonitor::broadcast_latest_holder_commitment_txn`]: crate::chain::channelmonitor::ChannelMonitor::broadcast_latest_holder_commitment_txn
40083990
pub(crate) fn queue_latest_holder_commitment_txn_for_broadcast<B: Deref, F: Deref, L: Deref>(
4009-
&mut self, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithChannelMonitor<L>,
3991+
&mut self, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithContext<L>,
40103992
require_funding_seen: bool,
40113993
)
40123994
where
@@ -4034,8 +4016,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40344016
}
40354017

40364018
fn renegotiated_funding<L: Deref>(
4037-
&mut self, logger: &WithChannelMonitor<L>,
4038-
channel_parameters: &ChannelTransactionParameters,
4019+
&mut self, logger: &WithContext<L>, channel_parameters: &ChannelTransactionParameters,
40394020
alternative_holder_commitment_tx: &HolderCommitmentTransaction,
40404021
alternative_counterparty_commitment_tx: &CommitmentTransaction,
40414022
) -> Result<(), ()>
@@ -4210,7 +4191,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42104191

42114192
#[rustfmt::skip]
42124193
fn update_monitor<B: Deref, F: Deref, L: Deref>(
4213-
&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: &F, logger: &WithChannelMonitor<L>
4194+
&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: &F, logger: &WithContext<L>
42144195
) -> Result<(), ()>
42154196
where B::Target: BroadcasterInterface,
42164197
F::Target: FeeEstimator,
@@ -5254,7 +5235,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
52545235
/// Note that this includes possibly-locktimed-in-the-future transactions!
52555236
#[rustfmt::skip]
52565237
fn unsafe_get_latest_holder_commitment_txn<L: Deref>(
5257-
&mut self, logger: &WithChannelMonitor<L>
5238+
&mut self, logger: &WithContext<L>
52585239
) -> Vec<Transaction> where L::Target: Logger {
52595240
log_debug!(logger, "Getting signed copy of latest holder commitment transaction!");
52605241
let commitment_tx = {
@@ -5307,7 +5288,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
53075288
#[rustfmt::skip]
53085289
fn block_connected<B: Deref, F: Deref, L: Deref>(
53095290
&mut self, header: &Header, txdata: &TransactionData, height: u32, broadcaster: B,
5310-
fee_estimator: F, logger: &WithChannelMonitor<L>,
5291+
fee_estimator: F, logger: &WithContext<L>,
53115292
) -> Vec<TransactionOutputs>
53125293
where B::Target: BroadcasterInterface,
53135294
F::Target: FeeEstimator,
@@ -5327,7 +5308,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
53275308
height: u32,
53285309
broadcaster: B,
53295310
fee_estimator: &LowerBoundedFeeEstimator<F>,
5330-
logger: &WithChannelMonitor<L>,
5311+
logger: &WithContext<L>,
53315312
) -> Vec<TransactionOutputs>
53325313
where
53335314
B::Target: BroadcasterInterface,
@@ -5360,7 +5341,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
53605341
height: u32,
53615342
broadcaster: B,
53625343
fee_estimator: &LowerBoundedFeeEstimator<F>,
5363-
logger: &WithChannelMonitor<L>,
5344+
logger: &WithContext<L>,
53645345
) -> Vec<TransactionOutputs>
53655346
where
53665347
B::Target: BroadcasterInterface,
@@ -5647,7 +5628,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
56475628
mut claimable_outpoints: Vec<PackageTemplate>,
56485629
broadcaster: &B,
56495630
fee_estimator: &LowerBoundedFeeEstimator<F>,
5650-
logger: &WithChannelMonitor<L>,
5631+
logger: &WithContext<L>,
56515632
) -> Vec<TransactionOutputs>
56525633
where
56535634
B::Target: BroadcasterInterface,
@@ -5867,7 +5848,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
58675848

58685849
#[rustfmt::skip]
58695850
fn blocks_disconnected<B: Deref, F: Deref, L: Deref>(
5870-
&mut self, fork_point: BestBlock, broadcaster: B, fee_estimator: F, logger: &WithChannelMonitor<L>
5851+
&mut self, fork_point: BestBlock, broadcaster: B, fee_estimator: F, logger: &WithContext<L>
58715852
) where B::Target: BroadcasterInterface,
58725853
F::Target: FeeEstimator,
58735854
L::Target: Logger,
@@ -5920,7 +5901,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
59205901
txid: &Txid,
59215902
broadcaster: B,
59225903
fee_estimator: &LowerBoundedFeeEstimator<F>,
5923-
logger: &WithChannelMonitor<L>,
5904+
logger: &WithContext<L>,
59245905
) where
59255906
B::Target: BroadcasterInterface,
59265907
F::Target: FeeEstimator,
@@ -6031,7 +6012,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
60316012

60326013
#[rustfmt::skip]
60336014
fn should_broadcast_holder_commitment_txn<L: Deref>(
6034-
&self, logger: &WithChannelMonitor<L>
6015+
&self, logger: &WithContext<L>
60356016
) -> Option<PaymentHash> where L::Target: Logger {
60366017
// There's no need to broadcast our commitment transaction if we've seen one confirmed (even
60376018
// with 1 confirmation) as it'll be rejected as duplicate/conflicting.
@@ -6098,7 +6079,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
60986079
/// or counterparty commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
60996080
#[rustfmt::skip]
61006081
fn is_resolving_htlc_output<L: Deref>(
6101-
&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &WithChannelMonitor<L>,
6082+
&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &WithContext<L>,
61026083
) where L::Target: Logger {
61036084
let funding_spent = get_confirmed_funding_scope!(self);
61046085

@@ -6355,7 +6336,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
63556336
/// own.
63566337
#[rustfmt::skip]
63576338
fn check_tx_and_push_spendable_outputs<L: Deref>(
6358-
&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &WithChannelMonitor<L>,
6339+
&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &WithContext<L>,
63596340
) where L::Target: Logger {
63606341
let funding_spent = get_confirmed_funding_scope!(self);
63616342
for spendable_output in self.get_spendable_outputs(funding_spent, tx) {

0 commit comments

Comments
 (0)