Line data Source code
1 : #include "fd_ssload.h"
2 :
3 : #include "../../../disco/genesis/fd_genesis_cluster.h"
4 : #include "../../../flamenco/runtime/fd_runtime_const.h"
5 : #include "../../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
6 : #include "fd_ssmsg.h"
7 :
8 : FD_STATIC_ASSERT( FD_HARD_FORKS_MAX==sizeof(((fd_snapshot_manifest_t *)0)->hard_forks)/sizeof(fd_hard_fork_t), hard_forks_max );
9 : FD_STATIC_ASSERT( FD_BLOCKHASHES_MAX==sizeof(((fd_snapshot_manifest_t *)0)->blockhashes)/sizeof(fd_snapshot_manifest_blockhash_t), blockhashes_max );
10 : FD_STATIC_ASSERT( FD_RUNTIME_MAX_SNAPSHOT_VOTE_ACCOUNTS==sizeof(((fd_snapshot_manifest_t *)0)->vote_accounts)/sizeof(fd_snapshot_manifest_vote_account_t), vote_accounts_max );
11 : FD_STATIC_ASSERT( FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN==sizeof(((fd_snapshot_manifest_t *)0)->epoch_stakes)/sizeof(fd_snapshot_manifest_epoch_stakes_t), epoch_stakes_len );
12 : FD_STATIC_ASSERT( FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS==sizeof(((fd_snapshot_manifest_epoch_stakes_t *)0)->vote_stakes)/sizeof(fd_snapshot_manifest_vote_stakes_t), epoch_vote_stakes_max );
13 : FD_STATIC_ASSERT( FD_EPOCH_CREDITS_MAX==sizeof(((fd_snapshot_manifest_vote_stakes_t *)0)->epoch_credits)/sizeof(epoch_credits_t), vote_stakes_epoch_credits_max );
14 :
15 : int
16 : fd_ssload_manifest_validate( fd_snapshot_manifest_t const * manifest,
17 : ulong max_vote_accounts,
18 0 : ulong max_stake_accounts ) {
19 :
20 0 : if( FD_UNLIKELY( max_vote_accounts!=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ||
21 0 : !max_stake_accounts ) ) {
22 0 : FD_LOG_WARNING(( "banks capacity mismatch: max_vote_accounts=%lu (expected %lu) max_stake_accounts=%lu (expected nonzero)",
23 0 : max_vote_accounts, FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS,
24 0 : max_stake_accounts ));
25 0 : return -1;
26 0 : }
27 :
28 0 : if( FD_UNLIKELY( !manifest->ticks_per_slot ) ) {
29 0 : FD_LOG_WARNING(( "corrupt snapshot: ticks_per_slot must be non-zero" ));
30 0 : return -1;
31 0 : }
32 :
33 : /* slots_per_epoch must be at least FD_EPOCH_LEN_MIN, so that
34 : fd_slot_to_epoch and related functions produce valid data.
35 : This check must come before any epoch computation. */
36 :
37 0 : if( FD_UNLIKELY( manifest->epoch_schedule_params.slots_per_epoch<FD_EPOCH_LEN_MIN ) ) {
38 0 : FD_LOG_WARNING(( "corrupt snapshot: slots_per_epoch %lu below minimum %lu",
39 0 : manifest->epoch_schedule_params.slots_per_epoch, FD_EPOCH_LEN_MIN ));
40 0 : return -1;
41 0 : }
42 :
43 0 : if( FD_UNLIKELY( manifest->epoch_schedule_params.slots_per_epoch>FD_RUNTIME_SLOTS_PER_EPOCH ) ) {
44 0 : FD_LOG_WARNING(( "unsupported snapshot: slots_per_epoch %lu above maximum %lu",
45 0 : manifest->epoch_schedule_params.slots_per_epoch, FD_RUNTIME_SLOTS_PER_EPOCH ));
46 0 : return -1;
47 0 : }
48 :
49 0 : if( FD_UNLIKELY( manifest->epoch_schedule_params.warmup>1 ) ) {
50 0 : FD_LOG_WARNING(( "corrupt snapshot: warmup %u is not boolean", (uint)manifest->epoch_schedule_params.warmup ));
51 0 : return -1;
52 0 : }
53 :
54 : /* Validate that the manifest's first_normal_{epoch,slot} are
55 : consistent with the derivation from slots_per_epoch and warmup. */
56 :
57 0 : fd_epoch_schedule_t derived;
58 0 : if( FD_UNLIKELY( !fd_epoch_schedule_derive( &derived,
59 0 : manifest->epoch_schedule_params.slots_per_epoch,
60 0 : manifest->epoch_schedule_params.leader_schedule_slot_offset,
61 0 : manifest->epoch_schedule_params.warmup ) ) ) {
62 0 : FD_LOG_WARNING(( "corrupt snapshot: fd_epoch_schedule_derive failed" ));
63 0 : return -1;
64 0 : }
65 0 : if( FD_UNLIKELY( derived.first_normal_epoch!=manifest->epoch_schedule_params.first_normal_epoch ) ) {
66 0 : FD_LOG_WARNING(( "corrupt snapshot: first_normal_epoch mismatch (manifest=%lu derived=%lu)",
67 0 : manifest->epoch_schedule_params.first_normal_epoch, derived.first_normal_epoch ));
68 0 : return -1;
69 0 : }
70 0 : if( FD_UNLIKELY( derived.first_normal_slot!=manifest->epoch_schedule_params.first_normal_slot ) ) {
71 0 : FD_LOG_WARNING(( "corrupt snapshot: first_normal_slot mismatch (manifest=%lu derived=%lu)",
72 0 : manifest->epoch_schedule_params.first_normal_slot, derived.first_normal_slot ));
73 0 : return -1;
74 0 : }
75 :
76 : /* Blockhash queue structural validation */
77 :
78 0 : ulong const age_cnt = manifest->blockhashes_len;
79 0 : fd_snapshot_manifest_blockhash_t const * ages = manifest->blockhashes;
80 :
81 0 : if( FD_UNLIKELY( !age_cnt || age_cnt>FD_BLOCKHASHES_MAX ) ) {
82 0 : FD_LOG_WARNING(( "corrupt snapshot: invalid blockhash age count %lu (max %lu)", age_cnt, FD_BLOCKHASHES_MAX ));
83 0 : return -1;
84 0 : }
85 :
86 : /* hash_index values may skip (see fd_blockhashes.h), so the only
87 : structural requirements are that no two entries share a hash_index
88 : and that the newest index leaves room for the runtime to register
89 : the next one. */
90 :
91 0 : for( ulong i=0UL; i<age_cnt; i++ ) {
92 0 : if( FD_UNLIKELY( ages[ i ].hash_index==ULONG_MAX ) ) {
93 0 : FD_LOG_WARNING(( "corrupt snapshot: blockhash queue sequence number wraparound (hash_index=%lu)", ages[ i ].hash_index ));
94 0 : return -1;
95 0 : }
96 0 : for( ulong j=0UL; j<i; j++ ) {
97 0 : if( FD_UNLIKELY( ages[ i ].hash_index==ages[ j ].hash_index ) ) {
98 0 : FD_LOG_WARNING(( "corrupt snapshot: duplicate blockhash queue hash_index=%lu", ages[ i ].hash_index ));
99 0 : return -1;
100 0 : }
101 0 : }
102 0 : }
103 :
104 : /* Array bounds checks, reject manifests whose counts exceed the
105 : fixed-size arrays in fd_snapshot_manifest_t. Validating here
106 : enables early recovery from malformed snapshots. */
107 :
108 0 : if( FD_UNLIKELY( manifest->hard_fork_cnt>FD_HARD_FORKS_MAX ) ) {
109 0 : FD_LOG_WARNING(( "corrupt snapshot: hard_fork_cnt %lu exceeds max %lu",
110 0 : manifest->hard_fork_cnt, FD_HARD_FORKS_MAX ));
111 0 : return -1;
112 0 : }
113 :
114 0 : if( FD_UNLIKELY( manifest->vote_accounts_len>FD_RUNTIME_MAX_SNAPSHOT_VOTE_ACCOUNTS ) ) {
115 0 : FD_LOG_WARNING(( "corrupt snapshot: vote_accounts_len %lu exceeds max %lu",
116 0 : manifest->vote_accounts_len, FD_RUNTIME_MAX_SNAPSHOT_VOTE_ACCOUNTS ));
117 0 : return -1;
118 0 : }
119 :
120 : /* Epoch credits downcasting validation */
121 :
122 0 : for( ulong i=0UL; i<FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN; i++ ) {
123 0 : if( FD_UNLIKELY( manifest->epoch_stakes[i].vote_stakes_len>FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ) ) {
124 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes_len %lu exceeds max %lu",
125 0 : i, manifest->epoch_stakes[i].vote_stakes_len, FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ));
126 0 : return -1;
127 0 : }
128 0 : for( ulong j=0UL; j<manifest->epoch_stakes[i].vote_stakes_len; j++ ) {
129 0 : fd_snapshot_manifest_vote_stakes_t const * vs = &manifest->epoch_stakes[i].vote_stakes[j];
130 0 : if( FD_UNLIKELY( vs->epoch_credits_history_len>FD_EPOCH_CREDITS_MAX ) ) {
131 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits_history_len %lu exceeds max %lu",
132 0 : i, j, vs->epoch_credits_history_len, FD_EPOCH_CREDITS_MAX ));
133 0 : return -1;
134 0 : }
135 : /* The Alpenglow migration marker is a special credits record that
136 : separates pre-Alpenglow and post-Alpenglow credits. Note
137 : Alpenglow is for a slot, so the credit epoch before and after
138 : can be the same. Validators who earned credits before and
139 : after will have two entries for the same epoch. */
140 :
141 0 : ulong ec_base = 0UL;
142 0 : ulong prev = ULONG_MAX; /* index of the previous non-marker entry */
143 0 : int marker_seen = 0;
144 :
145 0 : for( ulong k=0UL; k<vs->epoch_credits_history_len; k++ ) {
146 0 : epoch_credits_t const * epc = &vs->epoch_credits[k];
147 0 : if( FD_UNLIKELY( fd_epoch_credits_is_alpenglow_marker( epc ) ) ) {
148 0 : if( FD_UNLIKELY( marker_seen ) ) {
149 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu] has more than one alpenglow migration marker", i, j ));
150 0 : return -1;
151 0 : }
152 0 : marker_seen = 1;
153 0 : continue;
154 0 : }
155 0 : if( FD_UNLIKELY( prev==ULONG_MAX ) ) ec_base = epc->prev_credits;
156 0 : if( FD_UNLIKELY( epc->prev_credits>epc->credits ) ) {
157 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits[%lu].prev_credits %lu exceeds credits %lu",
158 0 : i, j, k, epc->prev_credits, epc->credits ));
159 0 : return -1;
160 0 : }
161 0 : if( FD_UNLIKELY( prev!=ULONG_MAX && epc->epoch<vs->epoch_credits[prev].epoch ) ) {
162 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits[%lu].epoch %lu is less than previous epoch %lu",
163 0 : i, j, k, epc->epoch, vs->epoch_credits[prev].epoch ));
164 0 : return -1;
165 0 : }
166 0 : if( FD_UNLIKELY( prev!=ULONG_MAX && epc->prev_credits!=vs->epoch_credits[prev].credits ) ) {
167 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits[%lu].prev_credits %lu does not equal previous credits %lu",
168 0 : i, j, k, epc->prev_credits, vs->epoch_credits[prev].credits ));
169 0 : return -1;
170 0 : }
171 0 : if( FD_UNLIKELY( epc->epoch>(ulong)USHORT_MAX ) ) {
172 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits[%lu].epoch %lu exceeds USHORT_MAX",
173 0 : i, j, k, epc->epoch ));
174 0 : return -1;
175 0 : }
176 0 : if( FD_UNLIKELY( epc->credits<ec_base ) ) {
177 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch_stakes[%lu].vote_stakes[%lu].epoch_credits[%lu].credits %lu is below base %lu",
178 0 : i, j, k, epc->credits, ec_base ));
179 0 : return -1;
180 0 : }
181 0 : prev = k;
182 0 : }
183 0 : }
184 0 : }
185 :
186 : /* Epoch stakes index validation. fd_slot_to_leader_schedule_epoch
187 : is inlined here with overflow-safe arithmetic. */
188 :
189 0 : fd_epoch_schedule_t epoch_schedule = (fd_epoch_schedule_t){
190 0 : .slots_per_epoch = manifest->epoch_schedule_params.slots_per_epoch,
191 0 : .leader_schedule_slot_offset = manifest->epoch_schedule_params.leader_schedule_slot_offset,
192 0 : .warmup = manifest->epoch_schedule_params.warmup,
193 0 : .first_normal_epoch = manifest->epoch_schedule_params.first_normal_epoch,
194 0 : .first_normal_slot = manifest->epoch_schedule_params.first_normal_slot,
195 0 : };
196 :
197 0 : ulong epoch = fd_slot_to_epoch( &epoch_schedule, manifest->slot, NULL );
198 :
199 : /* Compute leader_schedule_epoch with overflow safety. Mirrors
200 : fd_slot_to_leader_schedule_epoch but rejects overflow instead
201 : of silently wrapping. */
202 :
203 0 : ulong leader_schedule_epoch;
204 0 : if( FD_UNLIKELY( manifest->slot<epoch_schedule.first_normal_slot ) ) {
205 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( epoch, 1UL, &leader_schedule_epoch ) ) ) {
206 0 : FD_LOG_WARNING(( "corrupt snapshot: leader_schedule_epoch overflow (epoch=%lu)", epoch ));
207 0 : return -1;
208 0 : }
209 0 : } else {
210 0 : ulong delta = manifest->slot-epoch_schedule.first_normal_slot;
211 0 : ulong sum;
212 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( delta, epoch_schedule.leader_schedule_slot_offset, &sum ) ) ) {
213 0 : FD_LOG_WARNING(( "corrupt snapshot: leader_schedule_slot_offset overflow "
214 0 : "(slot_delta=%lu leader_schedule_slot_offset=%lu)",
215 0 : delta, epoch_schedule.leader_schedule_slot_offset ));
216 0 : return -1;
217 0 : }
218 0 : ulong n_epochs = sum/epoch_schedule.slots_per_epoch;
219 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( epoch_schedule.first_normal_epoch, n_epochs, &leader_schedule_epoch ) ) ) {
220 0 : FD_LOG_WARNING(( "corrupt snapshot: leader_schedule_epoch overflow "
221 0 : "(first_normal_epoch=%lu n_epochs=%lu)",
222 0 : epoch_schedule.first_normal_epoch, n_epochs ));
223 0 : return -1;
224 0 : }
225 0 : }
226 :
227 0 : ulong epoch_stakes_base = epoch>3UL ? epoch-3UL : 0UL;
228 :
229 0 : if( FD_UNLIKELY( leader_schedule_epoch<epoch_stakes_base ) ) {
230 0 : FD_LOG_WARNING(( "corrupt snapshot: leader_schedule_epoch %lu < epoch_stakes_base %lu",
231 0 : leader_schedule_epoch, epoch_stakes_base ));
232 0 : return -1;
233 0 : }
234 0 : ulong t_1_idx = leader_schedule_epoch-epoch_stakes_base;
235 0 : if( FD_UNLIKELY( t_1_idx>=FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN ) ) {
236 0 : FD_LOG_WARNING(( "corrupt snapshot: epoch stakes index %lu out of range (max %lu)",
237 0 : t_1_idx, FD_RUNTIME_MANIFEST_EPOCH_STAKES_LEN ));
238 0 : return -1;
239 0 : }
240 :
241 0 : if( FD_UNLIKELY( manifest->epoch_stakes[t_1_idx].vote_stakes_len>max_vote_accounts ) ) {
242 0 : FD_LOG_WARNING(( "corrupt snapshot: T-1 epoch stakes length %lu exceeds max_vote_accounts %lu",
243 0 : manifest->epoch_stakes[t_1_idx].vote_stakes_len, max_vote_accounts ));
244 0 : return -1;
245 0 : }
246 :
247 0 : ulong t_2_idx = epoch-epoch_stakes_base;
248 0 : if( FD_UNLIKELY( manifest->epoch_stakes[t_2_idx].vote_stakes_len>max_vote_accounts ) ) {
249 0 : FD_LOG_WARNING(( "corrupt snapshot: T-2 epoch stakes length %lu exceeds max_vote_accounts %lu",
250 0 : manifest->epoch_stakes[t_2_idx].vote_stakes_len, max_vote_accounts ));
251 0 : return -1;
252 0 : }
253 :
254 0 : for( ulong j=0UL; j<manifest->epoch_stakes[t_1_idx].vote_stakes_len; j++ ) {
255 0 : if( FD_UNLIKELY( !manifest->epoch_stakes[t_1_idx].vote_stakes[j].stake ) ) {
256 0 : FD_LOG_WARNING(( "corrupt snapshot: T-1 epoch stakes entry %lu has zero stake", j ));
257 0 : return -1;
258 0 : }
259 0 : }
260 :
261 0 : return 0;
262 0 : }
263 :
264 : #define SORT_NAME fd_blockhash_sort
265 0 : #define SORT_KEY_T fd_blockhash_info_t
266 0 : #define SORT_BEFORE(a,b) ((a).hash_index<(b).hash_index)
267 : #include "../../../util/tmpl/fd_sort.c"
268 :
269 : static int
270 : blockhashes_recover( fd_blockhashes_t * blockhashes,
271 : fd_snapshot_manifest_blockhash_t const * ages,
272 : ulong age_cnt,
273 0 : ulong seed ) {
274 :
275 : /* The caller must guarantee that fd_ssload_manifest_validate has
276 : already been invoked, verifying that age_cnt is in the range
277 : (0, FD_BLOCKHASHES_MAX] and that hash_index values are distinct. */
278 :
279 0 : if( FD_UNLIKELY( !fd_blockhashes_init( blockhashes, seed ) ) ) {
280 0 : FD_LOG_WARNING(( "failed to initialize blockhash queue" ));
281 0 : return -1;
282 0 : }
283 :
284 : /* The manifest lists entries in arbitrary order (Agave serializes a
285 : HashMap). sort the deque by hash_index, then index them in the map.
286 :
287 : The deque was just initialized, so it starts at slot 0 and the at
288 : most FD_BLOCKHASHES_MAX entries are contiguous at the front of its
289 : array without wrapping, which is what makes sorting it in place
290 : valid. */
291 :
292 0 : FD_TEST( fd_blockhash_deq_empty( blockhashes->d.deque ) );
293 0 : FD_TEST( !( blockhashes->d.start % fd_blockhash_deq_max( blockhashes->d.deque ) ) ); /* head at array slot 0 */
294 0 : FD_TEST( age_cnt<=fd_blockhash_deq_max( blockhashes->d.deque ) );
295 :
296 0 : for( ulong i=0UL; i<age_cnt; i++ ) {
297 0 : fd_snapshot_manifest_blockhash_t const * elem = &ages[ i ];
298 0 : fd_blockhash_info_t * info = fd_blockhash_deq_push_tail_nocopy( blockhashes->d.deque );
299 0 : fd_memset( info, 0, sizeof(fd_blockhash_info_t) );
300 0 : fd_memcpy( info->hash.uc, elem->hash, 32UL );
301 0 : info->lamports_per_signature = elem->lamports_per_signature;
302 0 : info->hash_index = elem->hash_index;
303 0 : }
304 0 : fd_blockhash_sort_inplace( blockhashes->d.deque, age_cnt );
305 :
306 0 : for( ulong i=0UL; i<age_cnt; i++ ) {
307 0 : fd_blockhash_map_idx_insert( blockhashes->map, i, blockhashes->d.deque );
308 0 : }
309 :
310 0 : return 0;
311 0 : }
312 :
313 : int
314 : fd_ssload_recover_validate( fd_snapshot_manifest_t const * manifest,
315 0 : fd_banks_t const * banks ) {
316 0 : return fd_ssload_manifest_validate( manifest, banks->max_vote_accounts, banks->max_stake_accounts );
317 0 : }
318 :
319 : int
320 : fd_ssload_recover_apply( fd_snapshot_manifest_t * manifest,
321 : fd_bank_t * bank,
322 0 : ulong blockhash_seed ) {
323 :
324 : /* Slot */
325 :
326 0 : bank->f.slot = manifest->slot;
327 0 : bank->f.parent_slot = manifest->parent_slot;
328 :
329 : /* Bank Hash */
330 :
331 0 : fd_hash_t hash;
332 0 : fd_memcpy( &hash.uc, manifest->bank_hash, 32UL );
333 0 : bank->f.bank_hash = hash;
334 :
335 0 : fd_hash_t parent_hash;
336 0 : fd_memcpy( &parent_hash.uc, manifest->parent_bank_hash, 32UL );
337 0 : bank->f.prev_bank_hash = parent_hash;
338 :
339 0 : fd_fee_rate_governor_t * fee_rate_governor = &bank->f.fee_rate_governor;
340 0 : fee_rate_governor->target_lamports_per_signature = manifest->fee_rate_governor.target_lamports_per_signature;
341 0 : fee_rate_governor->target_signatures_per_slot = manifest->fee_rate_governor.target_signatures_per_slot;
342 0 : fee_rate_governor->min_lamports_per_signature = manifest->fee_rate_governor.min_lamports_per_signature;
343 0 : fee_rate_governor->max_lamports_per_signature = manifest->fee_rate_governor.max_lamports_per_signature;
344 0 : fee_rate_governor->burn_percent = manifest->fee_rate_governor.burn_percent;
345 : /* https://github.com/anza-xyz/agave/blob/v3.0.3/runtime/src/serde_snapshot.rs#L464-L466 */
346 0 : bank->f.rbh_lamports_per_sig = manifest->lamports_per_signature;
347 :
348 0 : fd_inflation_t * inflation = &bank->f.inflation;
349 0 : inflation->initial = manifest->inflation_params.initial;
350 0 : inflation->terminal = manifest->inflation_params.terminal;
351 0 : inflation->taper = manifest->inflation_params.taper;
352 0 : inflation->foundation = manifest->inflation_params.foundation;
353 0 : inflation->foundation_term = manifest->inflation_params.foundation_term;
354 0 : inflation->unused = 0.0;
355 :
356 0 : fd_epoch_schedule_t * epoch_schedule = &bank->f.epoch_schedule;
357 0 : epoch_schedule->slots_per_epoch = manifest->epoch_schedule_params.slots_per_epoch;
358 0 : epoch_schedule->leader_schedule_slot_offset = manifest->epoch_schedule_params.leader_schedule_slot_offset;
359 0 : epoch_schedule->warmup = manifest->epoch_schedule_params.warmup;
360 0 : epoch_schedule->first_normal_epoch = manifest->epoch_schedule_params.first_normal_epoch;
361 0 : epoch_schedule->first_normal_slot = manifest->epoch_schedule_params.first_normal_slot;
362 :
363 0 : ulong epoch = fd_slot_to_epoch( epoch_schedule, manifest->slot, NULL );
364 0 : bank->f.epoch = epoch;
365 :
366 0 : fd_rent_t * rent = &bank->f.rent;
367 0 : rent->lamports_per_uint8_year = manifest->rent_params.lamports_per_uint8_year;
368 0 : rent->exemption_threshold = manifest->rent_params.exemption_threshold;
369 0 : rent->burn_percent = manifest->rent_params.burn_percent;
370 :
371 : /* https://github.com/anza-xyz/agave/blob/v3.0.6/ledger/src/blockstore_processor.rs#L1118
372 : None gets treated as 0 for hash verification. */
373 0 : ulong restored_hashes_per_tick = manifest->has_hashes_per_tick ? manifest->hashes_per_tick : 0UL;
374 :
375 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
376 0 : if( FD_LIKELY( manifest->has_accounts_lthash ) ) {
377 0 : fd_memcpy( lthash, manifest->accounts_lthash, sizeof(fd_lthash_value_t) );
378 0 : } else {
379 0 : fd_memset( lthash, 0, sizeof(fd_lthash_value_t) );
380 0 : }
381 0 : fd_bank_lthash_end_locking_modify( bank );
382 :
383 0 : fd_blockhashes_t * blockhashes = &bank->f.block_hash_queue;
384 0 : if( FD_UNLIKELY( blockhashes_recover( blockhashes, manifest->blockhashes, manifest->blockhashes_len, blockhash_seed ) ) ) {
385 0 : FD_LOG_WARNING(( "blockhash queue recovery failed" ));
386 0 : return -1;
387 0 : }
388 :
389 : /* PoH */
390 0 : fd_blockhashes_t const * bhq = &bank->f.block_hash_queue;
391 0 : fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( bhq );
392 0 : if( FD_LIKELY( last_hash ) ) bank->f.poh = *last_hash;
393 :
394 0 : bank->f.capitalization = manifest->capitalization;
395 0 : bank->f.txn_count = manifest->transaction_count;
396 0 : bank->f.signature_count = manifest->signature_count;
397 0 : bank->f.tick_height = manifest->tick_height;
398 0 : bank->f.max_tick_height = manifest->max_tick_height;
399 0 : bank->f.ticks_per_slot = manifest->ticks_per_slot;
400 0 : bank->f.genesis_creation_time = manifest->creation_time_seconds;
401 0 : bank->f.slot_params = FD_SLOT_PARAMS_400MS;
402 0 : bank->f.slot_params.ns_per_slot = manifest->ns_per_slot;
403 0 : bank->f.slot_params.ns_per_slot_adjusted = fd_ulong_sat_sub( bank->f.slot_params.ns_per_slot, FD_TARGET_SLOT_ADJUSTMENT_NS );
404 0 : bank->f.slot_params.slots_per_year = manifest->slots_per_year;
405 0 : bank->f.slot_params.hashes_per_tick = restored_hashes_per_tick;
406 0 : bank->f.block_height = manifest->block_height;
407 0 : bank->f.execution_fees = manifest->collector_fees;
408 0 : bank->f.priority_fees = 0UL;
409 :
410 : /* Set the cluster type based on the genesis creation time. This is
411 : later cross referenced against the genesis hash. */
412 0 : switch( bank->f.genesis_creation_time ) {
413 0 : case FD_RUNTIME_GENESIS_CREATION_TIME_TESTNET:
414 0 : bank->f.cluster_type = FD_CLUSTER_TESTNET;
415 0 : break;
416 0 : case FD_RUNTIME_GENESIS_CREATION_TIME_MAINNET:
417 0 : bank->f.cluster_type = FD_CLUSTER_MAINNET_BETA;
418 0 : break;
419 0 : case FD_RUNTIME_GENESIS_CREATION_TIME_DEVNET:
420 0 : bank->f.cluster_type = FD_CLUSTER_DEVNET;
421 0 : break;
422 0 : default:
423 0 : bank->f.cluster_type = FD_CLUSTER_UNKNOWN;
424 0 : }
425 :
426 : /* Update last restart slot
427 : https://github.com/solana-labs/solana/blob/30531d7a5b74f914dde53bfbb0bc2144f2ac92bb/runtime/src/bank.rs#L2152
428 :
429 : old_bank->hard_forks is sorted ascending by slot number.
430 : To find the last restart slot, take the highest hard fork slot
431 : number that is less or equal than the current slot number.
432 : (There might be some hard forks in the future, ignore these)
433 :
434 : SIMD-0047: The first restart slot should be `0` */
435 0 : bank->f.hard_fork_cnt = manifest->hard_fork_cnt;
436 0 : if( FD_LIKELY( manifest->hard_fork_cnt ) ) {
437 0 : for( ulong i=0UL; i<manifest->hard_fork_cnt; i++ ) {
438 0 : bank->f.hard_forks[ i ] = manifest->hard_forks[ i ];
439 0 : }
440 :
441 0 : for( ulong i=0UL; i<manifest->hard_fork_cnt; i++ ) {
442 0 : ulong slot = manifest->hard_forks[ manifest->hard_fork_cnt-1UL-i ].slot;
443 0 : if( FD_LIKELY( slot<=manifest->slot ) ) {
444 0 : break;
445 0 : }
446 0 : }
447 0 : }
448 :
449 : /* snapin populates the root stake delegation cache directly from the
450 : account stream. The manifest's primary stake delegations are
451 : intentionally ignored. */
452 :
453 : /* We also want to set the total stake to be the total amount of stake
454 : at the end of the previous epoch. This value is used for the
455 : get_epoch_stake syscall.
456 :
457 : A note on Agave's indexing scheme for their epoch_stakes
458 : structure:
459 :
460 : https://github.com/anza-xyz/agave/blob/v2.2.14/runtime/src/bank.rs#L6175
461 :
462 : If we are loading a snapshot and replaying in the middle of
463 : epoch 7, the syscall is supposed to return the total stake at
464 : the end of epoch 6. The epoch_stakes structure is indexed in
465 : Agave by the epoch number of the leader schedule that the
466 : stakes are meant to determine. For instance, to get the
467 : stakes at the end of epoch 6, we should query by 8, because
468 : the leader schedule for epoch 8 is determined based on the
469 : stakes at the end of epoch 6. Therefore, we save the total
470 : epoch stake by querying for epoch+1. This logic is encapsulated
471 : in fd_ssmanifest_parser.c. */
472 :
473 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
474 0 : fd_vote_stakes_reset( vote_stakes );
475 0 : bank->vote_stakes_fork_id = fd_vote_stakes_init( vote_stakes, bank->f.epoch );
476 0 : ulong vote_stakes_fork_id = bank->vote_stakes_fork_id;
477 :
478 0 : ulong leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, manifest->slot );
479 0 : ulong epoch_stakes_base = epoch > 3UL ? epoch - 3UL : 0UL;
480 0 : ulong t_1_idx = leader_schedule_epoch - epoch_stakes_base;
481 0 : ulong t_2_idx = epoch - epoch_stakes_base;
482 :
483 0 : bank->f.total_epoch_stake = manifest->epoch_stakes[t_1_idx].total_stake;
484 :
485 0 : bank->epoch_credits_fork_id = fd_epoch_credits_store_new_fork( fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id );
486 0 : ulong epoch_credits_len = 0UL;
487 0 : fd_epoch_credits_view_t epoch_credits_view[1];
488 0 : FD_TEST( fd_epoch_credits_view_init( epoch_credits_view, fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id ) );
489 :
490 : /* Populate the top votes for the end of the T-1 epoch if the
491 : snapshot is in epoch T. */
492 0 : for( ulong i=0UL; i<manifest->epoch_stakes[t_1_idx].vote_stakes_len; i++ ) {
493 0 : fd_snapshot_manifest_vote_stakes_t const * elem = &manifest->epoch_stakes[t_1_idx].vote_stakes[i];
494 :
495 0 : fd_vote_stakes_snap_insert_t_1( vote_stakes, vote_stakes_fork_id, (fd_pubkey_t *)elem->vote, (fd_pubkey_t *)elem->identity, elem->stake, elem->commission, elem->identity_bls,
496 0 : elem->commission_block_bps, elem->pending_delegator_rewards,
497 0 : (fd_pubkey_t const *)elem->commission_inflation, (fd_pubkey_t const *)elem->commission_block );
498 :
499 : /* Reward recalculation resolves every epoch credits entry against
500 : the t_1 set, so only admitted accounts may get one. */
501 0 : if( FD_UNLIKELY( !fd_vote_stakes_query_t_1( vote_stakes, vote_stakes_fork_id, (fd_pubkey_t const *)elem->vote, NULL ) ) ) continue;
502 :
503 0 : if( FD_UNLIKELY( epoch_credits_len>=FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ) ) {
504 0 : FD_LOG_WARNING(( "corrupt snapshot: more vote accounts than the epoch credits store holds (%lu)", FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ));
505 0 : fd_epoch_credits_view_fini( epoch_credits_view );
506 0 : return -1;
507 0 : }
508 0 : fd_epoch_credits_t * ec = &epoch_credits_view->credits[epoch_credits_len];
509 0 : fd_memcpy( ec->pubkey, elem->vote, 32UL );
510 :
511 0 : ulong cnt = 0UL;
512 0 : ec->base_credits = 0UL;
513 0 : ec->has_ag_migration_marker = 0;
514 0 : for( ulong j=0UL; j<elem->epoch_credits_history_len; j++ ) {
515 0 : if( FD_UNLIKELY( fd_epoch_credits_is_alpenglow_marker( &elem->epoch_credits[ j ] ) ) ) {
516 0 : ec->has_ag_migration_marker = 1;
517 0 : continue;
518 0 : }
519 0 : if( FD_UNLIKELY( !cnt ) ) ec->base_credits = elem->epoch_credits[ j ].prev_credits;
520 0 : ec->epoch[ cnt ] = (ushort)elem->epoch_credits[ j ].epoch;
521 0 : ec->credits_delta[ cnt ] = elem->epoch_credits[ j ].credits - ec->base_credits;
522 0 : ec->prev_credits_delta[ cnt ] = elem->epoch_credits[ j ].prev_credits - ec->base_credits;
523 0 : cnt++;
524 0 : }
525 : /* Manifest validation already rejects non-increasing epochs (except
526 : Alpenglow marker). */
527 0 : ec->cnt = (uchar)cnt;
528 0 : ec->fast_path_ok = fd_epoch_credits_fast_path_ok( ec );
529 0 : FD_TEST( ec->fast_path_ok ); /* manifest validation enforces all three invariants */
530 0 : epoch_credits_len++;
531 0 : }
532 0 : epoch_credits_view->len = epoch_credits_len;
533 0 : fd_epoch_credits_view_fini( epoch_credits_view );
534 0 : fd_vote_stakes_finalize( vote_stakes, vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_1 );
535 :
536 : /* Populate the top votes for the end of the T-2 epoch if the
537 : snapshot is in epoch T. */
538 0 : for( ulong i=0UL; i<manifest->epoch_stakes[t_2_idx].vote_stakes_len; i++ ) {
539 0 : fd_snapshot_manifest_vote_stakes_t const * elem = &manifest->epoch_stakes[t_2_idx].vote_stakes[i];
540 0 : fd_vote_stakes_snap_insert_t_2( vote_stakes, vote_stakes_fork_id, (fd_pubkey_t *)elem->vote, (fd_pubkey_t *)elem->identity, elem->stake, elem->commission, elem->identity_bls,
541 0 : elem->commission_block_bps, elem->pending_delegator_rewards,
542 0 : (fd_pubkey_t const *)elem->commission_inflation, (fd_pubkey_t const *)elem->commission_block );
543 0 : }
544 0 : fd_vote_stakes_finalize( vote_stakes, vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_2 );
545 :
546 : /* Populate the top votes for the end of the T-3 through T-5 epochs
547 : if the snapshot is in epoch T. Entries the snapshot does not
548 : carry stay non-resident. */
549 0 : for( ulong n=3UL; n<=5UL && epoch>=n-2UL; n++ ) {
550 0 : fd_snapshot_manifest_epoch_stakes_t const * epoch_stakes = &manifest->epoch_stakes[t_2_idx-(n-2UL)];
551 0 : for( ulong i=0UL; i<epoch_stakes->vote_stakes_len; i++ ) {
552 0 : fd_snapshot_manifest_vote_stakes_t const * elem = &epoch_stakes->vote_stakes[i];
553 0 : fd_vote_stakes_snap_insert_t_n( vote_stakes, vote_stakes_fork_id, n, (fd_pubkey_t *)elem->vote, (fd_pubkey_t *)elem->identity, elem->stake, elem->commission, elem->identity_bls,
554 0 : elem->commission_block_bps, elem->pending_delegator_rewards, NULL, NULL );
555 0 : }
556 0 : fd_vote_stakes_finalize( vote_stakes, vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_2+(int)(n-2UL) );
557 0 : }
558 :
559 0 : bank->accdb_fork_id = (fd_accdb_fork_id_t){ .val = manifest->accdb_fork_id };
560 0 : bank->parent_accdb_fork_id = bank->accdb_fork_id;
561 0 : bank->txncache_fork_id = (fd_txncache_fork_id_t){ .val = manifest->txncache_fork_id };
562 :
563 0 : return 0;
564 0 : }
565 :
566 : int
567 : fd_ssload_recover( fd_snapshot_manifest_t * manifest,
568 : fd_banks_t * banks,
569 : fd_bank_t * bank,
570 0 : ulong blockhash_seed ) {
571 :
572 0 : if( FD_UNLIKELY( fd_ssload_recover_validate( manifest, banks ) ) ) {
573 0 : FD_LOG_WARNING(( "snapshot manifest validation failed" ));
574 0 : return -1;
575 0 : }
576 :
577 0 : return fd_ssload_recover_apply( manifest, bank, blockhash_seed );
578 0 : }
|