Skip to content

Commit 2773537

Browse files
committed
f Account for nLockTime given in spend_spendable_outputs
1 parent 2b7ecf9 commit 2773537

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/event.rs

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

300+
let cur_height = self.channel_manager.current_best_block().height();
301+
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
302+
300303
// Sign the final funding transaction and broadcast it.
301304
match self.wallet.create_funding_transaction(
302305
output_script,
303306
channel_value_satoshis,
304307
confirmation_target,
308+
locktime,
305309
) {
306310
Ok(final_tx) => {
307311
// Give the funding transaction back to LDK for opening the channel.
@@ -557,11 +561,17 @@ where
557561
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
558562
let tx_feerate =
559563
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
564+
565+
// We set nLockTime to the current height to discourage fee sniping.
566+
let cur_height = self.channel_manager.current_best_block().height();
567+
let locktime: PackedLockTime =
568+
LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
560569
let res = self.keys_manager.spend_spendable_outputs(
561570
output_descriptors,
562571
Vec::new(),
563572
destination_address.script_pubkey(),
564573
tx_feerate,
574+
Some(locktime),
565575
&Secp256k1::new(),
566576
);
567577
match res {

src/wallet.rs

Lines changed: 5 additions & 4 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::{Script, Transaction, TxOut, Txid};
27+
use bitcoin::{PackedLockTime, LockTime, Script, Transaction, TxOut, Txid};
2828

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

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

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

168-
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).enable_rbf();
168+
tx_builder.add_recipient(output_script, value_sats).fee_rate(fee_rate).nlocktime(locktime).enable_rbf();
169169

170170
let mut psbt = match tx_builder.finish() {
171171
Ok((psbt, _)) => {
@@ -365,7 +365,7 @@ where
365365
pub fn spend_spendable_outputs<C: Signing>(
366366
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
367367
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
368-
secp_ctx: &Secp256k1<C>,
368+
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
369369
) -> Result<Transaction, ()> {
370370
let only_non_static = &descriptors
371371
.iter()
@@ -377,6 +377,7 @@ where
377377
outputs,
378378
change_destination_script,
379379
feerate_sat_per_1000_weight,
380+
locktime,
380381
secp_ctx,
381382
)
382383
}

0 commit comments

Comments
 (0)