Skip to content

Commit

Permalink
Merge pull request #5 from propeller-heads/tnl/ENG-3748-get-contract-…
Browse files Browse the repository at this point in the history
…bytecode

feat: implement get_contract_bytecode util function
  • Loading branch information
tamaralipows authored Oct 17, 2024
2 parents 35cc294 + 50fd8e7 commit 55adcaf
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 10 deletions.
113 changes: 103 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "2544793
foundry-evm = { git = "https://github.com/foundry-rs/foundry", rev = "2544793" }
revm-inspectors = { version = "0.5", features = ["serde"] }

mini-moka = "0.10"
tempfile = "3.13.0"

[dev-dependencies]
mockito = "1.1.1"
warp = "0.3.5"
Expand Down
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod state;
pub mod tycho;
pub mod uniswap_v2;
pub mod uniswap_v3;
mod vm;

/// A trait for converting types to and from `Bytes`.
///
Expand Down
1 change: 1 addition & 0 deletions src/protocol/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod utils;
58 changes: 58 additions & 0 deletions src/protocol/vm/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// TODO: remove skip for clippy dead_code check
#![allow(dead_code)]

use mini_moka::sync::Cache;
use std::{
fs::File,
io::Read,
path::Path,
sync::{Arc, LazyLock},
};

static BYTECODE_CACHE: LazyLock<Cache<Arc<String>, Vec<u8>>> = LazyLock::new(|| Cache::new(1_000));

pub fn get_contract_bytecode(path: &str) -> std::io::Result<Vec<u8>> {
if let Some(bytecode) = BYTECODE_CACHE.get(&Arc::new(path.to_string())) {
return Ok(bytecode.clone());
}

let mut file = File::open(Path::new(path))?;
let mut code = Vec::new();

file.read_to_end(&mut code)?;
BYTECODE_CACHE.insert(Arc::new(path.to_string()), code.clone());

Ok(code)
}

#[cfg(test)]
mod tests {
use super::*;
use std::{fs::remove_file, io::Write};
use tempfile::NamedTempFile;

#[test]
fn test_get_contract_bytecode() {
// Create a temporary file with some test data
let mut temp_file = NamedTempFile::new().unwrap();
let test_data = b"Test contract bytecode";
temp_file.write_all(test_data).unwrap();
let temp_path = temp_file.path().to_str().unwrap();

// First call to get_contract_bytecode
let result1 = get_contract_bytecode(temp_path).unwrap();
assert_eq!(result1, test_data);

// Second call to get_contract_bytecode (should use cached data)
// Verify that the cache was used (file is not read twice)
remove_file(&temp_file).unwrap(); // This removes the temporary file
let result2 = get_contract_bytecode(temp_path).unwrap();
assert_eq!(result2, test_data);
}

#[test]
fn test_get_contract_bytecode_error() {
let result = get_contract_bytecode("non_existent_file.txt");
assert!(result.is_err());
}
}

0 comments on commit 55adcaf

Please sign in to comment.