Skip to content

Commit

Permalink
chore: remove some unnecessary async/await (#7289)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Mar 1, 2024
1 parent 9fff5c2 commit e78b947
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 108 deletions.
8 changes: 2 additions & 6 deletions crates/cast/bin/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ impl CallArgs {
let (env, fork, chain) =
TracingExecutor::get_fork_material(&config, evm_opts).await?;

let mut executor =
foundry_evm::executors::TracingExecutor::new(env, fork, evm_version, debug)
.await;
let mut executor = TracingExecutor::new(env, fork, evm_version, debug);

let trace = match executor.deploy(
sender,
Expand Down Expand Up @@ -175,9 +173,7 @@ impl CallArgs {
let (env, fork, chain) =
TracingExecutor::get_fork_material(&config, evm_opts).await?;

let mut executor =
foundry_evm::executors::TracingExecutor::new(env, fork, evm_version, debug)
.await;
let mut executor = TracingExecutor::new(env, fork, evm_version, debug);

let (tx, _) = builder.build();

Expand Down
3 changes: 1 addition & 2 deletions crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ impl RunArgs {

let (mut env, fork, chain) = TracingExecutor::get_fork_material(&config, evm_opts).await?;

let mut executor =
TracingExecutor::new(env.clone(), fork, self.evm_version, self.debug).await;
let mut executor = TracingExecutor::new(env.clone(), fork, self.evm_version, self.debug);

env.block.number = U256::from(tx_block_number);

Expand Down
6 changes: 2 additions & 4 deletions crates/chisel/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,8 @@ impl SessionSource {
let backend = match self.config.backend.take() {
Some(backend) => backend,
None => {
let backend = Backend::spawn(
self.config.evm_opts.get_fork(&self.config.foundry_config, env.clone()),
)
.await;
let fork = self.config.evm_opts.get_fork(&self.config.foundry_config, env.clone());
let backend = Backend::spawn(fork);
self.config.backend = Some(backend.clone());
backend
}
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{
///
/// This is merely a wrapper for [`Project::compile()`] which also prints to stdout depending on its
/// settings.
#[must_use = "this builder does nothing unless you call a `compile*` method"]
#[must_use = "ProjectCompiler does nothing unless you call a `compile*` method"]
pub struct ProjectCompiler {
/// Whether we are going to verify the contracts after compilation.
verify: Option<bool>,
Expand Down
16 changes: 5 additions & 11 deletions crates/evm/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ pub struct Backend {

impl Backend {
/// Creates a new Backend with a spawned multi fork thread.
pub async fn spawn(fork: Option<CreateFork>) -> Self {
Self::new(MultiFork::spawn().await, fork)
pub fn spawn(fork: Option<CreateFork>) -> Self {
Self::new(MultiFork::spawn(), fork)
}

/// Creates a new instance of `Backend`
Expand Down Expand Up @@ -449,16 +449,11 @@ impl Backend {

/// Creates a new instance of `Backend` with fork added to the fork database and sets the fork
/// as active
pub(crate) async fn new_with_fork(
id: &ForkId,
fork: Fork,
journaled_state: JournaledState,
) -> Self {
let mut backend = Self::spawn(None).await;
pub(crate) fn new_with_fork(id: &ForkId, fork: Fork, journaled_state: JournaledState) -> Self {
let mut backend = Self::spawn(None);
let fork_ids = backend.inner.insert_new_fork(id.clone(), fork.db, journaled_state);
backend.inner.launched_with_fork = Some((id.clone(), fork_ids.0, fork_ids.1));
backend.active_fork_ids = Some(fork_ids);

backend
}

Expand Down Expand Up @@ -1885,8 +1880,7 @@ fn commit_transaction<I: Inspector<Backend>>(

let fork = fork.clone();
let journaled_state = journaled_state.clone();
let db = crate::utils::RuntimeOrHandle::new()
.block_on(async move { Backend::new_with_fork(fork_id, fork, journaled_state).await });
let db = Backend::new_with_fork(fork_id, fork, journaled_state);
evm.database(db);

match evm.inspect(inspector) {
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/core/src/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum BackendRequest {
///
/// This handler will remain active as long as it is reachable (request channel still open) and
/// requests are in progress.
#[must_use = "BackendHandler does nothing unless polled."]
#[must_use = "futures do nothing unless polled"]
pub struct BackendHandler<P> {
provider: P,
/// Stores all the data.
Expand Down Expand Up @@ -759,7 +759,7 @@ mod tests {
evm_opts,
};

let backend = Backend::spawn(Some(fork)).await;
let backend = Backend::spawn(Some(fork));

// some rng contract from etherscan
let address: Address = "63091244180ae240c87d1f528f5f269134cb07b3".parse().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/core/src/fork/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl MultiFork {
}

/// Creates a new pair and spawns the `MultiForkHandler` on a background thread.
pub async fn spawn() -> Self {
pub fn spawn() -> Self {
trace!(target: "fork::multi", "spawning multifork");

let (fork, mut handler) = Self::new();
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/evm/src/executors/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ pub struct TracingExecutor {
}

impl TracingExecutor {
pub async fn new(
pub fn new(
env: revm::primitives::Env,
fork: Option<CreateFork>,
version: Option<EvmVersion>,
debug: bool,
) -> Self {
let db = Backend::spawn(fork).await;
let db = Backend::spawn(fork);
Self {
// configures a bare version of the evm executor: no cheatcode inspector is enabled,
// tracing will be enabled only for the targeted transaction
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl CoverageArgs {
let known_contracts = runner.known_contracts.clone();
let filter = self.filter;
let (tx, rx) = channel::<(String, SuiteResult)>();
let handle = tokio::task::spawn(async move { runner.test(&filter, tx).await });
let handle = tokio::task::spawn_blocking(move || runner.test(&filter, tx));

// Add hit data to the coverage report
let data = rx
Expand Down
3 changes: 1 addition & 2 deletions crates/forge/bin/cmd/script/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl ScriptArgs {
Some(db) => db.clone(),
None => {
let fork = script_config.evm_opts.get_fork(&script_config.config, env.clone());
let backend = Backend::spawn(fork).await;
let backend = Backend::spawn(fork);
script_config.backends.insert(url.clone(), backend.clone());
backend
}
Expand All @@ -291,7 +291,6 @@ impl ScriptArgs {
// no need to cache it, since there won't be any onchain simulation that we'd need
// to cache the backend for.
Backend::spawn(script_config.evm_opts.get_fork(&script_config.config, env.clone()))
.await
}
};

Expand Down
6 changes: 3 additions & 3 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl TestArgs {
}

if self.json {
let results = runner.test_collect(filter).await;
let results = runner.test_collect(filter);
println!("{}", serde_json::to_string(&results)?);
return Ok(TestOutcome::new(results, self.allow_failure));
}
Expand All @@ -303,9 +303,9 @@ impl TestArgs {
// Run tests.
let (tx, rx) = channel::<(String, SuiteResult)>();
let timer = Instant::now();
let handle = tokio::task::spawn({
let handle = tokio::task::spawn_blocking({
let filter = filter.clone();
async move { runner.test(&filter, tx).await }
move || runner.test(&filter, tx)
});

let mut gas_report =
Expand Down
12 changes: 6 additions & 6 deletions crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ impl MultiContractRunner {
/// The same as [`test`](Self::test), but returns the results instead of streaming them.
///
/// Note that this method returns only when all tests have been executed.
pub async fn test_collect(&mut self, filter: &dyn TestFilter) -> BTreeMap<String, SuiteResult> {
self.test_iter(filter).await.collect()
pub fn test_collect(&mut self, filter: &dyn TestFilter) -> BTreeMap<String, SuiteResult> {
self.test_iter(filter).collect()
}

/// Executes _all_ tests that match the given `filter`.
///
/// The same as [`test`](Self::test), but returns the results instead of streaming them.
///
/// Note that this method returns only when all tests have been executed.
pub async fn test_iter(
pub fn test_iter(
&mut self,
filter: &dyn TestFilter,
) -> impl Iterator<Item = (String, SuiteResult)> {
let (tx, rx) = mpsc::channel();
self.test(filter, tx).await;
self.test(filter, tx);
rx.into_iter()
}

Expand All @@ -144,11 +144,11 @@ impl MultiContractRunner {
/// before executing all contracts and their tests in _parallel_.
///
/// Each Executor gets its own instance of the `Backend`.
pub async fn test(&mut self, filter: &dyn TestFilter, tx: mpsc::Sender<(String, SuiteResult)>) {
pub fn test(&mut self, filter: &dyn TestFilter, tx: mpsc::Sender<(String, SuiteResult)>) {
trace!("running all tests");

// The DB backend that serves all the data.
let db = Backend::spawn(self.fork.take()).await;
let db = Backend::spawn(self.fork.take());
let executor = ExecutorBuilder::new()
.inspectors(|stack| {
stack
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/it/cheats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test_cheats_local() {

let mut config = Config::with_root(PROJECT.root());
config.fs_permissions = FsPermissions::new(vec![PathPermission::read_write("./")]);
let runner = runner_with_config(config).await;
let runner = runner_with_config(config);

TestConfig::with_filter(runner, filter).run().await;
}
27 changes: 11 additions & 16 deletions crates/forge/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl TestConfig {
Self::with_filter(runner, Filter::matches_all())
}

pub async fn filter(filter: Filter) -> Self {
Self::with_filter(runner().await, filter)
pub fn filter(filter: Filter) -> Self {
Self::with_filter(runner(), filter)
}

pub fn with_filter(runner: MultiContractRunner, filter: Filter) -> Self {
Expand All @@ -55,8 +55,8 @@ impl TestConfig {
}

/// Executes the test runner
pub async fn test(&mut self) -> BTreeMap<String, SuiteResult> {
self.runner.test_collect(&self.filter).await
pub fn test(&mut self) -> BTreeMap<String, SuiteResult> {
self.runner.test_collect(&self.filter)
}

pub async fn run(&mut self) {
Expand All @@ -69,7 +69,7 @@ impl TestConfig {
/// * filter matched 0 test cases
/// * a test results deviates from the configured `should_fail` setting
pub async fn try_run(&mut self) -> eyre::Result<()> {
let suite_result = self.test().await;
let suite_result = self.test();
if suite_result.is_empty() {
eyre::bail!("empty test result");
}
Expand Down Expand Up @@ -127,20 +127,20 @@ pub fn base_runner() -> MultiContractRunnerBuilder {
}

/// Builds a non-tracing runner
pub async fn runner() -> MultiContractRunner {
pub fn runner() -> MultiContractRunner {
let mut config = Config::with_root(PROJECT.root());
config.fs_permissions = FsPermissions::new(vec![PathPermission::read_write(manifest_root())]);
runner_with_config(config).await
runner_with_config(config)
}

/// Builds a non-tracing runner
pub async fn runner_with_config(mut config: Config) -> MultiContractRunner {
pub fn runner_with_config(mut config: Config) -> MultiContractRunner {
config.rpc_endpoints = rpc_endpoints();
config.allow_paths.push(manifest_root().to_path_buf());

let root = &PROJECT.paths.root;
let opts = &*EVM_OPTS;
let env = opts.evm_env().await.expect("could not instantiate fork environment");
let env = opts.local_evm_env();
let output = COMPILED.clone();
base_runner()
.with_cheats_config(CheatsConfig::new(&config, opts.clone(), None))
Expand All @@ -150,16 +150,11 @@ pub async fn runner_with_config(mut config: Config) -> MultiContractRunner {
}

/// Builds a tracing runner
pub async fn tracing_runner() -> MultiContractRunner {
pub fn tracing_runner() -> MultiContractRunner {
let mut opts = EVM_OPTS.clone();
opts.verbosity = 5;
base_runner()
.build(
&PROJECT.paths.root,
(*COMPILED).clone(),
EVM_OPTS.evm_env().await.expect("Could not instantiate fork environment"),
opts,
)
.build(&PROJECT.paths.root, (*COMPILED).clone(), EVM_OPTS.local_evm_env(), opts)
.unwrap()
}

Expand Down
24 changes: 12 additions & 12 deletions crates/forge/tests/it/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::{collections::BTreeMap, env};
#[tokio::test(flavor = "multi_thread")]
async fn test_core() {
let filter = Filter::new(".*", ".*", ".*core");
let mut runner = runner().await;
let results = runner.test_collect(&filter).await;
let mut runner = runner();
let results = runner.test_collect(&filter);

assert_multiple(
&results,
Expand Down Expand Up @@ -79,8 +79,8 @@ async fn test_core() {
#[tokio::test(flavor = "multi_thread")]
async fn test_linking() {
let filter = Filter::new(".*", ".*", ".*linking");
let mut runner = runner().await;
let results = runner.test_collect(&filter).await;
let mut runner = runner();
let results = runner.test_collect(&filter);

assert_multiple(
&results,
Expand Down Expand Up @@ -113,8 +113,8 @@ async fn test_linking() {
#[tokio::test(flavor = "multi_thread")]
async fn test_logs() {
let filter = Filter::new(".*", ".*", ".*logs");
let mut runner = runner().await;
let results = runner.test_collect(&filter).await;
let mut runner = runner();
let results = runner.test_collect(&filter);

assert_multiple(
&results,
Expand Down Expand Up @@ -678,26 +678,26 @@ async fn test_env_vars() {
env::remove_var(env_var_key);

let filter = Filter::new("testSetEnv", ".*", ".*");
let mut runner = runner().await;
let _ = runner.test_collect(&filter).await;
let mut runner = runner();
let _ = runner.test_collect(&filter);

assert_eq!(env::var(env_var_key).unwrap(), env_var_val);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_doesnt_run_abstract_contract() {
let filter = Filter::new(".*", ".*", ".*Abstract.t.sol".to_string().as_str());
let mut runner = runner().await;
let results = runner.test_collect(&filter).await;
let mut runner = runner();
let results = runner.test_collect(&filter);
assert!(results.get("core/Abstract.t.sol:AbstractTestBase").is_none());
assert!(results.get("core/Abstract.t.sol:AbstractTest").is_some());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_trace() {
let filter = Filter::new(".*", ".*", ".*trace");
let mut runner = tracing_runner().await;
let suite_result = runner.test_collect(&filter).await;
let mut runner = tracing_runner();
let suite_result = runner.test_collect(&filter);

// TODO: This trace test is very basic - it is probably a good candidate for snapshot
// testing.
Expand Down
Loading

0 comments on commit e78b947

Please sign in to comment.