Skip to content

Commit

Permalink
move testing-specific cargo-build-bpf call to pyth_simulator.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
drozdziak1 committed Sep 14, 2023
1 parent 57611f9 commit 6ec25eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/check-fomatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
pre-commit:
runs-on: solana-labs/solana:v1.14.7 # cargo check will attempt the BPF binary build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
Expand All @@ -16,9 +16,4 @@ jobs:
profile: minimal
toolchain: nightly-2023-03-01
components: rustfmt, clippy
- name: Install Solana
run: |
set -eux
sh -c "$(curl -sSfL https://release.solana.com/v1.16.13/install)"
- uses: pre-commit/[email protected]
1 change: 1 addition & 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 program/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", rev="60
serde_json = "1.0"
test-generator = "0.3.1"
csv = "1.1"
lazy_static = "1.4.0"

# IMPORTANT: Feature names on this crate must not use hyphens!
#
Expand Down
31 changes: 0 additions & 31 deletions program/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,6 @@ use {
fn main() {
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();

// The tests depend on the BPF build (because we load the BPF program into the solana simulator).
// Always build the bpf binary before doing anything else.
if target_arch != "bpf" {
// cargo-build-bpf does not get information about features
// from this script. We need to fish them out and pass them
// manually.
let mut features = vec![];
for (env, _val) in std::env::vars() {
if env.starts_with("CARGO_FEATURE_") {
features.push(env.trim().replace("CARGO_FEATURE_", "").to_lowercase());
}
}

let mut cmd = Command::new("cargo");
cmd.arg("build-bpf");

if !features.is_empty() {
cmd.arg("--features");
cmd.args(features);
}

let status = cmd.status().unwrap();

if !status.success() {
panic!(
"cargo-build-bpf did not exit with 0 (code {:?})",
status.code()
);
}
}

// OUT_DIR is the path cargo provides to a build directory under `target/` specifically for
// isolated build artifacts. We use this to build the C program and then link against the
// resulting static library. This allows building the program when used as a dependency of
Expand Down
49 changes: 40 additions & 9 deletions program/rust/src/tests/pyth_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,48 @@ use {
fs::File,
iter::once,
mem::size_of,
path::Path,
path::PathBuf,
},
};

lazy_static::lazy_static! {
// Build the oracle binary and make it available to the
// simulator. lazy_static makes this happen only once per test
// run.
static ref ORACLE_PROGRAM_BINARY_PATH: PathBuf = {

// Detect features and pass them onto cargo-build-bpf
let features: Vec<&str> = vec![
#[cfg(feature = "pythnet")]
"pythnet",

#[cfg(feature = "price_v2_resize")]
"price_v2_resize",

];

let mut cmd = std::process::Command::new("cargo");
cmd.arg("build-bpf");

if !features.is_empty() {
cmd.arg("--features");
cmd.args(features);
}

let status = cmd.status().unwrap();

if !status.success() {
panic!(
"cargo-build-bpf did not exit with 0 (code {:?})",
status.code()
);
}

let target_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../../target");

PathBuf::from(target_dir).join("deploy/pyth_oracle.so")
};
}

/// Simulator for the state of the pyth program on Solana. You can run solana transactions against
/// this struct to test how pyth instructions execute in the Solana runtime.
Expand Down Expand Up @@ -104,12 +142,7 @@ struct ProductMetadata {
impl PythSimulator {
/// Deploys the oracle program as upgradable
pub async fn new() -> PythSimulator {
let mut bpf_data = read_file(
std::env::current_dir()
.unwrap()
.join(Path::new("../../target/deploy/pyth_oracle.so")),
);

let mut bpf_data = read_file(&*ORACLE_PROGRAM_BINARY_PATH);

let mut program_test = ProgramTest::default();
let program_key = Pubkey::new_unique();
Expand Down Expand Up @@ -153,7 +186,6 @@ impl PythSimulator {
program_test.add_account(program_key, program_account);
program_test.add_account(programdata_key, programdata_account);


// Start validator
let context = program_test.start_with_context().await;
let genesis_keypair = copy_keypair(&context.payer);
Expand All @@ -176,7 +208,6 @@ impl PythSimulator {
result
}


/// Process a transaction containing `instructions` signed by `signers`.
/// `payer` is used to pay for and sign the transaction.
async fn process_ixs(
Expand Down

0 comments on commit 6ec25eb

Please sign in to comment.