1
1
#![ allow( clippy:: await_holding_refcell_ref) ]
2
- use anchor_lang:: {
3
- solana_program:: { instruction:: Instruction , slot_history:: SlotHistory } ,
4
- InstructionData , ToAccountMetas ,
2
+ use {
3
+ anchor_lang:: {
4
+ solana_program:: { instruction:: Instruction , slot_history:: SlotHistory } ,
5
+ InstructionData , ToAccountMetas ,
6
+ } ,
7
+ solana_program_test:: * ,
8
+ solana_sdk:: {
9
+ clock:: Clock , compute_budget:: ComputeBudgetInstruction , signer:: Signer ,
10
+ transaction:: Transaction ,
11
+ } ,
12
+ tests:: fixtures:: TestFixture ,
13
+ validator_history:: ClusterHistory ,
5
14
} ;
6
- use solana_program_test:: * ;
7
- use solana_sdk:: {
8
- clock:: Clock , compute_budget:: ComputeBudgetInstruction , signer:: Signer ,
9
- transaction:: Transaction ,
10
- } ;
11
- use tests:: fixtures:: TestFixture ;
12
- use validator_history:: ClusterHistory ;
15
+
16
+ const MS_PER_SLOT : u64 = 400 ;
17
+
18
+ fn create_copy_cluster_history_transaction ( fixture : & TestFixture ) -> Transaction {
19
+ let instruction = Instruction {
20
+ program_id : validator_history:: id ( ) ,
21
+ data : validator_history:: instruction:: CopyClusterInfo { } . data ( ) ,
22
+ accounts : validator_history:: accounts:: CopyClusterInfo {
23
+ cluster_history_account : fixture. cluster_history_account ,
24
+ slot_history : anchor_lang:: solana_program:: sysvar:: slot_history:: id ( ) ,
25
+ signer : fixture. keypair . pubkey ( ) ,
26
+ }
27
+ . to_account_metas ( None ) ,
28
+ } ;
29
+ let heap_request_ix = ComputeBudgetInstruction :: request_heap_frame ( 256 * 1024 ) ;
30
+ let compute_budget_ix = ComputeBudgetInstruction :: set_compute_unit_limit ( 300_000 ) ;
31
+
32
+ Transaction :: new_signed_with_payer (
33
+ & [ heap_request_ix, compute_budget_ix, instruction] ,
34
+ Some ( & fixture. keypair . pubkey ( ) ) ,
35
+ & [ & fixture. keypair ] ,
36
+ fixture. ctx . borrow ( ) . last_blockhash ,
37
+ )
38
+ }
13
39
14
40
#[ tokio:: test]
15
41
async fn test_copy_cluster_info ( ) {
@@ -32,25 +58,7 @@ async fn test_copy_cluster_info() {
32
58
slot_history. add ( latest_slot + 1 ) ;
33
59
34
60
// Submit instruction
35
- let instruction = Instruction {
36
- program_id : validator_history:: id ( ) ,
37
- data : validator_history:: instruction:: CopyClusterInfo { } . data ( ) ,
38
- accounts : validator_history:: accounts:: CopyClusterInfo {
39
- cluster_history_account : fixture. cluster_history_account ,
40
- slot_history : anchor_lang:: solana_program:: sysvar:: slot_history:: id ( ) ,
41
- signer : fixture. keypair . pubkey ( ) ,
42
- }
43
- . to_account_metas ( None ) ,
44
- } ;
45
- let heap_request_ix = ComputeBudgetInstruction :: request_heap_frame ( 256 * 1024 ) ;
46
- let compute_budget_ix = ComputeBudgetInstruction :: set_compute_unit_limit ( 300_000 ) ;
47
-
48
- let transaction = Transaction :: new_signed_with_payer (
49
- & [ heap_request_ix, compute_budget_ix, instruction] ,
50
- Some ( & fixture. keypair . pubkey ( ) ) ,
51
- & [ & fixture. keypair ] ,
52
- ctx. borrow ( ) . last_blockhash ,
53
- ) ;
61
+ let transaction = create_copy_cluster_history_transaction ( & fixture) ;
54
62
55
63
ctx. borrow_mut ( ) . set_sysvar ( & slot_history) ;
56
64
fixture. submit_transaction_assert_success ( transaction) . await ;
@@ -74,3 +82,41 @@ async fn test_copy_cluster_info() {
74
82
assert ! ( account. history. arr[ 1 ] . total_blocks == 2 ) ;
75
83
assert_eq ! ( account. cluster_history_last_update_slot, latest_slot)
76
84
}
85
+
86
+ #[ tokio:: test]
87
+ async fn test_start_epoch_timestamp ( ) {
88
+ // Initialize
89
+ let fixture = TestFixture :: new ( ) . await ;
90
+ let ctx = & fixture. ctx ;
91
+ fixture. initialize_config ( ) . await ;
92
+ fixture. initialize_cluster_history_account ( ) . await ;
93
+
94
+ // Set SlotHistory sysvar
95
+ let slot_history = SlotHistory :: default ( ) ;
96
+ ctx. borrow_mut ( ) . set_sysvar ( & slot_history) ;
97
+
98
+ // Submit epoch 0 instruction
99
+ let transaction = create_copy_cluster_history_transaction ( & fixture) ;
100
+ fixture. submit_transaction_assert_success ( transaction) . await ;
101
+
102
+ // Change epoch and set clock timestamps in the future
103
+ fixture. advance_num_epochs ( 1 ) . await ;
104
+ let dif_slots = fixture. advance_clock ( 1 , MS_PER_SLOT ) . await ;
105
+
106
+ // Submit epoch 1 instruction
107
+ let transaction = create_copy_cluster_history_transaction ( & fixture) ;
108
+ fixture. submit_transaction_assert_success ( transaction) . await ;
109
+
110
+ let account: ClusterHistory = fixture
111
+ . load_and_deserialize ( & fixture. cluster_history_account )
112
+ . await ;
113
+
114
+ assert_eq ! ( account. history. arr[ 0 ] . epoch, 0 ) ;
115
+ assert_eq ! ( account. history. arr[ 1 ] . epoch, 1 ) ;
116
+ assert_ne ! ( account. history. arr[ 0 ] . epoch_start_timestamp, u32 :: MAX ) ;
117
+ assert_ne ! ( account. history. arr[ 1 ] . epoch_start_timestamp, u32 :: MAX ) ;
118
+ assert_eq ! (
119
+ account. history. arr[ 0 ] . epoch_start_timestamp,
120
+ account. history. arr[ 1 ] . epoch_start_timestamp - ( dif_slots * MS_PER_SLOT ) as u32
121
+ ) ;
122
+ }
0 commit comments