light_program_test/utils/
register_test_forester.rs

1use light_client::rpc::{Rpc, RpcError};
2// When devenv is enabled, use light_registry's types and SDK
3#[cfg(feature = "devenv")]
4use light_registry::{
5    sdk::create_register_forester_instruction, utils::get_forester_pda, ForesterConfig, ForesterPda,
6};
7use solana_sdk::{
8    pubkey::Pubkey,
9    signature::{Keypair, Signer},
10};
11
12// When devenv is NOT enabled, use local registry_sdk
13#[cfg(not(feature = "devenv"))]
14use crate::registry_sdk::{
15    create_register_forester_instruction, deserialize_forester_pda, get_forester_pda,
16    ForesterConfig, ForesterPda,
17};
18
19/// Creates and asserts forester account creation.
20pub async fn register_test_forester<R: Rpc>(
21    rpc: &mut R,
22    governance_authority: &Keypair,
23    forester_authority: &Pubkey,
24    config: ForesterConfig,
25) -> Result<(), RpcError> {
26    let ix = create_register_forester_instruction(
27        &governance_authority.pubkey(),
28        &governance_authority.pubkey(),
29        forester_authority,
30        config,
31    );
32    rpc.create_and_send_transaction(
33        &[ix],
34        &governance_authority.pubkey(),
35        &[governance_authority],
36    )
37    .await?;
38    assert_registered_forester(
39        rpc,
40        forester_authority,
41        ForesterPda {
42            authority: *forester_authority,
43            config,
44            active_weight: 1,
45            ..Default::default()
46        },
47    )
48    .await
49}
50
51#[cfg(feature = "devenv")]
52async fn assert_registered_forester<R: Rpc>(
53    rpc: &mut R,
54    forester: &Pubkey,
55    expected_account: ForesterPda,
56) -> Result<(), RpcError> {
57    let pda = get_forester_pda(forester).0;
58    let account_data = rpc.get_anchor_account::<ForesterPda>(&pda).await?.unwrap();
59    if account_data != expected_account {
60        return Err(RpcError::AssertRpcError(format!(
61            "Expected account data: {:?}, got: {:?}",
62            expected_account, account_data
63        )));
64    }
65    Ok(())
66}
67
68#[cfg(not(feature = "devenv"))]
69async fn assert_registered_forester<R: Rpc>(
70    rpc: &mut R,
71    forester: &Pubkey,
72    expected_account: ForesterPda,
73) -> Result<(), RpcError> {
74    let pda = get_forester_pda(forester).0;
75    let account = rpc
76        .get_account(pda)
77        .await?
78        .ok_or_else(|| RpcError::CustomError(format!("Forester PDA account not found: {}", pda)))?;
79    let account_data = deserialize_forester_pda(&account.data)
80        .map_err(|e| RpcError::CustomError(format!("Failed to deserialize ForesterPda: {}", e)))?;
81    if account_data != expected_account {
82        return Err(RpcError::AssertRpcError(format!(
83            "Expected account data: {:?}, got: {:?}",
84            expected_account, account_data
85        )));
86    }
87    Ok(())
88}