From a0d4e968679d0945fde74e2aa6c14ccf06926809 Mon Sep 17 00:00:00 2001 From: Daniel Freire Date: Wed, 11 Dec 2024 16:19:17 -0300 Subject: [PATCH] enha: make finish_pending_block hold write logs for less time --- src/eth/storage/temporary/inmemory.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/eth/storage/temporary/inmemory.rs b/src/eth/storage/temporary/inmemory.rs index 015628eef..d07953b65 100644 --- a/src/eth/storage/temporary/inmemory.rs +++ b/src/eth/storage/temporary/inmemory.rs @@ -180,11 +180,27 @@ impl TemporaryStorage for InMemoryTemporaryStorage { fn finish_pending_block(&self) -> anyhow::Result { let mut pending_block = self.pending_block.write(); + let mut latest = self.latest_block.write(); + + let next_number = (*pending_block).block.header.number.next_block_number(); + *latest = Some(std::mem::replace(&mut *pending_block, InMemoryTemporaryStorageState::new(next_number))); + + drop(latest); + let latest = self.latest_block.read(); + drop(pending_block); #[cfg(feature = "dev")] - let mut finished_block = pending_block.block.clone(); + let mut finished_block = (*latest) + .as_ref() + .expect("latest should be Some after finishing the pending block") + .block + .clone(); #[cfg(not(feature = "dev"))] - let finished_block = pending_block.block.clone(); + let finished_block = (*latest) + .as_ref() + .expect("latest should be Some after finishing the pending block") + .block + .clone(); #[cfg(feature = "dev")] { @@ -195,12 +211,6 @@ impl TemporaryStorage for InMemoryTemporaryStorage { } } - let mut latest = self.latest_block.write(); - *latest = Some(std::mem::replace( - &mut *pending_block, - InMemoryTemporaryStorageState::new(finished_block.header.number.next_block_number()), - )); - Ok(finished_block) }