light_test_utils/
assert_epoch.rs

1use light_client::rpc::RpcConnection;
2use light_registry::{
3    protocol_config::state::ProtocolConfigPda,
4    utils::{get_epoch_pda_address, get_forester_pda, get_protocol_config_pda_address},
5    EpochPda, ForesterEpochPda, ForesterPda,
6};
7use solana_sdk::pubkey::Pubkey;
8
9pub async fn assert_finalized_epoch_registration<R: RpcConnection>(
10    rpc: &mut R,
11    forester_epoch_pda_pubkey: &Pubkey,
12    epoch_pda_pubkey: &Pubkey,
13) {
14    let epoch_pda = rpc
15        .get_anchor_account::<EpochPda>(epoch_pda_pubkey)
16        .await
17        .unwrap()
18        .unwrap();
19    let expected_total_epoch_weight = epoch_pda.registered_weight;
20    let forester_epoch_pda = rpc
21        .get_anchor_account::<ForesterEpochPda>(forester_epoch_pda_pubkey)
22        .await
23        .unwrap()
24        .unwrap();
25    assert!(forester_epoch_pda.total_epoch_weight.is_some());
26    assert_eq!(
27        forester_epoch_pda.total_epoch_weight.unwrap(),
28        expected_total_epoch_weight
29    );
30}
31
32pub async fn assert_epoch_pda<R: RpcConnection>(
33    rpc: &mut R,
34    epoch: u64,
35    expected_registered_weight: u64,
36) {
37    let epoch_pda_pubkey = get_epoch_pda_address(epoch);
38    let epoch_pda = rpc
39        .get_anchor_account::<EpochPda>(&epoch_pda_pubkey)
40        .await
41        .unwrap()
42        .unwrap();
43    let protocol_config_pda_pubkey = get_protocol_config_pda_address().0;
44    let protocol_config_pda = rpc
45        .get_anchor_account::<ProtocolConfigPda>(&protocol_config_pda_pubkey)
46        .await
47        .unwrap()
48        .unwrap();
49    assert_eq!(epoch_pda.registered_weight, expected_registered_weight);
50    assert_eq!(epoch_pda.total_work, 0);
51    assert_eq!(epoch_pda.protocol_config, protocol_config_pda.config);
52    assert_eq!(epoch_pda.epoch, epoch);
53}
54/// Helper function to fetch the forester epoch and epoch account to assert diff
55/// after transaction.
56pub async fn fetch_epoch_and_forester_pdas<R: RpcConnection>(
57    rpc: &mut R,
58    forester_epoch_pda: &Pubkey,
59    epoch_pda: &Pubkey,
60) -> (ForesterEpochPda, EpochPda) {
61    let forester_epoch_pda = rpc
62        .get_anchor_account::<ForesterEpochPda>(forester_epoch_pda)
63        .await
64        .unwrap()
65        .unwrap();
66    println!("forester_epoch_pda: {:?}", forester_epoch_pda);
67    let epoch_pda = rpc
68        .get_anchor_account::<EpochPda>(epoch_pda)
69        .await
70        .unwrap()
71        .unwrap();
72    println!("epoch_pda: {:?}", epoch_pda);
73
74    (forester_epoch_pda, epoch_pda)
75}
76
77/// Asserts:
78/// 1. ForesterEpochPda has reported work
79/// 2. EpochPda has updated total work by forester work counter
80pub async fn assert_report_work<R: RpcConnection>(
81    rpc: &mut R,
82    forester_epoch_pda_pubkey: &Pubkey,
83    epoch_pda_pubkey: &Pubkey,
84    mut pre_forester_epoch_pda: ForesterEpochPda,
85    mut pre_epoch_pda: EpochPda,
86) {
87    let forester_epoch_pda = rpc
88        .get_anchor_account::<ForesterEpochPda>(forester_epoch_pda_pubkey)
89        .await
90        .unwrap()
91        .unwrap();
92    pre_forester_epoch_pda.has_reported_work = true;
93    assert_eq!(forester_epoch_pda, pre_forester_epoch_pda);
94    let epoch_pda = rpc
95        .get_anchor_account::<EpochPda>(epoch_pda_pubkey)
96        .await
97        .unwrap()
98        .unwrap();
99    pre_epoch_pda.total_work += forester_epoch_pda.work_counter;
100    assert_eq!(epoch_pda, pre_epoch_pda);
101}
102
103/// Asserts the correct creation of a ForesterEpochPda.
104pub async fn assert_registered_forester_pda<R: RpcConnection>(
105    rpc: &mut R,
106    forester_epoch_pda_pubkey: &Pubkey,
107    forester_derivation_pubkey: &Pubkey,
108    epoch: u64,
109) {
110    let (forester_pda_pubkey, _) = get_forester_pda(forester_derivation_pubkey);
111
112    let epoch_pda_pubkey = get_epoch_pda_address(epoch);
113    let epoch_pda = rpc
114        .get_anchor_account::<EpochPda>(&epoch_pda_pubkey)
115        .await
116        .unwrap()
117        .unwrap();
118    let forester_pda = rpc
119        .get_anchor_account::<ForesterPda>(&forester_pda_pubkey)
120        .await
121        .unwrap()
122        .unwrap();
123    let epoch_active_phase_start_slot = epoch_pda.protocol_config.genesis_slot
124        + epoch_pda.protocol_config.registration_phase_length
125        + epoch_pda.epoch * epoch_pda.protocol_config.active_phase_length;
126    let expected_forester_epoch_pda = ForesterEpochPda {
127        authority: forester_pda.authority,
128        config: forester_pda.config,
129        epoch: epoch_pda.epoch,
130        weight: forester_pda.active_weight,
131        work_counter: 0,
132        has_reported_work: false,
133        forester_index: epoch_pda.registered_weight - forester_pda.active_weight,
134        total_epoch_weight: None,
135        epoch_active_phase_start_slot,
136        protocol_config: epoch_pda.protocol_config,
137        finalize_counter: 0,
138    };
139    let forester_epoch_pda = rpc
140        .get_anchor_account::<ForesterEpochPda>(forester_epoch_pda_pubkey)
141        .await
142        .unwrap()
143        .unwrap();
144    assert_eq!(forester_epoch_pda, expected_forester_epoch_pda);
145}