Skip to content

Commit

Permalink
⚡️ Instead of creating our own IDL try to use the anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Aug 29, 2024
1 parent 444cc4d commit 03b6960
Show file tree
Hide file tree
Showing 110 changed files with 5,527 additions and 13,582 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ resolver = "1"
anchor-client = ">=0.29.0"
anchor-syn = ">=0.29.0"
anchor-lang = ">=0.29.0"
anchor-lang-idl-spec = "0.1.0"


# SOLANA
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod test;
pub use test::test;

mod init;
pub use init::{init, SnapshotsType, TestsType};
pub use init::{init, TestsType};

mod clean;
pub use clean::clean;
2 changes: 1 addition & 1 deletion crates/cli/src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ pub async fn build(root: Option<String>) {
}
}
};
let mut generator: TestGenerator = TestGenerator::new_with_root(root, false);
let mut generator: TestGenerator = TestGenerator::new_with_root(root);
generator.build().await?;
}
16 changes: 3 additions & 13 deletions crates/cli/src/command/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use trident_client::___private::{Commander, TestGenerator};

use crate::_discover;

use super::SnapshotsType;

pub const TRIDENT_TOML: &str = "Trident.toml";

#[derive(Subcommand)]
Expand All @@ -29,10 +27,7 @@ pub enum FuzzCommand {
crash_file_path: String,
},
/// Add new fuzz test. Explicit fuzz test name is not yet supported. Implicit name is fuzz_ID, where ID is automatically derived.
Add {
#[clap(default_value = "file")]
snapshots_type: SnapshotsType,
},
Add,
}

#[throws]
Expand Down Expand Up @@ -69,13 +64,8 @@ pub async fn fuzz(root: Option<String>, subcmd: FuzzCommand) {
commander.run_fuzzer_debug(target, crash_file_path).await?;
}

FuzzCommand::Add { snapshots_type } => {
// generate generator with root so that we do not need to again
// look for root within the generator
let mut generator = match snapshots_type {
SnapshotsType::Macro => TestGenerator::new_with_root(root, false),
SnapshotsType::File => TestGenerator::new_with_root(root, true),
};
FuzzCommand::Add => {
let mut generator = TestGenerator::new_with_root(root);
generator.add_fuzz_test().await?;
}
};
Expand Down
12 changes: 2 additions & 10 deletions crates/cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@ pub enum TestsType {
Fuzz,
Poc,
}
#[derive(ValueEnum, Clone)]
pub enum SnapshotsType {
Macro,
File,
}

#[throws]
pub async fn init(tests_type: TestsType, snapshots_type: SnapshotsType) {
pub async fn init(tests_type: TestsType) {
// look for Anchor.toml
let root = if let Some(r) = _discover(ANCHOR_TOML)? {
r
} else {
bail!("It does not seem that Anchor is initialized because the Anchor.toml file was not found in any parent directory!");
};

let mut generator: TestGenerator = match snapshots_type {
SnapshotsType::Macro => TestGenerator::new_with_root(root, false),
SnapshotsType::File => TestGenerator::new_with_root(root, true),
};
let mut generator: TestGenerator = TestGenerator::new_with_root(root);

match tests_type {
TestsType::Poc => {
Expand Down
10 changes: 2 additions & 8 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Error;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use command::{SnapshotsType, TestsType};
use command::TestsType;
use fehler::throws;

// subcommand functions to call and nested subcommands
Expand Down Expand Up @@ -50,9 +50,6 @@ enum Command {
/// Specifies the types of tests for which the frameworks should be initialized.
#[clap(default_value = "fuzz")]
tests_type: TestsType,
/// Specifies type of Accounts Snapshots, i.e used derive macro or generated file
#[clap(default_value = "file")]
snapshots_type: SnapshotsType,
},
/// Removes target contents except for KeyPair and removes hfuzz_target folder
Clean,
Expand All @@ -67,10 +64,7 @@ pub async fn start() {
Command::KeyPair { subcmd } => command::keypair(subcmd)?,
Command::Test { root } => command::test(root).await?,
Command::Fuzz { root, subcmd } => command::fuzz(root, subcmd).await?,
Command::Init {
tests_type,
snapshots_type,
} => command::init(tests_type, snapshots_type).await?,
Command::Init { tests_type } => command::init(tests_type).await?,
Command::Clean => command::clean().await?,
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ trident-fuzz = { path = "../fuzz", version = "0.1.0" }
anchor-lang = { workspace = true, features = ["init-if-needed"] }
anchor-syn = { workspace = true }
anchor-client = { workspace = true, features = ["async"] }
anchor-lang-idl-spec = { workspace = true }

# SOLANA
solana-sdk = { workspace = true }
Expand Down Expand Up @@ -71,3 +72,4 @@ quinn-proto = { version = "0.10.6", features = ["arbitrary"] }
pathdiff = "0.2.1"
indicatif = "0.17.8"
regex = "1.10.3"
convert_case = "0.6.0"
60 changes: 60 additions & 0 deletions crates/client/src/anchor_idl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use anchor_lang_idl_spec::Idl;
use cargo_metadata::Package;
use convert_case::{Case, Casing};

use std::error::Error;
use std::fs::{self, File};
use std::io::Read;
use std::path::PathBuf;

pub fn load_idls(
dir_path: PathBuf,
program_packages: &[Package],
) -> Result<Vec<Idl>, Box<dyn Error>> {
let mut idls = Vec::new();

let package_names: Vec<String> = program_packages
.iter()
.map(|package| {
let name = &package.name;
name.to_case(Case::Snake)
})
.collect();

// Read the directory and iterate over each entry
for entry in fs::read_dir(dir_path)? {
let entry = entry?;
let path = entry.path();
let idl_name = entry.file_name();
let idl_name_str = idl_name.to_str().unwrap();

// Only process .json files
if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some("json") {
// Remove the .json extension to get the package name
let package_name = idl_name_str.trim_end_matches(".json");

// Check if the package name is in the list of known packages
if package_names.iter().any(|name| name == package_name) {
// Open the file in read-only mode
let mut file = File::open(&path)?;

// Read the file contents into a string
let mut json_content = String::new();
file.read_to_string(&mut json_content)?;

// Parse the string of data into an Idl struct
match serde_json::from_str::<Idl>(&json_content) {
Ok(parsed_idl) => {
idls.push(parsed_idl);
}
Err(e) => {
eprintln!("Failed to parse {}: {}", path.display(), e);
// Continue to the next file on failure
}
}
}
}
}

Ok(idls)
}
Loading

0 comments on commit 03b6960

Please sign in to comment.