forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Selector generator tool (matter-labs#2844)
## What ❔ * A small tool to generate the selector hashes based on the ABI from json files ## Why ❔ * The output json can be useful for humans to better understand some of the errors (and calldata) * It can also be read by our tools, to make the debugging easier. In the future, we could call this tool regularly on each contracts version change, but for now it can stay as manual.
- Loading branch information
Showing
7 changed files
with
670 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "selector_generator" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
authors.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json.workspace = true | ||
sha3.workspace = true | ||
glob.workspace = true | ||
clap = { workspace = true, features = ["derive"] } |
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,13 @@ | ||
# Generates the list of solidity selectors | ||
|
||
This tool generates a mapping from solidity selectors to function names. | ||
|
||
The output json file can be used by multiple tools to improve debugging and readability. | ||
|
||
By default, it appends the newly found selectors into the list. | ||
|
||
To run, first make sure that you have your contracts compiled and then run: | ||
|
||
``` | ||
cargo run ../../../contracts ../../../etc/selector-generator-data/selectors.json | ||
``` |
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,105 @@ | ||
use std::{ | ||
collections::HashMap, | ||
fs::{File, OpenOptions}, | ||
io::{self}, | ||
}; | ||
|
||
use clap::Parser; | ||
use glob::glob; | ||
use serde::{Deserialize, Serialize}; | ||
use sha3::{Digest, Keccak256}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct ABIEntry { | ||
#[serde(rename = "type")] | ||
entry_type: String, | ||
name: Option<String>, | ||
inputs: Option<Vec<ABIInput>>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct ABIInput { | ||
#[serde(rename = "type")] | ||
input_type: String, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Cli { | ||
contracts_dir: String, | ||
output_file: String, | ||
} | ||
|
||
/// Computes solidity selector for a given method and arguments. | ||
fn compute_selector(name: &str, inputs: &[ABIInput]) -> String { | ||
let signature = format!( | ||
"{}({})", | ||
name, | ||
inputs | ||
.iter() | ||
.map(|i| i.input_type.clone()) | ||
.collect::<Vec<_>>() | ||
.join(",") | ||
); | ||
let mut hasher = Keccak256::new(); | ||
hasher.update(signature); | ||
format!("{:x}", hasher.finalize())[..8].to_string() | ||
} | ||
|
||
/// Analyses all the JSON files, looking for 'abi' entries, and then computing the selectors for them. | ||
fn process_files(directory: &str, output_file: &str) -> io::Result<()> { | ||
let mut selectors: HashMap<String, String> = match File::open(output_file) { | ||
Ok(file) => serde_json::from_reader(file).unwrap_or_default(), | ||
Err(_) => HashMap::new(), | ||
}; | ||
let selectors_before = selectors.len(); | ||
let mut analyzed_files = 0; | ||
|
||
for entry in glob(&format!("{}/**/*.json", directory)).expect("Failed to read glob pattern") { | ||
match entry { | ||
Ok(path) => { | ||
let file_path = path.clone(); | ||
let file = File::open(path)?; | ||
let json: Result<serde_json::Value, _> = serde_json::from_reader(file); | ||
|
||
if let Ok(json) = json { | ||
if let Some(abi) = json.get("abi").and_then(|v| v.as_array()) { | ||
analyzed_files += 1; | ||
for item in abi { | ||
let entry: ABIEntry = serde_json::from_value(item.clone()).unwrap(); | ||
if entry.entry_type == "function" { | ||
if let (Some(name), Some(inputs)) = (entry.name, entry.inputs) { | ||
let selector = compute_selector(&name, &inputs); | ||
selectors.entry(selector).or_insert(name); | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
eprintln!("Error parsing file: {:?} - ignoring.", file_path) | ||
} | ||
} | ||
Err(e) => eprintln!("Error reading file: {:?}", e), | ||
} | ||
} | ||
println!( | ||
"Analyzed {} files. Added {} selectors (before: {} after: {})", | ||
analyzed_files, | ||
selectors.len() - selectors_before, | ||
selectors_before, | ||
selectors.len() | ||
); | ||
|
||
let file = OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.open(output_file)?; | ||
serde_json::to_writer_pretty(file, &selectors)?; | ||
Ok(()) | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
let args = Cli::parse(); | ||
process_files(&args.contracts_dir, &args.output_file) | ||
} |
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,3 @@ | ||
# List of selectors from our contracts | ||
|
||
To regenerate the list, please use the selector_generator tool from core/bin directory. |
Oops, something went wrong.