Skip to content

Commit 074d7ba

Browse files
committed
Include funding_outpoint in FundingScope
1 parent e0cc446 commit 074d7ba

File tree

4 files changed

+86
-56
lines changed

4 files changed

+86
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,9 @@ pub(super) struct FundingScope {
16091609
next_local_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
16101610
#[cfg(any(test, fuzzing))]
16111611
next_remote_commitment_tx_fee_info_cached: Mutex<Option<CommitmentTxInfoCached>>,
1612+
1613+
/// The late-bound funding outpoint
1614+
funding_outpoint: Option<OutPoint>,
16121615
}
16131616

16141617
impl FundingScope {
@@ -1633,6 +1636,12 @@ impl FundingScope {
16331636
)
16341637
})
16351638
}
1639+
1640+
/// Returns the `funding_txo` we either got from our peer, or were given by
1641+
/// [`OutboundV1Channel::get_funding_created`].
1642+
pub fn get_funding_txo(&self) -> Option<OutPoint> {
1643+
self.funding_outpoint
1644+
}
16361645
}
16371646

16381647
/// Contains everything about the channel including state, and various flags.
@@ -1944,6 +1953,8 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
19441953

19451954
fn funding(&self) -> &FundingScope;
19461955

1956+
fn funding_mut(&mut self) -> &mut FundingScope;
1957+
19471958
fn received_msg(&self) -> &'static str;
19481959

19491960
fn check_counterparty_commitment_signature<L: Deref>(
@@ -1978,6 +1989,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
19781989
Err(ChannelError::Close(e)) => {
19791990
// TODO(dual_funding): Update for V2 established channels.
19801991
if !self.context().is_outbound() {
1992+
self.funding_mut().funding_outpoint = None;
19811993
self.context_mut().channel_transaction_parameters.funding_outpoint = None;
19821994
}
19831995
return Err(ChannelError::Close(e));
@@ -2031,7 +2043,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
20312043

20322044
let context = self.context();
20332045
let funding_redeemscript = context.get_funding_redeemscript();
2034-
let funding_txo = context.get_funding_txo().unwrap();
2046+
let funding_txo = self.funding().get_funding_txo().unwrap();
20352047
let funding_txo_script = funding_redeemscript.to_p2wsh();
20362048
let obscure_factor = get_commitment_transaction_number_obscure_factor(&context.get_holder_pubkeys().payment_point, &context.get_counterparty_pubkeys().payment_point, context.is_outbound());
20372049
let shutdown_script = context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
@@ -2074,6 +2086,10 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for OutboundV1Channel<SP> wh
20742086
&self.funding
20752087
}
20762088

2089+
fn funding_mut(&mut self) -> &mut FundingScope {
2090+
&mut self.funding
2091+
}
2092+
20772093
fn received_msg(&self) -> &'static str {
20782094
"funding_signed"
20792095
}
@@ -2092,6 +2108,10 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for InboundV1Channel<SP> whe
20922108
&self.funding
20932109
}
20942110

2111+
fn funding_mut(&mut self) -> &mut FundingScope {
2112+
&mut self.funding
2113+
}
2114+
20952115
fn received_msg(&self) -> &'static str {
20962116
"funding_created"
20972117
}
@@ -2110,6 +2130,10 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where
21102130
&self.funding
21112131
}
21122132

2133+
fn funding_mut(&mut self) -> &mut FundingScope {
2134+
&mut self.funding
2135+
}
2136+
21132137
fn received_msg(&self) -> &'static str {
21142138
"commitment_signed"
21152139
}
@@ -2218,6 +2242,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22182242
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
22192243
))).map_err(|e| (self, e));
22202244
};
2245+
self.funding.funding_outpoint = Some(outpoint);
22212246
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
22222247
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
22232248

@@ -2229,6 +2254,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
22292254
commitment_signed
22302255
},
22312256
Err(err) => {
2257+
self.funding.funding_outpoint = None;
22322258
self.context.channel_transaction_parameters.funding_outpoint = None;
22332259
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
22342260
.map_err(|e| (self, e));
@@ -2496,6 +2522,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24962522
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
24972523
#[cfg(any(test, fuzzing))]
24982524
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2525+
2526+
funding_outpoint: None,
24992527
};
25002528
let channel_context = ChannelContext {
25012529
user_id,
@@ -2731,6 +2759,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27312759
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
27322760
#[cfg(any(test, fuzzing))]
27332761
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
2762+
2763+
funding_outpoint: None,
27342764
};
27352765
let channel_context = Self {
27362766
user_id,
@@ -3001,12 +3031,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30013031
self.outbound_scid_alias = outbound_scid_alias;
30023032
}
30033033

3004-
/// Returns the funding_txo we either got from our peer, or were given by
3005-
/// get_funding_created.
3006-
pub fn get_funding_txo(&self) -> Option<OutPoint> {
3007-
self.channel_transaction_parameters.funding_outpoint
3008-
}
3009-
30103034
/// Returns the height in which our funding transaction was confirmed.
30113035
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
30123036
let conf_height = self.funding_tx_confirmation_height;
@@ -4253,9 +4277,9 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42534277

42544278
/// Returns the transaction ID if there is a pending funding transaction that is yet to be
42554279
/// broadcast.
4256-
pub fn unbroadcasted_funding_txid(&self) -> Option<Txid> {
4280+
pub fn unbroadcasted_funding_txid(&self, funding: &FundingScope) -> Option<Txid> {
42574281
self.if_unbroadcasted_funding(||
4258-
self.channel_transaction_parameters.funding_outpoint.map(|txo| txo.txid)
4282+
funding.funding_outpoint.map(|txo| txo.txid)
42594283
)
42604284
}
42614285

@@ -4266,8 +4290,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42664290

42674291
/// Returns the transaction ID if there is a pending batch funding transaction that is yet to be
42684292
/// broadcast.
4269-
pub fn unbroadcasted_batch_funding_txid(&self) -> Option<Txid> {
4270-
self.unbroadcasted_funding_txid().filter(|_| self.is_batch_funding())
4293+
pub fn unbroadcasted_batch_funding_txid(&self, funding: &FundingScope) -> Option<Txid> {
4294+
self.unbroadcasted_funding_txid(funding).filter(|_| self.is_batch_funding())
42714295
}
42724296

42734297
/// Gets the latest commitment transaction and any dependent transactions for relay (forcing
@@ -4294,7 +4318,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42944318
_ => {}
42954319
}
42964320
}
4297-
let monitor_update = if let Some(funding_txo) = self.get_funding_txo() {
4321+
let monitor_update = if let Some(funding_txo) = funding.get_funding_txo() {
42984322
// If we haven't yet exchanged funding signatures (ie channel_state < AwaitingChannelReady),
42994323
// returning a channel monitor update here would imply a channel monitor update before
43004324
// we even registered the channel monitor to begin with, which is invalid.
@@ -4312,7 +4336,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43124336
}))
43134337
} else { None }
43144338
} else { None };
4315-
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid();
4339+
let unbroadcasted_batch_funding_txid = self.unbroadcasted_batch_funding_txid(funding);
43164340
let unbroadcasted_funding_tx = self.unbroadcasted_funding();
43174341

43184342
self.channel_state = ChannelState::ShutdownComplete;
@@ -4328,7 +4352,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
43284352
counterparty_node_id: self.counterparty_node_id,
43294353
unbroadcasted_funding_tx,
43304354
is_manual_broadcast: self.is_manual_broadcast,
4331-
channel_funding_txo: self.get_funding_txo(),
4355+
channel_funding_txo: funding.get_funding_txo(),
43324356
last_local_balance_msat: funding.value_to_self_msat,
43334357
}
43344358
}
@@ -4800,7 +4824,7 @@ impl<SP: Deref> FundedChannel<SP> where
48004824
}
48014825

48024826
fn funding_outpoint(&self) -> OutPoint {
4803-
self.context.channel_transaction_parameters.funding_outpoint.unwrap()
4827+
self.funding.funding_outpoint.unwrap()
48044828
}
48054829

48064830
/// Claims an HTLC while we're disconnected from a peer, dropping the [`ChannelMonitorUpdate`]
@@ -5099,6 +5123,7 @@ impl<SP: Deref> FundedChannel<SP> where
50995123
debug_assert!(matches!(
51005124
self.context.channel_state, ChannelState::AwaitingChannelReady(_)
51015125
));
5126+
self.funding.funding_outpoint = None;
51025127
self.context.channel_transaction_parameters.funding_outpoint = None;
51035128
self.context.channel_id = self.context.temporary_channel_id.expect(
51045129
"temporary_channel_id should be set since unset_funding_info is only called on funded \
@@ -7193,14 +7218,14 @@ impl<SP: Deref> FundedChannel<SP> where
71937218
closure_reason,
71947219
monitor_update: None,
71957220
dropped_outbound_htlcs: Vec::new(),
7196-
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
7221+
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(&self.funding),
71977222
channel_id: self.context.channel_id,
71987223
user_channel_id: self.context.user_id,
71997224
channel_capacity_satoshis: self.funding.channel_value_satoshis,
72007225
counterparty_node_id: self.context.counterparty_node_id,
72017226
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
72027227
is_manual_broadcast: self.context.is_manual_broadcast,
7203-
channel_funding_txo: self.context.get_funding_txo(),
7228+
channel_funding_txo: self.funding.get_funding_txo(),
72047229
last_local_balance_msat: self.funding.value_to_self_msat,
72057230
}
72067231
}
@@ -7769,7 +7794,7 @@ impl<SP: Deref> FundedChannel<SP> where
77697794
L::Target: Logger
77707795
{
77717796
let mut msgs = (None, None);
7772-
if let Some(funding_txo) = self.context.get_funding_txo() {
7797+
if let Some(funding_txo) = self.funding.get_funding_txo() {
77737798
for &(index_in_block, tx) in txdata.iter() {
77747799
// Check if the transaction is the expected funding transaction, and if it is,
77757800
// check that it pays the right amount to the right script.
@@ -8745,8 +8770,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
87458770

87468771
signature.map(|signature| msgs::FundingCreated {
87478772
temporary_channel_id: self.context.temporary_channel_id.unwrap(),
8748-
funding_txid: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().txid,
8749-
funding_output_index: self.context.channel_transaction_parameters.funding_outpoint.as_ref().unwrap().index,
8773+
funding_txid: self.funding.funding_outpoint.as_ref().unwrap().txid,
8774+
funding_output_index: self.funding.funding_outpoint.as_ref().unwrap().index,
87508775
signature,
87518776
#[cfg(taproot)]
87528777
partial_signature_with_nonce: None,
@@ -8775,6 +8800,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
87758800
}
87768801
self.context.assert_no_commitment_advancement(self.unfunded_context.transaction_number(), "funding_created");
87778802

8803+
self.funding.funding_outpoint = Some(funding_txo);
87788804
self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
87798805
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
87808806

@@ -9163,6 +9189,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
91639189
self.context.assert_no_commitment_advancement(holder_commitment_point.transaction_number(), "funding_created");
91649190

91659191
let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
9192+
self.funding.funding_outpoint = Some(funding_txo);
91669193
self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
91679194
// This is an externally observable change before we finish all our checks. In particular
91689195
// check_funding_created_signature may fail.
@@ -10452,6 +10479,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1045210479
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
1045310480
#[cfg(any(test, fuzzing))]
1045410481
next_remote_commitment_tx_fee_info_cached: Mutex::new(None),
10482+
10483+
funding_outpoint: channel_parameters.funding_outpoint,
1045510484
},
1045610485
context: ChannelContext {
1045710486
user_id,
@@ -11331,6 +11360,7 @@ mod tests {
1133111360
selected_contest_delay: 144
1133211361
});
1133311362
chan.context.channel_transaction_parameters.funding_outpoint = Some(funding_info);
11363+
chan.funding.funding_outpoint = Some(funding_info);
1133411364
signer.provide_channel_parameters(&chan.context.channel_transaction_parameters);
1133511365

1133611366
assert_eq!(counterparty_pubkeys.payment_point.serialize()[..],

lightning/src/ln/channel_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl ChannelDetails {
506506
},
507507
outbound_htlc_maximum_msat: context.get_counterparty_htlc_maximum_msat(funding),
508508
},
509-
funding_txo: context.get_funding_txo(),
509+
funding_txo: funding.get_funding_txo(),
510510
// Note that accept_channel (or open_channel) is always the first message, so
511511
// `have_received_message` indicates that type negotiation has completed.
512512
channel_type: if context.have_received_message() {

0 commit comments

Comments
 (0)