Skip to content

Commit

Permalink
(wip) CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-valerio committed Oct 22, 2024
1 parent bc63529 commit f71c8ff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ contract-metadata = { version = "4.1.1" }
colored = { version = "2.1.0" }
thiserror = { version = "1.0.63" }
time = { version = "0.3.36" }
pretty_assertions = "1.4.1"


[lints.rust]
Expand Down
1 change: 0 additions & 1 deletion src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ mod tests {
default_gas_limit: Some(Weight::from_parts(100_000_000_000, 0)),
storage_deposit_limit: Some("1000000000".into()),
instantiate_initial_value: Some("500".into()),
constructor_payload: Some("0x1234".into()),
verbose: true,
instrumented_contract_path: Some(InstrumentedPath::from("/tmp/instrumented")),
fuzz_output: Some(PathBuf::from("/tmp/fuzz_output")),
Expand Down
3 changes: 2 additions & 1 deletion src/instrumenter/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! phink_log {

impl ContractVisitor for Instrumenter {
fn input_directory(&self) -> PathBuf {
todo!()
self.z_config.contract_path().unwrap()
}

fn output_directory(&self) -> PathBuf {
Expand Down Expand Up @@ -213,6 +213,7 @@ mod tests {
self,
File,
},
path::Path,
};
use tempfile::{
tempdir,
Expand Down
24 changes: 17 additions & 7 deletions src/instrumenter/seeder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::instrumenter::traits::visitor::ContractVisitor;
use anyhow::bail;
use quote::quote;
use std::path::PathBuf;
use std::path::{
Path,
PathBuf,
};
use syn::{
parse_quote,
visit_mut::{
Expand All @@ -17,19 +20,22 @@ use syn::{
Pat,
Stmt,
};
use tempfile::tempdir;

#[derive(Debug, Clone)]
pub struct SeedExtractInjector {
contract_path: PathBuf,
compiled_path: Option<PathBuf>,
}

impl SeedExtractInjector {
pub fn new(contract_path: &PathBuf) -> anyhow::Result<Self> {
pub fn new(contract_path: &Path, compiled_path: Option<PathBuf>) -> anyhow::Result<Self> {
if !contract_path.exists() {
bail!("Couldn't find the contract at {}", contract_path.display())
}
Ok(Self {
contract_path: contract_path.to_path_buf(),
compiled_path,
})
}
pub fn extract_seeds(&self) -> anyhow::Result<()> {
Expand All @@ -39,15 +45,18 @@ impl SeedExtractInjector {

impl ContractVisitor for SeedExtractInjector {
fn input_directory(&self) -> PathBuf {
todo!()
self.contract_path.to_path_buf()
}

fn output_directory(&self) -> PathBuf {
todo!()
match &self.compiled_path {
None => tempdir().unwrap().into_path(),
Some(contract) => contract.into(),
}
}

fn verbose(&self) -> bool {
todo!()
true
}
}

Expand Down Expand Up @@ -152,9 +161,9 @@ impl VisitMut for SeedExtractInjector {

#[cfg(test)]
mod tests {

use crate::instrumenter::seeder::SeedExtractInjector;
use quote::quote;
use std::path::PathBuf;
use syn::{
parse_str,
visit_mut::VisitMut,
Expand Down Expand Up @@ -246,7 +255,8 @@ mod tests {
}"#;

let mut syntax_tree: File = parse_str(input_code).expect("Failed to parse code");
let mut seed_injector = SeedExtractInjector::new().unwrap();
let mut seed_injector =
SeedExtractInjector::new(&PathBuf::from("sample/dummy"), None).unwrap();
seed_injector.visit_file_mut(&mut syntax_tree);

let generated_code = quote!(#syntax_tree).to_string();
Expand Down
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ enum Commands {
/// Run the tests of the ink! smart-contract to execute the
/// messages and extracts valid seeds fromit. For instance, if a test call three messages,
/// those three messages will be extracted to be used as seeds inside the corpus directory
GenerateSeed(Contract),
GenerateSeed {
/// Path where the contract is located. It must be the root directory of
/// the contract
contract: PathBuf,
/// Path where the temporary contract will be compiled to. Optionnal. In `tmp` if not
/// defined.
compiled_directory: Option<PathBuf>,
},
/// Instrument the ink! contract, and compile it with Phink features
Instrument(Contract),
/// Run all the seeds
Expand All @@ -87,7 +94,7 @@ enum Commands {
},
}

#[derive(clap::Args, Debug)]
#[derive(clap::Args, Debug, Clone)]
struct Contract {
/// Path where the contract is located. It must be the root directory of
/// the contract
Expand Down Expand Up @@ -174,11 +181,14 @@ fn handle_cli() -> anyhow::Result<()> {
.context("Couldn't generate handle the ZiggyConfig")?,
)
}
Commands::GenerateSeed(contract_path) => {
let seeder = SeedExtractInjector::new(&contract_path.contract_path)?;
Commands::GenerateSeed {
contract,
compiled_directory,
} => {
let seeder = SeedExtractInjector::new(&contract, compiled_directory)?;
seeder
.extract_seeds()
.context(format!("Couldn't extract the seed from {contract_path:?}"))?;
.context(format!("Couldn't extract the seed from {contract:?}"))?;
Ok(())
}
}
Expand Down

0 comments on commit f71c8ff

Please sign in to comment.