Skip to content

Commit dbb51a3

Browse files
committed
Don't pass a latest-block-time to Channel unless we have one
When calling `Channel::best_block_updated` we pass it the timestamp of the block we're connecting so that it can track the highest timestamp it has seen. However, in some cases, we don't actually have a timestamp to pass, which `Channel::best_block_updated` will happily ignore as it always takes the `max` of its existing value. Thus, we really should pass a `None` to ensure the API is understandable, which we do here.
1 parent 860639f commit dbb51a3

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10156,8 +10156,8 @@ where
1015610156
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
1015710157
/// back.
1015810158
pub fn best_block_updated<NS: Deref, L: Deref>(
10159-
&mut self, height: u32, highest_header_time: u32, chain_hash: ChainHash, node_signer: &NS,
10160-
user_config: &UserConfig, logger: &L,
10159+
&mut self, height: u32, highest_header_time: Option<u32>, chain_hash: ChainHash,
10160+
node_signer: &NS, user_config: &UserConfig, logger: &L,
1016110161
) -> Result<BestBlockUpdatedRes, ClosureReason>
1016210162
where
1016310163
NS::Target: NodeSigner,
@@ -10173,7 +10173,7 @@ where
1017310173

1017410174
#[rustfmt::skip]
1017510175
fn do_best_block_updated<NS: Deref, L: Deref>(
10176-
&mut self, height: u32, highest_header_time: u32,
10176+
&mut self, height: u32, highest_header_time: Option<u32>,
1017710177
chain_node_signer: Option<(ChainHash, &NS, &UserConfig)>, logger: &L
1017810178
) -> Result<(Option<FundingConfirmedMessage>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>
1017910179
where
@@ -10197,7 +10197,9 @@ where
1019710197
}
1019810198
});
1019910199

10200-
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
10200+
if let Some(time) = highest_header_time {
10201+
self.context.update_time_counter = cmp::max(self.context.update_time_counter, time);
10202+
}
1020110203

1020210204
// Check if the funding transaction was unconfirmed
1020310205
let funding_tx_confirmations = self.funding.get_funding_tx_confirmations(height);
@@ -10352,12 +10354,9 @@ where
1035210354
// We handle the funding disconnection by calling best_block_updated with a height one
1035310355
// below where our funding was connected, implying a reorg back to conf_height - 1.
1035410356
let reorg_height = funding.funding_tx_confirmation_height - 1;
10355-
// We use the time field to bump the current time we set on channel updates if its
10356-
// larger. If we don't know that time has moved forward, we can just set it to the last
10357-
// time we saw and it will be ignored.
10358-
let best_time = self.context.update_time_counter;
1035910357

10360-
match self.do_best_block_updated(reorg_height, best_time, None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>, logger) {
10358+
let signer_config = None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>;
10359+
match self.do_best_block_updated(reorg_height, None, signer_config, logger) {
1036110360
Ok((channel_ready, timed_out_htlcs, announcement_sigs)) => {
1036210361
assert!(channel_ready.is_none(), "We can't generate a funding with 0 confirmations?");
1036310362
assert!(timed_out_htlcs.is_empty(), "We can't have accepted HTLCs with a timeout before our funding confirmation?");

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13072,7 +13072,7 @@ where
1307213072
self.do_chain_event(Some(fork_point.height), |channel| {
1307313073
channel.best_block_updated(
1307413074
fork_point.height,
13075-
0,
13075+
None,
1307613076
self.chain_hash,
1307713077
&self.node_signer,
1307813078
&self.default_configuration,
@@ -13122,7 +13122,17 @@ where
1312213122
let last_best_block_height = self.best_block.read().unwrap().height;
1312313123
if height < last_best_block_height {
1312413124
let timestamp = self.highest_seen_timestamp.load(Ordering::Acquire);
13125-
self.do_chain_event(Some(last_best_block_height), |channel| channel.best_block_updated(last_best_block_height, timestamp as u32, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None)));
13125+
let do_update = |channel: &mut FundedChannel<SP>| {
13126+
channel.best_block_updated(
13127+
last_best_block_height,
13128+
Some(timestamp as u32),
13129+
self.chain_hash,
13130+
&self.node_signer,
13131+
&self.default_configuration,
13132+
&&WithChannelContext::from(&self.logger, &channel.context, None)
13133+
)
13134+
};
13135+
self.do_chain_event(Some(last_best_block_height), do_update);
1312613136
}
1312713137
}
1312813138

@@ -13182,7 +13192,14 @@ where
1318213192
}
1318313193
}
1318413194

13185-
channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None))
13195+
channel.best_block_updated(
13196+
height,
13197+
Some(header.time),
13198+
self.chain_hash,
13199+
&self.node_signer,
13200+
&self.default_configuration,
13201+
&&WithChannelContext::from(&self.logger, &channel.context, None)
13202+
)
1318613203
});
1318713204

1318813205
macro_rules! max_time {

0 commit comments

Comments
 (0)