diff --git a/etherlink/kernel_evm/kernel/src/block.rs b/etherlink/kernel_evm/kernel/src/block.rs index 3593f0bbd148c054486400cca344b021da346d5a..5318d34c8ff28ddec49c890b8e37673d9a58c4f4 100644 --- a/etherlink/kernel_evm/kernel/src/block.rs +++ b/etherlink/kernel_evm/kernel/src/block.rs @@ -246,17 +246,35 @@ enum BlueprintParsing { } #[cfg_attr(feature = "benchmark", inline(never))] -#[allow(clippy::too_many_arguments)] fn next_bip_from_blueprints( host: &mut Host, - current_block_number: U256, - current_block_parent_hash: H256, - previous_timestamp: Timestamp, tick_counter: &TickCounter, config: &mut Configuration, kernel_upgrade: &Option, minimum_base_fee_per_gas: U256, ) -> Result { + let ( + current_block_number, + current_block_parent_hash, + previous_timestamp, + receipts_root, + transactions_root, + ) = match block_storage::read_current(host) { + Ok(block) => ( + block.number + 1, + block.hash, + block.timestamp, + block.receipts_root, + block.transactions_root, + ), + Err(_) => ( + U256::zero(), + GENESIS_PARENT_HASH, + Timestamp::from(0), + vec![0; 32], + vec![0; 32], + ), + }; let (blueprint, size) = read_blueprint( host, config, @@ -287,6 +305,8 @@ fn next_bip_from_blueprints( current_block_parent_hash, tick_counter.c, gas_price, + receipts_root, + transactions_root, ); tezos_evm_logging::log!( @@ -305,8 +325,6 @@ fn compute_bip( host: &mut Host, outbox_queue: &OutboxQueue<'_, impl Path>, mut block_in_progress: BlockInProgress, - previous_receipts_root: &[u8], - previous_transactions_root: &[u8], precompiles: &PrecompileBTreeMap, evm_account_storage: &mut EthereumAccountStorage, tick_counter: &mut TickCounter, @@ -355,12 +373,7 @@ fn compute_bip( *tick_counter = TickCounter::finalize(block_in_progress.estimated_ticks_in_run); let new_block = block_in_progress - .finalize_and_store( - host, - &constants, - previous_receipts_root, - previous_transactions_root, - ) + .finalize_and_store(host, &constants) .context("Failed to finalize the block in progress")?; Ok(BlockComputationResult::Finished { included_delayed_transactions, @@ -461,28 +474,6 @@ pub fn produce( // in blocks is set to the pool address. let coinbase = sequencer_pool_address.unwrap_or_default(); - let ( - current_block_number, - current_block_parent_hash, - previous_timestamp, - previous_receipts_root, - previous_transactions_root, - ) = match block_storage::read_current(host) { - Ok(block) => ( - block.number + 1, - block.hash, - block.timestamp, - block.receipts_root, - block.transactions_root, - ), - Err(_) => ( - U256::zero(), - GENESIS_PARENT_HASH, - Timestamp::from(0), - vec![0; 32], - vec![0; 32], - ), - }; let mut evm_account_storage = init_account_storage().context("Failed to initialize EVM account storage")?; let mut tick_counter = TickCounter::new(0u64); @@ -493,13 +484,11 @@ pub fn produce( precompiles::precompile_set::>(config.enable_fa_bridge); // Check if there's a BIP in storage to resume its execution - let (processed_blueprint, block_in_progress_provenance, block_in_progress) = + let (block_in_progress_provenance, block_in_progress) = match storage::read_block_in_progress(&safe_host)? { - Some(block_in_progress) => ( - block_in_progress.number, - BlockInProgressProvenance::Storage, - block_in_progress, - ), + Some(block_in_progress) => { + (BlockInProgressProvenance::Storage, block_in_progress) + } None => { // Using `safe_host.host` allows to escape from the failsafe storage, which is necessary // because the sequencer pool address is located outside of `/evm/world_state`. @@ -508,9 +497,6 @@ pub fn produce( // Execute at most one of the stored blueprints let block_in_progress = match next_bip_from_blueprints( safe_host.host, - current_block_number, - current_block_parent_hash, - previous_timestamp, &tick_counter, config, &kernel_upgrade, @@ -530,20 +516,15 @@ pub fn produce( // We are going to execute a new block, we copy the storage to allow // to revert if the block fails. safe_host.start()?; - ( - current_block_number, - BlockInProgressProvenance::Blueprint, - *block_in_progress, - ) + (BlockInProgressProvenance::Blueprint, *block_in_progress) } }; + let processed_blueprint = block_in_progress.number; match compute_bip( &mut safe_host, &outbox_queue, block_in_progress, - &previous_receipts_root, - &previous_transactions_root, &precompiles, &mut evm_account_storage, &mut tick_counter, @@ -1322,6 +1303,8 @@ mod tests { U256::from(1), transactions, block_constants.block_fees.base_fee_per_gas(), + vec![0; 32], + vec![0; 32], ); // run is almost full wrt ticks let limits = Limits::default(); diff --git a/etherlink/kernel_evm/kernel/src/block_in_progress.rs b/etherlink/kernel_evm/kernel/src/block_in_progress.rs index 32670f99833bdbfed74e70d13f9b3c80905c5f87..0726000fa5d1c96714edbcbdecf869ca1de2edd7 100644 --- a/etherlink/kernel_evm/kernel/src/block_in_progress.rs +++ b/etherlink/kernel_evm/kernel/src/block_in_progress.rs @@ -19,6 +19,7 @@ use primitive_types::{H160, H256, U256}; use rlp::{Decodable, DecoderError, Encodable}; use std::collections::VecDeque; use tezos_ethereum::block::{BlockConstants, BlockFees, L2Block}; +use tezos_ethereum::eth_gen::OwnedHash; use tezos_ethereum::rlp_helpers::*; use tezos_ethereum::transaction::{ IndexedLog, TransactionHash, TransactionObject, TransactionReceipt, @@ -59,6 +60,10 @@ pub struct BlockInProgress { /// The base fee, is adjusted before and after the computation of /// the block pub base_fee_per_gas: U256, + /// Receipts root + pub previous_receipts_root: OwnedHash, + /// Transactions root + pub previous_transactions_root: OwnedHash, } impl Encodable for BlockInProgress { @@ -77,8 +82,10 @@ impl Encodable for BlockInProgress { logs_offset, timestamp, base_fee_per_gas, + previous_receipts_root, + previous_transactions_root, } = self; - stream.begin_list(12); + stream.begin_list(14); stream.append(number); append_queue(stream, tx_queue); append_txs(stream, valid_txs); @@ -91,6 +98,8 @@ impl Encodable for BlockInProgress { stream.append(logs_offset); append_timestamp(stream, *timestamp); stream.append(base_fee_per_gas); + stream.append(previous_receipts_root); + stream.append(previous_transactions_root); } } @@ -113,7 +122,7 @@ impl Decodable for BlockInProgress { if !decoder.is_list() { return Err(DecoderError::RlpExpectedToBeList); } - if decoder.item_count()? != 12 { + if decoder.item_count()? != 14 { return Err(DecoderError::RlpIncorrectListLen); } @@ -131,6 +140,11 @@ impl Decodable for BlockInProgress { let logs_offset: u64 = decode_field(&next(&mut it)?, "logs_offset")?; let timestamp = decode_timestamp(&next(&mut it)?)?; let base_fee_per_gas = decode_field(&next(&mut it)?, "base_fee_per_gas")?; + let previous_receipts_root: OwnedHash = + decode_field(&next(&mut it)?, "previous_receipts_root")?; + let previous_transactions_root: OwnedHash = + decode_field(&next(&mut it)?, "previous_transactions_root")?; + let bip = Self { number, tx_queue, @@ -145,6 +159,8 @@ impl Decodable for BlockInProgress { logs_offset, timestamp, base_fee_per_gas, + previous_receipts_root, + previous_transactions_root, }; Ok(bip) } @@ -181,6 +197,7 @@ impl BlockInProgress { self.tx_queue.len() } + #[allow(clippy::too_many_arguments)] pub fn new_with_ticks( number: U256, parent_hash: H256, @@ -188,6 +205,8 @@ impl BlockInProgress { estimated_ticks_in_run: u64, timestamp: Timestamp, base_fee_per_gas: U256, + previous_receipts_root: OwnedHash, + previous_transactions_root: OwnedHash, ) -> Self { Self { number, @@ -203,6 +222,8 @@ impl BlockInProgress { logs_offset: 0, timestamp, base_fee_per_gas, + previous_receipts_root, + previous_transactions_root, } } @@ -212,6 +233,8 @@ impl BlockInProgress { number: U256, transactions: VecDeque, base_fee_per_gas: U256, + receipts_root: OwnedHash, + transactions_root: OwnedHash, ) -> BlockInProgress { Self::new_with_ticks( number, @@ -220,6 +243,8 @@ impl BlockInProgress { 0u64, Timestamp::from(0i64), base_fee_per_gas, + receipts_root, + transactions_root, ) } @@ -256,6 +281,8 @@ impl BlockInProgress { parent_hash: H256, tick_counter: u64, base_fee_per_gas: U256, + receipts_root: OwnedHash, + transactions_root: OwnedHash, ) -> BlockInProgress { // blueprint is turn into a ring to allow popping from the front let ring = blueprint.transactions.into(); @@ -266,6 +293,8 @@ impl BlockInProgress { tick_counter, blueprint.timestamp, base_fee_per_gas, + receipts_root, + transactions_root, ) } @@ -413,13 +442,11 @@ impl BlockInProgress { self, host: &mut Host, block_constants: &BlockConstants, - previous_receipts_root: &[u8], - previous_transactions_root: &[u8], ) -> Result { let state_root = Self::safe_store_get_hash(host, &EVM_ACCOUNTS_PATH)?; - let receipts_root = self.receipts_root(host, previous_receipts_root)?; + let receipts_root = self.receipts_root(host, &self.previous_receipts_root)?; let transactions_root = - self.transactions_root(host, previous_transactions_root)?; + self.transactions_root(host, &self.previous_transactions_root)?; let base_fee_per_gas = base_fee_per_gas( host, self.timestamp, @@ -621,10 +648,12 @@ mod tests { logs_offset: 33, timestamp: Timestamp::from(0i64), base_fee_per_gas: U256::from(21000u64), + previous_receipts_root: vec![0; 32], + previous_transactions_root: vec![0; 32], }; let encoded = bip.rlp_bytes(); - let expected = "f902622af8e6f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f871a00808080808080808080808080808080808080808080808080808080808080808f84e01b84bf84908080880088034a00808080808080808080808080808080808080808080808080808080808080808a00808080808080808080808080808080808080808080808080808080808080808f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021880000000000000000825208"; + let expected = "f902a42af8e6f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f871a00808080808080808080808080808080808080808080808080808080808080808f84e01b84bf84908080880088034a00808080808080808080808080808080808080808080808080808080808080808a00808080808080808080808080808080808080808080808080808080808080808f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); @@ -657,10 +686,12 @@ mod tests { logs_offset: 0, timestamp: Timestamp::from(0i64), base_fee_per_gas: U256::from(21000u64), + previous_receipts_root: vec![0; 32], + previous_transactions_root: vec![0; 32], }; let encoded = bip.rlp_bytes(); - let expected = "f902192af87cf83ca00101010101010101010101010101010101010101010101010101010101010101da02d8019401010101010101010101010101010101010101010180f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909e1a002020202020202020202020202020202020202020202020202020202020202020304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080880000000000000000825208"; + let expected = "f9025b2af87cf83ca00101010101010101010101010101010101010101010101010101010101010101da02d8019401010101010101010101010101010101010101010180f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909e1a002020202020202020202020202020202020202020202020202020202020202020304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); @@ -693,10 +724,12 @@ mod tests { logs_offset: 4, timestamp: Timestamp::from(0i64), base_fee_per_gas: U256::from(21000u64), + previous_receipts_root: vec![0; 32], + previous_transactions_root: vec![0; 32], }; let encoded = bip.rlp_bytes(); - let expected = "f9022d2af8b1f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004880000000000000000825208"; + let expected = "f9026f2af8b1f871a00101010101010101010101010101010101010101010101010101010101010101f84e01b84bf84901010180018026a00101010101010101010101010101010101010101010101010101010101010101a00101010101010101010101010101010101010101010101010101010101010101f83ca00808080808080808080808080808080808080808080808080808080808080808da02d8089408080808080808080808080808080808080808080180f842a00202020202020202020202020202020202020202020202020202020202020202a00909090909090909090909090909090909090909090909090909090909090909c00304a0050505050505050505050505050505050505050505050505050505050505050563b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004880000000000000000825208a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000"; pretty_assertions::assert_str_eq!(hex::encode(encoded), expected); diff --git a/etherlink/kernel_evm/kernel/src/gas_price.rs b/etherlink/kernel_evm/kernel/src/gas_price.rs index c74e18669bcdae451a483840d06c471729dfef53..16618b3b16a5813b654b58227a8511c49c7c4f02 100644 --- a/etherlink/kernel_evm/kernel/src/gas_price.rs +++ b/etherlink/kernel_evm/kernel/src/gas_price.rs @@ -204,12 +204,14 @@ mod test { 0, timestamp.into(), U256::zero(), + vec![0; 32], + vec![0; 32], ); bip.estimated_ticks_in_block = TOLERANCE; register_block(&mut host, &bip).unwrap(); bip.clone() - .finalize_and_store(&mut host, &dummy_block_constants, &[], &[]) + .finalize_and_store(&mut host, &dummy_block_constants) .unwrap(); // At tolerance, gas price should be min. @@ -222,7 +224,7 @@ mod test { // register more blocks - now double tolerance bip.number = 1.into(); register_block(&mut host, &bip).unwrap(); - bip.finalize_and_store(&mut host, &dummy_block_constants, &[], &[]) + bip.finalize_and_store(&mut host, &dummy_block_constants) .unwrap(); let gas_price_now = base_fee_per_gas(&host, timestamp.into(), min);