light_program_test/utils/
setup_light_programs.rs

1use light_client::rpc::RpcError;
2use light_compressed_account::constants::REGISTERED_PROGRAM_PDA;
3use light_registry::account_compression_cpi::sdk::get_registered_program_pda;
4use litesvm::LiteSVM;
5use solana_compute_budget::compute_budget::ComputeBudget;
6use solana_pubkey::Pubkey;
7
8use crate::{
9    accounts::{
10        registered_program_accounts::{
11            registered_program_test_account_registry_program,
12            registered_program_test_account_system_program,
13        },
14        test_accounts::NOOP_PROGRAM_ID,
15    },
16    utils::find_light_bin::find_light_bin,
17};
18
19/// Creates ProgramTestContext with light protocol and additional programs.
20///
21/// Programs:
22/// 1. light_registry program
23/// 2. account_compression program
24/// 3. light_compressed_token program
25/// 4. light_system_program program
26pub fn setup_light_programs(
27    additional_programs: Option<Vec<(&'static str, Pubkey)>>,
28) -> Result<LiteSVM, RpcError> {
29    let program_test = LiteSVM::new().with_log_bytes_limit(Some(100_000));
30    let program_test = program_test.with_compute_budget(ComputeBudget {
31        compute_unit_limit: 1_400_000,
32        ..Default::default()
33    });
34    let mut program_test = program_test.with_transaction_history(0);
35    let project_root_target_deploy_path = std::env::var("SBF_OUT_DIR")
36        .map_err(|_| RpcError::CustomError("SBF_OUT_DIR not set.".to_string()))?;
37    // find path to bin where light cli stores program binaries.
38    let light_bin_path = find_light_bin().ok_or(RpcError::CustomError(
39        "Failed to find light binary path. To use light-program-test zk compression cli needs to be installed and light system programs need to be downloaded. Light system programs are downloaded the first time light test-validator is run.".to_string(),
40    ))?;
41    let light_bin_path = light_bin_path
42        .to_str()
43        .ok_or(RpcError::CustomError(format!(
44            "Found invalid light binary path {:?}",
45            light_bin_path
46        )))?;
47    let path = format!("{}/light_registry.so", light_bin_path);
48    program_test
49        .add_program_from_file(light_registry::ID, path.clone())
50        .inspect_err(|_| {
51            println!("Program light_registry bin not found in {}", path);
52        })?;
53    let path = format!("{}/account_compression.so", light_bin_path);
54    program_test
55        .add_program_from_file(account_compression::ID, path.clone())
56        .inspect_err(|_| {
57            println!("Program account_compression bin not found in {}", path);
58        })?;
59    let path = format!("{}/light_compressed_token.so", light_bin_path);
60    program_test
61        .add_program_from_file(light_compressed_token::ID, path.clone())
62        .inspect_err(|_| {
63            println!("Program light_compressed_token bin not found in {}", path);
64        })?;
65    let path = format!("{}/spl_noop.so", light_bin_path);
66    program_test
67        .add_program_from_file(NOOP_PROGRAM_ID, path.clone())
68        .inspect_err(|_| {
69            println!("Program spl_noop bin not found in {}", path);
70        })?;
71
72    let path = format!("{}/light_system_program_pinocchio.so", light_bin_path);
73    program_test
74        .add_program_from_file(light_sdk::constants::LIGHT_SYSTEM_PROGRAM_ID, path.clone())
75        .inspect_err(|_| {
76            println!(
77                "Program light_system_program_pinocchio bin not found in {}",
78                path
79            );
80        })?;
81
82    let registered_program = registered_program_test_account_system_program();
83    program_test
84        .set_account(
85            Pubkey::new_from_array(REGISTERED_PROGRAM_PDA),
86            registered_program,
87        )
88        .map_err(|e| {
89            RpcError::CustomError(format!("Setting registered program account failed {}", e))
90        })?;
91    let registered_program = registered_program_test_account_registry_program();
92    program_test
93        .set_account(
94            get_registered_program_pda(&light_registry::ID),
95            registered_program,
96        )
97        .map_err(|e| {
98            RpcError::CustomError(format!("Setting registered program account failed {}", e))
99        })?;
100    if let Some(programs) = additional_programs {
101        for (name, id) in programs {
102            let path = format!("{}/{}.so", project_root_target_deploy_path, name);
103            program_test
104                .add_program_from_file(id, path.clone())
105                .inspect_err(|_| {
106                    println!("Program {} bin not found in {}", name, path);
107                })?;
108        }
109    }
110    Ok(program_test)
111}