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