Skip to content

Commit

Permalink
Merge branch 'main' into nmv/add-musl-target-to-artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
MexicanAce committed Sep 5, 2023
2 parents 1d9f625 + fbff511 commit 2843348
Show file tree
Hide file tree
Showing 11 changed files with 1,309 additions and 938 deletions.
1,669 changes: 966 additions & 703 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ categories = ["cryptography"]
publish = false # We don't want to publish our binaries.

[dependencies]
zksync_basic_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_core = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
vm = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
vlog = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_contracts = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_utils = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_state = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_web3_decl = { git = "https://github.com/matter-labs/zksync-era.git", rev = "e575ec101fe88627ab541a52464ab5025c16e6b4" }
zksync_basic_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_core = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
vm = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
vlog = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_contracts = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_utils = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_state = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }
zksync_web3_decl = { git = "https://github.com/matter-labs/zksync-era.git", rev = "a98e454221da7d6ecad9b317cf44b0786e819659" }

anyhow = "1.0"
tokio = { version = "1", features = ["time", "rt"] }
Expand Down
14 changes: 8 additions & 6 deletions src/configuration_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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>>,
pub struct ConfigurationApiNamespace<S> {
node: Arc<RwLock<InMemoryNodeInner<S>>>,
}

impl ConfigurationApiNamespace {
pub fn new(node: Arc<RwLock<InMemoryNodeInner>>) -> Self {
impl<S> ConfigurationApiNamespace<S> {
pub fn new(node: Arc<RwLock<InMemoryNodeInner<S>>>) -> Self {
Self { node }
}
}
Expand Down Expand Up @@ -70,7 +70,9 @@ pub trait ConfigurationApiNamespaceT {
fn config_set_resolve_hashes(&self, value: bool) -> Result<bool>;
}

impl ConfigurationApiNamespaceT for ConfigurationApiNamespace {
impl<S: std::marker::Send + std::marker::Sync + 'static> ConfigurationApiNamespaceT
for ConfigurationApiNamespace<S>
{
fn config_get_show_calls(&self) -> Result<String> {
let reader = self.node.read().unwrap();
Ok(reader.show_calls.to_string())
Expand Down
2 changes: 1 addition & 1 deletion src/console_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ impl ConsoleLogHandler {
tokens.iter().map(|t| format!("{}", t)).join(" ")
})
});
println!("{}", message.to_string().cyan());
println!("{}", message.cyan());
}
}
119 changes: 69 additions & 50 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use std::{
};

use tokio::runtime::Builder;
use zksync_basic_types::{L1BatchNumber, L2ChainId, MiniblockNumber, H256, U64};
use zksync_basic_types::{Address, L1BatchNumber, L2ChainId, MiniblockNumber, H256, U256, U64};

use zksync_types::{
api::{BlockIdVariant, BlockNumber},
api::{BlockIdVariant, BlockNumber, Transaction},
l2::L2Tx,
StorageKey,
};
Expand All @@ -25,9 +25,9 @@ use zksync_utils::{bytecode::hash_bytecode, h256_to_u256};
use zksync_web3_decl::{jsonrpsee::http_client::HttpClient, namespaces::EthNamespaceClient};
use zksync_web3_decl::{jsonrpsee::http_client::HttpClientBuilder, namespaces::ZksNamespaceClient};

use crate::deps::InMemoryStorage;
use crate::deps::ReadStorage as RS;
use crate::node::TEST_NODE_NETWORK_ID;
use crate::{deps::InMemoryStorage, http_fork_source::HttpForkSource};

pub fn block_on<F: Future + Send + 'static>(future: F) -> F::Output
where
Expand All @@ -46,14 +46,15 @@ where

/// In memory storage, that allows 'forking' from other network.
/// 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)]
pub struct ForkStorage {
pub inner: Arc<RwLock<ForkStorageInner>>,
pub struct ForkStorage<S> {
pub inner: Arc<RwLock<ForkStorageInner<S>>>,
pub chain_id: L2ChainId,
}

#[derive(Debug)]
pub struct ForkStorageInner {
pub struct ForkStorageInner<S> {
// Underlying local storage
pub raw_storage: InMemoryStorage,
// Cache of data that was read from remote location.
Expand All @@ -62,11 +63,11 @@ pub struct ForkStorageInner {
pub factory_dep_cache: HashMap<H256, Option<Vec<u8>>>,
// 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<ForkDetails>,
pub fork: Option<ForkDetails<S>>,
}

impl ForkStorage {
pub fn new(fork: Option<ForkDetails>, dev_use_local_contracts: bool) -> Self {
impl<S: ForkSource> ForkStorage<S> {
pub fn new(fork: Option<ForkDetails<S>>, dev_use_local_contracts: bool) -> Self {
let chain_id = fork
.as_ref()
.and_then(|d| d.overwrite_chain_id)
Expand Down Expand Up @@ -100,23 +101,19 @@ impl ForkStorage {
if let Some(value) = mutator.value_read_cache.get(key) {
return *value;
}
let fork_ = (*fork).clone();
let l2_miniblock = fork.l2_miniblock;
let key_ = *key;

let client = fork.create_client();

let result = block_on(async move {
client
.get_storage_at(
*key_.account().address(),
h256_to_u256(*key_.key()),
Some(BlockIdVariant::BlockNumber(BlockNumber::Number(U64::from(
fork_.l2_miniblock,
)))),
)
.await
})
.unwrap();
let result = fork
.fork_source
.get_storage_at(
*key_.account().address(),
h256_to_u256(*key_.key()),
Some(BlockIdVariant::BlockNumber(BlockNumber::Number(U64::from(
l2_miniblock,
)))),
)
.unwrap();

mutator.value_read_cache.insert(*key, result);
result
Expand All @@ -136,8 +133,7 @@ impl ForkStorage {
return value.clone();
}

let client = fork.create_client();
let result = block_on(async move { client.get_bytecode_by_hash(hash).await }).unwrap();
let result = fork.fork_source.get_bytecode_by_hash(hash).unwrap();
mutator.factory_dep_cache.insert(hash, result.clone());
result
} else {
Expand All @@ -146,7 +142,7 @@ impl ForkStorage {
}
}

impl ReadStorage for ForkStorage {
impl<S: std::fmt::Debug + ForkSource> ReadStorage for ForkStorage<S> {
fn is_write_initial(&mut self, key: &StorageKey) -> bool {
let mut mutator = self.inner.write().unwrap();
mutator.raw_storage.is_write_initial(key)
Expand All @@ -161,7 +157,7 @@ impl ReadStorage for ForkStorage {
}
}

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

impl ForkStorage {
impl<S> ForkStorage<S> {
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 All @@ -187,11 +183,36 @@ impl ForkStorage {
}
}

/// Trait that provides necessary data when
/// forking a remote chain.
/// The method signatures are similar to methods from ETHNamespace and ZKNamespace.
pub trait ForkSource {
/// Returns the Storage value at a given index for given address.
fn get_storage_at(
&self,
address: Address,
idx: U256,
block: Option<BlockIdVariant>,
) -> eyre::Result<H256>;

/// Returns the bytecode stored under this hash (if available).
fn get_bytecode_by_hash(&self, hash: H256) -> eyre::Result<Option<Vec<u8>>>;
/// Returns the transaction for a given hash.
fn get_transaction_by_hash(&self, hash: H256) -> eyre::Result<Option<Transaction>>;

/// Gets all transactions that belong to a given miniblock.
fn get_raw_block_transactions(
&self,
block_number: MiniblockNumber,
) -> eyre::Result<Vec<zksync_types::Transaction>>;
}

/// Holds the information about the original chain.
/// "S" is the implementation of the ForkSource.
#[derive(Debug, Clone)]
pub struct ForkDetails {
// URL to the server.
pub fork_url: String,
pub struct ForkDetails<S> {
// Source of the fork data (for example HTTPForkSoruce)
pub fork_source: S,
// Block number at which we forked (the next block to create is l1_block + 1)
pub l1_block: L1BatchNumber,
pub l2_miniblock: u64,
Expand All @@ -200,7 +221,7 @@ pub struct ForkDetails {
pub l1_gas_price: u64,
}

impl ForkDetails {
impl ForkDetails<HttpForkSource> {
pub async fn from_url_and_miniblock_and_chain(
url: &str,
client: HttpClient,
Expand All @@ -217,19 +238,20 @@ impl ForkDetails {

println!(
"Creating fork from {:?} L1 block: {:?} L2 block: {:?} with timestamp {:?} and L1 gas price {:?}",
url, l1_batch_number, miniblock, block_details.timestamp, block_details.l1_gas_price,
url, l1_batch_number, miniblock, block_details.base.timestamp, block_details.base.l1_gas_price,
);

ForkDetails {
fork_url: url.to_owned(),
fork_source: HttpForkSource {
fork_url: url.to_owned(),
},
l1_block: l1_batch_number,
block_timestamp: block_details.timestamp,
block_timestamp: block_details.base.timestamp,
l2_miniblock: miniblock,
overwrite_chain_id: chain_id,
l1_gas_price: block_details.l1_gas_price,
l1_gas_price: block_details.base.l1_gas_price,
}
}

/// Create a fork from a given network at a given height.
pub async fn from_network(fork: &str, fork_at: Option<u64>) -> Self {
let (url, client) = Self::fork_to_url_and_client(fork);
Expand All @@ -253,7 +275,9 @@ impl ForkDetails {

Self::from_url_and_miniblock_and_chain(url, client, l2_miniblock, overwrite_chain_id).await
}
}

impl<S: ForkSource> ForkDetails<S> {
/// Return URL and HTTP client for a given fork name.
pub fn fork_to_url_and_client(fork: &str) -> (&str, HttpClient) {
let url = match fork {
Expand All @@ -271,18 +295,19 @@ impl ForkDetails {

/// Returns transactions that are in the same L2 miniblock as replay_tx, but were executed before it.
pub async fn get_earlier_transactions_in_same_block(&self, replay_tx: H256) -> Vec<L2Tx> {
let client = self.create_client();

let tx_details = client
let tx_details = self
.fork_source
.get_transaction_by_hash(replay_tx)
.await
.unwrap()
.unwrap();
let miniblock = MiniblockNumber(tx_details.block_number.unwrap().as_u32());

// And we're fetching all the transactions from this miniblock.
let block_transactions: Vec<zksync_types::Transaction> =
client.get_raw_block_transactions(miniblock).await.unwrap();
let block_transactions = self
.fork_source
.get_raw_block_transactions(miniblock)
.unwrap();

let mut tx_to_apply = Vec::new();

for tx in block_transactions {
Expand All @@ -299,10 +324,4 @@ impl ForkDetails {
replay_tx, miniblock
);
}

pub fn create_client(&self) -> HttpClient {
HttpClientBuilder::default()
.build(self.fork_url.clone())
.expect("Unable to create a client for fork")
}
}
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -136,7 +136,7 @@ pub fn print_call(call: &Call, padding: usize, show_calls: &ShowCalls, resolve_h
call.r#type,
address_to_human_readable(call.to)
.map(|x| format!("{:<52}", x))
.unwrap_or(format!("{:<52}", format!("{:?}", call.to).bold()).to_string()),
.unwrap_or(format!("{:<52}", format!("{:?}", call.to).bold())),
function_signature,
call.revert_reason
.as_ref()
Expand Down
62 changes: 62 additions & 0 deletions src/http_fork_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use eyre::Context;
use zksync_web3_decl::{
jsonrpsee::http_client::{HttpClient, HttpClientBuilder},
namespaces::{EthNamespaceClient, ZksNamespaceClient},
};

use crate::fork::{block_on, ForkSource};

#[derive(Debug)]
/// Fork source that gets the data via HTTP requests.
pub struct HttpForkSource {
/// URL for the network to fork.
pub fork_url: String,
}

impl HttpForkSource {
pub fn create_client(&self) -> HttpClient {
HttpClientBuilder::default()
.build(self.fork_url.clone())
.unwrap_or_else(|_| panic!("Unable to create a client for fork: {}", self.fork_url))
}
}

impl ForkSource for HttpForkSource {
fn get_storage_at(
&self,
address: zksync_basic_types::Address,
idx: zksync_basic_types::U256,
block: Option<zksync_types::api::BlockIdVariant>,
) -> eyre::Result<zksync_basic_types::H256> {
let client = self.create_client();
block_on(async move { client.get_storage_at(address, idx, block).await })
.wrap_err("fork http client failed")
}

fn get_bytecode_by_hash(
&self,
hash: zksync_basic_types::H256,
) -> eyre::Result<Option<Vec<u8>>> {
let client = self.create_client();
block_on(async move { client.get_bytecode_by_hash(hash).await })
.wrap_err("fork http client failed")
}

fn get_transaction_by_hash(
&self,
hash: zksync_basic_types::H256,
) -> eyre::Result<Option<zksync_types::api::Transaction>> {
let client = self.create_client();
block_on(async move { client.get_transaction_by_hash(hash).await })
.wrap_err("fork http client failed")
}

fn get_raw_block_transactions(
&self,
block_number: zksync_basic_types::MiniblockNumber,
) -> eyre::Result<Vec<zksync_types::Transaction>> {
let client = self.create_client();
block_on(async move { client.get_raw_block_transactions(block_number).await })
.wrap_err("fork http client failed")
}
}
Loading

0 comments on commit 2843348

Please sign in to comment.