@@ -34,7 +34,7 @@ use bitcoin::secp256k1;
3434use bitcoin:: sighash:: EcdsaSighashType ;
3535
3636use crate :: ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
37- use crate :: ln:: { PaymentHash , PaymentPreimage } ;
37+ use crate :: ln:: { PaymentHash , PaymentPreimage , ChannelId } ;
3838use crate :: ln:: msgs:: DecodeError ;
3939use crate :: ln:: channel_keys:: { DelayedPaymentKey , DelayedPaymentBasepoint , HtlcBasepoint , HtlcKey , RevocationKey , RevocationBasepoint } ;
4040use crate :: ln:: chan_utils:: { self , CommitmentTransaction , CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCClaim , ChannelTransactionParameters , HolderCommitmentTransaction , TxCreationKeys } ;
@@ -47,7 +47,7 @@ use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, SpendableOutputDe
4747use crate :: chain:: onchaintx:: { ClaimEvent , OnchainTxHandler } ;
4848use crate :: chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
4949use crate :: chain:: Filter ;
50- use crate :: util:: logger:: Logger ;
50+ use crate :: util:: logger:: { Logger , Record } ;
5151use crate :: util:: ser:: { Readable , ReadableArgs , RequiredWrapper , MaybeReadable , UpgradableRequired , Writer , Writeable , U48 } ;
5252use crate :: util:: byte_utils;
5353use crate :: events:: { Event , EventHandler } ;
@@ -1125,6 +1125,30 @@ macro_rules! _process_events_body {
11251125}
11261126pub ( super ) use _process_events_body as process_events_body;
11271127
1128+ pub ( crate ) struct WithChannelMonitor < ' a , L : Deref > where L :: Target : Logger {
1129+ logger : & ' a L ,
1130+ peer_id : Option < PublicKey > ,
1131+ channel_id : Option < ChannelId > ,
1132+ }
1133+
1134+ impl < ' a , L : Deref > Logger for WithChannelMonitor < ' a , L > where L :: Target : Logger {
1135+ fn log ( & self , mut record : Record ) {
1136+ record. peer_id = self . peer_id ;
1137+ record. channel_id = self . channel_id ;
1138+ self . logger . log ( record)
1139+ }
1140+ }
1141+
1142+ impl < ' a , ' b , L : Deref > WithChannelMonitor < ' a , L > where L :: Target : Logger {
1143+ pub ( crate ) fn from < S : WriteableEcdsaChannelSigner > ( logger : & ' a L , monitor : & ' b ChannelMonitor < S > ) -> Self {
1144+ WithChannelMonitor {
1145+ logger,
1146+ peer_id : monitor. get_counterparty_node_id ( ) ,
1147+ channel_id : Some ( monitor. get_funding_txo ( ) . 0 . to_channel_id ( ) ) ,
1148+ }
1149+ }
1150+ }
1151+
11281152impl < Signer : WriteableEcdsaChannelSigner > ChannelMonitor < Signer > {
11291153 /// For lockorder enforcement purposes, we need to have a single site which constructs the
11301154 /// `inner` mutex, otherwise cases where we lock two monitors at the same time (eg in our
@@ -4501,7 +4525,7 @@ mod tests {
45014525 use super :: ChannelMonitorUpdateStep ;
45024526 use crate :: { check_added_monitors, check_spends, get_local_commitment_txn, get_monitor, get_route_and_payment_hash, unwrap_send_err} ;
45034527 use crate :: chain:: { BestBlock , Confirm } ;
4504- use crate :: chain:: channelmonitor:: ChannelMonitor ;
4528+ use crate :: chain:: channelmonitor:: { ChannelMonitor , WithChannelMonitor } ;
45054529 use crate :: chain:: package:: { weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT } ;
45064530 use crate :: chain:: transaction:: OutPoint ;
45074531 use crate :: sign:: InMemorySigner ;
@@ -4514,6 +4538,7 @@ mod tests {
45144538 use crate :: util:: errors:: APIError ;
45154539 use crate :: util:: test_utils:: { TestLogger , TestBroadcaster , TestFeeEstimator } ;
45164540 use crate :: util:: ser:: { ReadableArgs , Writeable } ;
4541+ use crate :: util:: logger:: Logger ;
45174542 use crate :: sync:: { Arc , Mutex } ;
45184543 use crate :: io;
45194544 use crate :: ln:: features:: ChannelTypeFeatures ;
@@ -4703,6 +4728,7 @@ mod tests {
47034728
47044729 let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..10 ] ) ;
47054730 let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
4731+
47064732 monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
47074733 htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
47084734 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
@@ -4900,5 +4926,62 @@ mod tests {
49004926 }
49014927 }
49024928
4929+ #[ test]
4930+ fn test_with_channel_monitor_impl_logger ( ) {
4931+ let secp_ctx = Secp256k1 :: new ( ) ;
4932+ let logger = Arc :: new ( TestLogger :: new ( ) ) ;
4933+
4934+ let dummy_key = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4935+
4936+ let keys = InMemorySigner :: new (
4937+ & secp_ctx,
4938+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4939+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4940+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4941+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4942+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4943+ [ 41 ; 32 ] ,
4944+ 0 ,
4945+ [ 0 ; 32 ] ,
4946+ [ 0 ; 32 ] ,
4947+ ) ;
4948+
4949+ let counterparty_pubkeys = ChannelPublicKeys {
4950+ funding_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) ) ,
4951+ revocation_basepoint : RevocationBasepoint :: from ( PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) ) ,
4952+ payment_point : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 46 ; 32 ] ) . unwrap ( ) ) ,
4953+ delayed_payment_basepoint : DelayedPaymentBasepoint :: from ( PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 47 ; 32 ] ) . unwrap ( ) ) ) ,
4954+ htlc_basepoint : HtlcBasepoint :: from ( PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 48 ; 32 ] ) . unwrap ( ) ) ) ,
4955+ } ;
4956+ let funding_outpoint = OutPoint { txid : Txid :: all_zeros ( ) , index : u16:: max_value ( ) } ;
4957+ let channel_parameters = ChannelTransactionParameters {
4958+ holder_pubkeys : keys. holder_channel_pubkeys . clone ( ) ,
4959+ holder_selected_contest_delay : 66 ,
4960+ is_outbound_from_holder : true ,
4961+ counterparty_parameters : Some ( CounterpartyChannelTransactionParameters {
4962+ pubkeys : counterparty_pubkeys,
4963+ selected_contest_delay : 67 ,
4964+ } ) ,
4965+ funding_outpoint : Some ( funding_outpoint) ,
4966+ channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( )
4967+ } ;
4968+ let shutdown_pubkey = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4969+ let best_block = BestBlock :: from_network ( Network :: Testnet ) ;
4970+ let monitor = ChannelMonitor :: new ( Secp256k1 :: new ( ) , keys,
4971+ Some ( ShutdownScript :: new_p2wpkh_from_pubkey ( shutdown_pubkey) . into_inner ( ) ) , 0 , & ScriptBuf :: new ( ) ,
4972+ ( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , ScriptBuf :: new ( ) ) ,
4973+ & channel_parameters, ScriptBuf :: new ( ) , 46 , 0 , HolderCommitmentTransaction :: dummy ( & mut Vec :: new ( ) ) ,
4974+ best_block, dummy_key) ;
4975+
4976+ let chan_id = monitor. inner . lock ( ) . unwrap ( ) . funding_info . 0 . to_channel_id ( ) . clone ( ) ;
4977+ let context_logger = WithChannelMonitor :: from ( & logger, & monitor) ;
4978+ log_error ! ( context_logger, "This is an error" ) ;
4979+ log_warn ! ( context_logger, "This is an error" ) ;
4980+ log_debug ! ( context_logger, "This is an error" ) ;
4981+ log_trace ! ( context_logger, "This is an error" ) ;
4982+ log_gossip ! ( context_logger, "This is an error" ) ;
4983+ log_info ! ( context_logger, "This is an error" ) ;
4984+ logger. assert_log_context_contains ( "lightning::chain::channelmonitor::tests" , Some ( dummy_key) , Some ( chan_id) , 6 ) ;
4985+ }
49034986 // Further testing is done in the ChannelManager integration tests.
49044987}
0 commit comments