-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(snapshot): code split (#198)
* refactor: extract logger functions * refactor: extract shortned address to function * chore: merge with origin branch * chore: remove duplicated function declaration * chore: move get_shortnet_target and set_logger_env to common * refactor: extract function selector resolvers to resolve.rs * refactor: move get_contract_bytecode to a new bytecode module * refactor: move get_selectors to selectors module * refactor: make bytecode disambled external from get_resolved_selectors * refactor: remove logger from params of resolve_signature * refactor: rework snapshot mod variable and args declaration order * refactor: remove logger from params list and initialize internally * fix: remove extrar logger param from get_contract_bytecode call * chore: change rpc from llama to ankr * fix(tests): remove unnecessary `0x` prefix constraint * refactor: rename resolve_custom_event_signatures to resolve_event_signatures * refactor: rename get_contract_bytecode to get_bytecode_from_target * fix: update comments from get_bytecode_from_target to be module agnostic * refactor: switch from logger::debug_max to debug_max * fix: remove out of context log * refactor: remove get_logger_and_trace function * style: code format * refactor: change target param from string reference to string slice --------- Co-authored-by: Jon-Becker <jonathan@jbecker.dev>
- Loading branch information
1 parent
cfbf6b1
commit 23addf5
Showing
9 changed files
with
572 additions
and
336 deletions.
There are no files selected for viewing
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,87 @@ | ||
use super::rpc::get_code; | ||
use crate::{ | ||
constants::{ADDRESS_REGEX, BYTECODE_REGEX}, | ||
debug_max, | ||
utils::io::logging::Logger, | ||
}; | ||
use std::fs; | ||
|
||
pub async fn get_bytecode_from_target( | ||
target: &str, | ||
rpc_url: &str, | ||
) -> Result<String, Box<dyn std::error::Error>> { | ||
let (logger, _) = Logger::new(""); | ||
|
||
if ADDRESS_REGEX.is_match(target)? { | ||
// Target is a contract address, so we need to fetch the bytecode from the RPC provider. | ||
get_code(target, rpc_url).await | ||
} else if BYTECODE_REGEX.is_match(target)? { | ||
debug_max!("using provided bytecode for snapshotting."); | ||
|
||
// Target is already a bytecode, so we just need to remove 0x from the begining | ||
Ok(target.replacen("0x", "", 1)) | ||
} else { | ||
debug_max!("using provided file for snapshotting."); | ||
|
||
// Target is a file path, so we need to read the bytecode from the file. | ||
match fs::read_to_string(target) { | ||
Ok(contents) => { | ||
let _contents = contents.replace('\n', ""); | ||
if BYTECODE_REGEX.is_match(&_contents)? && _contents.len() % 2 == 0 { | ||
Ok(_contents.replacen("0x", "", 1)) | ||
} else { | ||
logger.error(&format!("file '{}' doesn't contain valid bytecode.", &target)); | ||
std::process::exit(1) | ||
} | ||
} | ||
Err(_) => { | ||
logger.error(&format!("failed to open file '{}' .", &target)); | ||
std::process::exit(1) | ||
} | ||
} | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
|
||
#[tokio::test] | ||
async fn test_get_bytecode_when_target_is_address() { | ||
let bytecode = get_bytecode_from_target( | ||
"0x9f00c43700bc0000Ff91bE00841F8e04c0495000", | ||
"https://rpc.ankr.com/eth", | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_bytecode_when_target_is_bytecode() { | ||
let bytecode = get_bytecode_from_target( | ||
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", | ||
"https://rpc.ankr.com/eth", | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_bytecode_when_target_is_file_path() { | ||
let file_path = "./mock-file.txt"; | ||
let mock_bytecode = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"; | ||
|
||
fs::write(file_path, mock_bytecode).unwrap(); | ||
|
||
let bytecode = | ||
get_bytecode_from_target(file_path, "https://rpc.ankr.com/eth").await.unwrap(); | ||
|
||
assert!(BYTECODE_REGEX.is_match(&bytecode).unwrap()); | ||
|
||
fs::remove_file(file_path).unwrap(); | ||
} | ||
} |
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,3 +1,4 @@ | ||
pub mod bytecode; | ||
pub mod compiler; | ||
pub mod evm; | ||
pub mod lexers; | ||
|
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
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
Oops, something went wrong.