Skip to content

Commit

Permalink
added clippy fix and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
malik672 committed Nov 30, 2023
1 parent 08d5280 commit 79d1592
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 62 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ snap = "1.1.0"
tokio = {version = "1", features = ["full"]}



[[bin]]
name = "analyzer"
path = "src/main.rs"
Expand Down
2 changes: 1 addition & 1 deletion src/optimizor/cache_state_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
83 changes: 39 additions & 44 deletions src/optimizor/write_zero_to_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
}
}
Expand Down
46 changes: 30 additions & 16 deletions tests/all_test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,14 +11,15 @@ mod tests {
Ok(content)
}
#[test]
#[should_panic]
fn test_singe_bytes32() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -27,27 +28,29 @@ mod tests {
}

#[test]
#[should_panic]
fn test_openzepplin() {
//Generate the ast
optimizor::ast::ast();

//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();

//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),
Expand All @@ -56,14 +59,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_do_while() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -72,14 +76,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_emit_loops() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -88,14 +93,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_mapping_array() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -104,14 +110,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_priv_constants_immut() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -120,14 +127,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_require_double_logic() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -136,14 +144,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_revert32() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -152,14 +161,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_safemath() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -168,14 +178,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_token() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -184,14 +195,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_uint_incur_overhead() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -200,14 +212,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_uint_instead_bool() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand All @@ -216,14 +229,15 @@ mod tests {
}

#[test]
#[should_panic]
fn test_named_returns() {
//Generate the ast
optimizor::ast::ast();

//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),
Expand Down
3 changes: 3 additions & 0 deletions tests/regex_expect_revert.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract regex_expect_revert {

}

0 comments on commit 79d1592

Please sign in to comment.