Skip to content

Commit

Permalink
Cache onchain builder result. (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloud10240 authored Sep 19, 2023
1 parent 487dd6e commit ec8542e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
26 changes: 19 additions & 7 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,36 @@ impl FileSystemCache {

impl Cache for FileSystemCache {
fn save(&self, key: &str, value: &str) -> Result<(), Box<dyn Error>> {
// write `value` to file `key`, create a new file if it doesn't exist
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(self.file_path.clone() + "/" + key)?;
let path = if key.len() < 5 {
format!("{}/{}", self.file_path, key)
} else {
format!("{}/{}/{}/{}", self.file_path, &key[0..2], &key[2..4], &key[4..])
};

let path_obj = Path::new(&path);
if let Some(parent) = path_obj.parent() {
fs::create_dir_all(parent)?;
}
let mut file = OpenOptions::new().write(true).create(true).open(path)?;
file.write_all(value.as_bytes())?;
Ok(())
}

fn load(&self, key: &str) -> Result<String, Box<dyn Error>> {
if !Path::exists(Path::new((self.file_path.clone() + "/" + key).as_str())) {
let path = if key.len() < 5 {
format!("{}/{}", self.file_path, key)
} else {
format!("{}/{}/{}/{}", self.file_path, &key[0..2], &key[2..4], &key[4..])
};

if !Path::new(&path).exists() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Key not found",
)));
}

let mut file = File::open(self.file_path.clone() + "/" + key)?;
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
Expand Down
23 changes: 21 additions & 2 deletions src/evm/blaz/builder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs;
use std::hash::{Hash, Hasher};
use std::process::id;
use std::rc::Rc;
use std::str::FromStr;
use std::thread::sleep;
use std::time::Duration;
use std::collections::hash_map::DefaultHasher;
use bytes::Bytes;
use itertools::Itertools;
use libafl::impl_serdeany;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::cache::{Cache, FileSystemCache};
use crate::evm::blaz::get_client;
use crate::evm::host::FuzzHost;
use crate::evm::input::{ConciseEVMInput, EVMInput};
Expand All @@ -23,17 +26,20 @@ use crate::generic_vm::vm_executor::GenericVM;
#[derive(Clone)]
pub struct BuildJob {
pub build_server: String,
pub replacements: HashMap<EVMAddress, Option<BuildJobResult>>
pub replacements: HashMap<EVMAddress, Option<BuildJobResult>>,
cache: FileSystemCache,
}

pub static mut BUILD_SERVER: &str = "https://solc-builder.fuzz.land/";
const NEEDS: &str = "runtimeBytecode,abi,sourcemap,sources";

impl BuildJob {
pub fn new(build_server: String, replacements: HashMap<EVMAddress, Option<BuildJobResult>>) -> Self {
let cache = FileSystemCache::new("./cache");
Self {
build_server,
replacements
replacements,
cache
}
}

Expand All @@ -58,6 +64,16 @@ impl BuildJob {
return replacement.clone();
}

let mut hasher = DefaultHasher::new();
let key = format!("onchain_{}_{}", chain.as_str(), addr.to_string().as_str());
key.hash(&mut hasher);
let hash = hasher.finish().to_string();
if let Ok(t) = self.cache.load(hash.as_str()) {
if let Ok(deserialized_result) = serde_json::from_str::<BuildJobResult>(&t) {
return Some(deserialized_result);
}
}

let job = self.async_submit_onchain_job(chain, addr);
if job.is_none() {
return None;
Expand All @@ -67,6 +83,9 @@ impl BuildJob {
if result.is_none() {
return None;
}
if let Some(res) = &result {
self.cache.save(hash.as_str(), &serde_json::to_string(res).unwrap()).unwrap();
}
return result;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/evm/onchain/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl OnChainConfig {

fn get(&self, url: String) -> Option<String> {
let mut hasher = DefaultHasher::new();
let key = format!("post_{}", url.as_str());
let key = format!("get_{}", url.as_str());
key.hash(&mut hasher);
let hash = hasher.finish().to_string();
if let Ok(t) = self.rpc_cache.load(hash.as_str()) {
Expand Down

0 comments on commit ec8542e

Please sign in to comment.