From 3e00fa42384dcd22e89862173c6da498f40580f0 Mon Sep 17 00:00:00 2001 From: Prabhat Verma Date: Thu, 19 Dec 2024 15:23:20 +0530 Subject: [PATCH] chore: add recursive proof using rocksDB --- core/src/prover.rs | 9 +++++++++ nexus/host/src/lib.rs | 27 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/core/src/prover.rs b/core/src/prover.rs index d28170b..3a44c4e 100644 --- a/core/src/prover.rs +++ b/core/src/prover.rs @@ -13,12 +13,21 @@ pub fn run() { let touched_states: StateUpdate = Z::read_input::().unwrap(); let header: AvailHeader = Z::read_input::().unwrap(); let mut header_store: HeaderStore = Z::read_input::().unwrap(); + let img_id: [u32; 8] = Z::read_input::<[u32; 8]>().unwrap(); let zkvm_state_machine = ZKVMStateMachine::::new(); let zkvm_result = zkvm_state_machine .execute_batch(&header, &mut header_store, &txs, touched_states) .expect("Should not have panicked."); + if header_store.inner.clone().len() != 0 { + Z::verify(img_id, &header_store.inner.first().unwrap()).unwrap(); + // = 0 only for the case of genesis + if header_store.inner.first().unwrap().avail_header_hash != header.parent_hash { + panic!("match not found"); + } + } + // let after_stf = env::cycle_count(); // eprintln!("after STF {}", after_stf); diff --git a/nexus/host/src/lib.rs b/nexus/host/src/lib.rs index 57f456e..c183f54 100644 --- a/nexus/host/src/lib.rs +++ b/nexus/host/src/lib.rs @@ -10,7 +10,7 @@ use nexus_core::{ types::{ AvailHeader, HeaderStore, NexusBlock, NexusBlockWithPointers, NexusHeader, Proof as NexusProof, Transaction, TransactionResult, TransactionStatus, - TransactionWithStatus, TransactionZKVM, TxParams, H256, + TransactionWithStatus, TransactionZKVM, TxParams, H256, StatementDigest, }, zkvm::{ traits::{ZKVMEnv, ZKVMProof, ZKVMProver}, @@ -117,6 +117,7 @@ async fn execute_batch< header: &AvailHeader, header_store: &mut HeaderStore, prover_mode: ProverMode, + prev_proof: Option

, ) -> Result<(P, NexusHeader, HashMap, Option), Error> where

>::Error: std::fmt::Debug, @@ -154,11 +155,17 @@ where .collect(); let zkvm_txs = zkvm_txs?; + let img_id = StatementDigest(NEXUS_RUNTIME_ID); zkvm_prover.add_input(&zkvm_txs).unwrap(); zkvm_prover.add_input(&state_update).unwrap(); zkvm_prover.add_input(&header).unwrap(); zkvm_prover.add_input(&header_store).unwrap(); + zkvm_prover.add_input(&img_id).unwrap(); + match prev_proof { + Some(i) => {zkvm_prover.add_proof_for_recursion(i).unwrap();} + _ => () + } let mut proof = zkvm_prover.prove()?; let result: NexusHeader = proof.public_inputs()?; @@ -181,6 +188,17 @@ pub async fn execution_engine_handle( ) -> Result<(), anyhow::Error> { const MAX_HEADERS: usize = 5; let mut header_array: Vec

= Vec::new(); + + // Initialize prev_proof from RocksDB + let mut prev_proof: Option = { + let db_lock = node_db.lock().await; + match db_lock.get::(b"prev_proof") { + Ok(Some(proof)) => Some(proof), + Ok(None) => None, + Err(_) => return Err(anyhow!("Failed to retrieve prev_proof from RocksDB")), + } + }; + loop { if *shutdown_rx.borrow() { println!("Shutdown signal received. Stopping execution engine..."); @@ -247,11 +265,14 @@ pub async fn execution_engine_handle( &AvailHeader::from(&header), &mut old_headers, prover_mode.clone(), + prev_proof.clone() ) .await { - Ok((_, result, tx_result, tree_update_batch)) => { - //The execute_batch method on state machine would have updated the version in the storage. + Ok((proof, result, tx_result, tree_update_batch)) => { + prev_proof = Some(proof.clone()); + let db_lock = node_db.lock().await; + db_lock.put(b"prev_proof", &proof)?; let updated_version = state.lock().await.get_version(false)?; save_batch_information(