Skip to content

Commit

Permalink
Implement anti-fee sniping on funding tx
Browse files Browse the repository at this point in the history
I.e., set `nLockTime` to current block height
  • Loading branch information
tnull committed May 22, 2023
1 parent 5df65d2 commit b37a146
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ where
// channel.
let confirmation_target = ConfirmationTarget::Normal;

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

Expand Down Expand Up @@ -569,6 +570,7 @@ where
Some(locktime),
&Secp256k1::new(),
);

match res {
Ok(spending_tx) => self.wallet.broadcast_transactions(&[&spending_tx]),
Err(err) => {
Expand Down
11 changes: 8 additions & 3 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bitcoin::bech32::u5;
use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, Signing};
use bitcoin::{PackedLockTime, LockTime, Script, Transaction, TxOut, Txid};
use bitcoin::{LockTime, PackedLockTime, Script, Transaction, TxOut, Txid};

use std::collections::HashMap;
use std::sync::{Arc, Condvar, Mutex, RwLock};
Expand Down Expand Up @@ -156,14 +156,19 @@ where
}

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

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

tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
tx_builder
.add_recipient(output_script, value_sats)
.fee_rate(fee_rate)
.nlocktime(locktime)
.enable_rbf();

let mut psbt = match tx_builder.finish() {
Ok((psbt, _)) => {
Expand Down

0 comments on commit b37a146

Please sign in to comment.