Skip to content

Commit a54159c

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 cf96b7d commit a54159c

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
@@ -9291,8 +9291,8 @@ where
92919291
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
92929292
/// back.
92939293
pub fn best_block_updated<NS: Deref, L: Deref>(
9294-
&mut self, height: u32, highest_header_time: u32, chain_hash: ChainHash, node_signer: &NS,
9295-
user_config: &UserConfig, logger: &L,
9294+
&mut self, height: u32, highest_header_time: Option<u32>, chain_hash: ChainHash,
9295+
node_signer: &NS, user_config: &UserConfig, logger: &L,
92969296
) -> Result<BestBlockUpdatedRes, ClosureReason>
92979297
where
92989298
NS::Target: NodeSigner,
@@ -9308,7 +9308,7 @@ where
93089308

93099309
#[rustfmt::skip]
93109310
fn do_best_block_updated<NS: Deref, L: Deref>(
9311-
&mut self, height: u32, highest_header_time: u32,
9311+
&mut self, height: u32, highest_header_time: Option<u32>,
93129312
chain_node_signer: Option<(ChainHash, &NS, &UserConfig)>, logger: &L
93139313
) -> Result<(Option<FundingConfirmedMessage>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>
93149314
where
@@ -9332,7 +9332,9 @@ where
93329332
}
93339333
});
93349334

9335-
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
9335+
if let Some(time) = highest_header_time {
9336+
self.context.update_time_counter = cmp::max(self.context.update_time_counter, time);
9337+
}
93369338

93379339
// Check if the funding transaction was unconfirmed
93389340
let funding_tx_confirmations = self.funding.get_funding_tx_confirmations(height);
@@ -9482,12 +9484,10 @@ where
94829484
// We handle the funding disconnection by calling best_block_updated with a height one
94839485
// below where our funding was connected, implying a reorg back to conf_height - 1.
94849486
let reorg_height = funding.funding_tx_confirmation_height - 1;
9485-
// We use the time field to bump the current time we set on channel updates if its
9486-
// larger. If we don't know that time has moved forward, we can just set it to the last
9487-
// time we saw and it will be ignored.
9488-
let best_time = self.context.update_time_counter;
94899487

9490-
match self.do_best_block_updated(reorg_height, best_time, None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>, logger) {
9488+
let signer_config = None::<(ChainHash, &&dyn NodeSigner, &UserConfig)>;
9489+
let res = self.do_best_block_updated(reorg_height, None, signer_config, logger);
9490+
match res {
94919491
Ok((channel_ready, timed_out_htlcs, announcement_sigs)) => {
94929492
assert!(channel_ready.is_none(), "We can't generate a funding with 0 confirmations?");
94939493
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
@@ -12035,7 +12035,7 @@ where
1203512035
self.do_chain_event(Some(new_best_block.height), |channel| {
1203612036
channel.best_block_updated(
1203712037
new_best_block.height,
12038-
0,
12038+
None,
1203912039
self.chain_hash,
1204012040
&self.node_signer,
1204112041
&self.default_configuration,
@@ -12085,7 +12085,17 @@ where
1208512085
let last_best_block_height = self.best_block.read().unwrap().height;
1208612086
if height < last_best_block_height {
1208712087
let timestamp = self.highest_seen_timestamp.load(Ordering::Acquire);
12088-
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)));
12088+
let do_update = |channel: &mut FundedChannel<SP>| {
12089+
channel.best_block_updated(
12090+
last_best_block_height,
12091+
Some(timestamp as u32),
12092+
self.chain_hash,
12093+
&self.node_signer,
12094+
&self.default_configuration,
12095+
&&WithChannelContext::from(&self.logger, &channel.context, None)
12096+
)
12097+
};
12098+
self.do_chain_event(Some(last_best_block_height), do_update);
1208912099
}
1209012100
}
1209112101

@@ -12145,7 +12155,14 @@ where
1214512155
}
1214612156
}
1214712157

12148-
channel.best_block_updated(height, header.time, self.chain_hash, &self.node_signer, &self.default_configuration, &&WithChannelContext::from(&self.logger, &channel.context, None))
12158+
channel.best_block_updated(
12159+
height,
12160+
Some(header.time),
12161+
self.chain_hash,
12162+
&self.node_signer,
12163+
&self.default_configuration,
12164+
&&WithChannelContext::from(&self.logger, &channel.context, None)
12165+
)
1214912166
});
1215012167

1215112168
macro_rules! max_time {

0 commit comments

Comments
 (0)