Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test setup for era_vm #229

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62d4e8b
Add basic test setup for era_vm
MarcosNicolau Aug 13, 2024
fbcdde8
Add bootloader test
MarcosNicolau Aug 13, 2024
96e729f
Fix bootloader test
MarcosNicolau Aug 13, 2024
96dc0be
Adapt utils test functions to era_vm
MarcosNicolau Aug 13, 2024
7ebe3f6
Fix test util verify_required_storage
MarcosNicolau Aug 13, 2024
1d3f68c
Add upgrade tests
MarcosNicolau Aug 13, 2024
5465991
Add code_oracle tests
MarcosNicolau Aug 13, 2024
9601ca9
Add default_aa tests
MarcosNicolau Aug 13, 2024
8ac9e9a
Add gas_limit tests
MarcosNicolau Aug 13, 2024
0f66a1b
Add is_write_initial test
MarcosNicolau Aug 13, 2024
ec1df4a
Add read_word_from_bootloader_heap to vm
MarcosNicolau Aug 13, 2024
02b62ad
Add l1_tx_execution tests
MarcosNicolau Aug 13, 2024
65222fa
Add bytecode_publishing and transfer tests
MarcosNicolau Aug 13, 2024
0d780be
make and restore snapshots
juan518munoz Aug 13, 2024
c285a85
make and restore snapshots
juan518munoz Aug 13, 2024
b365644
add storage & tracing execution tests
juan518munoz Aug 13, 2024
69524ff
add rollbacks, sekp & simple_execution
juan518munoz Aug 13, 2024
8c74d62
Add nonce_holder & require_eip712 tests
MarcosNicolau Aug 13, 2024
5cae6ed
Run zk fmt
MarcosNicolau Aug 14, 2024
995eb86
Add refunds tests
MarcosNicolau Aug 14, 2024
c4590ba
fix test bytecode_publishing
juan518munoz Aug 14, 2024
12eae0a
Add get_used_contracts tests
MarcosNicolau Aug 14, 2024
a517830
Add l2_blocks tests
MarcosNicolau Aug 14, 2024
93ad045
Add todo!() to pubdatarequested hook
MarcosNicolau Aug 14, 2024
0e8e466
Add NearCallCatch hook
MarcosNicolau Aug 14, 2024
7066bdc
initial integration to world
juan518munoz Aug 15, 2024
7667e7a
re-enable tests
juan518munoz Aug 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,3 @@ zksync_contract_verification_server = { version = "0.1.0", path = "core/node/con
zksync_node_api_server = { version = "0.1.0", path = "core/node/api_server" }
zksync_tee_verifier_input_producer = { version = "0.1.0", path = "core/node/tee_verifier_input_producer" }
zksync_base_token_adjuster = { version = "0.1.0", path = "core/node/base_token_adjuster" }

[patch.crates-io]
iai = { git = "https://github.com/lambdaclass/iai", branch = "fix/valgrind-forward-compatibility" }
2 changes: 1 addition & 1 deletion core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ zk_evm_1_4_0.workspace = true
zk_evm_1_3_3.workspace = true
zk_evm_1_3_1.workspace = true
vm2.workspace = true
era_vm = {path = "../../../../"}
era_vm = { path = "../../../../era_vm" }
Copy link

@fkrause98 fkrause98 Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work when zksync-era is used as a submodule for era_vm, believe me I've tried. Please restore this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When developing, we can modify this as needed.

# era_vm = {git = "https://github.com/lambdaclass/era_vm.git", branch = "main"}

circuit_sequencer_api_1_3_3.workspace = true
Expand Down
26 changes: 26 additions & 0 deletions core/lib/multivm/src/versions/era_vm/glue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use zksync_types::l2_to_l1_log::{L2ToL1Log, SystemL2ToL1Log};
use zksync_utils::u256_to_h256;

use crate::glue::GlueFrom;

impl GlueFrom<&era_vm::state::L2ToL1Log> for SystemL2ToL1Log {
fn glue_from(value: &era_vm::state::L2ToL1Log) -> Self {
let era_vm::state::L2ToL1Log {
key,
value,
is_service,
address,
shard_id,
tx_number,
} = *value;

Self(L2ToL1Log {
shard_id,
is_service,
tx_number_in_block: tx_number,
sender: address,
key: u256_to_h256(key),
value: u256_to_h256(value),
})
}
}
4 changes: 4 additions & 0 deletions core/lib/multivm/src/versions/era_vm/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ pub(crate) enum Hook {
TxHasEnded,
DebugLog,
DebugReturnData,
NearCallCatch,
AskOperatorForRefund,
NotifyAboutRefund,
PostResult,
FinalBatchInfo,
PubdataRequested,
}

impl Hook {
Expand All @@ -23,10 +25,12 @@ impl Hook {
4 => Hook::TxHasEnded,
5 => Hook::DebugLog,
6 => Hook::DebugReturnData,
7 => Hook::NearCallCatch,
8 => Hook::AskOperatorForRefund,
9 => Hook::NotifyAboutRefund,
10 => Hook::PostResult,
11 => Hook::FinalBatchInfo,
12 => Hook::PubdataRequested,
_ => panic!("Unknown hook {}", hook),
}
}
Expand Down
3 changes: 3 additions & 0 deletions core/lib/multivm/src/versions/era_vm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub mod bootloader_state;
mod bytecode;
mod event;
mod glue;
mod hook;
mod initial_bootloader_memory;
mod snapshot;
#[cfg(test)]
mod tests;
mod transaction_data;
pub mod vm;
28 changes: 6 additions & 22 deletions core/lib/multivm/src/versions/era_vm/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
use era_vm::state::StateSnapshot;
use zksync_types::H256;

#[derive(Debug, Clone)]
pub(crate) struct BootloaderStateSnapshot {
/// ID of the next transaction to be executed.
pub(crate) tx_to_execute: usize,
/// Stored L2 blocks in bootloader memory
pub(crate) l2_blocks_len: usize,
/// Snapshot of the last L2 block. Only this block could be changed during the rollback
pub(crate) last_l2_block: L2BlockSnapshot,
/// The number of 32-byte words spent on the already included compressed bytecodes.
pub(crate) compressed_bytecodes_encoding: usize,
/// Current offset of the free space in the bootloader memory.
pub(crate) free_tx_offset: usize,
/// Whether the pubdata information has been provided already
pub(crate) is_pubdata_information_provided: bool,
}
use super::bootloader_state::BootloaderStateSnapshot;

#[derive(Debug, Clone)]
pub(crate) struct L2BlockSnapshot {
Expand All @@ -25,11 +12,8 @@ pub(crate) struct L2BlockSnapshot {
}

pub struct VmSnapshot {
execution: era_vm::execution::Execution,

// TODO: Implement snapshots in era vm
// world_snapshot: vm2::ExternalSnapshot,
bootloader_snapshot: BootloaderStateSnapshot,
suspended_at: u16,
gas_for_account_validation: u32,
pub execution: StateSnapshot,
pub bootloader_snapshot: BootloaderStateSnapshot,
pub suspended_at: u16,
pub gas_for_account_validation: u32,
}
52 changes: 52 additions & 0 deletions core/lib/multivm/src/versions/era_vm/tests/bootloader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use zksync_types::U256;

use crate::{
interface::{ExecutionResult, Halt, TxExecutionMode, VmExecutionMode, VmInterface},
versions::era_vm::tests::{
tester::VmTesterBuilder,
utils::{get_bootloader, verify_required_memory, BASE_SYSTEM_CONTRACTS},
},
};

#[test]
fn test_dummy_bootloader() {
let mut base_system_contracts = BASE_SYSTEM_CONTRACTS.clone();
base_system_contracts.bootloader = get_bootloader("dummy");

let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_base_system_smart_contracts(base_system_contracts)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.build();

let result = vm.vm.execute(VmExecutionMode::Batch);
assert!(!result.result.is_failed());

let correct_first_cell = U256::from_str_radix("123123123", 16).unwrap();

verify_required_memory(
&vm.vm.inner.execution,
vec![(correct_first_cell, vm2::FIRST_HEAP, 0)],
);
}

#[test]
fn test_bootloader_out_of_gas() {
let mut base_system_contracts = BASE_SYSTEM_CONTRACTS.clone();
base_system_contracts.bootloader = get_bootloader("dummy");

let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_base_system_smart_contracts(base_system_contracts)
.with_bootloader_gas_limit(10)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.build();

let res = vm.vm.execute(VmExecutionMode::Batch);
assert!(matches!(
res.result,
ExecutionResult::Halt {
reason: Halt::BootloaderOutOfGas
}
));
}
40 changes: 40 additions & 0 deletions core/lib/multivm/src/versions/era_vm/tests/bytecode_publishing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use zksync_types::event::extract_long_l2_to_l1_messages;
use zksync_utils::bytecode::compress_bytecode;

use crate::{
era_vm::tests::{
tester::{DeployContractsTx, TxType, VmTesterBuilder},
utils::read_test_contract,
},
interface::{TxExecutionMode, VmExecutionMode, VmInterface},
};

#[test]
fn test_bytecode_publishing() {
// In this test, we aim to ensure that the contents of the compressed bytecodes
// are included as part of the L2->L1 long messages
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.build();

let counter = read_test_contract();
let account = &mut vm.rich_accounts[0];

let compressed_bytecode = compress_bytecode(&counter).unwrap();

let DeployContractsTx { tx, .. } = account.get_deploy_tx(&counter, None, TxType::L2);
vm.vm.push_transaction(tx);
let result = vm.vm.execute(VmExecutionMode::OneTx);
assert!(!result.result.is_failed(), "Transaction wasn't successful");

vm.vm.execute(VmExecutionMode::Batch);

let state = vm.vm.get_current_execution_state();
let long_messages = extract_long_l2_to_l1_messages(&state.events);
assert!(
long_messages.contains(&compressed_bytecode),
"Bytecode not published"
);
}
Loading
Loading