@@ -33,7 +33,7 @@ use bitcoin::secp256k1::{SecretKey, PublicKey};
33
33
use bitcoin:: { secp256k1, EcdsaSighashType } ;
34
34
35
35
use crate :: ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
36
- use crate :: ln:: { PaymentHash , PaymentPreimage } ;
36
+ use crate :: ln:: { PaymentHash , PaymentPreimage , ChannelId } ;
37
37
use crate :: ln:: msgs:: DecodeError ;
38
38
use crate :: ln:: chan_utils;
39
39
use crate :: ln:: chan_utils:: { CommitmentTransaction , CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCClaim , ChannelTransactionParameters , HolderCommitmentTransaction , TxCreationKeys } ;
@@ -46,7 +46,7 @@ use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, SpendableOutputDe
46
46
use crate :: chain:: onchaintx:: { ClaimEvent , OnchainTxHandler } ;
47
47
use crate :: chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
48
48
use crate :: chain:: Filter ;
49
- use crate :: util:: logger:: Logger ;
49
+ use crate :: util:: logger:: { Logger , Record } ;
50
50
use crate :: util:: ser:: { Readable , ReadableArgs , RequiredWrapper , MaybeReadable , UpgradableRequired , Writer , Writeable , U48 } ;
51
51
use crate :: util:: byte_utils;
52
52
use crate :: events:: { Event , EventHandler } ;
@@ -1124,6 +1124,30 @@ macro_rules! _process_events_body {
1124
1124
}
1125
1125
pub ( super ) use _process_events_body as process_events_body;
1126
1126
1127
+ pub ( crate ) struct WithChannelMonitor < ' a , L : Deref > where L :: Target : Logger {
1128
+ logger : & ' a L ,
1129
+ peer_id : Option < PublicKey > ,
1130
+ channel_id : Option < ChannelId > ,
1131
+ }
1132
+
1133
+ impl < ' a , L : Deref > Logger for WithChannelMonitor < ' a , L > where L :: Target : Logger {
1134
+ fn log ( & self , mut record : Record ) {
1135
+ record. peer_id = self . peer_id ;
1136
+ record. channel_id = self . channel_id ;
1137
+ self . logger . log ( record)
1138
+ }
1139
+ }
1140
+
1141
+ impl < ' a , ' b , L : Deref > WithChannelMonitor < ' a , L > where L :: Target : Logger {
1142
+ pub ( crate ) fn from < S : WriteableEcdsaChannelSigner > ( logger : & ' a L , monitor : & ' b ChannelMonitor < S > ) -> Self {
1143
+ WithChannelMonitor {
1144
+ logger,
1145
+ peer_id : monitor. get_counterparty_node_id ( ) ,
1146
+ channel_id : Some ( monitor. get_funding_txo ( ) . 0 . to_channel_id ( ) ) ,
1147
+ }
1148
+ }
1149
+ }
1150
+
1127
1151
impl < Signer : WriteableEcdsaChannelSigner > ChannelMonitor < Signer > {
1128
1152
/// For lockorder enforcement purposes, we need to have a single site which constructs the
1129
1153
/// `inner` mutex, otherwise cases where we lock two monitors at the same time (eg in our
@@ -4499,7 +4523,7 @@ mod tests {
4499
4523
use super :: ChannelMonitorUpdateStep ;
4500
4524
use crate :: { check_added_monitors, check_spends, get_local_commitment_txn, get_monitor, get_route_and_payment_hash, unwrap_send_err} ;
4501
4525
use crate :: chain:: { BestBlock , Confirm } ;
4502
- use crate :: chain:: channelmonitor:: ChannelMonitor ;
4526
+ use crate :: chain:: channelmonitor:: { ChannelMonitor , WithChannelMonitor } ;
4503
4527
use crate :: chain:: package:: { weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT } ;
4504
4528
use crate :: chain:: transaction:: OutPoint ;
4505
4529
use crate :: sign:: InMemorySigner ;
@@ -4512,6 +4536,7 @@ mod tests {
4512
4536
use crate :: util:: errors:: APIError ;
4513
4537
use crate :: util:: test_utils:: { TestLogger , TestBroadcaster , TestFeeEstimator } ;
4514
4538
use crate :: util:: ser:: { ReadableArgs , Writeable } ;
4539
+ use crate :: util:: logger:: Logger ;
4515
4540
use crate :: sync:: { Arc , Mutex } ;
4516
4541
use crate :: io;
4517
4542
use bitcoin:: { PackedLockTime , Sequence , Witness } ;
@@ -4700,6 +4725,7 @@ mod tests {
4700
4725
4701
4726
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..10 ] ) ;
4702
4727
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
4728
+
4703
4729
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
4704
4730
htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
4705
4731
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_inner ( Sha256 :: hash ( b"1" ) . into_inner ( ) ) ,
@@ -4896,5 +4922,62 @@ mod tests {
4896
4922
}
4897
4923
}
4898
4924
4925
+ #[ test]
4926
+ fn test_with_channel_monitor_impl_logger ( ) {
4927
+ let secp_ctx = Secp256k1 :: new ( ) ;
4928
+ let logger = Arc :: new ( TestLogger :: new ( ) ) ;
4929
+
4930
+ let dummy_key = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4931
+
4932
+ let keys = InMemorySigner :: new (
4933
+ & secp_ctx,
4934
+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4935
+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4936
+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4937
+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4938
+ SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ,
4939
+ [ 41 ; 32 ] ,
4940
+ 0 ,
4941
+ [ 0 ; 32 ] ,
4942
+ [ 0 ; 32 ] ,
4943
+ ) ;
4944
+
4945
+ let counterparty_pubkeys = ChannelPublicKeys {
4946
+ funding_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 44 ; 32 ] ) . unwrap ( ) ) ,
4947
+ revocation_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( ) ) ,
4948
+ payment_point : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 46 ; 32 ] ) . unwrap ( ) ) ,
4949
+ delayed_payment_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 47 ; 32 ] ) . unwrap ( ) ) ,
4950
+ htlc_basepoint : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 48 ; 32 ] ) . unwrap ( ) )
4951
+ } ;
4952
+ let funding_outpoint = OutPoint { txid : Txid :: all_zeros ( ) , index : u16:: max_value ( ) } ;
4953
+ let channel_parameters = ChannelTransactionParameters {
4954
+ holder_pubkeys : keys. holder_channel_pubkeys . clone ( ) ,
4955
+ holder_selected_contest_delay : 66 ,
4956
+ is_outbound_from_holder : true ,
4957
+ counterparty_parameters : Some ( CounterpartyChannelTransactionParameters {
4958
+ pubkeys : counterparty_pubkeys,
4959
+ selected_contest_delay : 67 ,
4960
+ } ) ,
4961
+ funding_outpoint : Some ( funding_outpoint) ,
4962
+ channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( )
4963
+ } ;
4964
+ let shutdown_pubkey = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4965
+ let best_block = BestBlock :: from_network ( Network :: Testnet ) ;
4966
+ let monitor = ChannelMonitor :: new ( Secp256k1 :: new ( ) , keys,
4967
+ Some ( ShutdownScript :: new_p2wpkh_from_pubkey ( shutdown_pubkey) . into_inner ( ) ) , 0 , & Script :: new ( ) ,
4968
+ ( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
4969
+ & channel_parameters, Script :: new ( ) , 46 , 0 , HolderCommitmentTransaction :: dummy ( & mut Vec :: new ( ) ) ,
4970
+ best_block, dummy_key) ;
4971
+
4972
+ let chan_id = monitor. inner . lock ( ) . unwrap ( ) . funding_info . 0 . to_channel_id ( ) . clone ( ) ;
4973
+ let context_logger = WithChannelMonitor :: from ( & logger, & monitor) ;
4974
+ log_error ! ( context_logger, "This is an error" ) ;
4975
+ log_warn ! ( context_logger, "This is an error" ) ;
4976
+ log_debug ! ( context_logger, "This is an error" ) ;
4977
+ log_trace ! ( context_logger, "This is an error" ) ;
4978
+ log_gossip ! ( context_logger, "This is an error" ) ;
4979
+ log_info ! ( context_logger, "This is an error" ) ;
4980
+ logger. assert_log_context_contains ( "lightning::chain::channelmonitor::tests" , Some ( dummy_key) , Some ( chan_id) , 6 ) ;
4981
+ }
4899
4982
// Further testing is done in the ChannelManager integration tests.
4900
4983
}
0 commit comments