Line data Source code
1 : #include "fd_sysvar.h"
2 : #include "fd_sysvar_clock.h"
3 : #include "fd_sysvar_epoch_schedule.h"
4 : #include "../fd_runtime_stack.h"
5 : #include "../fd_system_ids.h"
6 : #include "../program/fd_program_util.h"
7 : #include "../../accdb/fd_accdb_sync.h"
8 :
9 : /* Syvar Clock Possible Values:
10 : slot:
11 : [0, ULONG_MAX]
12 :
13 : epoch:
14 : [0, slot/432000UL]
15 :
16 : epoch_start_timestamp:
17 : [0, ULONG_MAX]
18 :
19 : unix_timestamp:
20 : This value is bounded by the slot distance from the
21 : epoch_start_timestamp.
22 : The protocol allows for a maximum drift (either fast or slow) from the
23 : start of the epoch's timestamp. The expected time is called the PoH
24 : offset. This offset is calculated by (epoch_start_timestamp + slots
25 : since epoch * slot_duration). The drift is then bounded by the
26 : max_allowable_drift_{slow,fast}. The stake weighted offset can be
27 : 150% more than the PoH offset and 25% less than the PoH offset.
28 : So, the bounds for the unix_timestamp can be calculated by:
29 : upper bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 2.5
30 : lower bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 0.75
31 :
32 : leader_schedule_epoch:
33 : This is the value of the epoch used for the leader schedule. It is
34 : computed based on the values of the epoch schedule (first_normal_slot,
35 : leader_schedule_slot_offset, slots_per_epoch). It is always equal to
36 : ((slot - first_normal_slot) + leader_schedule_slot_offset) / schedule->slots_per_epoch
37 : */
38 :
39 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L14 */
40 0 : #define MAX_ALLOWABLE_DRIFT_FAST_PERCENT ( 25U )
41 :
42 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L15 */
43 0 : #define MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ( 150U )
44 :
45 : /* Do all intermediate calculations at nanosecond precision, to mirror
46 : Solana's behavior. */
47 45 : #define NS_IN_S ((long)1e9)
48 :
49 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2110-L2117 */
50 : static inline long
51 45 : unix_timestamp_from_genesis( fd_bank_t * bank ) {
52 : /* TODO: genesis_creation_time needs to be a long in the bank. */
53 45 : return fd_long_sat_add(
54 45 : (long)fd_bank_genesis_creation_time_get( bank ),
55 45 : (long)( fd_uint128_sat_mul( fd_bank_slot_get( bank ), fd_bank_ns_per_slot_get( bank ).ud ) / NS_IN_S ) );
56 45 : }
57 :
58 : void
59 : fd_sysvar_clock_write( fd_bank_t * bank,
60 : fd_accdb_user_t * accdb,
61 : fd_funk_txn_xid_t const * xid,
62 : fd_capture_ctx_t * capture_ctx,
63 108 : fd_sol_sysvar_clock_t * clock ) {
64 108 : uchar enc[ sizeof(fd_sol_sysvar_clock_t) ];
65 108 : fd_bincode_encode_ctx_t ctx = {
66 108 : .data = enc,
67 108 : .dataend = enc + sizeof(fd_sol_sysvar_clock_t),
68 108 : };
69 108 : if( FD_UNLIKELY( fd_sol_sysvar_clock_encode( clock, &ctx ) ) ) {
70 0 : FD_LOG_ERR(( "fd_sol_sysvar_clock_encode failed" ));
71 0 : }
72 :
73 108 : fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_clock_id, enc, sizeof(fd_sol_sysvar_clock_t) );
74 108 : }
75 :
76 : fd_sol_sysvar_clock_t *
77 : fd_sysvar_clock_read( fd_accdb_user_t * accdb,
78 : fd_funk_txn_xid_t const * xid,
79 63 : fd_sol_sysvar_clock_t * clock ) {
80 63 : fd_accdb_ro_t ro[1];
81 63 : if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, &fd_sysvar_clock_id ) ) ) {
82 0 : return NULL;
83 0 : }
84 :
85 : /* This check is needed as a quirk of the fuzzer. If a sysvar account
86 : exists in the accounts database, but doesn't have any lamports,
87 : this means that the account does not exist. This wouldn't happen
88 : in a real execution environment. */
89 63 : if( FD_UNLIKELY( fd_accdb_ref_lamports( ro )==0UL ) ) {
90 0 : fd_accdb_close_ro( accdb, ro );
91 0 : return NULL;
92 0 : }
93 :
94 63 : fd_sol_sysvar_clock_t * res = fd_bincode_decode_static(
95 63 : sol_sysvar_clock, clock,
96 63 : fd_accdb_ref_data_const( ro ),
97 63 : fd_accdb_ref_data_sz ( ro ),
98 63 : NULL );
99 63 : fd_accdb_close_ro( accdb, ro );
100 63 : return res;
101 63 : }
102 :
103 : void
104 : fd_sysvar_clock_init( fd_bank_t * bank,
105 : fd_accdb_user_t * accdb,
106 : fd_funk_txn_xid_t const * xid,
107 45 : fd_capture_ctx_t * capture_ctx ) {
108 45 : long timestamp = unix_timestamp_from_genesis( bank );
109 :
110 45 : fd_sol_sysvar_clock_t clock = {
111 45 : .slot = fd_bank_slot_get( bank ),
112 45 : .epoch = 0,
113 45 : .epoch_start_timestamp = timestamp,
114 45 : .leader_schedule_epoch = 1,
115 45 : .unix_timestamp = timestamp,
116 45 : };
117 45 : fd_sysvar_clock_write( bank, accdb, xid, capture_ctx, &clock );
118 45 : }
119 :
120 : #define SORT_NAME sort_stake_ts
121 0 : #define SORT_KEY_T ts_est_ele_t
122 0 : #define SORT_BEFORE(a,b) ( (a).timestamp < (b).timestamp )
123 : #include "../../../util/tmpl/fd_sort.c"
124 :
125 : /* get_timestamp_estimate calculates a timestamp estimate. Does not
126 : modify the slot context. Walks all cached vote accounts (from the
127 : "bank") and calculates a unix timestamp estimate. Returns the
128 : timestamp estimate. spad is used for scratch allocations (allocates
129 : a treap of size FD_SYSVAR_CLOCK_STAKE_WEIGHTS_MAX). Crashes the
130 : process with FD_LOG_ERR on failure (e.g. too many vote accounts).
131 :
132 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2563-L2601 */
133 : long
134 : get_timestamp_estimate( fd_bank_t * bank,
135 : fd_sol_sysvar_clock_t * clock,
136 63 : fd_runtime_stack_t * runtime_stack ) {
137 63 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
138 63 : ulong slot_duration = fd_bank_ns_per_slot_get( bank ).ul[0];
139 63 : ulong current_slot = fd_bank_slot_get( bank );
140 :
141 63 : ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
142 63 : ulong ts_ele_cnt = 0UL;
143 :
144 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L41 */
145 63 : uint128 total_stake = 0UL;
146 :
147 : /* A timestamp estimate is calculated at every slot using the most
148 : recent vote states of voting validators. This estimated is based on
149 : a stake weighted median using the stake as of the end of epoch E-2
150 : if we are currently in epoch E. We do not count vote accounts that
151 : have not voted in an epoch's worth of slots (432k). */
152 63 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
153 :
154 63 : FD_TEST( fd_vote_states_cnt( vote_states )<=FD_RUNTIME_MAX_VOTE_ACCOUNTS );
155 :
156 63 : fd_vote_states_iter_t iter_[1];
157 63 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
158 63 : !fd_vote_states_iter_done( iter );
159 63 : fd_vote_states_iter_next( iter ) ) {
160 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
161 :
162 : /* https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2445 */
163 0 : ulong slot_delta;
164 0 : int err = fd_ulong_checked_sub( current_slot, vote_state->last_vote_slot, &slot_delta );
165 0 : if( FD_UNLIKELY( err ) ) {
166 : /* Don't count vote accounts with a last vote slot that is greater
167 : than the current slot. */
168 0 : continue;
169 0 : }
170 :
171 0 : if( FD_UNLIKELY( !vote_state->stake_t_2 ) ) {
172 : /* Don't count vote accounts that didn't have stake at the end of
173 : epoch E-2. */
174 0 : continue;
175 0 : }
176 :
177 : /* Don't count vote accounts that haven't voted in the past 432k
178 : slots (length of an epoch).
179 : https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2446-L2447 */
180 0 : if( FD_UNLIKELY( slot_delta>epoch_schedule->slots_per_epoch ) ) {
181 0 : continue;
182 0 : }
183 :
184 : /* Calculate the timestamp estimate by taking the last vote
185 : timestamp and adding the estimated time since the last vote
186 : (delta from last vote slot to current slot * slot duration).
187 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L44-L45 */
188 0 : ulong offset = fd_ulong_sat_mul( slot_duration, slot_delta );
189 0 : long estimate = vote_state->last_vote_timestamp + (long)(offset / NS_IN_S);
190 :
191 : /* For each timestamp, accumulate the stake from E-2. If the entry
192 : for the timestamp doesn't exist yet, insert it. Otherwise,
193 : update the existing entry.
194 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L46-L53 */
195 0 : ts_eles[ ts_ele_cnt ] = (ts_est_ele_t){
196 0 : .timestamp = estimate,
197 0 : .stake = { .ud=vote_state->stake_t_2 },
198 0 : };
199 0 : ts_ele_cnt++;
200 :
201 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L54 */
202 0 : total_stake += vote_state->stake_t_2;
203 0 : }
204 63 : fd_bank_vote_states_end_locking_query( bank );
205 :
206 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L56-L58 */
207 63 : if( FD_UNLIKELY( total_stake==0UL ) ) {
208 63 : return 0L;
209 63 : }
210 :
211 0 : sort_stake_ts_inplace( ts_eles, ts_ele_cnt );
212 :
213 : /* Populate estimate with the stake-weighted median timestamp.
214 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L59-L68 */
215 0 : uint128 stake_accumulator = 0;
216 0 : long estimate = 0L;
217 0 : for( ulong i=0UL; i<ts_ele_cnt; i++ ) {
218 0 : stake_accumulator = fd_uint128_sat_add( stake_accumulator, ts_eles[i].stake.ud );
219 0 : if( stake_accumulator>(total_stake/2UL) ) {
220 0 : estimate = ts_eles[ i ].timestamp;
221 0 : break;
222 0 : }
223 0 : }
224 :
225 0 : int const fix_estimate_into_u64 = FD_FEATURE_ACTIVE_BANK( bank, warp_timestamp_again );
226 :
227 : /* Bound estimate by `max_allowable_drift` since the start of the epoch
228 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L69-L99 */
229 0 : ulong epoch_start_slot = fd_epoch_slot0( epoch_schedule, clock->epoch );
230 0 : long epoch_start_timestamp = clock->epoch_start_timestamp;
231 :
232 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L71-L72 */
233 0 : ulong poh_estimate_offset = fd_ulong_sat_mul( slot_duration, fd_ulong_sat_sub( current_slot, epoch_start_slot ) );
234 :
235 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L73-L77 */
236 0 : ulong estimate_offset;
237 0 : if( fix_estimate_into_u64 ) {
238 0 : estimate_offset = fd_ulong_sat_mul( NS_IN_S, fd_ulong_sat_sub( (ulong)estimate, (ulong)epoch_start_timestamp ) );
239 0 : } else {
240 0 : estimate_offset = fd_ulong_sat_mul( NS_IN_S, (ulong)fd_long_sat_sub( estimate, epoch_start_timestamp ) );
241 0 : }
242 :
243 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L78-L81 */
244 0 : ulong max_allowable_drift_fast = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_FAST_PERCENT ) / 100UL;
245 0 : ulong max_allowable_drift_slow = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ) / 100UL;
246 :
247 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L82-L98 */
248 0 : if( estimate_offset>poh_estimate_offset && fd_ulong_sat_sub( estimate_offset, poh_estimate_offset )>max_allowable_drift_slow ) {
249 0 : estimate = fd_long_sat_add(
250 0 : epoch_start_timestamp,
251 0 : fd_long_sat_add( (long)poh_estimate_offset / NS_IN_S, (long)max_allowable_drift_slow / NS_IN_S ) );
252 0 : } else if( estimate_offset<poh_estimate_offset && fd_ulong_sat_sub( poh_estimate_offset, estimate_offset )>max_allowable_drift_fast ) {
253 0 : estimate = fd_long_sat_sub(
254 0 : fd_long_sat_add( epoch_start_timestamp, (long)poh_estimate_offset / NS_IN_S ),
255 0 : (long)max_allowable_drift_fast / NS_IN_S );
256 0 : }
257 :
258 0 : return estimate;
259 63 : }
260 :
261 : /* TODO: This function should be called from genesis bootup as well with
262 : parent_epoch = NULL
263 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2158-L2215 */
264 : void
265 : fd_sysvar_clock_update( fd_bank_t * bank,
266 : fd_accdb_user_t * accdb,
267 : fd_funk_txn_xid_t const * xid,
268 : fd_capture_ctx_t * capture_ctx,
269 : fd_runtime_stack_t * runtime_stack,
270 63 : ulong const * parent_epoch ) {
271 63 : fd_sol_sysvar_clock_t clock_[1];
272 63 : fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, xid, clock_ );
273 63 : if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
274 :
275 63 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
276 63 : ulong current_slot = fd_bank_slot_get( bank );
277 63 : ulong current_epoch = fd_slot_to_epoch( epoch_schedule, current_slot, NULL );
278 :
279 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2159 */
280 63 : long unix_timestamp = clock->unix_timestamp;
281 :
282 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2175 */
283 63 : long ancestor_timestamp = clock->unix_timestamp;
284 :
285 : /* TODO: Are we handling slot 0 correctly?
286 : https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2176-L2183 */
287 63 : long timestamp_estimate = get_timestamp_estimate( bank, clock, runtime_stack );
288 :
289 : /* If the timestamp was successfully calculated, use it. It not keep the old one. */
290 63 : if( FD_LIKELY( timestamp_estimate!=0L ) ) {
291 0 : unix_timestamp = timestamp_estimate;
292 :
293 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2180-L2182 */
294 0 : if( timestamp_estimate<ancestor_timestamp ) {
295 0 : unix_timestamp = ancestor_timestamp;
296 0 : }
297 0 : }
298 :
299 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2191-L2197 */
300 63 : long epoch_start_timestamp = (parent_epoch!=NULL && *parent_epoch!=current_epoch) ?
301 45 : unix_timestamp :
302 63 : clock->epoch_start_timestamp;
303 :
304 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2198-L2201 */
305 63 : if( FD_UNLIKELY( current_slot==0UL ) ) {
306 0 : long timestamp_from_genesis = unix_timestamp_from_genesis( bank );
307 0 : unix_timestamp = timestamp_from_genesis;
308 0 : epoch_start_timestamp = timestamp_from_genesis;
309 0 : }
310 :
311 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2202-L2208 */
312 63 : *clock = (fd_sol_sysvar_clock_t){
313 63 : .slot = current_slot,
314 63 : .epoch_start_timestamp = epoch_start_timestamp,
315 63 : .epoch = current_epoch,
316 63 : .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, current_slot ),
317 63 : .unix_timestamp = unix_timestamp,
318 63 : };
319 :
320 : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2209-L2214 */
321 63 : fd_sysvar_clock_write( bank, accdb, xid, capture_ctx, clock );
322 63 : }
|