Skip to content

Commit e116023

Browse files
committed
Add change_script to FundingNegotiationContext
Instead of passing the optional change script as another parameter to FundingNegotiationContext::into_interactive_tx_constructor, make it a member of FundingNegotiationContext. Also, allow it to be passed to ChannelManager::splice_channel.
1 parent 08cc0be commit e116023

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

lightning/src/ln/channel.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5954,17 +5954,19 @@ pub(super) struct FundingNegotiationContext {
59545954
/// The funding inputs we will be contributing to the channel.
59555955
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
59565956
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5957+
/// The change output script. This will be used if needed or -- if not set -- generated using
5958+
/// `SignerProvider::get_destination_script`.
5959+
#[allow(dead_code)] // TODO(splicing): Remove once splicing is enabled.
5960+
pub change_script: Option<ScriptBuf>,
59575961
}
59585962

59595963
impl FundingNegotiationContext {
59605964
/// Prepare and start interactive transaction negotiation.
5961-
/// `change_destination_opt` - Optional destination for optional change; if None,
5962-
/// default destination address is used.
59635965
/// If error occurs, it is caused by our side, not the counterparty.
59645966
#[cfg(splicing)]
59655967
fn into_interactive_tx_constructor<SP: Deref, ES: Deref>(
59665968
self, context: &ChannelContext<SP>, funding: &FundingScope, signer_provider: &SP,
5967-
entropy_source: &ES, holder_node_id: PublicKey, change_destination_opt: Option<ScriptBuf>,
5969+
entropy_source: &ES, holder_node_id: PublicKey,
59685970
) -> Result<InteractiveTxConstructor, AbortReason>
59695971
where
59705972
SP::Target: SignerProvider,
@@ -6001,12 +6003,12 @@ impl FundingNegotiationContext {
60016003
context.holder_dust_limit_satoshis,
60026004
)?;
60036005
if let Some(change_value) = change_value_opt {
6004-
let change_script = if let Some(script) = change_destination_opt {
6006+
let change_script = if let Some(script) = self.change_script {
60056007
script
60066008
} else {
6007-
signer_provider.get_destination_script(context.channel_keys_id).map_err(
6008-
|_err| AbortReason::InternalError("Error getting destination script"),
6009-
)?
6009+
signer_provider
6010+
.get_destination_script(context.channel_keys_id)
6011+
.map_err(|_err| AbortReason::InternalError("Error getting change script"))?
60106012
};
60116013
let mut change_output =
60126014
TxOut { value: Amount::from_sat(change_value), script_pubkey: change_script };
@@ -10573,11 +10575,13 @@ where
1057310575
/// Initiate splicing.
1057410576
/// - `our_funding_inputs`: the inputs we contribute to the new funding transaction.
1057510577
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
10578+
/// - `change_script`: an option change output script. If `None` and needed, one will be
10579+
/// generated by `SignerProvider::get_destination_script`.
1057610580
#[cfg(splicing)]
1057710581
pub fn splice_channel(
1057810582
&mut self, our_funding_contribution_satoshis: i64,
10579-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, funding_feerate_per_kw: u32,
10580-
locktime: u32,
10583+
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
10584+
funding_feerate_per_kw: u32, locktime: u32,
1058110585
) -> Result<msgs::SpliceInit, APIError> {
1058210586
// Check if a splice has been initiated already.
1058310587
// Note: only a single outstanding splice is supported (per spec)
@@ -10648,6 +10652,7 @@ where
1064810652
funding_feerate_sat_per_1000_weight: funding_feerate_per_kw,
1064910653
shared_funding_input: Some(prev_funding_input),
1065010654
our_funding_inputs: funding_inputs,
10655+
change_script,
1065110656
};
1065210657

1065310658
self.pending_splice = Some(PendingSplice {
@@ -10755,6 +10760,7 @@ where
1075510760
funding_feerate_sat_per_1000_weight: msg.funding_feerate_per_kw,
1075610761
shared_funding_input: Some(prev_funding_input),
1075710762
our_funding_inputs: Vec::new(),
10763+
change_script: None,
1075810764
};
1075910765

1076010766
let mut interactive_tx_constructor = funding_negotiation_context
@@ -10764,7 +10770,6 @@ where
1076410770
signer_provider,
1076510771
entropy_source,
1076610772
holder_node_id.clone(),
10767-
None,
1076810773
)
1076910774
.map_err(|err| {
1077010775
ChannelError::Warn(format!(
@@ -10864,7 +10869,6 @@ where
1086410869
signer_provider,
1086510870
entropy_source,
1086610871
holder_node_id.clone(),
10867-
None,
1086810872
)
1086910873
.map_err(|err| {
1087010874
ChannelError::Warn(format!("V2 channel rejected due to sender error, {:?}", err))
@@ -12443,6 +12447,7 @@ where
1244312447
funding_feerate_sat_per_1000_weight,
1244412448
shared_funding_input: None,
1244512449
our_funding_inputs: funding_inputs,
12450+
change_script: None,
1244612451
};
1244712452
let chan = Self {
1244812453
funding,
@@ -12598,6 +12603,7 @@ where
1259812603
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
1259912604
shared_funding_input: None,
1260012605
our_funding_inputs: our_funding_inputs.clone(),
12606+
change_script: None,
1260112607
};
1260212608
let shared_funding_output = TxOut {
1260312609
value: Amount::from_sat(funding.get_value_satoshis()),

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::secp256k1::{PublicKey, SecretKey};
3333
use bitcoin::{secp256k1, Sequence};
3434
#[cfg(splicing)]
35-
use bitcoin::{TxIn, Weight};
35+
use bitcoin::{ScriptBuf, TxIn, Weight};
3636

3737
use crate::blinded_path::message::MessageForwardNode;
3838
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
@@ -4437,13 +4437,13 @@ where
44374437
#[rustfmt::skip]
44384438
pub fn splice_channel(
44394439
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
4440-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
4440+
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
44414441
funding_feerate_per_kw: u32, locktime: Option<u32>,
44424442
) -> Result<(), APIError> {
44434443
let mut res = Ok(());
44444444
PersistenceNotifierGuard::optionally_notify(self, || {
44454445
let result = self.internal_splice_channel(
4446-
channel_id, counterparty_node_id, our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime
4446+
channel_id, counterparty_node_id, our_funding_contribution_satoshis, our_funding_inputs, change_script, funding_feerate_per_kw, locktime
44474447
);
44484448
res = result;
44494449
match res {
@@ -4459,8 +4459,8 @@ where
44594459
fn internal_splice_channel(
44604460
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
44614461
our_funding_contribution_satoshis: i64,
4462-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, funding_feerate_per_kw: u32,
4463-
locktime: Option<u32>,
4462+
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
4463+
funding_feerate_per_kw: u32, locktime: Option<u32>,
44644464
) -> Result<(), APIError> {
44654465
let per_peer_state = self.per_peer_state.read().unwrap();
44664466

@@ -4484,6 +4484,7 @@ where
44844484
let msg = chan.splice_channel(
44854485
our_funding_contribution_satoshis,
44864486
our_funding_inputs,
4487+
change_script,
44874488
funding_feerate_per_kw,
44884489
locktime,
44894490
)?;

lightning/src/ln/interactivetxs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,7 @@ mod tests {
30033003
funding_feerate_sat_per_1000_weight,
30043004
shared_funding_input: None,
30053005
our_funding_inputs: inputs,
3006+
change_script: None,
30063007
};
30073008
assert_eq!(
30083009
calculate_change_output_value(&context, false, &ScriptBuf::new(), &outputs, 300),

lightning/src/ln/splicing_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn test_v1_splice_in() {
7474
&acceptor_node.node.get_our_node_id(),
7575
splice_in_sats as i64,
7676
funding_inputs,
77+
None, // change_script
7778
funding_feerate_per_kw,
7879
None, // locktime
7980
)
@@ -322,6 +323,7 @@ fn test_v1_splice_in_negative_insufficient_inputs() {
322323
&nodes[1].node.get_our_node_id(),
323324
splice_in_sats as i64,
324325
funding_inputs,
326+
None, // change_script
325327
1024, // funding_feerate_per_kw,
326328
None, // locktime
327329
);

0 commit comments

Comments
 (0)