Skip to content

Commit 20c4dc5

Browse files
committed
f Account for nLockTime given in spend_spendable_outputs
1 parent e847937 commit 20c4dc5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/event.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use lightning::util::errors::APIError;
2020
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
2121

2222
use bitcoin::secp256k1::{PublicKey, Secp256k1};
23-
use bitcoin::OutPoint;
23+
use bitcoin::{LockTime, OutPoint, PackedLockTime};
2424
use rand::{thread_rng, Rng};
2525
use std::collections::VecDeque;
2626
use std::ops::Deref;
@@ -270,11 +270,15 @@ where
270270
// channel.
271271
let confirmation_target = ConfirmationTarget::Normal;
272272

273+
let cur_height = self.channel_manager.current_best_block().height();
274+
let locktime = LockTime::from_height(cur_height).unwrap_or(LockTime::ZERO);
275+
273276
// Sign the final funding transaction and broadcast it.
274277
match self.wallet.create_funding_transaction(
275278
output_script,
276279
channel_value_satoshis,
277280
confirmation_target,
281+
locktime,
278282
) {
279283
Ok(final_tx) => {
280284
// Give the funding transaction back to LDK for opening the channel.
@@ -556,11 +560,17 @@ where
556560
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
557561
let tx_feerate =
558562
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
563+
564+
// We set nLockTime to the current height to discourage fee sniping.
565+
let cur_height = self.channel_manager.current_best_block().height();
566+
let locktime: PackedLockTime =
567+
LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
559568
let res = self.keys_manager.spend_spendable_outputs(
560569
output_descriptors,
561570
Vec::new(),
562571
destination_address.script_pubkey(),
563572
tx_feerate,
573+
Some(locktime),
564574
&Secp256k1::new(),
565575
);
566576
match res {

src/wallet.rs

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

2929
use std::collections::HashMap;
3030
use std::ops::Deref;
@@ -154,13 +154,18 @@ where
154154

155155
pub(crate) fn create_funding_transaction(
156156
&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).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, _)) => {
@@ -372,7 +377,7 @@ where
372377
pub fn spend_spendable_outputs<C: Signing>(
373378
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
374379
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
375-
secp_ctx: &Secp256k1<C>,
380+
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
376381
) -> Result<Option<Transaction>, ()> {
377382
let only_non_static = &descriptors
378383
.iter()
@@ -388,6 +393,7 @@ where
388393
outputs,
389394
change_destination_script,
390395
feerate_sat_per_1000_weight,
396+
locktime,
391397
secp_ctx,
392398
)
393399
.map(Some)

0 commit comments

Comments
 (0)