Skip to content

Commit b37a146

Browse files
committed
Implement anti-fee sniping on funding tx
I.e., set `nLockTime` to current block height
1 parent 5df65d2 commit b37a146

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/event.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ where
292292
// channel.
293293
let confirmation_target = ConfirmationTarget::Normal;
294294

295+
// We set nLockTime to the current height to discourage fee sniping.
295296
let cur_height = self.channel_manager.current_best_block().height();
296297
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
297298

@@ -569,6 +570,7 @@ where
569570
Some(locktime),
570571
&Secp256k1::new(),
571572
);
573+
572574
match res {
573575
Ok(spending_tx) => self.wallet.broadcast_transactions(&[&spending_tx]),
574576
Err(err) => {

src/wallet.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::bech32::u5;
2222
use bitcoin::secp256k1::ecdh::SharedSecret;
2323
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
2424
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, Signing};
25-
use bitcoin::{PackedLockTime, LockTime, Script, Transaction, TxOut, Txid};
25+
use bitcoin::{LockTime, PackedLockTime, Script, Transaction, TxOut, Txid};
2626

2727
use std::collections::HashMap;
2828
use std::sync::{Arc, Condvar, Mutex, RwLock};
@@ -156,14 +156,19 @@ where
156156
}
157157

158158
pub(crate) fn create_funding_transaction(
159-
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget, locktime: LockTime,
159+
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget,
160+
locktime: LockTime,
160161
) -> Result<Transaction, Error> {
161162
let fee_rate = self.estimate_fee_rate(confirmation_target);
162163

163164
let locked_wallet = self.inner.lock().unwrap();
164165
let mut tx_builder = locked_wallet.build_tx();
165166

166-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
167+
tx_builder
168+
.add_recipient(output_script, value_sats)
169+
.fee_rate(fee_rate)
170+
.nlocktime(locktime)
171+
.enable_rbf();
167172

168173
let mut psbt = match tx_builder.finish() {
169174
Ok((psbt, _)) => {

0 commit comments

Comments
 (0)