Skip to content

Commit 6d16896

Browse files
committed
Move FundingTxInput::sequence to Utxo
A forthcoming commit will change CoinSelection to include FundingTxInput instead of Utxo, though the former will probably be renamed. This is so CoinSelectionSource can be used when funding a splice. Further updating WalletSource to use FundingTxInput is not desirable, however, as it would result in looking up each confirmed UTXOs previous transaction even if it is not selected. See Wallet's implementation of CoinSelectionSource, which delegates to WalletSource for listing all confirmed UTXOs. This commit moves FundingTxInput::sequence to Utxo, and thus the responsibility for setting it to WalletSource implementations. Doing so will allow Wallet's CoinSelectionSource implementation to delegate looking up previous transactions to WalletSource without having to explicitly set the sequence on any FundingTxInput.
1 parent fb81fe7 commit 6d16896

4 files changed

Lines changed: 20 additions & 13 deletions

File tree

lightning/src/events/bump_transaction/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,15 @@ pub struct Utxo {
281281
/// with their lengths included, required to satisfy the output's script. The weight consumed by
282282
/// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
283283
pub satisfaction_weight: u64,
284+
/// The sequence number to use in the [`TxIn`] when spending the UTXO.
285+
pub sequence: Sequence,
284286
}
285287

286288
impl_writeable_tlv_based!(Utxo, {
287289
(1, outpoint, required),
288290
(3, output, required),
289291
(5, satisfaction_weight, required),
292+
(7, sequence, (default_value, Sequence::ENABLE_RBF_NO_LOCKTIME)),
290293
});
291294

292295
impl Utxo {
@@ -301,6 +304,7 @@ impl Utxo {
301304
outpoint,
302305
output: TxOut { value, script_pubkey: ScriptBuf::new_p2pkh(pubkey_hash) },
303306
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + 1, /* empty witness */
307+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
304308
}
305309
}
306310

@@ -320,6 +324,7 @@ impl Utxo {
320324
},
321325
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64
322326
+ P2WPKH_WITNESS_WEIGHT,
327+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
323328
}
324329
}
325330

@@ -329,6 +334,7 @@ impl Utxo {
329334
outpoint,
330335
output: TxOut { value, script_pubkey: ScriptBuf::new_p2wpkh(pubkey_hash) },
331336
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
337+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
332338
}
333339
}
334340
}
@@ -717,7 +723,7 @@ where
717723
tx.input.push(TxIn {
718724
previous_output: utxo.outpoint,
719725
script_sig: ScriptBuf::new(),
720-
sequence: Sequence::ZERO,
726+
sequence: utxo.sequence,
721727
witness: Witness::new(),
722728
});
723729
}
@@ -1343,6 +1349,7 @@ mod tests {
13431349
script_pubkey: ScriptBuf::new(),
13441350
},
13451351
satisfaction_weight: 5, // Just the script_sig and witness lengths
1352+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
13461353
}],
13471354
change_output: None,
13481355
},

lightning/src/ln/funding.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ pub struct FundingTxInput {
108108
/// [`TxOut`]: bitcoin::TxOut
109109
pub(super) utxo: Utxo,
110110

111-
/// The sequence number to use in the [`TxIn`].
112-
///
113-
/// [`TxIn`]: bitcoin::TxIn
114-
pub(super) sequence: Sequence,
115-
116111
/// The transaction containing the unspent [`TxOut`] referenced by [`utxo`].
117112
///
118113
/// [`TxOut`]: bitcoin::TxOut
@@ -122,7 +117,7 @@ pub struct FundingTxInput {
122117

123118
impl_writeable_tlv_based!(FundingTxInput, {
124119
(1, utxo, required),
125-
(3, sequence, required),
120+
(3, _sequence, (legacy, Sequence, |input: &FundingTxInput| Some(input.utxo.sequence))),
126121
(5, prevtx, required),
127122
});
128123

@@ -140,8 +135,8 @@ impl FundingTxInput {
140135
.ok_or(())?
141136
.clone(),
142137
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(),
138+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
143139
},
144-
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
145140
prevtx,
146141
})
147142
}
@@ -234,14 +229,14 @@ impl FundingTxInput {
234229
///
235230
/// [`TxIn`]: bitcoin::TxIn
236231
pub fn sequence(&self) -> Sequence {
237-
self.sequence
232+
self.utxo.sequence
238233
}
239234

240235
/// Sets the sequence number to use in the [`TxIn`].
241236
///
242237
/// [`TxIn`]: bitcoin::TxIn
243238
pub fn set_sequence(&mut self, sequence: Sequence) {
244-
self.sequence = sequence;
239+
self.utxo.sequence = sequence;
245240
}
246241

247242
/// Converts the [`FundingTxInput`] into a [`Utxo`] for coin selection.

lightning/src/ln/interactivetxs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,9 +2061,13 @@ impl InteractiveTxConstructor {
20612061

20622062
let mut inputs_to_contribute: Vec<(SerialId, InputOwned)> = inputs_to_contribute
20632063
.into_iter()
2064-
.map(|FundingTxInput { utxo, sequence, prevtx: prev_tx }| {
2064+
.map(|FundingTxInput { utxo, prevtx: prev_tx }| {
20652065
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
2066-
let txin = TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() };
2066+
let txin = TxIn {
2067+
previous_output: utxo.outpoint,
2068+
sequence: utxo.sequence,
2069+
..Default::default()
2070+
};
20672071
let prev_output = utxo.output;
20682072
let input = InputOwned::Single(SingleOwnedInput {
20692073
input: txin,

lightning/src/util/anchor_channel_reserves.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ where
330330
#[cfg(test)]
331331
mod test {
332332
use super::*;
333-
use bitcoin::{OutPoint, ScriptBuf, TxOut, Txid};
333+
use bitcoin::{OutPoint, ScriptBuf, Sequence, TxOut, Txid};
334334
use std::str::FromStr;
335335

336336
#[test]
@@ -358,6 +358,7 @@ mod test {
358358
},
359359
output: TxOut { value: amount, script_pubkey: ScriptBuf::new() },
360360
satisfaction_weight: 1 * 4 + (1 + 1 + 72 + 1 + 33),
361+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
361362
}
362363
}
363364

0 commit comments

Comments
 (0)