@@ -34,7 +34,7 @@ use bitcoin::secp256k1;
34
34
use bitcoin:: sighash:: EcdsaSighashType ;
35
35
36
36
use crate :: ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
37
- use crate :: ln:: { PaymentHash , PaymentPreimage } ;
37
+ use crate :: ln:: { PaymentHash , PaymentPreimage , ChannelId } ;
38
38
use crate :: ln:: msgs:: DecodeError ;
39
39
use crate :: ln:: channel_keys:: { DelayedPaymentKey , DelayedPaymentBasepoint , HtlcBasepoint , HtlcKey , RevocationKey , RevocationBasepoint } ;
40
40
use crate :: ln:: chan_utils:: { self , CommitmentTransaction , CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCClaim , ChannelTransactionParameters , HolderCommitmentTransaction , TxCreationKeys } ;
@@ -47,7 +47,7 @@ use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, SpendableOutputDe
47
47
use crate :: chain:: onchaintx:: { ClaimEvent , OnchainTxHandler } ;
48
48
use crate :: chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
49
49
use crate :: chain:: Filter ;
50
- use crate :: util:: logger:: Logger ;
50
+ use crate :: util:: logger:: { Logger , Record } ;
51
51
use crate :: util:: ser:: { Readable , ReadableArgs , RequiredWrapper , MaybeReadable , UpgradableRequired , Writer , Writeable , U48 } ;
52
52
use crate :: util:: byte_utils;
53
53
use crate :: events:: { Event , EventHandler } ;
@@ -1125,6 +1125,30 @@ macro_rules! _process_events_body {
1125
1125
}
1126
1126
pub ( super ) use _process_events_body as process_events_body;
1127
1127
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
+
1128
1152
impl < Signer : WriteableEcdsaChannelSigner > ChannelMonitor < Signer > {
1129
1153
/// For lockorder enforcement purposes, we need to have a single site which constructs the
1130
1154
/// `inner` mutex, otherwise cases where we lock two monitors at the same time (eg in our
@@ -4501,7 +4525,7 @@ mod tests {
4501
4525
use super :: ChannelMonitorUpdateStep ;
4502
4526
use crate :: { check_added_monitors, check_spends, get_local_commitment_txn, get_monitor, get_route_and_payment_hash, unwrap_send_err} ;
4503
4527
use crate :: chain:: { BestBlock , Confirm } ;
4504
- use crate :: chain:: channelmonitor:: ChannelMonitor ;
4528
+ use crate :: chain:: channelmonitor:: { ChannelMonitor , WithChannelMonitor } ;
4505
4529
use crate :: chain:: package:: { weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT } ;
4506
4530
use crate :: chain:: transaction:: OutPoint ;
4507
4531
use crate :: sign:: InMemorySigner ;
@@ -4514,6 +4538,7 @@ mod tests {
4514
4538
use crate :: util:: errors:: APIError ;
4515
4539
use crate :: util:: test_utils:: { TestLogger , TestBroadcaster , TestFeeEstimator } ;
4516
4540
use crate :: util:: ser:: { ReadableArgs , Writeable } ;
4541
+ use crate :: util:: logger:: Logger ;
4517
4542
use crate :: sync:: { Arc , Mutex } ;
4518
4543
use crate :: io;
4519
4544
use crate :: ln:: features:: ChannelTypeFeatures ;
@@ -4703,6 +4728,7 @@ mod tests {
4703
4728
4704
4729
let mut htlcs = preimages_slice_to_htlcs ! ( preimages[ 0 ..10 ] ) ;
4705
4730
let dummy_commitment_tx = HolderCommitmentTransaction :: dummy ( & mut htlcs) ;
4731
+
4706
4732
monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
4707
4733
htlcs. into_iter ( ) . map ( |( htlc, _) | ( htlc, Some ( dummy_sig) , None ) ) . collect ( ) ) . unwrap ( ) ;
4708
4734
monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
@@ -4900,5 +4926,62 @@ mod tests {
4900
4926
}
4901
4927
}
4902
4928
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 : 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 : PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 47 ; 32 ] ) . unwrap ( ) ) ,
4954
+ htlc_basepoint : 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
+ }
4903
4986
// Further testing is done in the ChannelManager integration tests.
4904
4987
}
0 commit comments