Skip to content

Commit

Permalink
rework pubdata approach
Browse files Browse the repository at this point in the history
  • Loading branch information
perekopskiy committed Oct 8, 2024
1 parent 847c3da commit f9e4c90
Show file tree
Hide file tree
Showing 72 changed files with 1,097 additions and 701 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

32 changes: 22 additions & 10 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use zksync_contracts::{
};
use zksync_multivm::{
interface::{
pubdata::pubdata_params_to_builder,
storage::{InMemoryStorage, StorageView, WriteStorage},
tracer::VmExecutionStopReason,
L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode, VmFactory,
Expand All @@ -21,11 +22,12 @@ use zksync_multivm::{
zk_evm_latest::aux_structures::Timestamp,
};
use zksync_types::{
block::L2BlockHasher, ethabi::Token, fee::Fee, fee_model::BatchFeeInput, l1::L1Tx, l2::L2Tx,
utils::storage_key_for_eth_balance, AccountTreeId, Address, Execute, K256PrivateKey,
L1BatchNumber, L1TxCommonData, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId, StorageKey,
Transaction, BOOTLOADER_ADDRESS, SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_GAS_PRICE_POSITION,
SYSTEM_CONTEXT_TX_ORIGIN_POSITION, U256, ZKPORTER_IS_AVAILABLE,
block::L2BlockHasher, commitment::PubdataParams, ethabi::Token, fee::Fee,
fee_model::BatchFeeInput, l1::L1Tx, l2::L2Tx, utils::storage_key_for_eth_balance,
AccountTreeId, Address, Execute, K256PrivateKey, L1BatchNumber, L1TxCommonData, L2BlockNumber,
L2ChainId, Nonce, ProtocolVersionId, StorageKey, Transaction, BOOTLOADER_ADDRESS,
SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_GAS_PRICE_POSITION, SYSTEM_CONTEXT_TX_ORIGIN_POSITION,
U256, ZKPORTER_IS_AVAILABLE,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, u256_to_h256};

Expand Down Expand Up @@ -231,7 +233,6 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
execution_mode: TxExecutionMode::VerifyExecute,
default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
chain_id: L2ChainId::default(),
pubdata_params: Default::default(),
};

let eth_token_sys_contract = load_sys_contract("L2BaseToken");
Expand Down Expand Up @@ -262,7 +263,14 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
output: tracer_result.clone(),
}
.into_tracer_pointer();
let mut vm: Vm<_, HistoryEnabled> = Vm::new(l1_batch, system_env, storage_view.to_rc_ptr());

let pubdata_builder = pubdata_params_to_builder(PubdataParams::default());
let mut vm: Vm<_, HistoryEnabled> = Vm::new(
l1_batch,
system_env,
storage_view.to_rc_ptr(),
Some(pubdata_builder),
);
let result = vm.inspect(&mut tracer.into(), VmExecutionMode::Bootloader);

assert!(!result.result.is_failed(), "The internal call has reverted");
Expand Down Expand Up @@ -314,11 +322,15 @@ pub(super) fn execute_user_txs_in_test_gas_vm(
execution_mode: TxExecutionMode::VerifyExecute,
default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
chain_id: L2ChainId::default(),
pubdata_params: Default::default(),
};
let pubdata_builder = pubdata_params_to_builder(PubdataParams::default());

let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));
let mut vm: Vm<_, HistoryEnabled> = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
Some(pubdata_builder),
);

let mut total_gas_refunded = 0;
for tx in txs {
Expand Down
1 change: 0 additions & 1 deletion core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ zksync_types.workspace = true
zksync_contracts.workspace = true
zksync_utils.workspace = true
zksync_system_constants.workspace = true
zksync_mini_merkle_tree.workspace = true
zksync_vm_interface.workspace = true

anyhow.workspace = true
Expand Down
56 changes: 4 additions & 52 deletions core/lib/multivm/src/utils/events.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,10 @@
use zksync_system_constants::L1_MESSENGER_ADDRESS;
use zksync_types::{
ethabi::{self, Token},
l2_to_l1_log::L2ToL1Log,
Address, H256, U256,
H256, U256,
};
use zksync_utils::{u256_to_bytes_be, u256_to_h256};

use crate::interface::VmEvent;

/// Corresponds to the following solidity event:
/// ```solidity
/// struct L2ToL1Log {
/// uint8 l2ShardId;
/// bool isService;
/// uint16 txNumberInBlock;
/// address sender;
/// bytes32 key;
/// bytes32 value;
/// }
/// ```
#[derive(Debug, Default, Clone, PartialEq)]
pub(crate) struct L1MessengerL2ToL1Log {
pub l2_shard_id: u8,
pub is_service: bool,
pub tx_number_in_block: u16,
pub sender: Address,
pub key: U256,
pub value: U256,
}

impl L1MessengerL2ToL1Log {
pub fn packed_encoding(&self) -> Vec<u8> {
let mut res: Vec<u8> = vec![];
res.push(self.l2_shard_id);
res.push(self.is_service as u8);
res.extend_from_slice(&self.tx_number_in_block.to_be_bytes());
res.extend_from_slice(self.sender.as_bytes());
res.extend(u256_to_bytes_be(&self.key));
res.extend(u256_to_bytes_be(&self.value));
res
}
}

impl From<L1MessengerL2ToL1Log> for L2ToL1Log {
fn from(log: L1MessengerL2ToL1Log) -> Self {
L2ToL1Log {
shard_id: log.l2_shard_id,
is_service: log.is_service,
tx_number_in_block: log.tx_number_in_block,
sender: log.sender,
key: u256_to_h256(log.key),
value: u256_to_h256(log.value),
}
}
}
use crate::interface::{pubdata::L1MessengerL2ToL1Log, VmEvent};

#[derive(Debug, PartialEq)]
pub(crate) struct L1MessengerBytecodePublicationRequest {
Expand Down Expand Up @@ -142,7 +93,8 @@ mod tests {
use zksync_system_constants::{
BOOTLOADER_ADDRESS, KNOWN_CODES_STORAGE_ADDRESS, L2_BASE_TOKEN_ADDRESS,
};
use zksync_types::L1BatchNumber;
use zksync_types::{Address, L1BatchNumber};
use zksync_utils::u256_to_h256;

use super::*;

Expand Down
13 changes: 11 additions & 2 deletions core/lib/multivm/src/versions/testonly.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use zksync_contracts::BaseSystemContracts;
use zksync_test_account::Account;
use zksync_types::{
Expand All @@ -8,10 +10,18 @@ use zksync_types::{
use zksync_utils::{bytecode::hash_bytecode, u256_to_h256};

use crate::{
interface::{storage::InMemoryStorage, L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode},
interface::{
pubdata::{rollup::RollupPubdataBuilder, PubdataBuilder},
storage::InMemoryStorage,
L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode,
},
vm_latest::constants::BATCH_COMPUTATIONAL_GAS_LIMIT,
};

pub(super) fn default_pubdata_builder() -> Rc<dyn PubdataBuilder> {
Rc::new(RollupPubdataBuilder::new(Address::zero()))
}

pub(super) fn default_system_env() -> SystemEnv {
SystemEnv {
zk_porter_available: false,
Expand All @@ -21,7 +31,6 @@ pub(super) fn default_system_env() -> SystemEnv {
execution_mode: TxExecutionMode::VerifyExecute,
default_validation_computational_gas_limit: BATCH_COMPUTATIONAL_GAS_LIMIT,
chain_id: L2ChainId::from(270),
pubdata_params: Default::default(),
}
}

Expand Down
29 changes: 17 additions & 12 deletions core/lib/multivm/src/versions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use crate::{
},
utils::get_max_gas_per_pubdata_byte,
versions::testonly::{
default_l1_batch, default_system_env, make_account_rich, ContractToDeploy,
default_l1_batch, default_pubdata_builder, default_system_env, make_account_rich,
ContractToDeploy,
},
vm_fast,
vm_latest::{self, HistoryEnabled},
Expand Down Expand Up @@ -206,12 +207,13 @@ where
{
let system_env = default_system_env();
let l1_batch_env = default_l1_batch(L1BatchNumber(1));
let pubdata_builder = default_pubdata_builder();
let mut storage = InMemoryStorage::with_system_contracts(hash_bytecode);
let mut harness = Harness::new(&l1_batch_env);
harness.setup_storage(&mut storage);

let storage = StorageView::new(storage).to_rc_ptr();
let mut vm = Vm::new(l1_batch_env, system_env, storage);
let mut vm = Vm::new(l1_batch_env, system_env, storage, Some(pubdata_builder));
harness.execute_on_vm(&mut vm);
(vm, harness)
}
Expand All @@ -230,6 +232,7 @@ fn sanity_check_harness_on_new_vm() {
fn sanity_check_shadow_vm() {
let system_env = default_system_env();
let l1_batch_env = default_l1_batch(L1BatchNumber(1));
let pubdata_builder = default_pubdata_builder();
let mut storage = InMemoryStorage::with_system_contracts(hash_bytecode);
let mut harness = Harness::new(&l1_batch_env);
harness.setup_storage(&mut storage);
Expand All @@ -242,6 +245,7 @@ fn sanity_check_shadow_vm() {
system_env,
main_storage,
shadow_storage,
Some(pubdata_builder),
);
harness.execute_on_vm(&mut vm);
}
Expand All @@ -261,16 +265,17 @@ fn shadow_vm_basics() {
harness.setup_storage(&mut storage);
let storage = StorageView::new(storage).to_rc_ptr();

let vm = dump
.clone()
.play_back_custom(|l1_batch_env, system_env, dump_storage| {
ShadowVm::<_, ReferenceVm, ReferenceVm<_>>::with_custom_shadow(
l1_batch_env,
system_env,
storage,
dump_storage,
)
});
let vm =
dump.clone()
.play_back_custom(|l1_batch_env, system_env, dump_storage, pubdata_builder| {
ShadowVm::<_, ReferenceVm, ReferenceVm<_>>::with_custom_shadow(
l1_batch_env,
system_env,
storage,
dump_storage,
pubdata_builder,
)
});
let new_dump = vm.dump_state();
pretty_assertions::assert_eq!(new_dump, dump);
}
10 changes: 8 additions & 2 deletions core/lib/multivm/src/versions/vm_1_3_2/vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashSet;
use std::{collections::HashSet, rc::Rc};

use zksync_types::Transaction;
use zksync_utils::{bytecode::hash_bytecode, h256_to_u256};
use zksync_vm_interface::pubdata::PubdataBuilder;

use crate::{
glue::{history_mode::HistoryMode, GlueInto},
Expand Down Expand Up @@ -187,7 +188,12 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}

impl<S: WriteStorage, H: HistoryMode> VmFactory<S> for Vm<S, H> {
fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
fn new(
batch_env: L1BatchEnv,
system_env: SystemEnv,
storage: StoragePtr<S>,
_pubdata_builder: Option<Rc<dyn PubdataBuilder>>,
) -> Self {
let oracle_tools = crate::vm_1_3_2::OracleTools::new(storage.clone());
let block_properties = crate::vm_1_3_2::BlockProperties {
default_aa_code_hash: h256_to_u256(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use zksync_utils::{h256_to_u256, u256_to_bytes_be, u256_to_h256};

use crate::{
interface::{
pubdata::L1MessengerL2ToL1Log,
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
L1BatchEnv, VmEvent, VmExecutionMode,
},
tracers::dynamic::vm_1_4_1::DynTracer,
utils::events::{
extract_bytecode_publication_requests_from_l1_messenger,
extract_l2tol1logs_from_l1_messenger, L1MessengerL2ToL1Log,
extract_l2tol1logs_from_l1_messenger,
},
vm_1_4_1::{
bootloader_state::{utils::apply_pubdata_to_memory, BootloaderState},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zksync_types::writes::{compress_state_diffs, StateDiffRecord};

use crate::utils::events::L1MessengerL2ToL1Log;
use crate::interface::pubdata::L1MessengerL2ToL1Log;

/// Struct based on which the pubdata blob is formed
#[derive(Debug, Clone, Default)]
Expand Down
10 changes: 9 additions & 1 deletion core/lib/multivm/src/versions/vm_1_4_1/vm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::rc::Rc;

use circuit_sequencer_api_1_4_1::sort_storage_access::sort_storage_access_queries;
use zksync_types::{
l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log},
Transaction,
};
use zksync_vm_interface::pubdata::PubdataBuilder;

use crate::{
glue::GlueInto,
Expand Down Expand Up @@ -148,7 +151,12 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}

impl<S: WriteStorage, H: HistoryMode> VmFactory<S> for Vm<S, H> {
fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
fn new(
batch_env: L1BatchEnv,
system_env: SystemEnv,
storage: StoragePtr<S>,
_pubdata_builder: Option<Rc<dyn PubdataBuilder>>,
) -> Self {
let (state, bootloader_state) = new_vm_state(storage.clone(), &system_env, &batch_env);
Self {
bootloader_state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use zksync_utils::{h256_to_u256, u256_to_bytes_be, u256_to_h256};

use crate::{
interface::{
pubdata::L1MessengerL2ToL1Log,
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
L1BatchEnv, VmEvent, VmExecutionMode,
},
tracers::dynamic::vm_1_4_1::DynTracer,
utils::events::{
extract_bytecode_publication_requests_from_l1_messenger,
extract_l2tol1logs_from_l1_messenger, L1MessengerL2ToL1Log,
extract_l2tol1logs_from_l1_messenger,
},
vm_1_4_2::{
bootloader_state::{utils::apply_pubdata_to_memory, BootloaderState},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zksync_types::writes::{compress_state_diffs, StateDiffRecord};

use crate::utils::events::L1MessengerL2ToL1Log;
use crate::interface::pubdata::L1MessengerL2ToL1Log;

/// Struct based on which the pubdata blob is formed
#[derive(Debug, Clone, Default)]
Expand Down
10 changes: 8 additions & 2 deletions core/lib/multivm/src/versions/vm_1_4_2/vm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::mem;
use std::{mem, rc::Rc};

use circuit_sequencer_api_1_4_2::sort_storage_access::sort_storage_access_queries;
use zksync_types::{
l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log},
Transaction,
};
use zksync_vm_interface::pubdata::PubdataBuilder;

use crate::{
glue::GlueInto,
Expand Down Expand Up @@ -155,7 +156,12 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}

impl<S: WriteStorage, H: HistoryMode> VmFactory<S> for Vm<S, H> {
fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
fn new(
batch_env: L1BatchEnv,
system_env: SystemEnv,
storage: StoragePtr<S>,
_pubdata_builder: Option<Rc<dyn PubdataBuilder>>,
) -> Self {
let (state, bootloader_state) = new_vm_state(storage.clone(), &system_env, &batch_env);
Self {
bootloader_state,
Expand Down
Loading

0 comments on commit f9e4c90

Please sign in to comment.