Line data Source code
1 : #include "fd_sysvar_epoch_schedule.h"
2 : #include "fd_sysvar.h"
3 : #include "../fd_system_ids.h"
4 : #include "../../accdb/fd_accdb_sync.h"
5 :
6 : fd_epoch_schedule_t *
7 : fd_epoch_schedule_derive( fd_epoch_schedule_t * schedule,
8 : ulong epoch_len,
9 : ulong leader_schedule_slot_offset,
10 12111 : int warmup ) {
11 :
12 12111 : if( FD_UNLIKELY( epoch_len < FD_EPOCH_LEN_MIN ) ) {
13 6 : FD_LOG_WARNING(( "epoch_len too small" ));
14 6 : return NULL;
15 6 : }
16 :
17 12105 : *schedule = (fd_epoch_schedule_t) {
18 12105 : .slots_per_epoch = epoch_len,
19 12105 : .leader_schedule_slot_offset = leader_schedule_slot_offset,
20 12105 : .warmup = !!warmup
21 12105 : };
22 :
23 12105 : if( warmup ) {
24 6054 : ulong ceil_log2_epoch = (ulong)fd_ulong_find_msb( epoch_len-1UL ) + 1UL;
25 6054 : ulong ceil_log2_len_min = (ulong)fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
26 :
27 6054 : schedule->first_normal_epoch = fd_ulong_sat_sub( ceil_log2_epoch, ceil_log2_len_min );
28 6054 : schedule->first_normal_slot = (1UL << ceil_log2_epoch) - FD_EPOCH_LEN_MIN;
29 6054 : }
30 :
31 12105 : return schedule;
32 12111 : }
33 :
34 : void
35 : fd_sysvar_epoch_schedule_write( fd_bank_t * bank,
36 : fd_accdb_user_t * accdb,
37 : fd_funk_txn_xid_t const * xid,
38 : fd_capture_ctx_t * capture_ctx,
39 18 : fd_epoch_schedule_t const * epoch_schedule ) {
40 :
41 18 : uchar enc[ FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ ] = {0};
42 :
43 18 : fd_bincode_encode_ctx_t ctx = {
44 18 : .data = enc,
45 18 : .dataend = enc + FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ
46 18 : };
47 18 : if( FD_UNLIKELY( fd_epoch_schedule_encode( epoch_schedule, &ctx ) ) ) {
48 0 : FD_LOG_ERR(( "fd_epoch_schedule_encode failed" ));
49 0 : }
50 :
51 18 : fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_epoch_schedule_id, enc, FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ );
52 18 : }
53 :
54 : fd_epoch_schedule_t *
55 : fd_sysvar_epoch_schedule_read( fd_accdb_user_t * accdb,
56 : fd_funk_txn_xid_t const * xid,
57 0 : fd_epoch_schedule_t * out ) {
58 0 : fd_accdb_ro_t ro[1];
59 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, &fd_sysvar_epoch_schedule_id ) ) ) {
60 0 : return NULL;
61 0 : }
62 :
63 : /* This check is needed as a quirk of the fuzzer. If a sysvar account
64 : exists in the accounts database, but doesn't have any lamports,
65 : this means that the account does not exist. This wouldn't happen
66 : in a real execution environment. */
67 0 : if( FD_UNLIKELY( fd_accdb_ref_lamports( ro )==0UL ) ) {
68 0 : fd_accdb_close_ro( accdb, ro );
69 0 : return NULL;
70 0 : }
71 :
72 0 : fd_epoch_schedule_t * rc = fd_bincode_decode_static(
73 0 : epoch_schedule, out,
74 0 : fd_accdb_ref_data_const( ro ),
75 0 : fd_accdb_ref_data_sz ( ro ) );
76 0 : fd_accdb_close_ro( accdb, ro );
77 0 : return rc;
78 0 : }
79 :
80 : void
81 : fd_sysvar_epoch_schedule_init( fd_bank_t * bank,
82 : fd_accdb_user_t * accdb,
83 : fd_funk_txn_xid_t const * xid,
84 0 : fd_capture_ctx_t * capture_ctx ) {
85 0 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
86 0 : fd_sysvar_epoch_schedule_write( bank, accdb, xid, capture_ctx, epoch_schedule );
87 0 : }
88 :
89 : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L105 */
90 :
91 : ulong
92 : fd_epoch_slot_cnt( fd_epoch_schedule_t const * schedule,
93 324 : ulong epoch ) {
94 :
95 324 : if( FD_UNLIKELY( epoch < schedule->first_normal_epoch ) ) {
96 63 : ulong exp = fd_ulong_sat_add( epoch, (ulong)fd_ulong_find_lsb( FD_EPOCH_LEN_MIN ) );
97 63 : return ( exp<64UL ? 1UL<<exp : ULONG_MAX ); // saturating_pow
98 63 : }
99 :
100 261 : return schedule->slots_per_epoch;
101 324 : }
102 :
103 : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L170 */
104 :
105 : ulong
106 : fd_epoch_slot0( fd_epoch_schedule_t const * schedule,
107 1047 : ulong epoch ) {
108 1047 : if( FD_UNLIKELY( epoch <= schedule->first_normal_epoch ) ) {
109 657 : ulong power = fd_ulong_if( epoch<64UL, 1UL<<epoch, ULONG_MAX );
110 657 : return fd_ulong_sat_mul( power-1UL, FD_EPOCH_LEN_MIN );
111 657 : }
112 :
113 390 : return fd_ulong_sat_add(
114 390 : fd_ulong_sat_mul(
115 390 : fd_ulong_sat_sub(
116 390 : epoch,
117 390 : schedule->first_normal_epoch),
118 390 : schedule->slots_per_epoch),
119 390 : schedule->first_normal_slot);
120 1047 : }
121 :
122 : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L140 */
123 :
124 : ulong
125 : fd_slot_to_epoch( fd_epoch_schedule_t const * schedule,
126 : ulong slot,
127 1323096 : ulong * out_offset_opt ) {
128 1323096 : ulong epoch;
129 1323096 : ulong offset;
130 :
131 1323096 : if( FD_UNLIKELY( slot < schedule->first_normal_slot ) ) {
132 : /* step0 = ceil(log2(FD_EPOCH_LEN_MIN))
133 : step1 = ceil(log2(FD_EPOCH_LEN_MIN + slot + 1))
134 : epoch = step1 - step0 - 1 */
135 :
136 23718 : ulong s0 = FD_EPOCH_LEN_MIN + slot + 1UL;
137 : /* Invariant: s0 > 1UL */
138 : /* Invariant: s0 > FD_EPOCH_LEN_MIN */
139 :
140 : /* Find lowest exp where (2^exp) >= s0
141 : (Only valid for s0 > 1UL and FD_EPOCH_LEN_MIN > 1UL) */
142 23718 : int exp = fd_ulong_find_msb( s0-1UL ) + 1;
143 23718 : int min_exp = fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
144 23718 : epoch = (ulong)( exp - min_exp - 1 );
145 23718 : ulong epoch_len = 1UL<<( epoch + (ulong)fd_uint_find_lsb( FD_EPOCH_LEN_MIN ) );
146 23718 : offset = slot - ( epoch_len - FD_EPOCH_LEN_MIN );
147 1299378 : } else {
148 : // FD_LOG_WARNING(("First %lu slots per epoch %lu", schedule->first_normal_slot, schedule->slots_per_epoch));
149 1299378 : if( FD_UNLIKELY( schedule->slots_per_epoch == 0UL ) ) {
150 12 : FD_LOG_WARNING(( "zero slots_per_epoch returning first_normal_epoch %lu", schedule->first_normal_epoch ));
151 12 : if( out_offset_opt ) *out_offset_opt = 0UL;
152 12 : return schedule->first_normal_epoch;
153 12 : }
154 1299366 : ulong n_slot = slot - schedule->first_normal_slot;
155 1299366 : ulong n_epoch = n_slot / schedule->slots_per_epoch;
156 1299366 : epoch = schedule->first_normal_epoch + n_epoch;
157 1299366 : offset = n_slot % schedule->slots_per_epoch;
158 1299366 : }
159 :
160 1323084 : ulong dummy_out;
161 1323084 : ulong * out_offset = out_offset_opt ? out_offset_opt : &dummy_out;
162 :
163 1323084 : *out_offset = offset;
164 1323084 : return epoch;
165 1323096 : }
166 :
167 : /* https://docs.rs/solana-epoch-schedule/2.2.1/src/solana_epoch_schedule/lib.rs.html#136-151 */
168 :
169 : ulong
170 : fd_slot_to_leader_schedule_epoch( fd_epoch_schedule_t const * schedule,
171 309 : ulong slot ) {
172 :
173 309 : if( FD_UNLIKELY( slot<schedule->first_normal_slot ) ) {
174 3 : return fd_slot_to_epoch( schedule, slot, NULL ) + 1UL;
175 3 : }
176 :
177 306 : if( FD_UNLIKELY( schedule->slots_per_epoch == 0UL ) ) {
178 12 : FD_LOG_WARNING(( "zero slots_per_epoch returning first_normal_epoch %lu", schedule->first_normal_epoch ));
179 12 : return schedule->first_normal_epoch;
180 12 : }
181 :
182 294 : ulong new_slots_since_first_normal_slot =
183 294 : slot - schedule->first_normal_slot;
184 294 : ulong new_first_normal_leader_schedule_slot =
185 294 : new_slots_since_first_normal_slot + schedule->leader_schedule_slot_offset;
186 294 : ulong new_epochs_since_first_normal_leader_schedule =
187 294 : new_first_normal_leader_schedule_slot / schedule->slots_per_epoch;
188 :
189 294 : return schedule->first_normal_epoch + new_epochs_since_first_normal_leader_schedule;
190 306 : }
|