From 0c7524788a2bdd0087445bc82240cb38ca26cc3e Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Mon, 27 Feb 2023 16:10:54 +0100 Subject: [PATCH 1/5] EVM/Mockup: extend blueprint's Queue with constructor --- src/kernel_evm_mockup/src/blueprint.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/kernel_evm_mockup/src/blueprint.rs b/src/kernel_evm_mockup/src/blueprint.rs index aeeb8f9763af..dccf0dc64e60 100644 --- a/src/kernel_evm_mockup/src/blueprint.rs +++ b/src/kernel_evm_mockup/src/blueprint.rs @@ -12,6 +12,7 @@ pub struct Blueprint { pub transactions: Vec, } +#[derive(Default)] pub struct Queue { // In our case, to make it simple and straightforward it will be // an array of pendings transactions even though it'll be only a @@ -20,6 +21,12 @@ pub struct Queue { } impl Queue { + pub fn new() -> Queue { + Queue { + proposals: Vec::new(), + } + } + pub fn add(queue: &mut Queue, transactions: Vec) { queue.proposals.push(Blueprint { transactions }) } -- GitLab From 72319de512c0bba61f0bcd94ff2a37c612de14c4 Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Wed, 1 Mar 2023 11:31:39 +0100 Subject: [PATCH 2/5] EVM/Mockup: ignore hash field from block The hash is not relevant for the production and storage of block (for now). --- src/kernel_evm_mockup/src/block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kernel_evm_mockup/src/block.rs b/src/kernel_evm_mockup/src/block.rs index 812463cf3773..9285024e06b0 100644 --- a/src/kernel_evm_mockup/src/block.rs +++ b/src/kernel_evm_mockup/src/block.rs @@ -41,10 +41,10 @@ impl L2Block { L2Block::DUMMY_HASH.into() } - pub fn new(number: L2Level, hash: OwnedHash, transactions: RawTransactions) -> Self { + pub fn new(number: L2Level, transactions: RawTransactions) -> Self { L2Block { number, - hash, + hash: L2Block::dummy_hash(), parent_hash: L2Block::dummy_hash(), nonce: L2Block::DUMMY_QUANTITY, sha3_uncles: L2Block::dummy_hash(), -- GitLab From b4bcfc71153ae01a2e1261331f15e6b390536fbe Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Wed, 1 Mar 2023 10:34:35 +0100 Subject: [PATCH 3/5] EVM/Mockup: read current block number from storage --- src/kernel_evm_mockup/src/storage.rs | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/kernel_evm_mockup/src/storage.rs b/src/kernel_evm_mockup/src/storage.rs index ef6d0fb2352d..8aee05b7a90a 100644 --- a/src/kernel_evm_mockup/src/storage.rs +++ b/src/kernel_evm_mockup/src/storage.rs @@ -12,7 +12,7 @@ use std::str::from_utf8; use crate::account::*; use crate::block::L2Block; use crate::error::Error; -use crate::eth_gen::{Hash, L2Level, RawTransactions}; +use crate::eth_gen::{Hash, L2Level, RawTransaction, RawTransactions}; use crate::wei::Wei; use primitive_types::U256; @@ -29,7 +29,7 @@ const EVM_BLOCKS_NUMBER: RefPath = RefPath::assert_from(b"/number"); const EVM_BLOCKS_HASH: RefPath = RefPath::assert_from(b"/hash"); const EVM_BLOCKS_TRANSACTIONS: RefPath = RefPath::assert_from(b"/transactions"); -const CODE_HASH_SIZE: usize = 32; +const HASH_MAX_SIZE: usize = 32; /// The size of one 256 bit word. Size in bytes pub const WORD_SIZE: usize = 32usize; @@ -90,7 +90,7 @@ pub fn read_account_code_hash( account_path: &OwnedPath, ) -> Result, Error> { let path = concat(account_path, &EVM_ACCOUNT_CODE_HASH)?; - host.store_read(&path, 0, CODE_HASH_SIZE) + host.store_read(&path, 0, HASH_MAX_SIZE) .map_err(Error::from) } @@ -149,6 +149,35 @@ pub fn store_account( store_code_hash(host, &account_path, &account.code_hash) } +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]; + + match load_value_slice(host, &path, &mut buffer) { + Ok(8) => Ok(u64::from_le_bytes(buffer)), + _ => Err(Error::Generic), + } +} + +fn read_current_block_transactions( + host: &mut Host, +) -> Result { + let path = concat(&EVM_CURRENT_BLOCK, &EVM_BLOCKS_TRANSACTIONS)?; + host.store_read(&path, 0, HASH_MAX_SIZE) + .map_err(Error::from) +} + +pub fn read_current_block( + host: &mut Host, +) -> Result { + let number = read_current_block_number(host)?; + let transactions = vec![read_current_block_transactions(host)?]; + + Ok(L2Block::new(number, transactions)) +} + fn store_block_number( host: &mut Host, block_path: &OwnedPath, -- GitLab From 3d3a185ea299c83e0a6713616cb06ee8f42206b2 Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Wed, 1 Mar 2023 11:41:27 +0100 Subject: [PATCH 4/5] EVM/Mockup: block production on stage two --- src/kernel_evm_mockup/src/block.rs | 24 ++++++++++++++++++++ src/kernel_evm_mockup/src/inbox.rs | 4 ++++ src/kernel_evm_mockup/src/lib.rs | 35 +++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/kernel_evm_mockup/src/block.rs b/src/kernel_evm_mockup/src/block.rs index 9285024e06b0..8cac25841749 100644 --- a/src/kernel_evm_mockup/src/block.rs +++ b/src/kernel_evm_mockup/src/block.rs @@ -2,7 +2,12 @@ // // SPDX-License-Identifier: MIT +use crate::blueprint::Queue; use crate::eth_gen::{L2Level, OwnedHash, Quantity, RawTransactions}; +use crate::inbox; +use crate::storage; +use host::rollup_core::RawRollupCore; +use host::runtime::Runtime; pub struct L2Block { // This choice of a L2 block representation is totally @@ -65,3 +70,22 @@ impl L2Block { } } } + +pub fn produce(host: &mut Host, queue: Queue) { + for proposal in queue.proposals.iter() { + let current_level = storage::read_current_block_number(host); + let next_level = match current_level { + Ok(current_level) => current_level + 1, + Err(_) => 0, + }; + let raw_transactions = proposal + .transactions + .iter() + .map(inbox::Transaction::to_raw_transaction) + .collect(); + let next_block = L2Block::new(next_level, raw_transactions); + storage::store_current_block(host, next_block).unwrap_or_else(|_| { + panic!("Error while storing the current block: stopping the daemon.") + }) + } +} diff --git a/src/kernel_evm_mockup/src/inbox.rs b/src/kernel_evm_mockup/src/inbox.rs index afe5ecc54a43..f59c8734f671 100644 --- a/src/kernel_evm_mockup/src/inbox.rs +++ b/src/kernel_evm_mockup/src/inbox.rs @@ -28,6 +28,10 @@ impl Transaction { tx, } } + + pub fn to_raw_transaction(&self) -> RawTransaction { + self.tx.clone() + } } pub fn read_input( diff --git a/src/kernel_evm_mockup/src/lib.rs b/src/kernel_evm_mockup/src/lib.rs index 053c037c8b36..ae36aa132a48 100644 --- a/src/kernel_evm_mockup/src/lib.rs +++ b/src/kernel_evm_mockup/src/lib.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MIT +use block::L2Block; use host::rollup_core::RawRollupCore; use host::runtime::Runtime; @@ -24,7 +25,31 @@ mod storage; mod wei; pub fn stage_one(host: &mut Host) -> Queue { - fetch(host) + let queue = fetch(host); + + for (i, blueprint) in queue.proposals.iter().enumerate() { + debug_msg!(host; "Blueprint {} contains {} transactions.\n", i, blueprint.transactions.len()); + } + + queue +} + +pub fn stage_two(host: &mut Host, queue: Queue) { + block::produce(host, queue); + + if let Ok(L2Block { + number, + hash, + transactions, + .. + }) = storage::read_current_block(host) + { + debug_msg!(host; "Block {} at number {} contains {} transaction(s).\n", + String::from_utf8(hash).expect("INVALID HASH"), + number, + transactions.len() + ) + } } pub fn init_mock_account(host: &mut Host) -> Result<(), Error> { @@ -43,11 +68,9 @@ pub fn main(host: &mut Host) { Err(_) => panic!("The account should be writable"), } - // Stage 1. - let Queue { proposals } = stage_one(host); - for (i, blueprint) in proposals.iter().enumerate() { - debug_msg!(host; "Blueprint {} contains {} transactions.\n", i, blueprint.transactions.len()); - } + let queue = stage_one(host); + + stage_two(host, queue) } kernel_entry!(main); -- GitLab From 553104b9623e833a5d6ff309426ea4d2e82416d5 Mon Sep 17 00:00:00 2001 From: Rodi-Can Bozman Date: Thu, 2 Mar 2023 11:57:15 +0100 Subject: [PATCH 5/5] EVM/Mockup: introduce dummy validation mechanism for stage two --- src/kernel_evm_mockup/src/block.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/kernel_evm_mockup/src/block.rs b/src/kernel_evm_mockup/src/block.rs index 8cac25841749..0ccfc03ad21f 100644 --- a/src/kernel_evm_mockup/src/block.rs +++ b/src/kernel_evm_mockup/src/block.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT use crate::blueprint::Queue; +use crate::error::Error; use crate::eth_gen::{L2Level, OwnedHash, Quantity, RawTransactions}; use crate::inbox; use crate::storage; @@ -71,6 +72,11 @@ impl L2Block { } } +fn validate(block: L2Block) -> Result { + // TODO: https://gitlab.com/tezos/tezos/-/issues/4749 + Ok(block) +} + pub fn produce(host: &mut Host, queue: Queue) { for proposal in queue.proposals.iter() { let current_level = storage::read_current_block_number(host); @@ -83,9 +89,11 @@ pub fn produce(host: &mut Host, queue: Queue) { .iter() .map(inbox::Transaction::to_raw_transaction) .collect(); - let next_block = L2Block::new(next_level, raw_transactions); - storage::store_current_block(host, next_block).unwrap_or_else(|_| { - panic!("Error while storing the current block: stopping the daemon.") - }) + let candidate_block = L2Block::new(next_level, raw_transactions); + if let Ok(valid_block) = validate(candidate_block) { + storage::store_current_block(host, valid_block).unwrap_or_else(|_| { + panic!("Error while storing the current block: stopping the daemon.") + }) + } } } -- GitLab