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

chore(engine): simplify StateRootTask creation and hook management #13213

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
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
31 changes: 18 additions & 13 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! State root task related functionality.
use alloy_primitives::map::{HashMap, HashSet};
use reth_evm::system_calls::OnStateHook;
use reth_provider::{
providers::ConsistentDbView, BlockReader, DBProvider, DatabaseProviderFactory,
StateCommitmentProvider,
Expand All @@ -20,7 +21,7 @@ use std::{
collections::BTreeMap,
ops::Deref,
sync::{
mpsc::{self, Receiver, Sender},
mpsc::{self, channel, Receiver, Sender},
Arc,
},
time::{Duration, Instant},
Expand Down Expand Up @@ -249,11 +250,9 @@ where
+ 'static,
{
/// Creates a new state root task with the unified message channel
pub(crate) fn new(
config: StateRootConfig<Factory>,
tx: Sender<StateRootMessage>,
rx: Receiver<StateRootMessage>,
) -> Self {
pub(crate) fn new(config: StateRootConfig<Factory>) -> Self {
let (tx, rx) = channel();

Self {
config,
rx,
Expand All @@ -279,6 +278,15 @@ where
StateRootHandle::new(rx)
}

/// Returns a state hook to be used to send state updates to this task.
pub(crate) fn state_hook(&self) -> impl OnStateHook {
let state_hook = StateHookSender::new(self.tx.clone());

move |state: &EvmState| {
let _ = state_hook.send(StateRootMessage::StateUpdate(state.clone()));
}
}

/// Handles state updates.
///
/// Returns proof targets derived from the state update.
Expand Down Expand Up @@ -670,7 +678,6 @@ mod tests {
reth_tracing::init_test_tracing();

let factory = create_test_provider_factory();
let (tx, rx) = std::sync::mpsc::channel();

let state_updates = create_mock_state_updates(10, 10);
let mut hashed_state = HashedPostState::default();
Expand Down Expand Up @@ -721,16 +728,14 @@ mod tests {
consistent_view: ConsistentDbView::new(factory, None),
input: Arc::new(TrieInput::from_state(hashed_state)),
};
let task = StateRootTask::new(config, tx.clone(), rx);
let task = StateRootTask::new(config);
let mut state_hook = task.state_hook();
let handle = task.spawn();

let state_hook_sender = StateHookSender::new(tx);
for update in state_updates {
state_hook_sender
.send(StateRootMessage::StateUpdate(update))
.expect("failed to send state");
state_hook.on_state(&update);
}
drop(state_hook_sender);
drop(state_hook);

let (root_from_task, _) = handle.wait_for_result().expect("task failed");
let root_from_base = state_root(accumulated_state);
Expand Down
Loading