From ab907598cca411133bcac4091a318fb742dc011c Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Wed, 4 Dec 2024 16:13:39 +0000 Subject: [PATCH] feat: add light-program-test crate and integrate into existing modules (#1372) --- Cargo.lock | 26 +++++++++++++++++++ Cargo.toml | 5 ++-- client/src/indexer/mod.rs | 2 -- client/src/rpc/mod.rs | 1 - .../name-service-without-macros/Cargo.toml | 1 + .../name-service-without-macros/tests/test.rs | 4 +-- .../programs/name-service/Cargo.toml | 1 + .../programs/name-service/tests/test.rs | 4 +-- light-program-test/Cargo.toml | 25 ++++++++++++++++++ light-program-test/src/lib.rs | 2 ++ .../src}/test_indexer.rs | 17 +++++------- .../src}/test_rpc.rs | 5 ++-- .../programs/sdk-test/Cargo.toml | 1 + .../programs/sdk-test/tests/test.rs | 4 +-- test-utils/Cargo.toml | 1 + test-utils/src/test_env.rs | 6 ++--- 16 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 light-program-test/Cargo.toml create mode 100644 light-program-test/src/lib.rs rename {client/src/indexer => light-program-test/src}/test_indexer.rs (99%) rename {client/src/rpc => light-program-test/src}/test_rpc.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 9077d2527f..a7f389d6bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3667,6 +3667,28 @@ dependencies = [ "thiserror", ] +[[package]] +name = "light-program-test" +version = "0.1.0" +dependencies = [ + "async-trait", + "borsh 0.10.3", + "light-client", + "light-hasher", + "light-indexed-merkle-tree", + "light-merkle-tree-reference", + "light-prover-client", + "light-sdk", + "log", + "num-bigint 0.4.6", + "num-traits", + "reqwest 0.11.27", + "solana-banks-client", + "solana-program-test", + "solana-sdk", + "tokio", +] + [[package]] name = "light-prover-client" version = "1.2.0" @@ -3815,6 +3837,7 @@ dependencies = [ "light-indexed-merkle-tree", "light-macros", "light-merkle-tree-reference", + "light-program-test", "light-prover-client", "light-registry", "light-system-program", @@ -4141,6 +4164,7 @@ dependencies = [ "light-client", "light-hasher", "light-macros", + "light-program-test", "light-sdk", "light-sdk-macros", "light-test-utils", @@ -4160,6 +4184,7 @@ dependencies = [ "light-client", "light-hasher", "light-macros", + "light-program-test", "light-sdk", "light-sdk-macros", "light-test-utils", @@ -5777,6 +5802,7 @@ dependencies = [ "light-client", "light-hasher", "light-macros", + "light-program-test", "light-sdk", "light-sdk-macros", "light-test-utils", diff --git a/Cargo.toml b/Cargo.toml index 95fdbbe41b..667666e116 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,8 @@ members = [ "test-programs/sdk-test-program/programs/sdk-test/", "forester-utils", "forester", - "photon-api", + "photon-api", + "light-program-test", ] [profile.release] @@ -95,7 +96,7 @@ account-compression = { path = "programs/account-compression", version = "1.2.0" light-compressed-token = { path = "programs/compressed-token", version = "1.2.0", features = ["cpi"] } light-system-program = { path = "programs/system", version = "1.2.0", features = ["cpi"] } light-registry = { path = "programs/registry", version = "1.2.0", features = ["cpi"]} - +light-program-test = { path = "light-program-test", version = "0.1.0" } # Math and crypto num-bigint = "0.4.6" num-traits = "0.2.19" diff --git a/client/src/indexer/mod.rs b/client/src/indexer/mod.rs index 61ddf0b3b3..709c9770c0 100644 --- a/client/src/indexer/mod.rs +++ b/client/src/indexer/mod.rs @@ -16,8 +16,6 @@ use thiserror::Error; use crate::rpc::RpcConnection; -pub mod test_indexer; - #[derive(Error, Debug)] pub enum IndexerError { #[error("RPC Error: {0}")] diff --git a/client/src/rpc/mod.rs b/client/src/rpc/mod.rs index a8a9559adc..60ef0920ac 100644 --- a/client/src/rpc/mod.rs +++ b/client/src/rpc/mod.rs @@ -2,7 +2,6 @@ pub mod errors; pub mod merkle_tree; pub mod rpc_connection; pub mod solana_rpc; -pub mod test_rpc; pub use errors::{assert_rpc_error, RpcError}; pub use rpc_connection::RpcConnection; diff --git a/examples/name-service/programs/name-service-without-macros/Cargo.toml b/examples/name-service/programs/name-service-without-macros/Cargo.toml index d859ebb95c..897d47d459 100644 --- a/examples/name-service/programs/name-service-without-macros/Cargo.toml +++ b/examples/name-service/programs/name-service-without-macros/Cargo.toml @@ -36,5 +36,6 @@ solana-sdk = { workspace = true } [dev-dependencies] light-client = { workspace = true , features = ["devenv"]} light-test-utils = { path = "../../../../test-utils", version = "1.2.0", features = ["devenv"] } +light-program-test = { workspace = true } solana-program-test = { workspace = true } tokio = "1.36.0" diff --git a/examples/name-service/programs/name-service-without-macros/tests/test.rs b/examples/name-service/programs/name-service-without-macros/tests/test.rs index 6f3119c261..d7b8631fef 100644 --- a/examples/name-service/programs/name-service-without-macros/tests/test.rs +++ b/examples/name-service/programs/name-service-without-macros/tests/test.rs @@ -3,10 +3,10 @@ use std::net::{Ipv4Addr, Ipv6Addr}; use anchor_lang::{AnchorDeserialize, InstructionData, ToAccountMetas}; -use light_client::indexer::test_indexer::TestIndexer; use light_client::indexer::{AddressMerkleTreeAccounts, Indexer, StateMerkleTreeAccounts}; use light_client::rpc::merkle_tree::MerkleTreeExt; -use light_client::rpc::test_rpc::ProgramTestRpcConnection; +use light_program_test::test_indexer::TestIndexer; +use light_program_test::test_rpc::ProgramTestRpcConnection; use light_sdk::account_meta::LightAccountMeta; use light_sdk::address::derive_address; use light_sdk::compressed_account::CompressedAccountWithMerkleContext; diff --git a/examples/name-service/programs/name-service/Cargo.toml b/examples/name-service/programs/name-service/Cargo.toml index 2ea79f575a..4a8f894e65 100644 --- a/examples/name-service/programs/name-service/Cargo.toml +++ b/examples/name-service/programs/name-service/Cargo.toml @@ -36,5 +36,6 @@ solana-sdk = { workspace = true } [dev-dependencies] light-client = { workspace = true , features = ["devenv"]} light-test-utils = { path = "../../../../test-utils", version = "1.2.0", features = ["devenv"] } +light-program-test = { workspace = true } solana-program-test = { workspace = true } tokio = "1.36.0" diff --git a/examples/name-service/programs/name-service/tests/test.rs b/examples/name-service/programs/name-service/tests/test.rs index a597fe6766..300a8ac9f3 100644 --- a/examples/name-service/programs/name-service/tests/test.rs +++ b/examples/name-service/programs/name-service/tests/test.rs @@ -3,10 +3,10 @@ use std::net::{Ipv4Addr, Ipv6Addr}; use anchor_lang::{AnchorDeserialize, InstructionData, ToAccountMetas}; -use light_client::indexer::test_indexer::TestIndexer; use light_client::indexer::{AddressMerkleTreeAccounts, Indexer, StateMerkleTreeAccounts}; use light_client::rpc::merkle_tree::MerkleTreeExt; -use light_client::rpc::test_rpc::ProgramTestRpcConnection; +use light_program_test::test_indexer::TestIndexer; +use light_program_test::test_rpc::ProgramTestRpcConnection; use light_sdk::address::{derive_address, derive_address_seed}; use light_sdk::compressed_account::CompressedAccountWithMerkleContext; use light_sdk::error::LightSdkError; diff --git a/light-program-test/Cargo.toml b/light-program-test/Cargo.toml new file mode 100644 index 0000000000..9da060ea1d --- /dev/null +++ b/light-program-test/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "light-program-test" +version = "0.1.0" +edition = "2021" + +[dependencies] + +light-client = { workspace = true } +light-prover-client = { workspace = true } +light-sdk = { workspace = true } +light-indexed-merkle-tree = { workspace = true } +light-merkle-tree-reference = { workspace = true } +light-hasher = { workspace = true } + +solana-sdk = { workspace = true } +solana-banks-client = { workspace = true } +solana-program-test = { workspace = true } + +log = { workspace = true } +borsh = { workspace = true } +tokio = { workspace = true } +async-trait = { workspace = true } +num-bigint = { workspace = true } +num-traits = { workspace = true } +reqwest = { workspace = true } \ No newline at end of file diff --git a/light-program-test/src/lib.rs b/light-program-test/src/lib.rs new file mode 100644 index 0000000000..f4fbdb5230 --- /dev/null +++ b/light-program-test/src/lib.rs @@ -0,0 +1,2 @@ +pub mod test_indexer; +pub mod test_rpc; diff --git a/client/src/indexer/test_indexer.rs b/light-program-test/src/test_indexer.rs similarity index 99% rename from client/src/indexer/test_indexer.rs rename to light-program-test/src/test_indexer.rs index 3ddd8a3da2..f5cef04e3d 100644 --- a/client/src/indexer/test_indexer.rs +++ b/light-program-test/src/test_indexer.rs @@ -1,11 +1,12 @@ -use std::{marker::PhantomData, time::Duration}; - -use crate::{ - indexer::Indexer, +use borsh::BorshDeserialize; +use light_client::{ + indexer::{ + AddressMerkleTreeAccounts, AddressMerkleTreeBundle, Indexer, StateMerkleTreeAccounts, + StateMerkleTreeBundle, + }, rpc::{merkle_tree::MerkleTreeExt, RpcConnection}, transaction_params::FeeConfig, }; -use borsh::BorshDeserialize; use light_hasher::Poseidon; use light_indexed_merkle_tree::{array::IndexedArray, reference::IndexedMerkleTree}; use light_merkle_tree_reference::MerkleTree; @@ -39,11 +40,7 @@ use num_bigint::BigInt; use num_traits::FromBytes; use reqwest::Client; use solana_sdk::pubkey::Pubkey; - -use super::{ - AddressMerkleTreeAccounts, AddressMerkleTreeBundle, StateMerkleTreeAccounts, - StateMerkleTreeBundle, -}; +use std::{marker::PhantomData, time::Duration}; #[derive(Debug)] pub struct TestIndexer diff --git a/client/src/rpc/test_rpc.rs b/light-program-test/src/test_rpc.rs similarity index 98% rename from client/src/rpc/test_rpc.rs rename to light-program-test/src/test_rpc.rs index c39ffa7d9f..e7d7cccf77 100644 --- a/client/src/rpc/test_rpc.rs +++ b/light-program-test/src/test_rpc.rs @@ -17,9 +17,8 @@ use solana_sdk::{ transaction::{Transaction, TransactionError}, }; -use crate::transaction_params::TransactionParams; - -use super::{merkle_tree::MerkleTreeExt, RpcConnection, RpcError}; +use light_client::rpc::{merkle_tree::MerkleTreeExt, RpcConnection, RpcError}; +use light_client::transaction_params::TransactionParams; pub struct ProgramTestRpcConnection { pub context: ProgramTestContext, diff --git a/test-programs/sdk-test-program/programs/sdk-test/Cargo.toml b/test-programs/sdk-test-program/programs/sdk-test/Cargo.toml index 9b55b9bb02..29c92f2840 100644 --- a/test-programs/sdk-test-program/programs/sdk-test/Cargo.toml +++ b/test-programs/sdk-test-program/programs/sdk-test/Cargo.toml @@ -35,6 +35,7 @@ solana-sdk = { workspace = true } [dev-dependencies] light-client = { workspace = true , features = ["devenv"]} +light-program-test = { workspace = true } light-test-utils = { path = "../../../../test-utils", version = "1.2.0", features = ["devenv"] } solana-program-test = { workspace = true } tokio = { workspace = true } diff --git a/test-programs/sdk-test-program/programs/sdk-test/tests/test.rs b/test-programs/sdk-test-program/programs/sdk-test/tests/test.rs index c5cdc424a9..824f5ec15c 100644 --- a/test-programs/sdk-test-program/programs/sdk-test/tests/test.rs +++ b/test-programs/sdk-test-program/programs/sdk-test/tests/test.rs @@ -1,10 +1,10 @@ #![cfg(feature = "test-sbf")] use anchor_lang::{AnchorDeserialize, InstructionData, ToAccountMetas}; -use light_client::indexer::test_indexer::TestIndexer; use light_client::indexer::{AddressMerkleTreeAccounts, Indexer, StateMerkleTreeAccounts}; use light_client::rpc::merkle_tree::MerkleTreeExt; -use light_client::rpc::test_rpc::ProgramTestRpcConnection; +use light_program_test::test_indexer::TestIndexer; +use light_program_test::test_rpc::ProgramTestRpcConnection; use light_sdk::account_meta::LightAccountMeta; use light_sdk::address::derive_address; use light_sdk::compressed_account::CompressedAccountWithMerkleContext; diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 2931e44c03..23970e4c95 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -38,6 +38,7 @@ light-concurrent-merkle-tree = { version = "1.1.0", path = "../merkle-tree/concu light-indexed-merkle-tree = { path = "../merkle-tree/indexed/", version = "1.1.0" } light-verifier = { path = "../circuit-lib/verifier", version = "1.1.0" } light-utils = { path = "../utils", version = "1.1.0" } +light-program-test = { workspace = true } forester-utils = { workspace = true } memoffset = "0.9.1" rand = "0.8" diff --git a/test-utils/src/test_env.rs b/test-utils/src/test_env.rs index cdcedc06a4..fbf7ce237c 100644 --- a/test-utils/src/test_env.rs +++ b/test-utils/src/test_env.rs @@ -375,7 +375,7 @@ pub async fn setup_test_programs_with_accounts( pub async fn setup_test_programs_with_accounts_v2( additional_programs: Option>, ) -> ( - light_client::rpc::test_rpc::ProgramTestRpcConnection, + light_program_test::test_rpc::ProgramTestRpcConnection, EnvAccounts, ) { setup_test_programs_with_accounts_with_protocol_config_v2( @@ -429,11 +429,11 @@ pub async fn setup_test_programs_with_accounts_with_protocol_config_v2( protocol_config: ProtocolConfig, register_forester_and_advance_to_active_phase: bool, ) -> ( - light_client::rpc::test_rpc::ProgramTestRpcConnection, + light_program_test::test_rpc::ProgramTestRpcConnection, EnvAccounts, ) { let context = setup_test_programs(additional_programs).await; - let mut context = light_client::rpc::test_rpc::ProgramTestRpcConnection { context }; + let mut context = light_program_test::test_rpc::ProgramTestRpcConnection { context }; let keypairs = EnvAccountKeypairs::program_test_default(); airdrop_lamports( &mut context,