Skip to content

Commit 5490ea1

Browse files
refactor: program test reduce forester utils dep (#1867)
* refactor: copy create_account_instruction * refactor: copy TreeAccounts * moved register test forester to light-program-test * removed forester-utils dep * refactor: test forester to light-test-utils * fix lint * fix tests * chore: bump light-program-test version 0.13.1 -> 0.13.2 * Update program-tests/utils/src/setup_forester.rs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * refactor: move forester_update -> light-test-utils" --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b5e7363 commit 5490ea1

File tree

26 files changed

+328
-155
lines changed

26 files changed

+328
-155
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ output1.txt
8686
.zed
8787

8888
**/.claude/**/*
89+
**/~/

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ light-registry = { path = "programs/registry", version = "2.0.0", features = [
184184
create-address-test-program = { path = "program-tests/create-address-test-program", version = "1.0.0", features = [
185185
"cpi",
186186
] }
187-
light-program-test = { path = "sdk-libs/program-test", version = "0.13.1" }
187+
light-program-test = { path = "sdk-libs/program-test", version = "0.13.2" }
188188
light-batched-merkle-tree = { path = "program-libs/batched-merkle-tree", version = "0.3.0" }
189189
light-merkle-tree-metadata = { path = "program-libs/merkle-tree-metadata", version = "0.3.0" }
190190
aligned-sized = { path = "program-libs/aligned-sized", version = "1.1.0" }

forester-utils/src/registry.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use light_registry::{
1111
create_rollover_state_merkle_tree_instruction, CreateRolloverMerkleTreeInstructionInputs,
1212
},
1313
protocol_config::state::ProtocolConfig,
14-
sdk::{create_register_forester_instruction, create_update_forester_pda_instruction},
14+
sdk::create_update_forester_pda_instruction,
1515
utils::get_forester_pda,
1616
ForesterConfig, ForesterPda,
1717
};
@@ -27,38 +27,6 @@ use crate::{
2727
instructions::create_account::create_account_instruction,
2828
};
2929

30-
/// Creates and asserts forester account creation.
31-
pub async fn register_test_forester<R: Rpc>(
32-
rpc: &mut R,
33-
governance_authority: &Keypair,
34-
forester_authority: &Pubkey,
35-
config: ForesterConfig,
36-
) -> Result<(), RpcError> {
37-
let ix = create_register_forester_instruction(
38-
&governance_authority.pubkey(),
39-
&governance_authority.pubkey(),
40-
forester_authority,
41-
config,
42-
);
43-
rpc.create_and_send_transaction(
44-
&[ix],
45-
&governance_authority.pubkey(),
46-
&[governance_authority],
47-
)
48-
.await?;
49-
assert_registered_forester(
50-
rpc,
51-
forester_authority,
52-
ForesterPda {
53-
authority: *forester_authority,
54-
config,
55-
active_weight: 1,
56-
..Default::default()
57-
},
58-
)
59-
.await
60-
}
61-
6230
pub async fn update_test_forester<R: Rpc>(
6331
rpc: &mut R,
6432
forester_authority: &Keypair,

forester/tests/legacy/batched_address_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration};
22

33
use forester::run_pipeline;
44
use forester_utils::{
5-
registry::{register_test_forester, update_test_forester},
5+
registry::update_test_forester,
66
rpc_pool::SolanaRpcPoolBuilder,
77
};
88
use light_batched_merkle_tree::{

program-tests/registry-test/tests/tests.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ use light_test_utils::{
7474
e2e_test_env::init_program_test_env,
7575
register_test_forester,
7676
setup_accounts::setup_accounts,
77+
setup_forester_and_advance_to_epoch,
7778
test_batch_forester::{
7879
assert_perform_state_mt_roll_over, create_append_batch_ix_data,
7980
create_batch_update_address_tree_instruction_data_with_proof, perform_batch_append,
@@ -193,6 +194,7 @@ async fn test_initialize_protocol_config() {
193194
payer,
194195
config: ProgramTestConfig::default(),
195196
};
197+
196198
let payer = rpc.get_payer().insecure_clone();
197199
let program_account_keypair = Keypair::from_bytes(&OLD_REGISTRY_ID_TEST_KEYPAIR).unwrap();
198200
let protocol_config = ProtocolConfig::default();
@@ -550,6 +552,10 @@ async fn test_custom_forester() {
550552
let mut rpc = LightProgramTest::new(ProgramTestConfig::default_with_batched_trees(true))
551553
.await
552554
.unwrap();
555+
let protocol_config = rpc.config.protocol_config;
556+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
557+
.await
558+
.unwrap();
553559
rpc.indexer = None;
554560

555561
let env = rpc.test_accounts.clone();
@@ -629,6 +635,10 @@ async fn test_custom_forester_batched() {
629635
let mut rpc = LightProgramTest::new(ProgramTestConfig::default_test_forester(true))
630636
.await
631637
.unwrap();
638+
let protocol_config = rpc.config.protocol_config;
639+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
640+
.await
641+
.unwrap();
632642
rpc.indexer = None;
633643
let env = rpc.test_accounts.clone();
634644
let tree_params = ProgramTestConfig::default_with_batched_trees(true)
@@ -749,7 +759,6 @@ async fn test_register_and_update_forester_pda() {
749759
let config = ProgramTestConfig {
750760
protocol_config: ProtocolConfig::default(),
751761
with_prover: false,
752-
register_forester_and_advance_to_active_phase: false,
753762
..Default::default()
754763
};
755764

@@ -1018,6 +1027,10 @@ async fn failing_test_forester() {
10181027
let mut rpc = LightProgramTest::new(ProgramTestConfig::default_with_batched_trees(true))
10191028
.await
10201029
.unwrap();
1030+
let protocol_config = rpc.config.protocol_config;
1031+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1032+
.await
1033+
.unwrap();
10211034
rpc.indexer = None;
10221035
let env = rpc.test_accounts.clone();
10231036
let payer = rpc.get_payer().insecure_clone();
@@ -1409,6 +1422,10 @@ async fn test_migrate_state() {
14091422
let mut rpc = LightProgramTest::new(ProgramTestConfig::default_with_batched_trees(true))
14101423
.await
14111424
.unwrap();
1425+
let protocol_config = rpc.config.protocol_config;
1426+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1427+
.await
1428+
.unwrap();
14121429
rpc.indexer = None;
14131430
let test_accounts = rpc.test_accounts.clone();
14141431
let payer = rpc.get_payer().insecure_clone();
@@ -1678,6 +1695,10 @@ async fn test_rollover_batch_state_tree() {
16781695
config.v2_state_tree_config = Some(params);
16791696

16801697
let mut rpc = LightProgramTest::new(config).await.unwrap();
1698+
let protocol_config = rpc.config.protocol_config;
1699+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1700+
.await
1701+
.unwrap();
16811702
rpc.indexer = None;
16821703
let test_accounts = rpc.test_accounts.clone();
16831704
let payer = rpc.get_payer().insecure_clone();
@@ -1869,6 +1890,11 @@ async fn test_batch_address_tree() {
18691890
CREATE_ADDRESS_TEST_PROGRAM_ID,
18701891
)]);
18711892
let mut rpc = LightProgramTest::new(config).await.unwrap();
1893+
let protocol_config = rpc.config.protocol_config;
1894+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1895+
.await
1896+
.unwrap();
1897+
18721898
rpc.indexer = None;
18731899
let env = rpc.test_accounts.clone();
18741900

@@ -2044,6 +2070,10 @@ async fn test_rollover_batch_address_tree() {
20442070
)]);
20452071
config.v2_address_tree_config = Some(tree_params);
20462072
let mut rpc = LightProgramTest::new(config).await.unwrap();
2073+
let protocol_config = rpc.config.protocol_config;
2074+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
2075+
.await
2076+
.unwrap();
20472077
rpc.indexer = None;
20482078
let env = rpc.test_accounts.clone();
20492079

program-tests/system-cpi-test/tests/test_program_owned_trees.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use light_registry::{
3131
};
3232
use light_test_utils::{
3333
airdrop_lamports, assert_custom_error_or_program_error, create_account_instruction,
34-
get_concurrent_merkle_tree, spl::create_mint_helper, FeeConfig, Rpc, RpcError,
35-
TransactionParams,
34+
get_concurrent_merkle_tree, setup_forester_and_advance_to_epoch, spl::create_mint_helper,
35+
FeeConfig, Rpc, RpcError, TransactionParams,
3636
};
3737
use serial_test::serial;
3838
use solana_sdk::{
@@ -211,6 +211,13 @@ async fn test_invalid_registered_program() {
211211
airdrop_lamports(&mut rpc, &payer.pubkey(), 100_000_000_000)
212212
.await
213213
.unwrap();
214+
215+
// Setup forester to ensure registered_forester_pda is initialized
216+
let protocol_config = rpc.config.protocol_config;
217+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
218+
.await
219+
.unwrap();
220+
214221
let group_seed_keypair = Keypair::new();
215222
let program_id_keypair = Keypair::from_bytes(&CPI_SYSTEM_TEST_PROGRAM_ID_KEYPAIR).unwrap();
216223
println!("program_id_keypair: {:?}", program_id_keypair.pubkey());

program-tests/system-test/tests/test.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use light_system_program::{
3434
use light_test_utils::{
3535
airdrop_lamports,
3636
assert_compressed_tx::assert_created_compressed_accounts,
37-
assert_custom_error_or_program_error,
37+
assert_custom_error_or_program_error, setup_forester_and_advance_to_epoch,
3838
system_program::{
3939
compress_sol_test, create_addresses_test, create_invoke_instruction,
4040
create_invoke_instruction_data_and_remaining_accounts, decompress_sol_test,
@@ -1674,6 +1674,11 @@ async fn regenerate_accounts() {
16741674
.unwrap();
16751675
let keypairs = for_regenerate_accounts();
16761676

1677+
// Setup forester and get epoch information
1678+
let forester_epoch = setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1679+
.await
1680+
.unwrap();
1681+
16771682
// List of public keys to fetch and export
16781683
let pubkeys = vec![
16791684
("merkle_tree_pubkey", env.v1_state_trees[0].merkle_tree),
@@ -1707,18 +1712,8 @@ async fn regenerate_accounts() {
17071712
"registered_forester_pda",
17081713
env.protocol.registered_forester_pda,
17091714
),
1710-
(
1711-
"forester_epoch_pda",
1712-
env.protocol
1713-
.forester_epoch
1714-
.as_ref()
1715-
.unwrap()
1716-
.forester_epoch_pda,
1717-
),
1718-
(
1719-
"epoch_pda",
1720-
env.protocol.forester_epoch.as_ref().unwrap().epoch_pda,
1721-
),
1715+
("forester_epoch_pda", forester_epoch.forester_epoch_pda),
1716+
("epoch_pda", forester_epoch.epoch_pda),
17221717
("batch_state_merkle_tree", env.v2_state_trees[0].merkle_tree),
17231718
("batched_output_queue", env.v2_state_trees[0].output_queue),
17241719
("batch_address_merkle_tree", env.v2_address_trees[0]),
@@ -1845,6 +1840,10 @@ async fn batch_invoke_test() {
18451840
let config = ProgramTestConfig::default_test_forester(false);
18461841

18471842
let mut rpc = LightProgramTest::new(config).await.unwrap();
1843+
let protocol_config = rpc.config.protocol_config;
1844+
setup_forester_and_advance_to_epoch(&mut rpc, &protocol_config)
1845+
.await
1846+
.unwrap();
18481847

18491848
let env = rpc.test_accounts.clone();
18501849
let payer = rpc.get_payer().insecure_clone();

program-tests/utils/src/e2e_test_env.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use forester_utils::{
7676
account_zero_copy::AccountZeroCopy,
7777
address_merkle_tree_config::{address_tree_ready_for_rollover, state_tree_ready_for_rollover},
7878
forester_epoch::{Epoch, Forester, TreeAccounts},
79-
registry::register_test_forester,
8079
utils::airdrop_lamports,
8180
};
8281
use light_batched_merkle_tree::{
@@ -132,6 +131,7 @@ use light_program_test::{
132131
TestIndexerExtensions,
133132
},
134133
program_test::{LightProgramTest, TestRpc},
134+
utils::register_test_forester::register_test_forester,
135135
};
136136
use light_prover_client::{
137137
constants::{PROVE_PATH, SERVER_ADDRESS},
@@ -394,23 +394,9 @@ where
394394
.config;
395395
// TODO: add clear test env enum
396396
// register foresters is only compatible with ProgramTest environment
397-
let (foresters, epoch_config) =
398-
if let Some(registered_epoch) = test_accounts.protocol.forester_epoch.as_ref() {
399-
let _forester = Forester {
400-
registration: registered_epoch.clone(),
401-
active: registered_epoch.clone(),
402-
..Default::default()
403-
};
404-
// Forester epoch account is assumed to exist (is inited with test program deployment)
405-
let forester = TestForester {
406-
keypair: test_accounts.protocol.forester.insecure_clone(),
407-
forester: _forester.clone(),
408-
is_registered: Some(0),
409-
};
410-
(vec![forester], _forester)
411-
} else {
412-
(Vec::<TestForester>::new(), Forester::default())
413-
};
397+
// Default forester setup - tests that need forester functionality should call
398+
// setup_forester_and_advance_to_epoch explicitly and manage their own forester state
399+
let (foresters, epoch_config) = (Vec::<TestForester>::new(), Forester::default());
414400
Self {
415401
payer,
416402
indexer,

program-tests/utils/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod mock_batched_forester;
3232
pub mod pack;
3333
pub mod registered_program_accounts_v1;
3434
pub mod setup_accounts;
35+
pub mod setup_forester;
3536
#[allow(unused)]
3637
pub mod spl;
3738
pub mod state_tree_rollover;
@@ -46,8 +47,7 @@ pub use forester_utils::{
4647
forester_epoch::{Epoch, TreeAccounts},
4748
registry::{
4849
create_rollover_address_merkle_tree_instructions,
49-
create_rollover_state_merkle_tree_instructions, register_test_forester,
50-
update_test_forester,
50+
create_rollover_state_merkle_tree_instructions, update_test_forester,
5151
},
5252
};
5353
pub use light_client::{
@@ -56,7 +56,9 @@ pub use light_client::{
5656
};
5757
use light_hasher::Poseidon;
5858
use light_program_test::accounts::address_tree::create_address_merkle_tree_and_queue_account;
59+
pub use light_program_test::utils::register_test_forester::register_test_forester;
5960
use light_registry::account_compression_cpi::sdk::get_registered_program_pda;
61+
pub use setup_forester::setup_forester_and_advance_to_epoch;
6062

6163
use crate::assert_queue::assert_address_queue_initialized;
6264

0 commit comments

Comments
 (0)