Skip to content

Commit

Permalink
feat: Extensible VirtualMachinError and removed PartialEq trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zarboq committed Feb 3, 2023
1 parent ad21e1d commit 2d6f092
Show file tree
Hide file tree
Showing 48 changed files with 1,343 additions and 1,078 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.1.0" }

[dev-dependencies]
iai = "0.1"
assert_matches = "1.5.0"

[dev-dependencies.rusty-hook]
version = "0.11"
Expand Down
47 changes: 25 additions & 22 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod tests {
utils::test_utils::*,
vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory},
};
use assert_matches::assert_matches;
use std::any::Any;

#[test]
Expand All @@ -245,7 +246,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::CantSubOffset(
5, 26
Expand All @@ -266,11 +267,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
)))
x
))) if x == MaybeRelocatable::from((2, 0))
);
}

Expand All @@ -286,11 +287,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(
VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0)))
))
VirtualMachineError::ExpectedRelocatable(x)
)) if x == MaybeRelocatable::from((1, 0))
);
}

Expand All @@ -316,7 +317,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::BigintToU32Fail)
);
Expand All @@ -334,11 +335,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
)))
x
))) if x == MaybeRelocatable::from((2, 0))
);
}

Expand All @@ -355,7 +356,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["blake2s_ptr_end"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check the inserted data
let expected_data: [u32; 204] = [
1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635,
Expand Down Expand Up @@ -398,15 +399,17 @@ mod tests {
vm.memory = memory![((1, 0), (2, 0)), ((2, 0), (2, 0))];
let ids_data = ids_data!["blake2s_ptr_end"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from(Felt::new(1795745351))
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((2, 0)) &&
y == MaybeRelocatable::from((2, 0)) &&
z == MaybeRelocatable::from(Felt::new(1795745351))
);
}

Expand All @@ -418,7 +421,7 @@ mod tests {
//Initialize fp
vm.run_context.fp = 1;
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::FailedToGetIds)
);
Expand All @@ -436,7 +439,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -464,7 +467,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -492,7 +495,7 @@ mod tests {
add_segments!(vm, 1);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -520,7 +523,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ mod tests {
vm_memory::memory::Memory,
},
};
use assert_matches::assert_matches;
use num_traits::{One, Zero};
use std::any::Any;

Expand Down Expand Up @@ -502,25 +503,27 @@ mod tests {
//Insert something into ap
vm.memory = memory![((1, 6), (1, 6))];
//ids and references are not needed for this test
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((1, 6)),
MaybeRelocatable::from((1, 6)),
MaybeRelocatable::from((3, 0))
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((1, 6)) &&
y == MaybeRelocatable::from((1, 6)) &&
z == MaybeRelocatable::from((3, 0))
);
}

#[test]
fn run_unknown_hint() {
let hint_code = "random_invalid_code";
let mut vm = vm!();
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::UnknownHint(hint_code.to_string())),
Err(HintError::UnknownHint(x)) if x == *hint_code.to_string()
);
}

Expand Down Expand Up @@ -551,11 +554,11 @@ mod tests {
vm.memory = memory![((1, 1), (1, 0))];

let ids_data = ids_data!["len"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((1, 1))
)))
x
))) if x == MaybeRelocatable::from((1, 1))
);
}

Expand Down Expand Up @@ -588,9 +591,9 @@ mod tests {
// initialize ids
vm.memory = memory![((0, 2), 5)];
let ids_data = ids_data!["continue_copying"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::VariableNotInScopeError("n".to_string()))
Err(HintError::VariableNotInScopeError(x)) if x == *"n".to_string()
);
}

Expand All @@ -609,15 +612,17 @@ mod tests {
vm.memory = memory![((1, 1), 5)];

let ids_data = ids_data!["continue_copying"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((1, 1)),
MaybeRelocatable::from(Felt::new(5)),
MaybeRelocatable::from(Felt::zero())
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((1, 1)) &&
y == MaybeRelocatable::from(Felt::new(5)) &&
z == MaybeRelocatable::from(Felt::zero())
);
}

Expand All @@ -641,7 +646,7 @@ mod tests {
// new vm scope is not created so that the hint raises an error:
// initialize memory segments
add_segments!(vm, 1);
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::FromScopeError(
ExecScopeError::ExitMainScopeError
Expand All @@ -656,7 +661,7 @@ mod tests {
let mut vm = vm!();
let mut exec_scopes = ExecutionScopes::new();
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Ok(())
);
Expand Down Expand Up @@ -706,9 +711,9 @@ mod tests {
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(2))];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::KeccakMaxSize(Felt::new(5), Felt::new(2)))
Err(HintError::KeccakMaxSize(x, y)) if x == Felt::new(5) && y == Felt::new(2)
);
}

Expand Down Expand Up @@ -752,9 +757,9 @@ mod tests {
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(10))];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::InvalidWordSize(Felt::new(-1)))
Err(HintError::InvalidWordSize(x)) if x == Felt::new(-1)
);
}

Expand Down Expand Up @@ -794,7 +799,7 @@ mod tests {
((1, 8), 0)
];
let ids_data = non_continuous_ids_data![("keccak_state", -7), ("high", -3), ("low", -2)];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::NoneInMemoryRange))
);
Expand Down Expand Up @@ -842,7 +847,7 @@ mod tests {
assert_eq!(exec_scopes.data.len(), 1);
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_eq!(
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
Expand All @@ -854,7 +859,7 @@ mod tests {
assert_eq!(exec_scopes.data.len(), 2);
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_eq!(
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ mod tests {
vm_core::VirtualMachine, vm_memory::memory::Memory,
},
};
use assert_matches::assert_matches;
use std::any::Any;

#[test]
Expand All @@ -285,7 +286,7 @@ mod tests {
vm.run_context.fp = 3;
//Create ids
let ids_data = ids_data!["low", "high", "inputs"];
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
}

#[test]
Expand All @@ -298,10 +299,10 @@ mod tests {
//Create ids
let ids_data = ids_data!["low", "high", "inputs"];
let error = run_hint!(vm, ids_data, hint_code);
assert!(matches!(
assert_matches!(
error,
Err(HintError::Internal(VirtualMachineError::MemoryError(_)))
));
);
}

#[test]
Expand All @@ -315,7 +316,7 @@ mod tests {

run_context!(vm, 0, 1, 1);
let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand Down Expand Up @@ -343,7 +344,7 @@ mod tests {
run_context!(vm, 0, 1, 1);

let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand All @@ -370,7 +371,7 @@ mod tests {
run_context!(vm, 0, 1, 1);

let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand Down
Loading

0 comments on commit 2d6f092

Please sign in to comment.