-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from malik672/write_zero
add write_to_zero
- Loading branch information
Showing
10 changed files
with
366 additions
and
1,226 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1 @@ | ||
{ | ||
"line_50": [ | ||
"Use Uint256 instead of bytes32 to store constant", | ||
"variables that are constant should have a visibility of private" | ||
], | ||
"line_57": [ | ||
"Use Uint256 instead of bytes32 to store constant", | ||
"variables that are constant should have a visibility of private" | ||
] | ||
} | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,10 @@ | ||
use std::fs; | ||
use tokio; | ||
mod config; | ||
mod loader; | ||
mod optimizor; | ||
use ethers_solc::{Project}; | ||
|
||
|
||
#[tokio::main] | ||
async fn main() { | ||
// create a project from a configuration file | ||
let project = Project::builder().build().unwrap(); | ||
let output = project | ||
.compile_files(vec!["src/contract.sol"]) | ||
.unwrap(); | ||
let artifacts = output.output().sources.0; | ||
let mut s_ast = artifacts.values(); | ||
let mut binding = s_ast.next(); | ||
let mut binding = binding.iter_mut().next(); | ||
let s_asts = binding.as_mut().unwrap(); | ||
let ast = s_asts.get(0).unwrap().source_file.ast.clone().unwrap(); | ||
let ast_json = serde_json::to_string(&ast).unwrap(); | ||
let write = fs::write("src/optimizor/ast.json", ast_json); | ||
// println!("{:?}", ast); | ||
loader::loader().await; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
use std::fs; | ||
use ethers_solc; | ||
use ethers_solc::{Project}; | ||
|
||
fn ast() { | ||
|
||
pub fn ast() { | ||
// create a project from a configuration file | ||
let project = Project::builder().build().unwrap(); | ||
let output = project | ||
.compile_files(vec!["src/contract.sol"]) | ||
.unwrap(); | ||
let artifacts = output.output().sources.0; | ||
let mut s_ast = artifacts.values(); | ||
let mut binding = s_ast.next(); | ||
let mut binding = binding.iter_mut().next(); | ||
let s_asts = binding.as_mut().unwrap(); | ||
let ast = s_asts.get(0).unwrap().source_file.ast.clone().unwrap(); | ||
let ast_json = serde_json::to_string(&ast).unwrap(); | ||
let write = fs::write("src/optimizor/ast.json", ast_json).expect("failed to write ast"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod gas_tricks; | ||
pub mod ast; | ||
pub mod gas_tricks; | ||
pub mod write_zero_to_storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use regex::Regex; | ||
use serde_json::Value; | ||
use std::fs; | ||
|
||
pub fn write_zero_to_storage() { | ||
let ast_json = fs::read_to_string("src/optimizor/ast.json").expect("Failed to read"); | ||
let ast: Value = serde_json::from_str(&ast_json).expect("Failed to deserialize"); | ||
let mut _name = ""; | ||
let mut _prev: usize = 0; | ||
if let Some(nodes) = ast.get("nodes").and_then(Value::as_array) { | ||
for node in nodes { | ||
if let Some(node_type) = node.get("nodeType").and_then(Value::as_str) { | ||
if node_type == "ContractDefinition" { | ||
if let Some(contract_nodes) = node.get("nodes").and_then(Value::as_array) { | ||
for contract_node in contract_nodes { | ||
if let Some(func_node) = contract_node.as_object() { | ||
if func_node.get("nodeType").and_then(Value::as_str) | ||
== Some("FunctionDefinition") | ||
{ | ||
if let Some(statements) = func_node | ||
.get("body") | ||
.and_then(|b| b.get("statements")) | ||
.and_then(Value::as_array) | ||
{ | ||
for statement in statements { | ||
if let Some(expression) = statement.get("expression") { | ||
if expression | ||
.get("operator") | ||
.and_then(Value::as_str) | ||
== Some("=") | ||
{ | ||
if let Some(left_hand_side) = | ||
expression.get("leftHandSide") | ||
{ | ||
_name = left_hand_side | ||
.get("name") | ||
.unwrap() | ||
.as_str() | ||
.unwrap_or(""); | ||
} | ||
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 | ||
.get("isConstant") | ||
.unwrap_or(&Value::Bool(true)) | ||
.as_bool() | ||
.unwrap_or(true) | ||
{ | ||
println!( | ||
"Avoid zero to one storage writes where possible. Line: {:?}", | ||
get_line_number_zero(_name, _prev) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn get_line_number_zero(src: &str, mut _prev: usize) -> String { | ||
// Read the source file as a string | ||
let contract = fs::read_to_string("src/contract.sol").expect("Failed to read"); | ||
|
||
// Split the contract content into lines | ||
let lines: Vec<&str> = contract.lines().collect(); | ||
|
||
// Format the string with " = 0" at the end | ||
let strss = format!("{} = 0", src); | ||
|
||
// Compile the regex pattern | ||
let variable_declaration_regex = Regex::new(&strss).unwrap(); | ||
|
||
let mut _line_numbers = 0; | ||
for (mut line_number, line) in lines.iter().enumerate() { | ||
line_number = _prev; | ||
_line_numbers = line_number + 1; | ||
if let Some(_capture) = variable_declaration_regex.captures(line) {} | ||
_prev = _line_numbers; | ||
} | ||
_prev.to_string() | ||
} |