diff --git a/execution/src/constants.rs b/execution/src/constants.rs new file mode 100644 index 0000000..dce6d56 --- /dev/null +++ b/execution/src/constants.rs @@ -0,0 +1 @@ +pub const PARALLEL_QUERY_BATCH_SIZE: usize = 20; diff --git a/execution/src/evm.rs b/execution/src/evm.rs index 02a9952..01591d1 100644 --- a/execution/src/evm.rs +++ b/execution/src/evm.rs @@ -22,6 +22,7 @@ use tokio::runtime::Runtime; use consensus::types::ExecutionPayload; use crate::{ + constants::PARALLEL_QUERY_BATCH_SIZE, errors::EvmError, rpc::ExecutionRpc, types::{Account, CallOpts}, @@ -146,33 +147,26 @@ impl<'a, R: ExecutionRpc> Evm<'a, R> { list.push(to_access_entry); list.push(producer_account); - let mut accounts = Vec::new(); - let batch_size = 20; - for i in (0..list.len()).step_by(batch_size) { - let end = cmp::min(i + batch_size, list.len()); - let chunk = &list[i..end]; - - let account_chunk_futs = chunk.iter().map(|account| { - let addr_fut = futures::future::ready(account.address); + let mut account_map = HashMap::new(); + for chunk in list.chunks(PARALLEL_QUERY_BATCH_SIZE) { + let account_chunk_futs = chunk.into_iter().map(|account| { let account_fut = execution.get_account( &account.address, Some(account.storage_keys.as_slice()), &payload, ); - async move { (addr_fut.await, account_fut.await) } + async move { (account.address, account_fut.await) } }); - let mut account_chunk = join_all(account_chunk_futs).await; - accounts.append(&mut account_chunk); - } + let account_chunk = join_all(account_chunk_futs).await; - let mut account_map = HashMap::new(); - accounts.iter().for_each(|account| { - let addr = account.0; - if let Ok(account) = &account.1 { - account_map.insert(addr, account.clone()); - } - }); + account_chunk + .into_iter() + .filter(|i| i.1.is_ok()) + .for_each(|(key, value)| { + account_map.insert(key, value.ok().unwrap()); + }); + } Ok(account_map) } diff --git a/execution/src/lib.rs b/execution/src/lib.rs index 8099dc1..e591328 100644 --- a/execution/src/lib.rs +++ b/execution/src/lib.rs @@ -1,3 +1,4 @@ +pub mod constants; pub mod errors; pub mod evm; pub mod rpc;