Skip to content

Commit

Permalink
Adapt code for the latest zksync era changes
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo committed Nov 30, 2023
1 parent 533053e commit 594fb0c
Show file tree
Hide file tree
Showing 10 changed files with 1,505 additions and 619 deletions.
1,969 changes: 1,416 additions & 553 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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 = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_core = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
multivm = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_contracts = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_utils = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_state = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_web3_decl = { git = "https://github.com/matter-labs/zksync-era.git", rev = "80273264a9512bc1e6f1d1f4372107f9167260b1" }
zksync_basic_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_core = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
multivm = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_contracts = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_utils = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_state = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
zksync_web3_decl = { git = "https://github.com/matter-labs/zksync-era.git", rev = "bd268ac02bc3530c1d3247cb9496c3e13c2e52d9" }
sha3 = "0.10.6"


Expand Down
9 changes: 5 additions & 4 deletions src/bootloader_debug.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use multivm::interface::{dyn_tracers::vm_1_3_3::DynTracer, tracer::VmExecutionStopReason};
use std::sync::Arc;

use multivm::vm_latest::{
constants::BOOTLOADER_HEAP_PAGE, BootloaderState, DynTracer, HistoryMode, SimpleMemory,
VmExecutionStopReason, VmTracer, ZkSyncVmState,
use multivm::vm_refunds_enhancement::{
constants::BOOTLOADER_HEAP_PAGE, BootloaderState, HistoryMode, SimpleMemory, VmTracer,
ZkSyncVmState,
};
use once_cell::sync::OnceCell;
use zksync_basic_types::U256;
Expand Down Expand Up @@ -81,7 +82,7 @@ pub struct BootloaderDebugTracer {
pub result: Arc<OnceCell<eyre::Result<BootloaderDebug, String>>>,
}

impl<S, H: HistoryMode> DynTracer<S, H> for BootloaderDebugTracer {}
impl<S, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for BootloaderDebugTracer {}

fn load_debug_slot<H: HistoryMode>(memory: &SimpleMemory<H>, slot: usize) -> U256 {
memory
Expand Down
16 changes: 11 additions & 5 deletions src/deps/system_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ use serde_json::Value;
use zksync_basic_types::{AccountTreeId, Address};
use zksync_types::{
block::DeployedContract, ACCOUNT_CODE_STORAGE_ADDRESS, BOOTLOADER_ADDRESS,
BOOTLOADER_UTILITIES_ADDRESS, BYTECODE_COMPRESSOR_ADDRESS, CONTRACT_DEPLOYER_ADDRESS,
ECRECOVER_PRECOMPILE_ADDRESS, EVENT_WRITER_ADDRESS, IMMUTABLE_SIMULATOR_STORAGE_ADDRESS,
KECCAK256_PRECOMPILE_ADDRESS, KNOWN_CODES_STORAGE_ADDRESS, L1_MESSENGER_ADDRESS,
L2_ETH_TOKEN_ADDRESS, MSG_VALUE_SIMULATOR_ADDRESS, NONCE_HOLDER_ADDRESS,
SHA256_PRECOMPILE_ADDRESS, SYSTEM_CONTEXT_ADDRESS,
BOOTLOADER_UTILITIES_ADDRESS, CONTRACT_DEPLOYER_ADDRESS, ECRECOVER_PRECOMPILE_ADDRESS,
EVENT_WRITER_ADDRESS, H160, IMMUTABLE_SIMULATOR_STORAGE_ADDRESS, KECCAK256_PRECOMPILE_ADDRESS,
KNOWN_CODES_STORAGE_ADDRESS, L1_MESSENGER_ADDRESS, L2_ETH_TOKEN_ADDRESS,
MSG_VALUE_SIMULATOR_ADDRESS, NONCE_HOLDER_ADDRESS, SHA256_PRECOMPILE_ADDRESS,
SYSTEM_CONTEXT_ADDRESS,
};

// TODO remove it after moving to boojum
pub const BYTECODE_COMPRESSOR_ADDRESS: Address = H160([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x0e,
]);

pub fn bytecode_from_slice(artifact_name: &str, contents: &[u8]) -> Vec<u8> {
let artifact: Value = serde_json::from_slice(contents).expect(artifact_name);
let bytecode = artifact["bytecode"]
Expand Down
10 changes: 6 additions & 4 deletions src/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
};
use itertools::Itertools;
use jsonrpc_core::{BoxFuture, Result};
use multivm::vm_virtual_blocks::{constants::ETH_CALL_GAS_LIMIT, CallTracer, HistoryDisabled, Vm};
use multivm::vm_latest::HistoryDisabled;
use multivm::interface::VmInterface;
use multivm::vm_refunds_enhancement::{constants::ETH_CALL_GAS_LIMIT, CallTracer, Vm};
use once_cell::sync::OnceCell;
use std::sync::{Arc, RwLock};
use zksync_basic_types::H256;
Expand Down Expand Up @@ -180,7 +182,7 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> DebugNamespaceT
// update the enforced_base_fee within l1_batch_env to match the logic in zksync_core
l1_batch_env.enforced_base_fee = Some(l2_tx.common_data.fee.max_fee_per_gas.as_u64());
let system_env = inner.create_system_env(bootloader_code.clone(), execution_mode);
let mut vm = Vm::new(l1_batch_env, system_env, storage, HistoryDisabled);
let mut vm: Vm<_, HistoryDisabled> = Vm::new(l1_batch_env, system_env, storage);

// We must inject *some* signature (otherwise bootloader code fails to generate hash).
if l2_tx.common_data.signature.is_empty() {
Expand All @@ -197,9 +199,9 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> DebugNamespaceT
vm.push_transaction(tx);

let call_tracer_result = Arc::new(OnceCell::default());
let tracer = CallTracer::new(call_tracer_result.clone(), HistoryDisabled);
let tracer = CallTracer::new(call_tracer_result.clone()).into_tracer_pointer();
let tx_result = vm.inspect(
vec![Box::new(tracer)],
tracer.into(),
multivm::interface::VmExecutionMode::OneTx,
);

Expand Down
16 changes: 9 additions & 7 deletions src/node/debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use itertools::Itertools;
use multivm::vm_virtual_blocks::{constants::ETH_CALL_GAS_LIMIT, CallTracer, HistoryDisabled, Vm};
use once_cell::sync::OnceCell;
use std::sync::Arc;

use multivm::interface::VmInterface;
use multivm::tracers::CallTracer;
use multivm::vm_latest::HistoryDisabled;
use multivm::vm_refunds_enhancement::{constants::ETH_CALL_GAS_LIMIT, ToTracerPointer, Vm};

use zksync_basic_types::H256;
use zksync_core::api_server::web3::backend_jsonrpc::error::into_jsrpc_error;
use zksync_state::StorageView;
Expand Down Expand Up @@ -167,7 +172,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> DebugNames
// update the enforced_base_fee within l1_batch_env to match the logic in zksync_core
l1_batch_env.enforced_base_fee = Some(l2_tx.common_data.fee.max_fee_per_gas.as_u64());
let system_env = inner.create_system_env(bootloader_code.clone(), execution_mode);
let mut vm = Vm::new(l1_batch_env, system_env, storage, HistoryDisabled);
let mut vm: Vm<_, HistoryDisabled> = Vm::new(l1_batch_env, system_env, storage);

// We must inject *some* signature (otherwise bootloader code fails to generate hash).
if l2_tx.common_data.signature.is_empty() {
Expand All @@ -184,11 +189,8 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> DebugNames
vm.push_transaction(tx);

let call_tracer_result = Arc::new(OnceCell::default());
let tracer = CallTracer::new(call_tracer_result.clone(), HistoryDisabled);
let tx_result = vm.inspect(
vec![Box::new(tracer)],
multivm::interface::VmExecutionMode::OneTx,
);
let tracer = CallTracer::new(call_tracer_result.clone()).into_tracer_pointer();
let tx_result = vm.inspect(tracer.into(), multivm::interface::VmExecutionMode::OneTx);

let call_traces = if only_top {
vec![]
Expand Down
6 changes: 3 additions & 3 deletions src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use colored::Colorize;
use futures::FutureExt;
use itertools::Itertools;
use multivm::interface::{ExecutionResult, TxExecutionMode};
use multivm::vm_virtual_blocks::constants::ETH_CALL_GAS_LIMIT;
use multivm::vm_refunds_enhancement::constants::ETH_CALL_GAS_LIMIT;
use zksync_basic_types::{web3, AccountTreeId, Address, Bytes, H160, H256, U256, U64};
use zksync_core::api_server::web3::backend_jsonrpc::error::into_jsrpc_error;
use zksync_state::ReadStorage;
Expand All @@ -17,7 +17,7 @@ use zksync_types::{
utils::storage_key_for_standard_token_balance,
StorageKey, L2_ETH_TOKEN_ADDRESS,
};
use zksync_utils::{h256_to_u256, h256_to_u64, u256_to_h256};
use zksync_utils::{h256_to_u256, u256_to_h256};
use zksync_web3_decl::{
error::Web3Error,
types::{FeeHistory, Filter, FilterChanges, SyncState},
Expand All @@ -28,7 +28,7 @@ use crate::{
fork::ForkSource,
namespaces::{EthNamespaceT, RpcResult},
node::{InMemoryNode, TransactionResult, L2_GAS_PRICE, MAX_TX_SIZE, PROTOCOL_VERSION},
utils::{self, not_implemented, IntoBoxedFuture},
utils::{self, h256_to_u64, not_implemented, IntoBoxedFuture},
};

impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> EthNamespaceT
Expand Down
54 changes: 25 additions & 29 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ use std::{

use multivm::interface::{
ExecutionResult, L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode,
VmExecutionResultAndLogs,
VmExecutionResultAndLogs, VmInterface,
};
use multivm::vm_latest::{
constants::{BLOCK_GAS_LIMIT, BLOCK_OVERHEAD_PUBDATA, MAX_PUBDATA_PER_BLOCK},
utils::{
fee::derive_base_fee_and_gas_per_pubdata,
l2_blocks::load_last_l2_block,
overhead::{derive_overhead, OverheadCoeficients},
use multivm::{
tracers::CallTracer,
vm_latest::HistoryDisabled,
vm_refunds_enhancement::{
constants::{BLOCK_GAS_LIMIT, BLOCK_OVERHEAD_PUBDATA, MAX_PUBDATA_PER_BLOCK},
utils::{
fee::derive_base_fee_and_gas_per_pubdata,
l2_blocks::load_last_l2_block,
overhead::{derive_overhead, OverheadCoefficients},
},
ToTracerPointer, Vm, VmTracer,

Check failure on line 41 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `VmTracer`

Check failure on line 41 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

unused import: `VmTracer`

Check failure on line 41 in src/node/in_memory.rs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04-github-hosted-16core)

unused import: `VmTracer`
},
CallTracer, HistoryDisabled, Vm, VmTracer,
};
use zksync_basic_types::{
web3::signing::keccak256, Address, Bytes, L1BatchNumber, MiniblockNumber, H160, H256, U256, U64,
Expand Down Expand Up @@ -556,7 +560,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
&self.fork_storage,
);

let coefficients = OverheadCoeficients::from_tx_type(EIP_712_TX_TYPE);
let coefficients = OverheadCoefficients::from_tx_type(EIP_712_TX_TYPE);
let overhead: u32 = derive_overhead(
suggested_gas_limit,
gas_per_pubdata_byte as u32,
Expand Down Expand Up @@ -679,7 +683,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
let l1_gas_price =
adjust_l1_gas_price_for_tx(l1_gas_price, L2_GAS_PRICE, tx.gas_per_pubdata_byte_limit());

let coefficients = OverheadCoeficients::from_tx_type(EIP_712_TX_TYPE);
let coefficients = OverheadCoefficients::from_tx_type(EIP_712_TX_TYPE);
// Set gas_limit for transaction
let gas_limit_with_overhead = tx_gas_limit
+ derive_overhead(
Expand Down Expand Up @@ -714,7 +718,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {

batch_env.l1_gas_price = l1_gas_price;

let mut vm = Vm::new(batch_env, system_env, storage, HistoryDisabled);
let mut vm: Vm<_, HistoryDisabled> = Vm::new(batch_env, system_env, storage.clone());

let tx: Transaction = l2_tx.into();
vm.push_transaction(tx);
Expand Down Expand Up @@ -1007,7 +1011,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
let (batch_env, _) = inner.create_l1_batch_env(storage.clone());
let system_env = inner.create_system_env(bootloader_code.clone(), execution_mode);

let mut vm = Vm::new(batch_env, system_env, storage, HistoryDisabled);
let mut vm: Vm<_, HistoryDisabled> = Vm::new(batch_env, system_env, storage.clone());

// We must inject *some* signature (otherwise bootloader code fails to generate hash).
if l2_tx.common_data.signature.is_empty() {
Expand All @@ -1019,13 +1023,9 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {

let call_tracer_result = Arc::new(OnceCell::default());

let custom_tracers =
vec![
Box::new(CallTracer::new(call_tracer_result.clone(), HistoryDisabled))
as Box<dyn VmTracer<StorageView<&ForkStorage<S>>, HistoryDisabled>>,
];
let custom_tracer = CallTracer::new(call_tracer_result.clone()).into_tracer_pointer();

let tx_result = vm.inspect(custom_tracers, VmExecutionMode::OneTx);
let tx_result = vm.inspect(custom_tracer.into(), VmExecutionMode::OneTx);

let call_traces = Arc::try_unwrap(call_tracer_result)
.unwrap()
Expand Down Expand Up @@ -1306,12 +1306,8 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
};
let system_env = inner.create_system_env(bootloader_code.clone(), execution_mode);

let mut vm = Vm::new(
batch_env.clone(),
system_env,
storage.clone(),
HistoryDisabled,
);
let mut vm: Vm<_, HistoryDisabled> =
Vm::new(batch_env.clone(), system_env, storage.clone());

let tx: Transaction = l2_tx.clone().into();

Expand All @@ -1321,14 +1317,14 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
let bootloader_debug_result = Arc::new(OnceCell::default());

let custom_tracers = vec![
Box::new(CallTracer::new(call_tracer_result.clone(), HistoryDisabled))
as Box<dyn VmTracer<StorageView<&ForkStorage<S>>, HistoryDisabled>>,
Box::new(BootloaderDebugTracer {
CallTracer::new(call_tracer_result.clone()).into_tracer_pointer(),
BootloaderDebugTracer {
result: bootloader_debug_result.clone(),
}) as Box<dyn VmTracer<StorageView<&ForkStorage<S>>, HistoryDisabled>>,
}
.into_tracer_pointer(),
];

let tx_result = vm.inspect(custom_tracers, VmExecutionMode::OneTx);
let tx_result = vm.inspect(custom_tracers.into(), VmExecutionMode::OneTx);

let call_traces = call_tracer_result.get().unwrap();

Expand Down
12 changes: 10 additions & 2 deletions src/node/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::collections::HashMap;
use bigdecimal::BigDecimal;
use colored::Colorize;
use futures::FutureExt;
use zksync_basic_types::{AccountTreeId, Address, L1BatchNumber, MiniblockNumber, U256};
use zksync_basic_types::{AccountTreeId, Address, L1BatchNumber, MiniblockNumber, H256, U256};
use zksync_core::api_server::web3::backend_jsonrpc::error::{internal_error, into_jsrpc_error};
use zksync_state::ReadStorage;
use zksync_types::{
api::{
BlockDetails, BlockDetailsBase, BlockStatus, BridgeAddresses, ProtocolVersion,
BlockDetails, BlockDetailsBase, BlockStatus, BridgeAddresses, Proof, ProtocolVersion,
TransactionDetails, TransactionStatus, TransactionVariant,
},
fee::Fee,
Expand Down Expand Up @@ -120,6 +120,14 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> ZksNamespa
Ok(transactions)
})
}
fn get_proof(
&self,
address: Address,

Check failure on line 125 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `address`

Check failure on line 125 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

unused variable: `address`

Check failure on line 125 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04-github-hosted-16core)

unused variable: `address`
keys: Vec<H256>,

Check failure on line 126 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `keys`

Check failure on line 126 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

unused variable: `keys`

Check failure on line 126 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04-github-hosted-16core)

unused variable: `keys`
l1_batch_number: L1BatchNumber,

Check failure on line 127 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / lint

unused variable: `l1_batch_number`

Check failure on line 127 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

unused variable: `l1_batch_number`

Check failure on line 127 in src/node/zks.rs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04-github-hosted-16core)

unused variable: `l1_batch_number`
) -> RpcResult<Proof> {
not_implemented("zks_getProof")
}

fn estimate_gas_l1_to_l2(
&self,
Expand Down
16 changes: 12 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::pin::Pin;

use chrono::{DateTime, Utc};
use futures::Future;
use multivm::interface::{ExecutionResult, VmExecutionResultAndLogs};
use multivm::vm_latest::{utils::fee::derive_base_fee_and_gas_per_pubdata, HistoryDisabled, Vm};
use zksync_basic_types::{U256, U64};
use multivm::interface::{ExecutionResult, VmExecutionResultAndLogs, VmInterface};
use multivm::vm_latest::HistoryDisabled;
use multivm::vm_refunds_enhancement::{utils::fee::derive_base_fee_and_gas_per_pubdata, Vm};
use zksync_basic_types::{H256, U256, U64};
use zksync_state::StorageView;
use zksync_state::WriteStorage;
use zksync_types::api::{BlockNumber, DebugCall, DebugCallType};
Expand Down Expand Up @@ -122,7 +124,7 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
multivm::interface::TxExecutionMode::VerifyExecute,
);

let mut vm = Vm::new(batch_env, system_env, storage.clone(), HistoryDisabled);
let mut vm: Vm<_, HistoryDisabled> = Vm::new(batch_env, system_env, storage.clone());

vm.execute(multivm::interface::VmExecutionMode::Bootloader);

Expand Down Expand Up @@ -458,3 +460,9 @@ mod tests {
}
}
}

/// Converts `h256` value as BE into the u64
pub fn h256_to_u64(value: H256) -> u64 {
let be_u64_bytes: [u8; 8] = value[24..].try_into().unwrap();
u64::from_be_bytes(be_u64_bytes)
}

0 comments on commit 594fb0c

Please sign in to comment.