Skip to content

Commit c0f5571

Browse files
committed
Move ChannelContext::funding_tx_confirmation_height to FundingScope
When processing confirmed transactions, if the funding transaction is found then information about it in the ChannelContext is updated. In preparation for splicing, move this data to FundingScope.
1 parent dc72eab commit c0f5571

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

lightning/src/ln/channel.rs

+43-39
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,7 @@ pub(super) struct FundingScope {
16871687
funding_transaction: Option<Transaction>,
16881688
/// The hash of the block in which the funding transaction was included.
16891689
funding_tx_confirmed_in: Option<BlockHash>,
1690+
funding_tx_confirmation_height: u32,
16901691
}
16911692

16921693
impl Writeable for FundingScope {
@@ -1698,6 +1699,7 @@ impl Writeable for FundingScope {
16981699
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
16991700
(9, self.funding_transaction, option),
17001701
(11, self.funding_tx_confirmed_in, option),
1702+
(13, self.funding_tx_confirmation_height, required),
17011703
});
17021704
Ok(())
17031705
}
@@ -1711,6 +1713,7 @@ impl Readable for FundingScope {
17111713
let mut channel_transaction_parameters = RequiredWrapper(None);
17121714
let mut funding_transaction = None;
17131715
let mut funding_tx_confirmed_in = None;
1716+
let mut funding_tx_confirmation_height = RequiredWrapper(None);
17141717

17151718
read_tlv_fields!(reader, {
17161719
(1, value_to_self_msat, required),
@@ -1719,6 +1722,7 @@ impl Readable for FundingScope {
17191722
(7, channel_transaction_parameters, (required: ReadableArgs, None)),
17201723
(9, funding_transaction, option),
17211724
(11, funding_tx_confirmed_in, option),
1725+
(13, funding_tx_confirmation_height, required),
17221726
});
17231727

17241728
Ok(Self {
@@ -1732,6 +1736,7 @@ impl Readable for FundingScope {
17321736
channel_transaction_parameters: channel_transaction_parameters.0.unwrap(),
17331737
funding_transaction,
17341738
funding_tx_confirmed_in,
1739+
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
17351740
#[cfg(any(test, fuzzing))]
17361741
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
17371742
#[cfg(any(test, fuzzing))]
@@ -1810,6 +1815,26 @@ impl FundingScope {
18101815
pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
18111816
self.funding_tx_confirmed_in
18121817
}
1818+
1819+
/// Returns the height in which our funding transaction was confirmed.
1820+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
1821+
let conf_height = self.funding_tx_confirmation_height;
1822+
if conf_height > 0 {
1823+
Some(conf_height)
1824+
} else {
1825+
None
1826+
}
1827+
}
1828+
1829+
/// Returns the current number of confirmations on the funding transaction.
1830+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
1831+
if self.funding_tx_confirmation_height == 0 {
1832+
// We either haven't seen any confirmation yet, or observed a reorg.
1833+
return 0;
1834+
}
1835+
1836+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
1837+
}
18131838
}
18141839

18151840
/// Info about a pending splice, used in the pre-splice channel
@@ -1969,7 +1994,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
19691994
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
19701995
expecting_peer_commitment_signed: bool,
19711996

1972-
funding_tx_confirmation_height: u32,
19731997
short_channel_id: Option<u64>,
19741998
/// Either the height at which this channel was created or the height at which it was last
19751999
/// serialized if it was serialized by versions prior to 0.0.103.
@@ -2780,6 +2804,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27802804
},
27812805
funding_transaction: None,
27822806
funding_tx_confirmed_in: None,
2807+
funding_tx_confirmation_height: 0,
27832808
};
27842809
let channel_context = ChannelContext {
27852810
user_id,
@@ -2843,7 +2868,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
28432868
closing_fee_limits: None,
28442869
target_closing_feerate_sats_per_kw: None,
28452870

2846-
funding_tx_confirmation_height: 0,
28472871
short_channel_id: None,
28482872
channel_creation_height: current_chain_height,
28492873

@@ -3016,6 +3040,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30163040
},
30173041
funding_transaction: None,
30183042
funding_tx_confirmed_in: None,
3043+
funding_tx_confirmation_height: 0,
30193044
};
30203045
let channel_context = Self {
30213046
user_id,
@@ -3077,7 +3102,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30773102
closing_fee_limits: None,
30783103
target_closing_feerate_sats_per_kw: None,
30793104

3080-
funding_tx_confirmation_height: 0,
30813105
short_channel_id: None,
30823106
channel_creation_height: current_chain_height,
30833107

@@ -3319,16 +3343,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
33193343
self.outbound_scid_alias = outbound_scid_alias;
33203344
}
33213345

3322-
/// Returns the height in which our funding transaction was confirmed.
3323-
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
3324-
let conf_height = self.funding_tx_confirmation_height;
3325-
if conf_height > 0 {
3326-
Some(conf_height)
3327-
} else {
3328-
None
3329-
}
3330-
}
3331-
33323346
/// Performs checks against necessary constraints after receiving either an `accept_channel` or
33333347
/// `accept_channel2` message.
33343348
pub fn do_accept_channel_checks(
@@ -3469,16 +3483,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34693483
Ok(())
34703484
}
34713485

3472-
/// Returns the current number of confirmations on the funding transaction.
3473-
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
3474-
if self.funding_tx_confirmation_height == 0 {
3475-
// We either haven't seen any confirmation yet, or observed a reorg.
3476-
return 0;
3477-
}
3478-
3479-
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
3480-
}
3481-
34823486
/// Allowed in any state (including after shutdown)
34833487
pub fn get_counterparty_node_id(&self) -> PublicKey {
34843488
self.counterparty_node_id
@@ -4887,7 +4891,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48874891
NS::Target: NodeSigner,
48884892
L::Target: Logger
48894893
{
4890-
if self.funding_tx_confirmation_height == 0 || self.funding_tx_confirmation_height + 5 > best_block_height {
4894+
if funding.funding_tx_confirmation_height == 0 || funding.funding_tx_confirmation_height + 5 > best_block_height {
48914895
return None;
48924896
}
48934897

@@ -6843,7 +6847,7 @@ impl<SP: Deref> FundedChannel<SP> where
68436847
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
68446848
{
68456849
// Broadcast only if not yet confirmed
6846-
if self.context.get_funding_tx_confirmation_height().is_none() {
6850+
if self.funding.get_funding_tx_confirmation_height().is_none() {
68476851
funding_broadcastable = Some(funding_transaction.clone())
68486852
}
68496853
}
@@ -8179,13 +8183,13 @@ impl<SP: Deref> FundedChannel<SP> where
81798183
// Called:
81808184
// * always when a new block/transactions are confirmed with the new height
81818185
// * when funding is signed with a height of 0
8182-
if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
8186+
if self.funding.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
81838187
return None;
81848188
}
81858189

8186-
let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8190+
let funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
81878191
if funding_tx_confirmations <= 0 {
8188-
self.context.funding_tx_confirmation_height = 0;
8192+
self.funding.funding_tx_confirmation_height = 0;
81898193
}
81908194

81918195
if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
@@ -8205,7 +8209,7 @@ impl<SP: Deref> FundedChannel<SP> where
82058209
// We got a reorg but not enough to trigger a force close, just ignore.
82068210
false
82078211
} else {
8208-
if self.context.funding_tx_confirmation_height != 0 &&
8212+
if self.funding.funding_tx_confirmation_height != 0 &&
82098213
self.context.channel_state < ChannelState::ChannelReady(ChannelReadyFlags::new())
82108214
{
82118215
// We should never see a funding transaction on-chain until we've received
@@ -8277,7 +8281,7 @@ impl<SP: Deref> FundedChannel<SP> where
82778281
for &(index_in_block, tx) in txdata.iter() {
82788282
// Check if the transaction is the expected funding transaction, and if it is,
82798283
// check that it pays the right amount to the right script.
8280-
if self.context.funding_tx_confirmation_height == 0 {
8284+
if self.funding.funding_tx_confirmation_height == 0 {
82818285
if tx.compute_txid() == funding_txo.txid {
82828286
let txo_idx = funding_txo.index as usize;
82838287
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.funding.get_funding_redeemscript().to_p2wsh() ||
@@ -8308,7 +8312,7 @@ impl<SP: Deref> FundedChannel<SP> where
83088312
}
83098313
}
83108314

8311-
self.context.funding_tx_confirmation_height = height;
8315+
self.funding.funding_tx_confirmation_height = height;
83128316
self.funding.funding_tx_confirmed_in = Some(*block_hash);
83138317
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
83148318
Ok(scid) => Some(scid),
@@ -8409,8 +8413,8 @@ impl<SP: Deref> FundedChannel<SP> where
84098413

84108414
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
84118415
self.context.channel_state.is_our_channel_ready() {
8412-
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8413-
if self.context.funding_tx_confirmation_height == 0 {
8416+
let mut funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
8417+
if self.funding.funding_tx_confirmation_height == 0 {
84148418
// Note that check_get_channel_ready may reset funding_tx_confirmation_height to
84158419
// zero if it has been reorged out, however in either case, our state flags
84168420
// indicate we've already sent a channel_ready
@@ -8450,10 +8454,10 @@ impl<SP: Deref> FundedChannel<SP> where
84508454
/// force-close the channel, but may also indicate a harmless reorganization of a block or two
84518455
/// before the channel has reached channel_ready and we can just wait for more blocks.
84528456
pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
8453-
if self.context.funding_tx_confirmation_height != 0 {
8457+
if self.funding.funding_tx_confirmation_height != 0 {
84548458
// We handle the funding disconnection by calling best_block_updated with a height one
84558459
// below where our funding was connected, implying a reorg back to conf_height - 1.
8456-
let reorg_height = self.context.funding_tx_confirmation_height - 1;
8460+
let reorg_height = self.funding.funding_tx_confirmation_height - 1;
84578461
// We use the time field to bump the current time we set on channel updates if its
84588462
// larger. If we don't know that time has moved forward, we can just set it to the last
84598463
// time we saw and it will be ignored.
@@ -8562,7 +8566,7 @@ impl<SP: Deref> FundedChannel<SP> where
85628566
}
85638567

85648568
self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
8565-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8569+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
85668570
return Err(ChannelError::Ignore(
85678571
"Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
85688572
}
@@ -8575,7 +8579,7 @@ impl<SP: Deref> FundedChannel<SP> where
85758579
pub fn get_signed_channel_announcement<NS: Deref>(
85768580
&self, node_signer: &NS, chain_hash: ChainHash, best_block_height: u32, user_config: &UserConfig
85778581
) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
8578-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8582+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
85798583
return None;
85808584
}
85818585
let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -10725,7 +10729,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1072510729
0u8.write(writer)?;
1072610730

1072710731
self.funding.funding_tx_confirmed_in.write(writer)?;
10728-
self.context.funding_tx_confirmation_height.write(writer)?;
10732+
self.funding.funding_tx_confirmation_height.write(writer)?;
1072910733
self.context.short_channel_id.write(writer)?;
1073010734

1073110735
self.context.counterparty_dust_limit_satoshis.write(writer)?;
@@ -11361,6 +11365,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1136111365
channel_transaction_parameters: channel_parameters,
1136211366
funding_transaction,
1136311367
funding_tx_confirmed_in,
11368+
funding_tx_confirmation_height,
1136411369
},
1136511370
pending_funding: pending_funding.unwrap(),
1136611371
context: ChannelContext {
@@ -11424,7 +11429,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1142411429
closing_fee_limits: None,
1142511430
target_closing_feerate_sats_per_kw,
1142611431

11427-
funding_tx_confirmation_height,
1142811432
short_channel_id,
1142911433
channel_creation_height,
1143011434

lightning/src/ln/channel_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl ChannelDetails {
532532
next_outbound_htlc_minimum_msat: balance.next_outbound_htlc_minimum_msat,
533533
user_channel_id: context.get_user_id(),
534534
confirmations_required: context.minimum_depth(),
535-
confirmations: Some(context.get_funding_tx_confirmations(best_block_height)),
535+
confirmations: Some(funding.get_funding_tx_confirmations(best_block_height)),
536536
force_close_spend_delay: funding.get_counterparty_selected_contest_delay(),
537537
is_outbound: funding.is_outbound(),
538538
is_channel_ready: context.is_usable(),

0 commit comments

Comments
 (0)