Skip to content

Commit aa3dbe8

Browse files
authored
Merge pull request #2923 from tnull/2024-03-improve-best-block
Refactor `BestBlock` to expose inner fields
2 parents 9bac73b + edb22e1 commit aa3dbe8

File tree

7 files changed

+79
-83
lines changed

7 files changed

+79
-83
lines changed

lightning-background-processor/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1342,8 +1342,8 @@ mod tests {
13421342

13431343
fn confirm_transaction_depth(node: &mut Node, tx: &Transaction, depth: u32) {
13441344
for i in 1..=depth {
1345-
let prev_blockhash = node.best_block.block_hash();
1346-
let height = node.best_block.height() + 1;
1345+
let prev_blockhash = node.best_block.block_hash;
1346+
let height = node.best_block.height + 1;
13471347
let header = create_dummy_header(prev_blockhash, height);
13481348
let txdata = vec![(0, tx)];
13491349
node.best_block = BestBlock::new(header.block_hash(), height);

lightning/src/chain/channelmonitor.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl OnchainEventEntry {
387387
}
388388

389389
fn has_reached_confirmation_threshold(&self, best_block: &BestBlock) -> bool {
390-
best_block.height() >= self.confirmation_threshold()
390+
best_block.height >= self.confirmation_threshold()
391391
}
392392
}
393393

@@ -1077,8 +1077,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
10771077
event.write(writer)?;
10781078
}
10791079

1080-
self.best_block.block_hash().write(writer)?;
1081-
writer.write_all(&self.best_block.height().to_be_bytes())?;
1080+
self.best_block.block_hash.write(writer)?;
1081+
writer.write_all(&self.best_block.height.to_be_bytes())?;
10821082

10831083
writer.write_all(&(self.onchain_events_awaiting_threshold_conf.len() as u64).to_be_bytes())?;
10841084
for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
@@ -2273,7 +2273,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
22732273
// before considering it "no longer pending" - this matches when we
22742274
// provide the ChannelManager an HTLC failure event.
22752275
Some(commitment_tx_output_idx) == htlc.transaction_output_index &&
2276-
us.best_block.height() >= event.height + ANTI_REORG_DELAY - 1
2276+
us.best_block.height >= event.height + ANTI_REORG_DELAY - 1
22772277
} else if let OnchainEvent::HTLCSpendConfirmation { commitment_tx_output_idx, .. } = event.event {
22782278
// If the HTLC was fulfilled with a preimage, we consider the HTLC
22792279
// immediately non-pending, matching when we provide ChannelManager
@@ -2674,7 +2674,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26742674
macro_rules! claim_htlcs {
26752675
($commitment_number: expr, $txid: expr) => {
26762676
let (htlc_claim_reqs, _) = self.get_counterparty_output_claim_info($commitment_number, $txid, None);
2677-
self.onchain_tx_handler.update_claims_view_from_requests(htlc_claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
2677+
self.onchain_tx_handler.update_claims_view_from_requests(htlc_claim_reqs, self.best_block.height, self.best_block.height, broadcaster, fee_estimator, logger);
26782678
}
26792679
}
26802680
if let Some(txid) = self.current_counterparty_commitment_txid {
@@ -2721,8 +2721,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27212721
// Assume that the broadcasted commitment transaction confirmed in the current best
27222722
// block. Even if not, its a reasonable metric for the bump criteria on the HTLC
27232723
// transactions.
2724-
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&holder_commitment_tx, self.best_block.height());
2725-
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
2724+
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&holder_commitment_tx, self.best_block.height);
2725+
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height, self.best_block.height, broadcaster, fee_estimator, logger);
27262726
}
27272727
}
27282728
}
@@ -2736,7 +2736,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27362736
let commitment_package = PackageTemplate::build_package(
27372737
self.funding_info.0.txid.clone(), self.funding_info.0.index as u32,
27382738
PackageSolvingData::HolderFundingOutput(funding_outp),
2739-
self.best_block.height(), self.best_block.height()
2739+
self.best_block.height, self.best_block.height
27402740
);
27412741
let mut claimable_outpoints = vec![commitment_package];
27422742
self.pending_monitor_events.push(MonitorEvent::HolderForceClosed(self.funding_info.0));
@@ -2753,7 +2753,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27532753
// assuming it gets confirmed in the next block. Sadly, we have code which considers
27542754
// "not yet confirmed" things as discardable, so we cannot do that here.
27552755
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(
2756-
&self.current_holder_commitment_tx, self.best_block.height()
2756+
&self.current_holder_commitment_tx, self.best_block.height
27572757
);
27582758
let unsigned_commitment_tx = self.onchain_tx_handler.get_unsigned_holder_commitment_tx();
27592759
let new_outputs = self.get_broadcasted_holder_watch_outputs(
@@ -2777,7 +2777,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27772777
{
27782778
let (claimable_outpoints, _) = self.generate_claimable_outpoints_and_watch_outputs();
27792779
self.onchain_tx_handler.update_claims_view_from_requests(
2780-
claimable_outpoints, self.best_block.height(), self.best_block.height(), broadcaster,
2780+
claimable_outpoints, self.best_block.height, self.best_block.height, broadcaster,
27812781
fee_estimator, logger
27822782
);
27832783
}
@@ -3593,11 +3593,11 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35933593
{
35943594
let block_hash = header.block_hash();
35953595

3596-
if height > self.best_block.height() {
3596+
if height > self.best_block.height {
35973597
self.best_block = BestBlock::new(block_hash, height);
35983598
log_trace!(logger, "Connecting new block {} at height {}", block_hash, height);
35993599
self.block_confirmed(height, block_hash, vec![], vec![], vec![], &broadcaster, &fee_estimator, logger)
3600-
} else if block_hash != self.best_block.block_hash() {
3600+
} else if block_hash != self.best_block.block_hash {
36013601
self.best_block = BestBlock::new(block_hash, height);
36023602
log_trace!(logger, "Best block re-orged, replaced with new block {} at height {}", block_hash, height);
36033603
self.onchain_events_awaiting_threshold_conf.retain(|ref entry| entry.height <= height);
@@ -3742,7 +3742,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
37423742
}
37433743
}
37443744

3745-
if height > self.best_block.height() {
3745+
if height > self.best_block.height {
37463746
self.best_block = BestBlock::new(block_hash, height);
37473747
}
37483748

@@ -3774,7 +3774,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
37743774
L::Target: Logger,
37753775
{
37763776
log_trace!(logger, "Processing {} matched transactions for block at height {}.", txn_matched.len(), conf_height);
3777-
debug_assert!(self.best_block.height() >= conf_height);
3777+
debug_assert!(self.best_block.height >= conf_height);
37783778

37793779
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
37803780
if should_broadcast {
@@ -3865,8 +3865,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38653865
}
38663866
}
38673867

3868-
self.onchain_tx_handler.update_claims_view_from_requests(claimable_outpoints, conf_height, self.best_block.height(), broadcaster, fee_estimator, logger);
3869-
self.onchain_tx_handler.update_claims_view_from_matched_txn(&txn_matched, conf_height, conf_hash, self.best_block.height(), broadcaster, fee_estimator, logger);
3868+
self.onchain_tx_handler.update_claims_view_from_requests(claimable_outpoints, conf_height, self.best_block.height, broadcaster, fee_estimator, logger);
3869+
self.onchain_tx_handler.update_claims_view_from_matched_txn(&txn_matched, conf_height, conf_hash, self.best_block.height, broadcaster, fee_estimator, logger);
38703870

38713871
// Determine new outputs to watch by comparing against previously known outputs to watch,
38723872
// updating the latter in the process.
@@ -4017,7 +4017,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40174017
// to the source, and if we don't fail the channel we will have to ensure that the next
40184018
// updates that peer sends us are update_fails, failing the channel if not. It's probably
40194019
// easier to just fail the channel as this case should be rare enough anyway.
4020-
let height = self.best_block.height();
4020+
let height = self.best_block.height;
40214021
macro_rules! scan_commitment {
40224022
($htlcs: expr, $holder_tx: expr) => {
40234023
for ref htlc in $htlcs {
@@ -4616,7 +4616,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
46164616
chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point).to_v0_p2wsh();
46174617
}
46184618

4619-
Ok((best_block.block_hash(), ChannelMonitor::from_impl(ChannelMonitorImpl {
4619+
Ok((best_block.block_hash, ChannelMonitor::from_impl(ChannelMonitorImpl {
46204620
latest_update_id,
46214621
commitment_transaction_number_obscure_factor,
46224622

lightning/src/chain/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ pub(crate) mod onchaintx;
3131
pub(crate) mod package;
3232

3333
/// The best known block as identified by its hash and height.
34-
#[derive(Clone, Copy, PartialEq, Eq)]
34+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
3535
pub struct BestBlock {
36-
block_hash: BlockHash,
37-
height: u32,
36+
/// The block's hash
37+
pub block_hash: BlockHash,
38+
/// The height at which the block was confirmed.
39+
pub height: u32,
3840
}
3941

4042
impl BestBlock {
@@ -51,12 +53,6 @@ impl BestBlock {
5153
pub fn new(block_hash: BlockHash, height: u32) -> Self {
5254
BestBlock { block_hash, height }
5355
}
54-
55-
/// Returns the best block hash.
56-
pub fn block_hash(&self) -> BlockHash { self.block_hash }
57-
58-
/// Returns the best block height.
59-
pub fn height(&self) -> u32 { self.height }
6056
}
6157

6258

lightning/src/ln/channel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4086,7 +4086,7 @@ impl<SP: Deref> Channel<SP> where
40864086

40874087
log_info!(logger, "Received channel_ready from peer for channel {}", &self.context.channel_id());
40884088

4089-
Ok(self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height(), logger))
4089+
Ok(self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height, logger))
40904090
}
40914091

40924092
pub fn update_add_htlc<F, FE: Deref, L: Deref>(
@@ -5475,7 +5475,7 @@ impl<SP: Deref> Channel<SP> where
54755475

54765476
let shutdown_msg = self.get_outbound_shutdown();
54775477

5478-
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height(), logger);
5478+
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height, logger);
54795479

54805480
if matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
54815481
// If we're waiting on a monitor update, we shouldn't re-send any channel_ready's.

0 commit comments

Comments
 (0)