Skip to content

Commit 222511e

Browse files
committed
Get funding info from context if available in get_funding_created
This logic ensures that users don’t need to create funding transactions again if they did so once for an UnfundedOutboundChannel.
1 parent 52862e3 commit 222511e

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22402240
self.channel_transaction_parameters.funding_outpoint
22412241
}
22422242

2243+
fn get_funding_transaction(&self) -> Option<Transaction> {
2244+
self.funding_transaction.clone()
2245+
}
2246+
22432247
/// Returns the height in which our funding transaction was confirmed.
22442248
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
22452249
let conf_height = self.funding_tx_confirmation_height;
@@ -7375,7 +7379,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
73757379
/// Note that channel_id changes during this call!
73767380
/// Do NOT broadcast the funding transaction until after a successful funding_signed call!
73777381
/// If an Err is returned, it is a ChannelError::Close.
7378-
pub fn get_funding_created<L: Deref>(&mut self, funding_transaction: Transaction, funding_txo: OutPoint, is_batch_funding: bool, logger: &L)
7382+
pub fn get_funding_created<L: Deref>(&mut self, funding_transaction: Option<Transaction>, funding_txo: Option<OutPoint>, is_batch_funding: Option<bool>, logger: &L)
73797383
-> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> where L::Target: Logger {
73807384
if !self.context.is_outbound() {
73817385
panic!("Tried to create outbound funding_created message on an inbound channel!");
@@ -7392,6 +7396,23 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
73927396
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
73937397
}
73947398

7399+
// This section of logic verifies if `funding_transaction` and `funding_txo` are provided.
7400+
// If not, they are retrieved from the context. The decision to proceed with the logic
7401+
// even when context values are available ensures consistent flow and passes through all
7402+
// original sanity checks.
7403+
let (funding_transaction, funding_txo, is_batch_funding) = match (funding_transaction, funding_txo, is_batch_funding) {
7404+
(Some(tx), Some(txo), Some(batch)) => (tx, txo, batch),
7405+
(None, None, None) => {
7406+
let funding_tx = self.context.get_funding_transaction()
7407+
.unwrap_or_else(|| panic!("Failed to get funding transaction from context"));
7408+
let funding_txo = self.context.get_funding_txo()
7409+
.unwrap_or_else(|| panic!("Failed to get funding txo from context"));
7410+
let batch_funding = self.context.is_batch_funding.is_some();
7411+
(funding_tx, funding_txo, batch_funding)
7412+
}
7413+
_ => panic!("Either all or None of funding_transaction, funding_txo, and is_batch_funding should be provided"),
7414+
};
7415+
73957416
self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
73967417
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
73977418

@@ -9511,7 +9532,7 @@ mod tests {
95119532
value: 10000000, script_pubkey: output_script.clone(),
95129533
}]};
95139534
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9514-
let funding_created_msg = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
9535+
let funding_created_msg = node_a_chan.get_funding_created(Some(tx.clone()), Some(funding_outpoint), Some(false), &&logger).map_err(|_| ()).unwrap();
95159536
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
95169537

95179538
// Node B --> Node A: funding signed
@@ -9640,7 +9661,7 @@ mod tests {
96409661
value: 10000000, script_pubkey: output_script.clone(),
96419662
}]};
96429663
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9643-
let funding_created_msg = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
9664+
let funding_created_msg = node_a_chan.get_funding_created(Some(tx.clone()), Some(funding_outpoint), Some(false), &&logger).map_err(|_| ()).unwrap();
96449665
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
96459666

96469667
// Node B --> Node A: funding signed
@@ -9829,7 +9850,7 @@ mod tests {
98299850
value: 10000000, script_pubkey: output_script.clone(),
98309851
}]};
98319852
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9832-
let funding_created_msg = node_a_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap();
9853+
let funding_created_msg = node_a_chan.get_funding_created(Some(tx.clone()), Some(funding_outpoint), Some(false), &&logger).map_err(|_| ()).unwrap();
98339854
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
98349855

98359856
// Node B --> Node A: funding signed
@@ -9896,7 +9917,7 @@ mod tests {
98969917
value: 10000000, script_pubkey: outbound_chan.context.get_funding_redeemscript(),
98979918
}]};
98989919
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
9899-
let funding_created = outbound_chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap().unwrap();
9920+
let funding_created = outbound_chan.get_funding_created(Some(tx.clone()), Some(funding_outpoint), Some(false), &&logger).map_err(|_| ()).unwrap().unwrap();
99009921
let mut chan = match inbound_chan.funding_created(&funding_created, best_block, &&keys_provider, &&logger) {
99019922
Ok((chan, _, _)) => chan,
99029923
Err((_, e)) => panic!("{}", e),
@@ -11029,7 +11050,7 @@ mod tests {
1102911050
]};
1103011051
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
1103111052
let funding_created_msg = node_a_chan.get_funding_created(
11032-
tx.clone(), funding_outpoint, true, &&logger,
11053+
Some(tx.clone()), Some(funding_outpoint), Some(true), &&logger,
1103311054
).map_err(|_| ()).unwrap();
1103411055
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(
1103511056
&funding_created_msg.unwrap(),

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4484,7 +4484,7 @@ where
44844484
funding_txo = find_funding_output(&chan, &funding_transaction)?;
44854485

44864486
let logger = WithChannelContext::from(&self.logger, &chan.context);
4487-
let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger)
4487+
let funding_res = chan.get_funding_created(Some(funding_transaction), Some(funding_txo), Some(is_batch_funding), &&logger)
44884488
.map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
44894489
let channel_id = chan.context.channel_id();
44904490
let reason = ClosureReason::ProcessingError { err: msg.clone() };

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9235,7 +9235,7 @@ fn test_duplicate_chan_id() {
92359235
match a_peer_state.channel_by_id.remove(&open_chan_2_msg.common_fields.temporary_channel_id).unwrap() {
92369236
ChannelPhase::UnfundedOutboundV1(mut chan) => {
92379237
let logger = test_utils::TestLogger::new();
9238-
chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap()
9238+
chan.get_funding_created(Some(tx.clone()), Some(funding_outpoint), Some(false), &&logger).map_err(|_| ()).unwrap()
92399239
},
92409240
_ => panic!("Unexpected ChannelPhase variant"),
92419241
}.unwrap()

0 commit comments

Comments
 (0)