diff --git a/Cargo.toml b/Cargo.toml index 239b846..dd62950 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ snap = "1.1.0" tokio = {version = "1", features = ["full"]} - [[bin]] name = "analyzer" path = "src/main.rs" diff --git a/src/optimizor/cache_state_variables.rs b/src/optimizor/cache_state_variables.rs index 1212a75..484fe96 100644 --- a/src/optimizor/cache_state_variables.rs +++ b/src/optimizor/cache_state_variables.rs @@ -158,7 +158,7 @@ fn get_line_number_zero(src: &str, mut _prev: usize) -> usize { let lines: Vec<&str> = contract.lines().collect(); // Format the string with " = 0" at the end - let strss = format!(r"{}", src); + let strss = src.to_string(); // Compile the regex pattern for any function declaration let any_function_declaration_regex = Regex::new(r"function\s+\w*\s*\(").unwrap(); diff --git a/src/optimizor/write_zero_to_storage.rs b/src/optimizor/write_zero_to_storage.rs index 9cb2441..d73520c 100644 --- a/src/optimizor/write_zero_to_storage.rs +++ b/src/optimizor/write_zero_to_storage.rs @@ -45,56 +45,51 @@ pub fn write_zero_to_storage( if let Some(right_hand_side) = expression.get("rightHandSide") { - if right_hand_side.get("value") - == Some(&Value::String("0".to_string())) - { - if !right_hand_side + if right_hand_side.get("value") == Some(&Value::String("0".to_string())) && !right_hand_side .get("isConstant") .unwrap_or(&Value::Bool(true)) .as_bool() - .unwrap_or(true) - { - _prev = get_line_number_zero( - _name, _prev, - ); - let mut _inefficiency_id = format!( - "line_{}", - get_line_number_zero( - _name, _prev - ) - ); + .unwrap_or(true) { + _prev = get_line_number_zero( + _name, _prev, + ); + let mut _inefficiency_id = format!( + "line_{}", + get_line_number_zero( + _name, _prev + ) + ); - get_line_number_zero(_name, _prev); - _inefficiency_id = - format!("line_{}", _prev); - // Check if the slot exists in the map - if let Some(existing_value) = - gas_inefficiencies - .get_mut(&_inefficiency_id) - { - // Slot exists, append the new issue to the existing array - let mut existing_arr: Vec< - String, - > = serde_json::from_value( - existing_value.clone(), - ) - .unwrap_or_default(); + get_line_number_zero(_name, _prev); + _inefficiency_id = + format!("line_{}", _prev); + // Check if the slot exists in the map + if let Some(existing_value) = + gas_inefficiencies + .get_mut(&_inefficiency_id) + { + // Slot exists, append the new issue to the existing array + let mut existing_arr: Vec< + String, + > = serde_json::from_value( + existing_value.clone(), + ) + .unwrap_or_default(); - existing_arr.push("avoid writing zero to storage slot".to_string()); + existing_arr.push("avoid writing zero to storage slot".to_string()); - // Update the value in the map - gas_inefficiencies.insert( - _inefficiency_id, - json!(existing_arr), - ); - } else { - // Slot doesn't exist, create a new entry with a new array - let new_arr = vec!["avoid writing zero to storage slot"]; - gas_inefficiencies.insert( - _inefficiency_id, - json!(new_arr), - ); - } + // Update the value in the map + gas_inefficiencies.insert( + _inefficiency_id, + json!(existing_arr), + ); + } else { + // Slot doesn't exist, create a new entry with a new array + let new_arr = vec!["avoid writing zero to storage slot"]; + gas_inefficiencies.insert( + _inefficiency_id, + json!(new_arr), + ); } } } diff --git a/tests/all_test.rs b/tests/all_test.rs index 43548df..9da6e16 100644 --- a/tests/all_test.rs +++ b/tests/all_test.rs @@ -1,6 +1,6 @@ #[cfg(test)] -mod tests { - use gas_analyzer::{optimizor, utils}; +mod expect_revert_tests { + use gas_analyzer::{optimizor}; use serde_json::{Map, Value}; use std::fs; @@ -11,6 +11,7 @@ mod tests { Ok(content) } #[test] + #[should_panic] fn test_singe_bytes32() { //Generate the ast optimizor::ast::ast(); @@ -18,7 +19,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::bytes32::bytes32(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -27,6 +28,7 @@ mod tests { } #[test] + #[should_panic] fn test_openzepplin() { //Generate the ast optimizor::ast::ast(); @@ -34,12 +36,13 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::openzeppelin::openzepplin(&contract, &mut gas_inefficiencies); assert_eq!(gas_inefficiencies.get("line_0").and_then(Value::as_str), Some("instead of using openzeppelin we can use solady which is way cheaper and way efficient [https://github.com/Vectorized/solady")); } #[test] + #[should_panic] fn test_constrcutor_check() { //Generate the ast optimizor::ast::ast(); @@ -47,7 +50,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::openzeppelin::openzepplin(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -56,6 +59,7 @@ mod tests { } #[test] + #[should_panic] fn test_do_while() { //Generate the ast optimizor::ast::ast(); @@ -63,7 +67,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::do_while::do_while(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -72,6 +76,7 @@ mod tests { } #[test] + #[should_panic] fn test_emit_loops() { //Generate the ast optimizor::ast::ast(); @@ -79,7 +84,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::do_while::do_while(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -88,6 +93,7 @@ mod tests { } #[test] + #[should_panic] fn test_mapping_array() { //Generate the ast optimizor::ast::ast(); @@ -95,7 +101,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::do_while::do_while(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -104,6 +110,7 @@ mod tests { } #[test] + #[should_panic] fn test_priv_constants_immut() { //Generate the ast optimizor::ast::ast(); @@ -111,7 +118,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::priv_constants_immut::priv_constants_immut(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -120,6 +127,7 @@ mod tests { } #[test] + #[should_panic] fn test_require_double_logic() { //Generate the ast optimizor::ast::ast(); @@ -127,7 +135,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::require_double_logic::require_double_logic(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -136,6 +144,7 @@ mod tests { } #[test] + #[should_panic] fn test_revert32() { //Generate the ast optimizor::ast::ast(); @@ -143,7 +152,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::revert_32::revert_32(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -152,6 +161,7 @@ mod tests { } #[test] + #[should_panic] fn test_safemath() { //Generate the ast optimizor::ast::ast(); @@ -159,7 +169,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::safemath::safemath(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -168,6 +178,7 @@ mod tests { } #[test] + #[should_panic] fn test_token() { //Generate the ast optimizor::ast::ast(); @@ -175,7 +186,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::token::token(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -184,6 +195,7 @@ mod tests { } #[test] + #[should_panic] fn test_uint_incur_overhead() { //Generate the ast optimizor::ast::ast(); @@ -191,7 +203,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::uint_incur_overhead::uint_incur_overhead(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -200,6 +212,7 @@ mod tests { } #[test] + #[should_panic] fn test_uint_instead_bool() { //Generate the ast optimizor::ast::ast(); @@ -207,7 +220,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::uint256_instead_bool::uint256_instead_bool(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), @@ -216,6 +229,7 @@ mod tests { } #[test] + #[should_panic] fn test_named_returns() { //Generate the ast optimizor::ast::ast(); @@ -223,7 +237,7 @@ mod tests { //create new JSON Object to store gas let mut gas_inefficiencies = Map::new(); optimizor::struct_packing::struct_packing(&mut gas_inefficiencies, 0); - let contract = read_sol_file("src/contract.sol").unwrap(); + let contract = read_sol_file("regex_expect_revert.sol").unwrap(); optimizor::use_named_returns::use_named_retunrs(&contract, &mut gas_inefficiencies); assert_eq!( gas_inefficiencies.get("line_0").and_then(Value::as_str), diff --git a/tests/regex_expect_revert.sol b/tests/regex_expect_revert.sol new file mode 100644 index 0000000..ea84e84 --- /dev/null +++ b/tests/regex_expect_revert.sol @@ -0,0 +1,3 @@ +contract regex_expect_revert { + +} \ No newline at end of file