Skip to content

Commit 0eca1e5

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 39667e3 commit 0eca1e5

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
@@ -1950,6 +1950,7 @@ pub(super) struct FundingScope {
19501950
funding_transaction: Option<Transaction>,
19511951
/// The hash of the block in which the funding transaction was included.
19521952
funding_tx_confirmed_in: Option<BlockHash>,
1953+
funding_tx_confirmation_height: u32,
19531954
}
19541955

19551956
impl Writeable for FundingScope {
@@ -1961,6 +1962,7 @@ impl Writeable for FundingScope {
19611962
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
19621963
(9, self.funding_transaction, option),
19631964
(11, self.funding_tx_confirmed_in, option),
1965+
(13, self.funding_tx_confirmation_height, required),
19641966
});
19651967
Ok(())
19661968
}
@@ -1975,6 +1977,7 @@ impl Readable for FundingScope {
19751977
let mut channel_transaction_parameters = RequiredWrapper(None);
19761978
let mut funding_transaction = None;
19771979
let mut funding_tx_confirmed_in = None;
1980+
let mut funding_tx_confirmation_height = RequiredWrapper(None);
19781981

19791982
read_tlv_fields!(reader, {
19801983
(1, value_to_self_msat, required),
@@ -1983,6 +1986,7 @@ impl Readable for FundingScope {
19831986
(7, channel_transaction_parameters, (required: ReadableArgs, None)),
19841987
(9, funding_transaction, option),
19851988
(11, funding_tx_confirmed_in, option),
1989+
(13, funding_tx_confirmation_height, required),
19861990
});
19871991

19881992
Ok(Self {
@@ -1996,6 +2000,7 @@ impl Readable for FundingScope {
19962000
channel_transaction_parameters: channel_transaction_parameters.0.unwrap(),
19972001
funding_transaction,
19982002
funding_tx_confirmed_in,
2003+
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
19992004
#[cfg(any(test, fuzzing))]
20002005
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
20012006
#[cfg(any(test, fuzzing))]
@@ -2072,6 +2077,26 @@ impl FundingScope {
20722077
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
20732078
&self.channel_transaction_parameters.channel_type_features
20742079
}
2080+
2081+
/// Returns the height in which our funding transaction was confirmed.
2082+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
2083+
let conf_height = self.funding_tx_confirmation_height;
2084+
if conf_height > 0 {
2085+
Some(conf_height)
2086+
} else {
2087+
None
2088+
}
2089+
}
2090+
2091+
/// Returns the current number of confirmations on the funding transaction.
2092+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
2093+
if self.funding_tx_confirmation_height == 0 {
2094+
// We either haven't seen any confirmation yet, or observed a reorg.
2095+
return 0;
2096+
}
2097+
2098+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
2099+
}
20752100
}
20762101

20772102
/// Info about a pending splice, used in the pre-splice channel
@@ -2233,7 +2258,6 @@ where
22332258
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
22342259
expecting_peer_commitment_signed: bool,
22352260

2236-
funding_tx_confirmation_height: u32,
22372261
short_channel_id: Option<u64>,
22382262
/// Either the height at which this channel was created or the height at which it was last
22392263
/// serialized if it was serialized by versions prior to 0.0.103.
@@ -3077,6 +3101,7 @@ where
30773101
},
30783102
funding_transaction: None,
30793103
funding_tx_confirmed_in: None,
3104+
funding_tx_confirmation_height: 0,
30803105
};
30813106
let channel_context = ChannelContext {
30823107
user_id,
@@ -3140,7 +3165,6 @@ where
31403165
closing_fee_limits: None,
31413166
target_closing_feerate_sats_per_kw: None,
31423167

3143-
funding_tx_confirmation_height: 0,
31443168
short_channel_id: None,
31453169
channel_creation_height: current_chain_height,
31463170

@@ -3318,6 +3342,7 @@ where
33183342
},
33193343
funding_transaction: None,
33203344
funding_tx_confirmed_in: None,
3345+
funding_tx_confirmation_height: 0,
33213346
};
33223347
let channel_context = Self {
33233348
user_id,
@@ -3379,7 +3404,6 @@ where
33793404
closing_fee_limits: None,
33803405
target_closing_feerate_sats_per_kw: None,
33813406

3382-
funding_tx_confirmation_height: 0,
33833407
short_channel_id: None,
33843408
channel_creation_height: current_chain_height,
33853409

@@ -3637,16 +3661,6 @@ where
36373661
self.outbound_scid_alias = outbound_scid_alias;
36383662
}
36393663

3640-
/// Returns the height in which our funding transaction was confirmed.
3641-
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
3642-
let conf_height = self.funding_tx_confirmation_height;
3643-
if conf_height > 0 {
3644-
Some(conf_height)
3645-
} else {
3646-
None
3647-
}
3648-
}
3649-
36503664
/// Performs checks against necessary constraints after receiving either an `accept_channel` or
36513665
/// `accept_channel2` message.
36523666
#[rustfmt::skip]
@@ -3788,16 +3802,6 @@ where
37883802
Ok(())
37893803
}
37903804

3791-
/// Returns the current number of confirmations on the funding transaction.
3792-
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
3793-
if self.funding_tx_confirmation_height == 0 {
3794-
// We either haven't seen any confirmation yet, or observed a reorg.
3795-
return 0;
3796-
}
3797-
3798-
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
3799-
}
3800-
38013805
/// Allowed in any state (including after shutdown)
38023806
pub fn get_counterparty_node_id(&self) -> PublicKey {
38033807
self.counterparty_node_id
@@ -7331,7 +7335,7 @@ where
73317335
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
73327336
{
73337337
// Broadcast only if not yet confirmed
7334-
if self.context.get_funding_tx_confirmation_height().is_none() {
7338+
if self.funding.get_funding_tx_confirmation_height().is_none() {
73357339
funding_broadcastable = Some(funding_transaction.clone())
73367340
}
73377341
}
@@ -8727,13 +8731,13 @@ where
87278731
// Called:
87288732
// * always when a new block/transactions are confirmed with the new height
87298733
// * when funding is signed with a height of 0
8730-
if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
8734+
if self.funding.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
87318735
return None;
87328736
}
87338737

8734-
let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8738+
let funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
87358739
if funding_tx_confirmations <= 0 {
8736-
self.context.funding_tx_confirmation_height = 0;
8740+
self.funding.funding_tx_confirmation_height = 0;
87378741
}
87388742

87398743
if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
@@ -8753,7 +8757,7 @@ where
87538757
// We got a reorg but not enough to trigger a force close, just ignore.
87548758
false
87558759
} else {
8756-
if self.context.funding_tx_confirmation_height != 0 &&
8760+
if self.funding.funding_tx_confirmation_height != 0 &&
87578761
self.context.channel_state < ChannelState::ChannelReady(ChannelReadyFlags::new())
87588762
{
87598763
// We should never see a funding transaction on-chain until we've received
@@ -8823,7 +8827,7 @@ where
88238827
for &(index_in_block, tx) in txdata.iter() {
88248828
// Check if the transaction is the expected funding transaction, and if it is,
88258829
// check that it pays the right amount to the right script.
8826-
if self.context.funding_tx_confirmation_height == 0 {
8830+
if self.funding.funding_tx_confirmation_height == 0 {
88278831
if tx.compute_txid() == funding_txo.txid {
88288832
let txo_idx = funding_txo.index as usize;
88298833
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.funding.get_funding_redeemscript().to_p2wsh() ||
@@ -8853,7 +8857,8 @@ where
88538857
}
88548858
}
88558859
}
8856-
self.context.funding_tx_confirmation_height = height;
8860+
8861+
self.funding.funding_tx_confirmation_height = height;
88578862
self.funding.funding_tx_confirmed_in = Some(*block_hash);
88588863
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
88598864
Ok(scid) => Some(scid),
@@ -8949,8 +8954,8 @@ where
89498954

89508955
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
89518956
self.context.channel_state.is_our_channel_ready() {
8952-
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8953-
if self.context.funding_tx_confirmation_height == 0 {
8957+
let mut funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
8958+
if self.funding.funding_tx_confirmation_height == 0 {
89548959
// Note that check_get_channel_ready may reset funding_tx_confirmation_height to
89558960
// zero if it has been reorged out, however in either case, our state flags
89568961
// indicate we've already sent a channel_ready
@@ -8991,10 +8996,10 @@ where
89918996
/// before the channel has reached channel_ready and we can just wait for more blocks.
89928997
#[rustfmt::skip]
89938998
pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
8994-
if self.context.funding_tx_confirmation_height != 0 {
8999+
if self.funding.funding_tx_confirmation_height != 0 {
89959000
// We handle the funding disconnection by calling best_block_updated with a height one
89969001
// below where our funding was connected, implying a reorg back to conf_height - 1.
8997-
let reorg_height = self.context.funding_tx_confirmation_height - 1;
9002+
let reorg_height = self.funding.funding_tx_confirmation_height - 1;
89989003
// We use the time field to bump the current time we set on channel updates if its
89999004
// larger. If we don't know that time has moved forward, we can just set it to the last
90009005
// time we saw and it will be ignored.
@@ -9069,7 +9074,7 @@ where
90699074
NS::Target: NodeSigner,
90709075
L::Target: Logger
90719076
{
9072-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9077+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
90739078
return None;
90749079
}
90759080

@@ -9192,7 +9197,7 @@ where
91929197
}
91939198

91949199
self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
9195-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9200+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
91969201
return Err(ChannelError::Ignore(
91979202
"Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
91989203
}
@@ -9206,7 +9211,7 @@ where
92069211
pub fn get_signed_channel_announcement<NS: Deref>(
92079212
&self, node_signer: &NS, chain_hash: ChainHash, best_block_height: u32, user_config: &UserConfig
92089213
) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
9209-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
9214+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
92109215
return None;
92119216
}
92129217
let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -11453,7 +11458,7 @@ where
1145311458
0u8.write(writer)?;
1145411459

1145511460
self.funding.funding_tx_confirmed_in.write(writer)?;
11456-
self.context.funding_tx_confirmation_height.write(writer)?;
11461+
self.funding.funding_tx_confirmation_height.write(writer)?;
1145711462
self.context.short_channel_id.write(writer)?;
1145811463

1145911464
self.context.counterparty_dust_limit_satoshis.write(writer)?;
@@ -12112,6 +12117,7 @@ where
1211212117
channel_transaction_parameters: channel_parameters,
1211312118
funding_transaction,
1211412119
funding_tx_confirmed_in,
12120+
funding_tx_confirmation_height,
1211512121
},
1211612122
pending_funding: pending_funding.unwrap(),
1211712123
context: ChannelContext {
@@ -12175,7 +12181,6 @@ where
1217512181
closing_fee_limits: None,
1217612182
target_closing_feerate_sats_per_kw,
1217712183

12178-
funding_tx_confirmation_height,
1217912184
short_channel_id,
1218012185
channel_creation_height,
1218112186

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)