Line data Source code
1 : #include "fd_replay_tile.h"
2 : #include "fd_sched.h"
3 : #include "fd_execrp.h"
4 : #include "fd_vote_tracker.h"
5 : #include "generated/fd_replay_tile_seccomp.h"
6 :
7 : #include "../genesis/fd_genesi_tile.h"
8 : #include "../poh/fd_poh.h"
9 : #include "../poh/fd_poh_tile.h"
10 : #include "../tower/fd_tower_tile.h"
11 : #include "../resolv/fd_resolv_tile.h"
12 : #include "../restore/utils/fd_ssload.h"
13 :
14 : #include "../../disco/tiles.h"
15 : #include "../../disco/fd_txn_m.h"
16 : #include "../../disco/store/fd_store.h"
17 : #include "../../disco/pack/fd_pack.h"
18 : #include "../../discof/fd_accdb_topo.h"
19 : #include "../../discof/reasm/fd_reasm.h"
20 : #include "../../disco/keyguard/fd_keyload.h"
21 : #include "../../disco/genesis/fd_genesis_cluster.h"
22 : #include "../../discof/genesis/genesis_hash.h"
23 : #include "../../util/pod/fd_pod.h"
24 : #include "../../flamenco/accdb/fd_accdb_admin_v1.h"
25 : #include "../../flamenco/accdb/fd_accdb_admin_v2.h"
26 : #include "../../flamenco/accdb/fd_accdb_impl_v1.h"
27 : #include "../../flamenco/accdb/fd_accdb_sync.h"
28 : #include "../../flamenco/accdb/fd_vinyl_req_pool.h"
29 : #include "../../flamenco/rewards/fd_rewards.h"
30 : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
31 : #include "../../flamenco/progcache/fd_progcache_admin.h"
32 : #include "../../disco/metrics/fd_metrics.h"
33 :
34 : #include "../../flamenco/runtime/fd_runtime.h"
35 : #include "../../flamenco/runtime/fd_runtime_stack.h"
36 : #include "../../flamenco/runtime/fd_genesis_parse.h"
37 : #include "../../flamenco/fd_flamenco_base.h"
38 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
39 : #include "../../flamenco/runtime/program/fd_precompiles.h"
40 :
41 : # if FD_HAS_FLATCC
42 : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
43 : #endif
44 :
45 : #include <stdio.h>
46 :
47 : /* Replay concepts:
48 :
49 : - Blocks are aggregations of entries aka. microblocks which are
50 : groupings of txns and are constructed by the block producer (see
51 : fd_pack).
52 :
53 : - Entries are grouped into entry batches by the block producer (see
54 : fd_pack / fd_shredder).
55 :
56 : - Entry batches are divided into chunks known as shreds by the block
57 : producer (see fd_shredder).
58 :
59 : - Shreds are grouped into forward-error-correction sets (FEC sets) by
60 : the block producer (see fd_shredder).
61 :
62 : - Shreds are transmitted to the rest of the cluster via the Turbine
63 : protocol (see fd_shredder / fd_shred).
64 :
65 : - Once enough shreds within a FEC set are received to recover the
66 : entirety of the shred data encoded by that FEC set, the receiver
67 : can "complete" the FEC set (see fd_fec_resolver).
68 :
69 : - If shreds in the FEC set are missing such that it can't complete,
70 : the receiver can use the Repair protocol to request missing shreds
71 : in FEC set (see fd_repair).
72 :
73 : - The current Repair protocol does not support requesting coding
74 : shreds. As a result, some FEC sets might be actually complete
75 : (contain all data shreds). Repair currently hacks around this by
76 : forcing completion but the long-term solution is to add support for
77 : fec_repairing coding shreds via Repair.
78 :
79 : - FEC sets are delivered in partial-order to the Replay tile by the
80 : Repair tile. Currently Replay only supports replaying entry batches
81 : so FEC sets need to reassembled into an entry batch before they can
82 : be replayed. The new Dispatcher will change this by taking a FEC
83 : set as input instead. */
84 :
85 0 : #define IN_KIND_SNAP ( 0)
86 0 : #define IN_KIND_GENESIS ( 1)
87 0 : #define IN_KIND_IPECHO ( 2)
88 0 : #define IN_KIND_TOWER ( 3)
89 0 : #define IN_KIND_RESOLV ( 4)
90 0 : #define IN_KIND_POH ( 5)
91 0 : #define IN_KIND_EXECRP ( 6)
92 0 : #define IN_KIND_SHRED ( 7)
93 0 : #define IN_KIND_TXSEND ( 8)
94 0 : #define IN_KIND_GUI ( 9)
95 0 : #define IN_KIND_RPC (10)
96 0 : #define IN_KIND_GOSSIP_OUT (11)
97 :
98 : #define DEBUG_LOGGING 0
99 :
100 : /* The first bank that the replay tile produces either for genesis
101 : or the snapshot boot will always be at bank index 0. */
102 0 : #define FD_REPLAY_BOOT_BANK_IDX (0UL)
103 :
104 : struct fd_replay_in_link {
105 : fd_wksp_t * mem;
106 : ulong chunk0;
107 : ulong wmark;
108 : ulong mtu;
109 : };
110 :
111 : typedef struct fd_replay_in_link fd_replay_in_link_t;
112 :
113 : struct fd_replay_out_link {
114 : ulong idx;
115 : fd_wksp_t * mem;
116 : ulong chunk0;
117 : ulong wmark;
118 : ulong chunk;
119 : };
120 :
121 : typedef struct fd_replay_out_link fd_replay_out_link_t;
122 :
123 : /* fd_block_id_map is a simple map of block-ids to bank indices. The
124 : map sits on top of an array of fd_block_id_ele_t. This serves as a
125 : translation layer between block ids to bank indices. The data
126 : array is indexed by bank index and the latest observed merkle root
127 : for the bank index is stored in the array. Once the block id has
128 : been observed, the entry is keyed by the latest merkle root (aka the
129 : block id). */
130 :
131 : struct fd_block_id_ele {
132 : fd_hash_t latest_mr;
133 : uint latest_fec_idx;
134 : int block_id_seen;
135 : ulong slot;
136 : ulong next_;
137 : };
138 : typedef struct fd_block_id_ele fd_block_id_ele_t;
139 :
140 : #define MAP_NAME fd_block_id_map
141 : #define MAP_ELE_T fd_block_id_ele_t
142 : #define MAP_KEY_T fd_hash_t
143 0 : #define MAP_KEY latest_mr
144 0 : #define MAP_NEXT next_
145 0 : #define MAP_KEY_EQ(k0,k1) (!memcmp((k0),(k1), sizeof(fd_hash_t)))
146 0 : #define MAP_KEY_HASH(key,seed) (fd_hash((seed),(key),sizeof(fd_hash_t)))
147 : #include "../../util/tmpl/fd_map_chain.c"
148 :
149 : static inline ulong
150 0 : fd_block_id_ele_get_idx( fd_block_id_ele_t * ele_arr, fd_block_id_ele_t * ele ) {
151 0 : return (ulong)(ele - ele_arr);
152 0 : }
153 :
154 : struct fd_replay_tile {
155 : fd_wksp_t * wksp;
156 :
157 : fd_accdb_admin_t accdb_admin[1];
158 : fd_accdb_user_t accdb[1];
159 : fd_progcache_admin_t progcache_admin[1];
160 :
161 : fd_txncache_t * txncache;
162 : fd_store_t * store;
163 : fd_banks_t banks[1];
164 :
165 : /* This flag is 1 If we have seen a vote signature that our node has
166 : sent out get rooted at least one time. The value is 0 otherwise.
167 : We can't become leader and pack blocks until this flag has been
168 : set. This parallels the Agave 'has_new_vote_been_rooted'. */
169 : int has_identity_vote_rooted;
170 : int wait_for_vote_to_start_leader;
171 :
172 : /* wfs_enabled is 1 if the validator is booted in
173 : wait_for_supermajority mode. In this mode replay (and, by extension,
174 : downstream consumers) is not allowed to make progress until 80% of
175 : the cluster has published their ContactInfo in Gossip with a
176 : shred version matching expected_shred_version. When this happens,
177 : wfs_complete will be set to 1. */
178 : int wfs_enabled;
179 : int wfs_complete;
180 :
181 : fd_hash_t expected_bank_hash;
182 :
183 : ulong reasm_seed;
184 : fd_reasm_t * reasm;
185 :
186 : fd_sched_t * sched;
187 :
188 : ulong vote_tracker_seed;
189 : fd_vote_tracker_t * vote_tracker;
190 :
191 : int has_genesis_hash;
192 : char genesis_path[ PATH_MAX ];
193 : fd_hash_t genesis_hash[1];
194 : fd_genesis_t genesis[1];
195 : ulong cluster_type;
196 :
197 : #define FD_REPLAY_HARD_FORKS_MAX (64UL)
198 : ulong hard_forks_cnt;
199 : ulong hard_forks[ FD_REPLAY_HARD_FORKS_MAX ];
200 : ulong hard_forks_cnts[ FD_REPLAY_HARD_FORKS_MAX ];
201 :
202 : ushort expected_shred_version;
203 : ushort ipecho_shred_version;
204 :
205 : /* A note on publishing ...
206 :
207 : The watermarks are used to publish our fork-aware structures. For
208 : example, store, banks, and txncache need to be published to release
209 : resources occupied by rooted or dead blocks. In general,
210 : publishing has the effect of pruning forks in those structures,
211 : indicating that it is ok to release the memory being occupied by
212 : the blocks on said forks. Tower is responsible for informing us of
213 : the latest block on the consensus rooted fork. As soon as we can,
214 : we should move the published root as close as possible to the
215 : latest consensus root, publishing/pruning everything on the fork
216 : tree along the way. That is, all the blocks that directly descend
217 : from the current published root (inclusive) to the new published
218 : root (exclusive) on the rooted fork, as well as all the minority
219 : forks that branch from said blocks.
220 :
221 : Ideally, we'd move the published root to the consensus root
222 : immediately upon receiving a new consensus root. However, that's
223 : not always safe to do. One thing we need to be careful about is
224 : making sure that there are no more users/consumers of
225 : soon-to-be-pruned blocks, lest a use-after-free occurs. This can
226 : be done by using a reference counter for each block. Any
227 : concurrent activity, such as transaction execution in the exec
228 : tiles, should retain a refcnt on the block for as
229 : long as it needs access to the shared fork-aware structures related
230 : to that block. Eventually, refcnt on a given block will drop down
231 : to 0 as the block either finishes replaying or gets marked as dead,
232 : and any other tile that has retained a refcnt on the block releases
233 : it. At that point, it becomes a candidate for pruning. The key to
234 : safe publishing then becomes figuring out how far we could advance
235 : the published root, such that every minority fork branching off of
236 : blocks in between the current published root (inclusive) and the
237 : new published root (exclusive) is safe to be pruned. This is a
238 : straightforward tree traversal, where if a block B on the rooted
239 : fork has refcnt 0, and all minority forks branching off of B also
240 : have refcnt 0, then B is safe to be pruned. We advance the
241 : published root to the farthest consecutively prunable block on the
242 : rooted fork. Note that reasm presents the replay tile with a clean
243 : view of the world where every block is chained off of a parent
244 : block. So there are no orpahned/dangling tree nodes to worry
245 : about. The world is a nice single tree as far as replay is
246 : concerned.
247 :
248 : In the following fork tree, every node is a block and the number in
249 : parentheses is the refcnt on the block. The chain marked with
250 : double slashes is the rooted fork. Suppose the published root is
251 : at block P, and consensus root is at block T. We can't publish
252 : past block P because Q has refcnt 1.
253 :
254 :
255 : P(0)
256 : / \\
257 : Q(1) A(0)
258 : / || \
259 : X(0) B(0) C(0)
260 : / || \
261 : Y(0) M(0) R(0)
262 : / || / \
263 : D(2) T(0) J(0) L(0)
264 : ||
265 : ..
266 : ..
267 : ..
268 : ||
269 : blocks we might be actively replaying
270 :
271 :
272 : When refcnt on Q drops to 0, we would be able to advance the
273 : published root to block M, because blocks P, A, and B, as well as
274 : all subtrees branching off of them, have refcnt 0, and therefore
275 : can be pruned. Block M itself cannot be pruned yet because its
276 : child block D has refcnt 2. After publishing/pruning, the fork
277 : tree would be:
278 :
279 :
280 : M(0)
281 : / ||
282 : D(2) T(0)
283 : ||
284 : ..
285 : ..
286 : ..
287 : ||
288 : blocks we might be actively replaying
289 :
290 :
291 : As a result, the shared fork-aware structures can free resources
292 : for blocks P, A, B, and all subtrees branching off of them.
293 :
294 : For the reference counting part, the replay tile is the sole entity
295 : that can update the refcnt. This ensures that all refcnt increment
296 : and decrement attempts are serialized at the replay tile, and that
297 : there are no racy resurrection of a soon-to-be-pruned block. If a
298 : refcnt increment request arrives after a block has been pruned,
299 : replay simply rejects the request.
300 :
301 : A note on the implementation of the above ...
302 :
303 : Upon receiving a new consensus root, we descend down the rooted
304 : fork from the current published root to the new consensus root. On
305 : each node/block of the rooted fork, we do a summation of the refcnt
306 : on the block and all the minority fork blocks branching from the
307 : block. If the summation is 0, the block is safe for pruning. We
308 : advance the published root to the far end of the consecutive run of
309 : 0 refcnt sums originating from the current published root. On our
310 : descent down the minority forks, we also mark any block that hasn't
311 : finished replaying as dead, so we don't waste time executing them.
312 : No more transactions shall be dispatched for execution from dead
313 : blocks.
314 :
315 : Blocks start out with a refcnt of 0. Other tiles may send a
316 : request to the replay tile for a reference on a block. The
317 : transaction dispatcher is another source of refcnt updates. On
318 : every dispatch of a transaction for block B, we increment the
319 : refcnt for B. And on every transaction finalization, we decrement
320 : the refcnt for B. This means that whenever the refcnt on a block
321 : is 0, there is no more reference on that block from the execution
322 : pipeline. While it might be tempting to simply increment the
323 : refcnt once when we start replaying a block, and decrement the
324 : refcnt once when we finish a block, this more fine-grained refcnt
325 : update strategy allows for aborting and potentially immediate
326 : pruning of blocks under interleaved block replay. Upon receiving a
327 : new consensus root, we can simply look at the refcnt on minority
328 : fork blocks, and a refcnt of 0 would imply that the block is safe
329 : for pruning, even if we haven't finished replaying it. Without the
330 : fine-grained refcnt, we would need to first stop dispatching from
331 : the aborted block, and then wait for a full drain of the execution
332 : pipeline to know for sure that there are no more in-flight
333 : transactions executing on the aborted block. Note that this will
334 : allow the refcnt on any block to transiently drop down to 0. We
335 : will not mistakenly prune an actively replaying block, aka a leaf
336 : node, that is chaining off of the rooted fork, because the
337 : consensus root is always an ancestor of the actively replaying tip.
338 : */
339 : fd_hash_t consensus_root; /* The most recent block to have reached max lockout in the tower. */
340 : ulong consensus_root_slot; /* slot number of the above. */
341 : ulong consensus_root_bank_idx; /* bank index of the above. */
342 : ulong published_root_slot; /* slot number of the published root. */
343 : ulong published_root_bank_idx; /* bank index of the published root. */
344 :
345 : /* We need to maintain a tile-local mapping of block-ids to bank index
346 : and vice versa. This translation layer is needed for conversion
347 : since tower operates on block-ids and downstream consumers of FEC
348 : sets operate on bank indices. This mapping must happen both ways:
349 : 1. tower sends us block ids and we must map them to bank indices.
350 : 2. when a block is completed, we must map the bank index to a block
351 : id to send a slot complete message to tower. */
352 : ulong block_id_len;
353 : fd_block_id_ele_t * block_id_arr;
354 : ulong block_id_map_seed;
355 : fd_block_id_map_t * block_id_map;
356 :
357 : /* Capture-related configs */
358 : fd_capture_ctx_t * capture_ctx;
359 : FILE * capture_file;
360 : fd_capture_link_buf_t cap_repl_out[1];
361 :
362 : /* Protobuf dumping context for debugging runtime execution and
363 : collecting seed corpora. */
364 : fd_dump_proto_ctx_t * dump_proto_ctx;
365 :
366 : /* Whether the runtime has been booted either from snapshot loading
367 : or from genesis. */
368 : int is_booted;
369 :
370 : /* Buffer to store vote towers that need to be published to the Tower
371 : tile. */
372 :
373 : fd_multi_epoch_leaders_t * mleaders;
374 :
375 : int larger_max_cost_per_block;
376 :
377 : fd_pubkey_t identity_pubkey[1]; /* TODO: Keyswitch */
378 :
379 : /* When we transition to becoming leader, we can only unbecome the
380 : leader if we have received a block id from the FEC reassembler, and
381 : a message from PoH that the leader slot has ended. After both of
382 : these conditions are met, then we are free to unbecome the leader.
383 : */
384 : uint is_leader : 1;
385 : uint supports_leader : 1;
386 : int recv_poh;
387 : ulong next_leader_slot;
388 : long next_leader_tickcount;
389 : ulong highwater_leader_slot;
390 : ulong reset_slot;
391 : fd_bank_t reset_bank[1];
392 : fd_hash_t reset_block_id;
393 : long reset_timestamp_nanos;
394 : double slot_duration_nanos;
395 : double slot_duration_ticks;
396 : fd_bank_t leader_bank[1];
397 :
398 : ulong resolv_tile_cnt;
399 :
400 : int in_kind[ 128 ];
401 : fd_replay_in_link_t in[ 128 ];
402 :
403 : fd_replay_out_link_t exec_out[ 1 ];
404 :
405 : fd_replay_out_link_t replay_out[1];
406 :
407 : fd_replay_out_link_t epoch_out[1];
408 :
409 : /* The gui tile needs to reliably own a reference to the most recent
410 : completed active bank. Replay needs to know if the gui as a
411 : consumer is enabled so it can increment the bank's refcnt before
412 : publishing the bank_idx to the gui. */
413 : int gui_enabled;
414 : int rpc_enabled;
415 :
416 : # if FD_HAS_FLATCC
417 : /* For dumping blocks to protobuf. For backtest only. */
418 : fd_block_dump_ctx_t * block_dump_ctx;
419 : # endif
420 :
421 : /* We need a few pieces of information to compute the right addresses
422 : for bundle crank information that we need to send to pack. */
423 : struct {
424 : int enabled;
425 : fd_pubkey_t vote_account;
426 : fd_bundle_crank_gen_t gen[1];
427 : } bundle;
428 :
429 : struct {
430 : ulong store_query_acquire;
431 : ulong store_query_release;
432 : fd_histf_t store_query_wait[1];
433 : fd_histf_t store_query_work[1];
434 : ulong store_query_cnt;
435 : ulong store_query_missing_cnt;
436 : ulong store_query_mr;
437 : ulong store_query_missing_mr;
438 :
439 : ulong slots_total;
440 : ulong transactions_total;
441 :
442 : ulong reasm_latest_slot;
443 : ulong reasm_latest_fec_idx;
444 :
445 : ulong sched_full;
446 : ulong reasm_empty;
447 : ulong leader_bid_wait;
448 : ulong banks_full;
449 : ulong storage_root_behind;
450 :
451 : fd_histf_t root_slot_dur[1];
452 : fd_histf_t root_account_dur[1];
453 : } metrics;
454 :
455 : uchar __attribute__((aligned(FD_MULTI_EPOCH_LEADERS_ALIGN))) mleaders_mem[ FD_MULTI_EPOCH_LEADERS_FOOTPRINT ];
456 :
457 : fd_runtime_stack_t runtime_stack;
458 : };
459 :
460 : typedef struct fd_replay_tile fd_replay_tile_t;
461 :
462 : FD_FN_CONST static inline ulong
463 0 : scratch_align( void ) {
464 0 : return 128UL;
465 0 : }
466 : FD_FN_PURE static inline ulong
467 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
468 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
469 :
470 0 : ulong l = FD_LAYOUT_INIT;
471 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
472 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_block_id_ele_t), sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
473 0 : l = FD_LAYOUT_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
474 0 : l = FD_LAYOUT_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
475 0 : l = FD_LAYOUT_APPEND( l, fd_reasm_align(), fd_reasm_footprint( tile->replay.fec_max ) );
476 0 : l = FD_LAYOUT_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots ) );
477 0 : l = FD_LAYOUT_APPEND( l, fd_vinyl_req_pool_align(), fd_vinyl_req_pool_footprint( 1UL, 1UL ) );
478 0 : l = FD_LAYOUT_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
479 0 : l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
480 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
481 :
482 0 : # if FD_HAS_FLATCC
483 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
484 0 : l = FD_LAYOUT_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
485 0 : }
486 0 : # endif
487 :
488 0 : l = FD_LAYOUT_FINI( l, scratch_align() );
489 :
490 0 : return l;
491 0 : }
492 :
493 : static inline void
494 0 : metrics_write( fd_replay_tile_t * ctx ) {
495 0 : FD_MCNT_SET ( REPLAY, STORE_QUERY_ACQUIRE, ctx->metrics.store_query_acquire );
496 0 : FD_MCNT_SET ( REPLAY, STORE_QUERY_RELEASE, ctx->metrics.store_query_release );
497 0 : FD_MHIST_COPY( REPLAY, STORE_QUERY_WAIT, ctx->metrics.store_query_wait );
498 0 : FD_MHIST_COPY( REPLAY, STORE_QUERY_WORK, ctx->metrics.store_query_work );
499 0 : FD_MCNT_SET ( REPLAY, STORE_QUERY_CNT, ctx->metrics.store_query_cnt );
500 0 : FD_MCNT_SET ( REPLAY, STORE_QUERY_MISSING_CNT, ctx->metrics.store_query_missing_cnt );
501 0 : FD_MGAUGE_SET( REPLAY, STORE_QUERY_MR, ctx->metrics.store_query_mr );
502 0 : FD_MGAUGE_SET( REPLAY, STORE_QUERY_MISSING_MR, ctx->metrics.store_query_missing_mr );
503 :
504 0 : FD_MGAUGE_SET( REPLAY, ROOT_SLOT, ctx->consensus_root_slot==ULONG_MAX ? 0UL : ctx->consensus_root_slot );
505 0 : ulong leader_slot = ctx->leader_bank->data ? fd_bank_slot_get( ctx->leader_bank ) : 0UL;
506 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
507 :
508 0 : if( FD_LIKELY( ctx->leader_bank->data ) ) {
509 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, leader_slot );
510 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
511 0 : } else {
512 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, ctx->next_leader_slot==ULONG_MAX ? 0UL : ctx->next_leader_slot );
513 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, 0UL );
514 0 : }
515 0 : FD_MGAUGE_SET( REPLAY, RESET_SLOT, ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot );
516 :
517 0 : fd_bank_data_t * bank_pool = fd_banks_get_bank_pool( ctx->banks->data );
518 0 : ulong live_banks = fd_banks_pool_max( bank_pool ) - fd_banks_pool_free( bank_pool );
519 0 : FD_MGAUGE_SET( REPLAY, LIVE_BANKS, live_banks );
520 :
521 0 : ulong reasm_free = fd_reasm_free( ctx->reasm );
522 0 : FD_MGAUGE_SET( REPLAY, REASM_FREE, reasm_free );
523 :
524 0 : FD_MCNT_SET( REPLAY, SLOTS_TOTAL, ctx->metrics.slots_total );
525 0 : FD_MCNT_SET( REPLAY, TRANSACTIONS_TOTAL, ctx->metrics.transactions_total );
526 :
527 0 : FD_MGAUGE_SET( REPLAY, REASM_LATEST_SLOT, ctx->metrics.reasm_latest_slot );
528 0 : FD_MGAUGE_SET( REPLAY, REASM_LATEST_FEC_IDX, ctx->metrics.reasm_latest_fec_idx );
529 :
530 0 : FD_MCNT_SET( REPLAY, SCHED_FULL, ctx->metrics.sched_full );
531 0 : FD_MCNT_SET( REPLAY, REASM_EMPTY, ctx->metrics.reasm_empty );
532 0 : FD_MCNT_SET( REPLAY, LEADER_BID_WAIT, ctx->metrics.leader_bid_wait );
533 0 : FD_MCNT_SET( REPLAY, BANKS_FULL, ctx->metrics.banks_full );
534 0 : FD_MCNT_SET( REPLAY, STORAGE_ROOT_BEHIND, ctx->metrics.storage_root_behind );
535 :
536 0 : FD_MCNT_SET( REPLAY, PROGCACHE_ROOTED, ctx->progcache_admin->metrics.root_cnt );
537 0 : FD_MCNT_SET( REPLAY, PROGCACHE_GC_ROOT, ctx->progcache_admin->metrics.gc_root_cnt );
538 :
539 0 : FD_MCNT_SET( REPLAY, ACCDB_CREATED, ctx->accdb->base.created_cnt );
540 0 : FD_MCNT_SET( REPLAY, ACCDB_REVERTED, ctx->accdb_admin->base.revert_cnt );
541 0 : FD_MCNT_SET( REPLAY, ACCDB_ROOTED, ctx->accdb_admin->base.root_cnt );
542 0 : FD_MCNT_SET( REPLAY, ACCDB_ROOTED_BYTES, ctx->accdb_admin->base.root_tot_sz );
543 0 : FD_MCNT_SET( REPLAY, ACCDB_GC_ROOT, ctx->accdb_admin->base.gc_root_cnt );
544 0 : FD_MCNT_SET( REPLAY, ACCDB_RECLAIMED, ctx->accdb_admin->base.reclaim_cnt );
545 0 : FD_MHIST_COPY( REPLAY, ROOT_SLOT_DURATION_SECONDS, ctx->metrics.root_slot_dur );
546 0 : FD_MHIST_COPY( REPLAY, ROOT_ACCOUNT_DURATION_SECONDS, ctx->metrics.root_account_dur );
547 0 : FD_MCNT_SET( REPLAY, ROOT_ELAPSED_SECONDS_DB, (ulong)ctx->accdb_admin->base.dt_vinyl );
548 0 : FD_MCNT_SET( REPLAY, ROOT_ELAPSED_SECONDS_COPY, (ulong)ctx->accdb_admin->base.dt_copy );
549 0 : FD_MCNT_SET( REPLAY, ROOT_ELAPSED_SECONDS_GC, (ulong)ctx->accdb_admin->base.dt_gc );
550 0 : }
551 :
552 : static inline ulong
553 : generate_epoch_info_msg( ulong epoch,
554 : fd_epoch_schedule_t const * epoch_schedule,
555 : fd_vote_states_t const * epoch_stakes,
556 : fd_features_t const * features,
557 : fd_epoch_info_msg_t * epoch_info_msg,
558 0 : int current_epoch ) {
559 0 : fd_vote_stake_weight_t * stake_weights = epoch_info_msg->weights;
560 :
561 0 : epoch_info_msg->epoch = epoch;
562 0 : epoch_info_msg->start_slot = fd_epoch_slot0( epoch_schedule, epoch );
563 0 : epoch_info_msg->slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
564 0 : epoch_info_msg->excluded_stake = 0UL;
565 0 : epoch_info_msg->vote_keyed_lsched = 1UL;
566 :
567 : /* FIXME: SIMD-0180 - hack to (de)activate in testnet vs mainnet.
568 : This code can be removed once the feature is active. */
569 0 : if( (1==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_TESTNET) ||
570 0 : (0==epoch_schedule->warmup && epoch<FD_SIMD0180_ACTIVE_EPOCH_MAINNET) ) {
571 0 : epoch_info_msg->vote_keyed_lsched = 0UL;
572 0 : }
573 :
574 : /* epoch_stakes from manifest are already filtered (stake>0), but not sorted */
575 0 : fd_vote_states_iter_t iter_[1];
576 0 : ulong idx = 0UL;
577 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 ) ) {
578 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
579 :
580 0 : ulong stake = current_epoch ? vote_state->stake_t_1 : vote_state->stake_t_2;
581 0 : if( FD_UNLIKELY( !stake ) ) continue;
582 :
583 0 : stake_weights[ idx ].stake = stake;
584 0 : memcpy( stake_weights[ idx ].id_key.uc, &vote_state->node_account, sizeof(fd_pubkey_t) );
585 0 : memcpy( stake_weights[ idx ].vote_key.uc, &vote_state->vote_account, sizeof(fd_pubkey_t) );
586 0 : idx++;
587 0 : }
588 0 : epoch_info_msg->staked_cnt = idx;
589 0 : sort_vote_weights_by_stake_vote_inplace( stake_weights, idx );
590 :
591 0 : epoch_info_msg->epoch_schedule = *epoch_schedule;
592 0 : epoch_info_msg->features = *features;
593 :
594 0 : return fd_epoch_info_msg_sz( epoch_info_msg->staked_cnt );
595 0 : }
596 :
597 : static void
598 : publish_epoch_info( fd_replay_tile_t * ctx,
599 : fd_stem_context_t * stem,
600 : fd_bank_t * bank,
601 0 : int current_epoch ) {
602 0 : fd_epoch_schedule_t const * schedule = fd_bank_epoch_schedule_query( bank );
603 0 : ulong epoch = fd_slot_to_epoch( schedule, fd_bank_slot_get( bank ), NULL );
604 :
605 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query( bank );
606 :
607 0 : fd_features_t const * features = fd_bank_features_query( bank );
608 :
609 0 : fd_epoch_info_msg_t * epoch_info_msg = fd_chunk_to_laddr( ctx->epoch_out->mem, ctx->epoch_out->chunk );
610 0 : ulong epoch_info_sz = generate_epoch_info_msg( epoch+fd_ulong_if( current_epoch, 1UL, 0UL), schedule, vote_states, features, epoch_info_msg, current_epoch );
611 0 : ulong epoch_info_sig = 4UL;
612 0 : fd_stem_publish( stem, ctx->epoch_out->idx, epoch_info_sig, ctx->epoch_out->chunk, epoch_info_sz, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
613 0 : ctx->epoch_out->chunk = fd_dcache_compact_next( ctx->epoch_out->chunk, epoch_info_sz, ctx->epoch_out->chunk0, ctx->epoch_out->wmark );
614 :
615 0 : fd_multi_epoch_leaders_epoch_msg_init( ctx->mleaders, epoch_info_msg );
616 0 : fd_multi_epoch_leaders_epoch_msg_fini( ctx->mleaders );
617 :
618 0 : fd_bank_vote_states_end_locking_query( bank );
619 0 : }
620 :
621 : /**********************************************************************/
622 : /* Transaction execution state machine helpers */
623 : /**********************************************************************/
624 :
625 : static void
626 : replay_block_start( fd_replay_tile_t * ctx,
627 : fd_stem_context_t * stem,
628 : ulong bank_idx,
629 : ulong parent_bank_idx,
630 0 : ulong slot ) {
631 0 : long before = fd_log_wallclock();
632 :
633 0 : fd_bank_t bank[1];
634 0 : if( FD_UNLIKELY( !fd_banks_bank_query( bank, ctx->banks, bank_idx ) ) ) {
635 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
636 0 : }
637 0 : if( FD_UNLIKELY( bank->data->flags!=FD_BANK_FLAGS_INIT ) ) {
638 0 : FD_LOG_CRIT(( "invariant violation: bank is not in correct state for bank index %lu", bank_idx ));
639 0 : }
640 :
641 0 : bank->data->preparation_begin_nanos = before;
642 :
643 0 : fd_bank_t parent_bank[1];
644 0 : if( FD_UNLIKELY( !fd_banks_bank_query( parent_bank, ctx->banks, parent_bank_idx ) ) ) {
645 0 : FD_LOG_CRIT(( "invariant violation: parent bank is NULL for bank index %lu", parent_bank_idx ));
646 0 : }
647 0 : ulong parent_slot = fd_bank_slot_get( parent_bank );
648 :
649 : /* Clone the bank from the parent. We must special case the first
650 : slot that is executed as the snapshot does not provide a parent
651 : block id. */
652 :
653 0 : if( FD_UNLIKELY( !fd_banks_clone_from_parent( bank, ctx->banks, bank_idx ) ) ) {
654 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
655 0 : }
656 0 : fd_bank_slot_set( bank, slot );
657 0 : fd_bank_parent_slot_set( bank, parent_slot );
658 0 : bank->data->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->data->txncache_fork_id );
659 :
660 : /* Create a new funk txn for the block. */
661 :
662 0 : fd_funk_txn_xid_t xid = { .ul = { slot, bank_idx } };
663 0 : fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
664 0 : fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
665 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
666 :
667 : /* Update any required runtime state and handle any potential epoch
668 : boundary change. */
669 :
670 0 : fd_bank_shred_cnt_set( bank, 0UL );
671 0 : fd_bank_execution_fees_set( bank, 0UL );
672 0 : fd_bank_priority_fees_set( bank, 0UL );
673 0 : fd_bank_tips_set( bank, 0UL );
674 :
675 0 : fd_bank_has_identity_vote_set( bank, 0 );
676 :
677 : /* Update block height. */
678 0 : fd_bank_block_height_set( bank, fd_bank_block_height_get( bank ) + 1UL );
679 :
680 0 : int is_epoch_boundary = 0;
681 0 : fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
682 0 : if( FD_UNLIKELY( is_epoch_boundary ) ) publish_epoch_info( ctx, stem, bank, 1 );
683 :
684 0 : ulong max_tick_height;
685 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( fd_bank_ticks_per_slot_get( parent_bank ), slot, &max_tick_height ) ) ) {
686 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, fd_bank_ticks_per_slot_get( parent_bank ) ));
687 0 : }
688 0 : fd_bank_max_tick_height_set( bank, max_tick_height );
689 0 : fd_bank_tick_height_set( bank, fd_bank_max_tick_height_get( parent_bank ) ); /* The parent's max tick height is our starting tick height. */
690 0 : fd_sched_set_poh_params( ctx->sched, bank->data->idx, fd_bank_tick_height_get( bank ), fd_bank_max_tick_height_get( bank ), fd_bank_hashes_per_tick_get( bank ), fd_bank_poh_query( parent_bank ) );
691 :
692 0 : FD_LOG_DEBUG(( "replay_block_start: bank_idx=%lu slot=%lu parent_bank_idx=%lu", bank_idx, slot, parent_bank_idx ));
693 0 : }
694 :
695 : static void
696 0 : cost_tracker_snap( fd_bank_t * bank, fd_replay_slot_completed_t * slot_info ) {
697 0 : if( bank->data->cost_tracker_pool_idx!=fd_bank_cost_tracker_pool_idx_null( fd_bank_get_cost_tracker_pool( bank->data ) ) ) {
698 0 : fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( bank );
699 0 : slot_info->cost_tracker.block_cost = cost_tracker->block_cost;
700 0 : slot_info->cost_tracker.vote_cost = cost_tracker->vote_cost;
701 0 : slot_info->cost_tracker.allocated_accounts_data_size = cost_tracker->allocated_accounts_data_size;
702 0 : slot_info->cost_tracker.block_cost_limit = cost_tracker->block_cost_limit;
703 0 : slot_info->cost_tracker.vote_cost_limit = cost_tracker->vote_cost_limit;
704 0 : slot_info->cost_tracker.account_cost_limit = cost_tracker->account_cost_limit;
705 0 : fd_bank_cost_tracker_end_locking_query( bank );
706 0 : } else {
707 0 : memset( &slot_info->cost_tracker, 0, sizeof(slot_info->cost_tracker) );
708 0 : }
709 0 : }
710 :
711 : static ulong
712 0 : get_identity_balance( fd_replay_tile_t * ctx, fd_funk_txn_xid_t xid ) {
713 0 : ulong identity_balance = ULONG_MAX;
714 0 : fd_accdb_ro_t identity_acc[1];
715 0 : if( FD_LIKELY( fd_accdb_open_ro( ctx->accdb, identity_acc, &xid, ctx->identity_pubkey ) ) ) {
716 0 : identity_balance = identity_acc->meta->lamports;
717 0 : fd_accdb_close_ro( ctx->accdb, identity_acc );
718 0 : }
719 0 : return identity_balance;
720 0 : }
721 :
722 : static void
723 : publish_slot_completed( fd_replay_tile_t * ctx,
724 : fd_stem_context_t * stem,
725 : fd_bank_t * bank,
726 : int is_initial,
727 0 : int is_leader ) {
728 :
729 0 : ulong slot = fd_bank_slot_get( bank );
730 :
731 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank->data->idx ];
732 :
733 : /* HACKY: hacky way of checking if we should send a null parent block
734 : id */
735 0 : fd_hash_t parent_block_id = {0};
736 0 : if( FD_UNLIKELY( !is_initial ) ) {
737 0 : parent_block_id = ctx->block_id_arr[ bank->data->parent_idx ].latest_mr;
738 0 : }
739 :
740 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank );
741 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
742 0 : FD_TEST( bank_hash );
743 0 : FD_TEST( block_hash );
744 :
745 0 : if( FD_LIKELY( !is_initial ) ) fd_txncache_finalize_fork( ctx->txncache, bank->data->txncache_fork_id, 0UL, block_hash->uc );
746 :
747 0 : fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
748 0 : ulong slot_idx;
749 0 : ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
750 :
751 0 : ctx->metrics.slots_total++;
752 0 : ctx->metrics.transactions_total = fd_bank_txn_count_get( bank );
753 :
754 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
755 0 : slot_info->slot = slot;
756 0 : slot_info->root_slot = ctx->consensus_root_slot;
757 0 : slot_info->storage_slot = ctx->published_root_slot;
758 0 : slot_info->epoch = epoch;
759 0 : slot_info->slot_in_epoch = slot_idx;
760 0 : slot_info->block_height = fd_bank_block_height_get( bank );
761 0 : slot_info->parent_slot = fd_bank_parent_slot_get( bank );
762 0 : slot_info->block_id = block_id_ele->latest_mr;
763 0 : slot_info->parent_block_id = parent_block_id;
764 0 : slot_info->bank_hash = *bank_hash;
765 0 : slot_info->block_hash = *block_hash;
766 0 : slot_info->transaction_count = fd_bank_txn_count_get( bank );
767 :
768 0 : fd_inflation_t inflation = fd_bank_inflation_get( bank );
769 0 : slot_info->inflation.foundation = inflation.foundation;
770 0 : slot_info->inflation.foundation_term = inflation.foundation_term;
771 0 : slot_info->inflation.terminal = inflation.terminal;
772 0 : slot_info->inflation.initial = inflation.initial;
773 0 : slot_info->inflation.taper = inflation.taper;
774 :
775 0 : fd_rent_t rent = fd_bank_rent_get( bank );
776 0 : slot_info->rent.burn_percent = rent.burn_percent;
777 0 : slot_info->rent.lamports_per_uint8_year = rent.lamports_per_uint8_year;
778 0 : slot_info->rent.exemption_threshold = rent.exemption_threshold;
779 :
780 0 : slot_info->first_fec_set_received_nanos = bank->data->first_fec_set_received_nanos;
781 0 : slot_info->preparation_begin_nanos = bank->data->preparation_begin_nanos;
782 0 : slot_info->first_transaction_scheduled_nanos = bank->data->first_transaction_scheduled_nanos;
783 0 : slot_info->last_transaction_finished_nanos = bank->data->last_transaction_finished_nanos;
784 0 : slot_info->completion_time_nanos = fd_log_wallclock();
785 :
786 : /* refcnt should be incremented by 1 for each consumer that uses
787 : `bank_idx`. Each consumer should decrement the bank's refcnt once
788 : they are done using the bank. */
789 0 : bank->data->refcnt++; /* tower_tile */
790 0 : if( FD_LIKELY( ctx->rpc_enabled ) ) bank->data->refcnt++; /* rpc tile */
791 0 : if( FD_LIKELY( ctx->gui_enabled ) ) bank->data->refcnt++; /* gui tile */
792 0 : slot_info->bank_idx = bank->data->idx;
793 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for tower, rpc, gui", bank->data->idx, slot, bank->data->refcnt ));
794 :
795 0 : slot_info->parent_bank_idx = ULONG_MAX;
796 0 : fd_bank_t parent_bank[1];
797 0 : if( FD_LIKELY( fd_banks_get_parent( parent_bank, ctx->banks, bank ) && ctx->gui_enabled ) ) {
798 0 : parent_bank->data->refcnt++;
799 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for gui", parent_bank->data->idx, fd_bank_slot_get( parent_bank ), parent_bank->data->refcnt ));
800 0 : slot_info->parent_bank_idx = parent_bank->data->idx;
801 0 : }
802 :
803 0 : slot_info->is_leader = is_leader;
804 :
805 0 : FD_BASE58_ENCODE_32_BYTES( ctx->block_id_arr[ bank->data->idx ].latest_mr.uc, block_id_cstr );
806 0 : FD_BASE58_ENCODE_32_BYTES( fd_bank_bank_hash_query( bank )->uc, bank_hash_cstr );
807 0 : FD_LOG_DEBUG(( "publish_slot_completed: bank_idx=%lu slot=%lu bank_hash=%s block_id=%s", bank->data->idx, slot, bank_hash_cstr, block_id_cstr ));
808 :
809 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() ) );
810 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 );
811 0 : }
812 :
813 : static void
814 : publish_slot_dead( fd_replay_tile_t * ctx,
815 : fd_stem_context_t * stem,
816 : ulong slot,
817 0 : fd_hash_t const * block_id ) {
818 0 : fd_replay_slot_dead_t * slot_dead = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
819 0 : slot_dead->slot = slot;
820 0 : slot_dead->block_id = *block_id;
821 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_SLOT_DEAD, ctx->replay_out->chunk, sizeof(fd_replay_slot_dead_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
822 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_slot_dead_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
823 0 : }
824 :
825 : static void
826 : publish_txn_executed( fd_replay_tile_t * ctx,
827 : fd_stem_context_t * stem,
828 0 : ulong txn_idx ) {
829 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
830 0 : fd_replay_txn_executed_t * txn_executed = fd_type_pun( fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk ) );
831 0 : *txn_executed->txn = *fd_sched_get_txn( ctx->sched, txn_idx );
832 0 : txn_executed->txn_err = txn_info->txn_err;
833 0 : txn_executed->is_committable = !!(txn_info->flags&FD_SCHED_TXN_IS_COMMITTABLE);
834 0 : txn_executed->is_fees_only = !!(txn_info->flags&FD_SCHED_TXN_IS_FEES_ONLY);
835 0 : txn_executed->tick_parsed = txn_info->tick_parsed;
836 0 : txn_executed->tick_sigverify_disp = txn_info->tick_sigverify_disp;
837 0 : txn_executed->tick_sigverify_done = txn_info->tick_sigverify_done;
838 0 : txn_executed->tick_exec_disp = txn_info->tick_exec_disp;
839 0 : txn_executed->tick_exec_done = txn_info->tick_exec_done;
840 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_TXN_EXECUTED, ctx->replay_out->chunk, sizeof(*txn_executed), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
841 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(*txn_executed), ctx->replay_out->chunk0, ctx->replay_out->wmark );
842 0 : }
843 :
844 : static void
845 : replay_block_finalize( fd_replay_tile_t * ctx,
846 : fd_stem_context_t * stem,
847 0 : fd_bank_t * bank ) {
848 0 : bank->data->last_transaction_finished_nanos = fd_log_wallclock();
849 :
850 0 : FD_TEST( !(bank->data->flags&FD_BANK_FLAGS_FROZEN) );
851 :
852 : /* Set poh hash in bank. */
853 0 : fd_hash_t * poh = fd_sched_get_poh( ctx->sched, bank->data->idx );
854 0 : fd_bank_poh_set( bank, *poh );
855 :
856 : /* Set shred count in bank. */
857 0 : fd_bank_shred_cnt_set( bank, fd_sched_get_shred_cnt( ctx->sched, bank->data->idx ) );
858 :
859 : /* Do hashing and other end-of-block processing. */
860 0 : fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx );
861 :
862 : /* Copy out cost tracker fields before freezing */
863 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
864 0 : cost_tracker_snap( bank, slot_info );
865 :
866 : /* fetch identity / vote balance updates infrequently */
867 0 : ulong slot = fd_bank_slot_get( bank );
868 0 : fd_funk_txn_xid_t xid = { .ul = { slot, bank->data->idx } };
869 0 : slot_info->identity_balance = FD_UNLIKELY( slot%4096==0UL ) ? get_identity_balance( ctx, xid ) : ULONG_MAX;
870 :
871 : /* Mark the bank as frozen. */
872 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
873 :
874 : /**********************************************************************/
875 : /* Bank hash comparison, and halt if there's a mismatch after replay */
876 : /**********************************************************************/
877 :
878 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank );
879 0 : FD_TEST( bank_hash );
880 :
881 : /* Must be last so we can measure completion time correctly, even
882 : though we could technically do this before the hash cmp and vote
883 : tower stuff. */
884 0 : publish_slot_completed( ctx, stem, bank, 0, 0 /* is_leader */ );
885 :
886 0 : # if FD_HAS_FLATCC
887 : /* If enabled, dump the block to a file and reset the dumping
888 : context state */
889 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
890 0 : fd_dump_block_to_protobuf( ctx->block_dump_ctx, ctx->banks, bank, ctx->accdb, ctx->dump_proto_ctx );
891 0 : fd_block_dump_context_reset( ctx->block_dump_ctx );
892 0 : }
893 0 : # endif
894 0 : }
895 :
896 : /**********************************************************************/
897 : /* Leader bank management */
898 : /**********************************************************************/
899 :
900 : static fd_bank_t *
901 : prepare_leader_bank( fd_replay_tile_t * ctx,
902 : ulong slot,
903 : long now,
904 : fd_hash_t const * parent_block_id,
905 0 : fd_stem_context_t * stem ) {
906 0 : long before = fd_log_wallclock();
907 :
908 : /* Make sure that we are not already leader. */
909 0 : FD_TEST( ctx->leader_bank->data==NULL );
910 :
911 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 );
912 0 : if( FD_UNLIKELY( !parent_ele ) ) {
913 0 : FD_BASE58_ENCODE_32_BYTES( parent_block_id->key, parent_block_id_b58 );
914 0 : FD_LOG_CRIT(( "invariant violation: parent bank index not found for merkle root %s", parent_block_id_b58 ));
915 0 : }
916 0 : ulong parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent_ele );
917 :
918 0 : fd_bank_t parent_bank[1];
919 0 : if( FD_UNLIKELY( !fd_banks_bank_query( parent_bank, ctx->banks, parent_bank_idx ) ) ) {
920 0 : FD_LOG_CRIT(( "invariant violation: parent bank not found for bank index %lu", parent_bank_idx ));
921 0 : }
922 0 : ulong parent_slot = fd_bank_slot_get( parent_bank );
923 :
924 0 : if( FD_UNLIKELY( !fd_banks_new_bank( ctx->leader_bank, ctx->banks, parent_bank_idx, now ) ) ) {
925 0 : FD_LOG_CRIT(( "invariant violation: leader bank is NULL for slot %lu", slot ));
926 0 : }
927 :
928 0 : if( FD_UNLIKELY( !fd_banks_clone_from_parent( ctx->leader_bank, ctx->banks, ctx->leader_bank->data->idx ) ) ) {
929 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for slot %lu", slot ));
930 0 : }
931 :
932 0 : ctx->leader_bank->data->preparation_begin_nanos = before;
933 :
934 0 : fd_bank_slot_set( ctx->leader_bank, slot );
935 0 : fd_bank_parent_slot_set( ctx->leader_bank, parent_slot );
936 0 : ctx->leader_bank->data->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, parent_bank->data->txncache_fork_id );
937 : /* prepare the funk transaction for the leader bank */
938 0 : fd_funk_txn_xid_t xid = { .ul = { slot, ctx->leader_bank->data->idx } };
939 0 : fd_funk_txn_xid_t parent_xid = { .ul = { parent_slot, parent_bank_idx } };
940 0 : fd_accdb_attach_child( ctx->accdb_admin, &parent_xid, &xid );
941 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, &parent_xid, &xid );
942 :
943 0 : fd_bank_execution_fees_set( ctx->leader_bank, 0UL );
944 0 : fd_bank_priority_fees_set( ctx->leader_bank, 0UL );
945 0 : fd_bank_shred_cnt_set( ctx->leader_bank, 0UL );
946 0 : fd_bank_tips_set( ctx->leader_bank, 0UL );
947 :
948 : /* Update block height. */
949 0 : fd_bank_block_height_set( ctx->leader_bank, fd_bank_block_height_get( ctx->leader_bank ) + 1UL );
950 :
951 0 : int is_epoch_boundary = 0;
952 0 : fd_runtime_block_execute_prepare( ctx->banks, ctx->leader_bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
953 0 : if( FD_UNLIKELY( is_epoch_boundary ) ) publish_epoch_info( ctx, stem, ctx->leader_bank, 1 );
954 :
955 0 : ulong max_tick_height;
956 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( fd_bank_ticks_per_slot_get( parent_bank ), slot, &max_tick_height ) ) ) {
957 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, fd_bank_ticks_per_slot_get( parent_bank ) ));
958 0 : }
959 0 : fd_bank_max_tick_height_set( ctx->leader_bank, max_tick_height );
960 0 : fd_bank_tick_height_set( ctx->leader_bank, fd_bank_max_tick_height_get( parent_bank ) ); /* The parent's max tick height is our starting tick height. */
961 :
962 : /* Now that a bank has been created for the leader slot, increment the
963 : reference count until we are done with the leader slot. */
964 0 : ctx->leader_bank->data->refcnt++;
965 :
966 0 : return ctx->leader_bank;
967 0 : }
968 :
969 : static void
970 : fini_leader_bank( fd_replay_tile_t * ctx,
971 0 : fd_stem_context_t * stem ) {
972 :
973 0 : FD_TEST( ctx->leader_bank->data!=NULL );
974 0 : FD_TEST( ctx->is_leader );
975 0 : FD_TEST( ctx->block_id_arr[ ctx->leader_bank->data->idx ].block_id_seen );
976 0 : FD_TEST( ctx->recv_poh );
977 :
978 0 : ctx->leader_bank->data->last_transaction_finished_nanos = fd_log_wallclock();
979 :
980 0 : ulong curr_slot = fd_bank_slot_get( ctx->leader_bank );
981 :
982 0 : fd_sched_block_add_done( ctx->sched, ctx->leader_bank->data->idx, ctx->leader_bank->data->parent_idx, curr_slot );
983 :
984 0 : fd_runtime_block_execute_finalize( ctx->leader_bank, ctx->accdb, ctx->capture_ctx );
985 :
986 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
987 0 : cost_tracker_snap( ctx->leader_bank, slot_info );
988 0 : fd_funk_txn_xid_t xid = { .ul = { curr_slot, ctx->leader_bank->data->idx } };
989 0 : slot_info->identity_balance = FD_UNLIKELY( curr_slot%4096==0UL ) ? get_identity_balance( ctx, xid ) : ULONG_MAX;
990 :
991 0 : fd_banks_mark_bank_frozen( ctx->banks, ctx->leader_bank );
992 :
993 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( ctx->leader_bank );
994 0 : FD_TEST( bank_hash );
995 :
996 0 : publish_slot_completed( ctx, stem, ctx->leader_bank, 0, 1 /* is_leader */ );
997 :
998 : /* The reference on the bank is finally no longer needed. */
999 0 : ctx->leader_bank->data->refcnt--;
1000 :
1001 : /* We are no longer leader so we can clear the bank index we use for
1002 : being the leader. */
1003 0 : ctx->leader_bank->data = NULL;
1004 0 : ctx->recv_poh = 0;
1005 0 : ctx->is_leader = 0;
1006 0 : }
1007 :
1008 : static void
1009 : publish_root_advanced( fd_replay_tile_t * ctx,
1010 0 : fd_stem_context_t * stem ) {
1011 :
1012 0 : fd_bank_t bank[1];
1013 0 : if( FD_UNLIKELY( !fd_banks_bank_query( bank, ctx->banks, ctx->consensus_root_bank_idx ) ) ) {
1014 0 : FD_LOG_CRIT(( "invariant violation: consensus root bank is NULL at bank index %lu", ctx->consensus_root_bank_idx ));
1015 0 : }
1016 :
1017 0 : if( ctx->rpc_enabled ) {
1018 0 : bank->data->refcnt++;
1019 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for gui", bank->data->idx, fd_bank_slot_get( bank ), bank->data->refcnt ));
1020 0 : }
1021 :
1022 : /* Increment the reference count on the consensus root bank to account
1023 : for the number of exec tiles that are waiting on it. */
1024 0 : bank->data->refcnt += ctx->resolv_tile_cnt;
1025 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for resolv", bank->data->idx, fd_bank_slot_get( bank ), bank->data->refcnt ));
1026 :
1027 0 : fd_replay_root_advanced_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1028 0 : msg->bank_idx = bank->data->idx;
1029 :
1030 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() ) );
1031 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 );
1032 0 : }
1033 :
1034 : /* init_funk performs pre-flight checks for the account database and
1035 : program cache. Ensures that the account database was set up
1036 : correctly by bootstrap components (e.g. genesis or snapshot loader).
1037 : Mirrors the account database's fork tree down to the program cache. */
1038 :
1039 : static void
1040 : init_funk( fd_replay_tile_t * ctx,
1041 0 : ulong bank_slot ) {
1042 : /* Ensure that the loaded bank root corresponds to the account
1043 : database's root. */
1044 0 : fd_funk_t * funk = fd_accdb_user_v1_funk( ctx->accdb );
1045 0 : if( FD_UNLIKELY( !funk->shmem ) ) {
1046 0 : FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to database shared memory objects" ));
1047 0 : }
1048 0 : fd_funk_txn_xid_t const * accdb_pub = fd_funk_last_publish( funk );
1049 0 : if( FD_UNLIKELY( accdb_pub->ul[0]!=bank_slot ) ) {
1050 0 : FD_LOG_CRIT(( "failed to initialize account database: accdb is at slot %lu, but chain state is at slot %lu\n"
1051 0 : "This is a bug in startup components.",
1052 0 : accdb_pub->ul[0], bank_slot ));
1053 0 : }
1054 0 : if( FD_UNLIKELY( fd_funk_last_publish_is_frozen( funk ) ) ) {
1055 0 : FD_LOG_CRIT(( "failed to initialize account database: accdb fork graph is not clean.\n"
1056 0 : "The account database should only contain state for the root slot at this point,\n"
1057 0 : "but there are incomplete database transactions leftover.\n"
1058 0 : "This is a bug in startup components." ));
1059 0 : }
1060 :
1061 : /* The program cache tracks the account database's fork graph at all
1062 : times. Perform initial synchronization: pivot from funk 'root' (a
1063 : sentinel XID) to 'last publish' (the bootstrap root slot). */
1064 0 : if( FD_UNLIKELY( !ctx->progcache_admin->funk->shmem ) ) {
1065 0 : FD_LOG_CRIT(( "failed to initialize account database: replay tile is not joined to program cache" ));
1066 0 : }
1067 0 : fd_progcache_clear( ctx->progcache_admin );
1068 :
1069 0 : fd_funk_txn_xid_t last_publish = fd_accdb_root_get( ctx->accdb_admin );
1070 0 : fd_progcache_txn_attach_child( ctx->progcache_admin, fd_funk_root( ctx->progcache_admin->funk ), &last_publish );
1071 0 : fd_progcache_txn_advance_root( ctx->progcache_admin, &last_publish );
1072 0 : }
1073 :
1074 : static void
1075 0 : init_after_snapshot( fd_replay_tile_t * ctx ) {
1076 : /* Now that the snapshot has been loaded in, we have to refresh the
1077 : stake delegations since the manifest does not contain the full set
1078 : of data required for the stake delegations. See
1079 : fd_stake_delegations.h for why this is required. */
1080 :
1081 0 : fd_bank_t bank[1];
1082 0 : if( FD_UNLIKELY( !fd_banks_bank_query( bank, ctx->banks, FD_REPLAY_BOOT_BANK_IDX ) ) ) {
1083 0 : FD_LOG_CRIT(( "invariant violation: replay bank is NULL at bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
1084 0 : }
1085 :
1086 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
1087 0 : init_funk( ctx, fd_bank_slot_get( bank ) );
1088 :
1089 0 : fd_stake_delegations_t * root_delegations = fd_banks_stake_delegations_root_query( ctx->banks );
1090 :
1091 0 : fd_stake_delegations_refresh( root_delegations, ctx->accdb, &xid );
1092 :
1093 : /* We want to clear out any stale vote states that may have been
1094 : included in the snapshot manifest. If an agave snapshot is created
1095 : shortly after a vote state is removed from the stakes cache (Agave
1096 : equivalent of vote states), then the vote state will be included in
1097 : the snapshot manifest. */
1098 :
1099 0 : fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
1100 0 : fd_vote_states_iter_t iter_[1];
1101 :
1102 0 : ulong stale_vote_acc_cnt = 0UL;
1103 0 : for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states );
1104 0 : !fd_vote_states_iter_done( iter );
1105 0 : fd_vote_states_iter_next( iter ) ) {
1106 0 : fd_vote_state_ele_t * vote_state = fd_vote_states_iter_ele( iter );
1107 0 : fd_accdb_ro_t ro[1];
1108 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( ctx->accdb, ro, &xid, &vote_state->vote_account ) ) ) {
1109 0 : ctx->runtime_stack.vote_accounts.stale_accs[ stale_vote_acc_cnt++ ] = vote_state->vote_account;
1110 0 : FD_BASE58_ENCODE_32_BYTES( vote_state->vote_account.uc, acc_cstr );
1111 0 : FD_LOG_DEBUG(( "vote account %s from manifest is stale", acc_cstr ));
1112 0 : } else {
1113 0 : fd_accdb_close_ro( ctx->accdb, ro );
1114 0 : }
1115 0 : }
1116 :
1117 0 : for( ulong i=0UL; i<stale_vote_acc_cnt; i++ ) {
1118 0 : fd_vote_states_remove( vote_states, &ctx->runtime_stack.vote_accounts.stale_accs[ i ] );
1119 0 : }
1120 0 : fd_bank_vote_states_end_locking_modify( bank );
1121 :
1122 : /* After both snapshots have been loaded in, we can determine if we should
1123 : start distributing rewards. */
1124 :
1125 0 : fd_rewards_recalculate_partitioned_rewards( ctx->banks, bank, ctx->accdb, &xid, &ctx->runtime_stack, ctx->capture_ctx );
1126 :
1127 0 : ulong snapshot_slot = fd_bank_slot_get( bank );
1128 0 : if( FD_UNLIKELY( !snapshot_slot ) ) {
1129 : /* Genesis-specific setup. */
1130 : /* FIXME: This branch does not set up a new block exec ctx
1131 : properly. Needs to do whatever prepare_new_block_execution
1132 : does, but just hacking that in breaks stuff. */
1133 0 : fd_runtime_update_leaders( bank, &ctx->runtime_stack );
1134 :
1135 0 : ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
1136 0 : fd_hash_t * poh = fd_bank_poh_modify( bank );
1137 0 : while( hashcnt_per_slot-- ) {
1138 0 : fd_sha256_hash( poh->hash, 32UL, poh->hash );
1139 0 : }
1140 :
1141 0 : int is_epoch_boundary = 0;
1142 0 : fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, &ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
1143 0 : FD_TEST( !is_epoch_boundary );
1144 0 : fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx );
1145 :
1146 0 : snapshot_slot = 0UL;
1147 0 : }
1148 :
1149 0 : }
1150 :
1151 : static inline int
1152 : maybe_become_leader( fd_replay_tile_t * ctx,
1153 0 : fd_stem_context_t * stem ) {
1154 0 : FD_TEST( ctx->is_booted );
1155 0 : if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX || ctx->is_leader || (!ctx->has_identity_vote_rooted && ctx->wait_for_vote_to_start_leader) || ctx->replay_out->idx==ULONG_MAX || !ctx->wfs_complete ) ) return 0;
1156 0 : if( !ctx->supports_leader ) return 0;
1157 :
1158 0 : FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
1159 0 : long now = fd_tickcount();
1160 0 : if( FD_LIKELY( now<ctx->next_leader_tickcount ) ) return 0;
1161 :
1162 : /* TODO:
1163 : if( FD_UNLIKELY( ctx->halted_switching_key ) ) return 0; */
1164 :
1165 : /* If a prior leader is still in the process of publishing their slot,
1166 : delay ours to let them finish ... unless they are so delayed that
1167 : we risk getting skipped by the leader following us. 1.2 seconds
1168 : is a reasonable default here, although any value between 0 and 1.6
1169 : seconds could be considered reasonable. This is arbitrary and
1170 : chosen due to intuition. */
1171 0 : if( FD_UNLIKELY( now<ctx->next_leader_tickcount+(long)(3.0*ctx->slot_duration_ticks) ) ) {
1172 0 : FD_TEST( ctx->reset_bank->data );
1173 :
1174 : /* TODO: Make the max_active_descendant calculation more efficient
1175 : by caching it in the bank structure and updating it as banks are
1176 : created and completed. */
1177 0 : ulong max_active_descendant = 0UL;
1178 0 : ulong child_idx = ctx->reset_bank->data->child_idx;
1179 0 : while( child_idx!=ULONG_MAX ) {
1180 0 : fd_bank_t child_bank[1];
1181 0 : fd_banks_bank_query( child_bank, ctx->banks, child_idx );
1182 0 : max_active_descendant = fd_ulong_max( max_active_descendant, fd_bank_slot_get( child_bank ) );
1183 0 : child_idx = child_bank->data->sibling_idx;
1184 0 : }
1185 :
1186 : /* If the max_active_descendant is >= next_leader_slot, we waited
1187 : too long and a leader after us started publishing to try and skip
1188 : us. Just start our leader slot immediately, we might win ... */
1189 0 : if( FD_LIKELY( max_active_descendant>=ctx->reset_slot && max_active_descendant<ctx->next_leader_slot ) ) {
1190 : /* If one of the leaders between the reset slot and our leader
1191 : slot is in the process of publishing (they have a descendant
1192 : bank that is in progress of being replayed), then keep waiting.
1193 : We probably wouldn't get a leader slot out before they
1194 : finished.
1195 :
1196 : Unless... we are past the deadline to start our slot by more
1197 : than 1.2 seconds, in which case we should probably start it to
1198 : avoid getting skipped by the leader behind us. */
1199 0 : return 0;
1200 0 : }
1201 0 : }
1202 :
1203 0 : long now_nanos = fd_log_wallclock();
1204 :
1205 0 : ctx->is_leader = 1;
1206 0 : ctx->recv_poh = 0;
1207 :
1208 0 : FD_TEST( ctx->highwater_leader_slot==ULONG_MAX || ctx->highwater_leader_slot<ctx->next_leader_slot );
1209 0 : ctx->highwater_leader_slot = ctx->next_leader_slot;
1210 :
1211 0 : FD_LOG_INFO(( "becoming leader for slot %lu, parent slot is %lu", ctx->next_leader_slot, ctx->reset_slot ));
1212 :
1213 : /* Acquires bank, sets up initial state, and refcnts it. */
1214 0 : fd_bank_t * bank = prepare_leader_bank( ctx, ctx->next_leader_slot, now_nanos, &ctx->reset_block_id, stem );
1215 0 : fd_funk_txn_xid_t xid = { .ul = { ctx->next_leader_slot, ctx->leader_bank->data->idx } };
1216 :
1217 0 : fd_bundle_crank_tip_payment_config_t config[1] = { 0 };
1218 0 : fd_pubkey_t tip_receiver_owner = {0};
1219 :
1220 0 : if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
1221 0 : fd_acct_addr_t tip_payment_config[1];
1222 0 : fd_acct_addr_t tip_receiver[1];
1223 0 : fd_bundle_crank_get_addresses( ctx->bundle.gen, fd_bank_epoch_get( bank ), tip_payment_config, tip_receiver );
1224 :
1225 0 : fd_accdb_ro_t tip_config_acc[1];
1226 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( ctx->accdb, tip_config_acc, &xid, tip_payment_config ) ) ) {
1227 : /* FIXME This should not crash the validator */
1228 0 : FD_BASE58_ENCODE_32_BYTES( tip_payment_config->b, tip_config_acc_b58 );
1229 0 : FD_LOG_CRIT(( "tip payment config account %s does not exist", tip_config_acc_b58 ));
1230 0 : }
1231 0 : ulong tip_cfg_sz = fd_accdb_ref_data_sz( tip_config_acc );
1232 0 : if( FD_UNLIKELY( tip_cfg_sz < sizeof(fd_bundle_crank_tip_payment_config_t) ) ) {
1233 : /* FIXME This should not crash the validator */
1234 0 : FD_LOG_HEXDUMP_CRIT(( "invalid tip payment config account data", fd_accdb_ref_data_const( tip_config_acc ), tip_cfg_sz ));
1235 0 : }
1236 0 : memcpy( config, fd_accdb_ref_data_const( tip_config_acc ), sizeof(fd_bundle_crank_tip_payment_config_t) );
1237 0 : fd_accdb_close_ro( ctx->accdb, tip_config_acc );
1238 :
1239 : /* It is possible that the tip receiver account does not exist yet
1240 : if it is the first time in an epoch. */
1241 0 : fd_accdb_ro_t tip_receiver_acc[1];
1242 0 : if( FD_LIKELY( fd_accdb_open_ro( ctx->accdb, tip_receiver_acc, &xid, tip_receiver ) ) ) {
1243 0 : tip_receiver_owner = FD_LOAD( fd_pubkey_t, fd_accdb_ref_owner( tip_receiver_acc ) );
1244 0 : fd_accdb_close_ro( ctx->accdb, tip_receiver_acc );
1245 0 : }
1246 0 : }
1247 :
1248 :
1249 0 : fd_became_leader_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1250 0 : msg->slot = ctx->next_leader_slot;
1251 0 : msg->slot_start_ns = now_nanos;
1252 0 : msg->slot_end_ns = now_nanos+(long)ctx->slot_duration_nanos;
1253 0 : msg->bank = NULL;
1254 0 : msg->bank_idx = bank->data->idx;
1255 0 : msg->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1256 0 : msg->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
1257 0 : msg->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)msg->ticks_per_slot);
1258 0 : msg->bundle->config[0] = config[0];
1259 0 : memcpy( msg->bundle->last_blockhash, fd_bank_poh_query( bank )->hash, sizeof(fd_hash_t) );
1260 0 : memcpy( msg->bundle->tip_receiver_owner, tip_receiver_owner.uc, sizeof(fd_pubkey_t) );
1261 :
1262 0 : if( FD_UNLIKELY( msg->hashcnt_per_tick==1UL ) ) {
1263 : /* Low power producer, maximum of one microblock per tick in the slot */
1264 0 : msg->max_microblocks_in_slot = msg->ticks_per_slot;
1265 0 : } else {
1266 : /* See the long comment in after_credit for this limit */
1267 0 : msg->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, msg->ticks_per_slot*(msg->hashcnt_per_tick-1UL) );
1268 0 : }
1269 :
1270 0 : msg->total_skipped_ticks = msg->ticks_per_slot*(ctx->next_leader_slot-ctx->reset_slot);
1271 0 : msg->epoch = fd_slot_to_epoch( fd_bank_epoch_schedule_query( bank ), ctx->next_leader_slot, NULL );
1272 :
1273 0 : fd_cost_tracker_t const * cost_tracker = fd_bank_cost_tracker_locking_query( bank );
1274 :
1275 0 : msg->limits.slot_max_cost = ctx->larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : cost_tracker->block_cost_limit;
1276 0 : msg->limits.slot_max_vote_cost = cost_tracker->vote_cost_limit;
1277 0 : msg->limits.slot_max_write_cost_per_acct = cost_tracker->account_cost_limit;
1278 :
1279 0 : fd_bank_cost_tracker_end_locking_query( bank );
1280 :
1281 0 : if( FD_UNLIKELY( msg->ticks_per_slot+msg->total_skipped_ticks>USHORT_MAX ) ) {
1282 : /* There can be at most USHORT_MAX skipped ticks, because the
1283 : parent_offset field in the shred data is only 2 bytes wide. */
1284 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 ));
1285 0 : }
1286 :
1287 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() ) );
1288 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 );
1289 :
1290 0 : ctx->next_leader_slot = ULONG_MAX;
1291 0 : ctx->next_leader_tickcount = LONG_MAX;
1292 :
1293 0 : return 1;
1294 0 : }
1295 :
1296 : static void
1297 : process_poh_message( fd_replay_tile_t * ctx,
1298 0 : fd_poh_leader_slot_ended_t const * slot_ended ) {
1299 :
1300 0 : FD_TEST( ctx->is_booted );
1301 0 : FD_TEST( ctx->is_leader );
1302 0 : FD_TEST( ctx->leader_bank->data!=NULL );
1303 :
1304 0 : FD_TEST( ctx->highwater_leader_slot>=slot_ended->slot );
1305 0 : FD_TEST( ctx->next_leader_slot>ctx->highwater_leader_slot );
1306 :
1307 : /* Update the poh hash in the bank. We will want to maintain a refcnt
1308 : on the bank until we have recieved the block id for the block after
1309 : it has been shredded. */
1310 :
1311 0 : memcpy( fd_bank_poh_modify( ctx->leader_bank ), slot_ended->blockhash, sizeof(fd_hash_t) );
1312 :
1313 0 : ctx->recv_poh = 1;
1314 0 : }
1315 :
1316 : static void
1317 : publish_reset( fd_replay_tile_t * ctx,
1318 : fd_stem_context_t * stem,
1319 0 : fd_bank_t * bank ) {
1320 0 : if( FD_UNLIKELY( ctx->replay_out->idx==ULONG_MAX ) ) return;
1321 :
1322 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
1323 0 : FD_TEST( block_hash );
1324 :
1325 0 : fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1326 :
1327 0 : reset->bank_idx = bank->data->idx;
1328 0 : reset->timestamp = fd_log_wallclock();
1329 0 : reset->completed_slot = fd_bank_slot_get( bank );
1330 0 : reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
1331 0 : reset->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1332 0 : reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
1333 0 : fd_memcpy( reset->completed_blockhash, block_hash->uc, sizeof(fd_hash_t) );
1334 :
1335 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
1336 0 : if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
1337 : /* Low power producer, maximum of one microblock per tick in the slot */
1338 0 : reset->max_microblocks_in_slot = ticks_per_slot;
1339 0 : } else {
1340 : /* See the long comment in after_credit for this limit */
1341 0 : reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
1342 0 : }
1343 0 : reset->next_leader_slot = ctx->next_leader_slot;
1344 :
1345 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() ) );
1346 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 );
1347 0 : }
1348 :
1349 : static void
1350 : store_xinsert( fd_store_t * store,
1351 0 : fd_hash_t const * merkle_root ) {
1352 0 : fd_store_pool_t pool = {
1353 0 : .pool = fd_wksp_laddr_fast( fd_store_wksp( store ), store->pool_mem_gaddr ),
1354 0 : .ele = fd_wksp_laddr_fast( fd_store_wksp( store ), store->pool_ele_gaddr ),
1355 0 : .ele_max = store->fec_max
1356 0 : };
1357 0 : int err; fd_store_fec_t * fec = fd_store_pool_acquire( &pool, NULL, 1 /* blocking */, &err );
1358 0 : if( FD_UNLIKELY( err!=FD_POOL_SUCCESS ) ) FD_LOG_CRIT(( "store pool: %s", fd_store_pool_strerror( err ) ));
1359 0 : fec->key.merkle_root = *merkle_root;
1360 0 : fec->key.part_idx = 0;
1361 0 : fec->cmr = (fd_hash_t){ 0 };
1362 0 : fec->next = fd_store_pool_idx_null();
1363 0 : fec->data_sz = 0UL;
1364 :
1365 0 : FD_STORE_XLOCK_BEGIN( store ) {
1366 0 : fd_store_map_ele_insert( fd_wksp_laddr_fast( fd_store_wksp( store ), store->map_gaddr ), fec, pool.ele );
1367 0 : } FD_STORE_XLOCK_END;
1368 0 : }
1369 :
1370 : static void
1371 : boot_genesis( fd_replay_tile_t * ctx,
1372 : fd_stem_context_t * stem,
1373 0 : fd_genesis_meta_t const * meta ) {
1374 : /* If we are bootstrapping, we can't wait to wait for our identity
1375 : vote to be rooted as this creates a circular dependency. */
1376 0 : ctx->has_identity_vote_rooted = 1;
1377 :
1378 0 : uchar const * genesis_blob = (uchar const *)( meta+1 );
1379 0 : FD_TEST( meta->bootstrap && meta->has_lthash );
1380 0 : FD_TEST( fd_genesis_parse( ctx->genesis, genesis_blob, meta->blob_sz ) );
1381 :
1382 0 : fd_bank_t bank[1];
1383 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, FD_REPLAY_BOOT_BANK_IDX ) );
1384 0 : fd_funk_txn_xid_t xid = { .ul = { 0UL, FD_REPLAY_BOOT_BANK_IDX } };
1385 :
1386 : /* Do genesis-related processing in a non-rooted transaction */
1387 0 : fd_funk_txn_xid_t root_xid = { .ul = { LONG_MAX, LONG_MAX } };
1388 0 : fd_funk_txn_xid_t target_xid = { .ul = { 0UL, 0UL } };
1389 0 : fd_accdb_attach_child( ctx->accdb_admin, &root_xid, &target_xid );
1390 0 : fd_runtime_read_genesis( ctx->banks, bank, ctx->accdb, &xid, NULL, &meta->genesis_hash, &meta->lthash, ctx->genesis, genesis_blob, &ctx->runtime_stack );
1391 0 : fd_accdb_advance_root( ctx->accdb_admin, &target_xid );
1392 :
1393 0 : static const fd_txncache_fork_id_t txncache_root = { .val = USHORT_MAX };
1394 0 : bank->data->txncache_fork_id = fd_txncache_attach_child( ctx->txncache, txncache_root );
1395 :
1396 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
1397 0 : fd_txncache_finalize_fork( ctx->txncache, bank->data->txncache_fork_id, 0UL, block_hash->uc );
1398 :
1399 0 : publish_epoch_info( ctx, stem, bank, 0 );
1400 0 : publish_epoch_info( ctx, stem, bank, 1 );
1401 :
1402 : /* We call this after fd_runtime_read_genesis, which sets up the
1403 : slot_bank needed in blockstore_init. */
1404 0 : init_after_snapshot( ctx );
1405 :
1406 0 : ctx->published_root_slot = 0UL;
1407 0 : fd_sched_block_add_done( ctx->sched, bank->data->idx, ULONG_MAX, 0UL );
1408 :
1409 0 : fd_bank_block_height_set( bank, 1UL );
1410 :
1411 0 : ctx->consensus_root = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
1412 0 : ctx->consensus_root_slot = 0UL;
1413 0 : ctx->consensus_root_bank_idx = 0UL;
1414 0 : ctx->published_root_slot = 0UL;
1415 0 : ctx->published_root_bank_idx = 0UL;
1416 :
1417 0 : ctx->reset_slot = 0UL;
1418 0 : fd_memcpy( ctx->reset_bank, bank, sizeof(fd_bank_t) );
1419 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
1420 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1421 0 : if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
1422 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1423 0 : } else {
1424 0 : ctx->next_leader_tickcount = LONG_MAX;
1425 0 : }
1426 :
1427 0 : ctx->is_booted = 1;
1428 0 : maybe_become_leader( ctx, stem );
1429 :
1430 0 : fd_hash_t initial_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
1431 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 */
1432 0 : fec->bank_idx = bank->data->idx;
1433 0 : fec->bank_seq = bank->data->bank_seq;
1434 0 : store_xinsert( ctx->store, &initial_block_id );
1435 :
1436 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
1437 0 : block_id_ele->latest_mr = initial_block_id;
1438 0 : block_id_ele->slot = 0UL;
1439 :
1440 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1441 :
1442 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1443 0 : slot_info->identity_balance = get_identity_balance( ctx, xid );
1444 :
1445 0 : publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */ );
1446 0 : publish_root_advanced( ctx, stem );
1447 0 : publish_reset( ctx, stem, bank );
1448 0 : }
1449 :
1450 : static inline void
1451 0 : maybe_verify_cluster_type( fd_replay_tile_t * ctx ) {
1452 0 : if( FD_UNLIKELY( !ctx->is_booted || !ctx->has_genesis_hash ) ) {
1453 0 : return;
1454 0 : }
1455 :
1456 0 : FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc, hash_cstr );
1457 0 : ulong cluster = fd_genesis_cluster_identify( hash_cstr );
1458 : /* Map pyth-related clusters to unkwown. */
1459 0 : switch( cluster ) {
1460 0 : case FD_CLUSTER_PYTHNET:
1461 0 : case FD_CLUSTER_PYTHTEST:
1462 0 : cluster = FD_CLUSTER_UNKNOWN;
1463 0 : }
1464 :
1465 0 : if( FD_UNLIKELY( cluster!=ctx->cluster_type ) ) {
1466 0 : FD_LOG_ERR(( "Your genesis.bin file at `%s` has a genesis hash of `%s` which means the cluster is %s "
1467 0 : "but the snapshot you loaded is for a different cluster %s. If you are trying to join the "
1468 0 : "%s cluster, you can delete the genesis.bin file and restart the node to download the correct "
1469 0 : "genesis file automatically.",
1470 0 : ctx->genesis_path,
1471 0 : hash_cstr,
1472 0 : fd_genesis_cluster_name( cluster ),
1473 0 : fd_genesis_cluster_name( ctx->cluster_type ),
1474 0 : fd_genesis_cluster_name( cluster ) ));
1475 0 : }
1476 0 : }
1477 :
1478 : static void
1479 : on_snapshot_message( fd_replay_tile_t * ctx,
1480 : fd_stem_context_t * stem,
1481 : ulong in_idx,
1482 : ulong chunk,
1483 0 : ulong sig ) {
1484 0 : ulong msg = fd_ssmsg_sig_message( sig );
1485 0 : if( FD_LIKELY( msg==FD_SSMSG_DONE ) ) {
1486 : /* An end of message notification indicates the snapshot is loaded.
1487 : Replay is able to start executing from this point onwards. */
1488 : /* TODO: replay should finish booting. Could make replay a
1489 : state machine and set the state here accordingly. */
1490 0 : ctx->is_booted = 1;
1491 :
1492 0 : fd_bank_t bank[1];
1493 0 : if( FD_UNLIKELY( !fd_banks_bank_query( bank, ctx->banks, FD_REPLAY_BOOT_BANK_IDX ) ) ) {
1494 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
1495 0 : }
1496 :
1497 0 : ulong snapshot_slot = fd_bank_slot_get( bank );
1498 :
1499 0 : if( FD_UNLIKELY( ctx->wfs_enabled && memcmp( ctx->expected_bank_hash.uc, fd_bank_bank_hash_get( bank ).uc, sizeof(fd_hash_t) ) ) ) {
1500 0 : FD_BASE58_ENCODE_32_BYTES( ctx->expected_bank_hash.uc, expected_bank_hash_cstr );
1501 0 : FD_BASE58_ENCODE_32_BYTES( fd_bank_bank_hash_get( bank ).uc, actual_bank_hash_cstr );
1502 0 : FD_LOG_ERR(( "[consensus.wait_for_supermajority_with_bank_hash] expected_bank_hash=%s does not match snapshot slot"
1503 0 : "=%lu bank_hash=%s. If you are loading a snapshot from the network, check that the slot matches the "
1504 0 : "cluster restart slot. ", expected_bank_hash_cstr, snapshot_slot, actual_bank_hash_cstr ));
1505 0 : }
1506 0 : if( FD_UNLIKELY( ctx->wfs_enabled ) ) {
1507 0 : FD_LOG_NOTICE(( "waiting for supermajority at snapshot slot %lu", snapshot_slot ));
1508 0 : }
1509 :
1510 : /* FIXME: This is a hack because the block id of the snapshot slot
1511 : is not provided in the snapshot. A possible solution is to get
1512 : the block id of the snapshot slot from repair. */
1513 0 : fd_hash_t manifest_block_id = { .ul = { FD_RUNTIME_INITIAL_BLOCK_ID } };
1514 :
1515 0 : fd_funk_txn_xid_t xid = { .ul = { snapshot_slot, FD_REPLAY_BOOT_BANK_IDX } };
1516 0 : fd_features_restore( bank, ctx->accdb, &xid );
1517 :
1518 : /* Typically, when we cross an epoch boundary during normal
1519 : operation, we publish the stake weights for the new epoch. But
1520 : since we are starting from a snapshot, we need to publish two
1521 : epochs worth of stake weights: the previous epoch (which is
1522 : needed for voting on the current epoch), and the current epoch
1523 : (which is needed for voting on the next epoch). */
1524 0 : publish_epoch_info( ctx, stem, bank, 0 );
1525 0 : publish_epoch_info( ctx, stem, bank, 1 );
1526 :
1527 0 : ctx->consensus_root = manifest_block_id;
1528 0 : ctx->consensus_root_slot = snapshot_slot;
1529 0 : ctx->consensus_root_bank_idx = 0UL;
1530 0 : ctx->published_root_slot = ctx->consensus_root_slot;
1531 0 : ctx->published_root_bank_idx = 0UL;
1532 :
1533 0 : ctx->reset_slot = snapshot_slot;
1534 0 : fd_memcpy( ctx->reset_bank, bank, sizeof(fd_bank_t) );
1535 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
1536 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1537 0 : if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
1538 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1539 0 : } else {
1540 0 : ctx->next_leader_tickcount = LONG_MAX;
1541 0 : }
1542 :
1543 0 : fd_sched_block_add_done( ctx->sched, bank->data->idx, ULONG_MAX, snapshot_slot );
1544 0 : FD_TEST( bank->data->idx==0UL );
1545 :
1546 0 : fd_runtime_update_leaders( bank, &ctx->runtime_stack );
1547 :
1548 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
1549 0 : block_id_ele->latest_mr = manifest_block_id;
1550 0 : block_id_ele->slot = snapshot_slot;
1551 0 : block_id_ele->block_id_seen = 1;
1552 0 : block_id_ele->latest_fec_idx = 0U;
1553 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1554 :
1555 : /* We call this after fd_runtime_read_genesis, which sets up the
1556 : slot_bank needed in blockstore_init. */
1557 0 : init_after_snapshot( ctx );
1558 :
1559 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1560 0 : slot_info->identity_balance = get_identity_balance( ctx, xid );
1561 :
1562 0 : publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */ );
1563 0 : publish_root_advanced( ctx, stem );
1564 :
1565 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 */
1566 0 : fec->bank_idx = bank->data->idx;
1567 0 : fec->bank_seq = bank->data->bank_seq;
1568 0 : store_xinsert( ctx->store, &manifest_block_id );
1569 :
1570 0 : ctx->cluster_type = fd_bank_cluster_type_get( bank );
1571 :
1572 0 : maybe_verify_cluster_type( ctx );
1573 :
1574 0 : return;
1575 0 : }
1576 :
1577 0 : switch( msg ) {
1578 0 : case FD_SSMSG_MANIFEST_FULL:
1579 0 : case FD_SSMSG_MANIFEST_INCREMENTAL: {
1580 : /* We may either receive a full snapshot manifest or an
1581 : incremental snapshot manifest. Note that this external message
1582 : id is only used temporarily because replay cannot yet receive
1583 : the firedancer-internal snapshot manifest message. */
1584 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
1585 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 ));
1586 :
1587 0 : fd_bank_t bank[1];
1588 0 : fd_ssload_recover( fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ),
1589 0 : ctx->banks,
1590 0 : fd_banks_bank_query( bank, ctx->banks, FD_REPLAY_BOOT_BANK_IDX ),
1591 0 : ctx->runtime_stack.stakes.vote_credits,
1592 0 : msg==FD_SSMSG_MANIFEST_INCREMENTAL );
1593 :
1594 0 : fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
1595 0 : ctx->hard_forks_cnt = manifest->hard_forks_len;
1596 0 : for( ulong i=0UL; i<manifest->hard_forks_len; i++ ) {
1597 0 : ctx->hard_forks[ i ] = manifest->hard_forks[ i ];
1598 0 : ctx->hard_forks_cnts[ i ] = manifest->hard_forks_cnts[ i ];
1599 0 : }
1600 0 : break;
1601 0 : }
1602 0 : default: {
1603 0 : FD_LOG_ERR(( "Received unknown snapshot message with msg %lu", msg ));
1604 0 : return;
1605 0 : }
1606 0 : }
1607 :
1608 0 : return;
1609 0 : }
1610 :
1611 : static void
1612 : dispatch_task( fd_replay_tile_t * ctx,
1613 : fd_stem_context_t * stem,
1614 0 : fd_sched_task_t * task ) {
1615 :
1616 0 : switch( task->task_type ) {
1617 0 : case FD_SCHED_TT_TXN_EXEC: {
1618 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_exec->txn_idx );
1619 :
1620 0 : fd_bank_t bank[1];
1621 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, task->txn_exec->bank_idx ) );
1622 :
1623 0 : # if FD_HAS_FLATCC
1624 : /* Add the transaction to the block dumper if necessary. This
1625 : logic doesn't need to be fork-aware since it's only meant to
1626 : be used in backtest. */
1627 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
1628 0 : fd_dump_block_to_protobuf_collect_tx( ctx->block_dump_ctx, txn_p );
1629 0 : }
1630 0 : # endif
1631 :
1632 0 : bank->data->refcnt++;
1633 :
1634 0 : if( FD_UNLIKELY( !bank->data->first_transaction_scheduled_nanos ) ) bank->data->first_transaction_scheduled_nanos = fd_log_wallclock();
1635 :
1636 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
1637 0 : fd_execrp_txn_exec_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1638 0 : memcpy( exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
1639 0 : exec_msg->bank_idx = task->txn_exec->bank_idx;
1640 0 : exec_msg->txn_idx = task->txn_exec->txn_idx;
1641 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) {
1642 0 : exec_msg->capture_txn_idx = ctx->capture_ctx->current_txn_idx++;
1643 0 : }
1644 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_EXEC<<32) | task->txn_exec->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1645 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
1646 0 : break;
1647 0 : }
1648 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
1649 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_sigverify->txn_idx );
1650 :
1651 0 : fd_bank_t bank[1];
1652 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, task->txn_sigverify->bank_idx ) );
1653 0 : bank->data->refcnt++;
1654 :
1655 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
1656 0 : fd_execrp_txn_sigverify_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1657 0 : memcpy( exec_msg->txn, txn_p, sizeof(fd_txn_p_t) );
1658 0 : exec_msg->bank_idx = task->txn_sigverify->bank_idx;
1659 0 : exec_msg->txn_idx = task->txn_sigverify->txn_idx;
1660 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_SIGVERIFY<<32) | task->txn_sigverify->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
1661 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
1662 0 : break;
1663 0 : };
1664 0 : case FD_SCHED_TT_POH_HASH: {
1665 0 : fd_bank_t bank[ 1 ];
1666 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, task->poh_hash->bank_idx ) );
1667 0 : bank->data->refcnt++;
1668 :
1669 0 : fd_replay_out_link_t * exec_out = ctx->exec_out;
1670 0 : fd_execrp_poh_hash_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1671 0 : exec_msg->bank_idx = task->poh_hash->bank_idx;
1672 0 : exec_msg->mblk_idx = task->poh_hash->mblk_idx;
1673 0 : exec_msg->hashcnt = task->poh_hash->hashcnt;
1674 0 : memcpy( exec_msg->hash, task->poh_hash->hash, sizeof(fd_hash_t) );
1675 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_POH_HASH<<32) | task->poh_hash->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
1676 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
1677 0 : break;
1678 0 : };
1679 0 : default: {
1680 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
1681 0 : }
1682 0 : }
1683 0 : }
1684 :
1685 : static void
1686 : mark_bank_dead( fd_replay_tile_t * ctx,
1687 : fd_stem_context_t * stem,
1688 0 : ulong bank_idx ) {
1689 0 : fd_bank_t bank[1];
1690 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, bank_idx ) );
1691 0 : fd_banks_mark_bank_dead( ctx->banks, bank_idx );
1692 :
1693 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank_idx ];
1694 0 : if( block_id_ele->block_id_seen ) publish_slot_dead( ctx, stem, block_id_ele->slot, &block_id_ele->latest_mr );
1695 :
1696 0 : fd_reasm_fec_t * fec = fd_reasm_query( ctx->reasm, &block_id_ele->latest_mr );
1697 0 : if( FD_UNLIKELY( !fec ) ) return;
1698 0 : fec->bank_dead = 1;
1699 :
1700 0 : }
1701 :
1702 : /* Returns 1 if charge_busy. */
1703 : static int
1704 : replay( fd_replay_tile_t * ctx,
1705 0 : fd_stem_context_t * stem ) {
1706 :
1707 0 : if( FD_UNLIKELY( !ctx->is_booted ) ) return 0;
1708 :
1709 0 : int charge_busy = 0;
1710 0 : fd_sched_task_t task[ 1 ];
1711 0 : if( FD_UNLIKELY( !fd_sched_task_next_ready( ctx->sched, task ) ) ) {
1712 0 : return charge_busy; /* Nothing to execute or do. */
1713 0 : }
1714 :
1715 0 : charge_busy = 1;
1716 :
1717 0 : switch( task->task_type ) {
1718 0 : case FD_SCHED_TT_BLOCK_START: {
1719 0 : replay_block_start( ctx, stem, task->block_start->bank_idx, task->block_start->parent_bank_idx, task->block_start->slot );
1720 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_START, ULONG_MAX, ULONG_MAX, NULL );
1721 0 : break;
1722 0 : }
1723 0 : case FD_SCHED_TT_BLOCK_END: {
1724 0 : fd_bank_t bank[1];
1725 0 : fd_banks_bank_query( bank, ctx->banks, task->block_end->bank_idx );
1726 0 : if( FD_LIKELY( !(bank->data->flags&FD_BANK_FLAGS_DEAD) ) ) replay_block_finalize( ctx, stem, bank );
1727 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_END, ULONG_MAX, ULONG_MAX, NULL );
1728 0 : break;
1729 0 : }
1730 0 : case FD_SCHED_TT_TXN_EXEC:
1731 0 : case FD_SCHED_TT_TXN_SIGVERIFY:
1732 0 : case FD_SCHED_TT_POH_HASH: {
1733 : /* Common case: we have a transaction we need to execute. */
1734 0 : dispatch_task( ctx, stem, task );
1735 0 : break;
1736 0 : }
1737 0 : case FD_SCHED_TT_MARK_DEAD: {
1738 0 : fd_bank_t bank[ 1 ];
1739 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, task->mark_dead->bank_idx ) );
1740 0 : mark_bank_dead( ctx, stem, task->mark_dead->bank_idx );
1741 0 : break;
1742 0 : }
1743 0 : default: {
1744 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
1745 0 : }
1746 0 : }
1747 :
1748 0 : return charge_busy;
1749 0 : }
1750 :
1751 : static int
1752 : can_process_fec( fd_replay_tile_t * ctx,
1753 0 : int * evict_banks_out ) {
1754 0 : fd_reasm_fec_t * fec;
1755 0 : if( FD_UNLIKELY( fd_sched_can_ingest_cnt( ctx->sched )==0UL ) ) {
1756 0 : ctx->metrics.sched_full++;
1757 0 : return 0;
1758 0 : }
1759 :
1760 0 : if( FD_UNLIKELY( (fec = fd_reasm_peek( ctx->reasm ))==NULL ) ) {
1761 0 : ctx->metrics.reasm_empty++;
1762 0 : return 0;
1763 0 : }
1764 :
1765 0 : ctx->metrics.reasm_latest_slot = fec->slot;
1766 0 : ctx->metrics.reasm_latest_fec_idx = fec->fec_set_idx;
1767 :
1768 0 : if( FD_UNLIKELY( ctx->is_leader && fec->fec_set_idx==0U && fd_reasm_parent( ctx->reasm, fec )->bank_idx==ctx->leader_bank->data->idx ) ) {
1769 : /* This guards against a rare race where we receive the FEC set for
1770 : the slot right after our leader rotation before we freeze the
1771 : bank for the last slot in our leader rotation. Leader slot
1772 : freezing happens only after if we've received the final PoH hash
1773 : from the poh tile as well as the final FEC set for the leader
1774 : slot. So the race happens when FEC sets are delivered and
1775 : processed sooner than the PoH hash, aka when the
1776 : poh=>shred=>replay path for the block id beats the poh=>replay
1777 : path for the poh hash. To mitigate this race, we must block on
1778 : ingesting the FEC set for the ensuing slot before the leader
1779 : bank freezes, because that would violate ordering invariants in
1780 : banks and sched. */
1781 0 : FD_TEST( ctx->block_id_arr[ ctx->leader_bank->data->idx ].block_id_seen );
1782 0 : FD_TEST( !ctx->recv_poh );
1783 0 : ctx->metrics.leader_bid_wait++;
1784 0 : return 0;
1785 0 : }
1786 :
1787 : /* If fec_set_idx is 0, we need a new bank for a new slot. Banks must
1788 : not be full in this case. */
1789 0 : if( FD_UNLIKELY( fd_banks_is_full( ctx->banks ) && fec->fec_set_idx==0 ) ) {
1790 0 : ctx->metrics.banks_full++;
1791 : /* We only want to evict banks if sched is drained and banks is no
1792 : longer making progress. */
1793 0 : if( FD_UNLIKELY( fd_sched_is_drained( ctx->sched ) ) ) *evict_banks_out = 1;
1794 0 : return 0;
1795 0 : }
1796 :
1797 : /* Otherwise, banks may not be full, so we can always create a new
1798 : bank if needed. Or, if banks are full, the current fec set's
1799 : ancestor (idx 0) already created a bank for this slot.*/
1800 0 : return 1;
1801 0 : }
1802 :
1803 : static void
1804 : insert_fec_set( fd_replay_tile_t * ctx,
1805 : fd_stem_context_t * stem,
1806 0 : fd_reasm_fec_t * reasm_fec ) {
1807 :
1808 0 : long now = fd_log_wallclock();
1809 :
1810 0 : reasm_fec->parent_bank_idx = fd_reasm_parent( ctx->reasm, reasm_fec )->bank_idx;
1811 :
1812 0 : fd_bank_t parent_bank[1];
1813 0 : FD_TEST( fd_banks_bank_query( parent_bank, ctx->banks, reasm_fec->parent_bank_idx ) );
1814 0 : reasm_fec->parent_bank_seq = parent_bank->data->bank_seq;
1815 :
1816 0 : if( FD_UNLIKELY( reasm_fec->fec_set_idx==0U ) ) {
1817 : /* If the first FEC set for a slot is observed, provision a new bank
1818 : if you are not the leader. Remove any stale block id map entry
1819 : and update the block id entry. */
1820 0 : fd_bank_t bank_[1];
1821 0 : fd_bank_t * bank = NULL;
1822 0 : if( FD_UNLIKELY( reasm_fec->is_leader ) ) {
1823 0 : bank = ctx->leader_bank;
1824 0 : } else {
1825 0 : bank = fd_banks_new_bank( bank_, ctx->banks, reasm_fec->parent_bank_idx, now );
1826 0 : }
1827 :
1828 0 : reasm_fec->bank_idx = bank->data->idx;
1829 0 : reasm_fec->bank_seq = bank->data->bank_seq;
1830 :
1831 : /* At this point remove any stale entry in the block id map if it
1832 : exists and set the block id as not having been seen yet. This is
1833 : safe because we know that the old entry for this bank index has
1834 : already been pruned away. */
1835 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ reasm_fec->bank_idx ];
1836 0 : if( FD_LIKELY( fd_block_id_map_ele_query( ctx->block_id_map, &block_id_ele->latest_mr, NULL, ctx->block_id_arr )==block_id_ele ) ) {
1837 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->latest_mr, NULL, ctx->block_id_arr ) );
1838 0 : }
1839 0 : block_id_ele->block_id_seen = 0;
1840 0 : block_id_ele->slot = reasm_fec->slot;
1841 0 : block_id_ele->latest_fec_idx = 0U;
1842 0 : block_id_ele->latest_mr = reasm_fec->key;
1843 0 : } else {
1844 : /* We are continuing to execute through a slot that we already have
1845 : a bank index for. */
1846 0 : reasm_fec->bank_idx = reasm_fec->parent_bank_idx;
1847 0 : reasm_fec->bank_seq = reasm_fec->parent_bank_seq;
1848 :
1849 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ reasm_fec->bank_idx ];
1850 0 : if( FD_UNLIKELY( block_id_ele->latest_fec_idx>reasm_fec->fec_set_idx ) ) {
1851 0 : FD_LOG_WARNING(( "dropping FEC set (slot=%lu, fec_set_idx=%u) because it is at least as old as the latest FEC set (slot=%lu, fec_set_idx=%u)", reasm_fec->slot, reasm_fec->fec_set_idx, block_id_ele->slot, block_id_ele->latest_fec_idx ));
1852 0 : return;
1853 0 : }
1854 0 : block_id_ele->latest_fec_idx = reasm_fec->fec_set_idx;
1855 0 : block_id_ele->latest_mr = reasm_fec->key;
1856 0 : }
1857 :
1858 0 : if( FD_UNLIKELY( reasm_fec->slot_complete ) ) {
1859 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ reasm_fec->bank_idx ];
1860 0 : block_id_ele->block_id_seen = 1;
1861 0 : block_id_ele->latest_mr = reasm_fec->key;
1862 0 : block_id_ele->latest_fec_idx = reasm_fec->fec_set_idx;
1863 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
1864 0 : }
1865 :
1866 : /* If we are the leader, we don't need to process the FEC set. */
1867 0 : if( FD_UNLIKELY( reasm_fec->is_leader ) ) return;
1868 :
1869 : /* Forks form a partial ordering over FEC sets. The Repair tile
1870 : delivers FEC sets in-order per fork, but FEC set ordering across
1871 : forks is arbitrary */
1872 0 : fd_sched_fec_t sched_fec[ 1 ];
1873 :
1874 : # if DEBUG_LOGGING
1875 : FD_BASE58_ENCODE_32_BYTES( reasm_fec->key.key, key_b58 );
1876 : FD_BASE58_ENCODE_32_BYTES( reasm_fec->cmr.key, cmr_b58 );
1877 : 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, key_b58, cmr_b58 ));
1878 : # endif
1879 :
1880 0 : sched_fec->shred_cnt = reasm_fec->data_cnt;
1881 0 : sched_fec->is_last_in_batch = !!reasm_fec->data_complete;
1882 0 : sched_fec->is_last_in_block = !!reasm_fec->slot_complete;
1883 0 : sched_fec->bank_idx = reasm_fec->bank_idx;
1884 0 : sched_fec->parent_bank_idx = reasm_fec->parent_bank_idx;
1885 0 : sched_fec->slot = reasm_fec->slot;
1886 0 : sched_fec->parent_slot = reasm_fec->slot - reasm_fec->parent_off;
1887 0 : sched_fec->is_first_in_block = reasm_fec->fec_set_idx==0U;
1888 0 : fd_funk_txn_xid_t const root = fd_accdb_root_get( ctx->accdb_admin );
1889 0 : fd_funk_txn_xid_copy( sched_fec->alut_ctx->xid, &root );
1890 0 : sched_fec->alut_ctx->accdb[0] = ctx->accdb[0];
1891 0 : sched_fec->alut_ctx->els = ctx->published_root_slot;
1892 :
1893 0 : fd_bank_t bank[1];
1894 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, sched_fec->bank_idx ) );
1895 0 : if( sched_fec->is_first_in_block ) {
1896 0 : bank->data->refcnt++;
1897 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for sched", bank->data->idx, sched_fec->slot, bank->data->refcnt ));
1898 0 : }
1899 :
1900 : /* Read FEC set from the store. This should happen before we try to
1901 : ingest the FEC set. This allows us to filter out frags that were
1902 : in-flight when we published away minority forks that the frags land
1903 : on. These frags would have no bank to execute against, because
1904 : their corresponding banks, or parent banks, have also been pruned
1905 : during publishing. A query against store will rightfully tell us
1906 : that the underlying data is not found, implying that this is for a
1907 : minority fork that we can safeljy ignore. */
1908 :
1909 0 : ulong wait = (ulong)fd_log_wallclock();
1910 0 : ulong work = wait;
1911 0 : FD_STORE_SLOCK_BEGIN( ctx->store ) {
1912 0 : ctx->metrics.store_query_acquire++;
1913 0 : work = (ulong)fd_log_wallclock();
1914 0 : fd_histf_sample( ctx->metrics.store_query_wait, work - wait );
1915 :
1916 0 : fd_store_fec_t * store_fec = fd_store_query( ctx->store, &reasm_fec->key );
1917 0 : ctx->metrics.store_query_cnt++;
1918 0 : if( FD_UNLIKELY( !store_fec ) ) {
1919 :
1920 : /* The only case in which a FEC is not found in the store after
1921 : repair has notified is if the FEC was on a minority fork that
1922 : has already been published away. In this case we abandon the
1923 : entire slice because it is no longer relevant. */
1924 :
1925 0 : ctx->metrics.store_query_missing_cnt++;
1926 0 : ctx->metrics.store_query_missing_mr = reasm_fec->key.ul[0];
1927 0 : FD_BASE58_ENCODE_32_BYTES( reasm_fec->key.key, key_b58 );
1928 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, key_b58 ));
1929 0 : return;
1930 0 : }
1931 0 : sched_fec->fec = store_fec;
1932 0 : if( FD_UNLIKELY( !fd_sched_fec_ingest( ctx->sched, sched_fec ) ) ) { /* FIXME this critical section is unnecessarily complex. should refactor to just be held for the memcpy and block_offs. */
1933 0 : mark_bank_dead( ctx, stem, sched_fec->bank_idx );
1934 0 : }
1935 0 : } FD_STORE_SLOCK_END;
1936 :
1937 0 : ctx->metrics.store_query_release++;
1938 0 : fd_histf_sample( ctx->metrics.store_query_work, (ulong)fd_log_wallclock() - work );
1939 0 : }
1940 :
1941 : static void
1942 : process_fec_set( fd_replay_tile_t * ctx,
1943 : fd_stem_context_t * stem,
1944 0 : fd_reasm_fec_t * reasm_fec ) {
1945 :
1946 0 : fd_reasm_fec_t * parent = fd_reasm_parent( ctx->reasm, reasm_fec );
1947 0 : if( FD_UNLIKELY( !parent ) ) {
1948 0 : FD_LOG_WARNING(( "dropping FEC set (slot=%lu, fec_set_idx=%u) because it is unconnected in reasm", reasm_fec->slot, reasm_fec->fec_set_idx ));
1949 0 : return;
1950 0 : }
1951 :
1952 0 : if( FD_UNLIKELY( parent->bank_dead ) ) {
1953 : /* Inherit the dead flag from the parent. If a dead slot is
1954 : completed, we publish the slot as dead. Don't insert FECs for
1955 : dead slots. */
1956 0 : reasm_fec->bank_dead = 1;
1957 0 : if( FD_UNLIKELY( reasm_fec->slot_complete ) ) publish_slot_dead( ctx, stem, reasm_fec->slot, &reasm_fec->key );
1958 0 : FD_LOG_DEBUG(( "dropping FEC set (slot=%lu, fec_set_idx=%u) because parent bank is marked dead", reasm_fec->slot, reasm_fec->fec_set_idx ));
1959 0 : return;
1960 0 : }
1961 :
1962 : /* Standard case, the parent FEC has a valid corresponding bank. */
1963 0 : fd_bank_t parent_fec_bank[1];
1964 0 : if( FD_LIKELY( fd_banks_bank_query( parent_fec_bank, ctx->banks, parent->bank_idx ) &&
1965 0 : parent_fec_bank->data->bank_seq==parent->bank_seq ) ) {
1966 0 : insert_fec_set( ctx, stem, reasm_fec );
1967 0 : return;
1968 0 : }
1969 :
1970 : /* In the case the FEC doesn't directly connect, iterate up the reasm
1971 : tree to find the closest valid slot complete that corresponds to a
1972 : valid bank. */
1973 :
1974 : /* First keep track of all of the slot completes up to and including
1975 : the fec we want to insert off of. */
1976 0 : fd_reasm_fec_t * path[ FD_BANKS_MAX_BANKS ];
1977 0 : ulong path_cnt = 0UL;
1978 0 : path[ path_cnt++ ] = reasm_fec;
1979 :
1980 0 : for( fd_reasm_fec_t * curr = reasm_fec;; ) {
1981 0 : curr = fd_reasm_parent( ctx->reasm, curr );
1982 0 : if( FD_UNLIKELY( !curr ) ) return; /* If can't connect, drop the FEC. */
1983 0 : if( FD_LIKELY( !curr->slot_complete ) ) continue;
1984 :
1985 0 : FD_TEST( path_cnt<=FD_BANKS_MAX_BANKS );
1986 0 : path[ path_cnt++ ] = curr;
1987 :
1988 0 : fd_bank_t curr_bank[1];
1989 0 : if( FD_LIKELY( fd_banks_bank_query( curr_bank, ctx->banks, curr->bank_idx ) && curr_bank->data->bank_seq==curr->bank_seq ) ) break;
1990 0 : }
1991 :
1992 0 : for( ulong i=path_cnt; i>0UL; i-- ) {
1993 0 : fd_reasm_fec_t * slot_fecs[ FD_SHRED_BLK_MAX/32 ]; /* TODO: replace with fix-32 macro. */
1994 0 : fd_reasm_fec_t * leaf = path[ i-1 ];
1995 :
1996 : /* If there's not capacity in the sched or banks, return early and
1997 : drop the FEC. We have inserted as much as we can for now. */
1998 0 : if( FD_UNLIKELY( fd_sched_can_ingest_cnt( ctx->sched )<leaf->fec_set_idx ) ) return;
1999 0 : if( FD_UNLIKELY( fd_banks_is_full( ctx->banks ) ) ) return;
2000 :
2001 : /* Gather all FECs for this slot; */
2002 0 : fd_reasm_fec_t * curr = leaf;
2003 0 : for(;;) {
2004 0 : slot_fecs[ curr->fec_set_idx ] = curr;
2005 0 : if( curr->fec_set_idx==0U ) break;
2006 0 : curr = fd_reasm_parent( ctx->reasm, curr );
2007 0 : FD_TEST( curr );
2008 0 : }
2009 :
2010 0 : for( ulong j=0UL; j<=leaf->fec_set_idx; j++ ) {
2011 0 : insert_fec_set( ctx, stem, slot_fecs[ j ] );
2012 0 : }
2013 0 : }
2014 0 : }
2015 :
2016 : /* accdb_advance_root moves account records from the unrooted to the
2017 : rooted database. */
2018 :
2019 : static inline ulong
2020 0 : accdb_root_op_total( fd_replay_tile_t const * ctx ) {
2021 0 : return ctx->accdb_admin->base.root_cnt +
2022 0 : ctx->accdb_admin->base.reclaim_cnt;
2023 0 : }
2024 :
2025 : static void
2026 : accdb_advance_root( fd_replay_tile_t * ctx,
2027 : ulong slot,
2028 0 : ulong bank_idx ) {
2029 0 : fd_funk_txn_xid_t xid = { .ul[0] = slot, .ul[1] = bank_idx };
2030 0 : FD_LOG_DEBUG(( "advancing root to slot=%lu", slot ));
2031 :
2032 0 : long rooted_accounts = -(long)accdb_root_op_total( ctx );
2033 0 : long root_accounts_dt = -fd_tickcount();
2034 0 : fd_accdb_advance_root( ctx->accdb_admin, &xid );
2035 0 : rooted_accounts += (long)accdb_root_op_total( ctx );
2036 0 : root_accounts_dt += fd_tickcount();
2037 0 : fd_histf_sample( ctx->metrics.root_slot_dur, (ulong)root_accounts_dt );
2038 0 : fd_histf_sample( ctx->metrics.root_account_dur, (ulong)root_accounts_dt / (ulong)fd_long_max( rooted_accounts, 1L ) );
2039 :
2040 0 : fd_progcache_txn_advance_root( ctx->progcache_admin, &xid );
2041 0 : }
2042 :
2043 : static int
2044 0 : advance_published_root( fd_replay_tile_t * ctx ) {
2045 :
2046 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 );
2047 0 : if( FD_UNLIKELY( !block_id_ele ) ) {
2048 0 : FD_BASE58_ENCODE_32_BYTES( ctx->consensus_root.key, consensus_root_b58 );
2049 0 : FD_LOG_CRIT(( "invariant violation: block id ele not found for consensus root %s", consensus_root_b58 ));
2050 0 : }
2051 0 : ulong target_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
2052 :
2053 : /* If the identity vote has been seen on a bank that should be rooted,
2054 : then we are now ready to produce blocks. */
2055 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
2056 0 : fd_bank_t root_bank[1];
2057 0 : if( FD_UNLIKELY( !fd_banks_bank_query( root_bank, ctx->banks, target_bank_idx ) ) ) FD_LOG_CRIT(( "invariant violation: root bank not found for bank index %lu", target_bank_idx ));
2058 0 : if( FD_LIKELY( fd_bank_has_identity_vote_get( root_bank ) ) ) ctx->has_identity_vote_rooted = 1;
2059 0 : }
2060 :
2061 0 : ulong advanceable_root_idx = ULONG_MAX;
2062 0 : if( FD_UNLIKELY( !fd_banks_advance_root_prepare( ctx->banks, target_bank_idx, &advanceable_root_idx ) ) ) {
2063 0 : ctx->metrics.storage_root_behind++;
2064 0 : return 0;
2065 0 : }
2066 :
2067 0 : fd_bank_t bank[1];
2068 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, advanceable_root_idx ) );
2069 :
2070 0 : if( FD_UNLIKELY( advanceable_root_idx >= ctx->block_id_len ) ) {
2071 0 : FD_LOG_CRIT(( "invariant violation: advanceable root ele out of bounds [0, %lu) index %lu", ctx->block_id_len, advanceable_root_idx ));
2072 0 : }
2073 0 : fd_block_id_ele_t * advanceable_root_ele = &ctx->block_id_arr[ advanceable_root_idx ];
2074 :
2075 0 : ulong advanceable_root_slot = fd_bank_slot_get( bank );
2076 0 : accdb_advance_root( ctx, advanceable_root_slot, bank->data->idx );
2077 :
2078 0 : fd_txncache_advance_root( ctx->txncache, bank->data->txncache_fork_id );
2079 0 : fd_sched_advance_root( ctx->sched, advanceable_root_idx );
2080 0 : fd_banks_advance_root( ctx->banks, advanceable_root_idx );
2081 :
2082 : /* Set metrics pointers. */
2083 :
2084 :
2085 : /* Reasm also prunes from the store during its publish. */
2086 :
2087 0 : fd_reasm_publish( ctx->reasm, &advanceable_root_ele->latest_mr, ctx->store );
2088 :
2089 0 : ctx->published_root_slot = advanceable_root_slot;
2090 0 : ctx->published_root_bank_idx = advanceable_root_idx;
2091 :
2092 0 : return 1;
2093 0 : }
2094 :
2095 : static void
2096 : after_credit( fd_replay_tile_t * ctx,
2097 : fd_stem_context_t * stem,
2098 : int * opt_poll_in,
2099 0 : int * charge_busy ) {
2100 0 : if( FD_UNLIKELY( !ctx->is_booted || !ctx->wfs_complete ) ) return;
2101 :
2102 0 : if( FD_UNLIKELY( maybe_become_leader( ctx, stem ) ) ) {
2103 0 : *charge_busy = 1;
2104 0 : *opt_poll_in = 0;
2105 0 : return;
2106 0 : }
2107 :
2108 : /* If we are leader, we can only unbecome the leader iff we have
2109 : received the poh hash from the poh tile and block id from reasm.
2110 : We have to do an additional check against the slot of the leader
2111 : bank because we lazily remove entries from the block id arr. */
2112 0 : if( FD_UNLIKELY( ctx->is_leader &&
2113 0 : ctx->recv_poh &&
2114 0 : ctx->block_id_arr[ ctx->leader_bank->data->idx ].block_id_seen &&
2115 0 : ctx->block_id_arr[ ctx->leader_bank->data->idx ].slot==fd_bank_slot_get( ctx->leader_bank ) ) ) {
2116 :
2117 0 : fini_leader_bank( ctx, stem );
2118 0 : *charge_busy = 1;
2119 0 : *opt_poll_in = 0;
2120 0 : return;
2121 0 : }
2122 :
2123 0 : ulong bank_idx;
2124 0 : while( (bank_idx=fd_sched_pruned_block_next( ctx->sched ))!=ULONG_MAX ) {
2125 0 : fd_bank_t bank[1];
2126 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, bank_idx ) );
2127 0 : bank->data->refcnt--;
2128 0 : FD_LOG_DEBUG(( "bank (idx=%lu) refcnt decremented to %lu for sched", bank->data->idx, bank->data->refcnt ));
2129 0 : }
2130 :
2131 : /* If the published_root is not caught up to the consensus root, then
2132 : we should try to advance the published root. */
2133 0 : if( FD_UNLIKELY( ctx->consensus_root_bank_idx!=ctx->published_root_bank_idx && advance_published_root( ctx ) ) ) {
2134 0 : *charge_busy = 1;
2135 0 : *opt_poll_in = 0;
2136 0 : return;
2137 0 : }
2138 :
2139 0 : if( FD_UNLIKELY( fd_banks_prune_dead_banks( ctx->banks ) ) ) {
2140 0 : *charge_busy = 1;
2141 0 : *opt_poll_in = 0;
2142 0 : return;
2143 0 : }
2144 :
2145 : /* If the reassembler has a fec that is ready, we should process it
2146 : and pass it to the scheduler. */
2147 0 : int evict_banks = 0;
2148 0 : if( FD_LIKELY( can_process_fec( ctx, &evict_banks ) ) ) {
2149 0 : fd_reasm_fec_t * fec = fd_reasm_pop( ctx->reasm );
2150 0 : process_fec_set( ctx, stem, fec );
2151 0 : *charge_busy = 1;
2152 0 : *opt_poll_in = 0;
2153 0 : return;
2154 0 : }
2155 :
2156 0 : if( FD_UNLIKELY( evict_banks ) ) {
2157 0 : FD_LOG_WARNING(( "banks are full and partially executed frontier banks are being evicted" ));
2158 0 : ulong frontier_cnt = 0UL;
2159 0 : ulong frontier_indices[ FD_BANKS_MAX_BANKS ];
2160 0 : fd_banks_get_frontier( ctx->banks, frontier_indices, &frontier_cnt );
2161 :
2162 : /* Mark all frontier banks as dead. As refcnts on said banks are
2163 : drained, they will be pruned away. */
2164 0 : for( ulong i=0UL; i<frontier_cnt; i++ ) {
2165 0 : fd_bank_t bank[1];
2166 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, frontier_indices[i] ) );
2167 0 : if( FD_UNLIKELY( ctx->is_leader && frontier_indices[i]==ctx->leader_bank->data->idx ) ) continue;
2168 0 : mark_bank_dead( ctx, stem, bank->data->idx );
2169 0 : fd_sched_block_abandon( ctx->sched, bank->data->idx );
2170 0 : }
2171 0 : }
2172 :
2173 0 : *charge_busy = replay( ctx, stem );
2174 0 : *opt_poll_in = !*charge_busy;
2175 0 : }
2176 :
2177 : static int
2178 : before_frag( fd_replay_tile_t * ctx,
2179 : ulong in_idx,
2180 : ulong seq FD_PARAM_UNUSED,
2181 0 : ulong sig ) {
2182 :
2183 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_SHRED ) ) {
2184 : /* If reasm is full, we can not insert any more FEC sets. We must
2185 : not consume any frags from shred_out until reasm can process more
2186 : FEC sets. */
2187 :
2188 0 : if( FD_UNLIKELY( !fd_reasm_free( ctx->reasm ) ) ) {
2189 0 : return -1;
2190 0 : }
2191 0 : }
2192 :
2193 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT && sig!=FD_GOSSIP_UPDATE_TAG_WFS_DONE ) ) return 1;
2194 0 : return 0;
2195 0 : }
2196 :
2197 : static void
2198 : process_exec_task_done( fd_replay_tile_t * ctx,
2199 : fd_stem_context_t * stem,
2200 : fd_execrp_task_done_msg_t * msg,
2201 0 : ulong sig ) {
2202 :
2203 0 : ulong exec_tile_idx = sig&0xFFFFFFFFUL;
2204 :
2205 0 : fd_bank_t bank[1];
2206 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, msg->bank_idx ) );
2207 0 : FD_TEST( bank->data );
2208 0 : bank->data->refcnt--;
2209 :
2210 0 : switch( sig>>32 ) {
2211 0 : case FD_EXECRP_TT_TXN_EXEC: {
2212 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
2213 : /* Query the txn signature against our recently generated vote
2214 : txn signatures. If the query is successful, then we have
2215 : seen our own vote transaction land and this should be marked
2216 : in the bank. We go through this exercise until we've seen
2217 : our vote rooted. */
2218 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, msg->txn_exec->txn_idx );
2219 0 : if( fd_vote_tracker_query_sig( ctx->vote_tracker, fd_type_pun_const( txn_p->payload+TXN( txn_p )->signature_off ) ) ) {
2220 0 : *fd_bank_has_identity_vote_modify( bank ) += 1;
2221 0 : }
2222 0 : }
2223 0 : if( FD_UNLIKELY( !msg->txn_exec->is_committable && !(bank->data->flags&FD_BANK_FLAGS_DEAD) ) ) {
2224 : /* Every transaction in a valid block has to execute.
2225 : Otherwise, we should mark the block as dead. */
2226 0 : mark_bank_dead( ctx, stem, bank->data->idx );
2227 0 : fd_sched_block_abandon( ctx->sched, bank->data->idx );
2228 0 : }
2229 0 : if( FD_UNLIKELY( (bank->data->flags&FD_BANK_FLAGS_DEAD) && bank->data->refcnt==0UL ) ) {
2230 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
2231 0 : }
2232 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_EXEC, msg->txn_exec->txn_idx, exec_tile_idx, NULL );
2233 0 : FD_TEST( res==0 );
2234 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, msg->txn_exec->txn_idx );
2235 0 : txn_info->flags |= FD_SCHED_TXN_EXEC_DONE;
2236 0 : if( FD_LIKELY( !(txn_info->flags&FD_SCHED_TXN_SIGVERIFY_DONE)||!txn_info->txn_err ) ) { /* Set execution status if sigverify hasn't happened yet or if sigverify was a success. */
2237 0 : txn_info->txn_err = msg->txn_exec->txn_err;
2238 0 : txn_info->flags |= fd_ulong_if( msg->txn_exec->is_committable, FD_SCHED_TXN_IS_COMMITTABLE, 0UL );
2239 0 : txn_info->flags |= fd_ulong_if( msg->txn_exec->is_fees_only, FD_SCHED_TXN_IS_FEES_ONLY, 0UL );
2240 0 : }
2241 0 : if( FD_UNLIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) { /* UNLIKELY because generally exec happens before sigverify. */
2242 0 : publish_txn_executed( ctx, stem, msg->txn_exec->txn_idx );
2243 0 : }
2244 0 : break;
2245 0 : }
2246 0 : case FD_EXECRP_TT_TXN_SIGVERIFY: {
2247 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, msg->txn_sigverify->txn_idx );
2248 0 : txn_info->flags |= FD_SCHED_TXN_SIGVERIFY_DONE;
2249 0 : if( FD_UNLIKELY( msg->txn_sigverify->err ) ) {
2250 0 : txn_info->txn_err = FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
2251 0 : txn_info->flags &= ~FD_SCHED_TXN_IS_COMMITTABLE;
2252 0 : txn_info->flags &= ~FD_SCHED_TXN_IS_FEES_ONLY;
2253 0 : }
2254 0 : if( FD_UNLIKELY( msg->txn_sigverify->err && !(bank->data->flags&FD_BANK_FLAGS_DEAD) ) ) {
2255 : /* Every transaction in a valid block has to sigverify.
2256 : Otherwise, we should mark the block as dead. Also freeze the
2257 : bank if possible. */
2258 0 : mark_bank_dead( ctx, stem, bank->data->idx );
2259 0 : fd_sched_block_abandon( ctx->sched, bank->data->idx );
2260 0 : }
2261 0 : if( FD_UNLIKELY( (bank->data->flags&FD_BANK_FLAGS_DEAD) && bank->data->refcnt==0UL ) ) {
2262 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
2263 0 : }
2264 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_SIGVERIFY, msg->txn_sigverify->txn_idx, exec_tile_idx, NULL );
2265 0 : FD_TEST( res==0 );
2266 0 : if( FD_LIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) {
2267 0 : publish_txn_executed( ctx, stem, msg->txn_exec->txn_idx );
2268 0 : }
2269 0 : break;
2270 0 : }
2271 0 : case FD_EXECRP_TT_POH_HASH: {
2272 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_POH_HASH, ULONG_MAX, exec_tile_idx, msg->poh_hash );
2273 0 : if( FD_UNLIKELY( res<0 && !(bank->data->flags&FD_BANK_FLAGS_DEAD) ) ) {
2274 0 : mark_bank_dead( ctx, stem, bank->data->idx );
2275 0 : }
2276 0 : if( FD_UNLIKELY( (bank->data->flags&FD_BANK_FLAGS_DEAD) && bank->data->refcnt==0UL ) ) {
2277 0 : fd_banks_mark_bank_frozen( ctx->banks, bank );
2278 0 : }
2279 0 : break;
2280 0 : }
2281 0 : default: FD_LOG_CRIT(( "unexpected sig 0x%lx", sig ));
2282 0 : }
2283 :
2284 : /* Reference counter just decreased, and an exec tile just got freed
2285 : up. If there's a need to be more aggressively pruning, we could
2286 : check here if more slots just became publishable and publish. Not
2287 : publishing here shouldn't bloat the fork tree too much though. We
2288 : mark minority forks dead as soon as we can, and execution dispatch
2289 : stops on dead blocks. So shortly afterwards, dead blocks should be
2290 : eligible for pruning as in-flight transactions retire from the
2291 : execution pipeline. */
2292 :
2293 0 : }
2294 :
2295 : static void
2296 : process_tower_slot_done( fd_replay_tile_t * ctx,
2297 : fd_stem_context_t * stem,
2298 : fd_tower_slot_done_t const * msg,
2299 0 : ulong seq ) {
2300 0 : fd_bank_t replay_bank[1];
2301 0 : if( FD_UNLIKELY( !fd_banks_bank_query( replay_bank, ctx->banks, msg->replay_bank_idx ) ) ) FD_LOG_CRIT(( "invariant violation: bank not found for bank index %lu", msg->replay_bank_idx ));
2302 0 : replay_bank->data->refcnt--;
2303 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for tower", replay_bank->data->idx, msg->replay_slot, replay_bank->data->refcnt ));
2304 :
2305 0 : ctx->reset_block_id = msg->reset_block_id;
2306 0 : ctx->reset_slot = msg->reset_slot;
2307 0 : ctx->reset_timestamp_nanos = fd_log_wallclock();
2308 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 ) );
2309 0 : ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, min_leader_slot, ctx->identity_pubkey );
2310 0 : if( FD_LIKELY( ctx->next_leader_slot != ULONG_MAX ) ) {
2311 0 : ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
2312 0 : } else {
2313 0 : ctx->next_leader_tickcount = LONG_MAX;
2314 0 : }
2315 :
2316 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 );
2317 0 : if( FD_UNLIKELY( !block_id_ele ) ) {
2318 0 : FD_BASE58_ENCODE_32_BYTES( msg->reset_block_id.key, reset_block_id_b58 );
2319 0 : FD_LOG_CRIT(( "invariant violation: block id ele doesn't exist for reset block id: %s, slot: %lu", reset_block_id_b58, msg->reset_slot ));
2320 0 : }
2321 0 : ulong reset_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
2322 :
2323 0 : fd_bank_t bank[1];
2324 0 : if( FD_UNLIKELY( !fd_banks_bank_query( bank, ctx->banks, reset_bank_idx ) ) ) {
2325 0 : FD_LOG_CRIT(( "invariant violation: bank not found for bank index %lu", reset_bank_idx ));
2326 0 : }
2327 :
2328 0 : if( FD_LIKELY( msg->root_slot!=ULONG_MAX ) ) FD_TEST( msg->root_slot<=msg->reset_slot );
2329 0 : fd_memcpy( ctx->reset_bank, bank, sizeof(fd_bank_t) );
2330 :
2331 0 : if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
2332 0 : fd_poh_reset_t * reset = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2333 :
2334 0 : reset->bank_idx = bank->data->idx;
2335 0 : reset->timestamp = ctx->reset_timestamp_nanos;
2336 0 : reset->completed_slot = ctx->reset_slot;
2337 0 : reset->hashcnt_per_tick = fd_bank_hashes_per_tick_get( bank );
2338 0 : reset->ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
2339 0 : reset->tick_duration_ns = (ulong)(ctx->slot_duration_nanos/(double)reset->ticks_per_slot);
2340 :
2341 0 : fd_memcpy( reset->completed_block_id, &block_id_ele->latest_mr, sizeof(fd_hash_t) );
2342 :
2343 0 : fd_blockhashes_t const * block_hash_queue = fd_bank_block_hash_queue_query( bank );
2344 0 : fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
2345 0 : FD_TEST( last_hash );
2346 0 : fd_memcpy( reset->completed_blockhash, last_hash->uc, sizeof(fd_hash_t) );
2347 :
2348 0 : ulong ticks_per_slot = fd_bank_ticks_per_slot_get( bank );
2349 0 : if( FD_UNLIKELY( reset->hashcnt_per_tick==1UL ) ) {
2350 : /* Low power producer, maximum of one microblock per tick in the slot */
2351 0 : reset->max_microblocks_in_slot = ticks_per_slot;
2352 0 : } else {
2353 : /* See the long comment in after_credit for this limit */
2354 0 : reset->max_microblocks_in_slot = fd_ulong_min( MAX_MICROBLOCKS_PER_SLOT, ticks_per_slot*(reset->hashcnt_per_tick-1UL) );
2355 0 : }
2356 0 : reset->next_leader_slot = ctx->next_leader_slot;
2357 :
2358 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() ) );
2359 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 );
2360 0 : }
2361 :
2362 0 : FD_LOG_INFO(( "tower_slot_done(reset_slot=%lu, next_leader_slot=%lu, vote_slot=%lu, replay_slot=%lu, root_slot=%lu, seqno=%lu)", msg->reset_slot, ctx->next_leader_slot, msg->vote_slot, msg->replay_slot, msg->root_slot, seq ));
2363 0 : maybe_become_leader( ctx, stem );
2364 :
2365 0 : if( FD_LIKELY( msg->root_slot!=ULONG_MAX ) ) {
2366 :
2367 0 : FD_TEST( msg->root_slot>=ctx->consensus_root_slot );
2368 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 );
2369 0 : FD_TEST( block_id_ele );
2370 0 : ctx->consensus_root_slot = msg->root_slot;
2371 0 : ctx->consensus_root = msg->root_block_id;
2372 0 : ctx->consensus_root_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele );
2373 :
2374 0 : publish_root_advanced( ctx, stem );
2375 :
2376 0 : fd_sched_root_notify( ctx->sched, ctx->consensus_root_bank_idx );
2377 0 : }
2378 :
2379 0 : ulong distance = 0UL;
2380 0 : fd_bank_t * parent = bank;
2381 0 : while( parent ) {
2382 0 : if( FD_UNLIKELY( parent->data->idx==ctx->consensus_root_bank_idx ) ) break;
2383 0 : parent = fd_banks_get_parent( bank, ctx->banks, parent );
2384 0 : distance++;
2385 0 : }
2386 :
2387 0 : FD_MGAUGE_SET( REPLAY, ROOT_DISTANCE, distance );
2388 0 : }
2389 :
2390 : static void
2391 : process_fec_complete( fd_replay_tile_t * ctx,
2392 0 : uchar const * shred_buf ) {
2393 0 : fd_shred_t const * shred = (fd_shred_t const *)fd_type_pun_const( shred_buf );
2394 :
2395 0 : fd_hash_t const * merkle_root = (fd_hash_t const *)fd_type_pun_const( shred_buf + FD_SHRED_DATA_HEADER_SZ );
2396 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) );
2397 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) );
2398 :
2399 0 : int data_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_DATA_COMPLETE );
2400 0 : int slot_complete = !!( shred->data.flags & FD_SHRED_DATA_FLAG_SLOT_COMPLETE );
2401 :
2402 0 : if( FD_UNLIKELY( shred->slot - shred->data.parent_off == fd_reasm_slot0( ctx->reasm ) && shred->fec_set_idx == 0) ) {
2403 0 : chained_merkle_root = &fd_reasm_root( ctx->reasm )->key;
2404 0 : }
2405 :
2406 0 : if( FD_UNLIKELY( !fd_reasm_free( ctx->reasm ) ) ) {
2407 0 : FD_LOG_CRIT(( "unimplemented" )); /* TODO reasm eviction */
2408 0 : }
2409 0 : if( FD_UNLIKELY( fd_reasm_query( ctx->reasm, merkle_root ) ) ) return;
2410 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 ) );
2411 0 : }
2412 :
2413 : static void
2414 0 : process_resolv_slot_completed( fd_replay_tile_t * ctx, ulong bank_idx ) {
2415 0 : fd_bank_t bank[1];
2416 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, bank_idx ) );
2417 0 : bank->data->refcnt--;
2418 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for resolv", bank->data->idx, fd_bank_slot_get( bank ), bank->data->refcnt ));
2419 0 : }
2420 :
2421 : static void
2422 : process_vote_txn_sent( fd_replay_tile_t * ctx,
2423 0 : fd_txn_m_t * txnm ) {
2424 : /* The send tile has signed and sent a vote. Add this vote to the
2425 : vote tracker. We go through this exercise until we've seen our
2426 : vote rooted. */
2427 0 : if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) {
2428 0 : uchar * payload = ((uchar *)txnm) + sizeof(fd_txn_m_t);
2429 0 : uchar txn_mem[ FD_TXN_MAX_SZ ] __attribute__((aligned(alignof(fd_txn_t))));
2430 0 : fd_txn_t * txn = (fd_txn_t *)txn_mem;
2431 0 : if( FD_UNLIKELY( !fd_txn_parse( payload, txnm->payload_sz, txn_mem, NULL ) ) ) {
2432 0 : FD_LOG_CRIT(( "Could not parse txn from send tile" ));
2433 0 : }
2434 0 : fd_vote_tracker_insert( ctx->vote_tracker, fd_type_pun_const( payload+txn->signature_off ) );
2435 0 : }
2436 0 : }
2437 :
2438 : static inline void
2439 0 : maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
2440 0 : if( FD_LIKELY( ctx->expected_shred_version && ctx->ipecho_shred_version ) ) {
2441 0 : if( FD_UNLIKELY( ctx->expected_shred_version!=ctx->ipecho_shred_version ) ) {
2442 0 : FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from ipecho", ctx->expected_shred_version, ctx->ipecho_shred_version ) );
2443 0 : }
2444 0 : }
2445 :
2446 0 : if( FD_LIKELY( ctx->has_genesis_hash && ctx->hard_forks_cnt!=ULONG_MAX && (ctx->expected_shred_version || ctx->ipecho_shred_version) ) ) {
2447 0 : ushort expected_shred_version = ctx->expected_shred_version ? ctx->expected_shred_version : ctx->ipecho_shred_version;
2448 :
2449 0 : ushort actual_shred_version = compute_shred_version( ctx->genesis_hash->uc, ctx->hard_forks, ctx->hard_forks_cnts, ctx->hard_forks_cnt );
2450 :
2451 0 : if( FD_UNLIKELY( expected_shred_version!=actual_shred_version ) ) {
2452 0 : FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc, genesis_hash_b58 );
2453 0 : FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from genesis hash %s and hard forks", expected_shred_version, actual_shred_version, genesis_hash_b58 ));
2454 0 : }
2455 0 : }
2456 0 : }
2457 :
2458 : static void
2459 : process_tower_optimistic_confirmed( fd_replay_tile_t * ctx,
2460 : fd_stem_context_t * stem,
2461 0 : fd_tower_slot_confirmed_t const * msg ) {
2462 0 : FD_TEST( msg->bank_idx!=ULONG_MAX );
2463 :
2464 0 : if( ctx->rpc_enabled ) {
2465 0 : fd_bank_t bank[1];
2466 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, msg->bank_idx ) );
2467 0 : bank->data->refcnt++;
2468 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for rpc", bank->data->idx, fd_bank_slot_get( bank ), bank->data->refcnt ));
2469 0 : }
2470 :
2471 0 : fd_replay_oc_advanced_t * replay_msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2472 0 : replay_msg->bank_idx = msg->bank_idx;
2473 0 : replay_msg->slot = msg->slot;
2474 :
2475 0 : fd_stem_publish( stem, ctx->replay_out->idx, REPLAY_SIG_OC_ADVANCED, ctx->replay_out->chunk, sizeof(fd_replay_oc_advanced_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
2476 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_replay_oc_advanced_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
2477 0 : }
2478 :
2479 : static inline int
2480 : returnable_frag( fd_replay_tile_t * ctx,
2481 : ulong in_idx,
2482 : ulong seq,
2483 : ulong sig,
2484 : ulong chunk,
2485 : ulong sz,
2486 : ulong ctl,
2487 : ulong tsorig,
2488 : ulong tspub,
2489 0 : fd_stem_context_t * stem ) {
2490 0 : (void)seq;
2491 0 : (void)ctl;
2492 0 : (void)tsorig;
2493 0 : (void)tspub;
2494 :
2495 0 : if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
2496 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 ));
2497 :
2498 0 : switch( ctx->in_kind[in_idx] ) {
2499 0 : case IN_KIND_GENESIS: {
2500 0 : fd_genesis_meta_t const * meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2501 0 : ctx->has_genesis_hash = 1;
2502 0 : *ctx->genesis_hash = meta->genesis_hash;
2503 0 : if( FD_LIKELY( meta->bootstrap ) ) {
2504 0 : boot_genesis( ctx, stem, meta );
2505 0 : }
2506 :
2507 0 : maybe_verify_cluster_type( ctx );
2508 0 : maybe_verify_shred_version( ctx );
2509 0 : break;
2510 0 : }
2511 0 : case IN_KIND_IPECHO: {
2512 0 : FD_TEST( sig && sig<=USHORT_MAX );
2513 0 : ctx->ipecho_shred_version = (ushort)sig;
2514 0 : maybe_verify_shred_version( ctx );
2515 0 : break;
2516 0 : }
2517 0 : case IN_KIND_SNAP:
2518 0 : on_snapshot_message( ctx, stem, in_idx, chunk, sig );
2519 0 : maybe_verify_shred_version( ctx );
2520 0 : break;
2521 0 : case IN_KIND_EXECRP: {
2522 0 : process_exec_task_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), sig );
2523 0 : break;
2524 0 : }
2525 0 : case IN_KIND_POH: {
2526 0 : process_poh_message( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2527 0 : break;
2528 0 : }
2529 0 : case IN_KIND_RESOLV: {
2530 0 : fd_resolv_slot_exchanged_t * exchanged_slot = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2531 0 : process_resolv_slot_completed( ctx, exchanged_slot->bank_idx );
2532 0 : break;
2533 0 : }
2534 0 : case IN_KIND_TOWER: {
2535 0 : if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_DONE ) ) {
2536 0 : process_tower_slot_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), seq );
2537 0 : }
2538 0 : else if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_CONFIRMED ) ) {
2539 0 : fd_tower_slot_confirmed_t const * msg = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2540 0 : if( msg->kind==FD_TOWER_SLOT_CONFIRMED_OPTIMISTIC ) process_tower_optimistic_confirmed( ctx, stem, msg );
2541 0 : }
2542 0 : else if( FD_LIKELY( sig==FD_TOWER_SIG_SLOT_IGNORED ) ) {
2543 0 : fd_tower_slot_ignored_t const * msg = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2544 0 : fd_tower_slot_done_t ignored = {
2545 0 : .replay_slot = msg->slot,
2546 0 : .replay_bank_idx = msg->bank_idx,
2547 0 : .vote_slot = ULONG_MAX,
2548 0 : .reset_slot = ctx->reset_slot, /* Use most recent reset slot */
2549 0 : .reset_block_id = ctx->reset_block_id,
2550 0 : .root_slot = ULONG_MAX
2551 0 : };
2552 0 : process_tower_slot_done( ctx, stem, &ignored, seq );
2553 0 : }
2554 0 : break;
2555 0 : }
2556 0 : case IN_KIND_SHRED: {
2557 : /* TODO: This message/sz should be defined. */
2558 0 : if( sz!=0 && fd_disco_shred_out_msg_type( sig )==FD_SHRED_OUT_MSG_TYPE_FEC ) {
2559 : /* If receive a FEC complete message. */
2560 0 : process_fec_complete( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2561 0 : }
2562 0 : break;
2563 0 : }
2564 0 : case IN_KIND_TXSEND: {
2565 0 : process_vote_txn_sent( ctx, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
2566 0 : break;
2567 0 : }
2568 0 : case IN_KIND_GOSSIP_OUT: {
2569 0 : FD_TEST( sig==FD_GOSSIP_UPDATE_TAG_WFS_DONE );
2570 0 : ctx->wfs_complete = 1;
2571 0 : FD_LOG_NOTICE(( "Done waiting for supermajority. More than 80 percent of cluster stake has joined." ));
2572 0 : break;
2573 0 : }
2574 0 : case IN_KIND_RPC:
2575 0 : case IN_KIND_GUI: {
2576 0 : fd_bank_t bank[1];
2577 0 : FD_TEST( fd_banks_bank_query( bank, ctx->banks, sig ) );
2578 0 : bank->data->refcnt--;
2579 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for %s", bank->data->idx, fd_bank_slot_get( bank ), bank->data->refcnt, ctx->in_kind[ in_idx ]==IN_KIND_RPC ? "rpc" : "gui" ));
2580 0 : break;
2581 0 : }
2582 0 : default:
2583 0 : FD_LOG_ERR(( "unhandled kind %d", ctx->in_kind[ in_idx ] ));
2584 0 : }
2585 :
2586 0 : return 0;
2587 0 : }
2588 :
2589 : static inline fd_replay_out_link_t
2590 : out1( fd_topo_t const * topo,
2591 : fd_topo_tile_t const * tile,
2592 0 : char const * name ) {
2593 0 : ulong idx = ULONG_MAX;
2594 :
2595 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
2596 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
2597 0 : if( !strcmp( link->name, name ) ) {
2598 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 ));
2599 0 : idx = i;
2600 0 : }
2601 0 : }
2602 :
2603 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_replay_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
2604 :
2605 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
2606 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
2607 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
2608 :
2609 0 : return (fd_replay_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
2610 0 : }
2611 :
2612 : static void
2613 : privileged_init( fd_topo_t * topo,
2614 0 : fd_topo_tile_t * tile ) {
2615 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
2616 :
2617 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
2618 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
2619 :
2620 0 : if( FD_UNLIKELY( !strcmp( tile->replay.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
2621 :
2622 0 : ctx->identity_pubkey[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->replay.identity_key_path, /* pubkey only: */ 1 ) );
2623 :
2624 0 : if( FD_UNLIKELY( !tile->replay.bundle.vote_account_path[0] ) ) {
2625 0 : tile->replay.bundle.enabled = 0;
2626 0 : }
2627 :
2628 0 : if( FD_UNLIKELY( tile->replay.bundle.enabled ) ) {
2629 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.bundle.vote_account_path, ctx->bundle.vote_account.uc ) ) ) {
2630 0 : const uchar * vote_key = fd_keyload_load( tile->replay.bundle.vote_account_path, /* pubkey only: */ 1 );
2631 0 : fd_memcpy( ctx->bundle.vote_account.uc, vote_key, 32UL );
2632 0 : }
2633 0 : }
2634 :
2635 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->reasm_seed, sizeof(ulong) ) ) ) {
2636 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
2637 0 : }
2638 :
2639 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->vote_tracker_seed, sizeof(ulong) ) ) ) {
2640 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
2641 0 : }
2642 :
2643 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->block_id_map_seed, sizeof(ulong) ) ) ) {
2644 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
2645 0 : }
2646 0 : }
2647 :
2648 : static void
2649 : unprivileged_init( fd_topo_t * topo,
2650 0 : fd_topo_tile_t * tile ) {
2651 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
2652 :
2653 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
2654 :
2655 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
2656 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
2657 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 );
2658 0 : void * block_id_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
2659 0 : void * _txncache = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
2660 0 : void * reasm_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_reasm_align(), fd_reasm_footprint( tile->replay.fec_max ) );
2661 0 : void * sched_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots ) );
2662 0 : void * vinyl_req_pool_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_vinyl_req_pool_align(), fd_vinyl_req_pool_footprint( 1UL, 1UL ) );
2663 0 : void * vote_tracker_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
2664 0 : void * _capture_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
2665 0 : void * dump_proto_ctx_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
2666 0 : # if FD_HAS_FLATCC
2667 0 : void * block_dump_ctx = NULL;
2668 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
2669 0 : block_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
2670 0 : }
2671 0 : # endif
2672 :
2673 0 : ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
2674 :
2675 0 : ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
2676 0 : FD_TEST( store_obj_id!=ULONG_MAX );
2677 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
2678 0 : FD_TEST( ctx->store );
2679 :
2680 0 : ulong banks_obj_id = fd_pod_query_ulong( topo->props, "banks", ULONG_MAX );
2681 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
2682 0 : ulong banks_locks_obj_id = fd_pod_query_ulong( topo->props, "banks_locks", ULONG_MAX );
2683 0 : FD_TEST( banks_locks_obj_id!=ULONG_MAX );
2684 :
2685 0 : FD_TEST( fd_banks_join( ctx->banks, fd_topo_obj_laddr( topo, banks_obj_id ), fd_topo_obj_laddr( topo, banks_locks_obj_id ) ) );
2686 :
2687 0 : fd_bank_data_t * bank_pool = fd_banks_get_bank_pool( ctx->banks->data );
2688 0 : FD_MGAUGE_SET( REPLAY, MAX_LIVE_BANKS, fd_banks_pool_max( bank_pool ) );
2689 :
2690 0 : fd_bank_t bank[1];
2691 0 : FD_TEST( fd_banks_init_bank( bank, ctx->banks ) );
2692 0 : fd_bank_slot_set( bank, 0UL );
2693 0 : FD_TEST( bank->data->idx==FD_REPLAY_BOOT_BANK_IDX );
2694 :
2695 0 : ctx->consensus_root_slot = ULONG_MAX;
2696 0 : ctx->consensus_root = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
2697 0 : ctx->published_root_slot = ULONG_MAX;
2698 :
2699 0 : ctx->expected_shred_version = tile->replay.expected_shred_version;
2700 0 : ctx->ipecho_shred_version = 0;
2701 0 : fd_memcpy( ctx->genesis_path, tile->replay.genesis_path, sizeof(ctx->genesis_path) );
2702 0 : ctx->has_genesis_hash = 0;
2703 0 : ctx->cluster_type = FD_CLUSTER_UNKNOWN;
2704 0 : ctx->hard_forks_cnt = ULONG_MAX;
2705 :
2706 0 : if( FD_UNLIKELY( tile->replay.bundle.enabled ) ) {
2707 0 : ctx->bundle.enabled = 1;
2708 0 : if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->bundle.gen,
2709 0 : (fd_acct_addr_t const *)tile->replay.bundle.tip_distribution_program_addr,
2710 0 : (fd_acct_addr_t const *)tile->replay.bundle.tip_payment_program_addr,
2711 0 : (fd_acct_addr_t const *)ctx->bundle.vote_account.uc,
2712 0 : (fd_acct_addr_t const *)ctx->bundle.vote_account.uc, "NAN", 0UL ) ) ) {
2713 0 : FD_LOG_ERR(( "failed to initialize bundle crank gen" ));
2714 0 : }
2715 0 : } else {
2716 0 : ctx->bundle.enabled = 0;
2717 0 : }
2718 :
2719 0 : fd_features_t * features = fd_bank_features_modify( bank );
2720 0 : fd_features_enable_cleaned_up( features );
2721 :
2722 0 : char const * one_off_features[ 16UL ];
2723 0 : FD_TEST( tile->replay.enable_features_cnt<=sizeof(one_off_features)/sizeof(one_off_features[0]) );
2724 0 : for( ulong i=0UL; i<tile->replay.enable_features_cnt; i++ ) one_off_features[ i ] = tile->replay.enable_features[i];
2725 0 : fd_features_enable_one_offs( features, one_off_features, (uint)tile->replay.enable_features_cnt, 0UL );
2726 :
2727 0 : fd_topo_obj_t const * vinyl_data = fd_topo_find_tile_obj( topo, tile, "vinyl_data" );
2728 :
2729 0 : ulong progcache_obj_id; FD_TEST( (progcache_obj_id = fd_pod_query_ulong( topo->props, "progcache", ULONG_MAX ) )!=ULONG_MAX );
2730 0 : ulong progcache_locks_obj_id; FD_TEST( (progcache_locks_obj_id = fd_pod_query_ulong( topo->props, "progcache_locks", ULONG_MAX ) )!=ULONG_MAX );
2731 0 : FD_TEST( fd_progcache_admin_join( ctx->progcache_admin,
2732 0 : fd_topo_obj_laddr( topo, progcache_obj_id ),
2733 0 : fd_topo_obj_laddr( topo, progcache_locks_obj_id ) ) );
2734 :
2735 0 : ulong funk_obj_id; FD_TEST( (funk_obj_id = fd_pod_query_ulong( topo->props, "funk", ULONG_MAX ) )!=ULONG_MAX );
2736 0 : ulong funk_locks_obj_id; FD_TEST( (funk_locks_obj_id = fd_pod_query_ulong( topo->props, "funk_locks", ULONG_MAX ) )!=ULONG_MAX );
2737 0 : ulong max_depth = tile->replay.max_live_slots + tile->replay.write_delay_slots;
2738 0 : if( !vinyl_data ) {
2739 0 : FD_TEST( fd_accdb_admin_v1_init( ctx->accdb_admin,
2740 0 : fd_topo_obj_laddr( topo, funk_obj_id ),
2741 0 : fd_topo_obj_laddr( topo, funk_locks_obj_id ) ) );
2742 0 : } else {
2743 0 : fd_topo_obj_t const * vinyl_rq = fd_topo_find_tile_obj( topo, tile, "vinyl_rq" );
2744 0 : fd_topo_obj_t const * vinyl_req_pool = fd_topo_find_tile_obj( topo, tile, "vinyl_rpool" );
2745 0 : FD_TEST( fd_accdb_admin_v2_init( ctx->accdb_admin,
2746 0 : fd_topo_obj_laddr( topo, funk_obj_id ),
2747 0 : fd_topo_obj_laddr( topo, funk_locks_obj_id ),
2748 0 : fd_topo_obj_laddr( topo, vinyl_rq->id ),
2749 0 : topo->workspaces[ vinyl_data->wksp_id ].wksp,
2750 0 : fd_topo_obj_laddr( topo, vinyl_req_pool->id ),
2751 0 : vinyl_rq->id,
2752 0 : max_depth ) );
2753 0 : fd_accdb_admin_v2_delay_set( ctx->accdb_admin, tile->replay.write_delay_slots );
2754 0 : }
2755 0 : fd_accdb_init_from_topo( ctx->accdb, topo, tile, max_depth );
2756 :
2757 0 : void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->replay.txncache_obj_id );
2758 0 : fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
2759 0 : FD_TEST( txncache_shmem );
2760 0 : ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
2761 0 : FD_TEST( ctx->txncache );
2762 :
2763 0 : ctx->capture_ctx = NULL;
2764 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
2765 0 : ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
2766 0 : ctx->capture_ctx->solcap_start_slot = tile->replay.capture_start_slot;
2767 0 : ctx->capture_ctx->capture_solcap = 1;
2768 0 : }
2769 :
2770 0 : ctx->dump_proto_ctx = NULL;
2771 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.dump_proto_dir ) ) ) {
2772 0 : ctx->dump_proto_ctx = dump_proto_ctx_mem;
2773 0 : ctx->dump_proto_ctx->dump_proto_output_dir = tile->replay.dump_proto_dir;
2774 0 : if( FD_LIKELY( tile->replay.dump_block_to_pb ) ) {
2775 0 : ctx->dump_proto_ctx->dump_block_to_pb = !!tile->replay.dump_block_to_pb;
2776 0 : }
2777 0 : }
2778 :
2779 0 : # if FD_HAS_FLATCC
2780 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
2781 0 : ctx->block_dump_ctx = fd_block_dump_context_join( fd_block_dump_context_new( block_dump_ctx ) );
2782 0 : } else {
2783 0 : ctx->block_dump_ctx = NULL;
2784 0 : }
2785 0 : # endif
2786 :
2787 0 : ctx->is_booted = 0;
2788 :
2789 0 : ctx->larger_max_cost_per_block = tile->replay.larger_max_cost_per_block;
2790 :
2791 0 : ctx->reasm = fd_reasm_join( fd_reasm_new( reasm_mem, tile->replay.fec_max, ctx->reasm_seed ) );
2792 0 : FD_TEST( ctx->reasm );
2793 :
2794 0 : ctx->sched = fd_sched_join( fd_sched_new( sched_mem, tile->replay.sched_depth, tile->replay.max_live_slots, fd_topo_tile_name_cnt( topo, "execrp" ) ) );
2795 0 : FD_TEST( ctx->sched );
2796 :
2797 0 : FD_TEST( fd_vinyl_req_pool_new( vinyl_req_pool_mem, 1UL, 1UL ) );
2798 :
2799 0 : ctx->vote_tracker = fd_vote_tracker_join( fd_vote_tracker_new( vote_tracker_mem, ctx->vote_tracker_seed ) );
2800 0 : FD_TEST( ctx->vote_tracker );
2801 :
2802 0 : ctx->has_identity_vote_rooted = 0;
2803 0 : ctx->wait_for_vote_to_start_leader = tile->replay.wait_for_vote_to_start_leader;
2804 :
2805 0 : ctx->wfs_enabled = memcmp( tile->replay.wait_for_supermajority_with_bank_hash.uc, ((fd_pubkey_t){ 0 }).uc, sizeof(fd_pubkey_t) );
2806 0 : ctx->expected_bank_hash = tile->replay.wait_for_supermajority_with_bank_hash;
2807 0 : ctx->wfs_complete = !ctx->wfs_enabled;
2808 :
2809 0 : ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
2810 0 : FD_TEST( ctx->mleaders );
2811 :
2812 0 : ctx->is_leader = 0;
2813 0 : ctx->supports_leader = fd_topo_find_tile( topo, "pack", 0UL )!=ULONG_MAX;
2814 0 : ctx->reset_slot = 0UL;
2815 0 : fd_memset( ctx->reset_bank, 0, sizeof(fd_bank_t) );
2816 0 : ctx->reset_block_id = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
2817 0 : ctx->reset_timestamp_nanos = 0UL;
2818 0 : ctx->next_leader_slot = ULONG_MAX;
2819 0 : ctx->next_leader_tickcount = LONG_MAX;
2820 0 : ctx->highwater_leader_slot = ULONG_MAX;
2821 0 : ctx->slot_duration_nanos = 350L*1000L*1000L; /* TODO: Not fixed ... not always 350ms ... */
2822 0 : ctx->slot_duration_ticks = (double)ctx->slot_duration_nanos*fd_tempo_tick_per_ns( NULL );
2823 0 : ctx->leader_bank->data = NULL;
2824 :
2825 0 : ctx->block_id_len = tile->replay.max_live_slots;
2826 0 : ctx->block_id_arr = (fd_block_id_ele_t *)block_id_arr_mem;
2827 0 : ctx->block_id_map = fd_block_id_map_join( fd_block_id_map_new( block_id_map_mem, chain_cnt, ctx->block_id_map_seed ) );
2828 0 : FD_TEST( ctx->block_id_map );
2829 0 : for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) ctx->block_id_arr[ i ].block_id_seen = 0;
2830 :
2831 0 : ctx->resolv_tile_cnt = fd_topo_tile_name_cnt( topo, "resolv" );
2832 :
2833 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
2834 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
2835 0 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
2836 0 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
2837 :
2838 0 : if( FD_LIKELY( link->dcache ) ) {
2839 0 : ctx->in[ i ].mem = link_wksp->wksp;
2840 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
2841 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
2842 0 : ctx->in[ i ].mtu = link->mtu;
2843 0 : }
2844 :
2845 0 : if( !strcmp( link->name, "genesi_out" ) ) ctx->in_kind[ i ] = IN_KIND_GENESIS;
2846 0 : else if( !strcmp( link->name, "ipecho_out" ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
2847 0 : else if( !strcmp( link->name, "snapin_manif" ) ) ctx->in_kind[ i ] = IN_KIND_SNAP;
2848 0 : else if( !strcmp( link->name, "execrp_replay" ) ) ctx->in_kind[ i ] = IN_KIND_EXECRP;
2849 0 : else if( !strcmp( link->name, "tower_out" ) ) ctx->in_kind[ i ] = IN_KIND_TOWER;
2850 0 : else if( !strcmp( link->name, "poh_replay" ) ) ctx->in_kind[ i ] = IN_KIND_POH;
2851 0 : else if( !strcmp( link->name, "resolv_replay" ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
2852 0 : else if( !strcmp( link->name, "shred_out" ) ) ctx->in_kind[ i ] = IN_KIND_SHRED;
2853 0 : else if( !strcmp( link->name, "txsend_out" ) ) ctx->in_kind[ i ] = IN_KIND_TXSEND;
2854 0 : else if( !strcmp( link->name, "gui_replay" ) ) ctx->in_kind[ i ] = IN_KIND_GUI;
2855 0 : else if( !strcmp( link->name, "rpc_replay" ) ) ctx->in_kind[ i ] = IN_KIND_RPC;
2856 0 : else if( !strcmp( link->name, "gossip_out" ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;
2857 0 : else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
2858 0 : }
2859 :
2860 0 : *ctx->epoch_out = out1( topo, tile, "replay_epoch" ); FD_TEST( ctx->epoch_out->idx!=ULONG_MAX );
2861 0 : *ctx->replay_out = out1( topo, tile, "replay_out" ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
2862 0 : *ctx->exec_out = out1( topo, tile, "replay_execrp" ); FD_TEST( ctx->exec_out->idx!=ULONG_MAX );
2863 :
2864 0 : ctx->gui_enabled = fd_topo_find_tile( topo, "gui", 0UL )!=ULONG_MAX;
2865 0 : ctx->rpc_enabled = fd_topo_find_tile( topo, "rpc", 0UL )!=ULONG_MAX;
2866 :
2867 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
2868 0 : ulong idx = fd_topo_find_tile_out_link( topo, tile, "cap_repl", 0UL );
2869 0 : FD_TEST( idx!=ULONG_MAX );
2870 0 : fd_topo_link_t * link = &topo->links[ tile->out_link_id[ idx ] ];
2871 :
2872 :
2873 0 : fd_capture_link_buf_t * cap_repl_out = ctx->cap_repl_out;
2874 0 : cap_repl_out->base.vt = &fd_capture_link_buf_vt;
2875 0 : cap_repl_out->idx = idx;
2876 0 : cap_repl_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
2877 0 : cap_repl_out->chunk0 = fd_dcache_compact_chunk0( cap_repl_out->mem, link->dcache );
2878 0 : cap_repl_out->wmark = fd_dcache_compact_wmark( cap_repl_out->mem, link->dcache, link->mtu );
2879 0 : cap_repl_out->chunk = cap_repl_out->chunk0;
2880 0 : cap_repl_out->mcache = link->mcache;
2881 0 : cap_repl_out->depth = fd_mcache_depth( link->mcache );
2882 0 : cap_repl_out->seq = 0UL;
2883 :
2884 0 : ctx->capture_ctx->capctx_type.buf = cap_repl_out;
2885 0 : ctx->capture_ctx->capture_link = &cap_repl_out->base;
2886 0 : ctx->capture_ctx->current_txn_idx = 0UL;
2887 :
2888 :
2889 0 : ulong consumer_tile_idx = fd_topo_find_tile( topo, "solcap", 0UL );
2890 0 : fd_topo_tile_t * consumer_tile = &topo->tiles[ consumer_tile_idx ];
2891 0 : cap_repl_out->fseq = NULL;
2892 0 : for( ulong j = 0UL; j < consumer_tile->in_cnt; j++ ) {
2893 0 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ] == link->id ) ) {
2894 0 : cap_repl_out->fseq = fd_fseq_join( fd_topo_obj_laddr( topo, consumer_tile->in_link_fseq_obj_id[ j ] ) );
2895 0 : FD_TEST( cap_repl_out->fseq );
2896 0 : break;
2897 0 : }
2898 0 : }
2899 0 : }
2900 :
2901 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
2902 :
2903 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_query_wait, FD_MHIST_SECONDS_MIN( REPLAY, STORE_QUERY_WAIT ),
2904 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_QUERY_WAIT ) ) );
2905 0 : fd_histf_join( fd_histf_new( ctx->metrics.store_query_work, FD_MHIST_SECONDS_MIN( REPLAY, STORE_QUERY_WORK ),
2906 0 : FD_MHIST_SECONDS_MAX( REPLAY, STORE_QUERY_WORK ) ) );
2907 :
2908 0 : fd_histf_join( fd_histf_new( ctx->metrics.root_slot_dur, FD_MHIST_SECONDS_MIN( REPLAY, ROOT_SLOT_DURATION_SECONDS ),
2909 0 : FD_MHIST_SECONDS_MAX( REPLAY, ROOT_SLOT_DURATION_SECONDS ) ) );
2910 0 : fd_histf_join( fd_histf_new( ctx->metrics.root_account_dur, FD_MHIST_SECONDS_MIN( REPLAY, ROOT_ACCOUNT_DURATION_SECONDS ),
2911 0 : FD_MHIST_SECONDS_MAX( REPLAY, ROOT_ACCOUNT_DURATION_SECONDS ) ) );
2912 :
2913 : /* Ensure precompiles are available, crash fast otherwise */
2914 0 : fd_precompiles();
2915 :
2916 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
2917 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
2918 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
2919 0 : }
2920 :
2921 : static ulong
2922 : populate_allowed_seccomp( fd_topo_t const * topo FD_FN_UNUSED,
2923 : fd_topo_tile_t const * tile FD_FN_UNUSED,
2924 : ulong out_cnt,
2925 0 : struct sock_filter * out ) {
2926 :
2927 0 : populate_sock_filter_policy_fd_replay_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
2928 0 : return sock_filter_policy_fd_replay_tile_instr_cnt;
2929 0 : }
2930 :
2931 : static ulong
2932 : populate_allowed_fds( fd_topo_t const * topo FD_FN_UNUSED,
2933 : fd_topo_tile_t const * tile FD_FN_UNUSED,
2934 : ulong out_fds_cnt,
2935 0 : int * out_fds ) {
2936 :
2937 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
2938 :
2939 0 : ulong out_cnt = 0UL;
2940 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
2941 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
2942 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
2943 0 : return out_cnt;
2944 0 : }
2945 :
2946 : #undef DEBUG_LOGGING
2947 :
2948 : /* counting carefully, after_credit can generate at most 7 frags and
2949 : returnable_frag boot_genesis can also generate at most 7 frags, so 14
2950 : is a conservative bound. */
2951 0 : #define STEM_BURST (14UL)
2952 :
2953 : /* TODO: calculate this properly/fix stem to work with larger numbers of links */
2954 : /* 1000 chosen empirically as anything larger slowed down replay times. Need to calculate
2955 : this properly. */
2956 0 : #define STEM_LAZY ((long)10e3)
2957 :
2958 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_replay_tile_t
2959 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_replay_tile_t)
2960 :
2961 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
2962 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
2963 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
2964 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
2965 :
2966 : #include "../../disco/stem/fd_stem.c"
2967 :
2968 : fd_topo_run_tile_t fd_tile_replay = {
2969 : .name = "replay",
2970 : .populate_allowed_seccomp = populate_allowed_seccomp,
2971 : .populate_allowed_fds = populate_allowed_fds,
2972 : .scratch_align = scratch_align,
2973 : .scratch_footprint = scratch_footprint,
2974 : .privileged_init = privileged_init,
2975 : .unprivileged_init = unprivileged_init,
2976 : .run = stem_run,
2977 : };
|