Skip to content

Commit 2c125b7

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

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
@@ -270,6 +270,7 @@ where
270270
// channel.
271271
let confirmation_target = ConfirmationTarget::Normal;
272272

273+
// We set nLockTime to the current height to discourage fee sniping.
273274
let cur_height = self.channel_manager.current_best_block().height();
274275
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
275276

@@ -573,6 +574,7 @@ where
573574
Some(locktime),
574575
&Secp256k1::new(),
575576
);
577+
576578
match res {
577579
Ok(spending_tx) => self.wallet.broadcast_transactions(&[&spending_tx]),
578580
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::ops::Deref;
@@ -153,14 +153,19 @@ where
153153
}
154154

155155
pub(crate) fn create_funding_transaction(
156-
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget, locktime: LockTime,
156+
&self, output_script: Script, value_sats: u64, confirmation_target: ConfirmationTarget,
157+
locktime: LockTime,
157158
) -> Result<Transaction, Error> {
158159
let fee_rate = self.estimate_fee_rate(confirmation_target);
159160

160161
let locked_wallet = self.inner.lock().unwrap();
161162
let mut tx_builder = locked_wallet.build_tx();
162163

163-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
164+
tx_builder
165+
.add_recipient(output_script, value_sats)
166+
.fee_rate(fee_rate)
167+
.nlocktime(locktime)
168+
.enable_rbf();
164169

165170
let mut psbt = match tx_builder.finish() {
166171
Ok((psbt, _)) => {

0 commit comments

Comments
 (0)