Skip to content

Commit 65b4f68

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 b645cd3 commit 65b4f68

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9667,8 +9667,8 @@ where
96679667
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
96689668
/// back.
96699669
pub fn best_block_updated<NS: Deref, L: Deref>(
9670-
&mut self, height: u32, highest_header_time: u32, chain_hash: ChainHash, node_signer: &NS,
9671-
user_config: &UserConfig, logger: &L,
9670+
&mut self, height: u32, highest_header_time: Option<u32>, chain_hash: ChainHash,
9671+
node_signer: &NS, user_config: &UserConfig, logger: &L,
96729672
) -> Result<BestBlockUpdatedRes, ClosureReason>
96739673
where
96749674
NS::Target: NodeSigner,
@@ -9684,7 +9684,7 @@ where
96849684

96859685
#[rustfmt::skip]
96869686
fn do_best_block_updated<NS: Deref, L: Deref>(
9687-
&mut self, height: u32, highest_header_time: u32,
9687+
&mut self, height: u32, highest_header_time: Option<u32>,
96889688
chain_node_signer: Option<(ChainHash, &NS, &UserConfig)>, logger: &L
96899689
) -> Result<(Option<FundingConfirmedMessage>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>
96909690
where
@@ -9708,7 +9708,9 @@ where
97089708
}
97099709
});
97109710

9711-
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
9711+
if let Some(time) = highest_header_time {
9712+
self.context.update_time_counter = cmp::max(self.context.update_time_counter, time);
9713+
}
97129714

97139715
// Check if the funding transaction was unconfirmed
97149716
let funding_tx_confirmations = self.funding.get_funding_tx_confirmations(height);
@@ -9864,12 +9866,10 @@ where
98649866
// We handle the funding disconnection by calling best_block_updated with a height one
98659867
// below where our funding was connected, implying a reorg back to conf_height - 1.
98669868
let reorg_height = funding.funding_tx_confirmation_height - 1;
9867-
// We use the time field to bump the current time we set on channel updates if its
9868-
// larger. If we don't know that time has moved forward, we can just set it to the last
9869-
// time we saw and it will be ignored.
9870-
let best_time = self.context.update_time_counter;
98719869

9872-
match self.do_best_block_updated(reorg_height, best_time, None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>, logger) {
9870+
let signer_config = None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>;
9871+
let res = self.do_best_block_updated(reorg_height, None, signer_config, logger);
9872+
match res {
98739873
Ok((channel_ready, timed_out_htlcs, announcement_sigs)) => {
98749874
assert!(channel_ready.is_none(), "We can't generate a funding with 0 confirmations?");
98759875
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
@@ -12557,7 +12557,7 @@ where
1255712557
self.do_chain_event(Some(new_best_block.height), |channel| {
1255812558
channel.best_block_updated(
1255912559
new_best_block.height,
12560-
0,
12560+
None,
1256112561
self.chain_hash,
1256212562
&self.node_signer,
1256312563
&self.default_configuration,
@@ -12607,7 +12607,17 @@ where
1260712607
let last_best_block_height = self.best_block.read().unwrap().height;
1260812608
if height < last_best_block_height {
1260912609
let timestamp = self.highest_seen_timestamp.load(Ordering::Acquire);
12610-
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)));
12610+
let do_update = |channel: &mut FundedChannel<SP>| {
12611+
channel.best_block_updated(
12612+
last_best_block_height,
12613+
Some(timestamp as u32),
12614+
self.chain_hash,
12615+
&self.node_signer,
12616+
&self.default_configuration,
12617+
&&WithChannelContext::from(&self.logger, &channel.context, None)
12618+
)
12619+
};
12620+
self.do_chain_event(Some(last_best_block_height), do_update);
1261112621
}
1261212622
}
1261312623

@@ -12667,7 +12677,14 @@ where
1266712677
}
1266812678
}
1266912679

12670-
channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None))
12680+
channel.best_block_updated(
12681+
height,
12682+
Some(header.time),
12683+
self.chain_hash,
12684+
&self.node_signer,
12685+
&self.default_configuration,
12686+
&&WithChannelContext::from(&self.logger, &channel.context, None)
12687+
)
1267112688
});
1267212689

1267312690
macro_rules! max_time {

0 commit comments

Comments
 (0)