Skip to content

Commit

Permalink
fix: cleanup 1
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Sep 18, 2024
1 parent ad98381 commit fd1b064
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 45 deletions.
3 changes: 1 addition & 2 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ pub(crate) fn set_registers_and_run<F: RichField>(
)
});

let result = interpreter.run()?;
Ok(result)
interpreter.run()
}

impl<F: RichField> Interpreter<F> {
Expand Down
23 changes: 11 additions & 12 deletions evm_arithmetization/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,20 @@ impl<F: RichField> GenerationInputs<F> {
}

/// Post transaction execution tries retrieved from the prover's memory.
/// Used primarily as in the error returned in case of the failed execution,
/// for the purpose of error debugging.
/// Used primarily for error debugging in case of a failed execution.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DebugTrieOutputs {
pub struct DebugOutputTries {
pub state_trie: HashedPartialTrie,
pub transaction_trie: HashedPartialTrie,
pub receipt_trie: HashedPartialTrie,
}

/// Wrapper around the error that happened in the simulation
/// with the additional collected post simulation tries
/// with the additional collected post-simulation tries.
#[derive(Debug)]
pub struct SimulationError {
pub internal_error: anyhow::Error,
pub tries: Option<DebugTrieOutputs>,
pub tries: Option<DebugOutputTries>,
}

impl Display for SimulationError {
Expand Down Expand Up @@ -631,7 +630,7 @@ fn simulate_cpu<F: RichField>(
/// they are represented in the prover's memory.
pub(crate) fn collect_debug_tries<F: RichField>(
state: &GenerationState<F>,
) -> anyhow::Result<DebugTrieOutputs> {
) -> anyhow::Result<DebugOutputTries> {
let state_trie_ptr = u256_to_usize(
state
.memory
Expand All @@ -652,8 +651,8 @@ pub(crate) fn collect_debug_tries<F: RichField>(
.read_global_metadata(GlobalMetadata::TransactionTrieRoot),
)
.map_err(|_| anyhow!("transactions trie pointer is too large to fit in a usize."))?;
let transactions_trie = get_txn_trie::<HashedPartialTrie>(&state.memory, txn_trie_ptr)
.map_err(|e| {
let transaction_trie =
get_txn_trie::<HashedPartialTrie>(&state.memory, txn_trie_ptr).map_err(|e| {
anyhow!(
"unable to retrieve transaction trie for debugging purposes: {:?}",
e
Expand All @@ -666,17 +665,17 @@ pub(crate) fn collect_debug_tries<F: RichField>(
.read_global_metadata(GlobalMetadata::ReceiptTrieRoot),
)
.map_err(|_| anyhow!("receipts trie pointer is too large to fit in a usize."))?;
let receipts_trie = get_receipt_trie::<HashedPartialTrie>(&state.memory, receipt_trie_ptr)
let receipt_trie = get_receipt_trie::<HashedPartialTrie>(&state.memory, receipt_trie_ptr)
.map_err(|e| {
anyhow!(
"unable to retrieve receipt trie for debugging purposes: {:?}",
e
)
})?;

Ok(DebugTrieOutputs {
Ok(DebugOutputTries {
state_trie,
transaction_trie: transactions_trie,
receipt_trie: receipts_trie,
transaction_trie,
receipt_trie,
})
}
26 changes: 13 additions & 13 deletions evm_arithmetization/src/generation/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::interpreter::{set_registers_and_run, ExtraSegmentData, Interpreter};
use crate::generation::state::State;
use crate::generation::{collect_debug_tries, debug_inputs, DebugTrieOutputs, GenerationInputs};
use crate::generation::{collect_debug_tries, debug_inputs, DebugOutputTries, GenerationInputs};
use crate::witness::memory::MemoryState;
use crate::witness::state::RegistersState;
use crate::AllData;
Expand Down Expand Up @@ -93,7 +93,7 @@ pub struct SegmentError {
pub message: String,
pub block: u64,
pub segment_index: usize,
pub tries: Option<DebugTrieOutputs>,
pub tries: Option<DebugOutputTries>,
}

impl<F: RichField> SegmentDataIterator<F> {
Expand Down Expand Up @@ -138,8 +138,9 @@ impl<F: RichField> SegmentDataIterator<F> {

// Run the interpreter to get `registers_after` and the partial data for the
// next segment.
let run = set_registers_and_run(segment_data.registers_after, &mut self.interpreter);
if let Ok((updated_registers, mem_after)) = run {
let execution_result =
set_registers_and_run(segment_data.registers_after, &mut self.interpreter);
if let Ok((updated_registers, mem_after)) = execution_result {
let partial_segment_data = Some(build_segment_data(
segment_index + 1,
Some(updated_registers),
Expand All @@ -162,16 +163,15 @@ impl<F: RichField> SegmentDataIterator<F> {
inputs.txn_number_before + inputs.txn_hashes.len()
),
};
let s = format!(
"Segment generation {:?} for block {:?} ({}) failed with error {:?}",
segment_index,
block,
txn_range,
run.unwrap_err()
);
// In case of the error, return tries as part of the error for easier debugging
// In case of the error, return tries as part of the error for easier debugging.
Err(SegmentError {
message: s,
message: format!(
"segment generation {:?} for block {:?} ({}) failed with error {:?}",
segment_index,
block,
txn_range,
execution_result.unwrap_err()
),
block: block.as_u64(),
segment_index,
tries: collect_debug_tries(self.interpreter.get_generation_state()).ok(),
Expand Down
11 changes: 2 additions & 9 deletions trace_decoder/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{

/// TODO(0xaatif): document this after https://github.com/0xPolygonZero/zk_evm/issues/275
/// TODO: type parametrize observer depending on entrypoint. At the moment
/// `StateMpt` is hardcoded
/// `StateMpt` is hardcoded in this function.
pub fn entrypoint(
trace: BlockTrace,
other: OtherBlockData,
Expand Down Expand Up @@ -274,6 +274,7 @@ pub struct IntraBlockTries<StateTrieT> {
pub receipt: ReceiptTrie,
}

// TODO: we may want to refactor this many arguments.
#[allow(clippy::too_many_arguments)]
/// Does the main work mentioned in the [module documentation](super).
fn middle<StateTrieT: StateTrie + Clone>(
Expand Down Expand Up @@ -426,14 +427,6 @@ fn middle<StateTrieT: StateTrie + Clone>(

if do_writes {
acct.balance = balance.unwrap_or(acct.balance);
//*******************************************************************
// >>>>>>>>>>> DEBUG: Introduce error in the trie diff simulation
let addressdebug = "0x".to_string() + &hex::encode(addr.0);
if addressdebug.eq("0x71f755898886f79efa73334450f05a824faeae02") {
println!(">>>>>>>> I have found address and making a change txn index {txn_ix} address {addressdebug} making a balance from {} to {}!", acct.balance, acct.balance+U256::from(1));
acct.balance += U256::from(1);
}
//*******************************************************************
acct.nonce = nonce.unwrap_or(acct.nonce);
acct.code_hash = code_usage
.map(|it| match it {
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/src/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::IntraBlockTries;
/// Observer API for the trace decoder.
pub trait Observer<StateTrieT> {
/// Collect tries after the transaction/batch execution.
fn collect_tries(&mut self, block: u64, batch: u64, trie: IntraBlockTries<StateTrieT>);
fn collect_tries(&mut self, block: u64, batch: u64, tries: IntraBlockTries<StateTrieT>);
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions zero/src/bin/trie_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use anyhow::Result;
use clap::{Parser, ValueHint};
use evm_arithmetization::generation::DebugTrieOutputs;
use evm_arithmetization::generation::DebugOutputTries;
use futures::{future, TryStreamExt};
use paladin::directive::{Directive, IndexedStream};
use paladin::runtime::Runtime;
Expand Down Expand Up @@ -108,7 +108,7 @@ async fn main() -> Result<()> {
zero::trie_diff::compare_tries(
&block_prover_input,
batch_index,
&DebugTrieOutputs {
&DebugOutputTries {
state_trie: observer.data[prover_tries.batch_index]
.tries
.state
Expand Down
6 changes: 3 additions & 3 deletions zero/src/debug_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, Write};
use std::path::{Path, PathBuf};

use anyhow::Context;
use evm_arithmetization::generation::DebugTrieOutputs;
use evm_arithmetization::generation::DebugOutputTries;
use serde::{Deserialize, Serialize};

const DEBUG_FOLDER: &str = "./debug";
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct ErrorTriesFile {
pub error: String,
pub block_number: u64,
pub batch_index: usize,
pub tries: DebugTrieOutputs,
pub tries: DebugOutputTries,
}

pub fn generate_tries_debug_file_name(block_number: u64, batch_index: usize) -> String {
Expand All @@ -100,7 +100,7 @@ pub fn save_tries_to_disk(
err: &str,
block_number: u64,
batch_index: usize,
tries: &DebugTrieOutputs,
tries: &DebugOutputTries,
) -> anyhow::Result<()> {
let output_dir = PathBuf::from(DEBUG_FOLDER);

Expand Down
6 changes: 3 additions & 3 deletions zero/src/trie_diff/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use evm_arithmetization::generation::mpt::{AccountRlp, LegacyReceiptRlp};
use evm_arithmetization::generation::DebugTrieOutputs;
use evm_arithmetization::generation::DebugOutputTries;
use mpt_trie::debug_tools::diff::create_diff_between_tries;
use mpt_trie::utils::TrieNodeType;
use tracing::info;
Expand All @@ -9,8 +9,8 @@ use crate::prover::BlockProverInput;
pub fn compare_tries(
block_prover_input: &BlockProverInput,
batch_index: usize,
left: &DebugTrieOutputs,
right: &DebugTrieOutputs,
left: &DebugOutputTries,
right: &DebugOutputTries,
) -> anyhow::Result<()> {
let block_number = block_prover_input
.other_data
Expand Down

0 comments on commit fd1b064

Please sign in to comment.