Skip to content

Commit d94eca4

Browse files
committed
f Account for nLockTime given in spend_spendable_outputs
While we're here we also set the locktime for the funding transaction
1 parent d9a9e18 commit d94eca4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/event.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use lightning::util::errors::APIError;
2222
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2323

2424
use bitcoin::secp256k1::{PublicKey, Secp256k1};
25-
use bitcoin::OutPoint;
25+
use bitcoin::{LockTime, OutPoint, PackedLockTime};
2626
use rand::{thread_rng, Rng};
2727
use std::collections::VecDeque;
2828
use std::ops::Deref;
@@ -292,11 +292,16 @@ where
292292
// channel.
293293
let confirmation_target = ConfirmationTarget::Normal;
294294

295+
// We set nLockTime to the current height to discourage fee sniping.
296+
let cur_height = self.channel_manager.current_best_block().height();
297+
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
298+
295299
// Sign the final funding transaction and broadcast it.
296300
match self.wallet.create_funding_transaction(
297301
output_script,
298302
channel_value_satoshis,
299303
confirmation_target,
304+
locktime,
300305
) {
301306
Ok(final_tx) => {
302307
// Give the funding transaction back to LDK for opening the channel.
@@ -552,13 +557,20 @@ where
552557
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
553558
let tx_feerate =
554559
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
560+
561+
// We set nLockTime to the current height to discourage fee sniping.
562+
let cur_height = self.channel_manager.current_best_block().height();
563+
let locktime: PackedLockTime =
564+
LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
555565
let res = self.keys_manager.spend_spendable_outputs(
556566
output_descriptors,
557567
Vec::new(),
558568
destination_address.script_pubkey(),
559569
tx_feerate,
570+
Some(locktime),
560571
&Secp256k1::new(),
561572
);
573+
562574
match res {
563575
Ok(spending_tx) => self.wallet.broadcast_transactions(&[&spending_tx]),
564576
Err(err) => {

src/wallet.rs

Lines changed: 9 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::{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};
@@ -157,13 +157,18 @@ where
157157

158158
pub(crate) fn create_funding_transaction(
159159
&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).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, _)) => {
@@ -363,7 +368,7 @@ where
363368
pub fn spend_spendable_outputs<C: Signing>(
364369
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
365370
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
366-
secp_ctx: &Secp256k1<C>,
371+
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
367372
) -> Result<Transaction, ()> {
368373
let only_non_static = &descriptors
369374
.iter()
@@ -375,6 +380,7 @@ where
375380
outputs,
376381
change_destination_script,
377382
feerate_sat_per_1000_weight,
383+
locktime,
378384
secp_ctx,
379385
)
380386
}

0 commit comments

Comments
 (0)