Skip to content

Commit 05d2a8e

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 a2375f5 commit 05d2a8e

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
@@ -5962,17 +5962,19 @@ pub(super) struct FundingNegotiationContext {
59625962
/// The funding inputs we will be contributing to the channel.
59635963
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
59645964
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5965+
/// The change output script. This will be used if needed or -- if not set -- generated using
5966+
/// `SignerProvider::get_destination_script`.
5967+
#[allow(dead_code)] // TODO(splicing): Remove once splicing is enabled.
5968+
pub change_script: Option<ScriptBuf>,
59655969
}
59665970

59675971
impl FundingNegotiationContext {
59685972
/// Prepare and start interactive transaction negotiation.
5969-
/// `change_destination_opt` - Optional destination for optional change; if None,
5970-
/// default destination address is used.
59715973
/// If error occurs, it is caused by our side, not the counterparty.
59725974
#[cfg(splicing)]
59735975
fn into_interactive_tx_constructor<SP: Deref, ES: Deref>(
59745976
self, context: &ChannelContext<SP>, funding: &FundingScope, signer_provider: &SP,
5975-
entropy_source: &ES, holder_node_id: PublicKey, change_destination_opt: Option<ScriptBuf>,
5977+
entropy_source: &ES, holder_node_id: PublicKey,
59765978
) -> Result<InteractiveTxConstructor, AbortReason>
59775979
where
59785980
SP::Target: SignerProvider,
@@ -6009,12 +6011,12 @@ impl FundingNegotiationContext {
60096011
context.holder_dust_limit_satoshis,
60106012
)?;
60116013
if let Some(change_value) = change_value_opt {
6012-
let change_script = if let Some(script) = change_destination_opt {
6014+
let change_script = if let Some(script) = self.change_script {
60136015
script
60146016
} else {
6015-
signer_provider.get_destination_script(context.channel_keys_id).map_err(
6016-
|_err| AbortReason::InternalError("Error getting destination script"),
6017-
)?
6017+
signer_provider
6018+
.get_destination_script(context.channel_keys_id)
6019+
.map_err(|_err| AbortReason::InternalError("Error getting change script"))?
60186020
};
60196021
let mut change_output =
60206022
TxOut { value: Amount::from_sat(change_value), script_pubkey: change_script };
@@ -10584,11 +10586,13 @@ where
1058410586
/// Initiate splicing.
1058510587
/// - `our_funding_inputs`: the inputs we contribute to the new funding transaction.
1058610588
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
10589+
/// - `change_script`: an option change output script. If `None` and needed, one will be
10590+
/// generated by `SignerProvider::get_destination_script`.
1058710591
#[cfg(splicing)]
1058810592
pub fn splice_channel(
1058910593
&mut self, our_funding_contribution_satoshis: i64,
10590-
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, funding_feerate_per_kw: u32,
10591-
locktime: u32,
10594+
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
10595+
funding_feerate_per_kw: u32, locktime: u32,
1059210596
) -> Result<msgs::SpliceInit, APIError> {
1059310597
// Check if a splice has been initiated already.
1059410598
// Note: only a single outstanding splice is supported (per spec)
@@ -10659,6 +10663,7 @@ where
1065910663
funding_feerate_sat_per_1000_weight: funding_feerate_per_kw,
1066010664
shared_funding_input: Some(prev_funding_input),
1066110665
our_funding_inputs: funding_inputs,
10666+
change_script,
1066210667
};
1066310668

1066410669
self.pending_splice = Some(PendingSplice {
@@ -10766,6 +10771,7 @@ where
1076610771
funding_feerate_sat_per_1000_weight: msg.funding_feerate_per_kw,
1076710772
shared_funding_input: Some(prev_funding_input),
1076810773
our_funding_inputs: Vec::new(),
10774+
change_script: None,
1076910775
};
1077010776

1077110777
let mut interactive_tx_constructor = funding_negotiation_context
@@ -10775,7 +10781,6 @@ where
1077510781
signer_provider,
1077610782
entropy_source,
1077710783
holder_node_id.clone(),
10778-
None,
1077910784
)
1078010785
.map_err(|err| {
1078110786
ChannelError::WarnAndDisconnect(format!(
@@ -10875,7 +10880,6 @@ where
1087510880
signer_provider,
1087610881
entropy_source,
1087710882
holder_node_id.clone(),
10878-
None,
1087910883
)
1088010884
.map_err(|err| {
1088110885
ChannelError::WarnAndDisconnect(format!(
@@ -12459,6 +12463,7 @@ where
1245912463
funding_feerate_sat_per_1000_weight,
1246012464
shared_funding_input: None,
1246112465
our_funding_inputs: funding_inputs,
12466+
change_script: None,
1246212467
};
1246312468
let chan = Self {
1246412469
funding,
@@ -12614,6 +12619,7 @@ where
1261412619
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
1261512620
shared_funding_input: None,
1261612621
our_funding_inputs: our_funding_inputs.clone(),
12622+
change_script: None,
1261712623
};
1261812624
let shared_funding_output = TxOut {
1261912625
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
@@ -2999,6 +2999,7 @@ mod tests {
29992999
funding_feerate_sat_per_1000_weight,
30003000
shared_funding_input: None,
30013001
our_funding_inputs: inputs,
3002+
change_script: None,
30023003
};
30033004
assert_eq!(
30043005
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)