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