From dae0d6c478a83c80e3b3753c29f89e4ecd2a6313 Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Fri, 10 Nov 2023 14:26:27 -0300 Subject: [PATCH] Make some assertions more conservative (#876) * Make the REPL raise errors properly instead of just printing locally so loading files with mistakes causes an unsuccessful termination * Fix errors on some of our demos due to the fix above * Fix the chain meta command so it persists commitments with the right secret * Assert that the output of the padded frame is the same as the one from the original last frame --- demo/bank.lurk | 10 ++++---- demo/vdf.lurk | 2 +- src/cli/repl.rs | 53 ++++++++++++++++++--------------------- src/cli/repl/meta_cmd.rs | 5 ++-- src/lem/multiframe.rs | 4 +-- tests/lurk-cli-tests.rs | 17 +++++++++++++ tests/lurk-files-tests.rs | 1 - 7 files changed, 52 insertions(+), 40 deletions(-) diff --git a/demo/bank.lurk b/demo/bank.lurk index 8d7a020a1e..dd9aa3379c 100644 --- a/demo/bank.lurk +++ b/demo/bank.lurk @@ -219,7 +219,7 @@ ledger2 ;; Now we can transfer one unit from Church to Satoshi like before. -!(chain 0x0757a47095c97d0a58a8ff66a2146949ee9d60d3f0a82887c203edf1748be711 '(1 0 2)) +!(chain 0x099f5f1cef164ac0a5ef8d6b0cefd930d2d0bf40666df3ae1b12abc33a5d297a '(1 0 2)) !(prove) @@ -227,16 +227,16 @@ ledger2 ;; Then we can transfer 5 more, proceeding from the new head of the chain. -!(chain 0x0d0b562063718f3623c1749268da4dd74ed33142edc411f4e74d7a84e64f3f30 '(5 0 2)) +!(chain 0x11abc1857d88646e3a6a2d0be605a3639feca2b79191c65efb7c6c139465820e '(5 0 2)) !(prove) -!(verify "Nova_Pallas_10_045d2fef5862777cf50a9dc8df50650f1aebfcbfb8135adfea57fb8f45389af4") +!(verify "Nova_Pallas_10_3b5c0928297a8380b1003b343b2b35522b3c16a03fbf69b3deea91c50cbdfc66") ;; And once more, this time we'll transfer 20 from Turing to Church. -!(chain 0x3e232dc8d44c85697fa4205d0d945014d34b980af498aed9194216c27c489e05 '(20 1 0)) +!(chain 0x007f5ed09b15a2af11a0504a46884aff364eb7bb1e7e9543431443b962166cb5 '(20 1 0)) !(prove) -!(verify "Nova_Pallas_10_25c76318cda48570568b9bb766ee9a906790c4dc94c96b1d6549e96c3bff8aa7") +!(verify "Nova_Pallas_10_3dc75e7fc1fd2a629b04e6e7469ed20711a4012b03c3f1205c18a351b9cdb7ca") diff --git a/demo/vdf.lurk b/demo/vdf.lurk index f83a2cf5bb..98622152af 100644 --- a/demo/vdf.lurk +++ b/demo/vdf.lurk @@ -51,7 +51,7 @@ !(prove) -!(verify "Nova_Pallas_10_20c429019e3eade18d0182febe27b987af8da1ab15cf55b0794296deb0435929") +!(verify "Nova_Pallas_10_0edc673ca67889bd824bb09d6be89a1b93fc7bc013fff4f4cd50e7bf10587831") !(def timelock-encrypt (lambda (secret-key plaintext rounds) (let ((ciphertext (+ secret-key plaintext)) diff --git a/src/cli/repl.rs b/src/cli/repl.rs index 4c390e3ec0..5788789720 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -411,22 +411,21 @@ impl Repl { } pub fn handle_non_meta(&mut self, expr_ptr: Ptr) -> Result<()> { - self.eval_expr_and_memoize(expr_ptr) - .map(|(output, iterations)| { - let iterations_display = Self::pretty_iterations_display(iterations); - match output[2].tag() { - Tag::Cont(ContTag::Terminal) => { - println!( - "[{iterations_display}] => {}", - output[0].fmt_to_string(&self.store, &self.state.borrow()) - ) - } - Tag::Cont(ContTag::Error) => { - println!("Evaluation encountered an error after {iterations_display}") - } - _ => println!("Limit reached after {iterations_display}"), - } - }) + let (output, iterations) = self.eval_expr_and_memoize(expr_ptr)?; + let iterations_display = Self::pretty_iterations_display(iterations); + match output[2].tag() { + Tag::Cont(ContTag::Terminal) => { + println!( + "[{iterations_display}] => {}", + output[0].fmt_to_string(&self.store, &self.state.borrow()) + ); + Ok(()) + } + Tag::Cont(ContTag::Error) => { + bail!("Evaluation encountered an error after {iterations_display}") + } + _ => bail!("Limit reached after {iterations_display}"), + } } fn handle_meta(&mut self, expr_ptr: Ptr) -> Result<()> { @@ -437,7 +436,7 @@ impl Repl { match self.meta.get(cmdstr) { Some(cmd) => match (cmd.run)(self, cmdstr, &cdr) { Ok(()) => (), - Err(e) => println!("meta command failed with {}", e), + Err(e) => bail!("Meta command failed with {}", e), }, None => bail!("Unsupported meta command: {cmdstr}"), } @@ -464,6 +463,8 @@ impl Repl { let (syntax_start, mut new_input, ptr, is_meta) = self.store.read_maybe_meta(self.state.clone(), &input)?; if demo { + // adjustment to print the exclamation mark in the right place + let syntax_start = syntax_start - usize::from(is_meta); let potential_commentaries = &input[..syntax_start]; let actual_syntax = &input[syntax_start..new_input.location_offset()]; let input_marker = &self.input_marker(); @@ -488,11 +489,7 @@ impl Repl { } pub fn load_file(&mut self, file_path: &Utf8Path, demo: bool) -> Result<()> { - let input = if std::env::var("LURK_PANIC_IF_CANT_LOAD") == Ok("true".into()) { - read_to_string(file_path).unwrap_or_else(|_| panic!("File not found: {}", file_path)) - } else { - read_to_string(file_path)? - }; + let input = read_to_string(file_path)?; if demo { println!("Loading {file_path} in demo mode"); } else { @@ -543,24 +540,22 @@ impl Repl { Ok((.., expr_ptr, is_meta)) => { if is_meta { if let Err(e) = self.handle_meta(expr_ptr) { - println!("!Error: {e}"); + eprintln!("!Error: {e}") } } else if let Err(e) = self.handle_non_meta(expr_ptr) { - println!("Error: {e}"); + eprintln!("Error: {e}") } } Err(parser::Error::NoInput) => (), - Err(e) => { - println!("Read error: {e}") - } + Err(e) => eprintln!("Read error: {e}"), } } Err(ReadlineError::Interrupted | ReadlineError::Eof) => { println!("Exiting..."); break; } - Err(err) => { - println!("Read line error: {err}"); + Err(e) => { + eprintln!("Read line error: {e}"); break; } } diff --git a/src/cli/repl/meta_cmd.rs b/src/cli/repl/meta_cmd.rs index cff2dbcb5a..d8721c100d 100644 --- a/src/cli/repl/meta_cmd.rs +++ b/src/cli/repl/meta_cmd.rs @@ -598,11 +598,12 @@ impl MetaCmd { let Ptr::Atom(Tag::Expr(ExprTag::Comm), hash) = comm else { bail!("Second component of a chain must be a commitment") }; - let (_, fun) = repl + // retrieve from store to persist + let (secret, fun) = repl .store .open(hash) .expect("data must have been committed"); - repl.hide(F::NON_HIDING_COMMITMENT_SECRET, *fun) + repl.hide(*secret, *fun) }, }; } diff --git a/src/lem/multiframe.rs b/src/lem/multiframe.rs index 473861eea3..20b7b14482 100644 --- a/src/lem/multiframe.rs +++ b/src/lem/multiframe.rs @@ -481,7 +481,7 @@ impl<'a, F: LurkField, C: Coprocessor + 'a> MultiFrameTrait<'a, F, C> for Mul let padding_frame = lurk_step .call_simple(&output, store, lang, 0) .expect("reduction step failed"); - assert_eq!(padding_frame.input, padding_frame.output); + assert_eq!(output, padding_frame.output); inner_frames.resize(reduction_count, padding_frame.clone()); inner_frames } else { @@ -554,7 +554,7 @@ impl<'a, F: LurkField, C: Coprocessor + 'a> MultiFrameTrait<'a, F, C> for Mul let padding_frame = lurk_step .call_simple(&output, store, lang, 0) .expect("reduction step failed"); - assert_eq!(padding_frame.input, padding_frame.output); + assert_eq!(output, padding_frame.output); inner_frames.resize(reduction_count, padding_frame); } diff --git a/tests/lurk-cli-tests.rs b/tests/lurk-cli-tests.rs index 1ff08829ca..d025b19c5f 100644 --- a/tests/lurk-cli-tests.rs +++ b/tests/lurk-cli-tests.rs @@ -58,6 +58,7 @@ fn test_prove_and_verify() { file.write_all(b"!(verify \"Nova_Pallas_10_3f2526abf20fc9006dd93c0d3ff49954ef070ef52d2e88426974de42cc27bdb2\")\n").unwrap(); let mut cmd = lurk_cmd(); + cmd.env("LURK_PERF", "max-parallel-simple"); cmd.arg("load"); cmd.arg(lurk_file.into_string()); cmd.arg("--public-params-dir"); @@ -69,3 +70,19 @@ fn test_prove_and_verify() { cmd.assert().success(); } + +#[test] +fn test_repl_panic() { + let tmp_dir = Builder::new().prefix("tmp").tempdir().unwrap(); + let tmp_dir = Utf8Path::from_path(tmp_dir.path()).unwrap(); + let lurk_file = tmp_dir.join("panic.lurk"); + + let mut file = File::create(lurk_file.clone()).unwrap(); + // `x` is not bound + file.write_all(b"x\n").unwrap(); + + let mut cmd = lurk_cmd(); + cmd.arg("load"); + cmd.arg(lurk_file.into_string()); + cmd.assert().failure(); +} diff --git a/tests/lurk-files-tests.rs b/tests/lurk-files-tests.rs index 6ab9b14780..9b66e961b8 100644 --- a/tests/lurk-files-tests.rs +++ b/tests/lurk-files-tests.rs @@ -37,7 +37,6 @@ fn test_lurk_lib() { lurk_lib_examples.into_par_iter().for_each(|f| { let mut cmd = lurk_cmd(); cmd.current_dir(LURK_LIB_EXAMPLES_DIR); - cmd.env("LURK_PANIC_IF_CANT_LOAD", "true"); cmd.arg(f); cmd.assert().success(); });