Skip to content

Commit

Permalink
remove S generic on InMemoryNode and ForkStorage (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov authored Dec 16, 2024
1 parent 1c42504 commit 2033e3c
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 213 deletions.
6 changes: 1 addition & 5 deletions crates/cli/src/bytecode_override.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fs;

use anvil_zksync_core::fork::ForkSource;
use anvil_zksync_core::node::InMemoryNode;
use eyre::Context;
use hex::FromHex;
Expand All @@ -20,10 +19,7 @@ struct Bytecode {

// Loads a list of bytecodes and addresses from the directory and then inserts them directly
// into the Node's storage.
pub fn override_bytecodes<T: Clone + ForkSource + std::fmt::Debug>(
node: &InMemoryNode<T>,
bytecodes_dir: String,
) -> eyre::Result<()> {
pub fn override_bytecodes(node: &InMemoryNode, bytecodes_dir: String) -> eyre::Result<()> {
for entry in fs::read_dir(bytecodes_dir)? {
let entry = entry?;
let path = entry.path();
Expand Down
11 changes: 4 additions & 7 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use anvil_zksync_config::constants::{
};
use anvil_zksync_config::types::SystemContractsOptions;
use anvil_zksync_config::ForkPrintInfo;
use anvil_zksync_core::fork::{ForkDetails, ForkSource};
use anvil_zksync_core::http_fork_source::HttpForkSource;
use anvil_zksync_core::fork::ForkDetails;
use anvil_zksync_core::namespaces::{
AnvilNamespaceT, ConfigurationApiNamespaceT, DebugNamespaceT, EthNamespaceT,
EthTestNodeNamespaceT, EvmNamespaceT, HardhatNamespaceT, NetNamespaceT, Web3NamespaceT,
Expand Down Expand Up @@ -43,12 +42,10 @@ mod logging_middleware;
mod utils;

#[allow(clippy::too_many_arguments)]
async fn build_json_http<
S: std::marker::Sync + std::marker::Send + 'static + ForkSource + std::fmt::Debug + Clone,
>(
async fn build_json_http(
addr: SocketAddr,
log_level_filter: LevelFilter,
node: InMemoryNode<S>,
node: InMemoryNode,
enable_health_api: bool,
) -> tokio::task::JoinHandle<()> {
let (sender, recv) = oneshot::channel::<()>();
Expand Down Expand Up @@ -280,7 +277,7 @@ async fn main() -> anyhow::Result<()> {
};
let block_sealer = BlockSealer::new(sealing_mode);

let node: InMemoryNode<HttpForkSource> = InMemoryNode::new(
let node: InMemoryNode = InMemoryNode::new(
fork_details,
Some(observability),
&config,
Expand Down
22 changes: 9 additions & 13 deletions crates/core/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::{
convert::{TryFrom, TryInto},
fmt,
future::Future,
marker::PhantomData,
str::FromStr,
sync::{Arc, RwLock},
};
Expand Down Expand Up @@ -99,13 +98,13 @@ impl ForkNetwork {
/// If forking is enabled, it reads missing data from remote location.
/// S - is a struct that is used for source of the fork.
#[derive(Debug, Clone)]
pub struct ForkStorage<S> {
pub inner: Arc<RwLock<ForkStorageInner<S>>>,
pub struct ForkStorage {
pub inner: Arc<RwLock<ForkStorageInner>>,
pub chain_id: L2ChainId,
}

#[derive(Debug)]
pub struct ForkStorageInner<S> {
pub struct ForkStorageInner {
// Underlying local storage
pub raw_storage: InMemoryStorage,
// Cache of data that was read from remote location.
Expand All @@ -115,11 +114,9 @@ pub struct ForkStorageInner<S> {
// If set - it hold the necessary information on where to fetch the data.
// If not set - it will simply read from underlying storage.
pub fork: Option<Box<ForkDetails>>,
// ForkSource type no longer needed but retained to keep the old interface.
pub dummy: PhantomData<S>,
}

impl<S: ForkSource> ForkStorage<S> {
impl ForkStorage {
pub fn new(
fork: Option<ForkDetails>,
system_contracts_options: &SystemContractsOptions,
Expand All @@ -145,7 +142,6 @@ impl<S: ForkSource> ForkStorage<S> {
value_read_cache: Default::default(),
fork: fork.map(Box::new),
factory_dep_cache: Default::default(),
dummy: Default::default(),
})),
chain_id,
}
Expand Down Expand Up @@ -304,7 +300,7 @@ impl<S: ForkSource> ForkStorage<S> {
}
}

impl<S: std::fmt::Debug + ForkSource> ReadStorage for ForkStorage<S> {
impl ReadStorage for ForkStorage {
fn is_write_initial(&mut self, key: &StorageKey) -> bool {
self.is_write_initial_internal(key).unwrap()
}
Expand All @@ -322,7 +318,7 @@ impl<S: std::fmt::Debug + ForkSource> ReadStorage for ForkStorage<S> {
}
}

impl<S: std::fmt::Debug + ForkSource> ReadStorage for &ForkStorage<S> {
impl ReadStorage for &ForkStorage {
fn read_value(&mut self, key: &StorageKey) -> zksync_types::StorageValue {
self.read_value_internal(key).unwrap()
}
Expand All @@ -340,7 +336,7 @@ impl<S: std::fmt::Debug + ForkSource> ReadStorage for &ForkStorage<S> {
}
}

impl<S> ForkStorage<S> {
impl ForkStorage {
pub fn set_value(&mut self, key: StorageKey, value: zksync_types::StorageValue) {
let mut mutator = self.inner.write().unwrap();
mutator.raw_storage.set_value(key, value)
Expand Down Expand Up @@ -922,7 +918,7 @@ mod tests {
cache_config: CacheConfig::None,
};

let mut fork_storage: ForkStorage<testing::ExternalStorage> =
let mut fork_storage: ForkStorage =
ForkStorage::new(Some(fork_details), &options, false, None);

assert!(fork_storage.is_write_initial(&never_written_key));
Expand Down Expand Up @@ -985,7 +981,7 @@ mod tests {
fee_params: None,
cache_config: CacheConfig::None,
};
let mut fork_storage: ForkStorage<testing::ExternalStorage> = ForkStorage::new(
let mut fork_storage: ForkStorage = ForkStorage::new(
Some(fork_details),
&SystemContractsOptions::default(),
false,
Expand Down
7 changes: 2 additions & 5 deletions crates/core/src/node/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use zksync_web3_decl::error::Web3Error;
use crate::namespaces::DetailedTransaction;
use crate::utils::Numeric;
use crate::{
fork::ForkSource,
namespaces::{AnvilNamespaceT, ResetRequest, RpcResult},
node::InMemoryNode,
utils::{into_jsrpc_error, into_jsrpc_error_message, IntoBoxedFuture},
};

impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> AnvilNamespaceT
for InMemoryNode<S>
{
impl AnvilNamespaceT for InMemoryNode {
fn dump_state(&self, preserve_historical_states: Option<bool>) -> RpcResult<Bytes> {
self.dump_state(preserve_historical_states.unwrap_or(false))
.map_err(|err| {
Expand Down Expand Up @@ -231,7 +228,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> AnvilNames
}

fn stop_impersonating_account(&self, address: Address) -> RpcResult<()> {
InMemoryNode::<S>::stop_impersonating_account(self, address)
InMemoryNode::stop_impersonating_account(self, address)
.map(|_| ())
.map_err(|err| {
tracing::error!("failed stopping to impersonate account: {:?}", err);
Expand Down
12 changes: 5 additions & 7 deletions crates/core/src/node/block_producer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use crate::fork::ForkSource;
use crate::node::pool::{TxBatch, TxPool};
use crate::node::sealer::BlockSealer;
use crate::node::InMemoryNode;
use crate::system_contracts::SystemContracts;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use zksync_multivm::interface::TxExecutionMode;

pub struct BlockProducer<S: Clone> {
node: InMemoryNode<S>,
pub struct BlockProducer {
node: InMemoryNode,
pool: TxPool,
block_sealer: BlockSealer,
system_contracts: SystemContracts,
}

impl<S: Clone> BlockProducer<S> {
impl BlockProducer {
pub fn new(
node: InMemoryNode<S>,
node: InMemoryNode,
pool: TxPool,
block_sealer: BlockSealer,
system_contracts: SystemContracts,
Expand All @@ -32,7 +30,7 @@ impl<S: Clone> BlockProducer<S> {
}
}

impl<S: ForkSource + Clone + fmt::Debug> Future for BlockProducer<S> {
impl Future for BlockProducer {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down
5 changes: 1 addition & 4 deletions crates/core/src/node/config_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::node::time::ReadTime;
use crate::{
fork::ForkSource,
namespaces::{ConfigurationApiNamespaceT, Result},
node::InMemoryNode,
utils::into_jsrpc_error,
Expand All @@ -10,9 +9,7 @@ use anvil_zksync_config::types::{
};
use zksync_web3_decl::error::Web3Error;

impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> ConfigurationApiNamespaceT
for InMemoryNode<S>
{
impl ConfigurationApiNamespaceT for InMemoryNode {
fn config_get_show_calls(&self) -> Result<String> {
self.get_inner()
.read()
Expand Down
26 changes: 11 additions & 15 deletions crates/core/src/node/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ use zksync_web3_decl::error::Web3Error;

use crate::{
deps::storage_view::StorageView,
fork::ForkSource,
namespaces::{DebugNamespaceT, Result, RpcResult},
node::{InMemoryNode, MAX_TX_SIZE},
utils::{create_debug_output, into_jsrpc_error, to_real_block_number},
};

impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> DebugNamespaceT
for InMemoryNode<S>
{
impl DebugNamespaceT for InMemoryNode {
fn trace_block_by_number(
&self,
block: BlockNumber,
Expand Down Expand Up @@ -247,12 +244,11 @@ mod tests {
use super::*;
use crate::{
deps::system_contracts::bytecode_from_slice,
http_fork_source::HttpForkSource,
node::{InMemoryNode, TransactionResult},
testing::{self, LogBuilder},
};

fn deploy_test_contracts(node: &InMemoryNode<HttpForkSource>) -> (Address, Address) {
fn deploy_test_contracts(node: &InMemoryNode) -> (Address, Address) {
let private_key = K256PrivateKey::from_bytes(H256::repeat_byte(0xee)).unwrap();
let from_account = private_key.address();
node.set_rich_account(from_account, U256::from(DEFAULT_ACCOUNT_BALANCE));
Expand Down Expand Up @@ -291,7 +287,7 @@ mod tests {

#[tokio::test]
async fn test_trace_deployed_contract() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();

let (primary_deployed_address, secondary_deployed_address) = deploy_test_contracts(&node);
// trace a call to the primary contract
Expand Down Expand Up @@ -340,7 +336,7 @@ mod tests {

#[tokio::test]
async fn test_trace_only_top() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();

let (primary_deployed_address, _) = deploy_test_contracts(&node);

Expand Down Expand Up @@ -377,7 +373,7 @@ mod tests {

#[tokio::test]
async fn test_trace_reverts() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();

let (primary_deployed_address, _) = deploy_test_contracts(&node);

Expand Down Expand Up @@ -412,7 +408,7 @@ mod tests {

#[tokio::test]
async fn test_trace_transaction() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let inner = node.get_inner();
{
let mut writer = inner.write().unwrap();
Expand Down Expand Up @@ -440,7 +436,7 @@ mod tests {

#[tokio::test]
async fn test_trace_transaction_only_top() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let inner = node.get_inner();
{
let mut writer = inner.write().unwrap();
Expand Down Expand Up @@ -476,7 +472,7 @@ mod tests {

#[tokio::test]
async fn test_trace_transaction_not_found() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let result = node
.trace_transaction(H256::repeat_byte(0x1), None)
.await
Expand All @@ -486,7 +482,7 @@ mod tests {

#[tokio::test]
async fn test_trace_block_by_hash_empty() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let inner = node.get_inner();
{
let mut writer = inner.write().unwrap();
Expand All @@ -502,7 +498,7 @@ mod tests {

#[tokio::test]
async fn test_trace_block_by_hash() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let inner = node.get_inner();
{
let mut writer = inner.write().unwrap();
Expand Down Expand Up @@ -530,7 +526,7 @@ mod tests {

#[tokio::test]
async fn test_trace_block_by_number() {
let node = InMemoryNode::<HttpForkSource>::default();
let node = InMemoryNode::default();
let inner = node.get_inner();
{
let mut writer = inner.write().unwrap();
Expand Down
Loading

0 comments on commit 2033e3c

Please sign in to comment.