Skip to content

Commit

Permalink
Clippy and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LindaGuiga committed Oct 3, 2024
1 parent 5f9d771 commit 5c90909
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/constants/txn_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl NormalizedTxnField {
pub(crate) const COUNT: usize = 18;

/// Unscales this virtual offset by their respective `Segment` value.
// #[cfg(test)]
#[cfg(test)]
pub(crate) const fn unscale(&self) -> usize {
*self as usize - Segment::TxnFields as usize
}
Expand Down
11 changes: 6 additions & 5 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ pub(crate) struct Interpreter<F: RichField> {
pub(crate) struct_log_debugger_info: StructLogDebuggerInfo,
}

/// Structure holding necessary information to check the kernel execution
/// against struct logs.
pub(crate) struct StructLogDebuggerInfo {
/// Opcode counter within a transaction.
pub(crate) counter: usize,
/// Gas value in the kernel for a transaction (starting at `GasLimit` and
/// decreasing with each user opcode).
pub(crate) gas: usize,
}

Expand Down Expand Up @@ -611,10 +616,7 @@ impl<F: RichField> State<F> for Interpreter<F> {
// Check stack.
if let Some(txn_stack) = cur_txn_struct_logs.stack {
let cur_stack = self.get_full_stack();
let txn_stack = txn_stack
.into_iter()
.map(|s| U256::from(s))
.collect::<Vec<_>>();
let txn_stack = txn_stack.into_iter().map(U256::from).collect::<Vec<_>>();

Check failure on line 619 in evm_arithmetization/src/cpu/kernel/interpreter.rs

View workflow job for this annotation

GitHub Actions / Test evm_arithmetization

the trait bound `ethereum_types::U256: From<Uint<256, 4>>` is not satisfied

Check failure on line 619 in evm_arithmetization/src/cpu/kernel/interpreter.rs

View workflow job for this annotation

GitHub Actions / Test evm_arithmetization

the trait bound `ethereum_types::U256: From<Uint<256, 4>>` is not satisfied

Check failure on line 619 in evm_arithmetization/src/cpu/kernel/interpreter.rs

View workflow job for this annotation

GitHub Actions / Test evm_arithmetization

the trait bound `ethereum_types::U256: From<Uint<256, 4>>` is not satisfied

if txn_stack != cur_stack {
log::warn!(
Expand Down Expand Up @@ -763,7 +765,6 @@ impl<F: RichField> State<F> for Interpreter<F> {
stack
}

/// Returns the entire stack. Only used for checking against struct logs.
fn get_full_stack(&self) -> Vec<U256> {
let mut stack: Vec<U256> = (0..self.get_registers().stack_len)
.map(|i| crate::witness::util::stack_peek(self.get_generation_state(), i).unwrap())
Expand Down
33 changes: 16 additions & 17 deletions evm_arithmetization/src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub(crate) trait State<F: RichField> {
/// Returns a `State`'s stack.
fn get_stack(&self) -> Vec<U256>;

/// Returns the entire stack. Only used for checking against struct logs.
/// Returns the entire stack. Only used in the interpreter, for checking
/// against struct logs.
fn get_full_stack(&self) -> Vec<U256> {
vec![]
}
Expand Down Expand Up @@ -215,6 +216,7 @@ pub(crate) trait State<F: RichField> {
Self: Transition<F>,
{
let halt_offsets = self.get_halt_offsets();

let cycle_limit =
max_cpu_len_log.map(|max_len_log| (1 << max_len_log) - NUM_EXTRA_CYCLES_AFTER);

Expand Down Expand Up @@ -327,22 +329,19 @@ pub(crate) trait State<F: RichField> {
// The current `is_kernel` is the updated value (post operation execution). A
// `StructLogDebuggerError` might have arisen on a user
// operation before or after it was executed.
match e {
ProgramError::StructLogDebuggerError => {
let offset_name = KERNEL.offset_name(self.get_registers().program_counter);
bail!(
"{:?} at struct log debugger at pc={}, stack={:?}, memory={:?}",
e,
offset_name,
self.get_stack(),
self.mem_get_kernel_content()
.iter()
.map(|c| c.unwrap_or_default())
.collect_vec(),
);
}
_ => (),
};
if let ProgramError::StructLogDebuggerError = e {
let offset_name = KERNEL.offset_name(self.get_registers().program_counter);
bail!(
"{:?} at struct log debugger at pc={}, stack={:?}, memory={:?}",
e,
offset_name,
self.get_stack(),
self.mem_get_kernel_content()
.iter()
.map(|c| c.unwrap_or_default())
.collect_vec(),
);
}

self.rollback(checkpoint);
self.handle_error(e)
Expand Down
14 changes: 7 additions & 7 deletions evm_arithmetization/src/structlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn structlog_tracing_options(stack: bool, memory: bool, storage: bool) -> GethDe
}
}

// Gets the lightest possible structlog for transcation `tx_hash`.
// Gets the struct logs with the necessary logs for debugging.
pub async fn get_structlog_for_debug<ProviderT, TransportT>(
provider: &ProviderT,
tx_hash: &B256,
Expand All @@ -31,11 +31,11 @@ where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
{
let light_structlog_trace = provider
let structlog_trace = provider
.debug_trace_transaction(*tx_hash, structlog_tracing_options(true, false, false))
.await?;

let structlogs: Option<Vec<ZeroStructLog>> = normalize_structlog(light_structlog_trace);
let structlogs: Option<Vec<ZeroStructLog>> = normalize_structlog(structlog_trace);

Ok(structlogs)
}
Expand Down Expand Up @@ -129,8 +129,8 @@ pub mod zerostructlog {
a = a.replace("\"error\":{},", "");

let b = serde_json::from_str::<DefaultFrame>(&a)?;
let d: DefaultFrame = b.try_into()?;
Ok(d)

Ok(b)
}

pub(crate) fn normalize_structlog(
Expand All @@ -141,15 +141,15 @@ pub mod zerostructlog {
let all_struct_logs = structlog_frame
.struct_logs
.into_iter()
.map(|log| ZeroStructLog::from(log))
.map(ZeroStructLog::from)
.collect::<Vec<ZeroStructLog>>();
Some(all_struct_logs)
}
GethTrace::JS(structlog_js_object) => {
try_reserialize(&structlog_js_object).ok().map(|s| {
s.struct_logs
.into_iter()
.map(|log| ZeroStructLog::from(log))
.map(ZeroStructLog::from)
.collect::<Vec<ZeroStructLog>>()
})
}
Expand Down
20 changes: 11 additions & 9 deletions evm_arithmetization/src/witness/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::cpu::columns::CpuColumnsView;
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::assembler::BYTES_PER_OFFSET;
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::constants::txn_fields::NormalizedTxnField;
use crate::cpu::simple_logic::eq_iszero::generate_pinv_diff;
use crate::cpu::stack::MAX_USER_STACK_SIZE;
use crate::extension_tower::BN_BASE;
use crate::generation::state::State;
use crate::memory::segments::Segment;
use crate::util::u256_to_usize;
use crate::witness::errors::MemoryError::VirtTooLarge;
Expand Down Expand Up @@ -851,14 +851,16 @@ pub(crate) fn generate_exit_kernel<F: RichField, T: Transition<F>>(
// If we are in debug mode and the next operation is in user mode, update the
// `gas` field so we can check against struct logs.
if !is_kernel_mode {
let get_field = |field: NormalizedTxnField| {
state.get_generation_state().memory.contexts[0].segments[Segment::TxnFields.unscale()]
.get(field.unscale())
.as_usize()
};
let intrinsic_gas = get_field(NormalizedTxnField::IntrinsicGas);
let max_gas = get_field(NormalizedTxnField::GasLimit);
let txn_gas = max_gas - intrinsic_gas - gas_used_val as usize;
let gas_limit_address = MemoryAddress::new(
state.get_registers().context,
Segment::ContextMetadata,
ContextMetadata::GasLimit.unscale(), // context offsets are already scaled
);
let init_gas_limit = state
.get_mut_generation_state()
.get_from_memory(gas_limit_address)
.as_usize();
let txn_gas = init_gas_limit - gas_used_val as usize;

state.update_struct_logs_gas(txn_gas);
}
Expand Down
27 changes: 13 additions & 14 deletions trace_decoder/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ use crate::{
TxnInfo, TxnMeta, TxnTrace,
};

type GenerationAndStructLogs = (
Vec<GenerationInputs>,
Option<Vec<Vec<Option<Vec<ZeroStructLog>>>>>,
);

/// TODO(0xaatif): document this after <https://github.com/0xPolygonZero/zk_evm/issues/275>
pub fn entrypoint(
trace: BlockTrace,
other: OtherBlockData,
all_struct_logs: Option<Vec<Option<Vec<ZeroStructLog>>>>,
batch_size_hint: usize,
observer: &mut impl Observer<StateMpt>,
) -> anyhow::Result<(
Vec<GenerationInputs>,
Option<Vec<Vec<Option<Vec<ZeroStructLog>>>>>,
)> {
) -> anyhow::Result<GenerationAndStructLogs> {
ensure!(batch_size_hint != 0);

let BlockTrace {
Expand Down Expand Up @@ -76,16 +78,13 @@ pub fn entrypoint(
observer,
)?;

let batched_struct_logs = if let Some(struct_logs) = all_struct_logs {
Some(
struct_logs
.chunks(batch_size_hint)
.map(|c| c.to_vec())
.collect_vec(),
)
} else {
None
};
let batched_struct_logs = all_struct_logs.map(|struct_logs| {
struct_logs
.chunks(batch_size_hint)
.map(|c| c.to_vec())
.collect_vec()
});

let mut running_gas_used = 0;
Ok((
batches
Expand Down

0 comments on commit 5c90909

Please sign in to comment.