Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit d5b62fd

Browse files
committed
add feature switch
1 parent b02de5d commit d5b62fd

File tree

13 files changed

+155
-39
lines changed

13 files changed

+155
-39
lines changed

bus-mapping/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ mock = { path = "../mock" }
4141
rand.workspace = true
4242

4343
[features]
44-
default = ["test"]
44+
default = ["test", "enable-stack", "enable-storage"]
4545
test = ["mock", "rand"]
4646
scroll = ["eth-types/scroll", "mock?/scroll"]
4747
# Enable shanghai feature of mock only if mock is enabled (by test).
4848
shanghai = ["eth-types/shanghai", "mock?/shanghai"]
49+
tracer-tests = ["enable-memory"]
50+
enable-stack = ["eth-types/enable-stack"]
51+
enable-memory = ["eth-types/enable-memory"]
52+
enable-storage = ["eth-types/enable-storage"]

bus-mapping/src/circuit_input_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod execution;
88
mod input_state_ref;
99
#[cfg(feature = "scroll")]
1010
mod l2;
11-
#[cfg(test)]
11+
#[cfg(all(feature = "tracer-tests", test))]
1212
mod tracer_tests;
1313
mod transaction;
1414

bus-mapping/src/evm/opcodes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::{
1212
use core::fmt::Debug;
1313
use eth_types::{evm_unimplemented, GethExecStep, ToAddress, ToWord, Word};
1414

15-
use crate::util::CHECK_MEM_STRICT;
15+
#[cfg(feature = "enable-memory")]
16+
use crate::util::GETH_TRACE_CHECK_LEVEL;
1617

1718
#[cfg(any(feature = "test", test))]
1819
pub use self::sha3::sha3_tests::{gen_sha3_code, MemoryKind};
@@ -383,8 +384,8 @@ pub fn gen_associated_ops(
383384
state: &mut CircuitInputStateRef,
384385
geth_steps: &[GethExecStep],
385386
) -> Result<Vec<ExecStep>, Error> {
386-
let check_level = if *CHECK_MEM_STRICT { 2 } else { 0 }; // 0: no check, 1: check and log error and fix, 2: check and assert_eq
387-
if check_level >= 1 {
387+
#[cfg(feature = "enable-memory")]
388+
if GETH_TRACE_CHECK_LEVEL.should_check() {
388389
let memory_enabled = !geth_steps.iter().all(|s| s.memory.is_empty());
389390
assert!(memory_enabled);
390391
if memory_enabled {
@@ -414,7 +415,7 @@ pub fn gen_associated_ops(
414415
);
415416
}
416417
}
417-
if check_level >= 2 {
418+
if GETH_TRACE_CHECK_LEVEL.should_panic() {
418419
panic!("mem wrong");
419420
}
420421
state.call_ctx_mut()?.memory = geth_steps[0].memory.clone();

bus-mapping/src/rpc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ethers_providers::JsonRpcClient;
1111
use serde::Serialize;
1212
use std::collections::HashMap;
1313

14-
use crate::util::CHECK_MEM_STRICT;
14+
use crate::util::GETH_TRACE_CHECK_LEVEL;
1515

1616
/// Serialize a type.
1717
///
@@ -42,9 +42,9 @@ pub(crate) struct GethLoggerConfig {
4242
impl Default for GethLoggerConfig {
4343
fn default() -> Self {
4444
Self {
45-
enable_memory: false,
46-
disable_stack: false,
47-
disable_storage: false,
45+
enable_memory: cfg!(feature = "enable-memory"),
46+
disable_stack: !cfg!(feature = "enable-stack"),
47+
disable_storage: !cfg!(feature = "enable-storage"),
4848
enable_return_data: true,
4949
}
5050
}
@@ -149,7 +149,7 @@ impl<P: JsonRpcClient> GethClient<P> {
149149
pub async fn trace_tx_by_hash(&self, hash: H256) -> Result<Vec<GethExecTrace>, Error> {
150150
let hash = serialize(&hash);
151151
let cfg = GethLoggerConfig {
152-
enable_memory: *CHECK_MEM_STRICT,
152+
enable_memory: cfg!(feature = "enable-memory") && GETH_TRACE_CHECK_LEVEL.should_check(),
153153
..Default::default()
154154
};
155155
let cfg = serialize(&cfg);

bus-mapping/src/util.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use eth_types::{Hash, U256};
33
pub use eth_types::{KECCAK_CODE_HASH_EMPTY, POSEIDON_CODE_HASH_EMPTY};
44
use halo2_proofs::halo2curves::{bn256::Fr, group::ff::PrimeField};
55
use once_cell::sync::Lazy;
6+
use std::convert::Infallible;
67

78
use std::str::FromStr;
89

@@ -12,8 +13,44 @@ pub fn read_env_var<T: Clone + FromStr>(var_name: &'static str, default: T) -> T
1213
.map(|s| s.parse::<T>().unwrap_or_else(|_| default.clone()))
1314
.unwrap_or(default)
1415
}
15-
/// ..
16-
pub static CHECK_MEM_STRICT: Lazy<bool> = Lazy::new(|| read_env_var("CHECK_MEM_STRICT", false));
16+
/// env var for Geth trace sanity check level
17+
pub static GETH_TRACE_CHECK_LEVEL: Lazy<GethTraceSanityCheckLevel> =
18+
Lazy::new(|| read_env_var("GETH_TRACE_CHECK_LEVEL", GethTraceSanityCheckLevel::None));
19+
20+
/// Geth trace sanity check level
21+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22+
pub enum GethTraceSanityCheckLevel {
23+
/// No sanity check
24+
None,
25+
/// Check sanity and log error
26+
Check,
27+
/// Panic on error
28+
Strict,
29+
}
30+
31+
impl GethTraceSanityCheckLevel {
32+
/// Whether to do sanity check
33+
pub fn should_check(&self) -> bool {
34+
*self != GethTraceSanityCheckLevel::None
35+
}
36+
37+
/// Whether to panic on error
38+
pub fn should_panic(&self) -> bool {
39+
*self == GethTraceSanityCheckLevel::Strict
40+
}
41+
}
42+
43+
impl FromStr for GethTraceSanityCheckLevel {
44+
type Err = Infallible;
45+
46+
fn from_str(s: &str) -> Result<Self, Self::Err> {
47+
match s {
48+
"strict" => Ok(GethTraceSanityCheckLevel::Strict),
49+
_ if !s.is_empty() => Ok(GethTraceSanityCheckLevel::Check),
50+
_ => Ok(GethTraceSanityCheckLevel::None),
51+
}
52+
}
53+
}
1754

1855
/// Default number of bytes to pack into a field element.
1956
pub const POSEIDON_HASH_BYTES_IN_FIELD: usize = 31;

eth-types/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ default = ["warn-unimplemented"]
3030
warn-unimplemented = []
3131
shanghai = []
3232
scroll = []
33+
34+
# trace heap allocation related feature switches
35+
enable-stack = []
36+
enable-memory = []
37+
enable-storage = []

eth-types/src/l2_types.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
//! L2 types used to deserialize traces for l2geth.
22
33
use crate::{
4-
evm_types::{Gas, GasCost, Memory, OpcodeId, ProgramCounter, Stack, Storage},
4+
evm_types::{Gas, GasCost, OpcodeId, ProgramCounter},
55
Block, GethExecStep, GethExecTrace, Hash, Transaction, Word, H256,
66
};
77
use ethers_core::types::{Address, Bytes, U256, U64};
88
use serde::{Deserialize, Serialize};
99
use std::collections::HashMap;
1010

11+
#[cfg(feature = "enable-memory")]
12+
use crate::evm_types::Memory;
13+
#[cfg(feature = "enable-stack")]
14+
use crate::evm_types::Stack;
15+
#[cfg(feature = "enable-storage")]
16+
use crate::evm_types::Storage;
17+
1118
/// l2 block full trace
1219
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
1320
pub struct BlockTrace {
@@ -226,19 +233,18 @@ pub struct ExecStep {
226233
pub refund: u64,
227234
pub depth: isize,
228235
pub error: Option<String>,
236+
#[cfg(feature = "enable-stack")]
229237
pub stack: Option<Vec<Word>>,
238+
#[cfg(feature = "enable-memory")]
230239
pub memory: Option<Vec<Word>>,
240+
#[cfg(feature = "enable-storage")]
231241
pub storage: Option<HashMap<Word, Word>>,
232242
#[serde(rename = "extraData")]
233243
pub extra_data: Option<ExtraData>,
234244
}
235245

236246
impl From<ExecStep> for GethExecStep {
237247
fn from(e: ExecStep) -> Self {
238-
let stack = e.stack.map_or_else(Stack::new, Stack::from);
239-
let storage = e.storage.map_or_else(Storage::empty, Storage::from);
240-
let memory = e.memory.map_or_else(Memory::default, Memory::from);
241-
242248
GethExecStep {
243249
pc: ProgramCounter(e.pc as usize),
244250
// FIXME
@@ -248,9 +254,12 @@ impl From<ExecStep> for GethExecStep {
248254
refund: Gas(e.refund),
249255
depth: e.depth as u16,
250256
error: e.error,
251-
stack,
252-
memory,
253-
storage,
257+
#[cfg(feature = "enable-stack")]
258+
stack: e.stack.map_or_else(Stack::new, Stack::from),
259+
#[cfg(feature = "enable-memory")]
260+
memory: e.memory.map_or_else(Memory::default, Memory::from),
261+
#[cfg(feature = "enable-storage")]
262+
storage: e.storage.map_or_else(Storage::empty, Storage::from),
254263
}
255264
}
256265
}

eth-types/src/lib.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ use halo2_proofs::{
3030
halo2curves::{bn256::Fr, group::ff::PrimeField},
3131
};
3232

33-
use crate::evm_types::{
34-
memory::Memory, stack::Stack, storage::Storage, Gas, GasCost, OpcodeId, ProgramCounter,
35-
};
33+
use crate::evm_types::{Gas, GasCost, OpcodeId, ProgramCounter};
3634
use ethers_core::types;
3735
pub use ethers_core::{
3836
abi::ethereum_types::{BigEndianHash, U512},
@@ -41,11 +39,17 @@ pub use ethers_core::{
4139
Address, Block, Bytes, Signature, H160, H256, H64, U256, U64,
4240
},
4341
};
44-
4542
use once_cell::sync::Lazy;
4643
use serde::{de, Deserialize, Serialize};
4744
use std::{collections::HashMap, fmt, str::FromStr};
4845

46+
#[cfg(feature = "enable-memory")]
47+
use crate::evm_types::Memory;
48+
#[cfg(feature = "enable-stack")]
49+
use crate::evm_types::Stack;
50+
#[cfg(feature = "enable-storage")]
51+
use crate::evm_types::Storage;
52+
4953
/// Trait used to reduce verbosity with the declaration of the [`FieldExt`]
5054
/// trait and its repr.
5155
pub trait Field:
@@ -352,12 +356,15 @@ struct GethExecStepInternal {
352356
depth: u16,
353357
error: Option<String>,
354358
// stack is in hex 0x prefixed
359+
#[cfg(feature = "enable-stack")]
355360
#[serde(default)]
356361
stack: Vec<DebugU256>,
357362
// memory is in chunks of 32 bytes, in hex
363+
#[cfg(feature = "enable-memory")]
358364
#[serde(default)]
359365
memory: Vec<DebugU256>,
360366
// storage is hex -> hex
367+
#[cfg(feature = "enable-storage")]
361368
#[serde(default)]
362369
storage: HashMap<DebugU256, DebugU256>,
363370
}
@@ -375,10 +382,13 @@ pub struct GethExecStep {
375382
pub depth: u16,
376383
pub error: Option<String>,
377384
// stack is in hex 0x prefixed
385+
#[cfg(feature = "enable-stack")]
378386
pub stack: Stack,
379387
// memory is in chunks of 32 bytes, in hex
388+
#[cfg(feature = "enable-memory")]
380389
pub memory: Memory,
381390
// storage is hex -> hex
391+
#[cfg(feature = "enable-storage")]
382392
pub storage: Storage,
383393
}
384394

@@ -403,18 +413,21 @@ impl<'a> fmt::Debug for DebugWord<'a> {
403413

404414
impl fmt::Debug for GethExecStep {
405415
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
406-
f.debug_struct("Step")
407-
.field("pc", &format_args!("0x{:04x}", self.pc.0))
416+
let mut f = f.debug_struct("Step");
417+
f.field("pc", &format_args!("0x{:04x}", self.pc.0))
408418
.field("op", &self.op)
409419
.field("gas", &format_args!("{}", self.gas.0))
410420
.field("gas_cost", &format_args!("{}", self.gas_cost.0))
411421
.field("refund", &format_args!("{}", self.refund.0))
412422
.field("depth", &self.depth)
413-
.field("error", &self.error)
414-
.field("stack", &self.stack)
415-
// .field("memory", &self.memory)
416-
.field("storage", &self.storage)
417-
.finish()
423+
.field("error", &self.error);
424+
#[cfg(feature = "enable-stack")]
425+
f.field("stack", &self.stack);
426+
#[cfg(feature = "enable-memory")]
427+
f.field("memory", &self.memory);
428+
#[cfg(feature = "enable-storage")]
429+
f.field("storage", &self.storage);
430+
f.finish()
418431
}
419432
}
420433

@@ -432,13 +445,16 @@ impl<'de> Deserialize<'de> for GethExecStep {
432445
gas_cost: s.gas_cost,
433446
depth: s.depth,
434447
error: s.error,
448+
#[cfg(feature = "enable-stack")]
435449
stack: Stack(s.stack.iter().map(|dw| dw.to_word()).collect::<Vec<Word>>()),
450+
#[cfg(feature = "enable-memory")]
436451
memory: Memory::from(
437452
s.memory
438453
.iter()
439454
.map(|dw| dw.to_word())
440455
.collect::<Vec<Word>>(),
441456
),
457+
#[cfg(feature = "enable-storage")]
442458
storage: Storage(
443459
s.storage
444460
.iter()
@@ -552,7 +568,10 @@ macro_rules! word_map {
552568
#[cfg(test)]
553569
mod tests {
554570
use super::*;
555-
use crate::evm_types::{memory::Memory, opcode_ids::OpcodeId, stack::Stack};
571+
use crate::evm_types::{opcode_ids::OpcodeId, stack::Stack};
572+
573+
#[cfg(feature = "enable-memory")]
574+
use crate::evm_types::memory::Memory;
556575

557576
#[test]
558577
fn test_to_u16_array() {
@@ -648,8 +667,11 @@ mod tests {
648667
gas_cost: GasCost(3),
649668
depth: 1,
650669
error: None,
670+
#[cfg(feature = "enable-stack")]
651671
stack: Stack::new(),
672+
#[cfg(feature = "enable-storage")]
652673
storage: Storage(word_map!()),
674+
#[cfg(feature = "enable-memory")]
653675
memory: Memory::new(),
654676
},
655677
GethExecStep {
@@ -660,8 +682,11 @@ mod tests {
660682
gas_cost: GasCost(2100),
661683
depth: 1,
662684
error: None,
685+
#[cfg(feature = "enable-stack")]
663686
stack: Stack(vec![word!("0x1003e2d2"), word!("0x2a"), word!("0x0")]),
687+
#[cfg(feature = "enable-storage")]
664688
storage: Storage(word_map!("0x0" => "0x6f")),
689+
#[cfg(feature = "enable-memory")]
665690
memory: Memory::from(vec![word!("0x0"), word!("0x0"), word!("0x080")]),
666691
},
667692
GethExecStep {
@@ -672,12 +697,15 @@ mod tests {
672697
gas_cost: GasCost(42),
673698
depth: 1,
674699
error: None,
700+
#[cfg(feature = "enable-stack")]
675701
stack: Stack(vec![
676702
word!("0x3635c9adc5dea00000"),
677703
word!("0x40"),
678704
word!("0x0")
679705
]),
706+
#[cfg(feature = "enable-storage")]
680707
storage: Storage(word_map!()),
708+
#[cfg(feature = "enable-memory")]
681709
memory: Memory::from(vec![
682710
word!(
683711
"000000000000000000000000b8f67472dcc25589672a61905f7fd63f09e5d470"

external-tracer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl Default for LoggerConfig {
5858
}
5959

6060
impl LoggerConfig {
61+
#[cfg(feature = "enable-memory")]
6162
pub fn enable_memory() -> Self {
6263
Self {
6364
enable_memory: true,

testool/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ scroll = ["bus-mapping/scroll", "eth-types/scroll", "external-tracer/scroll", "m
5050
parallel_syn = ["halo2_proofs/parallel_syn", "zkevm-circuits/parallel_syn", "prover?/parallel_syn"]
5151
inner-prove = ["prover/test", "parallel_syn", "scroll", "shanghai"]
5252
chunk-prove = ["prover/test", "parallel_syn", "scroll", "shanghai"]
53+
54+
enable-stack = ["zkevm-circuits/enable-stack"]
55+
enable-memory = ["zkevm-circuits/enable-memory"]
56+
enable-storage = ["zkevm-circuits/enable-storage"]

0 commit comments

Comments
 (0)