Skip to content

Commit d4ad337

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 3ba20a6 commit d4ad337

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
@@ -1823,6 +1823,7 @@ pub(super) struct FundingScope {
18231823
funding_transaction: Option<Transaction>,
18241824
/// The hash of the block in which the funding transaction was included.
18251825
funding_tx_confirmed_in: Option<BlockHash>,
1826+
funding_tx_confirmation_height: u32,
18261827
}
18271828

18281829
impl Writeable for FundingScope {
@@ -1834,6 +1835,7 @@ impl Writeable for FundingScope {
18341835
(7, self.channel_transaction_parameters, (required: ReadableArgs, None)),
18351836
(9, self.funding_transaction, option),
18361837
(11, self.funding_tx_confirmed_in, option),
1838+
(13, self.funding_tx_confirmation_height, required),
18371839
});
18381840
Ok(())
18391841
}
@@ -1847,6 +1849,7 @@ impl Readable for FundingScope {
18471849
let mut channel_transaction_parameters = RequiredWrapper(None);
18481850
let mut funding_transaction = None;
18491851
let mut funding_tx_confirmed_in = None;
1852+
let mut funding_tx_confirmation_height = RequiredWrapper(None);
18501853

18511854
read_tlv_fields!(reader, {
18521855
(1, value_to_self_msat, required),
@@ -1855,6 +1858,7 @@ impl Readable for FundingScope {
18551858
(7, channel_transaction_parameters, (required: ReadableArgs, None)),
18561859
(9, funding_transaction, option),
18571860
(11, funding_tx_confirmed_in, option),
1861+
(13, funding_tx_confirmation_height, required),
18581862
});
18591863

18601864
Ok(Self {
@@ -1868,6 +1872,7 @@ impl Readable for FundingScope {
18681872
channel_transaction_parameters: channel_transaction_parameters.0.unwrap(),
18691873
funding_transaction,
18701874
funding_tx_confirmed_in,
1875+
funding_tx_confirmation_height: funding_tx_confirmation_height.0.unwrap(),
18711876
#[cfg(any(test, fuzzing))]
18721877
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
18731878
#[cfg(any(test, fuzzing))]
@@ -1941,6 +1946,26 @@ impl FundingScope {
19411946
pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
19421947
&self.channel_transaction_parameters.channel_type_features
19431948
}
1949+
1950+
/// Returns the height in which our funding transaction was confirmed.
1951+
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
1952+
let conf_height = self.funding_tx_confirmation_height;
1953+
if conf_height > 0 {
1954+
Some(conf_height)
1955+
} else {
1956+
None
1957+
}
1958+
}
1959+
1960+
/// Returns the current number of confirmations on the funding transaction.
1961+
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
1962+
if self.funding_tx_confirmation_height == 0 {
1963+
// We either haven't seen any confirmation yet, or observed a reorg.
1964+
return 0;
1965+
}
1966+
1967+
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
1968+
}
19441969
}
19451970

19461971
/// Info about a pending splice, used in the pre-splice channel
@@ -2100,7 +2125,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
21002125
/// milliseconds, so any accidental force-closes here should be exceedingly rare.
21012126
expecting_peer_commitment_signed: bool,
21022127

2103-
funding_tx_confirmation_height: u32,
21042128
short_channel_id: Option<u64>,
21052129
/// Either the height at which this channel was created or the height at which it was last
21062130
/// serialized if it was serialized by versions prior to 0.0.103.
@@ -2915,6 +2939,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29152939
},
29162940
funding_transaction: None,
29172941
funding_tx_confirmed_in: None,
2942+
funding_tx_confirmation_height: 0,
29182943
};
29192944
let channel_context = ChannelContext {
29202945
user_id,
@@ -2978,7 +3003,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
29783003
closing_fee_limits: None,
29793004
target_closing_feerate_sats_per_kw: None,
29803005

2981-
funding_tx_confirmation_height: 0,
29823006
short_channel_id: None,
29833007
channel_creation_height: current_chain_height,
29843008

@@ -3155,6 +3179,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
31553179
},
31563180
funding_transaction: None,
31573181
funding_tx_confirmed_in: None,
3182+
funding_tx_confirmation_height: 0,
31583183
};
31593184
let channel_context = Self {
31603185
user_id,
@@ -3216,7 +3241,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32163241
closing_fee_limits: None,
32173242
target_closing_feerate_sats_per_kw: None,
32183243

3219-
funding_tx_confirmation_height: 0,
32203244
short_channel_id: None,
32213245
channel_creation_height: current_chain_height,
32223246

@@ -3467,16 +3491,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34673491
self.outbound_scid_alias = outbound_scid_alias;
34683492
}
34693493

3470-
/// Returns the height in which our funding transaction was confirmed.
3471-
pub fn get_funding_tx_confirmation_height(&self) -> Option<u32> {
3472-
let conf_height = self.funding_tx_confirmation_height;
3473-
if conf_height > 0 {
3474-
Some(conf_height)
3475-
} else {
3476-
None
3477-
}
3478-
}
3479-
34803494
/// Performs checks against necessary constraints after receiving either an `accept_channel` or
34813495
/// `accept_channel2` message.
34823496
pub fn do_accept_channel_checks(
@@ -3617,16 +3631,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36173631
Ok(())
36183632
}
36193633

3620-
/// Returns the current number of confirmations on the funding transaction.
3621-
pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
3622-
if self.funding_tx_confirmation_height == 0 {
3623-
// We either haven't seen any confirmation yet, or observed a reorg.
3624-
return 0;
3625-
}
3626-
3627-
height.checked_sub(self.funding_tx_confirmation_height).map_or(0, |c| c + 1)
3628-
}
3629-
36303634
/// Allowed in any state (including after shutdown)
36313635
pub fn get_counterparty_node_id(&self) -> PublicKey {
36323636
self.counterparty_node_id
@@ -7087,7 +7091,7 @@ impl<SP: Deref> FundedChannel<SP> where
70877091
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
70887092
{
70897093
// Broadcast only if not yet confirmed
7090-
if self.context.get_funding_tx_confirmation_height().is_none() {
7094+
if self.funding.get_funding_tx_confirmation_height().is_none() {
70917095
funding_broadcastable = Some(funding_transaction.clone())
70927096
}
70937097
}
@@ -8451,13 +8455,13 @@ impl<SP: Deref> FundedChannel<SP> where
84518455
// Called:
84528456
// * always when a new block/transactions are confirmed with the new height
84538457
// * when funding is signed with a height of 0
8454-
if self.context.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
8458+
if self.funding.funding_tx_confirmation_height == 0 && self.context.minimum_depth != Some(0) {
84558459
return None;
84568460
}
84578461

8458-
let funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8462+
let funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
84598463
if funding_tx_confirmations <= 0 {
8460-
self.context.funding_tx_confirmation_height = 0;
8464+
self.funding.funding_tx_confirmation_height = 0;
84618465
}
84628466

84638467
if funding_tx_confirmations < self.context.minimum_depth.unwrap_or(0) as i64 {
@@ -8477,7 +8481,7 @@ impl<SP: Deref> FundedChannel<SP> where
84778481
// We got a reorg but not enough to trigger a force close, just ignore.
84788482
false
84798483
} else {
8480-
if self.context.funding_tx_confirmation_height != 0 &&
8484+
if self.funding.funding_tx_confirmation_height != 0 &&
84818485
self.context.channel_state < ChannelState::ChannelReady(ChannelReadyFlags::new())
84828486
{
84838487
// We should never see a funding transaction on-chain until we've received
@@ -8545,7 +8549,7 @@ impl<SP: Deref> FundedChannel<SP> where
85458549
for &(index_in_block, tx) in txdata.iter() {
85468550
// Check if the transaction is the expected funding transaction, and if it is,
85478551
// check that it pays the right amount to the right script.
8548-
if self.context.funding_tx_confirmation_height == 0 {
8552+
if self.funding.funding_tx_confirmation_height == 0 {
85498553
if tx.compute_txid() == funding_txo.txid {
85508554
let txo_idx = funding_txo.index as usize;
85518555
if txo_idx >= tx.output.len() || tx.output[txo_idx].script_pubkey != self.funding.get_funding_redeemscript().to_p2wsh() ||
@@ -8575,7 +8579,8 @@ impl<SP: Deref> FundedChannel<SP> where
85758579
}
85768580
}
85778581
}
8578-
self.context.funding_tx_confirmation_height = height;
8582+
8583+
self.funding.funding_tx_confirmation_height = height;
85798584
self.funding.funding_tx_confirmed_in = Some(*block_hash);
85808585
self.context.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
85818586
Ok(scid) => Some(scid),
@@ -8669,8 +8674,8 @@ impl<SP: Deref> FundedChannel<SP> where
86698674

86708675
if matches!(self.context.channel_state, ChannelState::ChannelReady(_)) ||
86718676
self.context.channel_state.is_our_channel_ready() {
8672-
let mut funding_tx_confirmations = height as i64 - self.context.funding_tx_confirmation_height as i64 + 1;
8673-
if self.context.funding_tx_confirmation_height == 0 {
8677+
let mut funding_tx_confirmations = height as i64 - self.funding.funding_tx_confirmation_height as i64 + 1;
8678+
if self.funding.funding_tx_confirmation_height == 0 {
86748679
// Note that check_get_channel_ready may reset funding_tx_confirmation_height to
86758680
// zero if it has been reorged out, however in either case, our state flags
86768681
// indicate we've already sent a channel_ready
@@ -8710,10 +8715,10 @@ impl<SP: Deref> FundedChannel<SP> where
87108715
/// force-close the channel, but may also indicate a harmless reorganization of a block or two
87118716
/// before the channel has reached channel_ready and we can just wait for more blocks.
87128717
pub fn funding_transaction_unconfirmed<L: Deref>(&mut self, logger: &L) -> Result<(), ClosureReason> where L::Target: Logger {
8713-
if self.context.funding_tx_confirmation_height != 0 {
8718+
if self.funding.funding_tx_confirmation_height != 0 {
87148719
// We handle the funding disconnection by calling best_block_updated with a height one
87158720
// below where our funding was connected, implying a reorg back to conf_height - 1.
8716-
let reorg_height = self.context.funding_tx_confirmation_height - 1;
8721+
let reorg_height = self.funding.funding_tx_confirmation_height - 1;
87178722
// We use the time field to bump the current time we set on channel updates if its
87188723
// larger. If we don't know that time has moved forward, we can just set it to the last
87198724
// time we saw and it will be ignored.
@@ -8786,7 +8791,7 @@ impl<SP: Deref> FundedChannel<SP> where
87868791
NS::Target: NodeSigner,
87878792
L::Target: Logger
87888793
{
8789-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8794+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
87908795
return None;
87918796
}
87928797

@@ -8907,7 +8912,7 @@ impl<SP: Deref> FundedChannel<SP> where
89078912
}
89088913

89098914
self.context.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
8910-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8915+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
89118916
return Err(ChannelError::Ignore(
89128917
"Got announcement_signatures prior to the required six confirmations - we may not have received a block yet that our peer has".to_owned()));
89138918
}
@@ -8920,7 +8925,7 @@ impl<SP: Deref> FundedChannel<SP> where
89208925
pub fn get_signed_channel_announcement<NS: Deref>(
89218926
&self, node_signer: &NS, chain_hash: ChainHash, best_block_height: u32, user_config: &UserConfig
89228927
) -> Option<msgs::ChannelAnnouncement> where NS::Target: NodeSigner {
8923-
if self.context.funding_tx_confirmation_height == 0 || self.context.funding_tx_confirmation_height + 5 > best_block_height {
8928+
if self.funding.funding_tx_confirmation_height == 0 || self.funding.funding_tx_confirmation_height + 5 > best_block_height {
89248929
return None;
89258930
}
89268931
let announcement = match self.get_channel_announcement(node_signer, chain_hash, user_config) {
@@ -11103,7 +11108,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1110311108
0u8.write(writer)?;
1110411109

1110511110
self.funding.funding_tx_confirmed_in.write(writer)?;
11106-
self.context.funding_tx_confirmation_height.write(writer)?;
11111+
self.funding.funding_tx_confirmation_height.write(writer)?;
1110711112
self.context.short_channel_id.write(writer)?;
1110811113

1110911114
self.context.counterparty_dust_limit_satoshis.write(writer)?;
@@ -11760,6 +11765,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1176011765
channel_transaction_parameters: channel_parameters,
1176111766
funding_transaction,
1176211767
funding_tx_confirmed_in,
11768+
funding_tx_confirmation_height,
1176311769
},
1176411770
pending_funding: pending_funding.unwrap(),
1176511771
context: ChannelContext {
@@ -11823,7 +11829,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1182311829
closing_fee_limits: None,
1182411830
target_closing_feerate_sats_per_kw,
1182511831

11826-
funding_tx_confirmation_height,
1182711832
short_channel_id,
1182811833
channel_creation_height,
1182911834

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)