Line data Source code
1 : #include "fd_replay_tile.h"
2 : #include "fd_sched.h"
3 : #include "fd_exec.h"
4 : #include "fd_vote_tracker.h"
5 : #include "../../flamenco/accdb/fd_accdb_sync.h"
6 : #include "generated/fd_replay_tile_seccomp.h"
7 :
8 : #include "../genesis/fd_genesi_tile.h"
9 : #include "../poh/fd_poh.h"
10 : #include "../poh/fd_poh_tile.h"
11 : #include "../tower/fd_tower_tile.h"
12 : #include "../resolv/fd_resolv_tile.h"
13 : #include "../restore/utils/fd_ssload.h"
14 :
15 : #include "../../disco/tiles.h"
16 : #include "../../disco/fd_txn_m.h"
17 : #include "../../disco/store/fd_store.h"
18 : #include "../../discof/reasm/fd_reasm.h"
19 : #include "../../disco/keyguard/fd_keyload.h"
20 : #include "../../util/pod/fd_pod.h"
21 : #include "../../flamenco/accdb/fd_accdb_admin.h"
22 : #include "../../flamenco/accdb/fd_accdb_user.h"
23 : #include "../../flamenco/rewards/fd_rewards.h"
24 : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
25 : #include "../../flamenco/progcache/fd_progcache_admin.h"
26 : #include "../../disco/metrics/fd_metrics.h"
27 :
28 : #include "../../flamenco/runtime/fd_runtime.h"
29 : #include "../../flamenco/runtime/fd_runtime_stack.h"
30 : #include "../../flamenco/fd_flamenco_base.h"
31 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
32 :
33 : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
34 :
35 : #include <errno.h>
36 :
37 : /* Replay concepts:
38 :
39 : - Blocks are aggregations of entries aka. microblocks which are
40 : groupings of txns and are constructed by the block producer (see
41 : fd_pack).
42 :
43 : - Entries are grouped into entry batches by the block producer (see
44 : fd_pack / fd_shredder).
45 :
46 : - Entry batches are divided into chunks known as shreds by the block
47 : producer (see fd_shredder).
48 :
49 : - Shreds are grouped into forward-error-correction sets (FEC sets) by
50 : the block producer (see fd_shredder).
51 :
52 : - Shreds are transmitted to the rest of the cluster via the Turbine
53 : protocol (see fd_shredder / fd_shred).
54 :
55 : - Once enough shreds within a FEC set are received to recover the
56 : entirety of the shred data encoded by that FEC set, the receiver
57 : can "complete" the FEC set (see fd_fec_resolver).
58 :
59 : - If shreds in the FEC set are missing such that it can't complete,
60 : the receiver can use the Repair protocol to request missing shreds
61 : in FEC set (see fd_repair).
62 :
63 : - The current Repair protocol does not support requesting coding
64 : shreds. As a result, some FEC sets might be actually complete
65 : (contain all data shreds). Repair currently hacks around this by
66 : forcing completion but the long-term solution is to add support for
67 : fec_repairing coding shreds via Repair.
68 :
69 : - FEC sets are delivered in partial-order to the Replay tile by the
70 : Repair tile. Currently Replay only supports replaying entry batches
71 : so FEC sets need to reassembled into an entry batch before they can
72 : be replayed. The new Dispatcher will change this by taking a FEC
73 : set as input instead. */
74 :
75 0 : #define IN_KIND_SNAP (0)
76 0 : #define IN_KIND_GENESIS (1)
77 0 : #define IN_KIND_IPECHO (2)
78 0 : #define IN_KIND_TOWER (3)
79 0 : #define IN_KIND_RESOLV (4)
80 0 : #define IN_KIND_POH (5)
81 0 : #define IN_KIND_EXEC (6)
82 0 : #define IN_KIND_SHRED (7)
83 0 : #define IN_KIND_VTXN (8)
84 0 : #define IN_KIND_GUI (9)
85 :
86 : #define DEBUG_LOGGING 0
87 :
88 : /* The first bank that that the replay tile produces either for genesis
89 : or the snapshot boot will always be at bank index 0. */
90 0 : #define FD_REPLAY_BOOT_BANK_IDX (0UL)
91 :
92 : struct fd_replay_in_link {
93 : fd_wksp_t * mem;
94 : ulong chunk0;
95 : ulong wmark;
96 : ulong mtu;
97 : };
98 :
99 : typedef struct fd_replay_in_link fd_replay_in_link_t;
100 :
101 : struct fd_replay_out_link {
102 : ulong idx;
103 : fd_wksp_t * mem;
104 : ulong chunk0;
105 : ulong wmark;
106 : ulong chunk;
107 : };
108 :
109 : typedef struct fd_replay_out_link fd_replay_out_link_t;
110 :
111 : /* fd_block_id_map is a simple map of block-ids to bank indices. The
112 : map sits on top of an array of fd_block_id_ele_t. This serves as a
113 : translation layer between block ids to bank indices. */
114 :
115 : struct fd_block_id_ele {
116 : fd_hash_t block_id;
117 : ulong slot; /* = FD_SLOT_NULL if not initialized */
118 : ulong next_;
119 : };
120 : typedef struct fd_block_id_ele fd_block_id_ele_t;
121 :
122 : #define MAP_NAME fd_block_id_map
123 : #define MAP_ELE_T fd_block_id_ele_t
124 : #define MAP_KEY_T fd_hash_t
125 0 : #define MAP_KEY block_id
126 0 : #define MAP_NEXT next_
127 0 : #define MAP_KEY_EQ(k0,k1) (!memcmp((k0),(k1), sizeof(fd_hash_t)))
128 0 : #define MAP_KEY_HASH(key,seed) (fd_hash((seed),(key),sizeof(fd_hash_t)))
129 : #include "../../util/tmpl/fd_map_chain.c"
130 :
131 : static inline ulong
132 0 : fd_block_id_ele_get_idx( fd_block_id_ele_t * ele_arr, fd_block_id_ele_t * ele ) {
133 0 : return (ulong)(ele - ele_arr);
134 0 : }
135 :
136 : struct fd_replay_tile {
137 : fd_wksp_t * wksp;
138 :
139 : /* tx_metadata_storage enables the log collector if enabled */
140 : int tx_metadata_storage;
141 :
142 : fd_accdb_admin_t accdb_admin[1];
143 : fd_accdb_user_t accdb[1];
144 : fd_progcache_admin_t progcache_admin[1];
145 :
146 : fd_txncache_t * txncache;
147 : fd_store_t * store;
148 : fd_banks_t * banks;
149 :
150 : /* This flag is 1 If we have seen a vote signature that our node has
151 : sent out get rooted at least one time. The value is 0 otherwise.
152 : We can't become leader and pack blocks until this flag has been
153 : set. This parallels the Agave 'has_new_vote_been_rooted'.
154 :
155 : TODO: Add a flag to the toml to make this optional. */
156 : int has_identity_vote_rooted;
157 :
158 : fd_reasm_t * reasm;
159 :
160 : /* Replay state machine. */
161 : fd_sched_t * sched;
162 : uint enable_bank_hash_cmp:1;
163 : fd_bank_hash_cmp_t * bank_hash_cmp;
164 : ulong exec_cnt;
165 : fd_replay_out_link_t exec_out[ 1 ]; /* Sending work down to exec tiles */
166 :
167 : fd_vote_tracker_t * vote_tracker;
168 :
169 : int has_genesis_hash;
170 : uchar genesis_hash[ 32UL ];
171 :
172 : #define FD_REPLAY_HARD_FORKS_MAX (64UL)
173 : ulong hard_forks_cnt;
174 : ulong hard_forks[ FD_REPLAY_HARD_FORKS_MAX ];
175 :
176 : ushort expected_shred_version;
177 : ushort ipecho_shred_version;
178 :
179 : /* A note on publishing ...
180 :
181 : The watermarks are used to publish our fork-aware structures. For
182 : example, store, banks, and txncache need to be published to release
183 : resources occupied by rooted or dead blocks. In general,
184 : publishing has the effect of pruning forks in those structures,
185 : indicating that it is ok to release the memory being occupied by
186 : the blocks on said forks. Tower is responsible for informing us of
187 : the latest block on the consensus rooted fork. As soon as we can,
188 : we should move the published root as close as possible to the
189 : latest consensus root, publishing/pruning everything on the fork
190 : tree along the way. That is, all the blocks that directly descend
191 : from the current published root (inclusive) to the new published
192 : root (exclusive) on the rooted fork, as well as all the minority
193 : forks that branch from said blocks.
194 :
195 : Ideally, we'd move the published root to the consensus root
196 : immediately upon receiving a new consensus root. However, that's
197 : not always safe to do. One thing we need to be careful about is
198 : making sure that there are no more users/consumers of
199 : soon-to-be-pruned blocks, lest a use-after-free occurs. This can
200 : be done by using a reference counter for each block. Any
201 : concurrent activity, such as transaction execution in the exec
202 : tiles, should retain a refcnt on the block for as
203 : long as it needs access to the shared fork-aware structures related
204 : to that block. Eventually, refcnt on a given block will drop down
205 : to 0 as the block either finishes replaying or gets marked as dead,
206 : and any other tile that has retained a refcnt on the block releases
207 : it. At that point, it becomes a candidate for pruning. The key to
208 : safe publishing then becomes figuring out how far we could advance
209 : the published root, such that every minority fork branching off of
210 : blocks in between the current published root (inclusive) and the
211 : new published root (exclusive) is safe to be pruned. This is a
212 : straightforward tree traversal, where if a block B on the rooted
213 : fork has refcnt 0, and all minority forks branching off of B also
214 : have refcnt 0, then B is safe to be pruned. We advance the
215 : published root to the farthest consecutively prunable block on the
216 : rooted fork. Note that reasm presents the replay tile with a clean
217 : view of the world where every block is chained off of a parent
218 : block. So there are no orpahned/dangling tree nodes to worry
219 : about. The world is a nice single tree as far as replay is
220 : concerned.
221 :
222 : In the following fork tree, every node is a block and the number in
223 : parentheses is the refcnt on the block. The chain marked with
224 : double slashes is the rooted fork. Suppose the published root is
225 : at block P, and consensus root is at block T. We can't publish
226 : past block P because Q has refcnt 1.
227 :
228 :
229 : P(0)
230 : / \\
231 : Q(1) A(0)
232 : / || \
233 : X(0) B(0) C(0)
234 : / || \
235 : Y(0) M(0) R(0)
236 : / || / \
237 : D(2) T(0) J(0) L(0)
238 : ||
239 : ..
240 : ..
241 : ..
242 : ||
243 : blocks we might be actively replaying
244 :
245 :
246 : When refcnt on Q drops to 0, we would be able to advance the
247 : published root to block M, because blocks P, A, and B, as well as
248 : all subtrees branching off of them, have refcnt 0, and therefore
249 : can be pruned. Block M itself cannot be pruned yet because its
250 : child block D has refcnt 2. After publishing/pruning, the fork
251 : tree would be:
252 :
253 :
254 : M(0)
255 : / ||
256 : D(2) T(0)
257 : ||
258 : ..
259 : ..
260 : ..
261 : ||
262 : blocks we might be actively replaying
263 :
264 :
265 : As a result, the shared fork-aware structures can free resources
266 : for blocks P, A, B, and all subtrees branching off of them.
267 :
268 : For the reference counting part, the replay tile is the sole entity
269 : that can update the refcnt. This ensures that all refcnt increment
270 : and decrement attempts are serialized at the replay tile, and that
271 : there are no racy resurrection of a soon-to-be-pruned block. If a
272 : refcnt increment request arrives after a block has been pruned,
273 : replay simply rejects the request.
274 :
275 : A note on the implementation of the above ...
276 :
277 : Upon receiving a new consensus root, we descend down the rooted
278 : fork from the current published root to the new consensus root. On
279 : each node/block of the rooted fork, we do a summation of the refcnt
280 : on the block and all the minority fork blocks branching from the
281 : block. If the summation is 0, the block is safe for pruning. We
282 : advance the published root to the far end of the consecutive run of
283 : 0 refcnt sums originating from the current published root. On our
284 : descent down the minority forks, we also mark any block that hasn't
285 : finished replaying as dead, so we don't waste time executing them.
286 : No more transactions shall be dispatched for execution from dead
287 : blocks.
288 :
289 : Blocks start out with a refcnt of 0. Other tiles may send a
290 : request to the replay tile for a reference on a block. The
291 : transaction dispatcher is another source of refcnt updates. On
292 : every dispatch of a transaction for block B, we increment the
293 : refcnt for B. And on every transaction finalization, we decrement
294 : the refcnt for B. This means that whenever the refcnt on a block
295 : is 0, there is no more reference on that block from the execution
296 : pipeline. While it might be tempting to simply increment the
297 : refcnt once when we start replaying a block, and decrement the
298 : refcnt once when we finish a block, this more fine-grained refcnt
299 : update strategy allows for aborting and potentially immediate
300 : pruning of blocks under interleaved block replay. Upon receiving a
301 : new consensus root, we can simply look at the refcnt on minority
302 : fork blocks, and a refcnt of 0 would imply that the block is safe
303 : for pruning, even if we haven't finished replaying it. Without the
304 : fine-grained refcnt, we would need to first stop dispatching from
305 : the aborted block, and then wait for a full drain of the execution
306 : pipeline to know for sure that there are no more in-flight
307 : transactions executing on the aborted block. Note that this will
308 : allow the refcnt on any block to transiently drop down to 0. We
309 : will not mistakenly prune an actively replaying block, aka a leaf
310 : node, that is chaining off of the rooted fork, because the
311 : consensus root is always an ancestor of the actively replaying tip.
312 : */
313 : fd_hash_t consensus_root; /* The most recent block to have reached max lockout in the tower. */
314 : ulong consensus_root_slot; /* slot number of the above. */
315 : ulong consensus_root_bank_idx; /* bank index of the above. */
316 : ulong published_root_slot; /* slot number of the published root. */
317 : ulong published_root_bank_idx; /* bank index of the published root. */
318 :
319 : /* We need to maintain a tile-local mapping of block-ids to bank index
320 : and vice versa. This translation layer is needed for conversion
321 : since tower operates on block-ids and downstream consumers of FEC
322 : sets operate on bank indices. This mapping must happen both ways:
323 : 1. tower sends us block ids and we must map them to bank indices.
324 : 2. when a block is completed, we must map the bank index to a block
325 : id to send a slot complete message to tower. */
326 : ulong block_id_len;
327 : fd_block_id_ele_t * block_id_arr;
328 : fd_block_id_map_t * block_id_map;
329 :
330 : /* Capture-related configs */
331 : fd_capture_ctx_t * capture_ctx;
332 : FILE * capture_file;
333 :
334 : /* Whether the runtime has been booted either from snapshot loading
335 : or from genesis. */
336 : int is_booted;
337 :
338 : /* Buffer to store vote towers that need to be published to the Tower
339 : tile. */
340 : ulong vote_tower_out_idx; /* index of vote tower to publish next */
341 : ulong vote_tower_out_len; /* number of vote towers in the buffer */
342 : fd_replay_tower_t vote_tower_out[FD_REPLAY_TOWER_VOTE_ACC_MAX];
343 :
344 : fd_multi_epoch_leaders_t * mleaders;
345 :
346 : fd_pubkey_t identity_pubkey[1]; /* TODO: Keyswitch */
347 :
348 : /* When we transition to becoming leader, we can only unbecome the
349 : leader if we have received a block id from the FEC reassembler, and
350 : a message from PoH that the leader slot has ended. After both of
351 : these conditions are met, then we are free to unbecome the leader.
352 : */
353 : int is_leader;
354 : int recv_poh;
355 : int recv_block_id;
356 : ulong next_leader_slot;
357 : long next_leader_tickcount;
358 : ulong highwater_leader_slot;
359 : ulong reset_slot;
360 : fd_hash_t reset_block_id;
361 : long reset_timestamp_nanos;
362 : double slot_duration_nanos;
363 : double slot_duration_ticks;
364 : ulong max_active_descendant;
365 : fd_bank_t * leader_bank; /* ==NULL if not currently the leader */
366 :
367 : ulong resolv_tile_cnt;
368 :
369 : int in_kind[ 64 ];
370 : fd_replay_in_link_t in[ 64 ];
371 :
372 : fd_replay_out_link_t replay_out[1];
373 :
374 : fd_replay_out_link_t stake_out[1];
375 :
376 : /* The gui tile needs to reliably own a reference to the most recent
377 : completed active bank. Replay needs to know if the gui as a
378 : consumer is enabled so it can increment the bank's refcnt before
379 : publishing the bank_idx to the gui. */
380 : int gui_enabled;
381 :
382 : /* For dumping blocks to protobuf. For backtest only. */
383 : fd_block_dump_ctx_t * block_dump_ctx;
384 :
385 : struct {
386 : fd_histf_t store_read_wait[ 1 ];
387 : fd_histf_t store_read_work[ 1 ];
388 : fd_histf_t store_publish_wait[ 1 ];
389 : fd_histf_t store_publish_work[ 1 ];
390 : fd_histf_t store_link_wait[ 1 ];
391 : fd_histf_t store_link_work[ 1 ];
392 :
393 : ulong slots_total;
394 : ulong transactions_total;
395 : } metrics;
396 :
397 : uchar __attribute__((aligned(FD_MULTI_EPOCH_LEADERS_ALIGN))) mleaders_mem[ FD_MULTI_EPOCH_LEADERS_FOOTPRINT ];
398 :
399 : fd_runtime_stack_t runtime_stack;
400 : };
401 :
402 : typedef struct fd_replay_tile fd_replay_tile_t;
403 :
404 : FD_FN_CONST static inline ulong
405 0 : scratch_align( void ) {
406 0 : return 128UL;
407 0 : }
408 : FD_FN_PURE static inline ulong
409 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
410 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
411 :
412 0 : ulong l = FD_LAYOUT_INIT;
413 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
414 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_block_id_ele_t), sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
415 0 : l = FD_LAYOUT_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
416 0 : l = FD_LAYOUT_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
417 0 : l = FD_LAYOUT_APPEND( l, fd_reasm_align(), fd_reasm_footprint( 1 << 20 ) );
418 0 : l = FD_LAYOUT_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.max_live_slots ) );
419 0 : l = FD_LAYOUT_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
420 0 : l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
421 :
422 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
423 0 : l = FD_LAYOUT_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
424 0 : }
425 :
426 0 : l = FD_LAYOUT_FINI( l, scratch_align() );
427 :
428 0 : return l;
429 0 : }
430 :
431 : static inline void
432 0 : metrics_write( fd_replay_tile_t * ctx ) {
433 0 : FD_MHIST_COPY( REPLAY, STORE_LINK_WAIT, ctx->metrics.store_link_wait );
434 0 : FD_MHIST_COPY( REPLAY, STORE_LINK_WORK, ctx->metrics.store_link_work );
435 0 : FD_MHIST_COPY( REPLAY, STORE_READ_WAIT, ctx->metrics.store_read_wait );
436 0 : FD_MHIST_COPY( REPLAY, STORE_READ_WORK, ctx->metrics.store_read_work );
437 0 : FD_MHIST_COPY( REPLAY, STORE_PUBLISH_WAIT, ctx->metrics.store_publish_wait );
438 0 : FD_MHIST_COPY( REPLAY, STORE_PUBLISH_WORK, ctx->metrics.store_publish_work );
439 :
440 0 : FD_MGAUGE_SET( REPLAY, ROOT_SLOT, ctx->consensus_root_slot==ULONG_MAX ? 0UL : ctx->consensus_root_slot );
441 0 : ulong leader_slot = ctx->leader_bank ? fd_bank_slot_get( ctx->leader_bank ) : 0UL;
442 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
443 :
444 0 : if( FD_LIKELY( ctx->leader_bank ) ) {
445 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, leader_slot );
446 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
447 0 : } else {
448 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, ctx->next_leader_slot==ULONG_MAX ? 0UL : ctx->next_leader_slot );
449 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, 0UL );
450 0 : }
451 0 : FD_MGAUGE_SET( REPLAY, RESET_SLOT, ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot );
452 :
453 0 : fd_bank_t * bank_pool = fd_banks_get_bank_pool( ctx->banks );
454 0 : ulong live_banks = fd_banks_pool_max( bank_pool ) - fd_banks_pool_free( bank_pool );
455 0 : FD_MGAUGE_SET( REPLAY, LIVE_BANKS, live_banks );
456 :
457 0 : FD_MCNT_SET( REPLAY, SLOTS_TOTAL, ctx->metrics.slots_total );
458 0 : FD_MCNT_SET( REPLAY, TRANSACTIONS_TOTAL, ctx->metrics.transactions_total );
459 0 : }
460 :
461 : static inline ulong
462 : generate_stake_weight_msg( ulong epoch,
463 : fd_epoch_schedule_t const * epoch_schedule,
464 : fd_vote_states_t const * epoch_stakes,
465 0 : ulong * stake_weight_msg_out ) {
466 0 : fd_stake_weight_msg_t * stake_weight_msg = (fd_stake_weight_msg_t *)fd_type_pun( stake_weight_msg_out );
467 0 : fd_vote_stake_weight_t * stake_weights = stake_weight_msg->weights;
468 :
469 0 : stake_weight_msg->epoch = epoch;
470 0 : stake_weight_msg->start_slot = fd_epoch_slot0( epoch_schedule, epoch );
471 0 : stake_weight_msg->slot_cnt = epoch_schedule->slots_per_epoch;
472 0 : stake_weight_msg->excluded_stake = 0UL;
473 0 : stake_weight_msg->vote_keyed_lsched = 1UL;
474 :
475 : /* FIXME: SIMD-0180 - hack to (de)activate in testnet vs mainnet.
476 : This code can be removed once the feature is active. */
477 0 : if( (1==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_TESTNET) ||
478 0 : (0==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_MAINNET) ) {
479 0 : stake_weight_msg->vote_keyed_lsched = 0UL;
480 0 : }
481 :
482 : /* epoch_stakes from manifest are already filtered (stake>0), but not sorted */
483 0 : fd_vote_states_iter_t iter_[1];
484 0 : ulong idx = 0UL;
485 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, epoch_stakes ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
486 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
487 0 : if( FD_UNLIKELY( !vote_state->stake ) ) continue;
488 :
489 0 : stake_weights[ idx ].stake = vote_state->stake;
490 0 : memcpy( stake_weights[ idx ].id_key.uc, &vote_state->node_account, sizeof(fd_pubkey_t) );
491 0 : memcpy( stake_weights[ idx ].vote_key.uc, &vote_state->vote_account, sizeof(fd_pubkey_t) );
492 0 : idx++;
493 0 : }
494 0 : stake_weight_msg->staked_cnt = idx;
495 0 : sort_vote_weights_by_stake_vote_inplace( stake_weights, idx );
496 :
497 0 : return fd_stake_weight_msg_sz( idx );
498 0 : }
499 :
500 : static void
501 : publish_stake_weights( fd_replay_tile_t * ctx,
502 : fd_stem_context_t * stem,
503 : fd_bank_t * bank,
504 0 : int current_epoch ) {
505 0 : fd_epoch_schedule_t const * schedule = fd_bank_epoch_schedule_query( bank );
506 0 : ulong epoch = fd_slot_to_epoch( schedule, fd_bank_slot_get( bank ), NULL );
507 :
508 0 : fd_vote_states_t const * vote_states_prev;
509 0 : if( FD_LIKELY( current_epoch ) ) vote_states_prev = fd_bank_vote_states_prev_locking_query( bank );
510 0 : else vote_states_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
511 :
512 0 : ulong * stake_weights_msg = fd_chunk_to_laddr( ctx->stake_out->mem, ctx->stake_out->chunk );
513 0 : ulong stake_weights_sz = generate_stake_weight_msg( epoch+fd_ulong_if( current_epoch, 1UL, 0UL), schedule, vote_states_prev, stake_weights_msg );
514 0 : ulong stake_weights_sig = 4UL;
515 0 : fd_stem_publish( stem, ctx->stake_out->idx, stake_weights_sig, ctx->stake_out->chunk, stake_weights_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
516 0 : ctx->stake_out->chunk = fd_dcache_compact_next( ctx->stake_out->chunk, stake_weights_sz, ctx->stake_out->chunk0, ctx->stake_out->wmark );
517 :
518 0 : if( FD_LIKELY( current_epoch ) ) fd_bank_vote_states_prev_end_locking_query( bank );
519 0 : else fd_bank_vote_states_prev_prev_end_locking_query( bank );
520 :
521 0 : fd_multi_epoch_leaders_stake_msg_init( ctx->mleaders, fd_type_pun_const( stake_weights_msg ) );
522 0 : fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
523 0 : }
524 :
525 : /**********************************************************************/
526 : /* Vote tower publishing helpers */
527 : /**********************************************************************/
528 :
529 : /* fd_replay_out_vote_tower_from_funk queries Funk for the state of the vote
530 : account with the given pubkey, and copies the state into the given
531 : fd_replay_tower_t structure. The account data is simply copied as-is.
532 :
533 : Parameters:
534 : - funk: The funk database instance to query vote account data from
535 : - funk_txn: The funk transaction context for consistent reads
536 : - pubkey: The public key of the vote account to retrieve
537 : - stake: The stake amount associated with this vote account
538 : - vote_tower_out: Output structure to populate with vote state information
539 :
540 : Failure modes:
541 : - Vote account data is too large (returns -1)
542 : - Vote account is not found in Funk (returns -1) */
543 : static int
544 : fd_replay_out_vote_tower_from_funk(
545 : fd_accdb_user_t * accdb,
546 : fd_funk_txn_xid_t const * xid,
547 : fd_pubkey_t const * pubkey,
548 : ulong stake,
549 : fd_replay_tower_t * vote_tower_out
550 0 : ) {
551 :
552 0 : fd_memset( vote_tower_out, 0, sizeof(fd_replay_tower_t) );
553 0 : vote_tower_out->key = *pubkey;
554 0 : vote_tower_out->stake = stake;
555 :
556 : /* Speculatively copy out the raw vote account state from Funk */
557 0 : for(;;) {
558 0 : fd_memset( vote_tower_out->acc, 0, sizeof(vote_tower_out->acc) );
559 :
560 0 : fd_accdb_peek_t peek[1];
561 0 : if( FD_UNLIKELY( !fd_accdb_peek( accdb, peek, xid, pubkey->uc ) ) ) {
562 : /* FIXME crash here? */
563 0 : FD_LOG_WARNING(( "vote account not found. address: %s", FD_BASE58_ENC_32_ALLOCA( pubkey->uc ) ));
564 0 : return -1;
565 0 : }
566 :
567 0 : ulong data_sz = fd_accdb_ref_data_sz( peek->acc );
568 0 : if( FD_UNLIKELY( data_sz > sizeof(vote_tower_out->acc) ) ) {
569 0 : FD_LOG_WARNING(( "vote account %s has too large data. dlen %lu > %lu",
570 0 : FD_BASE58_ENC_32_ALLOCA( pubkey->uc ),
571 0 : data_sz,
572 0 : sizeof(vote_tower_out->acc) ));
573 0 : return -1;
574 0 : }
575 :
576 0 : fd_memcpy( vote_tower_out->acc, fd_accdb_ref_data_const( peek->acc ), data_sz );
577 0 : vote_tower_out->acc_sz = data_sz;
578 :
579 0 : if( FD_LIKELY( fd_accdb_peek_test( peek ) ) ) break;
580 0 : FD_SPIN_PAUSE();
581 0 : }
582 :
583 0 : return 0;
584 0 : }
585 :
586 : /* This function buffers all the vote account towers that Tower needs at
587 : the end of this slot into the ctx->vote_tower_out buffer. These will
588 : then be published in after_credit.
589 :
590 : This function should be called at the end of a slot, before any epoch
591 : boundary processing. */
592 : static void
593 : buffer_vote_towers( fd_replay_tile_t * ctx,
594 : fd_funk_txn_xid_t const * xid,
595 0 : fd_bank_t * bank ) {
596 0 : ctx->vote_tower_out_idx = 0UL;
597 0 : ctx->vote_tower_out_len = 0UL;
598 :
599 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_prev_locking_query( bank );
600 0 : fd_vote_states_iter_t iter_[1];
601 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
602 0 : !fd_vote_states_iter_done( iter );
603 0 : fd_vote_states_iter_next( iter ) ) {
604 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
605 0 : if( FD_UNLIKELY( vote_state->stake == 0 ) ) continue; /* skip unstaked vote accounts */
606 0 : fd_pubkey_t const * vote_account_pubkey = &vote_state->vote_account;
607 0 : if( FD_UNLIKELY( ctx->vote_tower_out_len >= (FD_REPLAY_TOWER_VOTE_ACC_MAX-1UL) ) ) FD_LOG_ERR(( "vote_tower_out_len too large" ));
608 0 : if( FD_UNLIKELY( fd_replay_out_vote_tower_from_funk( ctx->accdb,
609 0 : xid,
610 0 : vote_account_pubkey,
611 0 : vote_state->stake,
612 0 : &ctx->vote_tower_out[ctx->vote_tower_out_len++] ) ) ) {
613 0 : FD_LOG_DEBUG(( "failed to get vote state for vote account %s", FD_BASE58_ENC_32_ALLOCA( vote_account_pubkey->uc ) ));
614 0 : }
615 0 : }
616 0 : fd_bank_vote_states_prev_end_locking_query( bank );
617 0 : }
618 :
619 : /* This function publishes the next vote tower in the
620 : ctx->vote_tower_out buffer to the tower tile.
621 :
622 : This function should be called in after_credit, after all the vote
623 : towers for the end of a slot have been buffered in
624 : ctx->vote_tower_out. */
625 :
626 : static void
627 : publish_next_vote_tower( fd_replay_tile_t * ctx,
628 0 : fd_stem_context_t * stem ) {
629 0 : int som = ctx->vote_tower_out_idx==0;
630 0 : int eom = ctx->vote_tower_out_idx==( ctx->vote_tower_out_len - 1 );
631 :
632 0 : fd_replay_tower_t * vote_state = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
633 0 : *vote_state = ctx->vote_tower_out[ ctx->vote_tower_out_idx ];
634 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_VOTE_STATE, ctx->replay_out->chunk, sizeof(fd_replay_tower_t), fd_frag_meta_ctl( 0UL, som, eom, 0 ), 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
635 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_tower_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
636 :
637 0 : ctx->vote_tower_out_idx++;
638 0 : }
639 :
640 : /**********************************************************************/
641 : /* Transaction execution state machine helpers */
642 : /**********************************************************************/
643 :
644 : static fd_bank_t *
645 : replay_block_start( fd_replay_tile_t * ctx,
646 : fd_stem_context_t * stem,
647 : ulong bank_idx,
648 : ulong parent_bank_idx,
649 0 : ulong slot ) {
650 0 : long before = fd_log_wallclock();
651 :
652 : /* Switch to a new block that we don't have a bank for. */
653 :
654 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
655 0 : if( FD_UNLIKELY( !bank ) ) {
656 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
657 0 : }
658 0 : if( FD_UNLIKELY( bank->flags!=FD_BANK_FLAGS_INIT ) ) {
659 0 : FD_LOG_CRIT(( "invariant violation: bank is not in correct state for bank index %lu", bank_idx ));
660 0 : }
661 :
662 0 : bank->preparation_begin_nanos = before;
663 :
664 0 : fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
665 0 : if( FD_UNLIKELY( !parent_bank ) ) {
666 0 : FD_LOG_CRIT(( "invariant violation: parent bank is NULL for bank index %lu", parent_bank_idx ));
667 0 : }
668 0 : if( FD_UNLIKELY( !(parent_bank->flags&FD_BANK_FLAGS_FROZEN) ) ) {
669 0 : FD_LOG_CRIT(( "invariant violation: parent bank is not frozen for bank index %lu", parent_bank_idx ));
670 0 : }
671 0 : ulong parent_slot = fd_bank_slot_get( parent_bank );
672 :
673 : /* Clone the bank from the parent. We must special case the first
674 : slot that is executed as the snapshot does not provide a parent
675 : block id. */
676 :
677 0 : bank = fd_banks_clone_from_parent( ctx->banks, bank_idx, parent_bank_idx );
678 0 : if( FD_UNLIKELY( !bank ) ) {
679 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
680 0 : }
681 0 : fd_bank_slot_set( bank, slot );
682 0 : fd_bank_parent_slot_set( bank, parent_slot );
683 0 : bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->txncache_fork_id );
684 :
685 : /* Create a new funk txn for the block. */
686 :
687 0 : fd_funk_txn_xid_t xid = { .ul = { slot, bank_idx } };
688 0 : fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
689 0 : fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
690 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
691 :
692 : /* Update any required runtime state and handle any potential epoch
693 : boundary change. */
694 :
695 0 : if( ctx->capture_ctx ) {
696 0 : fd_solcap_writer_set_slot( ctx->capture_ctx->capture, slot );
697 0 : }
698 :
699 0 : fd_bank_shred_cnt_set( bank, 0UL );
700 0 : fd_bank_execution_fees_set( bank, 0UL );
701 0 : fd_bank_priority_fees_set( bank, 0UL );
702 :
703 0 : fd_bank_has_identity_vote_set( bank, 0 );
704 :
705 : /* Set the tick height. */
706 0 : fd_bank_tick_height_set( bank, fd_bank_max_tick_height_get( bank ) );
707 :
708 : /* Update block height. */
709 0 : fd_bank_block_height_set( bank, fd_bank_block_height_get( bank ) + 1UL );
710 :
711 0 : ulong * max_tick_height = fd_bank_max_tick_height_modify( bank );
712 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
713 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( ticks_per_slot, slot, max_tick_height ) ) ) {
714 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
715 0 : }
716 0 : bank->flags |= fd_ulong_if( ctx->tx_metadata_storage, FD_BANK_FLAGS_EXEC_RECORDING, 0UL );
717 :
718 0 : int is_epoch_boundary = 0;
719 0 : fd_runtime_block_pre_execute_process_new_epoch(
720 0 : ctx->banks,
721 0 : bank,
722 0 : ctx->accdb,
723 0 : &xid,
724 0 : ctx->capture_ctx,
725 0 : &ctx->runtime_stack,
726 0 : &is_epoch_boundary );
727 0 : if( FD_UNLIKELY( is_epoch_boundary ) ) publish_stake_weights( ctx, stem, bank, 1 );
728 :
729 0 : FD_TEST( !fd_runtime_block_execute_prepare( bank, ctx->accdb, &xid, &ctx->runtime_stack, ctx->capture_ctx ) );
730 0 : return bank;
731 0 : }
732 :
733 : static void
734 : publish_slot_completed( fd_replay_tile_t * ctx,
735 : fd_stem_context_t * stem,
736 : fd_bank_t * bank,
737 0 : int is_initial ) {
738 :
739 0 : ulong slot = fd_bank_slot_get( bank );
740 :
741 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank->idx ];
742 :
743 : /* HACKY: hacky way of checking if we should send a null parent block
744 : id */
745 0 : fd_hash_t parent_block_id = {0};
746 0 : if( FD_UNLIKELY( !is_initial ) ) {
747 0 : parent_block_id = ctx->block_id_arr[ bank->parent_idx ].block_id;
748 0 : }
749 :
750 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank );
751 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last( fd_bank_block_hash_queue_query( bank ) );
752 0 : FD_TEST( bank_hash );
753 0 : FD_TEST( block_hash );
754 :
755 0 : if( FD_LIKELY( !is_initial ) ) fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
756 :
757 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
758 0 : ulong slot_idx;
759 0 : ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
760 :
761 0 : ctx->metrics.slots_total++;
762 0 : ctx->metrics.transactions_total = fd_bank_txn_count_get( bank );
763 :
764 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
765 0 : slot_info->slot = slot;
766 0 : slot_info->root_slot = ctx->consensus_root_slot;
767 0 : slot_info->epoch = epoch;
768 0 : slot_info->slot_in_epoch = slot_idx;
769 0 : slot_info->block_height = fd_bank_block_height_get( bank );
770 0 : slot_info->parent_slot = fd_bank_parent_slot_get( bank );
771 0 : slot_info->block_id = block_id_ele->block_id;
772 0 : slot_info->parent_block_id = parent_block_id;
773 0 : slot_info->bank_hash = *bank_hash;
774 0 : slot_info->block_hash = *block_hash;
775 0 : slot_info->transaction_count = fd_bank_txn_count_get( bank );
776 :
777 0 : slot_info->first_fec_set_received_nanos = bank->first_fec_set_received_nanos;
778 0 : slot_info->preparation_begin_nanos = bank->preparation_begin_nanos;
779 0 : slot_info->first_transaction_scheduled_nanos = bank->first_transaction_scheduled_nanos;
780 0 : slot_info->last_transaction_finished_nanos = bank->last_transaction_finished_nanos;
781 0 : slot_info->completion_time_nanos = fd_log_wallclock();
782 :
783 : /* refcnt should be incremented by 1 for each consumer that uses
784 : `bank_idx`. Each consumer should decrement the bank's refcnt once
785 : they are done usin the bank. */
786 0 : if( FD_LIKELY( ctx->gui_enabled ) ) bank->refcnt++; /* gui tile */
787 0 : slot_info->bank_idx = bank->idx;
788 :
789 0 : slot_info->parent_bank_idx = ULONG_MAX;
790 0 : fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
791 0 : if( FD_LIKELY( parent_bank && ctx->gui_enabled ) ) {
792 0 : parent_bank->refcnt++;
793 0 : slot_info->parent_bank_idx = parent_bank->idx;
794 0 : }
795 :
796 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_SLOT_COMPLETED, ctx->replay_out->chunk, sizeof(fd_replay_slot_completed_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
797 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_slot_completed_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
798 0 : }
799 :
800 : static void
801 : replay_block_finalize( fd_replay_tile_t * ctx,
802 : fd_stem_context_t * stem,
803 0 : fd_bank_t * bank ) {
804 :
805 0 : bank->last_transaction_finished_nanos = fd_log_wallclock();
806 :
807 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) fd_solcap_writer_flush( ctx->capture_ctx->capture );
808 :
809 0 : FD_TEST( !(bank->flags&FD_BANK_FLAGS_FROZEN) );
810 :
811 0 : ulong slot = fd_bank_slot_get( bank );
812 0 : fd_funk_txn_xid_t xid = { .ul = { slot, bank->idx } };
813 :
814 : /* Set poh hash in bank. */
815 0 : fd_hash_t * poh = fd_sched_get_poh( ctx->sched, bank->idx );
816 0 : fd_bank_poh_set( bank, *poh );
817 :
818 : /* Set shred count in bank. */
819 0 : fd_bank_shred_cnt_set( bank, fd_sched_get_shred_cnt( ctx->sched, bank->idx ) );
820 :
821 : /* Do hashing and other end-of-block processing. */
822 0 : fd_runtime_block_execute_finalize( bank, ctx->accdb, &xid, ctx->capture_ctx, 1 );
823 :
824 : /* Mark the bank as frozen. */
825 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
826 :
827 : /* Copy the vote tower of all the vote accounts into the buffer,
828 : which will be published in after_credit. */
829 0 : buffer_vote_towers( ctx, &xid, bank );
830 :
831 : /**********************************************************************/
832 : /* Bank hash comparison, and halt if there's a mismatch after replay */
833 : /**********************************************************************/
834 :
835 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank );
836 0 : FD_TEST( bank_hash );
837 :
838 0 : fd_bank_hash_cmp_t * bank_hash_cmp = ctx->bank_hash_cmp;
839 0 : fd_bank_hash_cmp_lock( bank_hash_cmp );
840 0 : fd_bank_hash_cmp_insert( bank_hash_cmp, fd_bank_slot_get( bank ), bank_hash, 1, 0 );
841 :
842 : /* Try to move the bank hash comparison watermark forward */
843 0 : for( ulong cmp_slot = bank_hash_cmp->watermark + 1; cmp_slot < fd_bank_slot_get( bank ); cmp_slot++ ) {
844 0 : if( FD_UNLIKELY( !ctx->enable_bank_hash_cmp ) ) {
845 0 : bank_hash_cmp->watermark = cmp_slot;
846 0 : break;
847 0 : }
848 0 : int rc = fd_bank_hash_cmp_check( bank_hash_cmp, cmp_slot );
849 0 : switch ( rc ) {
850 0 : case -1:
851 : /* Mismatch */
852 0 : FD_LOG_CRIT(( "Bank hash mismatch on slot: %lu. Halting.", cmp_slot ));
853 0 : break;
854 0 : case 0:
855 : /* Not ready */
856 0 : break;
857 0 : case 1:
858 : /* Match*/
859 0 : bank_hash_cmp->watermark = cmp_slot;
860 0 : break;
861 0 : default:;
862 0 : }
863 0 : }
864 :
865 0 : fd_bank_hash_cmp_unlock( bank_hash_cmp );
866 :
867 : /* Must be last so we can measure completion time correctly, even
868 : though we could technically do this before the hash cmp and vote
869 : tower stuff. */
870 0 : publish_slot_completed( ctx, stem, bank, 0 );
871 :
872 : /* If enabled, dump the block to a file and reset the dumping
873 : context state */
874 0 : if( FD_UNLIKELY( ctx->capture_ctx && ctx->capture_ctx->dump_block_to_pb ) ) {
875 0 : fd_dump_block_to_protobuf( ctx->block_dump_ctx, ctx->banks, bank, ctx->accdb->funk, ctx->capture_ctx );
876 0 : fd_block_dump_context_reset( ctx->block_dump_ctx );
877 0 : }
878 0 : }
879 :
880 : /**********************************************************************/
881 : /* Leader bank management */
882 : /**********************************************************************/
883 :
884 : static fd_bank_t *
885 : prepare_leader_bank( fd_replay_tile_t * ctx,
886 : ulong slot,
887 : long now,
888 : fd_hash_t const * parent_block_id,
889 0 : fd_stem_context_t * stem ) {
890 0 : long before = fd_log_wallclock();
891 :
892 : /* Make sure that we are not already leader. */
893 0 : FD_TEST( ctx->leader_bank==NULL );
894 :
895 0 : fd_block_id_ele_t * parent_ele = fd_block_id_map_ele_query( ctx->block_id_map, parent_block_id, NULL, ctx->block_id_arr );
896 0 : if( FD_UNLIKELY( !parent_ele ) ) {
897 0 : FD_LOG_CRIT(( "invariant violation: parent bank index not found for merkle root %s", FD_BASE58_ENC_32_ALLOCA( parent_block_id->uc ) ));
898 0 : }
899 0 : ulong parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent_ele );
900 :
901 0 : fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
902 0 : if( FD_UNLIKELY( !parent_bank ) ) {
903 0 : FD_LOG_CRIT(( "invariant violation: parent bank not found for bank index %lu", parent_bank_idx ));
904 0 : }
905 0 : ulong parent_slot = fd_bank_slot_get( parent_bank );
906 :
907 0 : ctx->leader_bank = fd_banks_new_bank( ctx->banks, parent_bank_idx, now );
908 0 : if( FD_UNLIKELY( !ctx->leader_bank ) ) {
909 0 : FD_LOG_CRIT(( "invariant violation: leader bank is NULL for slot %lu", slot ));
910 0 : }
911 :
912 0 : if( FD_UNLIKELY( !fd_banks_clone_from_parent( ctx->banks, ctx->leader_bank->idx, parent_bank_idx ) ) ) {
913 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for slot %lu", slot ));
914 0 : }
915 :
916 0 : ctx->leader_bank->preparation_begin_nanos = before;
917 :
918 0 : fd_bank_slot_set( ctx->leader_bank, slot );
919 0 : fd_bank_parent_slot_set( ctx->leader_bank, parent_slot );
920 0 : ctx->leader_bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->txncache_fork_id );
921 : /* prepare the funk transaction for the leader bank */
922 0 : fd_funk_txn_xid_t xid = { .ul = { slot, ctx->leader_bank->idx } };
923 0 : fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
924 0 : fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
925 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
926 :
927 0 : fd_bank_execution_fees_set( ctx->leader_bank, 0UL );
928 0 : fd_bank_priority_fees_set( ctx->leader_bank, 0UL );
929 0 : fd_bank_shred_cnt_set( ctx->leader_bank, 0UL );
930 :
931 : /* Set the tick height. */
932 0 : fd_bank_tick_height_set( ctx->leader_bank, fd_bank_max_tick_height_get( ctx->leader_bank ) );
933 :
934 : /* Update block height. */
935 0 : fd_bank_block_height_set( ctx->leader_bank, fd_bank_block_height_get( ctx->leader_bank ) + 1UL );
936 :
937 0 : ulong * max_tick_height = fd_bank_max_tick_height_modify( ctx->leader_bank );
938 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( ctx->leader_bank );
939 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( ticks_per_slot, slot, max_tick_height ) ) ) {
940 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
941 0 : }
942 :
943 0 : ctx->leader_bank->flags |= fd_ulong_if( ctx->tx_metadata_storage, FD_BANK_FLAGS_EXEC_RECORDING, 0UL );
944 :
945 0 : int is_epoch_boundary = 0;
946 0 : fd_runtime_block_pre_execute_process_new_epoch(
947 0 : ctx->banks,
948 0 : ctx->leader_bank,
949 0 : ctx->accdb,
950 0 : &xid,
951 0 : ctx->capture_ctx,
952 0 : &ctx->runtime_stack,
953 0 : &is_epoch_boundary );
954 0 : if( FD_UNLIKELY( is_epoch_boundary ) ) publish_stake_weights( ctx, stem, ctx->leader_bank, 1 );
955 :
956 0 : FD_TEST( !fd_runtime_block_execute_prepare( ctx->leader_bank, ctx->accdb, &xid, &ctx->runtime_stack, ctx->capture_ctx ) );
957 :
958 : /* Now that a bank has been created for the leader slot, increment the
959 : reference count until we are done with the leader slot. */
960 0 : ctx->leader_bank->refcnt++;
961 :
962 0 : return ctx->leader_bank;
963 0 : }
964 :
965 : static void
966 : fini_leader_bank( fd_replay_tile_t * ctx,
967 0 : fd_stem_context_t * stem ) {
968 :
969 0 : FD_TEST( ctx->leader_bank!=NULL );
970 0 : FD_TEST( ctx->is_leader );
971 0 : FD_TEST( ctx->recv_block_id );
972 0 : FD_TEST( ctx->recv_poh );
973 :
974 0 : ctx->leader_bank->last_transaction_finished_nanos = fd_log_wallclock();
975 :
976 0 : fd_banks_mark_bank_frozen( ctx->banks, ctx->leader_bank );
977 :
978 0 : fd_sched_block_add_done( ctx->sched, ctx->leader_bank->idx, ctx->leader_bank->parent_idx );
979 :
980 0 : ulong curr_slot = fd_bank_slot_get( ctx->leader_bank );
981 :
982 : /* Do hashing and other end-of-block processing */
983 0 : fd_funk_txn_map_t * txn_map = fd_funk_txn_map( ctx->accdb->funk );
984 0 : if( FD_UNLIKELY( !txn_map->map ) ) {
985 0 : FD_LOG_ERR(( "Could not find valid funk transaction map" ));
986 0 : }
987 0 : fd_funk_txn_xid_t xid = { .ul = { curr_slot, ctx->leader_bank->idx } };
988 :
989 0 : fd_runtime_block_execute_finalize( ctx->leader_bank, ctx->accdb, &xid, ctx->capture_ctx, 0 );
990 :
991 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( ctx->leader_bank );
992 0 : FD_TEST( bank_hash );
993 :
994 0 : fd_bank_hash_cmp_t * bank_hash_cmp = ctx->bank_hash_cmp;
995 0 : fd_bank_hash_cmp_lock( bank_hash_cmp );
996 0 : fd_bank_hash_cmp_insert( bank_hash_cmp, fd_bank_slot_get( ctx->leader_bank ), bank_hash, 1, 0 );
997 :
998 : /* Try to move the bank hash comparison watermark forward */
999 0 : for( ulong cmp_slot = bank_hash_cmp->watermark + 1; cmp_slot < fd_bank_slot_get( ctx->leader_bank ); cmp_slot++ ) {
1000 0 : if( FD_UNLIKELY( !ctx->enable_bank_hash_cmp ) ) {
1001 0 : bank_hash_cmp->watermark = cmp_slot;
1002 0 : break;
1003 0 : }
1004 0 : int rc = fd_bank_hash_cmp_check( bank_hash_cmp, cmp_slot );
1005 0 : switch ( rc ) {
1006 0 : case -1:
1007 : /* Mismatch */
1008 0 : FD_LOG_WARNING(( "Bank hash mismatch on slot: %lu. Halting.", cmp_slot ));
1009 0 : break;
1010 0 : case 0:
1011 : /* Not ready */
1012 0 : break;
1013 0 : case 1:
1014 : /* Match*/
1015 0 : bank_hash_cmp->watermark = cmp_slot;
1016 0 : break;
1017 0 : default:;
1018 0 : }
1019 0 : }
1020 :
1021 0 : fd_bank_hash_cmp_unlock( bank_hash_cmp );
1022 :
1023 0 : publish_slot_completed( ctx, stem, ctx->leader_bank, 0 );
1024 :
1025 : /* Copy the vote tower of all the vote accounts into the buffer,
1026 : which will be published in after_credit. */
1027 0 : buffer_vote_towers( ctx, &xid, ctx->leader_bank );
1028 :
1029 : /* The reference on the bank is finally no longer needed. */
1030 0 : ctx->leader_bank->refcnt--;
1031 :
1032 : /* We are no longer leader so we can clear the bank index we use for
1033 : being the leader. */
1034 0 : ctx->leader_bank = NULL;
1035 0 : ctx->recv_block_id = 0;
1036 0 : ctx->recv_poh = 0;
1037 0 : ctx->is_leader = 0;
1038 0 : }
1039 :
1040 : static void
1041 : publish_root_advanced( fd_replay_tile_t * ctx,
1042 0 : fd_stem_context_t * stem ) {
1043 :
1044 : /* FIXME: for now we want to send the child of the consensus root to
1045 : avoid data races with funk root advancing. This is a temporary
1046 : hack because currently it is not safe to query against the xid for
1047 : the root that is being advanced in funk. This doesn't eliminate
1048 : the data race that exists in funk, but reduces how often it occurs.
1049 :
1050 : Case that causes a data race:
1051 : replay: we are advancing the root from slot A->B
1052 : resolv: we are resolving ALUTs against slot B */
1053 :
1054 0 : fd_bank_t * consensus_root_bank = fd_banks_bank_query( ctx->banks, ctx->consensus_root_bank_idx );
1055 0 : if( FD_UNLIKELY( !consensus_root_bank ) ) {
1056 0 : FD_LOG_CRIT(( "invariant violation: consensus root bank is NULL at bank index %lu", ctx->consensus_root_bank_idx ));
1057 0 : }
1058 :
1059 0 : if( FD_UNLIKELY( consensus_root_bank->child_idx==ULONG_MAX ) ) {
1060 0 : return;
1061 0 : }
1062 :
1063 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, consensus_root_bank->child_idx );
1064 0 : if( FD_UNLIKELY( !bank ) ) {
1065 0 : FD_LOG_CRIT(( "invariant violation: consensus root bank child is NULL at bank index %lu", consensus_root_bank->child_idx ));
1066 0 : }
1067 :
1068 : /* Increment the reference count on the consensus root bank to account
1069 : for the number of exec tiles that are waiting on it. */
1070 0 : bank->refcnt += ctx->resolv_tile_cnt;
1071 :
1072 0 : fd_replay_root_advanced_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1073 0 : msg->bank_idx = bank->idx;
1074 :
1075 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_ROOT_ADVANCED, ctx->replay_out->chunk, sizeof(fd_replay_root_advanced_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1076 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_root_advanced_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
1077 0 : }
1078 :
1079 : /* init_funk performs pre-flight checks for the account database and
1080 : program cache. Ensures that the account database was set up
1081 : correctly by bootstrap components (e.g. genesis or snapshot loader).
1082 : Mirrors the account database's fork tree down to the program cache. */
1083 :
1084 : static void
1085 : init_funk( fd_replay_tile_t * ctx,
1086 0 : ulong bank_slot ) {
1087 : /* Ensure that the loaded bank root corresponds to the account
1088 : database's root. */
1089 0 : fd_funk_t * funk = ctx->accdb_admin->funk;
1090 0 : if( FD_UNLIKELY( !funk->shmem ) ) {
1091 0 : FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to database shared memory objects" ));
1092 0 : }
1093 0 : fd_funk_txn_xid_t const * accdb_pub = fd_funk_last_publish( funk );
1094 0 : if( FD_UNLIKELY( accdb_pub->ul[0]!=bank_slot ) ) {
1095 0 : FD_LOG_CRIT(( "failed to initialize account database: accdb is at slot %lu, but chain state is at slot %lu\n"
1096 0 : "This is a bug in startup components.",
1097 0 : accdb_pub->ul[0], bank_slot ));
1098 0 : }
1099 0 : if( FD_UNLIKELY( fd_funk_last_publish_is_frozen( funk ) ) ) {
1100 0 : FD_LOG_CRIT(( "failed to initialize account database: accdb fork graph is not clean.\n"
1101 0 : "The account database should only contain state for the root slot at this point,\n"
1102 0 : "but there are incomplete database transactions leftover.\n"
1103 0 : "This is a bug in startup components." ));
1104 0 : }
1105 :
1106 : /* The program cache tracks the account database's fork graph at all
1107 : times. Perform initial synchronization: pivot from funk 'root' (a
1108 : sentinel XID) to 'last publish' (the bootstrap root slot). */
1109 0 : if( FD_UNLIKELY( !ctx->progcache_admin->funk->shmem ) ) {
1110 0 : FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to program cache" ));
1111 0 : }
1112 0 : fd_progcache_clear( ctx->progcache_admin );
1113 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, fd_funk_root( ctx->progcache_admin->funk ), fd_funk_last_publish( ctx->accdb->funk ) );
1114 0 : fd_progcache_txn_advance_root( ctx->progcache_admin, fd_funk_last_publish( ctx->accdb->funk ) );
1115 0 : }
1116 :
1117 : static void
1118 0 : init_after_snapshot( fd_replay_tile_t * ctx ) {
1119 : /* Now that the snapshot has been loaded in, we have to refresh the
1120 : stake delegations since the manifest does not contain the full set
1121 : of data required for the stake delegations. See
1122 : fd_stake_delegations.h for why this is required. */
1123 :
1124 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
1125 0 : if( FD_UNLIKELY( !bank ) ) {
1126 0 : FD_LOG_CRIT(( "invariant violation: replay bank is NULL at bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
1127 0 : }
1128 :
1129 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->idx } };
1130 0 : init_funk( ctx, fd_bank_slot_get( bank ) );
1131 :
1132 0 : fd_stake_delegations_t * root_delegations = fd_banks_stake_delegations_root_query( ctx->banks );
1133 :
1134 0 : fd_stake_delegations_refresh( root_delegations, ctx->accdb->funk, &xid );
1135 :
1136 : /* After both snapshots have been loaded in, we can determine if we should
1137 : start distributing rewards. */
1138 :
1139 0 : fd_rewards_recalculate_partitioned_rewards( ctx->banks, bank, ctx->accdb->funk, &xid, &ctx->runtime_stack, ctx->capture_ctx );
1140 :
1141 0 : ulong snapshot_slot = fd_bank_slot_get( bank );
1142 0 : if( FD_UNLIKELY( !snapshot_slot ) ) {
1143 : /* Genesis-specific setup. */
1144 : /* FIXME: This branch does not set up a new block exec ctx
1145 : properly. Needs to do whatever prepare_new_block_execution
1146 : does, but just hacking that in breaks stuff. */
1147 0 : fd_runtime_update_leaders( bank, &ctx->runtime_stack );
1148 :
1149 0 : ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
1150 0 : fd_hash_t * poh = fd_bank_poh_modify( bank );
1151 0 : while( hashcnt_per_slot-- ) {
1152 0 : fd_sha256_hash( poh->hash, 32UL, poh->hash );
1153 0 : }
1154 :
1155 0 : FD_TEST( !fd_runtime_block_execute_prepare( bank, ctx->accdb, &xid, &ctx->runtime_stack, ctx->capture_ctx ) );
1156 0 : fd_runtime_block_execute_finalize( bank, ctx->accdb, &xid, ctx->capture_ctx, 1 );
1157 :
1158 0 : snapshot_slot = 0UL;
1159 0 : }
1160 :
1161 : /* Initialize consensus structures post-snapshot */
1162 :
1163 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
1164 :
1165 0 : fd_bank_hash_cmp_t * bank_hash_cmp = ctx->bank_hash_cmp;
1166 :
1167 0 : fd_vote_states_iter_t iter_[1];
1168 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
1169 0 : fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
1170 0 : bank_hash_cmp->total_stake += vote_state->stake;
1171 0 : }
1172 0 : bank_hash_cmp->watermark = snapshot_slot;
1173 :
1174 0 : fd_bank_vote_states_end_locking_query( bank );
1175 :
1176 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) fd_solcap_writer_flush( ctx->capture_ctx->capture );
1177 0 : }
1178 :
1179 : static inline int
1180 : maybe_become_leader( fd_replay_tile_t * ctx,
1181 0 : fd_stem_context_t * stem ) {
1182 0 : FD_TEST( ctx->is_booted );
1183 0 : if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX || ctx->is_leader || !ctx->has_identity_vote_rooted || ctx->replay_out->idx==ULONG_MAX ) ) return 0;
1184 :
1185 0 : FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
1186 0 : long now = fd_tickcount();
1187 0 : if( FD_LIKELY( now<ctx->next_leader_tickcount ) ) return 0;
1188 :
1189 : /* TODO:
1190 : if( FD_UNLIKELY( ctx->halted_switching_key ) ) return 0; */
1191 :
1192 : /* If a prior leader is still in the process of publishing their slot,
1193 : delay ours to let them finish ... unless they are so delayed that
1194 : we risk getting skipped by the leader following us. 1.2 seconds
1195 : is a reasonable default here, although any value between 0 and 1.6
1196 : seconds could be considered reasonable. This is arbitrary and
1197 : chosen due to intuition. */
1198 0 : if( FD_UNLIKELY( now<ctx->next_leader_tickcount+(long)(3.0*ctx->slot_duration_ticks) ) ) {
1199 : /* If the max_active_descendant is >= next_leader_slot, we waited
1200 : too long and a leader after us started publishing to try and skip
1201 : us. Just start our leader slot immediately, we might win ... */
1202 0 : if( FD_LIKELY( ctx->max_active_descendant>=ctx->reset_slot && ctx->max_active_descendant<ctx->next_leader_slot ) ) {
1203 : /* If one of the leaders between the reset slot and our leader
1204 : slot is in the process of publishing (they have a descendant
1205 : bank that is in progress of being replayed), then keep waiting.
1206 : We probably wouldn't get a leader slot out before they
1207 : finished.
1208 :
1209 : Unless... we are past the deadline to start our slot by more
1210 : than 1.2 seconds, in which case we should probably start it to
1211 : avoid getting skipped by the leader behind us. */
1212 0 : return 0;
1213 0 : }
1214 0 : }
1215 :
1216 0 : long now_nanos = fd_log_wallclock();
1217 :
1218 0 : ctx->is_leader = 1;
1219 0 : ctx->recv_poh = 0;
1220 0 : ctx->recv_block_id = 0;
1221 :
1222 0 : FD_TEST( ctx->highwater_leader_slot==ULONG_MAX || ctx->highwater_leader_slot<ctx->next_leader_slot );
1223 0 : ctx->highwater_leader_slot = ctx->next_leader_slot;
1224 :
1225 0 : FD_LOG_INFO(( "becoming leader for slot %lu, parent slot is %lu", ctx->next_leader_slot, ctx->reset_slot ));
1226 :
1227 : /* Acquires bank, sets up initial state, and refcnts it. */
1228 0 : fd_bank_t * bank = prepare_leader_bank( ctx, ctx->next_leader_slot, now_nanos, &ctx->reset_block_id, stem );
1229 :
1230 0 : fd_became_leader_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1231 0 : msg->slot = ctx->next_leader_slot;
1232 0 : msg->slot_start_ns = now_nanos;
1233 0 : msg->slot_end_ns = now_nanos+(long)ctx->slot_duration_nanos;
1234 0 : msg->bank = NULL;
1235 0 : msg->bank_idx = bank->idx;
1236 0 : msg->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1237 0 : msg->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
1238 0 : msg->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)msg->ticks_per_slot);
1239 :
1240 0 : if( FD_UNLIKELY( msg->hashcnt_per_tick==1UL ) ) {
1241 : /* Low power producer, maximum of one microblock per tick in the slot */
1242 0 : msg->max_microblocks_in_slot = msg->ticks_per_slot;
1243 0 : } else {
1244 : /* See the long comment in after_credit for this limit */
1245 0 : msg->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, msg->ticks_per_slot*(msg->hashcnt_per_tick-1UL) );
1246 0 : }
1247 :
1248 0 : msg->total_skipped_ticks = msg->ticks_per_slot*(ctx->next_leader_slot-ctx->reset_slot);
1249 0 : msg->epoch = fd_slot_to_epoch( fd_bank_epoch_schedule_query( bank ), ctx->next_leader_slot, NULL );
1250 0 : fd_memset( msg->bundle, 0, sizeof(msg->bundle) );
1251 :
1252 0 : fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( bank );
1253 :
1254 0 : msg->limits.slot_max_cost = cost_tracker->block_cost_limit;
1255 0 : msg->limits.slot_max_vote_cost = cost_tracker->vote_cost_limit;
1256 0 : msg->limits.slot_max_write_cost_per_acct = cost_tracker->account_cost_limit;
1257 :
1258 0 : fd_bank_cost_tracker_end_locking_query( bank );
1259 :
1260 0 : if( FD_UNLIKELY( msg->ticks_per_slot+msg->total_skipped_ticks>USHORT_MAX ) ) {
1261 : /* There can be at most USHORT_MAX skipped ticks, because the
1262 : parent_offset field in the shred data is only 2 bytes wide. */
1263 0 : FD_LOG_ERR(( "too many skipped ticks %lu for slot %lu, chain must halt", msg->ticks_per_slot+msg->total_skipped_ticks, ctx->next_leader_slot ));
1264 0 : }
1265 :
1266 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_BECAME_LEADER, ctx->replay_out->chunk, sizeof(fd_became_leader_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1267 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_became_leader_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
1268 :
1269 0 : ctx->next_leader_slot = ULONG_MAX;
1270 0 : ctx->next_leader_tickcount = LONG_MAX;
1271 :
1272 0 : return 1;
1273 0 : }
1274 :
1275 : static void
1276 : process_poh_message( fd_replay_tile_t * ctx,
1277 0 : fd_poh_leader_slot_ended_t const * slot_ended ) {
1278 :
1279 0 : FD_TEST( ctx->is_booted );
1280 0 : FD_TEST( ctx->is_leader );
1281 0 : FD_TEST( ctx->leader_bank!=NULL );
1282 :
1283 0 : FD_TEST( ctx->highwater_leader_slot>=slot_ended->slot );
1284 0 : FD_TEST( ctx->next_leader_slot>ctx->highwater_leader_slot );
1285 :
1286 : /* Update the poh hash in the bank. We will want to maintain a refcnt
1287 : on the bank until we have recieved the block id for the block after
1288 : it has been shredded. */
1289 :
1290 0 : memcpy( fd_bank_poh_modify( ctx->leader_bank ), slot_ended->blockhash, sizeof(fd_hash_t) );
1291 :
1292 0 : ctx->recv_poh = 1;
1293 0 : }
1294 :
1295 : static void
1296 : publish_reset( fd_replay_tile_t * ctx,
1297 : fd_stem_context_t * stem,
1298 0 : fd_bank_t const * bank ) {
1299 0 : if( FD_UNLIKELY( ctx->replay_out->idx==ULONG_MAX ) ) return;
1300 :
1301 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last( fd_bank_block_hash_queue_query( bank ) );
1302 0 : FD_TEST( block_hash );
1303 :
1304 0 : fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1305 :
1306 0 : reset->timestamp = fd_log_wallclock();
1307 0 : reset->completed_slot = fd_bank_slot_get( bank );
1308 0 : reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
1309 0 : reset->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1310 0 : reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
1311 0 : fd_memcpy( reset->completed_blockhash, block_hash->uc, sizeof(fd_hash_t) );
1312 :
1313 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1314 0 : if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
1315 : /* Low power producer, maximum of one microblock per tick in the slot */
1316 0 : reset->max_microblocks_in_slot = ticks_per_slot;
1317 0 : } else {
1318 : /* See the long comment in after_credit for this limit */
1319 0 : reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
1320 0 : }
1321 0 : reset->next_leader_slot = ctx->next_leader_slot;
1322 :
1323 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_RESET, ctx->replay_out->chunk, sizeof(fd_poh_reset_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1324 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_poh_reset_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
1325 0 : }
1326 :
1327 : static void
1328 : boot_genesis( fd_replay_tile_t * ctx,
1329 : fd_stem_context_t * stem,
1330 : ulong in_idx,
1331 0 : ulong chunk ) {
1332 : /* If we are bootstrapping, we can't wait to wait for our identity
1333 : vote to be rooted as this creates a circular dependency. */
1334 0 : ctx->has_identity_vote_rooted = 1;
1335 :
1336 0 : uchar const * lthash = (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
1337 0 : uchar const * genesis_hash = (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk )+sizeof(fd_lthash_value_t);
1338 :
1339 : // TODO: Do not pass the fd_types type between tiles, it have offsets
1340 : // that are unsafe and can't be validated as being in-bounds. Need to
1341 : // pass an actual owned genesis type.
1342 0 : fd_genesis_solana_global_t const * genesis = fd_type_pun( (uchar*)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk )+sizeof(fd_hash_t)+sizeof(fd_lthash_value_t) );
1343 :
1344 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
1345 0 : FD_TEST( bank );
1346 0 : fd_funk_txn_xid_t xid = { .ul = { 0UL, FD_REPLAY_BOOT_BANK_IDX } };
1347 :
1348 : /* Do genesis-related processing in a non-rooted transaction */
1349 0 : fd_funk_txn_xid_t root_xid; fd_funk_txn_xid_set_root( &root_xid );
1350 0 : fd_funk_txn_xid_t target_xid = { .ul = { 0UL, 0UL } };
1351 0 : fd_accdb_attach_child( ctx->accdb_admin, &root_xid, &target_xid );
1352 0 : fd_runtime_read_genesis( ctx->banks, bank, ctx->accdb, &xid, NULL, fd_type_pun_const( genesis_hash ), fd_type_pun_const( lthash ), genesis, &ctx->runtime_stack );
1353 0 : fd_accdb_advance_root( ctx->accdb_admin, &target_xid );
1354 :
1355 0 : static const fd_txncache_fork_id_t txncache_root = { .val = USHORT_MAX };
1356 0 : bank->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, txncache_root );
1357 :
1358 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last( fd_bank_block_hash_queue_query( bank ) );
1359 0 : fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
1360 :
1361 0 : publish_stake_weights( ctx, stem, bank, 0 );
1362 0 : publish_stake_weights( ctx, stem, bank, 1 );
1363 :
1364 : /* We call this after fd_runtime_read_genesis, which sets up the
1365 : slot_bank needed in blockstore_init. */
1366 0 : init_after_snapshot( ctx );
1367 :
1368 : /* Initialize store for genesis case, similar to snapshot case */
1369 0 : fd_hash_t genesis_block_id = { .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
1370 0 : fd_store_exacq( ctx->store );
1371 0 : if( FD_UNLIKELY( fd_store_root( ctx->store ) ) ) {
1372 0 : FD_LOG_CRIT(( "invariant violation: store root is not 0 for genesis" ));
1373 0 : }
1374 0 : fd_store_insert( ctx->store, 0, &genesis_block_id );
1375 0 : ctx->store->slot0 = 0UL; /* Genesis slot */
1376 0 : fd_store_exrel( ctx->store );
1377 :
1378 0 : ctx->published_root_slot = 0UL;
1379 0 : fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX );
1380 :
1381 0 : fd_bank_block_height_set( bank, 1UL );
1382 :
1383 0 : ctx->consensus_root = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
1384 0 : ctx->consensus_root_slot = 0UL;
1385 0 : ctx->consensus_root_bank_idx = 0UL;
1386 0 : ctx->published_root_slot = 0UL;
1387 0 : ctx->published_root_bank_idx = 0UL;
1388 :
1389 0 : ctx->reset_slot = 0UL;
1390 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
1391 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1392 0 : if( FD_LIKELY( ctx->next_leader_slot ) ) {
1393 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1394 0 : } else {
1395 0 : ctx->next_leader_tickcount = LONG_MAX;
1396 0 : }
1397 :
1398 0 : ctx->is_booted = 1;
1399 0 : maybe_become_leader( ctx, stem );
1400 :
1401 0 : fd_hash_t initial_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
1402 0 : fd_reasm_fec_t * fec = fd_reasm_insert( ctx->reasm, &initial_block_id, NULL, 0 /* genesis slot */, 0, 0, 0, 0, 1, 0 ); /* FIXME manifest block_id */
1403 0 : fec->bank_idx = 0UL;
1404 :
1405 :
1406 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
1407 0 : FD_TEST( block_id_ele );
1408 0 : block_id_ele->block_id = initial_block_id;
1409 0 : block_id_ele->slot = 0UL;
1410 :
1411 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1412 :
1413 0 : publish_slot_completed( ctx, stem, bank, 1 );
1414 0 : publish_root_advanced( ctx, stem );
1415 0 : publish_reset( ctx, stem, bank );
1416 0 : }
1417 :
1418 : static void
1419 : on_snapshot_message( fd_replay_tile_t * ctx,
1420 : fd_stem_context_t * stem,
1421 : ulong in_idx,
1422 : ulong chunk,
1423 0 : ulong sig ) {
1424 0 : ulong msg = fd_ssmsg_sig_message( sig );
1425 0 : if( FD_LIKELY( msg==FD_SSMSG_DONE ) ) {
1426 : /* An end of message notification indicates the snapshot is loaded.
1427 : Replay is able to start executing from this point onwards. */
1428 : /* TODO: replay should finish booting. Could make replay a
1429 : state machine and set the state here accordingly. */
1430 0 : ctx->is_booted = 1;
1431 :
1432 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
1433 0 : if( FD_UNLIKELY( !bank ) ) {
1434 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
1435 0 : }
1436 :
1437 0 : ulong snapshot_slot = fd_bank_slot_get( bank );
1438 : /* FIXME: This is a hack because the block id of the snapshot slot
1439 : is not provided in the snapshot. A possible solution is to get
1440 : the block id of the snapshot slot from repair. */
1441 0 : fd_hash_t manifest_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
1442 :
1443 0 : fd_store_exacq( ctx->store );
1444 0 : FD_TEST( !fd_store_root( ctx->store ) );
1445 0 : fd_store_insert( ctx->store, 0, &manifest_block_id );
1446 0 : ctx->store->slot0 = snapshot_slot; /* FIXME manifest_block_id */
1447 0 : fd_store_exrel( ctx->store );
1448 :
1449 : /* Typically, when we cross an epoch boundary during normal
1450 : operation, we publish the stake weights for the new epoch. But
1451 : since we are starting from a snapshot, we need to publish two
1452 : epochs worth of stake weights: the previous epoch (which is
1453 : needed for voting on the current epoch), and the current epoch
1454 : (which is needed for voting on the next epoch). */
1455 0 : publish_stake_weights( ctx, stem, bank, 0 );
1456 0 : publish_stake_weights( ctx, stem, bank, 1 );
1457 :
1458 0 : ctx->consensus_root = manifest_block_id;
1459 0 : ctx->consensus_root_slot = snapshot_slot;
1460 0 : ctx->consensus_root_bank_idx = 0UL;
1461 0 : ctx->published_root_slot = ctx->consensus_root_slot;
1462 0 : ctx->published_root_bank_idx = 0UL;
1463 :
1464 0 : ctx->reset_slot = snapshot_slot;
1465 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
1466 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1467 0 : if( FD_LIKELY( ctx->next_leader_slot ) ) {
1468 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1469 0 : } else {
1470 0 : ctx->next_leader_tickcount = LONG_MAX;
1471 0 : }
1472 :
1473 0 : fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX );
1474 0 : FD_TEST( bank->idx==0UL );
1475 :
1476 0 : fd_funk_txn_xid_t xid = { .ul = { snapshot_slot, FD_REPLAY_BOOT_BANK_IDX } };
1477 :
1478 0 : fd_features_restore( bank, ctx->accdb->funk, &xid );
1479 :
1480 0 : fd_runtime_update_leaders( bank, &ctx->runtime_stack );
1481 :
1482 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
1483 0 : FD_TEST( block_id_ele );
1484 0 : block_id_ele->block_id = manifest_block_id;
1485 0 : block_id_ele->slot = snapshot_slot;
1486 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1487 :
1488 : /* We call this after fd_runtime_read_genesis, which sets up the
1489 : slot_bank needed in blockstore_init. */
1490 0 : init_after_snapshot( ctx );
1491 :
1492 0 : publish_slot_completed( ctx, stem, bank, 1 );
1493 0 : publish_root_advanced( ctx, stem );
1494 :
1495 0 : fd_reasm_fec_t * fec = fd_reasm_insert( ctx->reasm, &manifest_block_id, NULL, snapshot_slot, 0, 0, 0, 0, 1, 0 ); /* FIXME manifest block_id */
1496 0 : fec->bank_idx = 0UL;
1497 0 : return;
1498 0 : }
1499 :
1500 0 : switch( msg ) {
1501 0 : case FD_SSMSG_MANIFEST_FULL:
1502 0 : case FD_SSMSG_MANIFEST_INCREMENTAL: {
1503 : /* We may either receive a full snapshot manifest or an
1504 : incremental snapshot manifest. Note that this external message
1505 : id is only used temporarily because replay cannot yet receive
1506 : the firedancer-internal snapshot manifest message. */
1507 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
1508 0 : FD_LOG_ERR(( "chunk %lu from in %d corrupt, not in range [%lu,%lu]", chunk, ctx->in_kind[ in_idx ], ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
1509 :
1510 0 : fd_ssload_recover( fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ),
1511 0 : ctx->banks,
1512 0 : fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX ),
1513 0 : ctx->runtime_stack.stakes.vote_credits );
1514 :
1515 0 : fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
1516 0 : ctx->hard_forks_cnt = manifest->hard_forks_len;
1517 0 : for( ulong i=0UL; i<manifest->hard_forks_len; i++ ) ctx->hard_forks[ i ] = manifest->hard_forks[ i ];
1518 0 : break;
1519 0 : }
1520 0 : default: {
1521 0 : FD_LOG_ERR(( "Received unknown snapshot message with msg %lu", msg ));
1522 0 : return;
1523 0 : }
1524 0 : }
1525 :
1526 0 : return;
1527 0 : }
1528 :
1529 : static void
1530 : dispatch_task( fd_replay_tile_t * ctx,
1531 : fd_stem_context_t * stem,
1532 0 : fd_sched_task_t * task ) {
1533 :
1534 0 : switch( task->task_type ) {
1535 0 : case FD_SCHED_TT_TXN_EXEC: {
1536 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_exec->txn_idx );
1537 :
1538 : /* FIXME: this should be done during txn parsing so that we don't
1539 : have to loop over all accounts a second time. */
1540 : /* Insert or reverify invoked programs for this epoch, if needed. */
1541 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_exec->bank_idx );
1542 :
1543 : /* Add the transaction to the block dumper if necessary. This
1544 : logic doesn't need to be fork-aware since it's only meant to
1545 : be used in backtest. */
1546 0 : if( FD_UNLIKELY( ctx->capture_ctx && ctx->capture_ctx->dump_block_to_pb ) ) {
1547 0 : fd_dump_block_to_protobuf_collect_tx( ctx->block_dump_ctx, txn_p );
1548 0 : }
1549 :
1550 0 : bank->refcnt++;
1551 :
1552 0 : if( FD_UNLIKELY( !bank->first_transaction_scheduled_nanos ) ) bank->first_transaction_scheduled_nanos = fd_log_wallclock();
1553 :
1554 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
1555 0 : fd_exec_txn_exec_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1556 0 : memcpy( &exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
1557 0 : exec_msg->bank_idx = task->txn_exec->bank_idx;
1558 0 : exec_msg->txn_idx = task->txn_exec->txn_idx;
1559 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXEC_TT_TXN_EXEC<<32) | task->txn_exec->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
1560 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
1561 0 : break;
1562 0 : }
1563 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
1564 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_sigverify->txn_idx );
1565 :
1566 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_sigverify->bank_idx );
1567 0 : bank->refcnt++;
1568 :
1569 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
1570 0 : fd_exec_txn_sigverify_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1571 0 : memcpy( &exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
1572 0 : exec_msg->bank_idx = task->txn_sigverify->bank_idx;
1573 0 : exec_msg->txn_idx = task->txn_sigverify->txn_idx;
1574 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXEC_TT_TXN_SIGVERIFY<<32) | task->txn_sigverify->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
1575 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
1576 0 : break;
1577 0 : };
1578 0 : default: {
1579 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
1580 0 : }
1581 0 : }
1582 0 : }
1583 :
1584 : /* Returns 1 if charge_busy. */
1585 : static int
1586 : replay( fd_replay_tile_t * ctx,
1587 0 : fd_stem_context_t * stem ) {
1588 :
1589 0 : if( FD_UNLIKELY( !ctx->is_booted ) ) return 0;
1590 :
1591 0 : int charge_busy = 0;
1592 0 : fd_sched_task_t task[ 1 ];
1593 0 : if( FD_UNLIKELY( !fd_sched_task_next_ready( ctx->sched, task ) ) ) {
1594 0 : return charge_busy; /* Nothing to execute or do. */
1595 0 : }
1596 :
1597 0 : charge_busy = 1;
1598 :
1599 0 : switch( task->task_type ) {
1600 0 : case FD_SCHED_TT_BLOCK_START: {
1601 0 : replay_block_start( ctx, stem, task->block_start->bank_idx, task->block_start->parent_bank_idx, task->block_start->slot );
1602 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_START, ULONG_MAX, ULONG_MAX );
1603 0 : break;
1604 0 : }
1605 0 : case FD_SCHED_TT_BLOCK_END: {
1606 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->block_end->bank_idx );
1607 0 : if( FD_LIKELY( !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) replay_block_finalize( ctx, stem, bank );
1608 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_END, ULONG_MAX, ULONG_MAX );
1609 0 : break;
1610 0 : }
1611 0 : case FD_SCHED_TT_TXN_EXEC:
1612 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
1613 : /* Likely/common case: we have a transaction we actually need to
1614 : execute. */
1615 0 : dispatch_task( ctx, stem, task );
1616 0 : break;
1617 0 : }
1618 0 : default: {
1619 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
1620 0 : }
1621 0 : }
1622 :
1623 0 : return charge_busy;
1624 0 : }
1625 :
1626 : static void
1627 : process_fec_set( fd_replay_tile_t * ctx,
1628 0 : fd_reasm_fec_t * reasm_fec ) {
1629 0 : long now = fd_log_wallclock();
1630 :
1631 : /* Linking only requires a shared lock because the fields that are
1632 : modified are only read on publish which uses exclusive lock. */
1633 :
1634 0 : long shacq_start, shacq_end, shrel_end;
1635 :
1636 0 : FD_STORE_SHARED_LOCK( ctx->store, shacq_start, shacq_end, shrel_end ) {
1637 0 : if( FD_UNLIKELY( !fd_store_link( ctx->store, &reasm_fec->key, &reasm_fec->cmr ) ) ) FD_LOG_WARNING(( "failed to link %s %s. slot %lu fec_set_idx %u", FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ), FD_BASE58_ENC_32_ALLOCA( &reasm_fec->cmr ), reasm_fec->slot, reasm_fec->fec_set_idx ));
1638 0 : } FD_STORE_SHARED_LOCK_END;
1639 0 : fd_histf_sample( ctx->metrics.store_link_wait, (ulong)fd_long_max( shacq_end - shacq_start, 0L ) );
1640 0 : fd_histf_sample( ctx->metrics.store_link_work, (ulong)fd_long_max( shrel_end - shacq_end, 0L ) );
1641 :
1642 : /* Update the reasm_fec with the correct bank index and parent bank
1643 : index. If the FEC belongs to a leader, we have already allocated
1644 : a bank index for the FEC and it just needs to be propagated to the
1645 : reasm_fec. */
1646 :
1647 0 : reasm_fec->parent_bank_idx = fd_reasm_parent( ctx->reasm, reasm_fec )->bank_idx;
1648 :
1649 0 : if( FD_UNLIKELY( reasm_fec->leader ) ) {
1650 : /* If we are the leader we just need to copy in the bank index that
1651 : the leader slot is using. */
1652 0 : FD_TEST( ctx->leader_bank!=NULL );
1653 0 : reasm_fec->bank_idx = ctx->leader_bank->idx;
1654 0 : } else if( FD_UNLIKELY( reasm_fec->fec_set_idx==0U ) ) {
1655 : /* If we are seeing a FEC with fec set idx 0, this means that we are
1656 : starting a new slot, and we need a new bank index. */
1657 0 : reasm_fec->bank_idx = fd_banks_new_bank( ctx->banks, reasm_fec->parent_bank_idx, now )->idx;
1658 0 : } else {
1659 : /* We are continuing to execute through a slot that we already have
1660 : a bank index for. */
1661 0 : reasm_fec->bank_idx = reasm_fec->parent_bank_idx;
1662 0 : }
1663 :
1664 0 : if( FD_UNLIKELY( reasm_fec->slot_complete ) ) {
1665 : /* Once the block id for a block is known it must be added to the
1666 : leader block mapping. */
1667 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ reasm_fec->bank_idx ];
1668 0 : FD_TEST( block_id_ele );
1669 :
1670 : /* If an entry already exists for this bank index in the block id
1671 : map, we can safely remove it and replace it with the new entry.
1672 : This is safe because we know that the old entry for this fork
1673 : index has already been pruned away. */
1674 0 : if( FD_LIKELY( block_id_ele->slot!=FD_SLOT_NULL && fd_block_id_map_ele_query( ctx->block_id_map, &block_id_ele->block_id, NULL, ctx->block_id_arr ) ) ) {
1675 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_id, NULL, ctx->block_id_arr ) );
1676 0 : }
1677 :
1678 0 : block_id_ele->block_id = reasm_fec->key;
1679 0 : block_id_ele->slot = reasm_fec->slot;
1680 :
1681 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1682 :
1683 0 : if( FD_UNLIKELY( reasm_fec->leader ) ) {
1684 0 : ctx->recv_block_id = 1;
1685 0 : }
1686 0 : }
1687 :
1688 0 : if( FD_UNLIKELY( reasm_fec->leader ) ) {
1689 0 : return;
1690 0 : }
1691 :
1692 : /* Forks form a partial ordering over FEC sets. The Repair tile
1693 : delivers FEC sets in-order per fork, but FEC set ordering across
1694 : forks is arbitrary */
1695 0 : fd_sched_fec_t sched_fec[ 1 ];
1696 :
1697 : # if DEBUG_LOGGING
1698 : FD_LOG_INFO(( "replay processing FEC set for slot %lu fec_set_idx %u, mr %s cmr %s", reasm_fec->slot, reasm_fec->fec_set_idx, FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ), FD_BASE58_ENC_32_ALLOCA( &reasm_fec->cmr ) ));
1699 : # endif
1700 :
1701 : /* Read FEC set from the store. This should happen before we try to
1702 : ingest the FEC set. This allows us to filter out frags that were
1703 : in-flight when we published away minority forks that the frags land
1704 : on. These frags would have no bank to execute against, because
1705 : their corresponding banks, or parent banks, have also been pruned
1706 : during publishing. A query against store will rightfully tell us
1707 : that the underlying data is not found, implying that this is for a
1708 : minority fork that we can safely ignore. */
1709 0 : FD_STORE_SHARED_LOCK( ctx->store, shacq_start, shacq_end, shrel_end ) {
1710 0 : fd_store_fec_t * store_fec = fd_store_query( ctx->store, &reasm_fec->key );
1711 0 : if( FD_UNLIKELY( !store_fec ) ) {
1712 : /* The only case in which a FEC is not found in the store after
1713 : repair has notified is if the FEC was on a minority fork that
1714 : has already been published away. In this case we abandon the
1715 : entire slice because it is no longer relevant. */
1716 0 : FD_LOG_WARNING(( "store fec for slot: %lu is on minority fork already pruned by publish. abandoning slice. root: %lu. pruned merkle: %s", reasm_fec->slot, ctx->consensus_root_slot, FD_BASE58_ENC_32_ALLOCA( &reasm_fec->key ) ));
1717 0 : return;
1718 0 : }
1719 0 : FD_TEST( store_fec );
1720 0 : sched_fec->fec = store_fec;
1721 0 : sched_fec->shred_cnt = reasm_fec->data_cnt;
1722 0 : } FD_STORE_SHARED_LOCK_END;
1723 :
1724 0 : fd_histf_sample( ctx->metrics.store_read_wait, (ulong)fd_long_max( shacq_end - shacq_start, 0UL ) );
1725 0 : fd_histf_sample( ctx->metrics.store_read_work, (ulong)fd_long_max( shrel_end - shacq_end, 0UL ) );
1726 :
1727 0 : sched_fec->is_last_in_batch = !!reasm_fec->data_complete;
1728 0 : sched_fec->is_last_in_block = !!reasm_fec->slot_complete;
1729 0 : sched_fec->bank_idx = reasm_fec->bank_idx;
1730 0 : sched_fec->parent_bank_idx = reasm_fec->parent_bank_idx;
1731 0 : sched_fec->slot = reasm_fec->slot;
1732 0 : sched_fec->parent_slot = reasm_fec->slot - reasm_fec->parent_off;
1733 0 : sched_fec->is_first_in_block = reasm_fec->fec_set_idx==0U;
1734 0 : fd_funk_txn_xid_copy( sched_fec->alut_ctx->xid, fd_funk_last_publish( ctx->accdb->funk ) );
1735 0 : sched_fec->alut_ctx->accdb[0] = ctx->accdb[0];
1736 0 : sched_fec->alut_ctx->els = ctx->published_root_slot;
1737 :
1738 0 : if( FD_UNLIKELY( !fd_sched_fec_ingest( ctx->sched, sched_fec ) ) ) {
1739 0 : fd_banks_mark_bank_dead( ctx->banks, fd_banks_bank_query( ctx->banks, sched_fec->bank_idx ) );
1740 0 : }
1741 0 : }
1742 :
1743 : static void
1744 : funk_publish( fd_replay_tile_t * ctx,
1745 : ulong slot,
1746 0 : ulong bank_idx ) {
1747 0 : fd_funk_txn_xid_t xid = { .ul[0] = slot, .ul[1] = bank_idx };
1748 0 : FD_LOG_DEBUG(( "publishing slot=%lu", slot ));
1749 :
1750 : /* This is the standard case. Publish all transactions up to and
1751 : including the watermark. This will publish any in-prep ancestors
1752 : of root_txn as well. */
1753 0 : fd_accdb_advance_root( ctx->accdb_admin, &xid );
1754 0 : fd_progcache_txn_advance_root( ctx->progcache_admin, &xid );
1755 0 : }
1756 :
1757 : static int
1758 0 : advance_published_root( fd_replay_tile_t * ctx ) {
1759 :
1760 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &ctx->consensus_root, NULL, ctx->block_id_arr );
1761 0 : if( FD_UNLIKELY( !block_id_ele ) ) {
1762 0 : FD_LOG_CRIT(( "invariant violation: block id ele not found for consensus root %s", FD_BASE58_ENC_32_ALLOCA( &ctx->consensus_root ) ));
1763 0 : }
1764 0 : ulong target_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
1765 :
1766 0 : fd_sched_root_notify( ctx->sched, target_bank_idx );
1767 :
1768 : /* If the identity vote has been seen on a bank that should be rooted,
1769 : then we are now ready to produce blocks. */
1770 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
1771 0 : fd_bank_t * root_bank = fd_banks_bank_query( ctx->banks, target_bank_idx );
1772 0 : if( FD_UNLIKELY( !root_bank ) ) FD_LOG_CRIT(( "invariant violation: root bank not found for bank index %lu", target_bank_idx ));
1773 0 : if( FD_LIKELY( fd_bank_has_identity_vote_get( root_bank ) ) ) ctx->has_identity_vote_rooted = 1;
1774 0 : }
1775 :
1776 0 : ulong advanceable_root_idx = ULONG_MAX;
1777 0 : if( FD_UNLIKELY( !fd_banks_advance_root_prepare( ctx->banks, target_bank_idx, &advanceable_root_idx ) ) ) return 0;
1778 :
1779 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, advanceable_root_idx );
1780 0 : FD_TEST( bank );
1781 :
1782 0 : fd_block_id_ele_t * advanceable_root_ele = &ctx->block_id_arr[ advanceable_root_idx ];
1783 0 : if( FD_UNLIKELY( !advanceable_root_ele ) ) {
1784 0 : FD_LOG_CRIT(( "invariant violation: advanceable root ele not found for bank index %lu", advanceable_root_idx ));
1785 0 : }
1786 :
1787 0 : long exacq_start, exacq_end, exrel_end;
1788 0 : FD_STORE_EXCLUSIVE_LOCK( ctx->store, exacq_start, exacq_end, exrel_end ) {
1789 0 : fd_store_publish( ctx->store, &advanceable_root_ele->block_id );
1790 0 : } FD_STORE_EXCLUSIVE_LOCK_END;
1791 :
1792 0 : fd_histf_sample( ctx->metrics.store_publish_wait, (ulong)fd_long_max( exacq_end-exacq_start, 0UL ) );
1793 0 : fd_histf_sample( ctx->metrics.store_publish_work, (ulong)fd_long_max( exrel_end-exacq_end, 0UL ) );
1794 :
1795 0 : ulong advanceable_root_slot = fd_bank_slot_get( bank );
1796 0 : funk_publish( ctx, advanceable_root_slot, bank->idx );
1797 :
1798 0 : fd_txncache_advance_root( ctx->txncache, bank->txncache_fork_id );
1799 0 : fd_sched_advance_root( ctx->sched, advanceable_root_idx );
1800 0 : fd_banks_advance_root( ctx->banks, advanceable_root_idx );
1801 0 : fd_reasm_publish( ctx->reasm, &advanceable_root_ele->block_id );
1802 :
1803 0 : ctx->published_root_slot = advanceable_root_slot;
1804 0 : ctx->published_root_bank_idx = advanceable_root_idx;
1805 :
1806 0 : return 1;
1807 0 : }
1808 :
1809 : static void
1810 : after_credit( fd_replay_tile_t * ctx,
1811 : fd_stem_context_t * stem,
1812 : int * opt_poll_in,
1813 0 : int * charge_busy ) {
1814 0 : if( FD_UNLIKELY( !ctx->is_booted ) ) return;
1815 :
1816 : /* Send any outstanding vote states to tower. TODO: Not sure why this
1817 : is here? Should happen when the slot completes instead? */
1818 0 : if( FD_UNLIKELY( ctx->vote_tower_out_idx<ctx->vote_tower_out_len ) ) {
1819 0 : *charge_busy = 1;
1820 0 : publish_next_vote_tower( ctx, stem );
1821 : /* Don't continue polling for fragments but instead skip to the next
1822 : iteration of the stem loop.
1823 :
1824 : This is necessary so that all the votes states for the end of a
1825 : particular slot are sent in one atomic block, and are not
1826 : interleaved with votes states at the end of other slots. */
1827 0 : *opt_poll_in = 0;
1828 0 : return;
1829 0 : }
1830 :
1831 0 : if( FD_UNLIKELY( maybe_become_leader( ctx, stem ) ) ) {
1832 0 : *charge_busy = 1;
1833 0 : *opt_poll_in = 0;
1834 0 : return;
1835 0 : }
1836 :
1837 : /* If the reassembler has a fec that is ready, we should process it
1838 : and pass it to the scheduler. */
1839 :
1840 0 : fd_reasm_fec_t * fec;
1841 : /* FIXME: The reasm logic needs to get reworked to support
1842 : equivocation more robustly. */
1843 0 : if( FD_LIKELY( fd_sched_can_ingest( ctx->sched, 1UL ) && !fd_banks_is_full( ctx->banks ) && (fec = fd_reasm_peek( ctx->reasm )) ) ) {
1844 :
1845 : /* If fec->eqvoc is set that means that equivocation mid-block was
1846 : detected in fd_reasm_t. We need to replay up to and including
1847 : the equivocating FEC on a new bank. */
1848 :
1849 0 : if( FD_UNLIKELY( fec->eqvoc ) ) {
1850 0 : FD_LOG_WARNING(( "Block equivocation detected at slot %lu", fec->slot ));
1851 :
1852 : /* We need to figure out which and how many FECs we need to
1853 : (re)insert into the scheduler. We work backwards from the
1854 : equivocating FEC, querying for chained merkle roots until we
1855 : reach the first FEC in the slot.
1856 : TODO: replace the magic number with a constant for the max
1857 : number of fecs possible in a slot with fix-32. */
1858 0 : fd_reasm_fec_t * fecs[ 1024 ] = { [0] = fec };
1859 0 : ulong fec_cnt = 1UL;
1860 0 : while( fecs[ fec_cnt-1UL ]->fec_set_idx!=0UL ) {
1861 0 : fec = fd_reasm_query( ctx->reasm, &fecs[ fec_cnt-1UL ]->cmr );
1862 0 : fecs[ fec_cnt++ ] = fec;
1863 0 : }
1864 :
1865 : /* If we don't have enough space in the scheduler to ingest all of
1866 : FECs, we can't proceed yet. */
1867 0 : if( FD_UNLIKELY( !fd_sched_can_ingest( ctx->sched, fec_cnt ) ) ) return;
1868 :
1869 : /* Now that we have validated that sched can ingest all of the
1870 : required FECs, it is finally safe to remove the equivocating
1871 : fec from the reasm deque. */
1872 0 : fd_reasm_out( ctx->reasm );
1873 :
1874 : /* Now we can process all of the FECs. */
1875 0 : for( ulong i=fec_cnt; i>0UL; i-- ) {
1876 0 : process_fec_set( ctx, fecs[i-1UL] );
1877 0 : }
1878 0 : } else {
1879 : /* Standard case. */
1880 0 : fec = fd_reasm_out( ctx->reasm );
1881 0 : process_fec_set( ctx, fec );
1882 0 : }
1883 :
1884 0 : *charge_busy = 1;
1885 0 : *opt_poll_in = 0;
1886 0 : return;
1887 0 : }
1888 :
1889 : /* If we are leader, we can only unbecome the leader iff we have
1890 : received the poh hash from the poh tile and block id from reasm. */
1891 0 : if( FD_UNLIKELY( ctx->is_leader && ctx->recv_block_id && ctx->recv_poh ) ) {
1892 0 : fini_leader_bank( ctx, stem );
1893 0 : *charge_busy = 1;
1894 0 : *opt_poll_in = 0;
1895 0 : return;
1896 0 : }
1897 :
1898 : /* If the published_root is not caught up to the consensus root, then
1899 : we should try to advance the published root. */
1900 0 : if( FD_UNLIKELY( ctx->consensus_root_bank_idx!=ctx->published_root_bank_idx && advance_published_root( ctx ) ) ) {
1901 0 : *charge_busy = 1;
1902 0 : *opt_poll_in = 0;
1903 0 : return;
1904 0 : }
1905 :
1906 0 : *charge_busy = replay( ctx, stem );
1907 0 : *opt_poll_in = !*charge_busy;
1908 0 : }
1909 :
1910 : static int
1911 : before_frag( fd_replay_tile_t * ctx,
1912 : ulong in_idx,
1913 : ulong seq FD_PARAM_UNUSED,
1914 0 : ulong sig FD_PARAM_UNUSED ) {
1915 :
1916 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_SHRED ) ) {
1917 : /* If reasm is full, we can not insert any more FEC sets. We must
1918 : not consume any frags from shred_out until reasm can process more
1919 : FEC sets. */
1920 :
1921 0 : if( FD_UNLIKELY( !fd_reasm_free( ctx->reasm ) ) ) {
1922 0 : return -1;
1923 0 : }
1924 0 : }
1925 :
1926 0 : return 0;
1927 0 : }
1928 :
1929 : static void
1930 : process_solcap_account_update( fd_replay_tile_t * ctx,
1931 0 : fd_capture_ctx_account_update_msg_t const * msg ) {
1932 :
1933 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
1934 0 : if( FD_UNLIKELY( !bank ) ) {
1935 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", msg->bank_idx ));
1936 0 : }
1937 :
1938 0 : if( FD_UNLIKELY( !ctx->capture_ctx || !ctx->capture_ctx->capture ) ) return;
1939 0 : if( FD_UNLIKELY( fd_bank_slot_get( bank )<ctx->capture_ctx->solcap_start_slot ) ) return;
1940 :
1941 0 : uchar const * account_data = (uchar const *)fd_type_pun_const( msg )+sizeof(fd_capture_ctx_account_update_msg_t);
1942 0 : fd_solcap_write_account( ctx->capture_ctx->capture, &msg->pubkey, &msg->info, account_data, msg->data_sz );
1943 0 : }
1944 :
1945 : static void
1946 : process_exec_task_done( fd_replay_tile_t * ctx,
1947 : fd_exec_task_done_msg_t * msg,
1948 0 : ulong sig ) {
1949 0 : if( FD_UNLIKELY( sig==0UL ) ) {
1950 : // FIXME remove this branch with new solcap
1951 0 : process_solcap_account_update( ctx, fd_type_pun( msg ) );
1952 0 : return;
1953 0 : }
1954 :
1955 0 : ulong exec_tile_idx = sig&0xFFFFFFFFUL;
1956 :
1957 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
1958 0 : bank->refcnt--;
1959 :
1960 0 : switch( sig>>32 ) {
1961 0 : case FD_EXEC_TT_TXN_EXEC: {
1962 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
1963 : /* Query the txn signature against our recently generated vote
1964 : txn signatures. If the query is successful, then we have
1965 : seen our own vote transaction land and this should be marked
1966 : in the bank. We go through this exercise until we've seen
1967 : our vote rooted. */
1968 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, msg->txn_exec->txn_idx );
1969 0 : if( fd_vote_tracker_query_sig( ctx->vote_tracker, fd_type_pun_const( txn_p->payload+TXN( txn_p )->signature_off ) ) ) {
1970 0 : *fd_bank_has_identity_vote_modify( bank ) += 1;
1971 0 : }
1972 0 : }
1973 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_EXEC, msg->txn_exec->txn_idx, exec_tile_idx );
1974 0 : if( FD_UNLIKELY( msg->txn_exec->err && !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) {
1975 : /* Every transaction in a valid block has to execute.
1976 : Otherwise, we should mark the block as dead. Also freeze the
1977 : bank if possible. */
1978 0 : fd_banks_mark_bank_dead( ctx->banks, bank );
1979 0 : fd_sched_block_abandon( ctx->sched, bank->idx );
1980 0 : }
1981 0 : if( FD_UNLIKELY( (bank->flags&FD_BANK_FLAGS_DEAD) && bank->refcnt==0UL ) ) {
1982 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
1983 0 : }
1984 0 : break;
1985 0 : }
1986 0 : case FD_EXEC_TT_TXN_SIGVERIFY: {
1987 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_SIGVERIFY, msg->txn_sigverify->txn_idx, exec_tile_idx );
1988 0 : if( FD_UNLIKELY( msg->txn_sigverify->err && !(bank->flags&FD_BANK_FLAGS_DEAD) ) ) {
1989 : /* Every transaction in a valid block has to sigverify.
1990 : Otherwise, we should mark the block as dead. Also freeze the
1991 : bank if possible. */
1992 0 : fd_banks_mark_bank_dead( ctx->banks, bank );
1993 0 : fd_sched_block_abandon( ctx->sched, bank->idx );
1994 0 : }
1995 0 : if( FD_UNLIKELY( (bank->flags&FD_BANK_FLAGS_DEAD) && bank->refcnt==0UL ) ) {
1996 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
1997 0 : }
1998 0 : break;
1999 0 : }
2000 0 : default: FD_LOG_CRIT(( "unexpected sig 0x%lx", sig ));
2001 0 : }
2002 :
2003 : /* Reference counter just decreased, and an exec tile just got freed
2004 : up. If there's a need to be more aggressively pruning, we could
2005 : check here if more slots just became publishable and publish. Not
2006 : publishing here shouldn't bloat the fork tree too much though. We
2007 : mark minority forks dead as soon as we can, and execution dispatch
2008 : stops on dead blocks. So shortly afterwards, dead blocks should be
2009 : eligible for pruning as in-flight transactions retire from the
2010 : execution pipeline. */
2011 :
2012 0 : }
2013 :
2014 : static void
2015 : process_tower_update( fd_replay_tile_t * ctx,
2016 : fd_stem_context_t * stem,
2017 0 : fd_tower_slot_done_t const * msg ) {
2018 :
2019 0 : ctx->reset_block_id = msg->reset_block_id;
2020 0 : ctx->reset_slot = msg->reset_slot;
2021 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
2022 0 : ulong min_leader_slot = fd_ulong_max( msg->reset_slot+1UL, fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot+1UL ) );
2023 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, min_leader_slot, ctx->identity_pubkey );
2024 0 : if( FD_LIKELY( ctx->next_leader_slot ) ) {
2025 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
2026 0 : } else {
2027 0 : ctx->next_leader_tickcount = LONG_MAX;
2028 0 : }
2029 :
2030 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &msg->reset_block_id, NULL, ctx->block_id_arr );
2031 0 : if( FD_UNLIKELY( !block_id_ele ) ) {
2032 0 : FD_LOG_CRIT(( "invariant violation: block id ele doesn't exist for reset block id: %s, slot: %lu", FD_BASE58_ENC_32_ALLOCA( &msg->reset_block_id ), msg->reset_slot ));
2033 0 : }
2034 0 : ulong reset_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
2035 :
2036 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, reset_bank_idx );
2037 0 : if( FD_UNLIKELY( !bank ) ) {
2038 0 : FD_LOG_CRIT(( "invariant violation: bank not found for bank index %lu", reset_bank_idx ));
2039 0 : }
2040 :
2041 0 : if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
2042 0 : fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2043 :
2044 0 : reset->timestamp = ctx->reset_timestamp_nanos;
2045 0 : reset->completed_slot = ctx->reset_slot;
2046 0 : reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
2047 0 : reset->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
2048 0 : reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
2049 :
2050 0 : fd_memcpy( reset->completed_block_id, &block_id_ele->block_id, sizeof(fd_hash_t) );
2051 :
2052 0 : fd_blockhashes_t const * block_hash_queue = fd_bank_block_hash_queue_query( bank );
2053 0 : fd_hash_t const * last_hash = fd_blockhashes_peek_last( block_hash_queue );
2054 0 : FD_TEST( last_hash );
2055 0 : fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
2056 :
2057 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
2058 0 : if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
2059 : /* Low power producer, maximum of one microblock per tick in the slot */
2060 0 : reset->max_microblocks_in_slot = ticks_per_slot;
2061 0 : } else {
2062 : /* See the long comment in after_credit for this limit */
2063 0 : reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
2064 0 : }
2065 0 : reset->next_leader_slot = ctx->next_leader_slot;
2066 :
2067 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_RESET, ctx->replay_out->chunk, sizeof(fd_poh_reset_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
2068 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_poh_reset_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
2069 0 : }
2070 :
2071 0 : FD_LOG_INFO(( "tower_update(reset_slot=%lu, next_leader_slot=%lu, vote_slot=%lu, new_root=%d, root_slot=%lu, root_block_id=%s)", msg->reset_slot, ctx->next_leader_slot, msg->vote_slot, msg->new_root, msg->root_slot, FD_BASE58_ENC_32_ALLOCA( &msg->root_block_id ) ));
2072 0 : maybe_become_leader( ctx, stem );
2073 :
2074 0 : if( FD_LIKELY( msg->new_root ) ) {
2075 :
2076 0 : FD_TEST( msg->root_slot>=ctx->consensus_root_slot );
2077 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &msg->root_block_id, NULL, ctx->block_id_arr );
2078 0 : FD_TEST( block_id_ele );
2079 :
2080 0 : ctx->consensus_root_slot = msg->root_slot;
2081 0 : ctx->consensus_root = msg->root_block_id;
2082 0 : ctx->consensus_root_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
2083 :
2084 0 : publish_root_advanced( ctx, stem );
2085 0 : }
2086 :
2087 0 : ulong distance = 0UL;
2088 0 : fd_bank_t * parent = bank;
2089 0 : while( parent ) {
2090 0 : if( FD_UNLIKELY( parent->idx==ctx->consensus_root_bank_idx ) ) break;
2091 0 : parent = fd_banks_get_parent( ctx->banks, parent );
2092 0 : distance++;
2093 0 : }
2094 :
2095 0 : FD_MGAUGE_SET( REPLAY, ROOT_DISTANCE, distance );
2096 0 : }
2097 :
2098 : static void
2099 : process_fec_complete( fd_replay_tile_t * ctx,
2100 0 : uchar const * shred_buf ) {
2101 0 : fd_shred_t const * shred = (fd_shred_t const *)fd_type_pun_const( shred_buf );
2102 :
2103 0 : fd_hash_t const * merkle_root = (fd_hash_t const *)fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ );
2104 0 : fd_hash_t const * chained_merkle_root = (fd_hash_t const *)fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) );
2105 0 : int is_leader_fec = *(int const *) fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) );
2106 :
2107 0 : int data_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE );
2108 0 : int slot_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE );
2109 :
2110 0 : FD_TEST( !fd_reasm_query( ctx->reasm, merkle_root ) );
2111 0 : if( FD_UNLIKELY( shred->slot - shred->data.parent_off == fd_reasm_slot0( ctx->reasm ) && shred->fec_set_idx == 0) ) {
2112 0 : chained_merkle_root = &fd_reasm_root( ctx->reasm )->key;
2113 0 : }
2114 0 : FD_TEST( fd_reasm_insert( ctx->reasm, merkle_root, chained_merkle_root, shred->slot, shred->fec_set_idx, shred->data.parent_off, (ushort)(shred->idx - shred->fec_set_idx + 1), data_complete, slot_complete, is_leader_fec ) );
2115 0 : }
2116 :
2117 : static void
2118 0 : process_resolv_slot_completed( fd_replay_tile_t * ctx, ulong bank_idx ) {
2119 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
2120 0 : FD_TEST( bank );
2121 :
2122 0 : bank->refcnt--;
2123 0 : }
2124 :
2125 : static void
2126 : process_vote_txn_sent( fd_replay_tile_t * ctx,
2127 0 : fd_txn_m_t * txnm ) {
2128 : /* The send tile has signed and sent a vote. Add this vote to the
2129 : vote tracker. We go through this exercise until we've seen our
2130 : vote rooted. */
2131 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
2132 0 : uchar * payload = ((uchar *)txnm) + sizeof(fd_txn_m_t);
2133 0 : uchar txn_mem[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
2134 0 : fd_txn_t * txn = (fd_txn_t *)txn_mem;
2135 0 : if( FD_UNLIKELY( !fd_txn_parse( payload, txnm->payload_sz, txn_mem, NULL ) ) ) {
2136 0 : FD_LOG_CRIT(( "Could not parse txn from send tile" ));
2137 0 : }
2138 0 : fd_vote_tracker_insert( ctx->vote_tracker, fd_type_pun_const( payload+txn->signature_off ) );
2139 0 : }
2140 0 : }
2141 :
2142 : static inline void
2143 0 : maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
2144 0 : if( FD_LIKELY( ctx->expected_shred_version && ctx->ipecho_shred_version ) ) {
2145 0 : if( FD_UNLIKELY( ctx->expected_shred_version!=ctx->ipecho_shred_version ) ) {
2146 0 : FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from ipecho", ctx->expected_shred_version, ctx->ipecho_shred_version ) );
2147 0 : }
2148 0 : }
2149 :
2150 0 : if( FD_LIKELY( ctx->has_genesis_hash && ctx->hard_forks_cnt!=ULONG_MAX && (ctx->expected_shred_version || ctx->ipecho_shred_version) ) ) {
2151 0 : ushort expected_shred_version = ctx->expected_shred_version ? ctx->expected_shred_version : ctx->ipecho_shred_version;
2152 :
2153 0 : union {
2154 0 : uchar c[ 32 ];
2155 0 : ushort s[ 16 ];
2156 0 : } running_hash;
2157 0 : fd_memcpy( running_hash.c, ctx->genesis_hash, sizeof(fd_hash_t) );
2158 :
2159 0 : ulong processed = 0UL;
2160 0 : ulong min_value = 0UL;
2161 0 : while( processed<ctx->hard_forks_cnt ) {
2162 0 : ulong min_index = ULONG_MAX;
2163 0 : for( ulong i=0UL; i<ctx->hard_forks_cnt; i++ ) {
2164 0 : if( ctx->hard_forks[ i ]>=min_value && (min_index==ULONG_MAX || ctx->hard_forks[ i ]<ctx->hard_forks[ min_index ] ) ) {
2165 0 : min_index = i;
2166 0 : }
2167 0 : }
2168 :
2169 0 : FD_TEST( min_index!=ULONG_MAX );
2170 0 : min_value = ctx->hard_forks[ min_index ];
2171 0 : ulong min_count = 0UL;
2172 0 : for( ulong i=0UL; i<ctx->hard_forks_cnt; i++ ) {
2173 0 : if( ctx->hard_forks[ i ]==min_value ) min_count++;
2174 0 : }
2175 :
2176 0 : uchar data[ 48UL ];
2177 0 : fd_memcpy( data, running_hash.c, sizeof(fd_hash_t) );
2178 0 : fd_memcpy( data+32UL, &min_value, sizeof(ulong) );
2179 0 : fd_memcpy( data+40UL, &min_count, sizeof(ulong) );
2180 :
2181 0 : FD_TEST( fd_sha256_hash( data, 48UL, running_hash.c ) );
2182 0 : processed += min_count;
2183 0 : min_value += 1UL;
2184 0 : }
2185 :
2186 0 : ushort xor = 0;
2187 0 : for( ulong i=0UL; i<16UL; i++ ) xor ^= running_hash.s[ i ];
2188 :
2189 0 : xor = fd_ushort_bswap( xor );
2190 0 : xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX );
2191 :
2192 0 : if( FD_UNLIKELY( expected_shred_version!=xor ) ) {
2193 0 : FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from genesis hash %s and hard forks", expected_shred_version, xor, FD_BASE58_ENC_32_ALLOCA( &ctx->genesis_hash ) ));
2194 0 : }
2195 0 : }
2196 0 : }
2197 :
2198 : static inline int
2199 : returnable_frag( fd_replay_tile_t * ctx,
2200 : ulong in_idx,
2201 : ulong seq,
2202 : ulong sig,
2203 : ulong chunk,
2204 : ulong sz,
2205 : ulong ctl,
2206 : ulong tsorig,
2207 : ulong tspub,
2208 0 : fd_stem_context_t * stem ) {
2209 0 : (void)seq;
2210 0 : (void)ctl;
2211 0 : (void)tsorig;
2212 0 : (void)tspub;
2213 :
2214 0 : if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
2215 0 : FD_LOG_ERR(( "chunk %lu %lu from in %d corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in_kind[ in_idx ], ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
2216 :
2217 0 : switch( ctx->in_kind[in_idx] ) {
2218 0 : case IN_KIND_GENESIS: {
2219 0 : uchar const * src = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2220 0 : ctx->has_genesis_hash = 1;
2221 0 : if( FD_LIKELY( sig==GENESI_SIG_BOOTSTRAP_COMPLETED ) ) {
2222 0 : boot_genesis( ctx, stem, in_idx, chunk );
2223 0 : fd_memcpy( ctx->genesis_hash, src+sizeof(fd_lthash_value_t), sizeof(fd_hash_t) );
2224 0 : } else {
2225 0 : fd_memcpy( ctx->genesis_hash, src, sizeof(fd_hash_t) );
2226 0 : }
2227 0 : maybe_verify_shred_version( ctx );
2228 0 : break;
2229 0 : }
2230 0 : case IN_KIND_IPECHO: {
2231 0 : FD_TEST( ctx->ipecho_shred_version && ctx->ipecho_shred_version<=USHORT_MAX );
2232 0 : ctx->ipecho_shred_version = (ushort)sig;
2233 0 : maybe_verify_shred_version( ctx );
2234 0 : break;
2235 0 : }
2236 0 : case IN_KIND_SNAP:
2237 0 : on_snapshot_message( ctx, stem, in_idx, chunk, sig );
2238 0 : maybe_verify_shred_version( ctx );
2239 0 : break;
2240 0 : case IN_KIND_EXEC: {
2241 0 : process_exec_task_done( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), sig );
2242 0 : break;
2243 0 : }
2244 0 : case IN_KIND_POH: {
2245 0 : process_poh_message( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2246 0 : break;
2247 0 : }
2248 0 : case IN_KIND_RESOLV: {
2249 0 : fd_resolv_slot_exchanged_t * exchanged_slot = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2250 0 : process_resolv_slot_completed( ctx, exchanged_slot->bank_idx );
2251 0 : break;
2252 0 : }
2253 0 : case IN_KIND_TOWER: {
2254 0 : process_tower_update( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2255 0 : break;
2256 0 : }
2257 0 : case IN_KIND_SHRED: {
2258 : /* TODO: This message/sz should be defined. */
2259 0 : if( sz==FD_SHRED_DATA_HEADER_SZ + sizeof(fd_hash_t) + sizeof(fd_hash_t) + sizeof(int) ) {
2260 : /* If receive a FEC complete message. */
2261 0 : process_fec_complete( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2262 0 : }
2263 0 : break;
2264 0 : }
2265 0 : case IN_KIND_VTXN: {
2266 0 : process_vote_txn_sent( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2267 0 : break;
2268 0 : }
2269 0 : case IN_KIND_GUI: {
2270 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, sig );
2271 0 : FD_TEST( bank );
2272 0 : bank->refcnt--;
2273 0 : break;
2274 0 : }
2275 0 : default:
2276 0 : FD_LOG_ERR(( "unhandled kind %d", ctx->in_kind[ in_idx ] ));
2277 0 : }
2278 :
2279 0 : return 0;
2280 0 : }
2281 :
2282 : static inline fd_replay_out_link_t
2283 : out1( fd_topo_t const * topo,
2284 : fd_topo_tile_t const * tile,
2285 0 : char const * name ) {
2286 0 : ulong idx = ULONG_MAX;
2287 :
2288 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
2289 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
2290 0 : if( !strcmp( link->name, name ) ) {
2291 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
2292 0 : idx = i;
2293 0 : }
2294 0 : }
2295 :
2296 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_replay_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
2297 :
2298 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
2299 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
2300 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
2301 :
2302 0 : return (fd_replay_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
2303 0 : }
2304 :
2305 : static void
2306 : privileged_init( fd_topo_t * topo,
2307 0 : fd_topo_tile_t * tile ) {
2308 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
2309 :
2310 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
2311 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
2312 :
2313 0 : if( FD_UNLIKELY( !strcmp( tile->replay.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
2314 :
2315 0 : ctx->identity_pubkey[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->replay.identity_key_path, /* pubkey only: */ 1 ) );
2316 0 : }
2317 :
2318 : static void
2319 : unprivileged_init( fd_topo_t * topo,
2320 0 : fd_topo_tile_t * tile ) {
2321 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
2322 :
2323 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
2324 :
2325 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
2326 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
2327 0 : void * block_id_arr_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_block_id_ele_t), sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
2328 0 : void * block_id_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
2329 0 : void * _txncache = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
2330 0 : void * reasm_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_reasm_align(), fd_reasm_footprint( 1 << 20 ) );
2331 0 : void * sched_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.max_live_slots ) );
2332 0 : void * vote_tracker_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
2333 0 : void * _capture_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
2334 0 : void * block_dump_ctx = NULL;
2335 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
2336 0 : block_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
2337 0 : }
2338 :
2339 0 : ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
2340 0 : FD_TEST( store_obj_id!=ULONG_MAX );
2341 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
2342 0 : FD_TEST( ctx->store );
2343 :
2344 0 : ctx->vote_tower_out_idx = 0UL;
2345 0 : ctx->vote_tower_out_len = 0UL;
2346 :
2347 0 : ulong banks_obj_id = fd_pod_query_ulong( topo->props, "banks", ULONG_MAX );
2348 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
2349 0 : ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
2350 0 : FD_TEST( ctx->banks );
2351 :
2352 0 : fd_bank_t * bank_pool = fd_banks_get_bank_pool( ctx->banks );
2353 0 : FD_MGAUGE_SET( REPLAY, MAX_LIVE_BANKS, fd_banks_pool_max( bank_pool ) );
2354 :
2355 0 : fd_bank_t * bank = fd_banks_init_bank( ctx->banks );
2356 0 : fd_bank_slot_set( bank, 0UL );
2357 0 : FD_TEST( bank );
2358 0 : FD_TEST( bank->idx==FD_REPLAY_BOOT_BANK_IDX );
2359 :
2360 0 : ctx->consensus_root_slot = ULONG_MAX;
2361 0 : ctx->consensus_root = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
2362 0 : ctx->published_root_slot = ULONG_MAX;
2363 :
2364 0 : ctx->expected_shred_version = tile->replay.expected_shred_version;
2365 0 : ctx->ipecho_shred_version = 0;
2366 0 : ctx->has_genesis_hash = 0;
2367 0 : ctx->hard_forks_cnt = ULONG_MAX;
2368 :
2369 : /* Set some initial values for the bank: hardcoded features and the
2370 : cluster version. */
2371 0 : fd_cluster_version_t * cluster_version = fd_bank_cluster_version_modify( bank );
2372 0 : if( FD_UNLIKELY( sscanf( tile->replay.cluster_version, "%u.%u.%u", &cluster_version->major, &cluster_version->minor, &cluster_version->patch )!=3 ) ) {
2373 0 : FD_LOG_ERR(( "failed to decode cluster version, configured as \"%s\"", tile->replay.cluster_version ));
2374 0 : }
2375 :
2376 0 : fd_features_t * features = fd_bank_features_modify( bank );
2377 0 : fd_features_enable_cleaned_up( features, cluster_version );
2378 :
2379 0 : char const * one_off_features[ 16UL ];
2380 0 : FD_TEST( tile->replay.enable_features_cnt<=sizeof(one_off_features)/sizeof(one_off_features[0]) );
2381 0 : for( ulong i=0UL; i<tile->replay.enable_features_cnt; i++ ) one_off_features[ i ] = tile->replay.enable_features[i];
2382 0 : fd_features_enable_one_offs( features, one_off_features, (uint)tile->replay.enable_features_cnt, 0UL );
2383 :
2384 0 : FD_TEST( fd_accdb_admin_join ( ctx->accdb_admin, fd_topo_obj_laddr( topo, tile->replay.funk_obj_id ) ) );
2385 0 : FD_TEST( fd_accdb_user_join ( ctx->accdb, fd_topo_obj_laddr( topo, tile->replay.funk_obj_id ) ) );
2386 0 : FD_TEST( fd_progcache_admin_join( ctx->progcache_admin, fd_topo_obj_laddr( topo, tile->replay.progcache_obj_id ) ) );
2387 :
2388 0 : void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->replay.txncache_obj_id );
2389 0 : fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
2390 0 : FD_TEST( txncache_shmem );
2391 0 : ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
2392 0 : FD_TEST( ctx->txncache );
2393 :
2394 0 : ctx->tx_metadata_storage = tile->replay.tx_metadata_storage;
2395 :
2396 0 : ctx->capture_ctx = NULL;
2397 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) || strcmp( "", tile->replay.dump_proto_dir ) ) ) {
2398 0 : ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
2399 0 : }
2400 :
2401 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
2402 0 : ctx->capture_ctx->checkpt_freq = ULONG_MAX;
2403 0 : ctx->capture_file = fopen( tile->replay.solcap_capture, "w+" );
2404 0 : if( FD_UNLIKELY( !ctx->capture_file ) ) FD_LOG_ERR(( "fopen(%s) failed (%d-%s)", tile->replay.solcap_capture, errno, fd_io_strerror( errno ) ));
2405 :
2406 0 : ctx->capture_ctx->capture_txns = 0;
2407 0 : ctx->capture_ctx->solcap_start_slot = tile->replay.capture_start_slot;
2408 0 : fd_solcap_writer_init( ctx->capture_ctx->capture, ctx->capture_file );
2409 0 : }
2410 :
2411 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.dump_proto_dir ) ) ) {
2412 0 : ctx->capture_ctx->dump_proto_output_dir = tile->replay.dump_proto_dir;
2413 0 : if( FD_LIKELY( tile->replay.dump_block_to_pb ) ) ctx->capture_ctx->dump_block_to_pb = tile->replay.dump_block_to_pb;
2414 0 : }
2415 :
2416 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
2417 0 : ctx->block_dump_ctx = fd_block_dump_context_join( fd_block_dump_context_new( block_dump_ctx ) );
2418 0 : } else {
2419 0 : ctx->block_dump_ctx = NULL;
2420 0 : }
2421 :
2422 0 : ctx->exec_cnt = fd_topo_tile_name_cnt( topo, "exec" );
2423 :
2424 0 : ctx->is_booted = 0;
2425 :
2426 0 : ctx->reasm = fd_reasm_join( fd_reasm_new( reasm_mem, 1 << 20, 0 ) );
2427 0 : FD_TEST( ctx->reasm );
2428 :
2429 0 : ctx->sched = fd_sched_join( fd_sched_new( sched_mem, tile->replay.max_live_slots, ctx->exec_cnt ), tile->replay.max_live_slots );
2430 0 : FD_TEST( ctx->sched );
2431 :
2432 0 : ctx->enable_bank_hash_cmp = !!tile->replay.enable_bank_hash_cmp;
2433 :
2434 0 : ulong bank_hash_cmp_obj_id = fd_pod_query_ulong( topo->props, "bh_cmp", ULONG_MAX );
2435 0 : FD_TEST( bank_hash_cmp_obj_id!=ULONG_MAX );
2436 0 : ctx->bank_hash_cmp = fd_bank_hash_cmp_join( fd_bank_hash_cmp_new( fd_topo_obj_laddr( topo, bank_hash_cmp_obj_id ) ) );
2437 0 : FD_TEST( ctx->bank_hash_cmp );
2438 :
2439 0 : ctx->vote_tracker = fd_vote_tracker_join( fd_vote_tracker_new( vote_tracker_mem, 0UL ) );
2440 0 : FD_TEST( ctx->vote_tracker );
2441 :
2442 0 : ctx->has_identity_vote_rooted = 0;
2443 :
2444 0 : ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
2445 0 : FD_TEST( ctx->mleaders );
2446 :
2447 0 : ctx->is_leader = 0;
2448 0 : ctx->reset_slot = 0UL;
2449 0 : ctx->reset_block_id = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
2450 0 : ctx->reset_timestamp_nanos = 0UL;
2451 0 : ctx->next_leader_slot = ULONG_MAX;
2452 0 : ctx->next_leader_tickcount = LONG_MAX;
2453 0 : ctx->highwater_leader_slot = ULONG_MAX;
2454 0 : ctx->slot_duration_nanos = 400L*1000L*1000L; /* TODO: Not fixed ... not always 400ms ... */
2455 0 : ctx->slot_duration_ticks = (double)ctx->slot_duration_nanos*fd_tempo_tick_per_ns( NULL );
2456 0 : ctx->max_active_descendant = 0UL; /* TODO: Update this properly ... */
2457 0 : ctx->leader_bank = NULL;
2458 :
2459 : /* TODO: We need a real seed here. */
2460 0 : ctx->block_id_len = tile->replay.max_live_slots;
2461 0 : ctx->block_id_arr = (fd_block_id_ele_t *)block_id_arr_mem;
2462 0 : ctx->block_id_map = fd_block_id_map_join( fd_block_id_map_new( block_id_map_mem, chain_cnt, 999UL ) );
2463 0 : FD_TEST( ctx->block_id_map );
2464 :
2465 0 : for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) {
2466 0 : ctx->block_id_arr[ i ].slot = FD_SLOT_NULL;
2467 0 : }
2468 :
2469 0 : ctx->resolv_tile_cnt = fd_topo_tile_name_cnt( topo, "resolv" );
2470 :
2471 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
2472 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
2473 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
2474 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
2475 :
2476 0 : if( FD_LIKELY( link->dcache ) ) {
2477 0 : ctx->in[ i ].mem = link_wksp->wksp;
2478 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
2479 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
2480 0 : ctx->in[ i ].mtu = link->mtu;
2481 0 : }
2482 :
2483 0 : if( !strcmp( link->name, "genesi_out" ) ) ctx->in_kind[ i ] = IN_KIND_GENESIS;
2484 0 : else if( !strcmp( link->name, "ipecho_out" ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
2485 0 : else if( !strcmp( link->name, "snapin_manif" ) ) ctx->in_kind[ i ] = IN_KIND_SNAP;
2486 0 : else if( !strcmp( link->name, "exec_replay" ) ) ctx->in_kind[ i ] = IN_KIND_EXEC;
2487 0 : else if( !strcmp( link->name, "tower_out" ) ) ctx->in_kind[ i ] = IN_KIND_TOWER;
2488 0 : else if( !strcmp( link->name, "poh_replay" ) ) ctx->in_kind[ i ] = IN_KIND_POH;
2489 0 : else if( !strcmp( link->name, "resolv_repla" ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
2490 0 : else if( !strcmp( link->name, "shred_out" ) ) ctx->in_kind[ i ] = IN_KIND_SHRED;
2491 0 : else if( !strcmp( link->name, "send_txns" ) ) ctx->in_kind[ i ] = IN_KIND_VTXN;
2492 0 : else if( !strcmp( link->name, "gui_replay" ) ) ctx->in_kind[ i ] = IN_KIND_GUI;
2493 0 : else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
2494 0 : }
2495 :
2496 0 : *ctx->stake_out = out1( topo, tile, "replay_stake" ); FD_TEST( ctx->stake_out->idx!=ULONG_MAX );
2497 0 : *ctx->replay_out = out1( topo, tile, "replay_out" ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
2498 :
2499 0 : ulong idx = fd_topo_find_tile_out_link( topo, tile, "replay_exec", 0UL );
2500 0 : FD_TEST( idx!=ULONG_MAX );
2501 0 : fd_topo_link_t * link = &topo->links[ tile->out_link_id[ idx ] ];
2502 :
2503 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
2504 0 : exec_out->idx = idx;
2505 0 : exec_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
2506 0 : exec_out->chunk0 = fd_dcache_compact_chunk0( exec_out->mem, link->dcache );
2507 0 : exec_out->wmark = fd_dcache_compact_wmark( exec_out->mem, link->dcache, link->mtu );
2508 0 : exec_out->chunk = exec_out->chunk0;
2509 :
2510 0 : ctx->gui_enabled = fd_topo_find_tile( topo, "gui", 0UL )!=ULONG_MAX;
2511 :
2512 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
2513 :
2514 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_link_wait, FD_MHIST_SECONDS_MIN( REPLAY, STORE_LINK_WAIT ),
2515 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_LINK_WAIT ) ) );
2516 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_link_work, FD_MHIST_SECONDS_MIN( REPLAY, STORE_LINK_WORK ),
2517 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_LINK_WORK ) ) );
2518 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_read_wait, FD_MHIST_SECONDS_MIN( REPLAY, STORE_READ_WAIT ),
2519 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_READ_WAIT ) ) );
2520 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_read_work, FD_MHIST_SECONDS_MIN( REPLAY, STORE_READ_WORK ),
2521 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_READ_WORK ) ) );
2522 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_publish_wait, FD_MHIST_SECONDS_MIN( REPLAY, STORE_PUBLISH_WAIT ),
2523 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_PUBLISH_WAIT ) ) );
2524 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_publish_work, FD_MHIST_SECONDS_MIN( REPLAY, STORE_PUBLISH_WORK ),
2525 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_PUBLISH_WORK ) ) );
2526 :
2527 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
2528 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
2529 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
2530 0 : }
2531 :
2532 : static ulong
2533 : populate_allowed_seccomp( fd_topo_t const * topo FD_FN_UNUSED,
2534 : fd_topo_tile_t const * tile FD_FN_UNUSED,
2535 : ulong out_cnt,
2536 0 : struct sock_filter * out ) {
2537 :
2538 0 : populate_sock_filter_policy_fd_replay_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
2539 0 : return sock_filter_policy_fd_replay_tile_instr_cnt;
2540 0 : }
2541 :
2542 : static ulong
2543 : populate_allowed_fds( fd_topo_t const * topo FD_FN_UNUSED,
2544 : fd_topo_tile_t const * tile FD_FN_UNUSED,
2545 : ulong out_fds_cnt,
2546 0 : int * out_fds ) {
2547 :
2548 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
2549 :
2550 0 : ulong out_cnt = 0UL;
2551 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
2552 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
2553 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
2554 0 : return out_cnt;
2555 0 : }
2556 :
2557 : #undef DEBUG_LOGGING
2558 :
2559 : /* counting carefully, after_credit can generate at most 7 frags and
2560 : returnable_frag boot_genesis can also generate at most 7 frags, so 14
2561 : is a conservative bound. */
2562 0 : #define STEM_BURST (14UL)
2563 :
2564 : /* TODO: calculate this properly/fix stem to work with larger numbers of links */
2565 : /* 1000 chosen empirically as anything larger slowed down replay times. Need to calculate
2566 : this properly. */
2567 0 : #define STEM_LAZY ((long)10e3)
2568 :
2569 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_replay_tile_t
2570 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_replay_tile_t)
2571 :
2572 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
2573 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
2574 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
2575 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
2576 :
2577 : #include "../../disco/stem/fd_stem.c"
2578 :
2579 : fd_topo_run_tile_t fd_tile_replay = {
2580 : .name = "replay",
2581 : .populate_allowed_seccomp = populate_allowed_seccomp,
2582 : .populate_allowed_fds = populate_allowed_fds,
2583 : .scratch_align = scratch_align,
2584 : .scratch_footprint = scratch_footprint,
2585 : .privileged_init = privileged_init,
2586 : .unprivileged_init = unprivileged_init,
2587 : .run = stem_run,
2588 : };
|