From 02833b80e0aaa5fa07d628eafa5ee56ecd5e8f82 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 30 Jul 2020 18:17:55 -0700 Subject: [PATCH] add .in and .state tests --- compiler/src/function/input/array.rs | 4 - compiler/src/function/main_function.rs | 4 - compiler/tests/input_files/mod.rs | 3 + .../program_inputs}/inputs/main.in | 0 .../program_inputs}/inputs/main_fail_name.in | 0 .../program_inputs}/inputs/main_fail_type.in | 0 .../program_inputs}/inputs/main_multiple.in | 0 .../program_inputs}/main.leo | 0 .../program_inputs}/main_multiple.leo | 0 .../program_inputs}/mod.rs | 0 .../program_inputs_and_state/access.leo | 17 ++++ .../program_inputs_and_state/inputs/basic.in | 5 + .../inputs/basic.state | 12 +++ .../inputs/token_withdraw.in | 6 ++ .../inputs/token_withdraw.state | 24 +++++ .../program_inputs_and_state/mod.rs | 28 ++++++ .../tests/input_files/state/access_all.leo | 8 ++ .../tests/input_files/state/access_state.leo | 3 + .../input_files/state/inputs/basic.state | 10 ++ .../state/inputs/section_invalid.state | 4 + .../state/inputs/section_undefined.state | 4 + .../state/inputs/token_withdraw.state | 24 +++++ .../state/inputs/visibility_fail.state | 1 + compiler/tests/input_files/state/mod.rs | 62 ++++++++++++ compiler/tests/mod.rs | 95 ++++++++++++++----- leo-inputs/src/leo-inputs.pest | 2 +- 26 files changed, 283 insertions(+), 33 deletions(-) create mode 100644 compiler/tests/input_files/mod.rs rename compiler/tests/{inputs => input_files/program_inputs}/inputs/main.in (100%) rename compiler/tests/{inputs => input_files/program_inputs}/inputs/main_fail_name.in (100%) rename compiler/tests/{inputs => input_files/program_inputs}/inputs/main_fail_type.in (100%) rename compiler/tests/{inputs => input_files/program_inputs}/inputs/main_multiple.in (100%) rename compiler/tests/{inputs => input_files/program_inputs}/main.leo (100%) rename compiler/tests/{inputs => input_files/program_inputs}/main_multiple.leo (100%) rename compiler/tests/{inputs => input_files/program_inputs}/mod.rs (100%) create mode 100644 compiler/tests/input_files/program_inputs_and_state/access.leo create mode 100644 compiler/tests/input_files/program_inputs_and_state/inputs/basic.in create mode 100644 compiler/tests/input_files/program_inputs_and_state/inputs/basic.state create mode 100644 compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.in create mode 100644 compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.state create mode 100644 compiler/tests/input_files/program_inputs_and_state/mod.rs create mode 100644 compiler/tests/input_files/state/access_all.leo create mode 100644 compiler/tests/input_files/state/access_state.leo create mode 100644 compiler/tests/input_files/state/inputs/basic.state create mode 100644 compiler/tests/input_files/state/inputs/section_invalid.state create mode 100644 compiler/tests/input_files/state/inputs/section_undefined.state create mode 100644 compiler/tests/input_files/state/inputs/token_withdraw.state create mode 100644 compiler/tests/input_files/state/inputs/visibility_fail.state create mode 100644 compiler/tests/input_files/state/mod.rs diff --git a/compiler/src/function/input/array.rs b/compiler/src/function/input/array.rs index 8f2aa19e94..5614e5b900 100644 --- a/compiler/src/function/input/array.rs +++ b/compiler/src/function/input/array.rs @@ -2,7 +2,6 @@ use crate::{ errors::FunctionError, - function::check_arguments_length, program::{new_scope, ConstrainedProgram}, value::ConstrainedValue, GroupType, @@ -30,9 +29,6 @@ impl> ConstrainedProgram { match input_value { Some(InputValue::Array(arr)) => { - // Check the dimension of the array - check_arguments_length(expected_length, arr.len(), span.clone())?; - // Allocate each value in the current row for (i, value) in arr.into_iter().enumerate() { let value_name = new_scope(name.clone(), i.to_string()); diff --git a/compiler/src/function/main_function.rs b/compiler/src/function/main_function.rs index 11a0a5cb66..76bc30b74c 100644 --- a/compiler/src/function/main_function.rs +++ b/compiler/src/function/main_function.rs @@ -2,7 +2,6 @@ use crate::{ errors::FunctionError, - function::check_arguments_length, program::{new_scope, ConstrainedProgram}, GroupType, OutputBytes, @@ -26,9 +25,6 @@ impl> ConstrainedProgram { let function_name = new_scope(scope.clone(), function.get_name()); let registers = inputs.get_registers(); - // Make sure we are given the correct number of inputs - check_arguments_length(function.inputs.len(), inputs.len(), function.span.clone())?; - // Iterate over main function inputs and allocate new passed-by variable values let mut input_variables = vec![]; for input_model in function.inputs.clone().into_iter() { diff --git a/compiler/tests/input_files/mod.rs b/compiler/tests/input_files/mod.rs new file mode 100644 index 0000000000..a420a7e6e2 --- /dev/null +++ b/compiler/tests/input_files/mod.rs @@ -0,0 +1,3 @@ +mod program_inputs; +mod program_inputs_and_state; +mod state; diff --git a/compiler/tests/inputs/inputs/main.in b/compiler/tests/input_files/program_inputs/inputs/main.in similarity index 100% rename from compiler/tests/inputs/inputs/main.in rename to compiler/tests/input_files/program_inputs/inputs/main.in diff --git a/compiler/tests/inputs/inputs/main_fail_name.in b/compiler/tests/input_files/program_inputs/inputs/main_fail_name.in similarity index 100% rename from compiler/tests/inputs/inputs/main_fail_name.in rename to compiler/tests/input_files/program_inputs/inputs/main_fail_name.in diff --git a/compiler/tests/inputs/inputs/main_fail_type.in b/compiler/tests/input_files/program_inputs/inputs/main_fail_type.in similarity index 100% rename from compiler/tests/inputs/inputs/main_fail_type.in rename to compiler/tests/input_files/program_inputs/inputs/main_fail_type.in diff --git a/compiler/tests/inputs/inputs/main_multiple.in b/compiler/tests/input_files/program_inputs/inputs/main_multiple.in similarity index 100% rename from compiler/tests/inputs/inputs/main_multiple.in rename to compiler/tests/input_files/program_inputs/inputs/main_multiple.in diff --git a/compiler/tests/inputs/main.leo b/compiler/tests/input_files/program_inputs/main.leo similarity index 100% rename from compiler/tests/inputs/main.leo rename to compiler/tests/input_files/program_inputs/main.leo diff --git a/compiler/tests/inputs/main_multiple.leo b/compiler/tests/input_files/program_inputs/main_multiple.leo similarity index 100% rename from compiler/tests/inputs/main_multiple.leo rename to compiler/tests/input_files/program_inputs/main_multiple.leo diff --git a/compiler/tests/inputs/mod.rs b/compiler/tests/input_files/program_inputs/mod.rs similarity index 100% rename from compiler/tests/inputs/mod.rs rename to compiler/tests/input_files/program_inputs/mod.rs diff --git a/compiler/tests/input_files/program_inputs_and_state/access.leo b/compiler/tests/input_files/program_inputs_and_state/access.leo new file mode 100644 index 0000000000..0d924e357e --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/access.leo @@ -0,0 +1,17 @@ +function main( + registers, + state, + record, + state_leaf, + data: u8[32] +) { + assert_eq!(registers.value_balance, 0u64); + + assert_eq!(state.leaf_index, 0u32); + + assert_eq!(record.value, 5u64); + + assert_eq!(state_leaf.network_id, 0u8); + + assert_eq!(data, [0u8; 32]); +} \ No newline at end of file diff --git a/compiler/tests/input_files/program_inputs_and_state/inputs/basic.in b/compiler/tests/input_files/program_inputs_and_state/inputs/basic.in new file mode 100644 index 0000000000..b62eee0443 --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/inputs/basic.in @@ -0,0 +1,5 @@ +[main] +a: bool = true; + +[registers] +b: bool = true; \ No newline at end of file diff --git a/compiler/tests/input_files/program_inputs_and_state/inputs/basic.state b/compiler/tests/input_files/program_inputs_and_state/inputs/basic.state new file mode 100644 index 0000000000..78706190fe --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/inputs/basic.state @@ -0,0 +1,12 @@ +[[public]] + +[state] +a: bool = true; + +[[private]] + +[record] +a: bool = true; + +[state_leaf] +a: bool = true; \ No newline at end of file diff --git a/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.in b/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.in new file mode 100644 index 0000000000..5e570eef23 --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.in @@ -0,0 +1,6 @@ +[main] +data: u8[32] = [0u8; 32]; + +[registers] +token_id: u8[32] = [0u8; 32]; +value_balance: u64 = 0; \ No newline at end of file diff --git a/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.state b/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.state new file mode 100644 index 0000000000..9d35cf30aa --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/inputs/token_withdraw.state @@ -0,0 +1,24 @@ +[[public]] + +[state] +leaf_index: u32 = 0; +root: u8[32] = [0u8; 32]; + +[[private]] + +[record] +serial_number: u8[32] = [0u8; 32]; +commitment: u8[32] = [0u8; 32]; +owner: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8; +value: u64 = 5; +payload: u8[32] = [0u8; 32]; +birth_program_id: u8[32] = [0u8; 32]; +death_program_id: u8[32] = [0u8; 32]; +serial_number_nonce: u8[32] = [0u8; 32]; +commitment_randomness: u8[32] = [0u8; 32]; + +[state_leaf] +path: u8[32][2] = [ [0u8; 32], [0u8; 32] ]; +memo: u8[32] = [0u8; 32]; +network_id: u8 = 0; +leaf_randomness: u8[32] = [0u8; 32]; \ No newline at end of file diff --git a/compiler/tests/input_files/program_inputs_and_state/mod.rs b/compiler/tests/input_files/program_inputs_and_state/mod.rs new file mode 100644 index 0000000000..9cfa29f248 --- /dev/null +++ b/compiler/tests/input_files/program_inputs_and_state/mod.rs @@ -0,0 +1,28 @@ +use crate::{assert_satisfied, parse_inputs_and_state, parse_program_with_inputs_and_state}; + +#[test] +fn test_basic() { + let inputs_bytes = include_bytes!("inputs/basic.in"); + let state_bytes = include_bytes!("inputs/basic.state"); + + parse_inputs_and_state(inputs_bytes, state_bytes).unwrap(); +} + +#[test] +fn test_full() { + let inputs_bytes = include_bytes!("inputs/token_withdraw.in"); + let state_bytes = include_bytes!("inputs/token_withdraw.state"); + + parse_inputs_and_state(inputs_bytes, state_bytes).unwrap(); +} + +#[test] +fn test_access() { + let program_bytes = include_bytes!("access.leo"); + let inputs_bytes = include_bytes!("inputs/token_withdraw.in"); + let state_bytes = include_bytes!("inputs/token_withdraw.state"); + + let program = parse_program_with_inputs_and_state(program_bytes, inputs_bytes, state_bytes).unwrap(); + + assert_satisfied(program); +} diff --git a/compiler/tests/input_files/state/access_all.leo b/compiler/tests/input_files/state/access_all.leo new file mode 100644 index 0000000000..0d9ab2e64c --- /dev/null +++ b/compiler/tests/input_files/state/access_all.leo @@ -0,0 +1,8 @@ +function main(state, record, state_leaf) { + assert_eq!(state.root, [0u8; 32]); + + let expected: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8; + assert_eq!(record.owner, expected); + + assert_eq!(state_leaf.network_id, 0u8); +} \ No newline at end of file diff --git a/compiler/tests/input_files/state/access_state.leo b/compiler/tests/input_files/state/access_state.leo new file mode 100644 index 0000000000..096cd2be83 --- /dev/null +++ b/compiler/tests/input_files/state/access_state.leo @@ -0,0 +1,3 @@ +function main(state) { + assert_eq!(state.root, [0u8; 32]); +} \ No newline at end of file diff --git a/compiler/tests/input_files/state/inputs/basic.state b/compiler/tests/input_files/state/inputs/basic.state new file mode 100644 index 0000000000..2f40f7f1c4 --- /dev/null +++ b/compiler/tests/input_files/state/inputs/basic.state @@ -0,0 +1,10 @@ +[[public]] +[state] +root: bool = true; + +[[private]] +[record] +id: bool = false; + +[state_leaf] +leaf: bool = true; \ No newline at end of file diff --git a/compiler/tests/input_files/state/inputs/section_invalid.state b/compiler/tests/input_files/state/inputs/section_invalid.state new file mode 100644 index 0000000000..955b881192 --- /dev/null +++ b/compiler/tests/input_files/state/inputs/section_invalid.state @@ -0,0 +1,4 @@ +[[public]] + +[record] +a: bool = true; \ No newline at end of file diff --git a/compiler/tests/input_files/state/inputs/section_undefined.state b/compiler/tests/input_files/state/inputs/section_undefined.state new file mode 100644 index 0000000000..5f881cbb29 --- /dev/null +++ b/compiler/tests/input_files/state/inputs/section_undefined.state @@ -0,0 +1,4 @@ +[[public]] + +[invalid] +a: bool = true; \ No newline at end of file diff --git a/compiler/tests/input_files/state/inputs/token_withdraw.state b/compiler/tests/input_files/state/inputs/token_withdraw.state new file mode 100644 index 0000000000..9d35cf30aa --- /dev/null +++ b/compiler/tests/input_files/state/inputs/token_withdraw.state @@ -0,0 +1,24 @@ +[[public]] + +[state] +leaf_index: u32 = 0; +root: u8[32] = [0u8; 32]; + +[[private]] + +[record] +serial_number: u8[32] = [0u8; 32]; +commitment: u8[32] = [0u8; 32]; +owner: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8; +value: u64 = 5; +payload: u8[32] = [0u8; 32]; +birth_program_id: u8[32] = [0u8; 32]; +death_program_id: u8[32] = [0u8; 32]; +serial_number_nonce: u8[32] = [0u8; 32]; +commitment_randomness: u8[32] = [0u8; 32]; + +[state_leaf] +path: u8[32][2] = [ [0u8; 32], [0u8; 32] ]; +memo: u8[32] = [0u8; 32]; +network_id: u8 = 0; +leaf_randomness: u8[32] = [0u8; 32]; \ No newline at end of file diff --git a/compiler/tests/input_files/state/inputs/visibility_fail.state b/compiler/tests/input_files/state/inputs/visibility_fail.state new file mode 100644 index 0000000000..c5928ddcbd --- /dev/null +++ b/compiler/tests/input_files/state/inputs/visibility_fail.state @@ -0,0 +1 @@ +[[pure]] \ No newline at end of file diff --git a/compiler/tests/input_files/state/mod.rs b/compiler/tests/input_files/state/mod.rs new file mode 100644 index 0000000000..e7736d1284 --- /dev/null +++ b/compiler/tests/input_files/state/mod.rs @@ -0,0 +1,62 @@ +use crate::{assert_satisfied, parse_program_with_state, parse_state}; + +#[test] +fn test_basic() { + let bytes = include_bytes!("inputs/basic.state"); + + parse_state(bytes).unwrap(); +} + +#[test] +fn test_token_withdraw() { + let bytes = include_bytes!("inputs/token_withdraw.state"); + + parse_state(bytes).unwrap(); +} + +#[test] +fn test_access_state() { + let program_bytes = include_bytes!("access_state.leo"); + let state_bytes = include_bytes!("inputs/token_withdraw.state"); + + let program = parse_program_with_state(program_bytes, state_bytes).unwrap(); + + assert_satisfied(program); +} + +#[test] +fn test_access_all() { + let program_bytes = include_bytes!("access_all.leo"); + let state_bytes = include_bytes!("inputs/token_withdraw.state"); + + let program = parse_program_with_state(program_bytes, state_bytes).unwrap(); + + assert_satisfied(program); +} + +#[test] +fn test_visibility_fail() { + let state_bytes = include_bytes!("inputs/visibility_fail.state"); + + let is_err = parse_state(state_bytes).is_err(); + + assert!(is_err); +} + +#[test] +fn test_section_undefined() { + let state_bytes = include_bytes!("inputs/section_undefined.state"); + + let is_err = parse_state(state_bytes).is_err(); + + assert!(is_err); +} + +#[test] +fn test_section_invalid() { + let state_bytes = include_bytes!("inputs/section_invalid.state"); + + let is_err = parse_state(state_bytes).is_err(); + + assert!(is_err); +} diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index a46f0a615a..1837615b3a 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -6,7 +6,7 @@ pub mod field; pub mod function; pub mod group; pub mod import; -pub mod inputs; +pub mod input_files; pub mod integers; pub mod macros; pub mod mutability; @@ -34,21 +34,6 @@ const EMPTY_FILE: &str = ""; pub type EdwardsTestCompiler = Compiler; pub type EdwardsConstrainedValue = ConstrainedValue; -pub fn parse_program_with_inputs( - program_bytes: &[u8], - input_bytes: &[u8], -) -> Result { - let mut compiler = new_compiler(); - - let program_string = String::from_utf8_lossy(program_bytes); - let inputs_string = String::from_utf8_lossy(input_bytes); - - compiler.parse_inputs(&inputs_string, EMPTY_FILE)?; - compiler.parse_program(&program_string)?; - - Ok(compiler) -} - fn new_compiler() -> EdwardsTestCompiler { let program_name = "test".to_string(); let path = PathBuf::from("/test/src/main.leo"); @@ -70,7 +55,76 @@ pub(crate) fn parse_inputs(bytes: &[u8]) -> Result Result { + let mut compiler = new_compiler(); + let state_string = String::from_utf8_lossy(bytes); + + compiler.parse_inputs(EMPTY_FILE, &state_string)?; + + Ok(compiler) +} + +pub(crate) fn parse_inputs_and_state( + inputs_bytes: &[u8], + state_bytes: &[u8], +) -> Result { + let mut compiler = new_compiler(); + let inputs_string = String::from_utf8_lossy(inputs_bytes); + let state_string = String::from_utf8_lossy(state_bytes); + + compiler.parse_inputs(&inputs_string, &state_string)?; + + Ok(compiler) +} + +pub fn parse_program_with_inputs( + program_bytes: &[u8], + input_bytes: &[u8], +) -> Result { + let mut compiler = new_compiler(); + + let program_string = String::from_utf8_lossy(program_bytes); + let inputs_string = String::from_utf8_lossy(input_bytes); + + compiler.parse_inputs(&inputs_string, EMPTY_FILE)?; + compiler.parse_program(&program_string)?; + + Ok(compiler) +} + +pub fn parse_program_with_state( + program_bytes: &[u8], + state_bytes: &[u8], +) -> Result { + let mut compiler = new_compiler(); + + let program_string = String::from_utf8_lossy(program_bytes); + let state_string = String::from_utf8_lossy(state_bytes); + + compiler.parse_inputs(EMPTY_FILE, &state_string)?; + compiler.parse_program(&program_string)?; + + Ok(compiler) +} + +pub fn parse_program_with_inputs_and_state( + program_bytes: &[u8], + inputs_bytes: &[u8], + state_bytes: &[u8], +) -> Result { + let mut compiler = new_compiler(); + + let program_string = String::from_utf8_lossy(program_bytes); + let inputs_string = String::from_utf8_lossy(inputs_bytes); + let state_string = String::from_utf8_lossy(state_bytes); + + compiler.parse_inputs(&inputs_string, &state_string)?; + compiler.parse_program(&program_string)?; Ok(compiler) } @@ -106,13 +160,6 @@ pub(crate) fn expect_synthesis_error(program: EdwardsTestCompiler) { assert!(!cs.is_satisfied()); } -// pub(crate) fn fail_enforce(program: EdwardsTestCompiler) { -// match get_compiler_error(program) { -// CompilerError::FunctionError(FunctionError::StatementError(StatementError::Error(_))) => {} -// error => panic!("Expected evaluate error, got {}", error), -// } -// } - pub(crate) fn generate_main_inputs(inputs: Vec<(&str, Option)>) -> MainInputs { let mut main_inputs = MainInputs::new(); diff --git a/leo-inputs/src/leo-inputs.pest b/leo-inputs/src/leo-inputs.pest index 589271f33f..986548f78f 100644 --- a/leo-inputs/src/leo-inputs.pest +++ b/leo-inputs/src/leo-inputs.pest @@ -76,7 +76,7 @@ type_boolean = { "bool" } type_address = { "address" } // Declared in types/data_type.rs -type_data = { type_field | type_group | type_boolean | type_integer } +type_data = { type_field | type_group | type_boolean | type_address | type_integer } // Declared in types/array_type.rs type_array = { type_data ~ ("[" ~ value_number ~ "]")+ }