Skip to content

Commit deafa46

Browse files
slowliIAvecillajrchatruc
authored
feat(vm): EVM emulator support – base (#2979)
## What ❔ Modifies the Era codebase to support the EVM emulator. Intentionally avoids changing the `contracts` submodule yet; as a consequence, there are no EVM emulation tests. ## Why ❔ Stepping stone for EVM equivalence. ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [x] Tests for the changes have been added / updated. - [x] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`. --------- Co-authored-by: IAvecilla <ignacio.avecilla@lambdaclass.com> Co-authored-by: Javier Chatruc <jrchatruc@gmail.com>
1 parent 3fd2fb1 commit deafa46

File tree

152 files changed

+2189
-487
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+2189
-487
lines changed

core/bin/genesis_generator/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async fn generate_new_config(
8787
genesis_commitment: None,
8888
bootloader_hash: Some(base_system_contracts.bootloader),
8989
default_aa_hash: Some(base_system_contracts.default_aa),
90+
evm_emulator_hash: base_system_contracts.evm_emulator,
9091
..genesis_config
9192
};
9293

core/bin/system-constants-generator/src/utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ pub static GAS_TEST_SYSTEM_CONTRACTS: Lazy<BaseSystemContracts> = Lazy::new(|| {
7171

7272
let bytecode = read_sys_contract_bytecode("", "DefaultAccount", ContractLanguage::Sol);
7373
let hash = hash_bytecode(&bytecode);
74+
7475
BaseSystemContracts {
7576
default_aa: SystemContractCode {
7677
code: bytes_to_be_words(bytecode),
7778
hash,
7879
},
7980
bootloader,
81+
evm_emulator: None,
8082
}
8183
});
8284

@@ -221,6 +223,7 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
221223
let base_system_smart_contracts = BaseSystemContracts {
222224
bootloader,
223225
default_aa,
226+
evm_emulator: None,
224227
};
225228

226229
let system_env = SystemEnv {

core/lib/config/src/configs/chain.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub struct StateKeeperConfig {
138138
pub bootloader_hash: Option<H256>,
139139
#[deprecated(note = "Use GenesisConfig::default_aa_hash instead")]
140140
pub default_aa_hash: Option<H256>,
141+
#[deprecated(note = "Use GenesisConfig::evm_emulator_hash instead")]
142+
pub evm_emulator_hash: Option<H256>,
141143
#[deprecated(note = "Use GenesisConfig::l1_batch_commit_data_generator_mode instead")]
142144
#[serde(default)]
143145
pub l1_batch_commit_data_generator_mode: L1BatchCommitmentMode,
@@ -178,6 +180,7 @@ impl StateKeeperConfig {
178180
protective_reads_persistence_enabled: true,
179181
bootloader_hash: None,
180182
default_aa_hash: None,
183+
evm_emulator_hash: None,
181184
l1_batch_commit_data_generator_mode: L1BatchCommitmentMode::Rollup,
182185
}
183186
}

core/lib/config/src/configs/genesis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct GenesisConfig {
1717
pub genesis_commitment: Option<H256>,
1818
pub bootloader_hash: Option<H256>,
1919
pub default_aa_hash: Option<H256>,
20+
pub evm_emulator_hash: Option<H256>,
2021
pub l1_chain_id: L1ChainId,
2122
pub sl_chain_id: Option<SLChainId>,
2223
pub l2_chain_id: L2ChainId,
@@ -49,6 +50,7 @@ impl GenesisConfig {
4950
genesis_commitment: Some(H256::repeat_byte(0x17)),
5051
bootloader_hash: Default::default(),
5152
default_aa_hash: Default::default(),
53+
evm_emulator_hash: Default::default(),
5254
l1_chain_id: L1ChainId(9),
5355
sl_chain_id: None,
5456
protocol_version: Some(ProtocolSemanticVersion {

core/lib/config/src/testonly.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl Distribution<configs::chain::StateKeeperConfig> for EncodeDist {
192192
fee_account_addr: None,
193193
bootloader_hash: None,
194194
default_aa_hash: None,
195+
evm_emulator_hash: None,
195196
l1_batch_commit_data_generator_mode: Default::default(),
196197
}
197198
}
@@ -732,6 +733,7 @@ impl Distribution<configs::GenesisConfig> for EncodeDist {
732733
genesis_commitment: Some(rng.gen()),
733734
bootloader_hash: Some(rng.gen()),
734735
default_aa_hash: Some(rng.gen()),
736+
evm_emulator_hash: Some(rng.gen()),
735737
fee_account: rng.gen(),
736738
l1_chain_id: L1ChainId(self.sample(rng)),
737739
sl_chain_id: None,

core/lib/constants/src/contracts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ pub const CODE_ORACLE_ADDRESS: Address = H160([
130130
0x00, 0x00, 0x80, 0x12,
131131
]);
132132

133+
pub const EVM_GAS_MANAGER_ADDRESS: Address = H160([
134+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135+
0x00, 0x00, 0x80, 0x13,
136+
]);
137+
133138
/// Note, that the `Create2Factory` is explicitly deployed on a non-system-contract address.
134139
pub const CREATE2_FACTORY_ADDRESS: Address = H160([
135140
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

core/lib/contracts/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ fn read_zbin_bytecode_from_path(bytecode_path: PathBuf) -> Vec<u8> {
293293
fs::read(&bytecode_path)
294294
.unwrap_or_else(|err| panic!("Can't read .zbin bytecode at {:?}: {}", bytecode_path, err))
295295
}
296+
296297
/// Hash of code and code which consists of 32 bytes words
297298
#[derive(Debug, Clone, Serialize, Deserialize)]
298299
pub struct SystemContractCode {
@@ -304,18 +305,23 @@ pub struct SystemContractCode {
304305
pub struct BaseSystemContracts {
305306
pub bootloader: SystemContractCode,
306307
pub default_aa: SystemContractCode,
308+
/// Never filled in constructors for now. The only way to get the EVM emulator enabled is to call [`Self::with_evm_emulator()`].
309+
pub evm_emulator: Option<SystemContractCode>,
307310
}
308311

309312
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
310313
pub struct BaseSystemContractsHashes {
311314
pub bootloader: H256,
312315
pub default_aa: H256,
316+
pub evm_emulator: Option<H256>,
313317
}
314318

315319
impl PartialEq for BaseSystemContracts {
316320
fn eq(&self, other: &Self) -> bool {
317321
self.bootloader.hash == other.bootloader.hash
318322
&& self.default_aa.hash == other.default_aa.hash
323+
&& self.evm_emulator.as_ref().map(|contract| contract.hash)
324+
== other.evm_emulator.as_ref().map(|contract| contract.hash)
319325
}
320326
}
321327

@@ -339,14 +345,27 @@ impl BaseSystemContracts {
339345
BaseSystemContracts {
340346
bootloader,
341347
default_aa,
348+
evm_emulator: None,
342349
}
343350
}
344-
// BaseSystemContracts with proved bootloader - for handling transactions.
351+
352+
/// BaseSystemContracts with proved bootloader - for handling transactions.
345353
pub fn load_from_disk() -> Self {
346354
let bootloader_bytecode = read_proved_batch_bootloader_bytecode();
347355
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
348356
}
349357

358+
/// Loads the latest EVM emulator for these base system contracts. Logically, it only makes sense to do for the latest protocol version.
359+
pub fn with_latest_evm_emulator(mut self) -> Self {
360+
let bytecode = read_sys_contract_bytecode("", "EvmInterpreter", ContractLanguage::Yul);
361+
let hash = hash_bytecode(&bytecode);
362+
self.evm_emulator = Some(SystemContractCode {
363+
code: bytes_to_be_words(bytecode),
364+
hash,
365+
});
366+
self
367+
}
368+
350369
/// BaseSystemContracts with playground bootloader - used for handling eth_calls.
351370
pub fn playground() -> Self {
352371
let bootloader_bytecode = read_playground_batch_bootloader_bytecode();
@@ -475,6 +494,7 @@ impl BaseSystemContracts {
475494
BaseSystemContractsHashes {
476495
bootloader: self.bootloader.hash,
477496
default_aa: self.default_aa.hash,
497+
evm_emulator: self.evm_emulator.as_ref().map(|contract| contract.hash),
478498
}
479499
}
480500
}

core/lib/dal/.sqlx/query-ae30067056fe29febd68408c2ca2e604958488a41d3ee2bcbd05d269bcdfc7aa.json renamed to core/lib/dal/.sqlx/query-05726523bb494b40011c28acd3f52dba1d37493d4c1db4b957cfec476a791b32.json

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/lib/dal/.sqlx/query-2dc550a35fb0f0ddb1aded83d54a2e93066a5cffbb3857dfd3c6fe00c307eada.json renamed to core/lib/dal/.sqlx/query-16d4658899c5b604fb794d44a8b3bef013ad12b66bdca7251be2af21e98fe870.json

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/lib/dal/.sqlx/query-34910600545933d85931d41bfe2dfcb3522a0772ac3d2476652df4216d823e04.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)