Skip to content

Commit

Permalink
Merge pull request #1 from buffalojoec/dev
Browse files Browse the repository at this point in the history
API Refactoring & More
  • Loading branch information
buffalojoec authored Jun 22, 2024
2 parents 3a579af + a1945f1 commit e861087
Show file tree
Hide file tree
Showing 13 changed files with 1,010 additions and 316 deletions.
6 changes: 3 additions & 3 deletions bencher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod result;

use {
mollusk::{Mollusk, ProgramResult},
mollusk::{result::ProgramResult, Mollusk},
result::{write_results, MolluskComputeUnitBenchResult},
solana_sdk::{account::AccountSharedData, instruction::Instruction, pubkey::Pubkey},
std::path::PathBuf,
Expand Down Expand Up @@ -63,14 +63,14 @@ impl MolluskComputeUnitBencher {
.mollusk
.process_instruction(&instruction, accounts.clone());

match result.result {
match result.program_result {
ProgramResult::Success => (),
_ => {
if self.must_pass {
panic!(
"Program execution failed, but `must_pass` was set. Error: \
{:?}",
result.result
result.program_result
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bencher/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
chrono::{DateTime, Utc},
mollusk::InstructionResult,
mollusk::result::InstructionResult,
num_format::{Locale, ToFormattedString},
std::path::Path,
};
Expand Down
2 changes: 1 addition & 1 deletion bencher/tests/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_markdown() {

let program_id = Pubkey::new_unique();

let instruction = Instruction::new_with_bytes(program_id, &[1], vec![]);
let instruction = Instruction::new_with_bytes(program_id, &[0], vec![]);
let accounts = vec![];

let mollusk = Mollusk::new(&program_id, "test_program");
Expand Down
1 change: 1 addition & 0 deletions harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ solana-bpf-loader-program = { workspace = true }
solana-program-runtime = { workspace = true }
solana-system-program = { workspace = true }
solana-sdk = { workspace = true }
solana-logger = "2.0.0"
83 changes: 83 additions & 0 deletions harness/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! Module for loading files from local filesystem.
//!
//! When compiling a Solana program wih `cargo build-sbf`, the environment
//! variable `SBF_OUT_DIR` defaults to `target/deploy` and the compiled program
//! ELF file is written to `target/deploy/program_name.so`.
//!
//! Note: The legacy/deprecated `cargo build-bpf` command used `BPF_OUT_DIR`
//! instead of `SBF_OUT_DIR`.
//!
//! As a result, the default search paths for ELF files are:
//!
//! * `tests/fixtures`
//! * `BPF_OUT_DIR`
//! * `SBF_OUT_DIR`
//! * The current working directory
//!
//! Since these functions are intended for the local filesystem and for testing
//! purposes, most of them will panic if the file is not found or if there is an
//! error reading the file.

use std::{
fs::File,
io::Read,
path::{Path, PathBuf},
};

fn default_shared_object_dirs() -> Vec<PathBuf> {
let mut search_path = vec![PathBuf::from("tests/fixtures")];

if let Ok(bpf_out_dir) = std::env::var("BPF_OUT_DIR") {
search_path.push(PathBuf::from(bpf_out_dir));
}

if let Ok(bpf_out_dir) = std::env::var("SBF_OUT_DIR") {
search_path.push(PathBuf::from(bpf_out_dir));
}

if let Ok(dir) = std::env::current_dir() {
search_path.push(dir);
}

search_path
}

fn find_file(filename: &str) -> Option<PathBuf> {
for dir in default_shared_object_dirs() {
let candidate = dir.join(filename);
if candidate.exists() {
return Some(candidate);
}
}
None
}

/// Read the contents of a file into a `Vec<u8>`.
pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> {
let path = path.as_ref();
let mut file = File::open(path)
.unwrap_or_else(|err| panic!("Failed to open \"{}\": {}", path.display(), err));

let mut file_data = Vec::new();
file.read_to_end(&mut file_data)
.unwrap_or_else(|err| panic!("Failed to read \"{}\": {}", path.display(), err));
file_data
}

/// Load a program ELF file from the local filesystem by program name.
///
/// The program ELF file is expected to be located in one of the default search
/// paths:
///
/// * `tests/fixtures`
/// * `BPF_OUT_DIR`
/// * `SBF_OUT_DIR`
/// * The current working directory
///
/// The name of the program ELF file is expected to be `{program_name}.so`.
pub fn load_program_elf(program_name: &str) -> Vec<u8> {
let file_name = format!("{program_name}.so");
let program_file = find_file(&file_name)
.unwrap_or_else(|| panic!("Program file data not available for \"{}\"", file_name,));
read_file(program_file)
}
Loading

0 comments on commit e861087

Please sign in to comment.