Skip to content

Commit 59be93c

Browse files
committed
add strict check
1 parent 3231fa5 commit 59be93c

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

bus-mapping/src/evm/opcodes.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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+
use crate::util::CHECK_MEM_STACK_STRICT;
1616

1717
#[cfg(any(feature = "test", test))]
1818
pub use self::sha3::sha3_tests::{gen_sha3_code, MemoryKind};
@@ -401,7 +401,7 @@ pub fn gen_associated_ops(
401401
state: &mut CircuitInputStateRef,
402402
geth_steps: &[GethExecStep],
403403
) -> Result<Vec<ExecStep>, Error> {
404-
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
404+
let check_level = if *CHECK_MEM_STACK_STRICT { 2 } else { 0 }; // 0: no check, 1: check and log error and fix, 2: check and assert_eq
405405
if check_level >= 1 {
406406
let memory_enabled = !geth_steps.iter().all(|s| s.memory.is_empty());
407407
if memory_enabled {
@@ -437,6 +437,38 @@ pub fn gen_associated_ops(
437437
state.call_ctx_mut()?.memory = geth_steps[0].memory.clone();
438438
}
439439
}
440+
let stack_enabled = !geth_steps.iter().all(|s| s.stack.is_empty());
441+
if stack_enabled {
442+
if state.call_ctx()?.stack != geth_steps[0].stack {
443+
log::error!(
444+
"wrong stack before {:?}. len in state {}, len in step {}",
445+
opcode_id,
446+
&state.call_ctx()?.stack.len(),
447+
&geth_steps[0].stack.len(),
448+
);
449+
log::error!("state stack {:?}", &state.call_ctx()?.stack);
450+
log::error!("step stack {:?}", &geth_steps[0].stack);
451+
452+
for i in
453+
0..std::cmp::min(state.call_ctx()?.stack.0.len(), geth_steps[0].stack.0.len())
454+
{
455+
let state_stack = state.call_ctx()?.stack.0[i];
456+
let step_stack = geth_steps[0].stack.0[i];
457+
if state_stack != step_stack {
458+
log::error!(
459+
"diff at {}: state {:?} != step {:?}",
460+
i,
461+
state_stack,
462+
step_stack
463+
);
464+
}
465+
}
466+
if check_level >= 2 {
467+
panic!("stack wrong");
468+
}
469+
state.call_ctx_mut()?.stack = geth_steps[0].stack.clone();
470+
}
471+
}
440472
}
441473

442474
// check if have error

bus-mapping/src/rpc.rs

+3-2
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::CHECK_MEM_STACK_STRICT;
1515

1616
/// Serialize a type.
1717
///
@@ -149,7 +149,8 @@ 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: *CHECK_MEM_STACK_STRICT,
153+
// TODO: disable_stack: !*CHECK_MEM_STACK_STRICT,
153154
..Default::default()
154155
};
155156
let cfg = serialize(&cfg);

bus-mapping/src/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub fn read_env_var<T: Clone + FromStr>(var_name: &'static str, default: T) -> T
1313
.unwrap_or(default)
1414
}
1515
/// ..
16-
pub static CHECK_MEM_STRICT: Lazy<bool> = Lazy::new(|| read_env_var("CHECK_MEM_STRICT", false));
16+
pub static CHECK_MEM_STACK_STRICT: Lazy<bool> =
17+
Lazy::new(|| read_env_var("CHECK_MEM_STACK_STRICT", false));
1718

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

eth-types/src/evm_types/stack.rs

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ impl Stack {
110110
Stack(words)
111111
}
112112

113+
/// Returns the length of the stack.
114+
pub fn len(&self) -> usize {
115+
self.0.len()
116+
}
117+
118+
/// Returns if the stack is empty.
119+
pub fn is_empty(&self) -> bool {
120+
self.0.is_empty()
121+
}
122+
113123
/// Returns the first available/free `StackAddress`.
114124
pub fn stack_pointer(&self) -> StackAddress {
115125
// Stack has 1024 slots.

testool/src/statetest/executor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ fn into_traceconfig(st: StateTest) -> (String, TraceConfig, StateTestResult) {
204204
}],
205205
accounts,
206206
logger_config: LoggerConfig {
207-
enable_memory: *bus_mapping::util::CHECK_MEM_STRICT,
207+
enable_memory: *bus_mapping::util::CHECK_MEM_STACK_STRICT,
208+
// TODO: disable_stack: !*bus_mapping::util::CHECK_MEM_STACK_STRICT,
208209
..Default::default()
209210
},
210211
#[cfg(feature = "shanghai")]

0 commit comments

Comments
 (0)