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

fix: adapt for the new version of EVM emulator #116

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
27e6221
Adapt for optional evm emulation
0xVolosnikov Oct 22, 2024
b55cb60
Update contracts
0xVolosnikov Oct 29, 2024
5c26c47
feat: tweak LLVM options for the latest version of EvmEmulator
vladimirradosavljevic Oct 29, 2024
72a598d
try starting EVM nonce from 0
hedgar2017 Oct 31, 2024
d684cb2
Revert "try starting EVM nonce from 0"
0xVolosnikov Nov 15, 2024
4c6cd86
Pin contracts
0xVolosnikov Nov 18, 2024
4d1b61f
Pin modified zk_evm version
0xVolosnikov Nov 18, 2024
f0ef5c5
Update contracts
0xVolosnikov Nov 19, 2024
a05dcac
Skip broken test
0xVolosnikov Nov 19, 2024
385d478
Disable test failing due to out-of-ergs
0xVolosnikov Nov 20, 2024
00de505
Fix issue with deployment nonce
0xVolosnikov Nov 20, 2024
945475e
Update contracts
0xVolosnikov Nov 20, 2024
f4877ed
Cargo fmt
0xVolosnikov Nov 20, 2024
06e5031
Increment deployment nonce for all callers
0xVolosnikov Nov 20, 2024
1d0fbe4
Increment initial deployment nonce only for EVM target
0xVolosnikov Nov 20, 2024
d7c0207
Change zk_evm version
0xVolosnikov Nov 21, 2024
eb5630f
Update contracts
0xVolosnikov Nov 27, 2024
70c3b96
Update contracts
0xVolosnikov Dec 3, 2024
85f574b
Rebase artifact
0xVolosnikov Dec 3, 2024
03d2739
Update LLVM.lock
0xVolosnikov Dec 3, 2024
fffac4c
Update contracts and adapt EVM deploy flow
0xVolosnikov Dec 9, 2024
bacd5b8
Update contracts
0xVolosnikov Dec 16, 2024
f2503b4
fix: remove repeats for RETURN-like EVM instructions
hedgar2017 Dec 19, 2024
ef03e6e
merge main
hedgar2017 Dec 19, 2024
a82a330
update system-contracts-stable-build
hedgar2017 Dec 19, 2024
93f7e3b
Update contracts
0xVolosnikov Jan 2, 2025
e4389de
Use Identity precompile
0xVolosnikov Jan 2, 2025
45de85f
Merge branch 'main' into vv-optional-evm-emulation
0xVolosnikov Jan 2, 2025
7c722d6
Temporary pin different contracts branch
0xVolosnikov Jan 2, 2025
427707b
Update contracts
0xVolosnikov Jan 3, 2025
2aa9120
Update contracts
0xVolosnikov Jan 3, 2025
504012c
Merge branch 'main' into vv-optional-evm-emulation
0xVolosnikov Jan 10, 2025
cb5502e
Merge branch 'main' into vv-optional-evm-emulation
hedgar2017 Jan 20, 2025
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
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
url = https://github.com/ethereum/solidity
branch = develop
[submodule "era-contracts"]
path = era-contracts
url = https://github.com/matter-labs/era-contracts
branch = stable/evm-emulator
path = era-contracts
url = https://github.com/matter-labs/era-contracts
branch = evm-emulator/fixes
51 changes: 39 additions & 12 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion compiler_tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ evm = { git = "https://github.com/rust-ethereum/evm", rev = "f7a23df6c478ca6a151
revm = { git = "https://github.com/bluealloy/revm", rev = "fa5650ee8a4d802f4f3557014dd157adfb074460" }

zkevm_opcode_defs = "=0.150.6"
zkevm_tester = { git = "https://github.com/matter-labs/era-zkevm_tester", branch = "v1.5.0" }
zkevm_tester = { git = "https://github.com/0xVolosnikov/era-zkevm_tester", rev = "cbade136a9060abd8ec29e06ed6c1bd14dec4ff6" }
vm2 = { git = "https://github.com/matter-labs/vm2", optional = true, package = "zksync_vm2" }

era-compiler-common = { git = "https://github.com/matter-labs/era-compiler-common", branch = "main" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl EVMContract {
pub fn runtime_code(&self, instruction_name: &str) -> String {
let repeats = match instruction_name {
"RETURNDATASIZE" | "RETURNDATACOPY" | "EXTCODESIZE" | "EXTCODEHASH" | "EXTCODECOPY"
| "CALL" | "STATICCALL" | "DELEGATECALL" | "CREATE" | "CREATE2" => 1,
| "CALL" | "STATICCALL" | "DELEGATECALL" | "CREATE" | "CREATE2" | "RETURN"
| "REVERT" | "STOP" | "INVALID" => 1,
_ => Self::RUNTIME_CODE_REPEATS,
};

Expand Down
16 changes: 14 additions & 2 deletions compiler_tester/src/vm/eravm/deployers/system_contract_deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,25 @@ impl EraVMDeployer for SystemContractDeployer {
calldata.extend(deploy_code);
calldata.extend(constructor_calldata);

vm.execute::<M>(
let mut result = vm.execute::<M>(
test_name,
entry_address,
caller,
Some(context_u128_value),
calldata,
Some(vm_launch_option),
)
)?;

if result.output.return_data.len() > 1 {
let gas_left = result
.output
.return_data
.remove(0)
.unwrap_certain_as_ref()
.as_u64();
result.gas = EraVM::EVM_CALL_GAS_LIMIT - gas_left;
}

Ok(result)
}
}
47 changes: 46 additions & 1 deletion compiler_tester/src/vm/eravm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use std::time::Instant;

use colored::Colorize;
use solidity_adapter::EVMVersion;
use zkevm_opcode_defs::ADDRESS_CONTRACT_DEPLOYER;

use crate::utils;
use crate::vm::execution_result::ExecutionResult;

use self::system_context::SystemContext;
Expand Down Expand Up @@ -49,6 +51,8 @@ pub struct EraVM {
storage_transient: HashMap<zkevm_tester::compiler_tests::StorageKey, web3::types::H256>,
/// The current EVM block number.
current_evm_block_number: u128,
/// The target instruction set.
target: era_compiler_common::Target,
}

impl EraVM {
Expand All @@ -59,6 +63,9 @@ impl EraVM {
/// The extra amount of gas consumed by every call to the EVM interpreter.
pub const EVM_INTERPRETER_GAS_OVERHEAD: u64 = 2500;

/// The `allowedBytecodesToDeploy` variable storage slot in the `ContractDeployer` contract.
pub const CONTRACT_DEPLOYER_ALLOWED_BYTECODES_MODE_SLOT: u64 = 1;

/// The `passGas` variable transient storage slot in the `EvmGasManager` contract.
pub const EVM_GAS_MANAGER_GAS_TRANSIENT_SLOT: u64 = 4;

Expand Down Expand Up @@ -122,9 +129,18 @@ impl EraVM {
system_contracts_save_path,
)?;

let storage = SystemContext::create_storage(target);
let mut storage = SystemContext::create_storage(target);
let storage_transient = HashMap::new();

// TODO move to the SystemContext after EVM emulator is ready
storage.insert(
zkevm_tester::compiler_tests::StorageKey {
address: web3::types::Address::from_low_u64_be(ADDRESS_CONTRACT_DEPLOYER.into()),
key: web3::types::U256::from(Self::CONTRACT_DEPLOYER_ALLOWED_BYTECODES_MODE_SLOT),
},
web3::types::H256::from_low_u64_be(1), // Allow EVM contracts deployment
);

let mut vm = Self {
known_contracts: HashMap::new(),
default_aa_code_hash: web3::types::U256::from_big_endian(
Expand All @@ -146,6 +162,7 @@ impl EraVM {
storage_transient,
published_evm_bytecodes: HashMap::new(),
current_evm_block_number: SystemContext::INITIAL_BLOCK_NUMBER,
target,
};

vm.add_known_contract(
Expand Down Expand Up @@ -340,6 +357,34 @@ impl EraVM {
0,
);

if self.target == era_compiler_common::Target::EVM {
// Increase deployment nonce of caller by 1 if it is 0 (we pretend that it is a contract)
let address_h256 = utils::address_to_h256(&caller);
let bytes = [
address_h256.as_bytes(),
&[0; era_compiler_common::BYTE_LENGTH_FIELD],
]
.concat();
let key = web3::signing::keccak256(&bytes).into();
let storage_key = zkevm_tester::compiler_tests::StorageKey {
address: web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_NONCE_HOLDER.into(),
),
key,
};
let prev_raw_nonce = self
.storage
.entry(storage_key)
.or_insert(web3::types::H256::zero());
let prev_raw_nonce_uint = utils::h256_to_u256(prev_raw_nonce);
if (prev_raw_nonce_uint >> 128) == web3::types::U256::zero() {
let new_raw_nonce = prev_raw_nonce_uint
+ (web3::types::U256::from(1) << web3::types::U256::from(128));
self.storage
.insert(storage_key, utils::u256_to_h256(&new_raw_nonce));
}
}

#[cfg(not(feature = "vm2"))]
{
let snapshot = zkevm_tester::compiler_tests::run_vm_multi_contracts(
Expand Down
20 changes: 12 additions & 8 deletions compiler_tester/src/vm/eravm/system_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl SystemContracts {
"era-contracts/system-contracts/contracts/precompiles/SHA256.yul";

/// The `identity` system contract implementation path.
const PATH_IDENTITY: (&'static str, &'static str) =
("tests/solidity/simple/system/identity.sol", "Identity");
const PATH_IDENTITY: &'static str = "era-contracts/system-contracts/contracts/precompiles/Identity.yul";

/// The `ecadd` system contract implementation path.
const PATH_ECADD: &'static str =
Expand Down Expand Up @@ -189,6 +188,10 @@ impl SystemContracts {
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_SHA256.into()),
Self::PATH_SHA256.to_owned(),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_IDENTITY.into()),
Self::PATH_IDENTITY.to_owned(),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::system_params::ADDRESS_ECADD.into(),
Expand All @@ -201,6 +204,12 @@ impl SystemContracts {
),
Self::PATH_ECMUL.to_owned(),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::system_params::ADDRESS_IDENTITY.into(),
),
Self::PATH_IDENTITY,
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_EVENT_WRITER.into(),
Expand All @@ -225,10 +234,6 @@ impl SystemContracts {
Some(Self::PATH_EMPTY_CONTRACT.1),
),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_IDENTITY.into()),
Self::normalize_name_fs(Self::PATH_IDENTITY.0, Some(Self::PATH_IDENTITY.1)),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_ACCOUNT_CODE_STORAGE.into(),
Expand Down Expand Up @@ -313,8 +318,7 @@ impl SystemContracts {
"6",
"-eravm-enable-split-loop-phi-live-ranges",
"-tail-merge-only-bbs-without-succ",
"-join-globalcopies",
"-disable-early-taildup",
"-tail-dup-fallthrough-bbs",
]
.into_iter()
.map(|option| option.to_owned())
Expand Down
2 changes: 1 addition & 1 deletion era-contracts
Submodule era-contracts updated 100 files
6 changes: 4 additions & 2 deletions ethereum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3296,7 +3296,8 @@ entries:
version: '>=0.6.0'
failed_create.sol:
hash: '0xcd337f294254561827a382cdd146e09d'
enabled: true
enabled: false
comment: 'TODO: out-of-ergs in underlying EraVM'
version: '>=0.7.0'
file_level_call_via_module.sol:
hash: '0x7484f59b7118567c0bb54761d5b87405'
Expand Down Expand Up @@ -6170,7 +6171,8 @@ entries:
version: '>=0.6.2'
create.sol:
hash: '0xa937f29a18a9b48c6ed9813c4cdfd006'
enabled: true
enabled: false
comment: 'TODO: VM implementation in Solidity tests is invalid'
version: '>=0.7.0'
invalid_error_encoding.sol:
hash: '0x87c1bfbd0f7d4b04f99dd8c7043c15fc'
Expand Down
Binary file modified system-contracts-stable-build
Binary file not shown.
Loading