Skip to content

Commit c80afe9

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 3497b59 commit c80afe9

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

lightning/src/events/bump_transaction/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,15 @@ pub struct Utxo {
284284
/// with their lengths included, required to satisfy the output's script. The weight consumed by
285285
/// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
286286
pub satisfaction_weight: u64,
287+
/// The sequence number to use in the [`TxIn`] when spending the UTXO.
288+
pub sequence: Sequence,
287289
}
288290

289291
impl_writeable_tlv_based!(Utxo, {
290292
(1, outpoint, required),
291293
(3, output, required),
292294
(5, satisfaction_weight, required),
295+
(7, sequence, (default_value, Sequence::ENABLE_RBF_NO_LOCKTIME)),
293296
});
294297

295298
impl Utxo {
@@ -304,6 +307,7 @@ impl Utxo {
304307
outpoint,
305308
output: TxOut { value, script_pubkey: ScriptBuf::new_p2pkh(pubkey_hash) },
306309
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + 1, /* empty witness */
310+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
307311
}
308312
}
309313

@@ -323,6 +327,7 @@ impl Utxo {
323327
},
324328
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64
325329
+ P2WPKH_WITNESS_WEIGHT,
330+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
326331
}
327332
}
328333

@@ -332,6 +337,7 @@ impl Utxo {
332337
outpoint,
333338
output: TxOut { value, script_pubkey: ScriptBuf::new_p2wpkh(pubkey_hash) },
334339
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
340+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
335341
}
336342
}
337343

@@ -343,6 +349,7 @@ impl Utxo {
343349
outpoint,
344350
output: TxOut { value, script_pubkey: ScriptBuf::new_p2tr_tweaked(tweaked_public_key) },
345351
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2TR_KEY_PATH_WITNESS_WEIGHT,
352+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
346353
}
347354
}
348355
}
@@ -737,7 +744,7 @@ where
737744
tx.input.push(TxIn {
738745
previous_output: utxo.outpoint,
739746
script_sig: ScriptBuf::new(),
740-
sequence: Sequence::ZERO,
747+
sequence: utxo.sequence,
741748
witness: Witness::new(),
742749
});
743750
}
@@ -1392,6 +1399,7 @@ mod tests {
13921399
script_pubkey: ScriptBuf::new(),
13931400
},
13941401
satisfaction_weight: 5, // Just the script_sig and witness lengths
1402+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
13951403
}],
13961404
change_output: None,
13971405
},

lightning/src/ln/funding.rs

Lines changed: 16 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,19 @@ pub struct FundingTxInput {
122117

123118
impl_writeable_tlv_based!(FundingTxInput, {
124119
(1, utxo, required),
125-
(3, sequence, required),
120+
(3, _sequence, (legacy, Sequence,
121+
|read_val: Option<&Sequence>| {
122+
if let Some(sequence) = read_val {
123+
// Utxo contains sequence now, so update it if the value read here differs since
124+
// this indicates Utxo::sequence was read with default_value
125+
let utxo: &mut Utxo = utxo.0.as_mut().expect("utxo is required");
126+
if utxo.sequence != *sequence {
127+
utxo.sequence = *sequence;
128+
}
129+
}
130+
Ok(())
131+
},
132+
|input: &FundingTxInput| Some(input.utxo.sequence))),
126133
(5, prevtx, required),
127134
});
128135

@@ -140,8 +147,8 @@ impl FundingTxInput {
140147
.ok_or(())?
141148
.clone(),
142149
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(),
150+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
143151
},
144-
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
145152
prevtx,
146153
})
147154
}
@@ -234,14 +241,14 @@ impl FundingTxInput {
234241
///
235242
/// [`TxIn`]: bitcoin::TxIn
236243
pub fn sequence(&self) -> Sequence {
237-
self.sequence
244+
self.utxo.sequence
238245
}
239246

240247
/// Sets the sequence number to use in the [`TxIn`].
241248
///
242249
/// [`TxIn`]: bitcoin::TxIn
243250
pub fn set_sequence(&mut self, sequence: Sequence) {
244-
self.sequence = sequence;
251+
self.utxo.sequence = sequence;
245252
}
246253

247254
/// 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
@@ -2054,9 +2054,13 @@ impl InteractiveTxConstructor {
20542054

20552055
let mut inputs_to_contribute: Vec<(SerialId, InputOwned)> = inputs_to_contribute
20562056
.into_iter()
2057-
.map(|FundingTxInput { utxo, sequence, prevtx: prev_tx }| {
2057+
.map(|FundingTxInput { utxo, prevtx: prev_tx }| {
20582058
let serial_id = generate_holder_serial_id(entropy_source, is_initiator);
2059-
let txin = TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() };
2059+
let txin = TxIn {
2060+
previous_output: utxo.outpoint,
2061+
sequence: utxo.sequence,
2062+
..Default::default()
2063+
};
20602064
let prev_output = utxo.output;
20612065
let input = InputOwned::Single(SingleOwnedInput {
20622066
input: txin,

lightning/src/util/anchor_channel_reserves.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ where
315315
#[cfg(test)]
316316
mod test {
317317
use super::*;
318-
use bitcoin::{OutPoint, ScriptBuf, TxOut, Txid};
318+
use bitcoin::{OutPoint, ScriptBuf, Sequence, TxOut, Txid};
319319
use std::str::FromStr;
320320

321321
#[test]
@@ -343,6 +343,7 @@ mod test {
343343
},
344344
output: TxOut { value: amount, script_pubkey: ScriptBuf::new() },
345345
satisfaction_weight: 1 * 4 + (1 + 1 + 72 + 1 + 33),
346+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
346347
}
347348
}
348349

0 commit comments

Comments
 (0)