From 6ec25ebc0977ae3c9c64476d99380f208abca6f5 Mon Sep 17 00:00:00 2001 From: Stan Drozd Date: Thu, 14 Sep 2023 18:19:54 +0200 Subject: [PATCH] move testing-specific cargo-build-bpf call to pyth_simulator.rs --- .github/workflows/check-fomatting.yml | 7 +--- Cargo.lock | 1 + program/rust/Cargo.toml | 1 + program/rust/build.rs | 31 --------------- program/rust/src/tests/pyth_simulator.rs | 49 +++++++++++++++++++----- 5 files changed, 43 insertions(+), 46 deletions(-) diff --git a/.github/workflows/check-fomatting.yml b/.github/workflows/check-fomatting.yml index 3213a45ca..2a2cdeaae 100644 --- a/.github/workflows/check-fomatting.yml +++ b/.github/workflows/check-fomatting.yml @@ -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 @@ -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/action@v2.0.3 diff --git a/Cargo.lock b/Cargo.lock index d52198b72..fdea94932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2514,6 +2514,7 @@ dependencies = [ "byteorder", "csv", "hex 0.3.2", + "lazy_static", "num-derive", "num-traits", "pythnet-sdk", diff --git a/program/rust/Cargo.toml b/program/rust/Cargo.toml index 7370d687c..bf10bd828 100644 --- a/program/rust/Cargo.toml +++ b/program/rust/Cargo.toml @@ -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! # diff --git a/program/rust/build.rs b/program/rust/build.rs index 5d6a6a460..5c7f7f207 100644 --- a/program/rust/build.rs +++ b/program/rust/build.rs @@ -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 diff --git a/program/rust/src/tests/pyth_simulator.rs b/program/rust/src/tests/pyth_simulator.rs index eee2dc4ff..a33328644 100644 --- a/program/rust/src/tests/pyth_simulator.rs +++ b/program/rust/src/tests/pyth_simulator.rs @@ -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. @@ -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(); @@ -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); @@ -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(