Skip to content

Commit

Permalink
Make era-test-node a library (#79)
Browse files Browse the repository at this point in the history
* Make era-test-node a library

* Fixed merge changes

* fixed tests

* fmt

* Moved showcalls to node
  • Loading branch information
mm-zk authored Aug 23, 2023
1 parent be80610 commit 0fc2ace
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/configuration_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpc_derive::rpc;
// Workspace uses

// Local uses
use crate::{node::InMemoryNodeInner, ShowCalls, ShowStorageLogs, ShowVMDetails};
use crate::{node::InMemoryNodeInner, node::ShowCalls, node::ShowStorageLogs, node::ShowVMDetails};

pub struct ConfigurationApiNamespace {
node: Arc<RwLock<InMemoryNodeInner>>,
Expand Down
2 changes: 1 addition & 1 deletion src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Helper methods to display transaction data in more human readable way.
use crate::{resolver, ShowCalls};
use crate::{node::ShowCalls, resolver};

use colored::Colorize;
use serde::Deserialize;
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod configuration_api;
pub mod console_log;
pub mod deps;
pub mod fork;
pub mod formatter;
pub mod node;
pub mod resolver;
pub mod utils;
pub mod zks;
83 changes: 2 additions & 81 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
//!
//! Contributions to improve `era-test-node` are welcome. Please refer to the contribution guidelines for more details.

use crate::node::{ShowStorageLogs, ShowVMDetails};
use clap::{Parser, Subcommand};
use configuration_api::ConfigurationApiNamespaceT;
use fork::ForkDetails;
use node::ShowCalls;
use zks::ZkMockNamespaceImpl;

mod configuration_api;
Expand All @@ -57,7 +59,6 @@ mod resolver;
mod utils;
mod zks;

use core::fmt::Display;
use node::InMemoryNode;
use zksync_core::api_server::web3::namespaces::NetNamespace;

Expand Down Expand Up @@ -194,86 +195,6 @@ struct Cli {
dev_use_local_contracts: bool,
}

#[derive(Debug, Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowCalls {
None,
User,
System,
All,
}

impl FromStr for ShowCalls {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowCalls::None),
"user" => Ok(ShowCalls::User),
"system" => Ok(ShowCalls::System),
"all" => Ok(ShowCalls::All),
_ => Err(()),
}
}
}

impl Display for ShowCalls {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowStorageLogs {
None,
Read,
Write,
All,
}

impl FromStr for ShowStorageLogs {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowStorageLogs::None),
"read" => Ok(ShowStorageLogs::Read),
"write" => Ok(ShowStorageLogs::Write),
"all" => Ok(ShowStorageLogs::All),
_ => Err(()),
}
}
}

impl Display for ShowStorageLogs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowVMDetails {
None,
All,
}

impl FromStr for ShowVMDetails {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowVMDetails::None),
"all" => Ok(ShowVMDetails::All),
_ => Err(()),
}
}
}

impl Display for ShowVMDetails {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Subcommand)]
enum Command {
/// Starts a new empty local network.
Expand Down
101 changes: 97 additions & 4 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ use crate::{
fork::{ForkDetails, ForkStorage},
formatter,
utils::{adjust_l1_gas_price_for_tx, derive_gas_estimation_overhead, IntoBoxedFuture},
ShowCalls, ShowStorageLogs, ShowVMDetails,
};
use clap::Parser;
use colored::Colorize;
use core::fmt::Display;
use futures::FutureExt;
use jsonrpc_core::BoxFuture;
use std::{
cmp::{self},
collections::HashMap,
convert::TryInto,
str::FromStr,
sync::{Arc, RwLock},
};

use vm::{
utils::{BLOCK_GAS_LIMIT, ETH_CALL_GAS_LIMIT},
vm::VmTxExecutionResult,
Expand Down Expand Up @@ -98,6 +101,96 @@ pub struct TxExecutionInfo {
pub result: VmTxExecutionResult,
}

#[derive(Debug, clap::Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowCalls {
None,
User,
System,
All,
}

impl FromStr for ShowCalls {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowCalls::None),
"user" => Ok(ShowCalls::User),
"system" => Ok(ShowCalls::System),
"all" => Ok(ShowCalls::All),
_ => Err(format!(
"Unknown ShowCalls value {} - expected one of none|user|system|all.",
s
)
.to_owned()),
}
}
}

impl Display for ShowCalls {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowStorageLogs {
None,
Read,
Write,
All,
}

impl FromStr for ShowStorageLogs {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowStorageLogs::None),
"read" => Ok(ShowStorageLogs::Read),
"write" => Ok(ShowStorageLogs::Write),
"all" => Ok(ShowStorageLogs::All),
_ => Err(format!(
"Unknown ShowStorageLogs value {} - expected one of none|read|write|all.",
s
)),
}
}
}

impl Display for ShowStorageLogs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Parser, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ShowVMDetails {
None,
All,
}

impl FromStr for ShowVMDetails {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"none" => Ok(ShowVMDetails::None),
"all" => Ok(ShowVMDetails::All),
_ => Err(format!(
"Unknown ShowVMDetails value {} - expected one of none|all.",
s
)),
}
}
}

impl Display for ShowVMDetails {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}

/// Helper struct for InMemoryNode.
pub struct InMemoryNodeInner {
/// Timestamp, batch number and miniblock number that will be used by the next block.
Expand Down Expand Up @@ -546,9 +639,9 @@ impl Default for InMemoryNode {
fn default() -> Self {
InMemoryNode::new(
None,
crate::ShowCalls::None,
crate::ShowStorageLogs::None,
crate::ShowVMDetails::None,
crate::node::ShowCalls::None,
ShowStorageLogs::None,
ShowVMDetails::None,
false,
false,
)
Expand Down
7 changes: 4 additions & 3 deletions src/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ mod tests {
use std::str::FromStr;

use crate::node::InMemoryNode;
use crate::node::ShowCalls;

use super::*;
use zksync_basic_types::Address;
Expand Down Expand Up @@ -298,9 +299,9 @@ mod tests {
// Arrange
let node = InMemoryNode::new(
None,
crate::ShowCalls::None,
crate::ShowStorageLogs::None,
crate::ShowVMDetails::None,
ShowCalls::None,
crate::node::ShowStorageLogs::None,
crate::node::ShowVMDetails::None,
false,
false,
);
Expand Down

0 comments on commit 0fc2ace

Please sign in to comment.