Skip to content

Commit

Permalink
add configure for SharedBackend block_in_place or not
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Jul 25, 2024
1 parent 596d267 commit dff92e2
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ where
}
}

/// mode for the `SharedBackend` to block or not block when interacting with the `BackendHandler`
#[derive(Clone, Debug, PartialEq)]
pub enum BlockingMod {
BlockInPlace,
Block,
}

/// A cloneable backend type that shares access to the backend data with all its clones.
///
/// This backend type is connected to the `BackendHandler` via a mpsc channel. The `BackendHandler`
Expand Down Expand Up @@ -553,6 +560,9 @@ pub struct SharedBackend {
/// There is only one instance of the type, so as soon as the last `SharedBackend` is deleted,
/// `FlushJsonBlockCacheDB` is also deleted and the cache is flushed.
cache: Arc<FlushJsonBlockCacheDB>,

/// The mode for the `SharedBackend` to block in place or not
blocking_mod: BlockingMod,
}

impl SharedBackend {
Expand Down Expand Up @@ -623,7 +633,7 @@ impl SharedBackend {
let (backend, backend_rx) = channel(1);
let cache = Arc::new(FlushJsonBlockCacheDB(Arc::clone(db.cache())));
let handler = BackendHandler::new(provider, db, backend_rx, pin_block);
(Self { backend, cache }, handler)
(Self { backend, cache, blocking_mod: BlockingMod::BlockInPlace }, handler)
}

/// Updates the pinned block to fetch data from
Expand All @@ -634,49 +644,69 @@ impl SharedBackend {

/// Returns the full block for the given block identifier
pub fn get_full_block(&self, block: impl Into<BlockId>) -> DatabaseResult<Block> {
tokio::task::block_in_place(|| {
let process_req = {
let (sender, rx) = oneshot_channel();
let req = BackendRequest::FullBlock(block.into(), sender);
self.backend.clone().try_send(req)?;
rx.recv()?
})
};
match self.blocking_mod {
BlockingMod::BlockInPlace => tokio::task::block_in_place(|| process_req),
BlockingMod::Block => process_req,
}
}

/// Returns the transaction for the hash
pub fn get_transaction(&self, tx: B256) -> DatabaseResult<WithOtherFields<Transaction>> {
tokio::task::block_in_place(|| {
let process_req = {
let (sender, rx) = oneshot_channel();
let req = BackendRequest::Transaction(tx, sender);
self.backend.clone().try_send(req)?;
rx.recv()?
})
};
match self.blocking_mod {
BlockingMod::BlockInPlace => tokio::task::block_in_place(|| process_req),
BlockingMod::Block => process_req,
}
}

fn do_get_basic(&self, address: Address) -> DatabaseResult<Option<AccountInfo>> {
tokio::task::block_in_place(|| {
let process_req = {
let (sender, rx) = oneshot_channel();
let req = BackendRequest::Basic(address, sender);
self.backend.clone().try_send(req)?;
rx.recv()?.map(Some)
})
};
match self.blocking_mod {
BlockingMod::BlockInPlace => tokio::task::block_in_place(|| process_req),
BlockingMod::Block => process_req,
}
}

fn do_get_storage(&self, address: Address, index: U256) -> DatabaseResult<U256> {
tokio::task::block_in_place(|| {
let process_req = {
let (sender, rx) = oneshot_channel();
let req = BackendRequest::Storage(address, index, sender);
self.backend.clone().try_send(req)?;
rx.recv()?
})
};
match self.blocking_mod {
BlockingMod::BlockInPlace => tokio::task::block_in_place(|| process_req),
BlockingMod::Block => process_req,
}
}

fn do_get_block_hash(&self, number: u64) -> DatabaseResult<B256> {
tokio::task::block_in_place(|| {
let process_req = {
let (sender, rx) = oneshot_channel();
let req = BackendRequest::BlockHash(number, sender);
self.backend.clone().try_send(req)?;
rx.recv()?
})
};
match self.blocking_mod {
BlockingMod::BlockInPlace => tokio::task::block_in_place(|| process_req),
BlockingMod::Block => process_req,
}
}

/// Inserts or updates data for multiple addresses
Expand Down

0 comments on commit dff92e2

Please sign in to comment.