Skip to content

Commit 3969264

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 cee02cc commit 3969264

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,7 @@ pub(super) struct FundingScope {
18181818
funding_transaction: Option<Transaction>,
18191819
/// The hash of the block in which the funding transaction was included.
18201820
funding_tx_confirmed_in: Option<BlockHash>,
1821+
funding_tx_confirmation_height: u32,
18211822
}
18221823

18231824
impl Writeable for FundingScope {
@@ -1829,6 +1830,7 @@ impl Writeable for FundingScope {
18291830
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
18301831
(9, self.funding_transaction, option),
18311832
(11, self.funding_tx_confirmed_in, option),
1833+
(13, self.funding_tx_confirmation_height, required),
18321834
});
18331835
Ok(())
18341836
}
@@ -1842,6 +1844,7 @@ impl Readable for FundingScope {
18421844
let mut channel_transaction_parameters = RequiredWrapper(None);
18431845
let mut funding_transaction = None;
18441846
let mut funding_tx_confirmed_in = None;
1847+
let mut funding_tx_confirmation_height = RequiredWrapper(None);
18451848

18461849
read_tlv_fields!(reader, {
18471850
(1, value_to_self_msat, required),
@@ -1850,6 +1853,7 @@ impl Readable for FundingScope {
18501853
(7, channel_transaction_parameters, (required: ReadableArgs, None)),
18511854
(9, funding_transaction, option),
18521855
(11, funding_tx_confirmed_in, option),
1856+
(13, funding_tx_confirmation_height, required),
18531857
});
18541858

18551859
Ok(Self {
@@ -1863,6 +1867,7 @@ impl Readable for FundingScope {
18631867
channel_transaction_parameters: channel_transaction_parameters.0.unwrap(),
18641868
funding_transaction,
18651869
funding_tx_confirmed_in,
1870+
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
18661871
#[cfg(any(test, fuzzing))]
18671872
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
18681873
#[cfg(any(test, fuzzing))]
@@ -1936,6 +1941,26 @@ impl FundingScope {
19361941
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
19371942
&self.channel_transaction_parameters.channel_type_features
19381943
}
1944+
1945+
/// Returns the height in which our funding transaction was confirmed.
1946+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
1947+
let conf_height = self.funding_tx_confirmation_height;
1948+
if conf_height > 0 {
1949+
Some(conf_height)
1950+
} else {
1951+
None
1952+
}
1953+
}
1954+
1955+
/// Returns the current number of confirmations on the funding transaction.
1956+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
1957+
if self.funding_tx_confirmation_height == 0 {
1958+
// We either haven't seen any confirmation yet, or observed a reorg.
1959+
return 0;
1960+
}
1961+
1962+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
1963+
}
19391964
}
19401965

19411966
/// Info about a pending splice, used in the pre-splice channel
@@ -2095,7 +2120,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
20952120
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
20962121
expecting_peer_commitment_signed: bool,
20972122

2098-
funding_tx_confirmation_height: u32,
20992123
short_channel_id: Option<u64>,
21002124
/// Either the height at which this channel was created or the height at which it was last
21012125
/// serialized if it was serialized by versions prior to 0.0.103.
@@ -2910,6 +2934,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29102934
},
29112935
funding_transaction: None,
29122936
funding_tx_confirmed_in: None,
2937+
funding_tx_confirmation_height: 0,
29132938
};
29142939
let channel_context = ChannelContext {
29152940
user_id,
@@ -2973,7 +2998,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29732998
closing_fee_limits: None,
29742999
target_closing_feerate_sats_per_kw: None,
29753000

2976-
funding_tx_confirmation_height: 0,
29773001
short_channel_id: None,
29783002
channel_creation_height: current_chain_height,
29793003

@@ -3144,6 +3168,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
31443168
},
31453169
funding_transaction: None,
31463170
funding_tx_confirmed_in: None,
3171+
funding_tx_confirmation_height: 0,
31473172
};
31483173
let channel_context = Self {
31493174
user_id,
@@ -3205,7 +3230,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32053230
closing_fee_limits: None,
32063231
target_closing_feerate_sats_per_kw: None,
32073232

3208-
funding_tx_confirmation_height: 0,
32093233
short_channel_id: None,
32103234
channel_creation_height: current_chain_height,
32113235

@@ -3456,16 +3480,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34563480
self.outbound_scid_alias = outbound_scid_alias;
34573481
}
34583482

3459-
/// Returns the height in which our funding transaction was confirmed.
3460-
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
3461-
let conf_height = self.funding_tx_confirmation_height;
3462-
if conf_height > 0 {
3463-
Some(conf_height)
3464-
} else {
3465-
None
3466-
}
3467-
}
3468-
34693483
/// Performs checks against necessary constraints after receiving either an `accept_channel` or
34703484
/// `accept_channel2` message.
34713485
pub fn do_accept_channel_checks(
@@ -3606,16 +3620,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36063620
Ok(())
36073621
}
36083622

3609-
/// Returns the current number of confirmations on the funding transaction.
3610-
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
3611-
if self.funding_tx_confirmation_height == 0 {
3612-
// We either haven't seen any confirmation yet, or observed a reorg.
3613-
return 0;
3614-
}
3615-
3616-
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
3617-
}
3618-
36193623
/// Allowed in any state (including after shutdown)
36203624
pub fn get_counterparty_node_id(&self) -> PublicKey {
36213625
self.counterparty_node_id
@@ -6902,7 +6906,7 @@ impl<SP: Deref> FundedChannel<SP> where
69026906
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
69036907
{
69046908
// Broadcast only if not yet confirmed
6905-
if self.context.get_funding_tx_confirmation_height().is_none() {
6909+
if self.funding.get_funding_tx_confirmation_height().is_none() {
69066910
funding_broadcastable = Some(funding_transaction.clone())
69076911
}
69086912
}
@@ -8332,13 +8336,13 @@ impl<SP: Deref> FundedChannel<SP> where
83328336
// Called:
83338337
// * always when a new block/transactions are confirmed with the new height
83348338
// * when funding is signed with a height of 0
8335-
if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
8339+
if self.funding.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
83368340
return None;
83378341
}
83388342

8339-
let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8343+
let funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
83408344
if funding_tx_confirmations <= 0 {
8341-
self.context.funding_tx_confirmation_height = 0;
8345+
self.funding.funding_tx_confirmation_height = 0;
83428346
}
83438347

83448348
if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
@@ -8358,7 +8362,7 @@ impl<SP: Deref> FundedChannel<SP> where
83588362
// We got a reorg but not enough to trigger a force close, just ignore.
83598363
false
83608364
} else {
8361-
if self.context.funding_tx_confirmation_height != 0 &&
8365+
if self.funding.funding_tx_confirmation_height != 0 &&
83628366
self.context.channel_state < ChannelState::ChannelReady(ChannelReadyFlags::new())
83638367
{
83648368
// We should never see a funding transaction on-chain until we've received
@@ -8426,7 +8430,7 @@ impl<SP: Deref> FundedChannel<SP> where
84268430
for &(index_in_block, tx) in txdata.iter() {
84278431
// Check if the transaction is the expected funding transaction, and if it is,
84288432
// check that it pays the right amount to the right script.
8429-
if self.context.funding_tx_confirmation_height == 0 {
8433+
if self.funding.funding_tx_confirmation_height == 0 {
84308434
if tx.compute_txid() == funding_txo.txid {
84318435
let txo_idx = funding_txo.index as usize;
84328436
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.funding.get_funding_redeemscript().to_p2wsh() ||
@@ -8456,7 +8460,8 @@ impl<SP: Deref> FundedChannel<SP> where
84568460
}
84578461
}
84588462
}
8459-
self.context.funding_tx_confirmation_height = height;
8463+
8464+
self.funding.funding_tx_confirmation_height = height;
84608465
self.funding.funding_tx_confirmed_in = Some(*block_hash);
84618466
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
84628467
Ok(scid) => Some(scid),
@@ -8550,8 +8555,8 @@ impl<SP: Deref> FundedChannel<SP> where
85508555

85518556
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
85528557
self.context.channel_state.is_our_channel_ready() {
8553-
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8554-
if self.context.funding_tx_confirmation_height == 0 {
8558+
let mut funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
8559+
if self.funding.funding_tx_confirmation_height == 0 {
85558560
// Note that check_get_channel_ready may reset funding_tx_confirmation_height to
85568561
// zero if it has been reorged out, however in either case, our state flags
85578562
// indicate we've already sent a channel_ready
@@ -8591,10 +8596,10 @@ impl<SP: Deref> FundedChannel<SP> where
85918596
/// force-close the channel, but may also indicate a harmless reorganization of a block or two
85928597
/// before the channel has reached channel_ready and we can just wait for more blocks.
85938598
pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
8594-
if self.context.funding_tx_confirmation_height != 0 {
8599+
if self.funding.funding_tx_confirmation_height != 0 {
85958600
// We handle the funding disconnection by calling best_block_updated with a height one
85968601
// below where our funding was connected, implying a reorg back to conf_height - 1.
8597-
let reorg_height = self.context.funding_tx_confirmation_height - 1;
8602+
let reorg_height = self.funding.funding_tx_confirmation_height - 1;
85988603
// We use the time field to bump the current time we set on channel updates if its
85998604
// larger. If we don't know that time has moved forward, we can just set it to the last
86008605
// time we saw and it will be ignored.
@@ -8667,7 +8672,7 @@ impl<SP: Deref> FundedChannel<SP> where
86678672
NS::Target: NodeSigner,
86688673
L::Target: Logger
86698674
{
8670-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8675+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
86718676
return None;
86728677
}
86738678

@@ -8788,7 +8793,7 @@ impl<SP: Deref> FundedChannel<SP> where
87888793
}
87898794

87908795
self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
8791-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8796+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
87928797
return Err(ChannelError::Ignore(
87938798
"Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
87948799
}
@@ -8801,7 +8806,7 @@ impl<SP: Deref> FundedChannel<SP> where
88018806
pub fn get_signed_channel_announcement<NS: Deref>(
88028807
&self, node_signer: &NS, chain_hash: ChainHash, best_block_height: u32, user_config: &UserConfig
88038808
) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
8804-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8809+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
88058810
return None;
88068811
}
88078812
let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -10976,7 +10981,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1097610981
0u8.write(writer)?;
1097710982

1097810983
self.funding.funding_tx_confirmed_in.write(writer)?;
10979-
self.context.funding_tx_confirmation_height.write(writer)?;
10984+
self.funding.funding_tx_confirmation_height.write(writer)?;
1098010985
self.context.short_channel_id.write(writer)?;
1098110986

1098210987
self.context.counterparty_dust_limit_satoshis.write(writer)?;
@@ -11633,6 +11638,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1163311638
channel_transaction_parameters: channel_parameters,
1163411639
funding_transaction,
1163511640
funding_tx_confirmed_in,
11641+
funding_tx_confirmation_height,
1163611642
},
1163711643
pending_funding: pending_funding.unwrap(),
1163811644
context: ChannelContext {
@@ -11696,7 +11702,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1169611702
closing_fee_limits: None,
1169711703
target_closing_feerate_sats_per_kw,
1169811704

11699-
funding_tx_confirmation_height,
1170011705
short_channel_id,
1170111706
channel_creation_height,
1170211707

lightning/src/ln/channel_state.rs

Lines changed: 1 addition & 1 deletion
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)