From 6e062629359a5de37e29a25c05c40f4d7b365ef6 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 18 Dec 2024 10:12:53 +0100 Subject: [PATCH] Update `best_block` field in `Confirm::best_block_updated` Previously, we wouldn't set the field as we aren't yet making use of it. Here, we start setting the field. To this end, we make `best_block` an `RwLock>` rather than `Option>`. --- lightning-liquidity/src/manager.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lightning-liquidity/src/manager.rs b/lightning-liquidity/src/manager.rs index 06587ba632c..1e467c302de 100644 --- a/lightning-liquidity/src/manager.rs +++ b/lightning-liquidity/src/manager.rs @@ -107,7 +107,7 @@ where lsps2_client_handler: Option>, service_config: Option, _client_config: Option, - best_block: Option>, + best_block: RwLock>, _chain_source: Option, } @@ -215,7 +215,7 @@ where { lsps2_service_handler, service_config, _client_config: client_config, - best_block: chain_params.map(|chain_params| RwLock::new(chain_params.best_block)), + best_block: RwLock::new(chain_params.map(|chain_params| chain_params.best_block)), _chain_source: chain_source, } } @@ -642,8 +642,7 @@ where &self, header: &bitcoin::block::Header, txdata: &chain::transaction::TransactionData, height: u32, ) { - if let Some(best_block) = &self.best_block { - let best_block = best_block.read().unwrap(); + if let Some(best_block) = self.best_block.read().unwrap().as_ref() { assert_eq!(best_block.block_hash, header.prev_blockhash, "Blocks must be connected in chain-order - the connected header must build on the last connected header"); assert_eq!(best_block.height, height - 1, @@ -656,8 +655,7 @@ where fn block_disconnected(&self, header: &bitcoin::block::Header, height: u32) { let new_height = height - 1; - if let Some(best_block) = &self.best_block { - let mut best_block = best_block.write().unwrap(); + if let Some(best_block) = self.best_block.write().unwrap().as_mut() { assert_eq!(best_block.block_hash, header.block_hash(), "Blocks must be disconnected in chain-order - the disconnected header must be the last connected header"); assert_eq!(best_block.height, height, @@ -690,7 +688,10 @@ where // confirmed at a height <= the one we now unconfirmed. } - fn best_block_updated(&self, _header: &bitcoin::block::Header, _height: u32) { + fn best_block_updated(&self, header: &bitcoin::block::Header, height: u32) { + let new_best_block = BestBlock::new(header.block_hash(), height); + *self.best_block.write().unwrap() = Some(new_best_block); + // TODO: Call best_block_updated on all sub-modules that require it, e.g., LSPS1MessageHandler. if let Some(lsps2_service_handler) = self.lsps2_service_handler.as_ref() { lsps2_service_handler.prune_peer_state();