Skip to content

Commit b3278a4

Browse files
Merge #1316: tx_builder: Support setting explicit nSequence for foreign inputs
9bb39a3 Avoid a wildcard match in tx construction (Steven Roose) 9e098a5 tx_builder: Support setting explicit nSequence for foreign inputs (Steven Roose) Pull request description: Fixes #1315. ACKs for top commit: evanlinjin: ACK 9bb39a3 danielabrozzoni: utACK 9bb39a3 Tree-SHA512: 42c96a58a762fa8737402ebd0132ce20ce0359c996cee9feeecb0b84e6fb73305be1aec9429fb97ba932486f0b35ac5caed7b43656456c5fb053e55330a12d47
2 parents d8f74dc + 9bb39a3 commit b3278a4

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

crates/bdk/src/types.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::convert::AsRef;
1414
use core::ops::Sub;
1515

1616
use bdk_chain::ConfirmationTime;
17-
use bitcoin::blockdata::transaction::{OutPoint, TxOut};
17+
use bitcoin::blockdata::transaction::{OutPoint, Sequence, TxOut};
1818
use bitcoin::{psbt, Weight};
1919

2020
use serde::{Deserialize, Serialize};
@@ -197,6 +197,8 @@ pub enum Utxo {
197197
Foreign {
198198
/// The location of the output.
199199
outpoint: OutPoint,
200+
/// The nSequence value to set for this input.
201+
sequence: Option<Sequence>,
200202
/// The information about the input we require to add it to a PSBT.
201203
// Box it to stop the type being too big.
202204
psbt_input: Box<psbt::Input>,
@@ -219,6 +221,7 @@ impl Utxo {
219221
Utxo::Foreign {
220222
outpoint,
221223
psbt_input,
224+
..
222225
} => {
223226
if let Some(prev_tx) = &psbt_input.non_witness_utxo {
224227
return &prev_tx.output[outpoint.vout as usize];
@@ -232,6 +235,14 @@ impl Utxo {
232235
}
233236
}
234237
}
238+
239+
/// Get the sequence number if an explicit sequence number has to be set for this input.
240+
pub fn sequence(&self) -> Option<Sequence> {
241+
match self {
242+
Utxo::Local(_) => None,
243+
Utxo::Foreign { sequence, .. } => *sequence,
244+
}
245+
}
235246
}
236247

237248
#[cfg(test)]

crates/bdk/src/wallet/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ impl<D> Wallet<D> {
13471347
}
13481348
Some(tx_builder::Version(x)) => x,
13491349
None if requirements.csv.is_some() => 2,
1350-
_ => 1,
1350+
None => 1,
13511351
};
13521352

13531353
// We use a match here instead of a unwrap_or_else as it's way more readable :)
@@ -1400,6 +1400,7 @@ impl<D> Wallet<D> {
14001400
}
14011401
};
14021402

1403+
// The nSequence to be by default for inputs unless an explicit sequence is specified.
14031404
let n_sequence = match (params.rbf, requirements.csv) {
14041405
// No RBF or CSV but there's an nLockTime, so the nSequence cannot be final
14051406
(None, None) if lock_time != absolute::LockTime::ZERO => {
@@ -1558,7 +1559,7 @@ impl<D> Wallet<D> {
15581559
.map(|u| bitcoin::TxIn {
15591560
previous_output: u.outpoint(),
15601561
script_sig: ScriptBuf::default(),
1561-
sequence: n_sequence,
1562+
sequence: u.sequence().unwrap_or(n_sequence),
15621563
witness: Witness::new(),
15631564
})
15641565
.collect();
@@ -1738,6 +1739,7 @@ impl<D> Wallet<D> {
17381739
satisfaction_weight,
17391740
utxo: Utxo::Foreign {
17401741
outpoint: txin.previous_output,
1742+
sequence: Some(txin.sequence),
17411743
psbt_input: Box::new(psbt::Input {
17421744
witness_utxo: Some(txout.clone()),
17431745
non_witness_utxo: Some(prev_tx.clone()),
@@ -2218,8 +2220,9 @@ impl<D> Wallet<D> {
22182220
}
22192221
}
22202222
Utxo::Foreign {
2221-
psbt_input: foreign_psbt_input,
22222223
outpoint,
2224+
psbt_input: foreign_psbt_input,
2225+
..
22232226
} => {
22242227
let is_taproot = foreign_psbt_input
22252228
.witness_utxo

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
389389
outpoint: OutPoint,
390390
psbt_input: psbt::Input,
391391
satisfaction_weight: usize,
392+
) -> Result<&mut Self, AddForeignUtxoError> {
393+
self.add_foreign_utxo_with_sequence(
394+
outpoint,
395+
psbt_input,
396+
satisfaction_weight,
397+
Sequence::MAX,
398+
)
399+
}
400+
401+
/// Same as [add_foreign_utxo](TxBuilder::add_foreign_utxo) but allows to set the nSequence value.
402+
pub fn add_foreign_utxo_with_sequence(
403+
&mut self,
404+
outpoint: OutPoint,
405+
psbt_input: psbt::Input,
406+
satisfaction_weight: usize,
407+
sequence: Sequence,
392408
) -> Result<&mut Self, AddForeignUtxoError> {
393409
if psbt_input.witness_utxo.is_none() {
394410
match psbt_input.non_witness_utxo.as_ref() {
@@ -413,6 +429,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
413429
satisfaction_weight,
414430
utxo: Utxo::Foreign {
415431
outpoint,
432+
sequence: Some(sequence),
416433
psbt_input: Box::new(psbt_input),
417434
},
418435
});

0 commit comments

Comments
 (0)