Skip to content

Commit 854e9ca

Browse files
committed
WIP: Add FundingNeeded event for splicing
Rather than requiring the user to pass FundingTxInputs when initiating a splice, generate a FundingNeeded event once the channel has become quiescent. This simplifies error handling and UTXO / change address clean-up by consolidating it in SpliceFailed event handling. Later, this event will be used for opportunistic contributions (i.e., when the counterparty wins quiescence or initiates), dual-funding, and RBF.
1 parent 3448da2 commit 854e9ca

File tree

6 files changed

+849
-471
lines changed

6 files changed

+849
-471
lines changed

lightning/src/events/bump_transaction/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +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::FundingTxInput;
3233
use crate::ln::types::ChannelId;
3334
use crate::prelude::*;
3435
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -49,7 +50,7 @@ use bitcoin::secp256k1::ecdsa::Signature;
4950
use bitcoin::secp256k1::{PublicKey, Secp256k1};
5051
use bitcoin::transaction::Version;
5152
use bitcoin::{
52-
OutPoint, Psbt, PubkeyHash, ScriptBuf, Sequence, Transaction, TxIn, TxOut, WPubkeyHash, Witness,
53+
FeeRate, OutPoint, Psbt, PubkeyHash, ScriptBuf, Sequence, Transaction, TxIn, TxOut, WPubkeyHash, Witness,
5354
};
5455

5556
/// A descriptor used to sign for a commitment transaction's anchor output.
@@ -269,6 +270,12 @@ pub struct Input {
269270
pub satisfaction_weight: u64,
270271
}
271272

273+
impl_writeable_tlv_based!(Input, {
274+
(1, outpoint, required),
275+
(3, previous_utxo, required),
276+
(5, satisfaction_weight, required),
277+
});
278+
272279
/// An unspent transaction output that is available to spend resulting from a successful
273280
/// [`CoinSelection`] attempt.
274281
#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
@@ -413,9 +420,16 @@ pub trait CoinSelectionSource {
413420
pub trait WalletSource {
414421
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
415422
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()>;
423+
424+
///
425+
fn select_confirmed_utxos<'a>(
426+
&'a self, must_spend: Vec<Input>, must_pay_to: &[TxOut], fee_rate: FeeRate,
427+
) -> AsyncResult<'a, Vec<FundingTxInput>, ()>;
428+
416429
/// Returns a script to use for change above dust resulting from a successful coin selection
417430
/// attempt.
418431
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
432+
419433
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
420434
/// the transaction known to the wallet (i.e., any provided via
421435
/// [`WalletSource::list_confirmed_utxos`]).

lightning/src/events/bump_transaction/sync.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use core::task;
1515

1616
use crate::chain::chaininterface::BroadcasterInterface;
1717
use crate::chain::ClaimId;
18+
use crate::ln::funding::FundingTxInput;
1819
use crate::prelude::*;
1920
use crate::sign::SignerProvider;
2021
use crate::util::async_poll::{dummy_waker, AsyncResult, MaybeSend, MaybeSync};
2122
use crate::util::logger::Logger;
2223

23-
use bitcoin::{Psbt, ScriptBuf, Transaction, TxOut};
24+
use bitcoin::{FeeRate, Psbt, ScriptBuf, Transaction, TxOut};
2425

2526
use super::BumpTransactionEvent;
2627
use super::{
@@ -36,9 +37,16 @@ use super::{
3637
pub trait WalletSourceSync {
3738
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
3839
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()>;
40+
41+
///
42+
fn select_confirmed_utxos(
43+
&self, must_spend: Vec<Input>, must_pay_to: &[TxOut], fee_rate: FeeRate,
44+
) -> Result<Vec<FundingTxInput>, ()>;
45+
3946
/// Returns a script to use for change above dust resulting from a successful coin selection
4047
/// attempt.
4148
fn get_change_script(&self) -> Result<ScriptBuf, ()>;
49+
4250
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
4351
/// the transaction known to the wallet (i.e., any provided via
4452
/// [`WalletSource::list_confirmed_utxos`]).
@@ -76,6 +84,13 @@ where
7684
Box::pin(async move { utxos })
7785
}
7886

87+
fn select_confirmed_utxos<'a>(
88+
&'a self, must_spend: Vec<Input>, must_pay_to: &[TxOut], fee_rate: FeeRate,
89+
) -> AsyncResult<'a, Vec<FundingTxInput>, ()> {
90+
let utxos = self.0.select_confirmed_utxos(must_spend, must_pay_to, fee_rate);
91+
Box::pin(async move { utxos })
92+
}
93+
7994
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()> {
8095
let script = self.0.get_change_script();
8196
Box::pin(async move { script })

lightning/src/events/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::blinded_path::payment::{
2525
use crate::chain::transaction;
2626
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
2727
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
28+
use crate::ln::funding::FundingTemplate;
2829
use crate::ln::types::ChannelId;
2930
use crate::ln::{msgs, LocalHTLCFailureReason};
3031
use crate::offers::invoice::Bolt12Invoice;
@@ -1816,6 +1817,28 @@ pub enum Event {
18161817
/// [`ChannelManager::respond_to_static_invoice_request`]: crate::ln::channelmanager::ChannelManager::respond_to_static_invoice_request
18171818
invoice_request: InvoiceRequest,
18181819
},
1820+
///
1821+
FundingNeeded {
1822+
/// The `channel_id` of the channel which you'll need to pass back into
1823+
/// [`ChannelManager::funding_contributed`].
1824+
///
1825+
/// [`ChannelManager::funding_contributed`]: crate::ln::channelmanager::ChannelManager::funding_contributed
1826+
channel_id: ChannelId,
1827+
/// The counterparty's `node_id`, which you'll need to pass back into
1828+
/// [`ChannelManager::funding_contributed`].
1829+
///
1830+
/// [`ChannelManager::funding_contributed`]: crate::ln::channelmanager::ChannelManager::funding_contributed
1831+
counterparty_node_id: PublicKey,
1832+
/// The `user_channel_id` value passed in for outbound channels, or for inbound channels if
1833+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
1834+
/// `user_channel_id` will be randomized for inbound channels.
1835+
///
1836+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
1837+
user_channel_id: u128,
1838+
1839+
///
1840+
funding_template: FundingTemplate,
1841+
},
18191842
/// Indicates that a channel funding transaction constructed interactively is ready to be
18201843
/// signed. This event will only be triggered if at least one input was contributed.
18211844
///
@@ -2347,6 +2370,20 @@ impl Writeable for Event {
23472370
(13, *contributed_outputs, optional_vec),
23482371
});
23492372
},
2373+
&Event::FundingNeeded {
2374+
ref channel_id,
2375+
ref user_channel_id,
2376+
ref counterparty_node_id,
2377+
ref funding_template,
2378+
} => {
2379+
54u8.write(writer)?;
2380+
write_tlv_fields!(writer, {
2381+
(1, channel_id, required),
2382+
(3, user_channel_id, required),
2383+
(5, counterparty_node_id, required),
2384+
(7, funding_template, required),
2385+
});
2386+
},
23502387
// Note that, going forward, all new events must only write data inside of
23512388
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
23522389
// data via `write_tlv_fields`.
@@ -2978,6 +3015,24 @@ impl MaybeReadable for Event {
29783015
};
29793016
f()
29803017
},
3018+
54u8 => {
3019+
let mut f = || {
3020+
_init_and_read_len_prefixed_tlv_fields!(reader, {
3021+
(1, channel_id, required),
3022+
(3, user_channel_id, required),
3023+
(5, counterparty_node_id, required),
3024+
(7, funding_template, required),
3025+
});
3026+
3027+
Ok(Some(Event::FundingNeeded {
3028+
channel_id: channel_id.0.unwrap(),
3029+
user_channel_id: user_channel_id.0.unwrap(),
3030+
counterparty_node_id: counterparty_node_id.0.unwrap(),
3031+
funding_template: funding_template.0.unwrap(),
3032+
}))
3033+
};
3034+
f()
3035+
},
29813036
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
29823037
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
29833038
// reads.

0 commit comments

Comments
 (0)