light_program_test/program_test/
config.rs

1#[cfg(feature = "devenv")]
2use account_compression::{
3    AddressMerkleTreeConfig, AddressQueueConfig, NullifierQueueConfig, StateMerkleTreeConfig,
4};
5#[cfg(feature = "devenv")]
6use light_batched_merkle_tree::{
7    initialize_address_tree::InitAddressTreeAccountsInstructionData,
8    initialize_state_tree::InitStateTreeAccountsInstructionData,
9};
10#[cfg(feature = "devenv")]
11use light_registry::protocol_config::state::ProtocolConfig;
12use solana_sdk::pubkey::Pubkey;
13
14use crate::logging::EnhancedLoggingConfig;
15
16/// Configuration for Light Program Test
17#[derive(Debug, Clone)]
18pub struct ProgramTestConfig {
19    pub additional_programs: Option<Vec<(&'static str, Pubkey)>>,
20    #[cfg(feature = "devenv")]
21    pub protocol_config: ProtocolConfig,
22    pub with_prover: bool,
23    #[cfg(feature = "devenv")]
24    pub auto_register_custom_programs_for_pda_compression: bool,
25    #[cfg(feature = "devenv")]
26    pub skip_register_programs: bool,
27    #[cfg(feature = "devenv")]
28    pub skip_v1_trees: bool,
29    #[cfg(feature = "devenv")]
30    pub skip_second_v1_tree: bool,
31    #[cfg(feature = "devenv")]
32    pub v1_state_tree_config: StateMerkleTreeConfig,
33    #[cfg(feature = "devenv")]
34    pub v1_nullifier_queue_config: NullifierQueueConfig,
35    #[cfg(feature = "devenv")]
36    pub v1_address_tree_config: AddressMerkleTreeConfig,
37    #[cfg(feature = "devenv")]
38    pub v1_address_queue_config: AddressQueueConfig,
39    #[cfg(feature = "devenv")]
40    pub v2_state_tree_config: Option<InitStateTreeAccountsInstructionData>,
41    #[cfg(feature = "devenv")]
42    pub v2_address_tree_config: Option<InitAddressTreeAccountsInstructionData>,
43    #[cfg(feature = "devenv")]
44    pub skip_protocol_init: bool,
45    /// Log failed transactions
46    pub log_failed_tx: bool,
47    /// Disable all logging
48    pub no_logs: bool,
49    /// Skip startup logs
50    pub skip_startup_logs: bool,
51    /// Register a forester for epoch 0 during setup
52    pub with_forester: bool,
53    /// Log Light Protocol events (BatchPublicTransactionEvent, etc.)
54    pub log_light_protocol_events: bool,
55    /// Enhanced transaction logging configuration
56    pub enhanced_logging: EnhancedLoggingConfig,
57}
58
59impl ProgramTestConfig {
60    pub fn new(
61        with_prover: bool,
62        additional_programs: Option<Vec<(&'static str, Pubkey)>>,
63    ) -> Self {
64        Self {
65            additional_programs,
66            with_prover,
67            ..Default::default()
68        }
69    }
70
71    #[cfg(feature = "v2")]
72    pub fn new_v2(
73        with_prover: bool,
74        additional_programs: Option<Vec<(&'static str, Pubkey)>>,
75    ) -> Self {
76        Self {
77            additional_programs,
78            with_prover,
79            #[cfg(feature = "devenv")]
80            v2_state_tree_config: Some(InitStateTreeAccountsInstructionData::test_default()),
81            #[cfg(feature = "devenv")]
82            v2_address_tree_config: Some(InitAddressTreeAccountsInstructionData::test_default()),
83            ..Default::default()
84        }
85    }
86
87    #[cfg(feature = "devenv")]
88    pub fn default_with_batched_trees(with_prover: bool) -> Self {
89        Self {
90            additional_programs: None,
91            with_prover,
92            v2_state_tree_config: Some(InitStateTreeAccountsInstructionData::test_default()),
93            v2_address_tree_config: Some(InitAddressTreeAccountsInstructionData::test_default()),
94            ..Default::default()
95        }
96    }
97
98    #[cfg(feature = "devenv")]
99    pub fn default_test_forester(with_prover: bool) -> Self {
100        Self {
101            additional_programs: None,
102            with_prover,
103            v2_state_tree_config: Some(InitStateTreeAccountsInstructionData::test_default()),
104            v2_address_tree_config: Some(InitAddressTreeAccountsInstructionData::test_default()),
105            ..Default::default()
106        }
107    }
108
109    /// Enable Light Protocol event logging
110    pub fn with_light_protocol_events(mut self) -> Self {
111        self.log_light_protocol_events = true;
112        self
113    }
114
115    /// Disable Light Protocol event logging
116    pub fn without_light_protocol_events(mut self) -> Self {
117        self.log_light_protocol_events = false;
118        self
119    }
120
121    /// Register custom instruction decoders for enhanced logging
122    ///
123    /// This allows registering decoders generated by `#[derive(InstructionDecoder)]`
124    /// or `#[instruction_decoder]` macros from `light-instruction-decoder-derive`.
125    ///
126    /// ## Example
127    ///
128    /// ```rust,ignore
129    /// use light_instruction_decoder_derive::instruction_decoder;
130    ///
131    /// #[instruction_decoder(program_id = crate::ID)]
132    /// #[program]
133    /// pub mod my_program {
134    ///     // ...
135    /// }
136    ///
137    /// let config = ProgramTestConfig::new_v2(true, Some(vec![("my_program", program_id)]))
138    ///     .with_decoders(vec![Box::new(MyProgramInstructionDecoder)]);
139    /// ```
140    pub fn with_decoders(
141        mut self,
142        decoders: Vec<Box<dyn crate::logging::InstructionDecoder>>,
143    ) -> Self {
144        self.enhanced_logging = self.enhanced_logging.with_decoders(decoders);
145        self
146    }
147}
148
149impl Default for ProgramTestConfig {
150    fn default() -> Self {
151        Self {
152            additional_programs: None,
153            #[cfg(feature = "devenv")]
154            protocol_config: ProtocolConfig {
155                // Init with an active epoch which doesn't end
156                active_phase_length: 1_000_000_000,
157                slot_length: 1_000_000_000 - 1,
158                genesis_slot: 0,
159                registration_phase_length: 2,
160                ..Default::default()
161            },
162            with_prover: true,
163            #[cfg(feature = "devenv")]
164            auto_register_custom_programs_for_pda_compression: true,
165            #[cfg(feature = "devenv")]
166            skip_second_v1_tree: false,
167            #[cfg(feature = "devenv")]
168            skip_register_programs: false,
169            #[cfg(feature = "devenv")]
170            v1_state_tree_config: StateMerkleTreeConfig::default(),
171            #[cfg(feature = "devenv")]
172            v1_address_tree_config: AddressMerkleTreeConfig::default(),
173            #[cfg(feature = "devenv")]
174            v1_address_queue_config: AddressQueueConfig::default(),
175            #[cfg(feature = "devenv")]
176            v1_nullifier_queue_config: NullifierQueueConfig::default(),
177            #[cfg(feature = "devenv")]
178            v2_state_tree_config: None,
179            #[cfg(feature = "devenv")]
180            v2_address_tree_config: None,
181            #[cfg(feature = "devenv")]
182            skip_protocol_init: false,
183            #[cfg(feature = "devenv")]
184            skip_v1_trees: false,
185            log_failed_tx: true,
186            no_logs: false,
187            skip_startup_logs: true,
188            with_forester: true,
189            log_light_protocol_events: false, // Disabled by default
190            enhanced_logging: EnhancedLoggingConfig::from_env(),
191        }
192    }
193}