Skip to content

Commit

Permalink
upgrade and downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw committed Dec 11, 2024
1 parent 8e67371 commit 903e936
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/eth/storage/temporary/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::collections::HashMap;

use parking_lot::lock_api::RwLockUpgradableReadGuard;
use parking_lot::RwLock;

use crate::eth::executor::EvmInput;
Expand Down Expand Up @@ -179,25 +180,23 @@ impl TemporaryStorage for InMemoryTemporaryStorage {
}

fn finish_pending_block(&self) -> anyhow::Result<PendingBlock> {
let next_state = {
let pending_block = self.pending_block.read();
InMemoryTemporaryStorageState::new(pending_block.block.header.number.next_block_number())
};
let pending_block = self.pending_block.upgradable_read();

let mut pending_block = self.pending_block.write();
let mut latest = self.latest_block.write();
let next_state = InMemoryTemporaryStorageState::new(pending_block.block.header.number.next_block_number());

let mut pending_block = RwLockUpgradableReadGuard::<'_, parking_lot::RawRwLock, InMemoryTemporaryStorageState>::upgrade(pending_block);
let mut latest = self.latest_block.write();
*latest = Some(std::mem::replace(&mut *pending_block, next_state));

drop(latest);
let latest = self.latest_block.read();
drop(pending_block);
let latest = parking_lot::lock_api::RwLockWriteGuard::<'_, parking_lot::RawRwLock, std::option::Option<InMemoryTemporaryStorageState>>::downgrade(latest);

#[cfg(feature = "dev")]
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 = latest.as_ref().expect("latest should be Some after finishing the pending block").block.clone();

drop(latest);
#[cfg(feature = "dev")]
{
// Update block timestamp only if evm_setNextBlockTimestamp was called,
Expand Down

0 comments on commit 903e936

Please sign in to comment.