diff --git a/etherlink/kernel_evm/Cargo.lock b/etherlink/kernel_evm/Cargo.lock index 547257c7131f60b47599cce1620dfbd6d6b3cce0..a563ee27e180ee5b217ccde6760692e3f744d033 100644 --- a/etherlink/kernel_evm/Cargo.lock +++ b/etherlink/kernel_evm/Cargo.lock @@ -2206,6 +2206,7 @@ dependencies = [ "sha3", "tezos-smart-rollup-core", "tezos-smart-rollup-debug", + "tezos-smart-rollup-encoding", "tezos-smart-rollup-host", "tezos-smart-rollup-mock", ] diff --git a/etherlink/kernel_evm/kernel/src/block.rs b/etherlink/kernel_evm/kernel/src/block.rs index 2ffdf9ac552287a1a06291531dd39b9a5e972b1d..b6208d49ffed3582ad519b29424c23801aa29a4d 100644 --- a/etherlink/kernel_evm/kernel/src/block.rs +++ b/etherlink/kernel_evm/kernel/src/block.rs @@ -601,6 +601,7 @@ mod tests { account_path, init_account_storage, EthereumAccountStorage, }; use primitive_types::{H160, U256}; + use tezos_evm_runtime::runtime::MockKernelHost; use std::str::FromStr; use tezos_ethereum::transaction::{ TransactionHash, TransactionStatus, TransactionType, TRANSACTION_HASH_SIZE, @@ -838,12 +839,7 @@ mod tests { #[test] // Test if the invalid transactions are producing receipts fn test_invalid_transactions_receipt_status() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -887,12 +883,7 @@ mod tests { #[test] // Test if a valid transaction is producing a receipt with a success status fn test_valid_transactions_receipt_status() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -936,12 +927,7 @@ mod tests { #[test] // Test if a valid transaction is producing a receipt with a contract address fn test_valid_transactions_receipt_contract_address() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -996,12 +982,7 @@ mod tests { #[test] // Test if several valid transactions can be performed fn test_several_valid_transactions() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1023,12 +1004,7 @@ mod tests { #[test] // Test if several valid proposals can produce valid blocks fn test_several_valid_proposals() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1083,12 +1059,7 @@ mod tests { #[test] // Test transfers gas consumption consistency fn test_cumulative_transfers_gas_consumption() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let base_gas = U256::from(21000); crate::storage::store_minimum_base_fee_per_gas(&mut host, base_gas).unwrap(); @@ -1151,12 +1122,7 @@ mod tests { // Test if we're able to read current block (with a filled queue) after // a block production fn test_read_storage_current_block_after_block_production_with_filled_queue() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut evm_account_storage = init_account_storage().unwrap(); @@ -1168,12 +1134,7 @@ mod tests { #[test] // Test that the same transaction can not be replayed twice fn test_replay_attack() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1233,12 +1194,7 @@ mod tests { #[test] fn test_blocks_are_indexed() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1304,12 +1260,7 @@ mod tests { #[test] fn test_stop_computation() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let block_constants = first_block(&mut host); let precompiles = precompiles::precompile_set(false); @@ -1387,12 +1338,7 @@ mod tests { #[test] fn invalid_transaction_should_bump_nonce() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut evm_account_storage = init_account_storage().unwrap(); @@ -1464,12 +1410,7 @@ mod tests { #[test] fn test_first_blocks() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); // first block should be 0 let blueprint = almost_empty_blueprint(); @@ -1582,12 +1523,7 @@ mod tests { #[test] fn test_reboot_many_tx_one_proposal() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1673,12 +1609,7 @@ mod tests { #[test] fn test_reboot_many_tx_many_proposal() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, @@ -1790,12 +1721,7 @@ mod tests { // address. // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); // ensure we're using the default block fees to test. let block_fees = crate::retrieve_block_fees(&mut host).unwrap(); @@ -1859,12 +1785,7 @@ mod tests { #[test] fn test_non_retriable_transaction_are_marked_as_failed() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), @@ -1987,12 +1908,7 @@ mod tests { #[test] // Test if a valid transaction is producing a receipt with a success status fn test_type_propagation() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, DUMMY_BASE_FEE_PER_GAS.into(), diff --git a/etherlink/kernel_evm/kernel/src/blueprint_storage.rs b/etherlink/kernel_evm/kernel/src/blueprint_storage.rs index 9eb637714f9b3e6c8c9c1de924fe62ef7c087452..6147f2de4569f2f5735c1e9e343e0e15360f383d 100644 --- a/etherlink/kernel_evm/kernel/src/blueprint_storage.rs +++ b/etherlink/kernel_evm/kernel/src/blueprint_storage.rs @@ -493,18 +493,13 @@ mod tests { use tezos_crypto_rs::hash::ContractKt1Hash; use tezos_ethereum::transaction::TRANSACTION_HASH_SIZE; use tezos_evm_runtime::mock_internal::MockInternal; - use tezos_evm_runtime::runtime::KernelHost; + use tezos_evm_runtime::runtime::{KernelHost, MockKernelHost}; use tezos_smart_rollup_encoding::public_key::PublicKey; use tezos_smart_rollup_host::runtime::Runtime; use tezos_smart_rollup_mock::MockHost; fn test_invalid_sequencer_blueprint_is_removed(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let delayed_inbox = DelayedInbox::new(&mut host).expect("Delayed inbox should be created"); let delayed_bridge: ContractKt1Hash = diff --git a/etherlink/kernel_evm/kernel/src/delayed_inbox.rs b/etherlink/kernel_evm/kernel/src/delayed_inbox.rs index e099bea8a87e7b3b2e1c07f90807da295fdfaea3..3d7f2b2f4aa168e71cbe1939c2a1d96baf28398d 100644 --- a/etherlink/kernel_evm/kernel/src/delayed_inbox.rs +++ b/etherlink/kernel_evm/kernel/src/delayed_inbox.rs @@ -383,6 +383,7 @@ mod tests { use primitive_types::{H160, U256}; use tezos_evm_runtime::mock_internal::MockInternal; use tezos_evm_runtime::runtime::KernelHost; + use tezos_evm_runtime::runtime::MockKernelHost; use tezos_smart_rollup_encoding::timestamp::Timestamp; use crate::inbox::TransactionContent::{Ethereum, EthereumDelayed}; @@ -422,12 +423,7 @@ mod tests { #[test] fn test_delayed_inbox_roundtrip() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut delayed_inbox = DelayedInbox::new(&mut host).expect("Delayed inbox should be created"); @@ -450,12 +446,7 @@ mod tests { #[test] fn test_delayed_inbox_roundtrip_error_non_delayed() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut delayed_inbox = DelayedInbox::new(&mut host).expect("Delayed inbox should be created"); diff --git a/etherlink/kernel_evm/kernel/src/evm_node_entrypoint.rs b/etherlink/kernel_evm/kernel/src/evm_node_entrypoint.rs index e091e0f3bd48d8b7fe981fcce5800d8b05f94072..8d6f75c12414dbde48ed5b788540a18dec0bf429 100644 --- a/etherlink/kernel_evm/kernel/src/evm_node_entrypoint.rs +++ b/etherlink/kernel_evm/kernel/src/evm_node_entrypoint.rs @@ -8,6 +8,8 @@ //! only. It allows to call specific functions of the kernel without //! using the inbox and a specific message. +use std::marker::PhantomData; + use crate::{delayed_inbox::DelayedInbox, inbox::Transaction}; use tezos_ethereum::rlp_helpers::FromRlpBytes; use tezos_evm_runtime::{internal_runtime::InternalHost, runtime::KernelHost}; @@ -19,11 +21,12 @@ const DELAYED_INPUT_PATH: RefPath = RefPath::assert_from(b"/__delayed_input"); #[allow(dead_code)] #[no_mangle] pub extern "C" fn populate_delayed_inbox() { - let mut sdk_host = unsafe { RollupHost::new() }; - let mut internal = InternalHost(); + let sdk_host = unsafe { RollupHost::new() }; + let internal = InternalHost(); let mut host = KernelHost { - host: &mut sdk_host, - internal: &mut internal, + host: sdk_host, + internal, + _pd: PhantomData }; let payload = host.store_read_all(&DELAYED_INPUT_PATH).unwrap(); let transaction = Transaction::from_rlp_bytes(&payload).unwrap(); diff --git a/etherlink/kernel_evm/kernel/src/gas_price.rs b/etherlink/kernel_evm/kernel/src/gas_price.rs index b08f7dad465943b0ad303d4a1cefa70b02f1ff53..890b1e5bf5629940f882ca01bbce5442abed957c 100644 --- a/etherlink/kernel_evm/kernel/src/gas_price.rs +++ b/etherlink/kernel_evm/kernel/src/gas_price.rs @@ -169,7 +169,7 @@ mod test { use std::collections::VecDeque; use tezos_evm_runtime::{ mock_internal::MockInternal, - runtime::{KernelHost, Runtime}, + runtime::{KernelHost, Runtime, MockKernelHost}, }; use tezos_smart_rollup_mock::MockHost; @@ -201,12 +201,7 @@ mod test { #[test] fn gas_price_responds_to_load() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let timestamp = 0_i64; let mut block_fees = BlockFees::new(U256::zero(), U256::zero(), U256::zero()); diff --git a/etherlink/kernel_evm/kernel/src/inbox.rs b/etherlink/kernel_evm/kernel/src/inbox.rs index bc63e821e4ed50f91d311f353d00bfda5ebf8147..9e0b982683e0448144fb4f7a0cc016f87106dabf 100644 --- a/etherlink/kernel_evm/kernel/src/inbox.rs +++ b/etherlink/kernel_evm/kernel/src/inbox.rs @@ -715,7 +715,7 @@ mod tests { use tezos_data_encoding::types::Bytes; use tezos_ethereum::transaction::TRANSACTION_HASH_SIZE; use tezos_evm_runtime::mock_internal::MockInternal; - use tezos_evm_runtime::runtime::KernelHost; + use tezos_evm_runtime::runtime::{KernelHost, MockKernelHost}; use tezos_smart_rollup_core::PREIMAGE_HASH_SIZE; use tezos_smart_rollup_encoding::contract::Contract; use tezos_smart_rollup_encoding::inbox::ExternalMessageFrame; @@ -833,12 +833,7 @@ mod tests { #[test] fn parse_valid_simple_transaction() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let tx_bytes = &hex::decode("f86d80843b9aca00825208940b52d4d3be5d18a7ab5e4476a2f5382bbf2b38d888016345785d8a000080820a95a0d9ef1298c18c88604e3f08e14907a17dfa81b1dc6b37948abe189d8db5cb8a43a06fc7040a71d71d3cb74bd05ead7046b10668ad255da60391c017eea31555f156").unwrap(); let tx = EthereumTransactionCommon::from_bytes(tx_bytes).unwrap(); @@ -870,12 +865,7 @@ mod tests { #[test] fn parse_valid_chunked_transaction() { let address = smart_rollup_address(); - let mut host = MockHost::with_address(&address); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::with_address(address); let (data, tx) = large_transaction(); let tx_hash: [u8; TRANSACTION_HASH_SIZE] = Keccak256::digest(data.clone()).into(); @@ -904,12 +894,7 @@ mod tests { #[test] fn parse_valid_kernel_upgrade() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); // Prepare the upgrade's payload let preimage_hash: [u8; PREIMAGE_HASH_SIZE] = hex::decode( @@ -968,12 +953,7 @@ mod tests { // Assert that trying to create a chunked transaction has no impact. Only // the first `NewChunkedTransaction` should be considered. fn recreate_chunked_transaction() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let chunk_hashes = vec![[1; TRANSACTION_HASH_SIZE], [2; TRANSACTION_HASH_SIZE]]; let tx_hash = [0; TRANSACTION_HASH_SIZE]; @@ -1015,12 +995,7 @@ mod tests { // Assert that an out of bound chunk is simply ignored and does // not make the kernel fail. fn out_of_bound_chunk_is_ignored() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let (data, _tx) = large_transaction(); let tx_hash = ZERO_TX_HASH; @@ -1073,12 +1048,7 @@ mod tests { // Assert that an unknown chunk is simply ignored and does // not make the kernel fail. fn unknown_chunk_is_ignored() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let (data, _tx) = large_transaction(); let tx_hash = ZERO_TX_HASH; @@ -1131,12 +1101,7 @@ mod tests { // - Chunk 1 // |--> Fails because the chunk is unknown fn transaction_is_complete_when_each_chunk_is_stored() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let (data, tx) = large_transaction(); let tx_hash: [u8; TRANSACTION_HASH_SIZE] = Keccak256::digest(data.clone()).into(); @@ -1197,12 +1162,7 @@ mod tests { // parsing. This won't happen in practice, though let address = smart_rollup_address(); - let mut host = MockHost::with_address(&address); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::with_address(address.clone()); let tx_bytes = &hex::decode("f86d80843b9aca00825208940b52d4d3be5d18a7ab5\ e4476a2f5382bbf2b38d888016345785d8a000080820a95a0d9ef1298c18c88604e3f08e14907a17dfa81b1dc6b37948abe189d8db5cb8a43a06\ @@ -1258,12 +1218,7 @@ mod tests { #[test] fn empty_inbox_returns_none() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); // Even reading the inbox with only the default elements returns // an empty inbox content. As we test in isolation there is nothing diff --git a/etherlink/kernel_evm/kernel/src/lib.rs b/etherlink/kernel_evm/kernel/src/lib.rs index 0d7422a258b58c9314bf6cd36d308b6cc3a55d32..5c36ba15fa514ae1a122bf73834de59f5eaa04f2 100644 --- a/etherlink/kernel_evm/kernel/src/lib.rs +++ b/etherlink/kernel_evm/kernel/src/lib.rs @@ -5,6 +5,8 @@ // // SPDX-License-Identifier: MIT +use std::marker::PhantomData; + use crate::configuration::{fetch_configuration, Configuration}; use crate::error::Error; use crate::error::UpgradeProcessError::Fallback; @@ -314,10 +316,11 @@ pub fn kernel_loop(host: &mut H // The kernel host is initialized as soon as possible. `kernel_loop` // shouldn't be called in tests as it won't use `MockInternal` for the // internal runtime. - let mut internal_storage = tezos_evm_runtime::internal_runtime::InternalHost(); + let internal_storage = tezos_evm_runtime::internal_runtime::InternalHost(); let mut host = KernelHost { host, - internal: &mut internal_storage, + internal: internal_storage, + _pd: PhantomData:: }; let reboot_counter = host @@ -402,7 +405,7 @@ mod tests { tx_common::EthereumTransactionCommon, }; use tezos_evm_runtime::mock_internal::MockInternal; - use tezos_evm_runtime::runtime::KernelHost; + use tezos_evm_runtime::runtime::{KernelHost, MockKernelHost}; use tezos_evm_runtime::safe_storage::SafeStorage; use tezos_smart_rollup::michelson::ticket::FA2_1Ticket; @@ -521,12 +524,7 @@ mod tests { #[test] fn test_reboot_during_block_production() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); crate::storage::store_minimum_base_fee_per_gas( &mut host, @@ -631,12 +629,7 @@ mod tests { #[test] fn load_block_fees_new() { // Arrange - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); // Act let result = crate::retrieve_block_fees(&mut host); @@ -658,12 +651,7 @@ mod tests { RefPath::assert_from(b"/evm/world_state/fees/minimum_base_fee_per_gas"); // Arrange - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let min_base_fee = U256::from(17); let curr_base_fee = U256::from(20); @@ -688,12 +676,7 @@ mod tests { #[test] fn test_xtz_withdrawal_applied() { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut mock_host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut mock_host = MockKernelHost::default(); let mut safe_storage = SafeStorage { host: &mut mock_host, }; @@ -815,12 +798,7 @@ mod tests { fn send_fa_deposit(enable_fa_bridge: bool) -> Option { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut mock_host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut mock_host = MockKernelHost::default(); let mut safe_storage = SafeStorage { host: &mut mock_host, }; @@ -916,12 +894,7 @@ mod tests { fn send_fa_withdrawal(enable_fa_bridge: bool) -> Vec> { // init host - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut mock_host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut mock_host = MockKernelHost::default(); let mut safe_storage = SafeStorage { host: &mut mock_host, }; diff --git a/etherlink/kernel_evm/kernel/src/simulation.rs b/etherlink/kernel_evm/kernel/src/simulation.rs index 285fd0892f34c87e1193c6afcf59af5ff6aa54fa..44a26acf6c79899c0aefb368ef506c73204f5908 100644 --- a/etherlink/kernel_evm/kernel/src/simulation.rs +++ b/etherlink/kernel_evm/kernel/src/simulation.rs @@ -814,7 +814,7 @@ mod tests { use tezos_ethereum::{ block::BlockConstants, transaction::TransactionType, tx_signature::TxSignature, }; - use tezos_evm_runtime::{mock_internal::MockInternal, runtime::KernelHost}; + use tezos_evm_runtime::{mock_internal::MockInternal, runtime::{KernelHost, MockKernelHost}}; use tezos_smart_rollup_mock::MockHost; use crate::{ @@ -954,12 +954,7 @@ mod tests { #[test] fn simulation_result() { // setup - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let new_address = create_contract(&mut host); // run evaluation num @@ -1017,12 +1012,7 @@ mod tests { #[test] fn evaluation_result_no_gas() { // setup - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let new_address = create_contract(&mut host); // run evaluation num @@ -1090,12 +1080,7 @@ mod tests { #[test] fn parse_simulation2() { // setup - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let new_address = create_contract(&mut host); let to = Some(new_address); @@ -1225,12 +1210,7 @@ mod tests { #[test] fn test_tx_validation_gas_price() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let block_fees = crate::retrieve_block_fees(&mut host).unwrap(); let gas_price = U256::one(); let tx_data = vec![]; @@ -1289,12 +1269,7 @@ mod tests { #[test] fn test_tx_validation_da_fees_not_covered() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let block_fees = crate::retrieve_block_fees(&mut host).unwrap(); let transaction = EthereumTransactionCommon::new( diff --git a/etherlink/kernel_evm/kernel/src/stage_one.rs b/etherlink/kernel_evm/kernel/src/stage_one.rs index 56d0413b3b7cc4948bbd3a78a4ea858185151a26..983aeb6eb3306f0ea0d236d40d71f4b3a57c0284 100644 --- a/etherlink/kernel_evm/kernel/src/stage_one.rs +++ b/etherlink/kernel_evm/kernel/src/stage_one.rs @@ -170,7 +170,7 @@ mod tests { use primitive_types::U256; use tezos_crypto_rs::hash::HashTrait; use tezos_data_encoding::types::Bytes; - use tezos_evm_runtime::{mock_internal::MockInternal, runtime::KernelHost}; + use tezos_evm_runtime::{mock_internal::MockInternal, runtime::{KernelHost, MockKernelHost}}; use tezos_smart_rollup::{ michelson::{ ticket::FA2_1Ticket, MichelsonBytes, MichelsonOption, MichelsonOr, @@ -189,12 +189,7 @@ mod tests { use super::*; fn dummy_sequencer_config(enable_dal: bool) -> Configuration { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let delayed_inbox = DelayedInbox::new(&mut host).expect("Delayed inbox should be created"); let delayed_bridge: ContractKt1Hash = @@ -305,12 +300,7 @@ mod tests { #[test] fn test_parsing_proxy_transaction() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host .add_external(Bytes::from(hex::decode(DUMMY_TRANSACTION).unwrap())); let mut conf = dummy_proxy_configuration(); @@ -327,12 +317,7 @@ mod tests { #[test] fn test_parsing_proxy_chunked_transaction() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host .add_external(Bytes::from(hex::decode(DUMMY_NEW_CHUNKED_TX).unwrap())); host.host @@ -352,12 +337,7 @@ mod tests { } fn test_sequencer_reject_proxy_transactions(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host .add_external(Bytes::from(hex::decode(DUMMY_TRANSACTION).unwrap())); let mut conf = dummy_sequencer_config(enable_dal); @@ -383,12 +363,7 @@ mod tests { } fn test_sequencer_reject_proxy_chunked_transactions(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host .add_external(Bytes::from(hex::decode(DUMMY_NEW_CHUNKED_TX).unwrap())); host.host @@ -418,12 +393,7 @@ mod tests { } fn test_parsing_valid_sequencer_chunk(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host.add_external(Bytes::from( hex::decode(DUMMY_BLUEPRINT_CHUNK_NUMBER_10).unwrap(), )); @@ -452,12 +422,7 @@ mod tests { } fn test_parsing_invalid_sequencer_chunk(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host.add_external(Bytes::from( hex::decode(DUMMY_BLUEPRINT_CHUNK_UNPARSABLE).unwrap(), )); @@ -484,12 +449,7 @@ mod tests { } fn test_proxy_rejects_sequencer_chunk(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); host.host.add_external(Bytes::from( hex::decode(DUMMY_BLUEPRINT_CHUNK_NUMBER_10).unwrap(), )); @@ -533,12 +493,7 @@ mod tests { } fn test_parsing_delayed_inbox(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_sequencer_config(enable_dal); let metadata = TransferMetadata::new( delayed_bridge(&conf), @@ -572,12 +527,7 @@ mod tests { } fn test_parsing_l1_contract_inbox(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_sequencer_config(enable_dal); let metadata = TransferMetadata::new( ContractKt1Hash::from_b58check(DUMMY_INVALID_TICKETER).unwrap(), @@ -612,12 +562,7 @@ mod tests { #[test] fn test_parsing_delayed_inbox_rejected_in_proxy() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_proxy_configuration(); let metadata = TransferMetadata::new( ContractKt1Hash::from_b58check(DUMMY_INVALID_TICKETER).unwrap(), @@ -639,12 +584,7 @@ mod tests { #[test] fn test_deposit_in_proxy_mode() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_proxy_configuration(); let metadata = TransferMetadata::new( conf.tezos_contracts.ticketer.clone().unwrap(), @@ -671,12 +611,7 @@ mod tests { #[test] fn test_deposit_with_invalid_ticketer() { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_proxy_configuration(); let metadata = TransferMetadata::new( ContractKt1Hash::from_b58check(DUMMY_INVALID_TICKETER).unwrap(), @@ -704,12 +639,7 @@ mod tests { } fn test_deposit_in_sequencer_mode(enable_dal: bool) { - let mut host = MockHost::default(); - let mut internal = MockInternal(); - let mut host = KernelHost { - host: &mut host, - internal: &mut internal, - }; + let mut host = MockKernelHost::default(); let mut conf = dummy_sequencer_config(enable_dal); let metadata = TransferMetadata::new( conf.tezos_contracts.ticketer.clone().unwrap(), diff --git a/etherlink/kernel_evm/runtime/Cargo.toml b/etherlink/kernel_evm/runtime/Cargo.toml index 349f7b36e475910c71a636338ef6b461b6bddec6..a2b28231d6e02715355b3ec55d2bde9878cc9f28 100644 --- a/etherlink/kernel_evm/runtime/Cargo.toml +++ b/etherlink/kernel_evm/runtime/Cargo.toml @@ -11,7 +11,8 @@ edition = "2021" tezos-smart-rollup-debug.workspace = true tezos-smart-rollup-core.workspace = true tezos-smart-rollup-host.workspace = true +tezos-smart-rollup-mock.workspace = true +tezos-smart-rollup-encoding.workspace = true sha3.workspace = true [dev-dependencies] -tezos-smart-rollup-mock.workspace = true diff --git a/etherlink/kernel_evm/runtime/src/runtime.rs b/etherlink/kernel_evm/runtime/src/runtime.rs index bf81396e033dba02555bae7c6c8cb105e87bd520..471bef86ab2bd0ebded12ed5efa1cc5e005d8242 100644 --- a/etherlink/kernel_evm/runtime/src/runtime.rs +++ b/etherlink/kernel_evm/runtime/src/runtime.rs @@ -8,7 +8,13 @@ // The kernel runtime requires both the standard Runtime and the // new Extended one. -use crate::internal_runtime::{ExtendedRuntime, InternalRuntime}; +use std::{marker::PhantomData, borrow::{BorrowMut, Borrow}}; + +use crate::{ + internal_runtime::{ExtendedRuntime, InternalRuntime}, + mock_internal::MockInternal, +}; +use tezos_smart_rollup_encoding::smart_rollup::SmartRollupAddress; use tezos_smart_rollup_core::PREIMAGE_HASH_SIZE; use tezos_smart_rollup_host::{ dal_parameters::RollupDalParameters, @@ -17,6 +23,7 @@ use tezos_smart_rollup_host::{ path::Path, runtime::{Runtime as SdkRuntime, RuntimeError, ValueType}, }; +use tezos_smart_rollup_mock::MockHost; pub trait Runtime: SdkRuntime + InternalRuntime + ExtendedRuntime {} @@ -24,32 +31,33 @@ pub trait Runtime: SdkRuntime + InternalRuntime + ExtendedRuntime {} // it also implements the kernel Runtime. impl Runtime for T {} -pub struct KernelHost { +pub struct KernelHost + Borrow, Internal> { pub host: Host, pub internal: Internal, + pub _pd: PhantomData } -impl SdkRuntime - for KernelHost<&mut Host, &mut Internal> +impl + Borrow, Internal: InternalRuntime> SdkRuntime + for KernelHost { #[inline(always)] fn write_output(&mut self, from: &[u8]) -> Result<(), RuntimeError> { - self.host.write_output(from) + self.host.borrow_mut().write_output(from) } #[inline(always)] fn write_debug(&self, msg: &str) { - self.host.write_debug(msg) + self.host.borrow().write_debug(msg) } #[inline(always)] fn read_input(&mut self) -> Result, RuntimeError> { - self.host.read_input() + self.host.borrow_mut().read_input() } #[inline(always)] fn store_has(&self, path: &T) -> Result, RuntimeError> { - self.host.store_has(path) + self.host.borrow().store_has(path) } #[inline(always)] @@ -59,7 +67,7 @@ impl SdkRuntime from_offset: usize, max_bytes: usize, ) -> Result, RuntimeError> { - self.host.store_read(path, from_offset, max_bytes) + self.host.borrow().store_read(path, from_offset, max_bytes) } #[inline(always)] @@ -69,12 +77,12 @@ impl SdkRuntime from_offset: usize, buffer: &mut [u8], ) -> Result { - self.host.store_read_slice(path, from_offset, buffer) + self.host.borrow().store_read_slice(path, from_offset, buffer) } #[inline(always)] fn store_read_all(&self, path: &impl Path) -> Result, RuntimeError> { - self.host.store_read_all(path) + self.host.borrow().store_read_all(path) } #[inline(always)] @@ -84,7 +92,7 @@ impl SdkRuntime src: &[u8], at_offset: usize, ) -> Result<(), RuntimeError> { - self.host.store_write(path, src, at_offset) + self.host.borrow_mut().store_write(path, src, at_offset) } #[inline(always)] @@ -93,22 +101,22 @@ impl SdkRuntime path: &T, src: &[u8], ) -> Result<(), RuntimeError> { - self.host.store_write_all(path, src) + self.host.borrow_mut().store_write_all(path, src) } #[inline(always)] fn store_delete(&mut self, path: &T) -> Result<(), RuntimeError> { - self.host.store_delete(path) + self.host.borrow_mut().store_delete(path) } #[inline(always)] fn store_delete_value(&mut self, path: &T) -> Result<(), RuntimeError> { - self.host.store_delete_value(path) + self.host.borrow_mut().store_delete_value(path) } #[inline(always)] fn store_count_subkeys(&self, prefix: &T) -> Result { - self.host.store_count_subkeys(prefix) + self.host.borrow().store_count_subkeys(prefix) } #[inline(always)] @@ -117,7 +125,7 @@ impl SdkRuntime from_path: &impl Path, to_path: &impl Path, ) -> Result<(), RuntimeError> { - self.host.store_move(from_path, to_path) + self.host.borrow_mut().store_move(from_path, to_path) } #[inline(always)] @@ -126,7 +134,7 @@ impl SdkRuntime from_path: &impl Path, to_path: &impl Path, ) -> Result<(), RuntimeError> { - self.host.store_copy(from_path, to_path) + self.host.borrow_mut().store_copy(from_path, to_path) } #[inline(always)] @@ -135,22 +143,22 @@ impl SdkRuntime hash: &[u8; PREIMAGE_HASH_SIZE], destination: &mut [u8], ) -> Result { - self.host.reveal_preimage(hash, destination) + self.host.borrow().reveal_preimage(hash, destination) } #[inline(always)] fn store_value_size(&self, path: &impl Path) -> Result { - self.host.store_value_size(path) + self.host.borrow().store_value_size(path) } #[inline(always)] fn mark_for_reboot(&mut self) -> Result<(), RuntimeError> { - self.host.mark_for_reboot() + self.host.borrow_mut().mark_for_reboot() } #[inline(always)] fn reveal_metadata(&self) -> RollupMetadata { - self.host.reveal_metadata() + self.host.borrow().reveal_metadata() } #[inline(always)] @@ -162,42 +170,43 @@ impl SdkRuntime destination: &mut [u8], ) -> Result { self.host + .borrow() .reveal_dal_page(published_level, slot_index, page_index, destination) } #[inline(always)] fn reveal_dal_parameters(&self) -> RollupDalParameters { - self.host.reveal_dal_parameters() + self.host.borrow().reveal_dal_parameters() } #[inline(always)] fn last_run_aborted(&self) -> Result { - self.host.last_run_aborted() + self.host.borrow().last_run_aborted() } #[inline(always)] fn upgrade_failed(&self) -> Result { - self.host.upgrade_failed() + self.host.borrow().upgrade_failed() } #[inline(always)] fn restart_forced(&self) -> Result { - self.host.restart_forced() + self.host.borrow().restart_forced() } #[inline(always)] fn reboot_left(&self) -> Result { - self.host.reboot_left() + self.host.borrow().reboot_left() } #[inline(always)] fn runtime_version(&self) -> Result { - self.host.runtime_version() + self.host.borrow().runtime_version() } } -impl InternalRuntime - for KernelHost<&mut Host, &mut Internal> +impl + BorrowMut, Internal: InternalRuntime> InternalRuntime + for KernelHost { #[inline(always)] fn __internal_store_get_hash( @@ -208,11 +217,43 @@ impl InternalRuntime } } -impl ExtendedRuntime - for KernelHost<&mut Host, &mut Internal> +impl + Borrow, Internal: InternalRuntime> ExtendedRuntime + for KernelHost { #[inline(always)] fn store_get_hash(&mut self, path: &T) -> Result, RuntimeError> { self.__internal_store_get_hash(path) } } + +impl + BorrowMut, Internal> KernelHost +where for<'a> &'a mut Host: BorrowMut +{ + pub fn to_ref_host(&mut self) -> KernelHost { + KernelHost { + host: &mut self.host, + internal: &mut self.internal, + _pd: PhantomData, + } + } +} + +pub type MockKernelHost = KernelHost; + +impl Default for MockKernelHost { + fn default() -> Self { + Self { + host: MockHost::default(), + internal: MockInternal(), + _pd: PhantomData + } + } +} + +impl MockKernelHost { + pub fn with_address(address: SmartRollupAddress) -> Self { + let host = MockHost::with_address(&address); + let internal = MockInternal(); + KernelHost { host, internal, _pd: PhantomData } + } +}