Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: spawn backendhandler on background thread #1704

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,13 @@ Chain ID: {}

let block_chain_db = BlockchainDb::new(meta, self.block_cache_path());

// This will spawn the background service that will use the provider to fetch blockchain
// This will spawn the background thread that will use the provider to fetch blockchain
// data from the other client
let backend = SharedBackend::spawn_backend(
let backend = SharedBackend::spawn_backend_thread(
Arc::clone(&provider),
block_chain_db.clone(),
Some(fork_block_number.into()),
)
.await;
);

let db = Arc::new(RwLock::new(ForkedDatabase::new(backend, block_chain_db)));
let fork = ClientFork::new(
Expand Down
38 changes: 34 additions & 4 deletions evm/src/executor/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ pub struct SharedBackend {
}

impl SharedBackend {
/// _Spawns_ a new `BackendHandler` on a background thread that listens for requests from any
/// _Spawns_ a new `BackendHandler` on a `tokio::task` that listens for requests from any
/// `SharedBackend`. Missing values get inserted in the `db`.
///
/// The spawned `BackendHandler` is dropped once the last `SharedBackend` connected to it is
/// The spawned `BackendHandler` finishes once the last `SharedBackend` connected to it is
/// dropped.
///
/// NOTE: this should be called with `Arc<Provider>`
Expand All @@ -398,12 +398,42 @@ impl SharedBackend {
M: Middleware + Unpin + 'static + Clone,
{
let (shared, handler) = Self::new(provider, db, pin_block);
// spawn the provider handler to background
trace!(target: "backendhandler", "spawning Backendhandler");
// spawn the provider handler to a task
trace!(target: "backendhandler", "spawning Backendhandler task");
tokio::spawn(handler);
shared
}

/// Same as `Self::spawn_backend` but spawns the `BackendHandler` on a separate `std::thread` in
/// its own `tokio::Runtime`
pub fn spawn_backend_thread<M>(
provider: M,
db: BlockchainDb,
pin_block: Option<BlockId>,
) -> Self
where
M: Middleware + Unpin + 'static + Clone,
{
let (shared, handler) = Self::new(provider, db, pin_block);

// spawn a light-weight thread with a thread-local async runtime just for
// sending and receiving data from the remote client
let _ = std::thread::Builder::new()
.name("fork-backend-thread".to_string())
.spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.expect("failed to create fork-backend-thread tokio runtime");

rt.block_on(async move { handler.await });
})
.expect("failed to spawn backendhandler thread");
trace!(target: "backendhandler", "spawned Backendhandler thread");

shared
}

/// Returns a new `SharedBackend` and the `BackendHandler`
pub fn new<M>(
provider: M,
Expand Down