Skip to content

Commit d80770b

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 e3addaa commit d80770b

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

lightning/src/ln/channel.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,7 @@ pub(super) struct FundingScope {
19621962
funding_transaction: Option<Transaction>,
19631963
/// The hash of the block in which the funding transaction was included.
19641964
funding_tx_confirmed_in: Option<BlockHash>,
1965+
funding_tx_confirmation_height: u32,
19651966
}
19661967

19671968
impl Writeable for FundingScope {
@@ -1973,6 +1974,7 @@ impl Writeable for FundingScope {
19731974
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
19741975
(9, self.funding_transaction, option),
19751976
(11, self.funding_tx_confirmed_in, option),
1977+
(13, self.funding_tx_confirmation_height, required),
19761978
});
19771979
Ok(())
19781980
}
@@ -1987,6 +1989,7 @@ impl Readable for FundingScope {
19871989
let mut channel_transaction_parameters = RequiredWrapper(None);
19881990
let mut funding_transaction = None;
19891991
let mut funding_tx_confirmed_in = None;
1992+
let mut funding_tx_confirmation_height = RequiredWrapper(None);
19901993

19911994
read_tlv_fields!(reader, {
19921995
(1, value_to_self_msat, required),
@@ -1995,6 +1998,7 @@ impl Readable for FundingScope {
19951998
(7, channel_transaction_parameters, (required: ReadableArgs, None)),
19961999
(9, funding_transaction, option),
19972000
(11, funding_tx_confirmed_in, option),
2001+
(13, funding_tx_confirmation_height, required),
19982002
});
19992003

20002004
Ok(Self {
@@ -2008,6 +2012,7 @@ impl Readable for FundingScope {
20082012
channel_transaction_parameters: channel_transaction_parameters.0.unwrap(),
20092013
funding_transaction,
20102014
funding_tx_confirmed_in,
2015+
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
20112016
#[cfg(any(test, fuzzing))]
20122017
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
20132018
#[cfg(any(test, fuzzing))]
@@ -2084,6 +2089,26 @@ impl FundingScope {
20842089
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
20852090
&self.channel_transaction_parameters.channel_type_features
20862091
}
2092+
2093+
/// Returns the height in which our funding transaction was confirmed.
2094+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
2095+
let conf_height = self.funding_tx_confirmation_height;
2096+
if conf_height > 0 {
2097+
Some(conf_height)
2098+
} else {
2099+
None
2100+
}
2101+
}
2102+
2103+
/// Returns the current number of confirmations on the funding transaction.
2104+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
2105+
if self.funding_tx_confirmation_height == 0 {
2106+
// We either haven't seen any confirmation yet, or observed a reorg.
2107+
return 0;
2108+
}
2109+
2110+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
2111+
}
20872112
}
20882113

20892114
/// Info about a pending splice, used in the pre-splice channel
@@ -2245,7 +2270,6 @@ where
22452270
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
22462271
expecting_peer_commitment_signed: bool,
22472272

2248-
funding_tx_confirmation_height: u32,
22492273
short_channel_id: Option<u64>,
22502274
/// Either the height at which this channel was created or the height at which it was last
22512275
/// serialized if it was serialized by versions prior to 0.0.103.
@@ -3093,6 +3117,7 @@ where
30933117
},
30943118
funding_transaction: None,
30953119
funding_tx_confirmed_in: None,
3120+
funding_tx_confirmation_height: 0,
30963121
};
30973122
let channel_context = ChannelContext {
30983123
user_id,
@@ -3156,7 +3181,6 @@ where
31563181
closing_fee_limits: None,
31573182
target_closing_feerate_sats_per_kw: None,
31583183

3159-
funding_tx_confirmation_height: 0,
31603184
short_channel_id: None,
31613185
channel_creation_height: current_chain_height,
31623186

@@ -3334,6 +3358,7 @@ where
33343358
},
33353359
funding_transaction: None,
33363360
funding_tx_confirmed_in: None,
3361+
funding_tx_confirmation_height: 0,
33373362
};
33383363
let channel_context = Self {
33393364
user_id,
@@ -3395,7 +3420,6 @@ where
33953420
closing_fee_limits: None,
33963421
target_closing_feerate_sats_per_kw: None,
33973422

3398-
funding_tx_confirmation_height: 0,
33993423
short_channel_id: None,
34003424
channel_creation_height: current_chain_height,
34013425

@@ -3651,16 +3675,6 @@ where
36513675
self.outbound_scid_alias = outbound_scid_alias;
36523676
}
36533677

3654-
/// Returns the height in which our funding transaction was confirmed.
3655-
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
3656-
let conf_height = self.funding_tx_confirmation_height;
3657-
if conf_height > 0 {
3658-
Some(conf_height)
3659-
} else {
3660-
None
3661-
}
3662-
}
3663-
36643678
/// Performs checks against necessary constraints after receiving either an `accept_channel` or
36653679
/// `accept_channel2` message.
36663680
#[rustfmt::skip]
@@ -3802,16 +3816,6 @@ where
38023816
Ok(())
38033817
}
38043818

3805-
/// Returns the current number of confirmations on the funding transaction.
3806-
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
3807-
if self.funding_tx_confirmation_height == 0 {
3808-
// We either haven't seen any confirmation yet, or observed a reorg.
3809-
return 0;
3810-
}
3811-
3812-
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
3813-
}
3814-
38153819
/// Allowed in any state (including after shutdown)
38163820
pub fn get_counterparty_node_id(&self) -> PublicKey {
38173821
self.counterparty_node_id
@@ -7362,7 +7366,7 @@ where
73627366
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
73637367
{
73647368
// Broadcast only if not yet confirmed
7365-
if self.context.get_funding_tx_confirmation_height().is_none() {
7369+
if self.funding.get_funding_tx_confirmation_height().is_none() {
73667370
funding_broadcastable = Some(funding_transaction.clone())
73677371
}
73687372
}
@@ -8771,13 +8775,13 @@ where
87718775
// Called:
87728776
// * always when a new block/transactions are confirmed with the new height
87738777
// * when funding is signed with a height of 0
8774-
if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
8778+
if self.funding.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
87758779
return None;
87768780
}
87778781

8778-
let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8782+
let funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
87798783
if funding_tx_confirmations <= 0 {
8780-
self.context.funding_tx_confirmation_height = 0;
8784+
self.funding.funding_tx_confirmation_height = 0;
87818785
}
87828786

87838787
if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
@@ -8797,7 +8801,7 @@ where
87978801
// We got a reorg but not enough to trigger a force close, just ignore.
87988802
false
87998803
} else {
8800-
if self.context.funding_tx_confirmation_height != 0 &&
8804+
if self.funding.funding_tx_confirmation_height != 0 &&
88018805
self.context.channel_state < ChannelState::ChannelReady(ChannelReadyFlags::new())
88028806
{
88038807
// We should never see a funding transaction on-chain until we've received
@@ -8867,7 +8871,7 @@ where
88678871
for &(index_in_block, tx) in txdata.iter() {
88688872
// Check if the transaction is the expected funding transaction, and if it is,
88698873
// check that it pays the right amount to the right script.
8870-
if self.context.funding_tx_confirmation_height == 0 {
8874+
if self.funding.funding_tx_confirmation_height == 0 {
88718875
if tx.compute_txid() == funding_txo.txid {
88728876
let txo_idx = funding_txo.index as usize;
88738877
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.funding.get_funding_redeemscript().to_p2wsh() ||
@@ -8897,7 +8901,8 @@ where
88978901
}
88988902
}
88998903
}
8900-
self.context.funding_tx_confirmation_height = height;
8904+
8905+
self.funding.funding_tx_confirmation_height = height;
89018906
self.funding.funding_tx_confirmed_in = Some(*block_hash);
89028907
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
89038908
Ok(scid) => Some(scid),
@@ -8997,8 +9002,8 @@ where
89979002

89989003
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
89999004
self.context.channel_state.is_our_channel_ready() {
9000-
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
9001-
if self.context.funding_tx_confirmation_height == 0 {
9005+
let mut funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
9006+
if self.funding.funding_tx_confirmation_height == 0 {
90029007
// Note that check_get_channel_ready may reset funding_tx_confirmation_height to
90039008
// zero if it has been reorged out, however in either case, our state flags
90049009
// indicate we've already sent a channel_ready
@@ -9039,10 +9044,10 @@ where
90399044
/// before the channel has reached channel_ready and we can just wait for more blocks.
90409045
#[rustfmt::skip]
90419046
pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
9042-
if self.context.funding_tx_confirmation_height != 0 {
9047+
if self.funding.funding_tx_confirmation_height != 0 {
90439048
// We handle the funding disconnection by calling best_block_updated with a height one
90449049
// below where our funding was connected, implying a reorg back to conf_height - 1.
9045-
let reorg_height = self.context.funding_tx_confirmation_height - 1;
9050+
let reorg_height = self.funding.funding_tx_confirmation_height - 1;
90469051
// We use the time field to bump the current time we set on channel updates if its
90479052
// larger. If we don't know that time has moved forward, we can just set it to the last
90489053
// time we saw and it will be ignored.
@@ -9117,7 +9122,7 @@ where
91179122
NS::Target: NodeSigner,
91189123
L::Target: Logger
91199124
{
9120-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9125+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
91219126
return None;
91229127
}
91239128

@@ -9240,7 +9245,7 @@ where
92409245
}
92419246

92429247
self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
9243-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9248+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
92449249
return Err(ChannelError::Ignore(
92459250
"Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
92469251
}
@@ -9254,7 +9259,7 @@ where
92549259
pub fn get_signed_channel_announcement<NS: Deref>(
92559260
&self, node_signer: &NS, chain_hash: ChainHash, best_block_height: u32, user_config: &UserConfig
92569261
) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
9257-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9262+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
92589263
return None;
92599264
}
92609265
let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -11506,7 +11511,7 @@ where
1150611511
0u8.write(writer)?;
1150711512

1150811513
self.funding.funding_tx_confirmed_in.write(writer)?;
11509-
self.context.funding_tx_confirmation_height.write(writer)?;
11514+
self.funding.funding_tx_confirmation_height.write(writer)?;
1151011515
self.context.short_channel_id.write(writer)?;
1151111516

1151211517
self.context.counterparty_dust_limit_satoshis.write(writer)?;
@@ -12165,6 +12170,7 @@ where
1216512170
channel_transaction_parameters: channel_parameters,
1216612171
funding_transaction,
1216712172
funding_tx_confirmed_in,
12173+
funding_tx_confirmation_height,
1216812174
},
1216912175
pending_funding: pending_funding.unwrap(),
1217012176
context: ChannelContext {
@@ -12228,7 +12234,6 @@ where
1222812234
closing_fee_limits: None,
1222912235
target_closing_feerate_sats_per_kw,
1223012236

12231-
funding_tx_confirmation_height,
1223212237
short_channel_id,
1223312238
channel_creation_height,
1223412239

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)