Skip to content

Commit 6e06262

Browse files
committed
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<Option<BestBlock>>` rather than `Option<RwLock<BestBlock>>`.
1 parent 8825fc3 commit 6e06262

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lightning-liquidity/src/manager.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ where
107107
lsps2_client_handler: Option<LSPS2ClientHandler<ES>>,
108108
service_config: Option<LiquidityServiceConfig>,
109109
_client_config: Option<LiquidityClientConfig>,
110-
best_block: Option<RwLock<BestBlock>>,
110+
best_block: RwLock<Option<BestBlock>>,
111111
_chain_source: Option<C>,
112112
}
113113

@@ -215,7 +215,7 @@ where {
215215
lsps2_service_handler,
216216
service_config,
217217
_client_config: client_config,
218-
best_block: chain_params.map(|chain_params| RwLock::new(chain_params.best_block)),
218+
best_block: RwLock::new(chain_params.map(|chain_params| chain_params.best_block)),
219219
_chain_source: chain_source,
220220
}
221221
}
@@ -642,8 +642,7 @@ where
642642
&self, header: &bitcoin::block::Header, txdata: &chain::transaction::TransactionData,
643643
height: u32,
644644
) {
645-
if let Some(best_block) = &self.best_block {
646-
let best_block = best_block.read().unwrap();
645+
if let Some(best_block) = self.best_block.read().unwrap().as_ref() {
647646
assert_eq!(best_block.block_hash, header.prev_blockhash,
648647
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
649648
assert_eq!(best_block.height, height - 1,
@@ -656,8 +655,7 @@ where
656655

657656
fn block_disconnected(&self, header: &bitcoin::block::Header, height: u32) {
658657
let new_height = height - 1;
659-
if let Some(best_block) = &self.best_block {
660-
let mut best_block = best_block.write().unwrap();
658+
if let Some(best_block) = self.best_block.write().unwrap().as_mut() {
661659
assert_eq!(best_block.block_hash, header.block_hash(),
662660
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header");
663661
assert_eq!(best_block.height, height,
@@ -690,7 +688,10 @@ where
690688
// confirmed at a height <= the one we now unconfirmed.
691689
}
692690

693-
fn best_block_updated(&self, _header: &bitcoin::block::Header, _height: u32) {
691+
fn best_block_updated(&self, header: &bitcoin::block::Header, height: u32) {
692+
let new_best_block = BestBlock::new(header.block_hash(), height);
693+
*self.best_block.write().unwrap() = Some(new_best_block);
694+
694695
// TODO: Call best_block_updated on all sub-modules that require it, e.g., LSPS1MessageHandler.
695696
if let Some(lsps2_service_handler) = self.lsps2_service_handler.as_ref() {
696697
lsps2_service_handler.prune_peer_state();

0 commit comments

Comments
 (0)