Skip to content

Commit a224937

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

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
@@ -297,6 +297,7 @@ where
297297
// channel.
298298
let confirmation_target = ConfirmationTarget::Normal;
299299

300+
// We set nLockTime to the current height to discourage fee sniping.
300301
let cur_height = self.channel_manager.current_best_block().height();
301302
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
302303

@@ -574,6 +575,7 @@ where
574575
Some(locktime),
575576
&Secp256k1::new(),
576577
);
578+
577579
match res {
578580
Ok(spending_tx) => self.wallet.broadcast_transactions(&[&spending_tx]),
579581
Err(err) => {

src/wallet.rs

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

2929
use std::collections::HashMap;
3030
use std::sync::{Arc, Condvar, Mutex, RwLock};
@@ -158,14 +158,19 @@ where
158158
}
159159

160160
pub(crate) fn create_funding_transaction(
161-
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget, locktime: LockTime,
161+
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget,
162+
locktime: LockTime,
162163
) -> Result<Transaction, Error> {
163164
let fee_rate = self.estimate_fee_rate(confirmation_target);
164165

165166
let locked_wallet = self.inner.lock().unwrap();
166167
let mut tx_builder = locked_wallet.build_tx();
167168

168-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
169+
tx_builder
170+
.add_recipient(output_script, value_sats)
171+
.fee_rate(fee_rate)
172+
.nlocktime(locktime)
173+
.enable_rbf();
169174

170175
let mut psbt = match tx_builder.finish() {
171176
Ok((psbt, _)) => {

0 commit comments

Comments
 (0)