Skip to content

Commit

Permalink
add prover foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Oct 10, 2024
1 parent 2900685 commit 0fc4240
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
52 changes: 33 additions & 19 deletions crates/l2/operator/proof_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::{
collections::HashMap,
fmt::format,
io::{BufReader, BufWriter, Read},
net::{IpAddr, TcpListener, TcpStream},
os::macos::raw::stat,
Expand All @@ -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::{
Expand All @@ -32,7 +35,6 @@ use crate::{
};

use revm::{
db::CacheDB,
primitives::{bitvec::view::AsBits, AccountInfo, FixedBytes, B256},
Evm, InMemoryDB,
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<MemoryDB, Box<dyn std::error::Error>> {
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<MemoryDB, Box<dyn std::error::Error>> {
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)?;

Expand Down Expand Up @@ -507,6 +520,7 @@ fn get_last_block_state(storage: &Store) -> Result<MemoryDB, Box<dyn std::error:

let mut block_headers = Vec::new();

let last_block_number = last_block.header.number;
for block_number in (oldest_block_number..=(last_block_number - 1)).rev() {
let block_header = state.database().get_block_header(block_number)?.unwrap();
block_headers.push(block_header);
Expand Down
Binary file not shown.
22 changes: 16 additions & 6 deletions crates/l2/prover/sp1/execution_program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ sp1_zkvm::entrypoint!(main);

pub fn main() {
let head_block_bytes = sp1_zkvm::io::read::<Vec<u8>>();
let parent_header_bytes = sp1_zkvm::io::read::<Vec<u8>>();
let memory_db = sp1_zkvm::io::read::<MemoryDB>();

// 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 = <ethereum_rust_core::types::Block as ethereum_rust_rlp::decode::RLPDecode>::decode(
&head_block_bytes,
)
.unwrap();

let parent_header =
<ethereum_rust_core::types::BlockHeader as ethereum_rust_rlp::decode::RLPDecode>::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.
Expand Down
8 changes: 4 additions & 4 deletions crates/l2/prover/zk_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ impl Prover {

pub fn prove_execution(&self, input: &ProverInput) -> Result<SP1ProofWithPublicValues, String> {
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 = {:?}",
Expand Down

0 comments on commit 0fc4240

Please sign in to comment.