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 0 : int warmup ) {
11 :
12 0 : if( FD_UNLIKELY( epoch_len < FD_EPOCH_LEN_MIN ) ) {
13 0 : FD_LOG_WARNING(( "epoch_len too small" ));
14 0 : return NULL;
15 0 : }
16 :
17 0 : *schedule = (fd_epoch_schedule_t) {
18 0 : .slots_per_epoch = epoch_len,
19 0 : .leader_schedule_slot_offset = leader_schedule_slot_offset,
20 0 : .warmup = !!warmup
21 0 : };
22 :
23 0 : if( warmup ) {
24 0 : ulong ceil_log2_epoch = (ulong)fd_ulong_find_msb( epoch_len-1UL ) + 1UL;
25 0 : ulong ceil_log2_len_min = (ulong)fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
26 :
27 0 : schedule->first_normal_epoch = fd_ulong_sat_sub( ceil_log2_epoch, ceil_log2_len_min );
28 0 : schedule->first_normal_slot = (1UL << ceil_log2_epoch) - FD_EPOCH_LEN_MIN;
29 0 : }
30 :
31 0 : return schedule;
32 0 : }
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 0 : fd_epoch_schedule_t const * epoch_schedule ) {
40 :
41 0 : uchar enc[ FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ ] = {0};
42 :
43 0 : fd_bincode_encode_ctx_t ctx = {
44 0 : .data = enc,
45 0 : .dataend = enc + FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ
46 0 : };
47 0 : if( FD_UNLIKELY( fd_epoch_schedule_encode( epoch_schedule, &ctx ) ) ) {
48 0 : FD_LOG_ERR(( "fd_epoch_schedule_encode failed" ));
49 0 : }
50 :
51 0 : fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_epoch_schedule_id, enc, FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ );
52 0 : }
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 = fd_bank_epoch_schedule_query( bank );
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 901 : ulong epoch ) {
94 :
95 901 : if( FD_UNLIKELY( epoch < schedule->first_normal_epoch ) ) {
96 0 : ulong exp = fd_ulong_sat_add( epoch, (ulong)fd_ulong_find_lsb( FD_EPOCH_LEN_MIN ) );
97 0 : return ( exp<64UL ? 1UL<<exp : ULONG_MAX ); // saturating_pow
98 0 : }
99 :
100 901 : return schedule->slots_per_epoch;
101 901 : }
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 8007 : ulong epoch ) {
108 8007 : if( FD_UNLIKELY( epoch <= schedule->first_normal_epoch ) ) {
109 5480 : ulong power = fd_ulong_if( epoch<64UL, 1UL<<epoch, ULONG_MAX );
110 5480 : return fd_ulong_sat_mul( power-1UL, FD_EPOCH_LEN_MIN );
111 5480 : }
112 :
113 2527 : return fd_ulong_sat_add(
114 2527 : fd_ulong_sat_mul(
115 2527 : fd_ulong_sat_sub(
116 2527 : epoch,
117 2527 : schedule->first_normal_epoch),
118 2527 : schedule->slots_per_epoch),
119 2527 : schedule->first_normal_slot);
120 8007 : }
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 7952 : ulong * out_offset_opt ) {
128 7952 : ulong epoch;
129 7952 : ulong offset;
130 :
131 7952 : 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 3519 : 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 3519 : int exp = fd_ulong_find_msb( s0-1UL ) + 1;
143 3519 : int min_exp = fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
144 3519 : epoch = (ulong)( exp - min_exp - 1 );
145 3519 : ulong epoch_len = 1UL<<( epoch + (ulong)fd_uint_find_lsb( FD_EPOCH_LEN_MIN ) );
146 3519 : offset = slot - ( epoch_len - FD_EPOCH_LEN_MIN );
147 4433 : } else {
148 : // FD_LOG_WARNING(("First %lu slots per epoch %lu", schedule->first_normal_slot, schedule->slots_per_epoch));
149 4433 : if( FD_UNLIKELY( schedule->slots_per_epoch == 0UL ) ) {
150 0 : FD_LOG_WARNING(( "zero slots_per_epoch returning first_normal_epoch %lu", schedule->first_normal_epoch ));
151 0 : if( out_offset_opt ) *out_offset_opt = 0UL;
152 0 : return schedule->first_normal_epoch;
153 0 : }
154 4433 : ulong n_slot = slot - schedule->first_normal_slot;
155 4433 : ulong n_epoch = n_slot / schedule->slots_per_epoch;
156 4433 : epoch = schedule->first_normal_epoch + n_epoch;
157 4433 : offset = n_slot % schedule->slots_per_epoch;
158 4433 : }
159 :
160 7952 : ulong dummy_out;
161 7952 : ulong * out_offset = out_offset_opt ? out_offset_opt : &dummy_out;
162 :
163 7952 : *out_offset = offset;
164 7952 : return epoch;
165 7952 : }
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 299 : ulong slot ) {
172 :
173 299 : if( FD_UNLIKELY( slot<schedule->first_normal_slot ) ) {
174 0 : return fd_slot_to_epoch( schedule, slot, NULL ) + 1UL;
175 0 : }
176 :
177 299 : if( FD_UNLIKELY( schedule->slots_per_epoch == 0UL ) ) {
178 0 : FD_LOG_WARNING(( "zero slots_per_epoch returning first_normal_epoch %lu", schedule->first_normal_epoch ));
179 0 : return schedule->first_normal_epoch;
180 0 : }
181 :
182 299 : ulong new_slots_since_first_normal_slot =
183 299 : slot - schedule->first_normal_slot;
184 299 : ulong new_first_normal_leader_schedule_slot =
185 299 : new_slots_since_first_normal_slot + schedule->leader_schedule_slot_offset;
186 299 : ulong new_epochs_since_first_normal_leader_schedule =
187 299 : new_first_normal_leader_schedule_slot / schedule->slots_per_epoch;
188 :
189 299 : return schedule->first_normal_epoch + new_epochs_since_first_normal_leader_schedule;
190 299 : }
|