Skip to content

Commit 7798325

Browse files
committed
f - use ConfirmedUtxo in TestWalletSource
1 parent 02ec3f7 commit 7798325

4 files changed

Lines changed: 24 additions & 33 deletions

File tree

lightning/src/events/bump_transaction/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::ln::chan_utils::{
2929
HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT, HTLC_TIMEOUT_INPUT_P2A_ANCHOR_WITNESS_WEIGHT,
3030
P2WSH_TXOUT_WEIGHT, SEGWIT_MARKER_FLAG_WEIGHT, TRUC_CHILD_MAX_WEIGHT, TRUC_MAX_WEIGHT,
3131
};
32-
use crate::ln::funding::ConfirmedUtxo;
32+
use crate::ln::funding::FundingTxInput;
3333
use crate::ln::types::ChannelId;
3434
use crate::prelude::*;
3535
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -340,6 +340,9 @@ impl Utxo {
340340
}
341341
}
342342

343+
/// An unspent transaction output with at least one confirmation.
344+
pub type ConfirmedUtxo = FundingTxInput;
345+
343346
/// The result of a successful coin selection attempt for a transaction requiring additional UTXOs
344347
/// to cover its fees.
345348
#[derive(Clone, Debug)]

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(
395395
let wallet_script = node.wallet_source.get_change_script().unwrap();
396396
for (idx, output) in tx.output.iter().enumerate() {
397397
if output.script_pubkey == wallet_script {
398-
let outpoint = bitcoin::OutPoint { txid: tx.compute_txid(), vout: idx as u32 };
399-
node.wallet_source.add_utxo(outpoint, output.value);
398+
node.wallet_source.add_utxo(tx.clone(), idx as u32);
400399
}
401400
}
402401
}

lightning/src/ln/funding.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ impl SpliceContribution {
9999
}
100100
}
101101

102-
/// An unspent transaction output with at least one confirmation.
102+
/// An input to contribute to a channel's funding transaction either when using the v2 channel
103+
/// establishment protocol or when splicing.
103104
#[derive(Debug, Clone)]
104-
pub struct ConfirmedUtxo {
105+
pub struct FundingTxInput {
105106
/// The unspent [`TxOut`] found in [`prevtx`].
106107
///
107108
/// [`TxOut`]: bitcoin::TxOut
@@ -115,10 +116,6 @@ pub struct ConfirmedUtxo {
115116
pub(crate) prevtx: Transaction,
116117
}
117118

118-
/// An input to contribute to a channel's funding transaction either when using the v2 channel
119-
/// establishment protocol or when splicing.
120-
pub type FundingTxInput = ConfirmedUtxo;
121-
122119
impl_writeable_tlv_based!(FundingTxInput, {
123120
(1, utxo, required),
124121
(3, _sequence, (legacy, Sequence, |input: &FundingTxInput| Some(input.utxo.sequence))),

lightning/src/util/test_utils.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::chain::channelmonitor::{
2222
use crate::chain::transaction::OutPoint;
2323
use crate::chain::WatchedOutput;
2424
use crate::events::bump_transaction::sync::WalletSourceSync;
25-
use crate::events::bump_transaction::Utxo;
25+
use crate::events::bump_transaction::{ConfirmedUtxo, Utxo};
2626
#[cfg(any(test, feature = "_externalize_tests"))]
2727
use crate::ln::chan_utils::CommitmentTransaction;
2828
use crate::ln::channel_state::ChannelDetails;
@@ -62,7 +62,6 @@ use crate::util::persist::{KVStore, KVStoreSync, MonitorName};
6262
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
6363
use crate::util::test_channel_signer::{EnforcementState, TestChannelSigner};
6464

65-
use bitcoin::absolute::LockTime;
6665
use bitcoin::amount::Amount;
6766
use bitcoin::block::Block;
6867
use bitcoin::constants::genesis_block;
@@ -72,7 +71,7 @@ use bitcoin::hashes::{hex::FromHex, Hash};
7271
use bitcoin::network::Network;
7372
use bitcoin::script::{Builder, Script, ScriptBuf};
7473
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
75-
use bitcoin::transaction::{Transaction, TxOut, Version};
74+
use bitcoin::transaction::{Transaction, TxOut};
7675
use bitcoin::{opcodes, Witness};
7776

7877
use bitcoin::secp256k1::ecdh::SharedSecret;
@@ -2211,7 +2210,7 @@ impl Drop for TestScorer {
22112210

22122211
pub struct TestWalletSource {
22132212
secret_key: SecretKey,
2214-
utxos: Mutex<Vec<Utxo>>,
2213+
utxos: Mutex<Vec<ConfirmedUtxo>>,
22152214
secp: Secp256k1<bitcoin::secp256k1::All>,
22162215
}
22172216

@@ -2220,21 +2219,13 @@ impl TestWalletSource {
22202219
Self { secret_key, utxos: Mutex::new(Vec::new()), secp: Secp256k1::new() }
22212220
}
22222221

2223-
pub fn add_utxo(&self, outpoint: bitcoin::OutPoint, value: Amount) -> TxOut {
2224-
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
2225-
let utxo = Utxo::new_v0_p2wpkh(outpoint, value, &public_key.wpubkey_hash().unwrap());
2226-
self.utxos.lock().unwrap().push(utxo.clone());
2227-
utxo.output
2228-
}
2229-
2230-
pub fn add_custom_utxo(&self, utxo: Utxo) -> TxOut {
2231-
let output = utxo.output.clone();
2222+
pub fn add_utxo(&self, prevtx: Transaction, vout: u32) {
2223+
let utxo = ConfirmedUtxo::new_p2wpkh(prevtx, vout).unwrap();
22322224
self.utxos.lock().unwrap().push(utxo);
2233-
output
22342225
}
22352226

22362227
pub fn remove_utxo(&self, outpoint: bitcoin::OutPoint) {
2237-
self.utxos.lock().unwrap().retain(|utxo| utxo.outpoint != outpoint);
2228+
self.utxos.lock().unwrap().retain(|utxo| utxo.outpoint() != outpoint);
22382229
}
22392230

22402231
pub fn clear_utxos(&self) {
@@ -2246,8 +2237,8 @@ impl TestWalletSource {
22462237
) -> Result<Transaction, bitcoin::sighash::P2wpkhError> {
22472238
let utxos = self.utxos.lock().unwrap();
22482239
for i in 0..tx.input.len() {
2249-
if let Some(utxo) =
2250-
utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output)
2240+
if let Some(ConfirmedUtxo { utxo, .. }) =
2241+
utxos.iter().find(|utxo| utxo.outpoint() == tx.input[i].previous_output)
22512242
{
22522243
let sighash = SighashCache::new(&tx).p2wpkh_signature_hash(
22532244
i,
@@ -2277,16 +2268,17 @@ impl TestWalletSource {
22772268

22782269
impl WalletSourceSync for TestWalletSource {
22792270
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
2280-
Ok(self.utxos.lock().unwrap().clone())
2271+
Ok(self.utxos.lock().unwrap().iter().map(|ConfirmedUtxo { utxo, .. }| utxo.clone()).collect())
22812272
}
22822273

22832274
fn get_prevtx(&self, utxo: &Utxo) -> Result<Transaction, ()> {
2284-
Ok(Transaction {
2285-
version: Version::TWO,
2286-
lock_time: LockTime::ZERO,
2287-
input: vec![],
2288-
output: vec![utxo.output.clone()],
2289-
})
2275+
self.utxos
2276+
.lock()
2277+
.unwrap()
2278+
.iter()
2279+
.find(|confirmed_utxo| confirmed_utxo.utxo == *utxo)
2280+
.map(|ConfirmedUtxo { prevtx, .. }| prevtx.clone())
2281+
.ok_or(())
22902282
}
22912283

22922284
fn get_change_script(&self) -> Result<ScriptBuf, ()> {

0 commit comments

Comments
 (0)