Skip to content

Commit d02b161

Browse files
committed
dump debugging data
1 parent 3050ccb commit d02b161

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

crates/libzkp/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,6 @@ pub fn verify_proof(proof: Vec<u8>, fork_name: &str, task_type: TaskType) -> eyr
111111
let verifier = verifier::get_verifier(fork_name)?;
112112

113113
let ret = verifier.lock().unwrap().verify(task_type, &proof)?;
114-
115-
if let Ok(debug_value) = std::env::var("ZKVM_DEBUG_PROOF") {
116-
use std::time::{SystemTime, UNIX_EPOCH};
117-
if !ret && debug_value.to_lowercase() == "true" {
118-
// Dump req.input to a temporary file
119-
let timestamp = SystemTime::now()
120-
.duration_since(UNIX_EPOCH)
121-
.unwrap_or_default()
122-
.as_secs();
123-
let filename = format!("/tmp/proof_{}.json", timestamp);
124-
if let Err(e) = std::fs::write(&filename, &proof) {
125-
eprintln!("Failed to write proof to file {}: {}", filename, e);
126-
} else {
127-
println!("Dumped failed proof to {}", filename);
128-
}
129-
}
130-
}
131-
132114
Ok(ret)
133115
}
134116

crates/libzkp_c/src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ use std::sync::OnceLock;
99

1010
static LOG_SETTINGS: OnceLock<Result<(), String>> = OnceLock::new();
1111

12+
fn enable_dump() -> bool {
13+
static ZKVM_DEBUG_DUMP: OnceLock<bool> = OnceLock::new();
14+
*ZKVM_DEBUG_DUMP.get_or_init(|| {
15+
std::env::var("ZKVM_DEBUG")
16+
.or_else(|_| std::env::var("ZKVM_DEBUG_PROOF"))
17+
.map(|s| s.to_lowercase() == "true")
18+
.unwrap_or(false)
19+
})
20+
}
21+
1222
/// # Safety
1323
#[no_mangle]
1424
pub unsafe extern "C" fn init_tracing() {
@@ -52,14 +62,32 @@ pub unsafe extern "C" fn init_l2geth(config: *const c_char) {
5262

5363
fn verify_proof(proof: *const c_char, fork_name: *const c_char, task_type: TaskType) -> c_char {
5464
let fork_name_str = c_char_to_str(fork_name);
65+
let proof_str = proof;
5566
let proof = c_char_to_vec(proof);
5667

5768
match libzkp::verify_proof(proof, fork_name_str, task_type) {
5869
Err(e) => {
5970
tracing::error!("{:?} verify failed, error: {:#}", task_type, e);
6071
false as c_char
6172
}
62-
Ok(result) => result as c_char,
73+
Ok(result) => {
74+
if !result && enable_dump() {
75+
use std::time::{SystemTime, UNIX_EPOCH};
76+
// Dump req.input to a temporary file
77+
let timestamp = SystemTime::now()
78+
.duration_since(UNIX_EPOCH)
79+
.unwrap_or_default()
80+
.as_secs();
81+
let filename = format!("/tmp/proof_{}.json", timestamp);
82+
let cstr = unsafe { std::ffi::CStr::from_ptr(proof_str) };
83+
if let Err(e) = std::fs::write(&filename, cstr.to_bytes()) {
84+
eprintln!("Failed to write proof to file {}: {}", filename, e);
85+
} else {
86+
println!("Dumped failed proof to {}", filename);
87+
}
88+
}
89+
result as c_char
90+
}
6391
}
6492
}
6593

@@ -167,6 +195,23 @@ pub unsafe extern "C" fn gen_universal_task(
167195
expected_pi_hash,
168196
}
169197
} else {
198+
if enable_dump() {
199+
use std::time::{SystemTime, UNIX_EPOCH};
200+
// Dump req.input to a temporary file
201+
let timestamp = SystemTime::now()
202+
.duration_since(UNIX_EPOCH)
203+
.unwrap_or_default()
204+
.as_secs();
205+
let c_str = unsafe { std::ffi::CStr::from_ptr(fork_name) };
206+
let filename = format!("/tmp/task_{}_{}.json", c_str.to_str().unwrap(), timestamp);
207+
let c_str = unsafe { std::ffi::CStr::from_ptr(task) };
208+
if let Err(e) = std::fs::write(&filename, c_str.to_bytes()) {
209+
eprintln!("Failed to write task to file {}: {}", filename, e);
210+
} else {
211+
println!("Dumped failed task to {}", filename);
212+
}
213+
}
214+
170215
tracing::error!("gen_universal_task failed, error: {:#}", ret.unwrap_err());
171216
failed_handling_result()
172217
}

0 commit comments

Comments
 (0)