diff --git a/crates/l2/operator/proof_data_provider.rs b/crates/l2/operator/proof_data_provider.rs index 3e59f52ef0..24394983b3 100644 --- a/crates/l2/operator/proof_data_provider.rs +++ b/crates/l2/operator/proof_data_provider.rs @@ -3,6 +3,7 @@ use std::{ collections::HashMap, + fmt::format, io::{BufReader, BufWriter, Read}, net::{IpAddr, TcpListener, TcpStream}, os::macos::raw::stat, @@ -13,8 +14,10 @@ use ethereum_rust_blockchain::{ find_parent_header, validate_block, validate_gas_used, validate_parent_canonical, validate_state_root, }; -use ethereum_rust_core::types::{Block, BlockBody, BlockHeader, TxKind}; -use ethereum_rust_evm::{evm_state, execute_block, get_state_transitions, RevmAddress}; +use ethereum_rust_core::types::{Block, BlockBody, BlockHeader, Receipt, Transaction, TxKind}; +use ethereum_rust_evm::{ + block_env, evm_state, execute_block, get_state_transitions, tx_env, RevmAddress, +}; use ethereum_rust_storage::Store; use ethereum_types::{Address, Bloom, H160, H256, U256}; use prover_lib::{ @@ -32,7 +35,6 @@ use crate::{ }; use revm::{ - db::CacheDB, primitives::{bitvec::view::AsBits, AccountInfo, FixedBytes, B256}, Evm, InMemoryDB, }; @@ -385,18 +387,32 @@ impl ProofDataProvider { block_is_valid: false, }; + let last_block_number = self.store.get_latest_block_number().unwrap().unwrap(); + let body = self + .store + .get_block_body(last_block_number) + .unwrap() + .unwrap(); + let header = self + .store + .get_block_header(last_block_number) + .unwrap() + .unwrap(); + + let last_block = Block { header, body }; + // we need the storage of the current_block-1 // we should execute the EVM with that state and simulating the inclusion of the current_block - let memory_db = get_last_block_state(&self.store).map_err(|e| format!("error code: {e}")); - // with the execution_outputs we should get a way to have the State/Store represented with Hashmaps - - // finally, this information has to be contained in an structure that can be de/serealized, - // so that, any zkVM can receive the state as input and prove the block execution. + let memory_db = + get_last_block_state(&self.store, &last_block).map_err(|e| format!("Error: {e}"))?; + // with the execution_outputs we should get a way to have the State/Store represented with Hashmaps. + // finally, this information has to be contained in anstructure that can be de/serealized, + // so that, any zkVM could receive the state as input and prove the block execution. let prover_inputs_execution = ProverInput { block: head_block, parent_block_header, - db: MemoryDB::default(), + db: memory_db, }; let response = match prover_mode { @@ -435,21 +451,18 @@ impl ProofDataProvider { } /// Same concept as adding a block [ethereum_rust_blockchain::add_block]. -fn get_last_block_state(storage: &Store) -> Result> { - let last_block_number = storage.get_latest_block_number()?.unwrap(); - let body = storage.get_block_body(last_block_number)?.unwrap(); - let header = storage.get_block_header(last_block_number)?.unwrap(); - - let last_block = Block { header, body }; - - validate_parent_canonical(&last_block, storage)?; +fn get_last_block_state( + storage: &Store, + last_block: &Block, +) -> Result> { + validate_parent_canonical(last_block, storage)?; let parent_header = find_parent_header(&last_block.header, storage)?; let mut state = evm_state(storage.clone(), last_block.header.parent_hash); - validate_block(&last_block, &parent_header, &state)?; + validate_block(last_block, &parent_header, &state)?; - let receipts = execute_block(&last_block, &mut state)?; + let receipts = execute_block(last_block, &mut state)?; validate_gas_used(&receipts, &last_block.header)?; @@ -507,6 +520,7 @@ fn get_last_block_state(storage: &Store) -> Result>(); + let parent_header_bytes = sp1_zkvm::io::read::>(); let memory_db = sp1_zkvm::io::read::(); - // Make Inputs public. - sp1_zkvm::io::commit(&head_block_bytes); - sp1_zkvm::io::commit(&memory_db); - - // TODO: For execution. - let mut cache_db = CacheDB::new(memory_db); + // SetUp data from inputs let block = ::decode( &head_block_bytes, ) .unwrap(); + let parent_header = + ::decode( + &parent_header_bytes, + ) + .unwrap(); + + // Make DataInputs public. + sp1_zkvm::io::commit(&block); + sp1_zkvm::io::commit(&parent_header); + sp1_zkvm::io::commit(&memory_db); + + // SetUp CacheDB in order to use execute_block() + let mut cache_db = CacheDB::new(memory_db); + let block_receipts = execute_block(&block, &mut cache_db).unwrap(); // TODO // Handle the case in which the gas used differs and throws an error. diff --git a/crates/l2/prover/zk_prover.rs b/crates/l2/prover/zk_prover.rs index 491245efe0..98bcebbfb2 100644 --- a/crates/l2/prover/zk_prover.rs +++ b/crates/l2/prover/zk_prover.rs @@ -100,13 +100,13 @@ impl Prover { pub fn prove_execution(&self, input: &ProverInput) -> Result { let head_block_rlp = input.block.clone().encode_to_vec(); - //let memory_db = input.db; + let parent_header_rlp = input.parent_block_header.clone().encode_to_vec(); - // Setup the inputs. + // Write the inputs let mut stdin = SP1Stdin::new(); - stdin.write(&head_block_rlp); - // TODO write db + stdin.write(&parent_header_rlp); + stdin.write(&input.db); info!( "Starting block execution proof for block = {:?}",