@@ -12,6 +12,7 @@ use lightning::util::ser::{Readable, WithoutLength};
12
12
13
13
use bitcoin:: secp256k1:: Secp256k1 ;
14
14
use bitcoin:: { LockTime , PackedLockTime } ;
15
+ use rand:: { thread_rng, Rng } ;
15
16
16
17
use crate :: hex_utils;
17
18
use crate :: BitcoindClient ;
@@ -111,7 +112,19 @@ pub(crate) async fn periodic_sweep(
111
112
bitcoind_client. get_est_sat_per_1000_weight ( ConfirmationTarget :: Background ) ;
112
113
113
114
// We set nLockTime to the current height to discourage fee sniping.
114
- let cur_height = channel_manager. current_best_block ( ) . height ( ) ;
115
+ // Occasionally randomly pick a nLockTime even further back, so
116
+ // that transactions that are delayed after signing for whatever reason,
117
+ // e.g. high-latency mix networks and some CoinJoin implementations, have
118
+ // better privacy.
119
+ // Logic copied from core: https://github.com/bitcoin/bitcoin/blob/1d4846a8443be901b8a5deb0e357481af22838d0/src/wallet/spend.cpp#L936
120
+ let mut cur_height = channel_manager. current_best_block ( ) . height ( ) ;
121
+
122
+ // 10% of the time
123
+ if thread_rng ( ) . gen_range ( 0 , 10 ) == 0 {
124
+ // subtract random number between 0 and 100
125
+ cur_height = cur_height. saturating_sub ( thread_rng ( ) . gen_range ( 0 , 100 ) ) ;
126
+ }
127
+
115
128
let locktime: PackedLockTime =
116
129
LockTime :: from_height ( cur_height) . map_or ( PackedLockTime :: ZERO , |l| l. into ( ) ) ;
117
130
0 commit comments