From 83289dff387b76ae57702061d5b1a38893a99bb6 Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Mon, 24 Apr 2023 14:56:14 +0200 Subject: [PATCH] EVM/Kernel: Move L2Block/BlocksConstants from kernel/evm-exec in tezos_ethereum --- src/kernel_evm/ethereum/src/block.rs | 126 ++++++++++++++++++ src/kernel_evm/ethereum/src/eth_gen.rs | 4 - src/kernel_evm/ethereum/src/lib.rs | 1 + src/kernel_evm/ethereum/src/transaction.rs | 8 +- src/kernel_evm/evm_execution/src/block.rs | 67 ---------- src/kernel_evm/evm_execution/src/handler.rs | 27 ++-- src/kernel_evm/evm_execution/src/lib.rs | 26 ++-- .../evm_execution/src/precompiles.rs | 2 +- src/kernel_evm/kernel/src/block.rs | 71 +--------- src/kernel_evm/kernel/src/genesis.rs | 3 +- src/kernel_evm/kernel/src/lib.rs | 7 +- src/kernel_evm/kernel/src/storage.rs | 35 ++--- 12 files changed, 184 insertions(+), 193 deletions(-) create mode 100644 src/kernel_evm/ethereum/src/block.rs delete mode 100644 src/kernel_evm/evm_execution/src/block.rs diff --git a/src/kernel_evm/ethereum/src/block.rs b/src/kernel_evm/ethereum/src/block.rs new file mode 100644 index 000000000000..330b8b1ee722 --- /dev/null +++ b/src/kernel_evm/ethereum/src/block.rs @@ -0,0 +1,126 @@ +// SPDX-FileCopyrightText: 2022-2023 TriliTech +// SPDX-FileCopyrightText: 2023 Nomadic Labs +// +// SPDX-License-Identifier: MIT + +use crate::eth_gen::OwnedHash; +use crate::transaction::TransactionHash; +use primitive_types::{H160, H256, U256}; + +/// All data for an Ethereum block. +/// +/// This data does not change for the duration of the block. All balues are +/// updated when the block is finalized and may change for the next block. +pub struct BlockConstants { + /// Price of one unit of gas in Wei + pub gas_price: U256, + /// The number of the current block + pub number: U256, + /// Who is the beneficiary of the current block + pub coinbase: H160, + /// Unix date/time of the current block - when was the previous block finished + pub timestamp: U256, + /// Mining difficulty of the current block. This relates to PoW, and we can set + /// the value to an arbitrary value. + pub difficulty: U256, + /// Gas limit for the current block. + pub gas_limit: U256, + /// The base fee per gas for doing a transaction. + pub base_fee_per_gas: U256, + /// Identifier for the chain. Normally this would identify the chain (Ethereum + /// main net, or some other net). We can use it to identify rollup EVM kernel. + pub chain_id: U256, +} + +impl BlockConstants { + /// Return the first block of the chain (genisis). + /// TODO find suitable values for gas_limit et.c. + /// To be done in . + pub fn first_block() -> Self { + Self { + gas_price: U256::one(), + number: U256::zero(), + coinbase: H160::zero(), + timestamp: U256::zero(), + difficulty: U256::zero(), + gas_limit: U256::one(), + base_fee_per_gas: U256::one(), + chain_id: U256::zero(), + } + } +} + +pub struct L2Block { + // This choice of a L2 block representation is totally + // arbitrarily based on what is an Ethereum block and is + // subject to change. + pub number: U256, + pub hash: H256, + pub parent_hash: H256, + pub nonce: U256, + pub sha3_uncles: OwnedHash, + pub logs_bloom: Option, + pub transactions_root: OwnedHash, + pub state_root: OwnedHash, + pub receipts_root: OwnedHash, + pub miner: OwnedHash, + pub difficulty: U256, + pub total_difficulty: U256, + pub extra_data: OwnedHash, + pub size: U256, + pub gas_limit: U256, + pub gas_used: U256, + pub timestamp: U256, + pub transactions: Vec, + pub uncles: Vec, +} + +impl L2Block { + const DUMMY_HASH: &str = "0000000000000000000000000000000000000000"; + const BLOCK_HASH_SIZE: usize = 32; + + fn dummy_hash() -> OwnedHash { + L2Block::DUMMY_HASH.into() + } + + fn dummy_block_hash() -> H256 { + H256([0; L2Block::BLOCK_HASH_SIZE]) + } + + pub fn new(number: U256, transactions: Vec) -> Self { + L2Block { + number, + hash: H256(number.into()), + parent_hash: L2Block::dummy_block_hash(), + nonce: U256::zero(), + sha3_uncles: L2Block::dummy_hash(), + logs_bloom: None, + transactions_root: L2Block::dummy_hash(), + state_root: L2Block::dummy_hash(), + receipts_root: L2Block::dummy_hash(), + miner: L2Block::dummy_hash(), + difficulty: U256::zero(), + total_difficulty: U256::zero(), + extra_data: L2Block::dummy_hash(), + size: U256::zero(), + gas_limit: U256::one(), + gas_used: U256::zero(), + timestamp: U256::zero(), + transactions, + uncles: Vec::new(), + } + } + + pub fn constants(&self) -> BlockConstants { + BlockConstants { + gas_price: U256::one(), + number: self.number, + coinbase: H160::zero(), + timestamp: self.timestamp, + difficulty: self.difficulty, + gas_limit: self.gas_limit, + base_fee_per_gas: U256::one(), + chain_id: U256::zero(), + } + } +} diff --git a/src/kernel_evm/ethereum/src/eth_gen.rs b/src/kernel_evm/ethereum/src/eth_gen.rs index 1d69aea151bc..3495d7af563d 100644 --- a/src/kernel_evm/ethereum/src/eth_gen.rs +++ b/src/kernel_evm/ethereum/src/eth_gen.rs @@ -2,9 +2,5 @@ // // SPDX-License-Identifier: MIT -pub const BLOCK_HASH_SIZE: usize = 32; -pub type BlockHash = [u8; BLOCK_HASH_SIZE]; - -pub type L2Level = u64; pub type OwnedHash = Vec; pub type Hash<'a> = &'a Vec; diff --git a/src/kernel_evm/ethereum/src/lib.rs b/src/kernel_evm/ethereum/src/lib.rs index 39fb28f179da..74c54cdd65d6 100644 --- a/src/kernel_evm/ethereum/src/lib.rs +++ b/src/kernel_evm/ethereum/src/lib.rs @@ -4,6 +4,7 @@ pub mod account; pub mod address; +pub mod block; pub mod eth_gen; pub mod signatures; pub mod transaction; diff --git a/src/kernel_evm/ethereum/src/transaction.rs b/src/kernel_evm/ethereum/src/transaction.rs index e44eac39203b..1cae7399cb4d 100644 --- a/src/kernel_evm/ethereum/src/transaction.rs +++ b/src/kernel_evm/ethereum/src/transaction.rs @@ -3,9 +3,9 @@ // SPDX-License-Identifier: MIT use crate::address::EthereumAddress; -use primitive_types::U256; +use primitive_types::{H256, U256}; -use crate::eth_gen::{BlockHash, L2Level, OwnedHash}; +use crate::eth_gen::OwnedHash; pub const TRANSACTION_HASH_SIZE: usize = 32; pub type TransactionHash = [u8; TRANSACTION_HASH_SIZE]; @@ -60,9 +60,9 @@ pub struct TransactionReceipt { /// Integer of the transactions index position in the block. pub index: u32, /// Hash of the block where this transaction was in. - pub block_hash: BlockHash, + pub block_hash: H256, /// Block number where this transaction was in. - pub block_number: L2Level, + pub block_number: U256, /// Address of the sender. pub from: EthereumAddress, /// Address of the receiver. null when its a contract creation transaction. diff --git a/src/kernel_evm/evm_execution/src/block.rs b/src/kernel_evm/evm_execution/src/block.rs deleted file mode 100644 index b0898c3b8d59..000000000000 --- a/src/kernel_evm/evm_execution/src/block.rs +++ /dev/null @@ -1,67 +0,0 @@ -// SPDX-FileCopyrightText: 2022 TriliTech -// -// SPDX-License-Identifier: MIT - -//! All that is constant for a block -//! -//! The Ethereum blocks on the rollup correspond to rollup inbox levels. -//! When then kernel "sees" an EOL from the PVM, it finalizes the current block -//! and starts creating the next one. - -use super::storage::blocks::get_current_number_n_timestamp; -use host::runtime::Runtime; -use primitive_types::{H160, U256}; -/// All data for an Ethereum block. -/// -/// This data does not change for the duration of the block. All balues are -/// updated when the block is finalized and may change for the next block. -pub struct BlockConstants { - /// Price of one unit of gas in Wei - pub gas_price: U256, - /// The number of the current block - pub number: U256, - /// Who is the beneficiary of the current block - pub coinbase: H160, - /// Unix date/time of the current block - when was the previous block finished - pub timestamp: U256, - /// Mining difficulty of the current block. This relates to PoW, and we can set - /// the value to an arbitrary value. - pub difficulty: U256, - /// Gas limit for the current block. - pub gas_limit: U256, - /// The base fee per gas for doing a transaction. - pub base_fee_per_gas: U256, - /// Identifier for the chain. Normally this would identify the chain (Ethereum - /// main net, or some other net). We can use it to identify rollup EVM kernel. - pub chain_id: U256, -} - -impl BlockConstants { - /// Return the first block of the chain (genisis). - /// TODO find suitable values for gas_limit et.c. - /// To be done in . - pub fn first_block() -> Self { - Self { - gas_price: U256::one(), - number: U256::zero(), - coinbase: H160::zero(), - timestamp: U256::zero(), - difficulty: U256::zero(), - gas_limit: U256::one(), - base_fee_per_gas: U256::one(), - chain_id: U256::zero(), - } - } - - /// Retrieve the current block constants from storage - pub fn from_storage(host: &Host) -> Self { - // This is a stub for now - just return the first block - let (number, timestamp) = get_current_number_n_timestamp(host) - .expect("Number and timestamp expected to exist before messages processing"); - BlockConstants { - number, - timestamp, - ..BlockConstants::first_block() - } - } -} diff --git a/src/kernel_evm/evm_execution/src/handler.rs b/src/kernel_evm/evm_execution/src/handler.rs index c07ade6d6107..3229ce3562ea 100644 --- a/src/kernel_evm/evm_execution/src/handler.rs +++ b/src/kernel_evm/evm_execution/src/handler.rs @@ -11,7 +11,6 @@ use crate::account_storage::{ account_path, AccountStorageError, EthereumAccount, EthereumAccountStorage, CODE_HASH_DEFAULT, }; -use crate::block::BlockConstants; use crate::storage; use crate::transaction::{ begin_transaction, commit_transaction, rollback_transaction, TransactionContext, @@ -31,6 +30,7 @@ use evm::{ use host::runtime::Runtime; use primitive_types::{H160, H256, U256}; use sha3::{Digest, Keccak256}; +use tezos_ethereum::block::BlockConstants; /// Maximum transaction stack depth. const MAXIMUM_TRANSACTION_DEPTH: usize = 1024_usize; @@ -927,7 +927,6 @@ impl<'a, Host: Runtime> Handler for EvmHandler<'a, Host> { mod test { use super::*; use crate::account_storage::init_account_storage; - use crate::block; use crate::precompiles; use evm::Config; use primitive_types::{H160, H256}; @@ -979,7 +978,7 @@ mod test { #[test] fn legacy_create_to_correct_address() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1016,7 +1015,7 @@ mod test { #[test] fn create2_to_correct_address() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1052,7 +1051,7 @@ mod test { #[test] fn create2_to_correct_address_nonzero_salt() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1091,7 +1090,7 @@ mod test { #[test] fn origin_instruction_returns_origin_address() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1158,7 +1157,7 @@ mod test { #[test] fn contract_call_produces_correct_output() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1260,7 +1259,7 @@ mod test { #[test] fn contract_call_fails_beyond_max_transaction_depth() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1363,7 +1362,7 @@ mod test { #[test] fn contract_call_succeeds_at_maximum_transaction_depth() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1458,7 +1457,7 @@ mod test { #[test] fn contract_can_use_durable_storage() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1533,7 +1532,7 @@ mod test { #[test] fn contract_create_can_use_durable_storage() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1580,7 +1579,7 @@ mod test { #[test] fn contract_call_does_transfer() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1646,7 +1645,7 @@ mod test { #[test] fn no_transfer_when_contract_call_fails() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); @@ -1716,7 +1715,7 @@ mod test { #[test] fn contract_call_fails_when_insufficient_funds_for_transfer() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_account_storage().unwrap(); let config = Config::london(); diff --git a/src/kernel_evm/evm_execution/src/lib.rs b/src/kernel_evm/evm_execution/src/lib.rs index 1011a1656701..4afa0ea47f93 100644 --- a/src/kernel_evm/evm_execution/src/lib.rs +++ b/src/kernel_evm/evm_execution/src/lib.rs @@ -12,11 +12,11 @@ use evm::executor::stack::PrecompileFailure; use evm::{Config, ExitError, ExitFatal}; use host::runtime::Runtime; use primitive_types::{H160, U256}; +use tezos_ethereum::block::BlockConstants; use tezos_smart_rollup_storage::StorageError; use thiserror::Error; pub mod account_storage; -pub mod block; pub mod handler; pub mod precompiles; pub mod storage; @@ -77,7 +77,7 @@ impl From for EthereumError { #[allow(clippy::too_many_arguments)] pub fn run_transaction<'a, Host>( host: &'a mut Host, - block: &'a block::BlockConstants, + block: &'a BlockConstants, evm_account_storage: &'a mut EthereumAccountStorage, precompiles: &'a precompiles::PrecompileBTreeMap, address: H160, @@ -189,7 +189,7 @@ mod test { #[test] fn transfer_without_sufficient_funds_fails() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -245,7 +245,7 @@ mod test { #[test] fn transfer_funds_with_sufficient_balance() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -302,7 +302,7 @@ mod test { #[test] fn create_contract_fails_with_insufficient_funds() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -347,7 +347,7 @@ mod test { #[test] fn create_contract_succeeds_with_valid_initialization() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -390,7 +390,7 @@ mod test { #[test] fn create_contract_fails_when_initialization_fails() { let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -439,7 +439,7 @@ mod test { fn call_non_existing_contract() { // Arange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); let target = H160::from_low_u64_be(117u64); @@ -562,7 +562,7 @@ mod test { fn call_simple_return_contract() { // Arrange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); let target = H160::from_low_u64_be(117u64); @@ -606,7 +606,7 @@ mod test { fn call_simple_revert_contract() { // Arrange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); let target = H160::from_low_u64_be(117u64); @@ -650,7 +650,7 @@ mod test { fn call_contract_with_invalid_opcode() { // Arrange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); let target = H160::from_low_u64_be(117u64); @@ -695,7 +695,7 @@ mod test { fn call_precompiled_contract() { // Arrange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let target = H160::from_low_u64_be(4u64); let mut evm_account_storage = init_evm_account_storage().unwrap(); @@ -730,7 +730,7 @@ mod test { fn create_and_call_contract() { // Arrange let mut mock_runtime = MockHost::default(); - let block = block::BlockConstants::first_block(); + let block = BlockConstants::first_block(); let precompiles = precompiles::precompile_set::(); let mut evm_account_storage = init_evm_account_storage().unwrap(); let target = H160::from_low_u64_be(117u64); diff --git a/src/kernel_evm/evm_execution/src/precompiles.rs b/src/kernel_evm/evm_execution/src/precompiles.rs index 3459dea4abe4..71c7eb6ad7f4 100644 --- a/src/kernel_evm/evm_execution/src/precompiles.rs +++ b/src/kernel_evm/evm_execution/src/precompiles.rs @@ -175,9 +175,9 @@ pub fn precompile_set() -> PrecompileBTreeMap { mod tests { use super::*; use crate::account_storage::init_account_storage as init_evm_account_storage; - use crate::block::BlockConstants; use evm::Config; use primitive_types::{H160, U256}; + use tezos_ethereum::block::BlockConstants; use tezos_smart_rollup_mock::MockHost; fn execute_precompiled( diff --git a/src/kernel_evm/kernel/src/block.rs b/src/kernel_evm/kernel/src/block.rs index 29b3ff72379d..7c385b22bf95 100644 --- a/src/kernel_evm/kernel/src/block.rs +++ b/src/kernel_evm/kernel/src/block.rs @@ -14,79 +14,12 @@ use tezos_smart_rollup_host::runtime::Runtime; use primitive_types::U256; use tezos_ethereum::account::Account; -use tezos_ethereum::eth_gen::{BlockHash, L2Level, OwnedHash, BLOCK_HASH_SIZE}; +use tezos_ethereum::block::L2Block; use tezos_ethereum::transaction::{ - TransactionHash, TransactionReceipt, TransactionStatus, TransactionType, + TransactionReceipt, TransactionStatus, TransactionType, }; - use tezos_ethereum::wei::Wei; -pub struct L2Block { - // This choice of a L2 block representation is totally - // arbitrarily based on what is an Ethereum block and is - // subject to change. - pub number: L2Level, - pub hash: BlockHash, - pub parent_hash: BlockHash, - pub nonce: U256, - pub sha3_uncles: OwnedHash, - pub logs_bloom: Option, - pub transactions_root: OwnedHash, - pub state_root: OwnedHash, - pub receipts_root: OwnedHash, - pub miner: OwnedHash, - pub difficulty: U256, - pub total_difficulty: U256, - pub extra_data: OwnedHash, - pub size: U256, - pub gas_limit: U256, - pub gas_used: U256, - pub timestamp: U256, - pub transactions: Vec, - pub uncles: Vec, -} - -impl L2Block { - const DUMMY_QUANTITY: U256 = U256::zero(); - const DUMMY_HASH: &str = "0000000000000000000000000000000000000000"; - - fn dummy_hash() -> OwnedHash { - L2Block::DUMMY_HASH.into() - } - - fn dummy_block_hash() -> BlockHash { - [0; BLOCK_HASH_SIZE] - } - - pub fn new(number: L2Level, transactions: Vec) -> Self { - let hash: BlockHash = { - let number: U256 = number.into(); - number.into() - }; - L2Block { - number, - hash, - parent_hash: L2Block::dummy_block_hash(), - nonce: L2Block::DUMMY_QUANTITY, - sha3_uncles: L2Block::dummy_hash(), - logs_bloom: None, - transactions_root: L2Block::dummy_hash(), - state_root: L2Block::dummy_hash(), - receipts_root: L2Block::dummy_hash(), - miner: L2Block::dummy_hash(), - difficulty: L2Block::DUMMY_QUANTITY, - total_difficulty: L2Block::DUMMY_QUANTITY, - extra_data: L2Block::dummy_hash(), - size: L2Block::DUMMY_QUANTITY, - gas_limit: L2Block::DUMMY_QUANTITY, - gas_used: L2Block::DUMMY_QUANTITY, - timestamp: L2Block::DUMMY_QUANTITY, - transactions, - uncles: Vec::new(), - } - } -} - fn get_tx_sender( tx: &EthereumTransactionCommon, ) -> Result<(OwnedPath, EthereumAddress), Error> { diff --git a/src/kernel_evm/kernel/src/genesis.rs b/src/kernel_evm/kernel/src/genesis.rs index 64e2bf42cb79..6182a4c0bd68 100644 --- a/src/kernel_evm/kernel/src/genesis.rs +++ b/src/kernel_evm/kernel/src/genesis.rs @@ -9,7 +9,6 @@ use crate::L2Block; use primitive_types::U256; use tezos_ethereum::account::Account; use tezos_ethereum::address::EthereumAddress; -use tezos_ethereum::eth_gen::L2Level; use tezos_ethereum::transaction::TransactionHash; use tezos_ethereum::transaction::TransactionReceipt; use tezos_ethereum::transaction::TransactionStatus; @@ -28,7 +27,7 @@ struct MintAccount { const GENESIS_ADDRESSS: [u8; 20] = [0; 20]; -const GENESIS_LEVEL: L2Level = 0; +const GENESIS_LEVEL: U256 = U256::zero(); const MINT_ACCOUNTS_NUMBER: usize = 3; diff --git a/src/kernel_evm/kernel/src/lib.rs b/src/kernel_evm/kernel/src/lib.rs index 0726fa257be9..554e356d984e 100644 --- a/src/kernel_evm/kernel/src/lib.rs +++ b/src/kernel_evm/kernel/src/lib.rs @@ -2,7 +2,8 @@ // // SPDX-License-Identifier: MIT -use block::L2Block; +use primitive_types::U256; +use tezos_ethereum::block::L2Block; use tezos_smart_rollup_debug::debug_msg; use tezos_smart_rollup_entrypoint::kernel_entry; use tezos_smart_rollup_host::runtime::Runtime; @@ -50,7 +51,7 @@ pub fn stage_two(host: &mut Host, queue: Queue) -> Result<(), Err debug_msg!( host, "Block {} at number {} contains {} transaction(s).\n", - String::from_utf8(hash.to_vec()).expect("INVALID HASH"), + String::from_utf8(hash.as_bytes().to_vec()).expect("INVALID HASH"), number, transactions.len() ) @@ -78,7 +79,7 @@ fn retrieve_smart_rollup_address( } fn genesis_initialisation(host: &mut Host) -> Result<(), Error> { - let block_path = storage::block_path(0)?; + let block_path = storage::block_path(U256::zero())?; match Runtime::store_has(host, &block_path) { Ok(Some(_)) => Ok(()), _ => genesis::init_block(host), diff --git a/src/kernel_evm/kernel/src/storage.rs b/src/kernel_evm/kernel/src/storage.rs index f9c4b88c4ef4..64cd7c4b21c0 100644 --- a/src/kernel_evm/kernel/src/storage.rs +++ b/src/kernel_evm/kernel/src/storage.rs @@ -9,16 +9,16 @@ use tezos_smart_rollup_host::runtime::{Runtime, ValueType}; use std::str::from_utf8; -use crate::block::L2Block; use crate::error::{Error, StorageError}; use tezos_ethereum::account::*; -use tezos_ethereum::eth_gen::{BlockHash, Hash, L2Level}; +use tezos_ethereum::block::L2Block; +use tezos_ethereum::eth_gen::Hash; use tezos_ethereum::transaction::{ TransactionHash, TransactionReceipt, TransactionStatus, TRANSACTION_HASH_SIZE, }; use tezos_ethereum::wei::Wei; -use primitive_types::{H160, U256}; +use primitive_types::{H160, H256, U256}; const SMART_ROLLUP_ADDRESS: RefPath = RefPath::assert_from(b"/metadata/smart_rollup_address"); @@ -134,7 +134,7 @@ pub fn account_path(address: Hash) -> Result { concat(&EVM_ACCOUNTS, &address_hash).map_err(Error::from) } -pub fn block_path(number: L2Level) -> Result { +pub fn block_path(number: U256) -> Result { let number: &str = &number.to_string(); let raw_number_path: Vec = format!("/{}", &number).into(); let number_path = OwnedPath::try_from(raw_number_path)?; @@ -235,11 +235,11 @@ pub fn store_account( store_code_hash(host, account_path, &account.code_hash) } -pub fn read_current_block_number(host: &mut Host) -> Result { +pub fn read_current_block_number(host: &mut Host) -> Result { let path = concat(&EVM_CURRENT_BLOCK, &EVM_BLOCKS_NUMBER)?; let mut buffer = [0_u8; 8]; store_read_slice(host, &path, &mut buffer, 8)?; - Ok(u64::from_le_bytes(buffer)) + Ok(U256::from_little_endian(&buffer)) } fn read_nth_block_transactions( @@ -270,20 +270,23 @@ pub fn read_current_block(host: &mut Host) -> Result( host: &mut Host, block_path: &OwnedPath, - block_number: L2Level, + block_number: U256, ) -> Result<(), Error> { let path = concat(block_path, &EVM_BLOCKS_NUMBER)?; - host.store_write(&path, &u64::to_le_bytes(block_number), 0) + let mut le_block_number: [u8; 32] = [0; 32]; + block_number.to_little_endian(&mut le_block_number); + host.store_write(&path, &le_block_number, 0) .map_err(Error::from) } fn store_block_hash( host: &mut Host, block_path: &OwnedPath, - block_hash: &BlockHash, + block_hash: &H256, ) -> Result<(), Error> { let path = concat(block_path, &EVM_BLOCKS_HASH)?; - host.store_write(&path, block_hash, 0).map_err(Error::from) + host.store_write(&path, block_hash.as_bytes(), 0) + .map_err(Error::from) } fn store_block_transactions( @@ -342,14 +345,14 @@ pub fn store_transaction_receipt( host.store_write(&index_path, &receipt.index.to_le_bytes(), 0)?; // Block hash let block_hash_path = concat(receipt_path, &TRANSACTION_RECEIPT_BLOCK_HASH)?; - host.store_write(&block_hash_path, &receipt.block_hash, 0)?; + host.store_write(&block_hash_path, receipt.block_hash.as_bytes(), 0)?; // Block number let block_number_path = concat(receipt_path, &TRANSACTION_RECEIPT_BLOCK_NUMBER)?; - host.store_write( - &block_number_path, - &u64::to_le_bytes(receipt.block_number), - 0, - )?; + let mut le_receipt_block_number: [u8; 32] = [0; 32]; + receipt + .block_number + .to_little_endian(&mut le_receipt_block_number); + host.store_write(&block_number_path, &le_receipt_block_number, 0)?; // From let from_path = concat(receipt_path, &TRANSACTION_RECEIPT_FROM)?; let from: H160 = receipt.from.into(); -- GitLab