Skip to content

Commit

Permalink
chore: clean up, lint, move write_json to utils, resolve conflicts, u…
Browse files Browse the repository at this point in the history
…pdate dev deps
  • Loading branch information
dutterbutter committed Dec 18, 2024
1 parent c34aa14 commit 127327e
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 396 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ tracing-subscriber = { version = "0.3", features = [
"local-time",
] }

#########################
# Test dependencies #
#########################
httptest = "0.15.4"
tempdir = "0.3.7"
maplit = "1.0.2"
zksync-web3-rs = "0.1.1"
ethers = { version = "2.0.4", features = ["rustls"] }
test-case = "3.3.1"

#########################
# Local dependencies #
#########################
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ tower-http.workspace = true
flate2.workspace = true

[dev-dependencies]
tempfile = "3"
tempdir.workspace = true
19 changes: 10 additions & 9 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{parse_genesis_file, write_json_file};
use crate::utils::parse_genesis_file;
use alloy_signer_local::coins_bip39::{English, Mnemonic};
use anvil_zksync_config::constants::{
DEFAULT_DISK_CACHE_DIR, DEFAULT_MNEMONIC, TEST_NODE_NETWORK_ID,
Expand All @@ -7,11 +7,13 @@ use anvil_zksync_config::types::{
AccountGenerator, CacheConfig, CacheType, Genesis, SystemContractsOptions,
};
use anvil_zksync_config::TestNodeConfig;
use anvil_zksync_core::{
node::{InMemoryNode, VersionedState},
utils::write_json_file,
};
use anvil_zksync_types::{
LogLevel, ShowCalls, ShowGasDetails, ShowStorageLogs, ShowVMDetails, TransactionOrder,
};
use anvil_zksync_core::node::state::VersionedState;
use anvil_zksync_core::node::InMemoryNode;
use anyhow::Result;
use clap::{arg, command, Parser, Subcommand};
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -433,7 +435,7 @@ impl Cli {
.with_no_mining(self.no_mining)
.with_allow_origin(self.allow_origin)
.with_no_cors(self.no_cors)
.with_transaction_order(self.order);
.with_transaction_order(self.order)
.with_state_interval(self.state_interval)
.with_dump_state(self.dump_state)
.with_preserve_historical_states(self.preserve_historical_states);
Expand Down Expand Up @@ -615,10 +617,12 @@ mod tests {
BlockSealer, BlockSealerMode, ImpersonationManager, InMemoryNode, TimestampManager, TxPool,
};
use clap::Parser;
use serde_json::Value;
use std::{
env,
net::{IpAddr, Ipv4Addr},
};
use tempdir::TempDir;

#[test]
fn can_parse_host() {
Expand Down Expand Up @@ -666,10 +670,7 @@ mod tests {

#[tokio::test]
async fn test_dump_state() -> anyhow::Result<()> {
use serde_json::Value;
use tempfile::tempdir;

let temp_dir = tempdir()?;
let temp_dir = TempDir::new("state-test").expect("failed creating temporary dir");
let dump_path = temp_dir.path().join("state.json");

let config = anvil_zksync_config::TestNodeConfig {
Expand All @@ -685,7 +686,7 @@ mod tests {
&config,
TimestampManager::default(),
ImpersonationManager::default(),
TxPool::new(ImpersonationManager::default()),
TxPool::new(ImpersonationManager::default(), config.transaction_order),
BlockSealer::new(BlockSealerMode::noop()),
);
let test_address = zksync_types::H160::random();
Expand Down
55 changes: 12 additions & 43 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::fs::File;
use std::time::Duration;
use std::{env, net::SocketAddr, str::FromStr};
use tower_http::cors::AllowOrigin;
use tokio::signal;
use tracing_subscriber::filter::LevelFilter;
use zksync_types::fee_model::{FeeModelConfigV2, FeeParams};
use zksync_types::H160;
Expand Down Expand Up @@ -279,61 +278,28 @@ async fn main() -> anyhow::Result<()> {
let any_server_stopped =
futures::future::select_all(server_handles.into_iter().map(|h| Box::pin(h.stopped())));

let mut threads = futures::future::join_all(config.host.iter().map(|host| {
let addr = SocketAddr::new(*host, config.port);
build_json_http(
addr,
log_level_filter,
node.clone(),
config.health_check_endpoint,
config.allow_origin.clone(),
config.no_cors,
)
}))
.await;

// Start the state dumper
let dump_state = config.dump_state.clone();
let dump_interval = config
.state_interval
.map(Duration::from_secs)
.unwrap_or(Duration::from_secs(60)); // Default to 60 seconds
let preserve_historical_states = config.preserve_historical_states;
let node_for_dumper = node.clone();
let mut state_dumper = PeriodicStateDumper::new(
let state_dumper = tokio::task::spawn(PeriodicStateDumper::new(
node_for_dumper,
dump_state,
dump_interval,
preserve_historical_states,
);
// Start the block producer
));

let system_contracts =
SystemContracts::from_options(&config.system_contracts_options, config.use_evm_emulator);
let block_producer = BlockProducer::new(
node.clone(),
pool.clone(),
block_sealer.clone(),
let block_producer_handle = tokio::task::spawn(BlockProducer::new(
node,
pool,
block_sealer,
system_contracts,
);

// Spawn a task to handle periodic dumping, block producer, and final dump on shutdown
let handle = tokio::spawn(async move {
tokio::select! {
_ = signal::ctrl_c() => {
tracing::trace!("received shutdown signal, shutting down");
},
_ = &mut state_dumper => {
tracing::trace!("State dumper completed");
},
_ = block_producer => {
tracing::trace!("Block producer completed");
}
}
state_dumper.dump().await;

std::process::exit(0);
});
threads.push(handle);
));

config.print(fork_print_info.as_ref());

Expand All @@ -346,7 +312,10 @@ async fn main() -> anyhow::Result<()> {
},
_ = block_producer_handle => {
tracing::trace!("block producer was stopped")
}
},
_ = state_dumper => {
tracing::trace!("state dumper was stopped")
},
}

Ok(())
Expand Down
23 changes: 0 additions & 23 deletions crates/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use anvil_zksync_config::types::Genesis;
use anvil_zksync_config::TestNodeConfig;
use anvil_zksync_core::fork::ForkDetails;
use anyhow::Context;
use serde::Serialize;
use std::fs;
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
};

/// Parses the genesis file from the given path.
pub fn parse_genesis_file(path: &str) -> Result<Genesis, String> {
Expand All @@ -17,22 +10,6 @@ pub fn parse_genesis_file(path: &str) -> Result<Genesis, String> {
serde_json::from_str(&file_content).map_err(|err| format!("Failed to parse JSON: {err}"))
}

/// Writes the given serializable object as JSON to the specified file path using pretty printing.
/// Returns an error if the file cannot be created or if serialization/writing fails.
pub fn write_json_file<T: Serialize>(path: &Path, obj: &T) -> anyhow::Result<()> {
let file = File::create(path)
.with_context(|| format!("Failed to create file '{}'", path.display()))?;
let mut writer = BufWriter::new(file);
// Note: intentionally using pretty printing for better readability.
serde_json::to_writer_pretty(&mut writer, obj)
.with_context(|| format!("Failed to write JSON to '{}'", path.display()))?;
writer
.flush()
.with_context(|| format!("Failed to flush writer for '{}'", path.display()))?;

Ok(())
}

/// Updates the configuration from fork details.
pub async fn update_with_fork_details(
config: &mut TestNodeConfig,
Expand Down
12 changes: 6 additions & 6 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ flate2.workspace = true
thiserror.workspace = true

[dev-dependencies]
httptest = "0.15.4"
tempdir = "0.3.7"
maplit = "1.0.2"
zksync-web3-rs = "0.1.1"
ethers = { version = "2.0.4", features = ["rustls"] }
test-case = "3.3.1"
maplit.workspace = true
ethers.workspace = true
httptest.workspace = true
tempdir.workspace = true
zksync-web3-rs.workspace = true
test-case.workspace = true
22 changes: 22 additions & 0 deletions crates/core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use anyhow::Context;
use chrono::{DateTime, Utc};
use serde::Serialize;
use std::{convert::TryInto, fmt};
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
};
use zksync_multivm::interface::{Call, CallType, ExecutionResult, VmExecutionResultAndLogs};
use zksync_types::{
api::{BlockNumber, DebugCall, DebugCallType},
Expand Down Expand Up @@ -188,6 +194,22 @@ pub fn calculate_eth_cost(gas_price_in_wei_per_gas: u64, gas_used: u64) -> f64 {
total_cost_in_gwei / 1e9
}

/// Writes the given serializable object as JSON to the specified file path using pretty printing.
/// Returns an error if the file cannot be created or if serialization/writing fails.
pub fn write_json_file<T: Serialize>(path: &Path, obj: &T) -> anyhow::Result<()> {
let file = File::create(path)
.with_context(|| format!("Failed to create file '{}'", path.display()))?;
let mut writer = BufWriter::new(file);
// Note: intentionally using pretty printing for better readability.
serde_json::to_writer_pretty(&mut writer, obj)
.with_context(|| format!("Failed to write JSON to '{}'", path.display()))?;
writer
.flush()
.with_context(|| format!("Failed to flush writer for '{}'", path.display()))?;

Ok(())
}

#[cfg(test)]
mod tests {
use zksync_types::U256;
Expand Down
Loading

0 comments on commit 127327e

Please sign in to comment.