Line data Source code
1 : #define _GNU_SOURCE
2 : #include <linux/futex.h> /* FUTEX_WAKE */
3 : #include <sys/syscall.h> /* SYS_futex */
4 : #include <unistd.h> /* syscall(2) */
5 :
6 : #include "fd_replay_tile.h"
7 : #include "fd_replay_tile_private.h"
8 : #include "../../ballet/bls/fd_bls12_381.h"
9 : #include "fd_sched.h"
10 : #include "fd_execrp.h"
11 : #include "generated/fd_replay_tile_seccomp.h"
12 :
13 : #include "../admin/fd_adminctl.h"
14 : #include <errno.h>
15 : #include <fcntl.h>
16 : #include "../genesis/fd_genesi_tile.h"
17 : #include "../poh/fd_poh.h"
18 : #include "../votor/fd_votor_tile.h"
19 : #include "../resolv/fd_resolv_tile.h"
20 : #include "../restore/utils/fd_ssload.h"
21 : #include "../restore/utils/fd_wfs.h"
22 : #include "../../flamenco/gossip/fd_gossip_message.h"
23 :
24 : #include "../../disco/tiles.h"
25 : #include "../../disco/fd_txn_m.h"
26 : #include "../../disco/shred/fd_fec_set.h"
27 : #include "../../disco/shred/fd_shred_tile.h"
28 : #include "../../disco/pack/fd_pack.h"
29 : #include "../../disco/pack/fd_pack_cost.h"
30 : #include "../backup/fd_snapmk_tile.h"
31 : #include "../../disco/keyguard/fd_keyload.h"
32 : #include "../../disco/genesis/fd_genesis_cluster.h"
33 : #include "../../discof/genesis/genesis_hash.h"
34 : #include "../../util/pod/fd_pod.h"
35 : #include "../../util/fd_boolau.h"
36 : #include "../../flamenco/rewards/fd_rewards.h"
37 : #include "../../flamenco/leaders/fd_multi_epoch_leaders.h"
38 : #include "../../flamenco/stakes/fd_stakes.h"
39 : #include "../../flamenco/progcache/fd_progcache_admin.h"
40 : #include "../../flamenco/rewards/fd_rewards.h"
41 : #include "../../disco/metrics/fd_metrics.h"
42 : #include "../../flamenco/events/fd_event_runtime.h"
43 : #include "../../disco/events/generated/fd_event_gen.h"
44 : #include "../../disco/repair/fd_repair_tile.h"
45 : #include "../../flamenco/runtime/fd_runtime.h"
46 : #include "../../flamenco/runtime/fd_runtime_stack_tmpl.h"
47 :
48 : #include "../../flamenco/runtime/sysvar/fd_sysvar_cache.h"
49 : #include "../../flamenco/runtime/sysvar/fd_sysvar_stake_history.h"
50 : #include "../../flamenco/runtime/sysvar/fd_sysvar_clock.h"
51 : #include "../../flamenco/runtime/sysvar/fd_sysvar_epoch_schedule.h"
52 : #include "../../flamenco/runtime/sysvar/fd_sysvar_rent.h"
53 : #include "../../flamenco/runtime/program/fd_precompiles.h"
54 : #include "../../flamenco/runtime/program/vote/fd_vote_state_versioned.h"
55 : #include "../../flamenco/runtime/program/vote/fd_vote_codec_tmpl.h"
56 : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
57 : #include "../../disco/events/fd_event_report.h"
58 : #include "../../disco/metrics/fd_metrics_sample.h"
59 :
60 : /* Replay concepts:
61 :
62 : - Blocks are aggregations of entries aka. microblocks which are
63 : groupings of txns and are constructed by the block producer (see
64 : fd_pack).
65 :
66 : - Entries are grouped into entry batches by the block producer (see
67 : fd_pack / fd_shredder).
68 :
69 : - Entry batches are divided into chunks known as shreds by the block
70 : producer (see fd_shredder).
71 :
72 : - Shreds are grouped into forward-error-correction sets (FEC sets) by
73 : the block producer (see fd_shredder).
74 :
75 : - Shreds are transmitted to the rest of the cluster via the Turbine
76 : protocol (see fd_shredder / fd_shred).
77 :
78 : - Once enough shreds within a FEC set are received to recover the
79 : entirety of the shred data encoded by that FEC set, the receiver
80 : can "complete" the FEC set (see fd_fec_resolver).
81 :
82 : - If shreds in the FEC set are missing such that it can't complete,
83 : the receiver can use the Repair protocol to request missing shreds
84 : in FEC set (see fd_repair).
85 :
86 : - The current Repair protocol does not support requesting coding
87 : shreds. As a result, some FEC sets might be actually complete
88 : (contain all data shreds). Repair currently hacks around this by
89 : forcing completion but the long-term solution is to add support for
90 : fec_repairing coding shreds via Repair.
91 :
92 : - FEC sets are delivered in partial-order to the Replay tile by the
93 : Repair tile. Currently Replay only supports replaying entry batches
94 : so FEC sets need to reassembled into an entry batch before they can
95 : be replayed. The new Dispatcher will change this by taking a FEC
96 : set as input instead. */
97 :
98 : /* Agave defines TIME_TO_COMPLETE_BLOCK_BROADCAST to adjust their slot
99 : end. Our own telemetry data of finishing pack => shredding last
100 : batch suggests roughly the same delay, so we mirror the constant. */
101 :
102 0 : #define FD_TIME_TO_COMPLETE_BROADCAST_NS (6000000L)
103 :
104 0 : #define IN_KIND_SNAP ( 0)
105 0 : #define IN_KIND_GENESIS ( 1)
106 0 : #define IN_KIND_IPECHO ( 2)
107 0 : #define IN_KIND_RESOLV ( 4)
108 0 : #define IN_KIND_MOTOR ( 5)
109 0 : #define IN_KIND_EXECRP ( 6)
110 0 : #define IN_KIND_REPAIR ( 7)
111 0 : #define IN_KIND_VOTOR ( 8)
112 0 : #define IN_KIND_RPC ( 9)
113 0 : #define IN_KIND_GOSSIP_OUT (10)
114 0 : #define IN_KIND_SNAPMK (11)
115 0 : #define IN_KIND_ADMIN (12)
116 :
117 : #define DEBUG_LOGGING 0
118 :
119 : /* The first bank that the replay tile produces either for genesis
120 : or the snapshot boot will always be at bank index 0. */
121 0 : #define FD_REPLAY_BOOT_BANK_IDX (0UL)
122 :
123 : /* wfs_resolve_leader_gate_auto resolves to true when
124 : wait_for_vote_to_start_leader is configured as auto, otherwise it
125 : passes the config value through. True keeps the leader gate armed,
126 : which is correct for a non-WFS boot and for NOOP. */
127 :
128 : static inline int
129 0 : wfs_resolve_leader_gate_auto( int wait_for_vote_to_start_leader ) {
130 0 : return fd_int_if( wait_for_vote_to_start_leader==FD_BOOLAU_AUTO,
131 0 : FD_BOOLAU_TRUE,
132 0 : wait_for_vote_to_start_leader );
133 0 : }
134 :
135 : static inline ulong
136 0 : fd_block_id_ele_get_idx( fd_block_id_ele_t * ele_arr, fd_block_id_ele_t * ele ) {
137 0 : return (ulong)(ele - ele_arr);
138 0 : }
139 :
140 : FD_FN_CONST static inline ulong
141 0 : scratch_align( void ) {
142 0 : return 128UL;
143 0 : }
144 : FD_FN_PURE static inline ulong
145 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
146 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
147 :
148 0 : ulong l = FD_LAYOUT_INIT;
149 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
150 0 : l = FD_LAYOUT_APPEND( l, fd_genesis_align(), fd_genesis_footprint( fd_genesis_account_max( tile->replay.genesis_max_message_size ) ) );
151 0 : l = FD_LAYOUT_APPEND( l, fd_runtime_stack_align(), fd_runtime_stack_footprint( FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS ) );
152 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_block_id_ele_t), sizeof(fd_block_id_ele_t) * tile->replay.max_live_slots );
153 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), sizeof(ulong) * tile->replay.max_live_slots );
154 0 : if( FD_UNLIKELY( tile->replay.report_runtime_diffs ) ) {
155 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_hash_t), sizeof(fd_hash_t) * FD_FEC_BLK_MAX * tile->replay.max_live_slots );
156 0 : l = FD_LAYOUT_APPEND( l, 8UL, FD_EVENT_RUNTIME_SLOT_DIFFS_FOOTPRINT * tile->replay.max_live_slots );
157 0 : }
158 0 : l = FD_LAYOUT_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
159 0 : l = FD_LAYOUT_APPEND( l, fd_bhm_map_align(), fd_bhm_map_footprint( chain_cnt ) );
160 0 : l = FD_LAYOUT_APPEND( l, fd_bhm_pool_align(), fd_bhm_pool_footprint( tile->replay.max_live_slots ) );
161 0 : l = FD_LAYOUT_APPEND( l, fd_bhm_dlist_align(), fd_bhm_dlist_footprint() );
162 0 : l = FD_LAYOUT_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
163 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->replay.max_live_slots, 0 ) );
164 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_reception_stats_t), sizeof(fd_reception_stats_t)*tile->replay.max_live_slots );
165 0 : l = FD_LAYOUT_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots, tile->replay.max_shreds_per_block, tile->replay.max_txn_per_slot ) );
166 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_pubkey_t), FD_SCHED_LTHASH_START_WRITES_MAX*sizeof(fd_pubkey_t) );
167 0 : l = FD_LAYOUT_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
168 0 : l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
169 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
170 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_event_block_completed_t), sizeof(fd_event_block_completed_t) );
171 0 : l = FD_LAYOUT_APPEND( l, fd_timing_slot_pool_align(), fd_timing_slot_pool_footprint( FD_REPLAY_TXN_TIMING_SLOTS ) );
172 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_replay_txn_timing_t), FD_REPLAY_TXN_TIMING_SLOTS*tile->replay.max_txn_per_slot*sizeof(fd_replay_txn_timing_t) );
173 0 : l = FD_LAYOUT_APPEND( l, alignof(ulong), tile->replay.max_live_slots*sizeof(ulong) );
174 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_replay_footer_end_t), tile->replay.max_live_slots*sizeof(fd_replay_footer_end_t) );
175 :
176 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
177 0 : l = FD_LAYOUT_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
178 0 : }
179 :
180 0 : l = FD_LAYOUT_FINI( l, scratch_align() );
181 :
182 0 : return l;
183 0 : }
184 :
185 : /* wait_info_healthy returns 1 if the replay tile is healthy: caught up
186 : and not falling behind turbine by more than 12 slots. */
187 :
188 : static inline int
189 : wait_info_healthy( int caught_up,
190 : ulong catch_up_max_fec_slot,
191 0 : ulong reset_slot ) {
192 0 : ulong turbine_slot = catch_up_max_fec_slot==ULONG_MAX ? 0UL : catch_up_max_fec_slot;
193 0 : ulong health_reset = reset_slot==ULONG_MAX ? 0UL : reset_slot;
194 0 : return caught_up && !( turbine_slot>health_reset && turbine_slot-health_reset>12UL );
195 0 : }
196 :
197 : /* query_next_leader_slot returns the next slot at or after from the
198 : one we lead, ULONG_MAX if the schedules hold none. */
199 :
200 : static ulong
201 : query_next_leader_slot( fd_replay_tile_t * ctx,
202 0 : ulong from ) {
203 0 : if( FD_UNLIKELY( ctx->next_leader_query_start==ULONG_MAX ||
204 0 : from<ctx->next_leader_query_start ||
205 0 : from>ctx->next_leader_query_slot ) ) {
206 0 : ctx->next_leader_query_start = from;
207 0 : ctx->next_leader_query_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, from, ctx->identity_pubkey );
208 0 : }
209 0 : return ctx->next_leader_query_slot;
210 0 : }
211 :
212 : /* refresh_epoch_info republishes the epoch fields wait_info exposes.
213 : Call wherever reset_slot moves. */
214 :
215 : static void
216 : refresh_epoch_info( fd_replay_tile_t * ctx,
217 : fd_bank_t const * bank,
218 0 : ulong slot ) {
219 0 : fd_epoch_schedule_t const * sched = &bank->f.epoch_schedule;
220 0 : ulong ep = fd_slot_to_epoch( sched, slot, NULL );
221 0 : ctx->epoch_end_slot = fd_epoch_slot0( sched, ep ) + fd_epoch_slot_cnt( sched, ep ) - 1UL;
222 0 : ctx->slots_per_epoch = sched->slots_per_epoch;
223 0 : ctx->ns_per_slot = bank->f.slot_params.ns_per_slot;
224 0 : }
225 :
226 : /* wait_info_publish writes the wait_info topology object so the wait
227 : command can read a consistent snapshot without touching metrics. */
228 :
229 : static void
230 0 : wait_info_publish( fd_replay_tile_t * ctx ) {
231 0 : if( FD_UNLIKELY( !ctx->wait_info ) ) return; /* not in this topology */
232 :
233 : /* Query the schedule, not ctx->next_leader_slot: there ULONG_MAX
234 : means already consumed, but the wire maps it to 0, which the gap
235 : check reads as no upcoming leader slot and passes. */
236 :
237 0 : ulong highwater = fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot );
238 0 : ulong from = fd_ulong_max( ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot+1UL,
239 0 : fd_ulong_if( !highwater, 0UL, highwater+1UL ) );
240 0 : int next_leader_known = !!fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, from );
241 0 : ulong nl = query_next_leader_slot( ctx, from );
242 0 : ulong next_leader = nl==ULONG_MAX ? 0UL : nl;
243 0 : int healthy = wait_info_healthy( ctx->caught_up, ctx->catch_up_max_fec_slot, ctx->reset_slot );
244 :
245 0 : fd_wait_info_write_begin( ctx->wait_info );
246 0 : ctx->wait_info->info.caught_up = healthy;
247 0 : ctx->wait_info->info.reset_slot = ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot;
248 0 : ctx->wait_info->info.tip_slot = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
249 0 : ctx->wait_info->info.leader_highwater_slot = highwater;
250 0 : ctx->wait_info->info.next_leader_known = next_leader_known;
251 0 : ctx->wait_info->info.next_leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot : next_leader;
252 0 : ctx->wait_info->info.leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot : 0UL;
253 0 : ctx->wait_info->info.epoch_end_slot = ctx->epoch_end_slot;
254 0 : ctx->wait_info->info.slots_per_epoch = ctx->slots_per_epoch;
255 0 : ctx->wait_info->info.ns_per_slot = ctx->ns_per_slot;
256 0 : ctx->wait_info->info.delinquent_known = ctx->delinquent_known;
257 0 : ctx->wait_info->info.delinquent_stake_lamports = ctx->delinquent_stake_lamports;
258 0 : ctx->wait_info->info.cluster_active_stake_lamports = ctx->cluster_active_stake_lamports;
259 0 : ctx->wait_info->info.snap_active = ctx->snapmk.active;
260 0 : ctx->wait_info->info.snap_finished_full = ctx->snapmk.snap_finished_full;
261 0 : ctx->wait_info->info.snap_finished_incr = ctx->snapmk.snap_finished_incr;
262 0 : ctx->wait_info->info.snap_full_interval_blocks = ctx->snapmk.full_interval_blocks;
263 0 : ctx->wait_info->info.snap_incr_interval_blocks = ctx->snapmk.incremental_interval_blocks;
264 0 : ctx->wait_info->info.snap_produced_incr_cnt = ctx->snapmk.snap_produced_incr_cnt;
265 0 : fd_wait_info_write_end( ctx->wait_info );
266 0 : }
267 :
268 : static inline void
269 0 : metrics_write( fd_replay_tile_t * ctx ) {
270 0 : fd_accdb_flush_metrics( ctx->accdb );
271 :
272 0 : ulong store_fec_spilled = 0UL;
273 0 : ulong store_fec_spill_bytes= 0UL;
274 0 : ulong store_fec_spill_reads= 0UL;
275 0 : ulong store_fec_spill_read_bytes = 0UL;
276 0 : ulong store_fec_cache_used = 0UL;
277 0 : ulong store_fec_cache_max = 0UL;
278 0 : ulong store_fec_cache_pinned = 0UL;
279 0 : if( FD_LIKELY( ctx->store ) ) {
280 0 : store_fec_spilled = FD_VOLATILE_CONST( ctx->store->fec_spill_cnt );
281 0 : store_fec_spill_bytes = FD_VOLATILE_CONST( ctx->store->fec_spill_bytes );
282 0 : store_fec_spill_reads = FD_VOLATILE_CONST( ctx->store->fec_spill_read_cnt );
283 0 : store_fec_spill_read_bytes = FD_VOLATILE_CONST( ctx->store->fec_spill_read_bytes );
284 0 : store_fec_cache_pinned = FD_VOLATILE_CONST( ctx->store->cache_pinned_cnt );
285 :
286 0 : store_fec_cache_max = FD_VOLATILE_CONST( ctx->store->cache_slot_cnt );
287 0 : ulong store_fec_cache_free = fd_ulong_min( FD_VOLATILE_CONST( ctx->store->cache_free_cnt ), store_fec_cache_max );
288 0 : store_fec_cache_used = store_fec_cache_max - store_fec_cache_free;
289 0 : }
290 :
291 0 : FD_MHIST_COPY( REPLAY, STORE_QUERY_DURATION_NANOS, ctx->metrics.store_query_work );
292 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_QUERIED, ctx->metrics.store_query_cnt );
293 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_QUERY_MISSED, ctx->metrics.store_query_missing_cnt );
294 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_SPILLED, store_fec_spilled );
295 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_SPILLED_BYTES, store_fec_spill_bytes );
296 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_SPILL_READ, store_fec_spill_reads );
297 0 : FD_MCNT_SET ( REPLAY, STORE_FEC_SPILL_READ_BYTES, store_fec_spill_read_bytes );
298 0 : FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_USED, store_fec_cache_used );
299 0 : FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_CAPACITY, store_fec_cache_max );
300 0 : FD_MGAUGE_SET( REPLAY, STORE_FEC_CACHE_PINNED, store_fec_cache_pinned );
301 :
302 0 : fd_txncache_metrics_t txncache_metrics[1];
303 0 : fd_txncache_metrics_snap( ctx->txncache, txncache_metrics );
304 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_PAGE_MISS, txncache_metrics->page_miss_cnt );
305 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_PAGE_SPILLED, txncache_metrics->page_spilled_cnt );
306 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_PAGE_SPILL_READ, txncache_metrics->page_spill_read_cnt );
307 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_FRAME_EVICTED, txncache_metrics->frame_evicted_cnt );
308 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_SPILL_DURATION_NANOS, txncache_metrics->spill_ticks );
309 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_LOCK_READ_WAIT_DURATION_NANOS, txncache_metrics->lock_read_wait_ticks );
310 0 : FD_MCNT_SET ( REPLAY, TXNCACHE_LOCK_WRITE_WAIT_DURATION_NANOS, txncache_metrics->lock_write_wait_ticks );
311 0 : FD_MGAUGE_SET( REPLAY, TXNCACHE_PAGE_USED, txncache_metrics->page_used );
312 0 : FD_MGAUGE_SET( REPLAY, TXNCACHE_PAGE_CAPACITY, txncache_metrics->page_capacity );
313 0 : FD_MGAUGE_SET( REPLAY, TXNCACHE_FRAME_USED, txncache_metrics->frame_used );
314 0 : FD_MGAUGE_SET( REPLAY, TXNCACHE_FRAME_CAPACITY, txncache_metrics->frame_capacity );
315 :
316 0 : fd_stake_delegations_metrics_t stake_delegations_metrics[1];
317 0 : fd_stake_delegations_metrics_snap( fd_banks_stake_delegations_root_query( ctx->banks ), stake_delegations_metrics );
318 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_ROOT_USED, stake_delegations_metrics->root_used );
319 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_ROOT_CAPACITY, stake_delegations_metrics->root_capacity );
320 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_ROOT_DISK_COUNT, stake_delegations_metrics->root_disk_cnt );
321 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_DELTA_USED, stake_delegations_metrics->delta_used );
322 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_DELTA_CAPACITY, stake_delegations_metrics->delta_capacity );
323 0 : FD_MGAUGE_SET( REPLAY, STAKE_DELEGATION_DELTA_DISK_COUNT, stake_delegations_metrics->delta_disk_cnt );
324 0 : FD_MCNT_SET ( REPLAY, STAKE_DELEGATION_ROOT_SPILLED, stake_delegations_metrics->root_spilled_cnt );
325 0 : FD_MCNT_SET ( REPLAY, STAKE_DELEGATION_DELTA_SPILLED, stake_delegations_metrics->delta_spilled_cnt );
326 0 : FD_MCNT_SET ( REPLAY, STAKE_DELEGATION_DISK_READ, stake_delegations_metrics->disk_read_cnt );
327 0 : FD_MCNT_SET ( REPLAY, STAKE_DELEGATION_DISK_WRITTEN, stake_delegations_metrics->disk_written_cnt );
328 0 : FD_MCNT_SET ( REPLAY, STAKE_DELEGATION_DISK_DURATION_NANOS, stake_delegations_metrics->disk_ticks );
329 :
330 0 : FD_MGAUGE_SET( REPLAY, ROOT_SLOT, ctx->consensus_root_slot==ULONG_MAX ? 0UL : ctx->consensus_root_slot );
331 0 : ulong leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot : 0UL;
332 :
333 0 : if( FD_LIKELY( ctx->leader_bank ) ) {
334 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, leader_slot );
335 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, leader_slot );
336 0 : } else {
337 0 : FD_MGAUGE_SET( REPLAY, NEXT_LEADER_SLOT, ctx->next_leader_slot==ULONG_MAX ? 0UL : ctx->next_leader_slot );
338 0 : FD_MGAUGE_SET( REPLAY, LEADER_SLOT, 0UL );
339 0 : }
340 0 : FD_MGAUGE_SET( REPLAY, RESET_SLOT, ctx->reset_slot==ULONG_MAX ? 0UL : ctx->reset_slot );
341 0 : if( FD_UNLIKELY( ctx->reset_slot!=ULONG_MAX && ctx->consensus_root_slot!=ULONG_MAX ) ) {
342 0 : FD_MGAUGE_SET( REPLAY, ROOT_SLOT_DISTANCE, fd_ulong_sat_sub( ctx->reset_slot, ctx->consensus_root_slot ) );
343 0 : }
344 0 : FD_MGAUGE_SET( REPLAY, LAST_REWARDED_VOTE_SLOT, fd_ulong_if( ctx->metrics.voted_slot==ULONG_MAX, 0UL, ctx->metrics.voted_slot ) );
345 :
346 0 : FD_MGAUGE_SET( REPLAY, BANK_USED, fd_banks_pool_used_cnt( ctx->banks ) );
347 :
348 0 : FD_MCNT_SET( REPLAY, BANK_FROZEN, ctx->metrics.slots_total );
349 0 : FD_MGAUGE_SET( REPLAY, CHAIN_TXN_HEIGHT, ctx->metrics.transactions_total );
350 :
351 0 : FD_MGAUGE_SET( REPLAY, LAST_REASSEMBLY_DEQUEUED_SLOT, ctx->metrics.reasm_latest_slot );
352 0 : FD_MGAUGE_SET( REPLAY, LAST_REASSEMBLY_DEQUEUED_FEC_INDEX, ctx->metrics.reasm_latest_fec_idx );
353 :
354 0 : fd_sched_metrics_write( ctx->sched, ctx->clock );
355 :
356 0 : FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_SCHED_FULL, ctx->metrics.sched_full );
357 0 : FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_LEADER_FREEZE_WAIT, ctx->metrics.leader_bid_wait );
358 0 : FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_BANK_FULL, ctx->metrics.banks_full );
359 0 : FD_MCNT_SET( REPLAY, FEC_POLL_SKIPPED_LANE_WAIT, ctx->metrics.lane_wait );
360 0 : FD_MCNT_SET( REPLAY, FEC_PARENT_MISSING_DROPPED, ctx->metrics.parent_unavailable );
361 0 : FD_MCNT_SET( REPLAY, LEADER_WINDOW_ALREADY_PRODUCED, ctx->metrics.leader_slot_already_produced );
362 0 : FD_MCNT_SET( REPLAY, LEADER_WINDOW_SUPERSEDED, ctx->metrics.leader_window_superseded );
363 0 : FD_MCNT_SET( REPLAY, FEC_ROOTED_PAST_SKIPPED, ctx->metrics.fec_rooted_past );
364 0 : FD_MCNT_SET( REPLAY, ROOT_ADVANCE_DEFERRED, ctx->metrics.storage_root_behind );
365 :
366 0 : fd_progcache_admin_metrics_t const * pcm = &fd_progcache_admin_metrics_g;
367 0 : FD_MCNT_SET( REPLAY, PROGCACHE_ROOTED, pcm->root_cnt );
368 :
369 : /* Shared metrics for the program cache */
370 0 : ulong pc_class_used[ FD_PROGCACHE_CACHE_CLASS_CNT ];
371 0 : ulong pc_class_max [ FD_PROGCACHE_CACHE_CLASS_CNT ];
372 0 : fd_progcache_cache_class_occupancy( ctx->progcache, pc_class_used, pc_class_max );
373 0 : FD_MGAUGE_ENUM_COPY( REPLAY, PROGCACHE_CLASS_USED, pc_class_used );
374 0 : FD_MGAUGE_ENUM_COPY( REPLAY, PROGCACHE_CLASS_CAPACITY, pc_class_max );
375 :
376 0 : FD_ACCDB_METRICS_WRITE( REPLAY, fd_accdb_metrics( ctx->accdb ) );
377 0 : }
378 :
379 : static void
380 : metrics_trigger( fd_replay_tile_t * ctx,
381 : uint reason,
382 0 : ulong slot ) {
383 0 : FD_COMPILER_MFENCE();
384 0 : metrics_write( ctx );
385 0 : FD_COMPILER_MFENCE();
386 0 : fd_event_metrics_trigger( reason, slot, ULONG_MAX );
387 0 : }
388 :
389 : /* Agave: delinquent once the last vote is this far behind.
390 : https://github.com/anza-xyz/agave/blob/v4.2.1/rpc-client-types/src/request.rs#L166 */
391 :
392 0 : #define DELINQUENT_VALIDATOR_SLOT_DISTANCE 128UL
393 :
394 : /* Sampling interval for the O(vote accounts) stake walk, a quarter of
395 : DELINQUENT_VALIDATOR_SLOT_DISTANCE so the figure cannot lag far
396 : behind the threshold it feeds. Stamping is not gated by it: a cert
397 : not stamped when it arrives is lost. */
398 :
399 0 : #define DELINQUENT_SAMPLE_SLOTS 32UL
400 :
401 : static ushort
402 : replay_voter_rank( fd_replay_tile_t * ctx,
403 : fd_bank_t * bank,
404 0 : ulong epoch ) {
405 0 : if( FD_LIKELY( ctx->voter_rank_cached && ctx->voter_rank_epoch==epoch ) ) return ctx->voter_rank;
406 :
407 0 : ushort fork_id = bank->vote_stakes_fork_id;
408 0 : ulong fork_epoch = fd_vote_stakes_fork_epoch( fd_bank_vote_stakes( bank ), fork_id );
409 0 : int iter_kind = FD_VOTE_STAKES_ITER_T_2;
410 0 : if( FD_UNLIKELY( epoch!=fork_epoch ) ) {
411 0 : if( FD_UNLIKELY( !fork_epoch || epoch!=fork_epoch-1UL ) ) return USHORT_MAX;
412 0 : iter_kind = FD_VOTE_STAKES_ITER_T_3;
413 0 : }
414 :
415 0 : ushort rank = USHORT_MAX;
416 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
417 0 : uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
418 0 : for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, iter_kind, iter_mem );
419 0 : !fd_vote_stakes_iter_done( vote_stakes, fork_id, iter_kind, iter );
420 0 : fd_vote_stakes_iter_next( vote_stakes, fork_id, iter_kind, iter ) ) {
421 0 : fd_vote_stakes_ele_t ele[1];
422 0 : fd_vote_stakes_iter_ele( vote_stakes, fork_id, iter_kind, iter, ele );
423 0 : if( FD_UNLIKELY( fd_pubkey_eq( &ele->node_account, ctx->identity_pubkey ) ) ) { rank = ele->rank; break; }
424 0 : }
425 0 : ctx->voter_rank_cached = 1;
426 0 : ctx->voter_rank_epoch = epoch;
427 0 : ctx->voter_rank = rank;
428 0 : return rank;
429 0 : }
430 :
431 : /* fd_rank_is_current returns 1 if the rank's last observed vote is
432 : recent enough. */
433 :
434 : static inline int
435 : fd_rank_is_current( ulong last_settled,
436 0 : ulong rank_last_voted ) {
437 0 : return fd_ulong_sat_sub( last_settled, rank_last_voted )<DELINQUENT_VALIDATOR_SLOT_DISTANCE;
438 0 : }
439 :
440 : /* fd_delinquent_epoch_roll clears the rank table when the epoch
441 : advances and raises the settled watermark. Ranks are remapped across
442 : the rollover, so the previous epoch's figure no longer describes the
443 : current stake set and reads unknown until a sample repopulates the
444 : table. The watermark cannot regress. */
445 :
446 : static void
447 : fd_delinquent_epoch_roll( fd_replay_tile_t * ctx,
448 : ulong reward_slot,
449 0 : ulong reward_epoch ) {
450 0 : if( FD_UNLIKELY( ctx->ag_last_voted_epoch==ULONG_MAX ||
451 0 : reward_epoch>ctx->ag_last_voted_epoch ) ) {
452 0 : fd_memset( ctx->ag_last_voted, 0, sizeof(ctx->ag_last_voted) );
453 0 : ctx->ag_last_voted_epoch = reward_epoch;
454 0 : ctx->delinquent_known = 0;
455 0 : ctx->ag_stamp_since = ULONG_MAX;
456 0 : }
457 0 : ctx->ag_last_settled = fd_ulong_max( ctx->ag_last_settled, reward_slot );
458 0 : }
459 :
460 : /* fd_update_delinquent stamps the reward cert's signers into
461 : ag_last_voted and recomputes delinquent stake. */
462 :
463 : static void
464 : fd_update_delinquent( fd_replay_tile_t * ctx,
465 : fd_bank_t const * bank,
466 : ulong reward_slot,
467 : ulong reward_epoch,
468 0 : fd_bls_set_t const * reward_set ) {
469 0 : if( FD_UNLIKELY( !ctx->wait_info ) ) return; /* not in this topology */
470 :
471 0 : ushort fork_id = bank->vote_stakes_fork_id;
472 0 : ulong fork_epoch = fd_vote_stakes_fork_epoch( fd_bank_vote_stakes( bank ), fork_id );
473 :
474 : /* Roll on the fork's epoch, not the cert's: the cert trails the bank
475 : by FD_NUM_SLOTS_FOR_REWARD slots, so it still names the old epoch
476 : over the first slots of a new one. */
477 0 : fd_delinquent_epoch_roll( ctx, ctx->ag_last_settled, fork_epoch );
478 :
479 : /* Only T-2 exposes stake and is_valid. */
480 0 : if( FD_UNLIKELY( reward_epoch!=fork_epoch ) ) return;
481 :
482 : /* Ranks are epoch-scoped, and forks can complete an epoch-E block
483 : after an epoch-E+1 one. */
484 0 : if( FD_UNLIKELY( ctx->ag_last_voted_epoch!=ULONG_MAX &&
485 0 : reward_epoch<ctx->ag_last_voted_epoch ) ) return;
486 :
487 : /* Raise the watermark, so stamps age out as certs settle. */
488 0 : fd_delinquent_epoch_roll( ctx, reward_slot, reward_epoch );
489 :
490 : /* An empty cert is absence of evidence, so it stamps nothing. With no
491 : figure yet there is nothing to age either, but once one is known the
492 : walk below must still run: the roll above advanced ag_last_settled,
493 : so existing stamps age out even while certs are absent. */
494 0 : if( FD_UNLIKELY( fd_bls_set_is_null( reward_set ) && !ctx->delinquent_known ) ) return;
495 :
496 0 : if( FD_UNLIKELY( ctx->ag_stamp_since==ULONG_MAX ) ) ctx->ag_stamp_since = reward_slot;
497 :
498 : /* Stamp straight off the cert: O(signers), no vote stakes walk. No
499 : need to filter: entries the walk skips are never read. */
500 0 : for( ulong rank=fd_bls_set_const_iter_init( reward_set );
501 0 : !fd_bls_set_const_iter_done( rank );
502 0 : rank=fd_bls_set_const_iter_next( reward_set, rank ) ) {
503 0 : if( FD_UNLIKELY( rank>=FD_VOTOR_VAT_MAX ) ) break; /* FD_BLS_SET_MAX is sized separately */
504 0 : ctx->ag_last_voted[ rank ] = fd_ulong_max( ctx->ag_last_voted[ rank ], reward_slot );
505 0 : }
506 :
507 0 : if( FD_LIKELY( ctx->delinquent_known &&
508 0 : reward_slot<ctx->delinquent_sample_slot+DELINQUENT_SAMPLE_SLOTS ) ) return;
509 0 : if( FD_UNLIKELY( reward_slot<ctx->ag_stamp_since+DELINQUENT_SAMPLE_SLOTS ) ) return;
510 : /* Slots arrive out of order across forks, so hold the watermark. */
511 0 : ctx->delinquent_sample_slot = fd_ulong_max( ctx->delinquent_sample_slot, reward_slot );
512 :
513 0 : fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
514 0 : ulong delinquent = 0UL;
515 0 : ulong total = 0UL;
516 :
517 0 : uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
518 0 : for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter_mem );
519 0 : !fd_vote_stakes_iter_done( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter );
520 0 : fd_vote_stakes_iter_next( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter ) ) {
521 0 : fd_vote_stakes_ele_t ele[1];
522 0 : fd_vote_stakes_iter_ele( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2, iter, ele );
523 0 : ulong stake = ele->stake;
524 0 : ushort rank = ele->rank;
525 0 : if( FD_UNLIKELY( !ele->is_valid || rank==FD_VOTE_STAKES_ALPENGLOW_RANK_NULL ) ) continue;
526 0 : FD_TEST( rank<FD_VOTOR_VAT_MAX );
527 :
528 0 : total += stake;
529 0 : if( FD_UNLIKELY( !fd_rank_is_current( ctx->ag_last_settled, ctx->ag_last_voted[ rank ] ) ) ) {
530 0 : delinquent += stake;
531 0 : }
532 0 : }
533 :
534 0 : ctx->delinquent_stake_lamports = delinquent;
535 0 : ctx->cluster_active_stake_lamports = total;
536 :
537 : /* Over-counts delinquency, so a partial union blocks rather than
538 : passes. */
539 0 : ctx->delinquent_known = 1;
540 0 : }
541 :
542 : static int
543 : replay_reward_cert_voted( fd_replay_tile_t * ctx,
544 : fd_bank_t * bank,
545 : ushort * rank_out,
546 0 : ushort * count_out ) {
547 0 : *rank_out = USHORT_MAX;
548 0 : *count_out = USHORT_MAX;
549 0 : if( FD_UNLIKELY( bank->f.slot<FD_NUM_SLOTS_FOR_REWARD ) ) return 0;
550 :
551 0 : ulong reward_slot = bank->f.slot-FD_NUM_SLOTS_FOR_REWARD;
552 0 : ulong reward_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, reward_slot, NULL );
553 0 : ushort rank = replay_voter_rank( ctx, bank, reward_epoch );
554 0 : *rank_out = rank;
555 :
556 0 : fd_block_footer_t const * footer = bank==ctx->leader_bank ? ctx->leader_footer : fd_sched_get_footer( ctx->sched, bank->idx );
557 0 : if( FD_UNLIKELY( !footer ) ) return 0;
558 :
559 : /* A validator included in both reward certificates is counted once. */
560 0 : fd_bls_set_t reward_set[ fd_bls_set_word_cnt ];
561 0 : fd_bls_set_null( reward_set );
562 0 : if( FD_UNLIKELY( footer->has_skip_reward_cert ) ) fd_bls_set_union( reward_set, reward_set, footer->skip_reward_cert.signer_set );
563 0 : if( FD_LIKELY( footer->has_notar_reward_cert ) ) fd_bls_set_union( reward_set, reward_set, footer->notar_reward_cert.signer_set );
564 0 : *count_out = (ushort)fd_bls_set_cnt( reward_set );
565 :
566 0 : fd_update_delinquent( ctx, bank, reward_slot, reward_epoch, reward_set );
567 :
568 0 : if( FD_UNLIKELY( rank==USHORT_MAX ) ) return 0;
569 :
570 : /* bits at or past the cert's nbits are left clear at decode, so the
571 : set test alone bounds the rank */
572 0 : int in_cert = fd_bls_set_test( reward_set, rank );
573 :
574 0 : if( FD_UNLIKELY( in_cert && ( ctx->metrics.voted_slot==ULONG_MAX || reward_slot>ctx->metrics.voted_slot ) ) ) ctx->metrics.voted_slot = reward_slot;
575 0 : return in_cert;
576 0 : }
577 :
578 : static void
579 : publish_epoch_info( fd_replay_tile_t * ctx,
580 : fd_stem_context_t * stem,
581 : fd_bank_t * bank,
582 0 : int next_epoch ) {
583 0 : fd_epoch_schedule_t const * schedule = &bank->f.epoch_schedule;
584 0 : ulong epoch = fd_slot_to_epoch( schedule, bank->f.slot, NULL ) + fd_ulong_if( next_epoch, 1UL, 0UL );
585 :
586 0 : fd_features_t const * features = &bank->f.features;
587 :
588 0 : fd_runtime_stack_t * runtime_stack = ctx->runtime_stack;
589 :
590 0 : fd_epoch_info_msg_t * epoch_info_msg = fd_chunk_to_laddr( ctx->epoch_out->mem, ctx->epoch_out->chunk );
591 :
592 0 : epoch_info_msg->staked_vote_cnt = next_epoch ? runtime_stack->epoch_weights.next_stake_weights_cnt : runtime_stack->epoch_weights.stake_weights_cnt;
593 0 : epoch_info_msg->staked_id_cnt = next_epoch ? runtime_stack->epoch_weights.next_id_weights_cnt : runtime_stack->epoch_weights.id_weights_cnt;
594 0 : epoch_info_msg->epoch_schedule = *schedule;
595 0 : epoch_info_msg->features = *features;
596 0 : epoch_info_msg->epoch = epoch;
597 0 : epoch_info_msg->start_slot = fd_epoch_slot0( schedule, epoch );
598 0 : epoch_info_msg->slot_cnt = fd_epoch_slot_cnt( schedule, epoch );
599 0 : epoch_info_msg->ns_per_slot = fd_slot_params_at_slot( bank, epoch_info_msg->start_slot ).ns_per_slot;
600 :
601 0 : fd_vote_stake_weight_t * stake_weights = fd_type_pun( epoch_info_msg + 1 );
602 0 : fd_vote_stake_weight_t * src_stake_weights = next_epoch ? runtime_stack->epoch_weights.next_stake_weights : runtime_stack->epoch_weights.stake_weights;
603 0 : memcpy( stake_weights, src_stake_weights, epoch_info_msg->staked_vote_cnt * sizeof(fd_vote_stake_weight_t) );
604 :
605 0 : fd_stake_weight_t * id_weights = fd_epoch_info_msg_id_weights( epoch_info_msg );
606 0 : fd_stake_weight_t * src_id_weights = next_epoch ? runtime_stack->epoch_weights.next_id_weights : runtime_stack->epoch_weights.id_weights;
607 0 : fd_memcpy( id_weights, src_id_weights, epoch_info_msg->staked_id_cnt * sizeof(fd_stake_weight_t) );
608 :
609 0 : ulong epoch_info_sz = fd_epoch_info_msg_sz( epoch_info_msg->staked_vote_cnt, epoch_info_msg->staked_id_cnt );
610 0 : ulong epoch_info_sig = 4UL;
611 : /* epoch_info_sz can exceed USHORT_MAX, consumers derive it from the
612 : message */
613 0 : fd_stem_publish( stem, ctx->epoch_out->idx, epoch_info_sig, ctx->epoch_out->chunk, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
614 0 : ctx->epoch_out->chunk = fd_dcache_compact_next( ctx->epoch_out->chunk, epoch_info_sz, ctx->epoch_out->chunk0, ctx->epoch_out->wmark );
615 :
616 0 : fd_multi_epoch_leaders_epoch_msg_init( ctx->mleaders, epoch_info_msg );
617 0 : fd_multi_epoch_leaders_epoch_msg_fini( ctx->mleaders );
618 :
619 0 : ctx->next_leader_query_start = ULONG_MAX;
620 0 : }
621 :
622 : static void
623 : publish_replay_out( fd_replay_tile_t * ctx,
624 : fd_stem_context_t * stem,
625 : ulong sig,
626 0 : ulong sz ) {
627 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
628 0 : if( FD_UNLIKELY( sig!=REPLAY_SIG_TXN_EXECUTED && sig!=REPLAY_SIG_SLOT_STATES ) ) {
629 0 : fd_replay_out_link_t * out = sig==REPLAY_SIG_MISSING_FEC ? ctx->rotor_out : ctx->slot_out;
630 0 : if( FD_LIKELY( out->idx!=ULONG_MAX ) ) { /* backtest has no rotor */
631 0 : fd_memcpy( fd_chunk_to_laddr( out->mem, out->chunk ), fd_chunk_to_laddr_const( ctx->replay_out->mem, ctx->replay_out->chunk ), sz );
632 0 : fd_stem_publish( stem, out->idx, sig, out->chunk, sz, 0UL, 0UL, tspub );
633 0 : out->chunk = fd_dcache_compact_next( out->chunk, sz, out->chunk0, out->wmark );
634 0 : }
635 0 : }
636 0 : fd_stem_publish( stem, ctx->replay_out->idx, sig, ctx->replay_out->chunk, sz, 0UL, 0UL, tspub );
637 0 : ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sz, ctx->replay_out->chunk0, ctx->replay_out->wmark );
638 0 : }
639 :
640 : /**********************************************************************/
641 : /* Transaction execution state machine helpers */
642 : /**********************************************************************/
643 :
644 : static inline void
645 : timing_slot_release( fd_replay_tile_t * ctx,
646 0 : ulong bank_idx ) {
647 0 : ulong tslot = ctx->timing_slot_of_bank[ bank_idx ];
648 0 : if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
649 0 : fd_timing_slot_pool_idx_release( ctx->timing_slot_pool, tslot );
650 0 : ctx->timing_slot_of_bank[ bank_idx ] = fd_timing_slot_pool_idx_null( ctx->timing_slot_pool );
651 0 : }
652 0 : }
653 :
654 : static void
655 : replay_block_start( fd_replay_tile_t * ctx,
656 : ulong bank_idx,
657 : ulong parent_bank_idx,
658 : ulong slot,
659 0 : int lthash_inband ) {
660 0 : long before = fd_clock_tile_now( ctx->clock );
661 :
662 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
663 0 : FD_CHECK_CRIT( bank, "invariant violation: bank is NULL" );
664 0 : FD_CHECK_CRIT( bank->state==FD_BANK_STATE_INIT, "invariant violation: bank is not in correct state" );
665 :
666 0 : bank->preparation_begin_nanos = before;
667 :
668 0 : FD_TEST( ctx->timing_slot_of_bank[ bank_idx ]==fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) );
669 0 : if( FD_LIKELY( fd_timing_slot_pool_free( ctx->timing_slot_pool ) ) ) {
670 0 : ulong tslot = fd_timing_slot_pool_idx_acquire( ctx->timing_slot_pool );
671 0 : fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot )->cnt = 0UL;
672 0 : ctx->timing_slot_of_bank[ bank_idx ] = tslot;
673 0 : }
674 :
675 0 : fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
676 0 : FD_CHECK_CRIT( parent_bank, "invariant violation: parent bank is NULL" );
677 0 : FD_CHECK_CRIT( parent_bank->state==FD_BANK_STATE_FROZEN || parent_bank->state==FD_BANK_STATE_PRUNABLE, "invariant violation: parent bank is not in correct state" );
678 :
679 : /* Clone the bank from the parent. We must special case the first
680 : slot that is executed as the snapshot does not provide a parent
681 : block id. */
682 :
683 0 : bank = fd_banks_clone_from_parent( ctx->banks, bank_idx );
684 0 : if( FD_UNLIKELY( !bank ) ) {
685 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", bank_idx ));
686 0 : }
687 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, slot );
688 0 : FD_TEST( leader );
689 0 : bank->f.slot = slot;
690 0 : bank->leader = *leader;
691 0 : bank->txncache_fork_id = fd_txncache_attach_child ( ctx->txncache, parent_bank->txncache_fork_id );
692 0 : bank->progcache_fork_id = fd_progcache_attach_child( ctx->progcache, parent_bank->progcache_fork_id );
693 0 : bank->accdb_fork_id = fd_accdb_attach_child ( ctx->accdb, parent_bank->accdb_fork_id );
694 0 : bank->parent_accdb_fork_id = parent_bank->accdb_fork_id;
695 :
696 0 : ulong new_epoch = fd_slot_to_epoch( &parent_bank->f.epoch_schedule, slot, NULL );
697 0 : ulong root_epoch = fd_slot_to_epoch( &parent_bank->f.epoch_schedule, ctx->published_root_slot, NULL );
698 0 : if( FD_UNLIKELY( new_epoch>root_epoch+1UL ) ) {
699 0 : FD_LOG_CRIT(( "firedancer replay does not support replaying more than one epoch ahead of the current root" ));
700 0 : }
701 :
702 0 : FD_TEST( bank->lthash_mode==FD_BANK_LTHASH_MODE_INBAND );
703 0 : if( FD_LIKELY( !lthash_inband ) ) {
704 0 : ctx->lthash_rec->cnt = 0UL;
705 0 : bank->lthash_rec = ctx->lthash_rec;
706 0 : bank->lthash_mode = FD_BANK_LTHASH_MODE_OOB_RECORD;
707 0 : }
708 :
709 : /* Update required runtime state and handle potential boundary. */
710 :
711 0 : int is_epoch_boundary = 0;
712 0 : fd_runtime_block_execute_prepare( ctx->banks, bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
713 0 : FD_CHECK_CRIT( !is_epoch_boundary || FD_FEATURE_ACTIVE_BANK( bank, alpenglow ), "alpenglow feature is not active" );
714 :
715 0 : if( FD_LIKELY( !lthash_inband ) ) {
716 0 : fd_sched_block_start_writes( ctx->sched, bank_idx, fd_type_pun_const( ctx->lthash_rec->keys ), ctx->lthash_rec->cnt );
717 0 : bank->lthash_rec = NULL;
718 0 : bank->lthash_mode = FD_BANK_LTHASH_MODE_OOB_SKIP;
719 0 : }
720 :
721 0 : ulong max_tick_height;
722 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( parent_bank->f.ticks_per_slot, slot, &max_tick_height ) ) ) {
723 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, parent_bank->f.ticks_per_slot ));
724 0 : }
725 0 : bank->f.max_tick_height = max_tick_height;
726 : /* in alpenglow, we expect only one tick per block. Instead of
727 : adjusting max tick height, we match agave behavior by setting
728 : tick height to max tick height - 1. These fields must stay in
729 : line with agave behavior. */
730 0 : bank->f.tick_height = bank->f.max_tick_height - 1UL;
731 0 : fd_sched_set_poh_params( ctx->sched, bank->idx, bank->f.tick_height, bank->f.max_tick_height, &parent_bank->f.poh );
732 0 : fd_sched_set_flh_active( ctx->sched, bank->idx, FD_FEATURE_ACTIVE_BANK( bank, alpenglow_fast_leader_handover ) );
733 :
734 0 : FD_LOG_DEBUG(( "replay_block_start: bank_idx=%lu slot=%lu parent_bank_idx=%lu", bank_idx, slot, parent_bank_idx ));
735 0 : }
736 :
737 : static void
738 0 : cost_tracker_snap( fd_bank_t * bank, fd_replay_slot_completed_t * slot_info ) {
739 0 : if( FD_LIKELY( bank->cost_tracker_fork_id!=USHORT_MAX ) ) {
740 0 : fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
741 0 : if( FD_UNLIKELY( cost_tracker->block_cost_limit==0UL ) ) {
742 0 : memset( &slot_info->cost_tracker, -1 /* ULONG_MAX */, sizeof(slot_info->cost_tracker) );
743 0 : } else {
744 0 : slot_info->cost_tracker.block_cost = cost_tracker->block_cost;
745 0 : slot_info->cost_tracker.allocated_accounts_data_size = cost_tracker->allocated_accounts_data_size;
746 0 : slot_info->cost_tracker.block_cost_limit = cost_tracker->block_cost_limit;
747 0 : slot_info->cost_tracker.account_cost_limit = cost_tracker->account_cost_limit;
748 0 : }
749 0 : fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
750 0 : } else {
751 0 : memset( &slot_info->cost_tracker, -1 /* ULONG_MAX */, sizeof(slot_info->cost_tracker) );
752 0 : }
753 0 : slot_info->cost_tracker.pool_idx = fd_ulong_if( bank->cost_tracker_fork_id==USHORT_MAX, ULONG_MAX, bank->cost_tracker_fork_id );
754 0 : }
755 :
756 : static int
757 0 : sched_dead_reason_to_event( int sched_reason ) {
758 0 : switch( sched_reason ) {
759 0 : case FD_SCHED_DEAD_REASON_UNPARSEABLE_CONTENT: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_UNPARSEABLE_CONTENT;
760 0 : case FD_SCHED_DEAD_REASON_SHORT_BLOCK: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SHORT_BLOCK;
761 0 : case FD_SCHED_DEAD_REASON_TOO_MANY_TXNS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_TXNS;
762 0 : case FD_SCHED_DEAD_REASON_TOO_MANY_MICROBLOCKS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_MICROBLOCKS;
763 0 : case FD_SCHED_DEAD_REASON_DUPLICATE_ACCOUNT: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_DUPLICATE_ACCOUNT;
764 0 : case FD_SCHED_DEAD_REASON_TRAILING_ENTRY: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TRAILING_ENTRY;
765 0 : case FD_SCHED_DEAD_REASON_TOO_MANY_TICKS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_TICKS;
766 0 : case FD_SCHED_DEAD_REASON_TOO_FEW_TICKS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_FEW_TICKS;
767 0 : case FD_SCHED_DEAD_REASON_TICK_HASH_MISMATCH: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TICK_HASH_MISMATCH;
768 0 : case FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_HASH_MISMATCH;
769 0 : case FD_SCHED_DEAD_REASON_ENTRY_HASH_MISMATCH_INGEST: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_HASH_MISMATCH_INGEST;
770 0 : case FD_SCHED_DEAD_REASON_DEAD_ANCESTOR: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_PARENT_DEAD;
771 0 : case FD_SCHED_DEAD_REASON_BAD_BLOCK_MARKER: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_BLOCK_MARKER;
772 0 : case FD_SCHED_DEAD_REASON_ALPENGLOW_HASH_CNT: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ALPENGLOW_HASH_CNT;
773 0 : case FD_SCHED_DEAD_REASON_MISSING_PARENT_MARKER: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MISSING_PARENT_MARKER;
774 0 : case FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_HEADERS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MULTIPLE_BLOCK_HEADERS;
775 0 : case FD_SCHED_DEAD_REASON_GENESIS_CERT_OUT_OF_ORDER: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_GENESIS_CERT_OUT_OF_ORDER;
776 0 : case FD_SCHED_DEAD_REASON_MULTIPLE_BLOCK_FOOTERS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MULTIPLE_BLOCK_FOOTERS;
777 0 : case FD_SCHED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ENTRY_AFTER_BLOCK_FOOTER;
778 0 : case FD_SCHED_DEAD_REASON_INVALID_ALPENTICK_POSITION: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_INVALID_ALPENTICK_POSITION;
779 0 : case FD_SCHED_DEAD_REASON_MISSING_BLOCK_FOOTER: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MISSING_BLOCK_FOOTER;
780 0 : case FD_SCHED_DEAD_REASON_SPURIOUS_UPDATE_PARENT: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SPURIOUS_UPDATE_PARENT;
781 0 : case FD_SCHED_DEAD_REASON_TOO_MANY_WRITABLE_ACCOUNTS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TOO_MANY_WRITABLE_ACCOUNTS;
782 0 : case FD_SCHED_DEAD_REASON_MULTIPLE_UPDATE_PARENTS: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_MULTIPLE_UPDATE_PARENTS;
783 0 : case FD_SCHED_DEAD_REASON_UPDATE_PARENT_MISMATCH: return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_UPDATE_PARENT_MISMATCH;
784 0 : default: FD_LOG_CRIT(( "unmapped scheduler dead reason %d", sched_reason ));
785 0 : }
786 0 : }
787 :
788 : static int
789 0 : sched_block_dead_reason_to_event( fd_replay_tile_t * ctx, ulong bank_idx ) {
790 0 : if( FD_UNLIKELY( fd_sched_block_is_discarded( ctx->sched, bank_idx ) ) ) return FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
791 0 : return sched_dead_reason_to_event( fd_sched_get_dead_reason( ctx->sched, bank_idx ) );
792 0 : }
793 :
794 : /* The stats array is keyed by bank idx. */
795 :
796 : static void
797 : block_completed_event_fill_reception( fd_replay_tile_t * ctx,
798 : fd_event_block_completed_t * ev,
799 0 : ulong bank_idx ) {
800 0 : ev->lowest_verified_fec_index = 0U;
801 0 : ev->last_completed_fec_set_index = UINT_MAX;
802 :
803 0 : fd_bank_t const * bank = bank_idx==ULONG_MAX ? NULL : fd_banks_bank_query( ctx->banks, bank_idx );
804 0 : if( FD_UNLIKELY( !bank ) ) return;
805 :
806 0 : fd_block_id_ele_t const * ele = &ctx->block_id_arr[ bank_idx ];
807 0 : ev->fec_set_count = ele->fec_cnt;
808 :
809 0 : if( FD_LIKELY( bank->parent_idx!=ULONG_MAX ) ) {
810 0 : fd_block_id_ele_t const * parent = &ctx->block_id_arr[ bank->parent_idx ];
811 0 : fd_memcpy( ev->parent_block_id, parent->dmr.uc, sizeof(ev->parent_block_id) );
812 0 : if( !ev->parent_slot ) ev->parent_slot = parent->slot;
813 0 : }
814 :
815 : /* A mismatched bank_seq means the entry belongs to a previous
816 : occupant of this bank idx, i.e. we have no stats for this block. */
817 0 : fd_reception_stats_t const * s = &ctx->reception_stats[ bank_idx ];
818 0 : if( FD_UNLIKELY( s->bank_seq!=bank->bank_seq ) ) return;
819 0 : ev->votor_repaired = !!s->votor_repaired;
820 0 : }
821 :
822 : static void
823 : block_completed_event_fill_bank( fd_replay_tile_t * ctx,
824 : fd_event_block_completed_t * ev,
825 0 : fd_bank_t * bank ) {
826 0 : fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
827 :
828 0 : int prepared = bank->preparation_begin_nanos!=0L;
829 0 : ulong slot = prepared ? bank->f.slot : ctx->block_id_arr[ bank->idx ].slot;
830 :
831 0 : ev->bank_seq = bank->bank_seq;
832 0 : ev->slot = slot;
833 0 : ev->parent_slot = prepared ? bank->f.parent_slot
834 0 : : bank->parent_idx!=ULONG_MAX ? ctx->block_id_arr[ bank->parent_idx ].slot
835 0 : : 0UL;
836 0 : if( FD_LIKELY( prepared ) ) ev->epoch = bank->f.epoch;
837 0 : else if( FD_LIKELY( ctx->notified_root_bank ) ) ev->epoch = fd_slot_to_epoch( &ctx->notified_root_bank->f.epoch_schedule, slot, NULL );
838 0 : fd_block_id_ele_t const * ele = &ctx->block_id_arr[ bank->idx ];
839 0 : fd_memcpy( ev->block_id, ele->block_id_seen ? ele->dmr.uc : ele->latest_mr.uc, sizeof(ev->block_id) );
840 0 : if( FD_LIKELY( bank->block_completed_nanos ) ) fd_memcpy( ev->bank_hash, bank->f.bank_hash.uc, sizeof(ev->bank_hash) );
841 :
842 0 : ev->first_fec_set_received_time = bank->is_leader ? 0UL : (ulong)bank->first_fec_set_received_nanos;
843 0 : ev->preparation_begin_time = (ulong)bank->preparation_begin_nanos;
844 0 : ev->first_transaction_scheduled_time = (ulong)bank->first_transaction_scheduled_nanos;
845 0 : ev->last_transaction_finished_time = (ulong)bank->last_transaction_finished_nanos;
846 0 : ev->block_completed_time = (ulong)bank->block_completed_nanos;
847 0 : ev->parent_block_completed_time = parent_bank ? (ulong)parent_bank->block_completed_nanos : 0UL;
848 :
849 0 : if( FD_UNLIKELY( bank->cost_tracker_fork_id!=USHORT_MAX ) ) {
850 0 : fd_cost_tracker_t const * ct = fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
851 0 : ev->cost_tracker_block_cost = ct->block_cost;
852 0 : ev->cost_tracker_allocated_accounts_data_size = ct->allocated_accounts_data_size;
853 0 : ev->cost_tracker_block_cost_limit = ct->block_cost_limit;
854 0 : ev->cost_tracker_account_cost_limit = ct->account_cost_limit;
855 0 : fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
856 0 : }
857 :
858 0 : ev->bank_idx = bank->idx;
859 0 : if( FD_LIKELY( prepared ) ) {
860 0 : ev->txncache_fork_id = bank->txncache_fork_id.val;
861 0 : ev->progcache_fork_id = bank->progcache_fork_id;
862 0 : ev->accdb_fork_id = bank->accdb_fork_id.val;
863 0 : ev->vote_stakes_fork_id = bank->vote_stakes_fork_id;
864 0 : ev->stake_rewards_fork_id = bank->stake_rewards_fork_id;
865 0 : ev->epoch_credits_fork_id = bank->epoch_credits_fork_id;
866 0 : ev->stake_delegations_fork_id = bank->stake_delegations_fork_id;
867 0 : ev->cost_tracker_pool_idx = fd_ulong_if( bank->cost_tracker_fork_id==USHORT_MAX, ULONG_MAX, bank->cost_tracker_fork_id );
868 0 : }
869 :
870 0 : block_completed_event_fill_reception( ctx, ev, bank->idx );
871 0 : }
872 :
873 : static int
874 0 : pack_end_reason_to_event( int reason ) {
875 0 : switch( reason ) {
876 0 : case FD_PACK_END_SLOT_REASON_TIME: return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_TIME;
877 0 : case FD_PACK_END_SLOT_REASON_MICROBLOCK: return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_MICROBLOCK_LIMIT;
878 0 : case FD_PACK_END_SLOT_REASON_ABANDONED: return FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_ABANDONED;
879 0 : default: FD_LOG_CRIT(( "unmapped pack end reason %d", reason ));
880 0 : }
881 0 : }
882 :
883 : static void
884 : block_completed_event_fill_leader( fd_replay_tile_t * ctx,
885 : fd_event_block_completed_t * ev,
886 0 : fd_bank_t * bank ) {
887 0 : FD_TEST( ctx->leader_stats.slot==bank->f.slot );
888 :
889 0 : ev->became_leader_time = (ulong)ctx->leader_stats.became_leader_nanos;
890 0 : ev->leader_slot_start_time = (ulong)ctx->leader_stats.leader_slot_start_nanos;
891 :
892 0 : ev->first_fec_set_received_time = (ulong)ctx->leader_stats.first_fec_returned_nanos;
893 0 : ev->recovered_shred_count = 0UL;
894 :
895 0 : ev->microblock_count = ctx->leader_stats.microblock_count;
896 0 : ev->pack_block_cost = ctx->leader_stats.pack_block_cost;
897 0 : ev->pack_vote_cost = 0UL;
898 0 : ev->pack_data_bytes = ctx->leader_stats.pack_data_bytes;
899 0 : ev->bundle_txn_count = ctx->leader_stats.bundle_txn_count;
900 0 : ev->pack_start_time = (ulong)ctx->leader_stats.pack_start_nanos;
901 0 : ev->pack_end_time = (ulong)ctx->leader_stats.pack_end_nanos;
902 0 : ev->pack_end_reason = pack_end_reason_to_event( ctx->leader_stats.pack_end_reason );
903 0 : }
904 :
905 : static void
906 : block_completed_event_fill_leader_txn_timing( fd_replay_tile_t * ctx,
907 : fd_event_block_completed_t * ev,
908 0 : fd_bank_t * bank ) {
909 0 : ulong tidx = ctx->leader_stats.timing_table_idx;
910 0 : if( FD_UNLIKELY( !ctx->leader_txn_timing || tidx>=FD_LEADER_TXN_TIMING_TABLE_CNT ) ) return;
911 0 : fd_leader_txn_timing_table_t const * table = fd_leader_txn_timing_table_const( ctx->leader_txn_timing, tidx, ctx->max_txn_per_slot );
912 0 : if( FD_UNLIKELY( table->slot!=bank->f.slot ) ) return;
913 :
914 0 : ulong cnt = fd_ulong_min( table->cnt, FD_EVENT_BLOCK_COMPLETED_TXN_TIMING_MAX );
915 0 : long first_dispatch = LONG_MAX;
916 0 : for( ulong i=0UL; i<cnt; i++ ) {
917 0 : fd_leader_txn_timing_rec_t const * rec = &table->rec[ i ];
918 0 : first_dispatch = fd_long_min( first_dispatch, rec->dispatched_ticks );
919 0 : ev->txn_timing[ i ] = (fd_event_block_completed_txn_timing_t){
920 0 : .received_time = (ulong)rec->received_ns,
921 0 : .dispatched_time = rec->dispatched_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->dispatched_ticks ),
922 0 : .replayed_time = rec->replayed_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->replayed_ticks ),
923 0 : .poh_mixed_time = rec->poh_mixed_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, rec->poh_mixed_ticks ),
924 0 : };
925 0 : }
926 0 : ev->txn_timing_cnt = cnt;
927 :
928 0 : ev->first_transaction_scheduled_time = first_dispatch==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, first_dispatch );
929 0 : }
930 :
931 : static void
932 : report_block_completed( fd_replay_tile_t * ctx,
933 : fd_bank_t * bank,
934 : int is_leader,
935 0 : fd_replay_slot_completed_t const * slot_info ) {
936 0 : if( FD_LIKELY( !fd_event_tl ) ) return;
937 :
938 0 : fd_event_block_completed_t * ev = ctx->block_completed_event;
939 0 : memset( ev, 0, FD_EVENT_BLOCK_COMPLETED_PREFIX_SZ );
940 :
941 0 : block_completed_event_fill_bank( ctx, ev, bank );
942 :
943 0 : if( FD_LIKELY( slot_info->cost_tracker.block_cost_limit!=ULONG_MAX ) ) {
944 0 : ev->cost_tracker_block_cost = slot_info->cost_tracker.block_cost;
945 0 : ev->cost_tracker_allocated_accounts_data_size = slot_info->cost_tracker.allocated_accounts_data_size;
946 0 : ev->cost_tracker_block_cost_limit = slot_info->cost_tracker.block_cost_limit;
947 0 : ev->cost_tracker_account_cost_limit = slot_info->cost_tracker.account_cost_limit;
948 0 : }
949 0 : ev->cost_tracker_pool_idx = slot_info->cost_tracker.pool_idx;
950 :
951 0 : ev->root_slot = ctx->consensus_root_slot;
952 0 : ev->storage_slot = ctx->published_root_slot;
953 0 : ev->caught_up = !!ctx->caught_up;
954 0 : ev->turbine_slot = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
955 0 : ev->fork_width = ctx->banks->curr_fork_width;
956 0 : ev->snapshot_in_progress = !!ctx->snapmk.active;
957 0 : ev->live_bank_count = fd_banks_pool_used_cnt( ctx->banks );
958 0 : ev->is_leader = is_leader;
959 :
960 0 : ev->pack_end_reason = FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_NOT_LEADER;
961 0 : if( FD_UNLIKELY( is_leader ) ) block_completed_event_fill_leader( ctx, ev, bank );
962 :
963 0 : ev->dead = 0;
964 0 : ev->dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
965 0 : ev->dead_time = 0UL;
966 0 : ev->abandoned = 0;
967 0 : ev->abandoned_reason = FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
968 0 : ev->abandoned_time = 0UL;
969 :
970 0 : if( FD_UNLIKELY( is_leader ) ) {
971 0 : block_completed_event_fill_leader_txn_timing( ctx, ev, bank );
972 0 : } else {
973 0 : ulong tslot = ctx->timing_slot_of_bank[ bank->idx ];
974 0 : if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
975 0 : fd_replay_txn_timing_slot_t const * slot = fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot );
976 0 : fd_replay_txn_timing_t const * rec = ctx->timing_rec + tslot*ctx->max_txn_per_slot;
977 0 : ulong cnt = fd_ulong_min( slot->cnt, FD_EVENT_BLOCK_COMPLETED_TXN_TIMING_MAX );
978 0 : for( ulong i=0UL; i<cnt; i++ ) {
979 0 : fd_replay_txn_timing_t const * t = &rec[ i ];
980 0 : ev->txn_timing[ i ] = (fd_event_block_completed_txn_timing_t){
981 0 : .received_time = (ulong)t->received_ns, /* already wallclock */
982 0 : .parsed_time = t->parsed_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->parsed_ticks ),
983 0 : .sigverify_dispatched_time = t->sigverify_disp_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->sigverify_disp_ticks ),
984 0 : .sigverify_done_time = t->sigverify_done_ticks==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->sigverify_done_ticks ),
985 0 : .dispatched_time = t->exec_disp_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->exec_disp_ticks ),
986 0 : .replayed_time = t->exec_done_ticks ==LONG_MAX ? 0UL : (ulong)fd_clock_epoch_y( ctx->clock->epoch, t->exec_done_ticks ),
987 0 : };
988 0 : }
989 0 : ev->txn_timing_cnt = cnt;
990 0 : }
991 0 : }
992 :
993 0 : if( FD_UNLIKELY( !ev->first_transaction_scheduled_time ) ) ev->first_transaction_scheduled_time = ev->last_transaction_finished_time;
994 :
995 0 : fd_event_report_block_completed( ev );
996 0 : }
997 :
998 : static void
999 : update_metric_identity_balance( fd_replay_tile_t * ctx,
1000 : fd_accdb_fork_id_t fork_id,
1001 0 : fd_pubkey_t const * identity ) {
1002 0 : ulong identity_balance = fd_accdb_lamports( ctx->accdb, fork_id, identity->uc );
1003 0 : FD_MGAUGE_SET( REPLAY, IDENTITY_BALANCE_LAMPORTS, identity_balance );
1004 0 : }
1005 :
1006 : static void
1007 : update_metric_vote_account( fd_replay_tile_t * ctx,
1008 : fd_bank_t const * bank,
1009 : fd_accdb_fork_id_t fork_id,
1010 0 : fd_pubkey_t const * vote_key ) {
1011 0 : ulong epoch_credits = 0UL;
1012 0 : fd_acc_t ro = fd_accdb_read_one( ctx->accdb, fork_id, vote_key->uc );
1013 : /* Unstaked voters are never admitted; don't flag them. Once set,
1014 : sticky until an epoch boundary actually admits the account. */
1015 0 : int admitted = fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, NULL );
1016 : /* SIMD-0123: delegator rewards do not count towards the balance
1017 : https://github.com/anza-xyz/agave/blob/v4.4/vote/src/vote_account.rs#L225-L234 */
1018 0 : ulong available_balance = ro.lamports;
1019 0 : if( FD_FEATURE_ACTIVE_BANK( bank, block_revenue_sharing ) ) {
1020 0 : ulong pending_delegator_rewards = 0UL;
1021 0 : if( FD_LIKELY( !fd_vote_account_pending_delegator_rewards( ro.data, ro.data_len, &pending_delegator_rewards ) ) ) {
1022 0 : available_balance = fd_ulong_sat_sub( ro.lamports, pending_delegator_rewards );
1023 0 : }
1024 0 : }
1025 0 : if( FD_UNLIKELY( !fd_stakes_vote_account_is_admissible( bank, available_balance, ro.owner, ro.data, ro.data_len ) ) ) {
1026 0 : ctx->vote_account_inadmissible = ctx->vote_account_staked || admitted;
1027 0 : } else if( FD_UNLIKELY( ctx->vote_account_inadmissible ) ) {
1028 0 : ctx->vote_account_inadmissible = ctx->vote_account_staked && !admitted;
1029 0 : }
1030 :
1031 0 : if( FD_LIKELY( ro.lamports ) ) {
1032 0 : fd_vote_state_versioned_t vsv[1];
1033 0 : if( FD_LIKELY( fd_vote_state_versioned_deserialize( vsv, ro.data, ro.data_len ) ) ) {
1034 0 : fd_vote_epoch_credits_t const * last_ec = fd_vsv_get_last_epoch_credits( vsv );
1035 0 : if( last_ec && last_ec->epoch==bank->f.epoch ) {
1036 0 : epoch_credits = fd_ulong_sat_sub( last_ec->credits, last_ec->prev_credits ); /* credits is the lifetime total */
1037 0 : }
1038 0 : }
1039 0 : }
1040 0 : fd_accdb_unread_one( ctx->accdb, &ro );
1041 :
1042 0 : FD_MGAUGE_SET( REPLAY, EPOCH_CREDITS, epoch_credits );
1043 0 : FD_MGAUGE_SET( REPLAY, IS_VOTE_ACCOUNT_INADMISSIBLE, (ulong)ctx->vote_account_inadmissible );
1044 0 : }
1045 :
1046 : static void
1047 : update_metric_active_stake( fd_bank_t const * bank,
1048 0 : fd_pubkey_t const * vote_key ) {
1049 0 : ulong my_active_stake = 0UL;
1050 :
1051 0 : fd_vote_stakes_ele_t ele[1];
1052 0 : if( fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, ele ) ) my_active_stake = ele->stake;
1053 :
1054 0 : FD_MGAUGE_SET( REPLAY, ACTIVE_STAKE_LAMPORTS, my_active_stake );
1055 0 : }
1056 :
1057 : static void
1058 : update_metric_balances( fd_replay_tile_t * ctx,
1059 0 : fd_bank_t * bank ) {
1060 0 : fd_accdb_fork_id_t fork_id = bank->accdb_fork_id;
1061 0 : fd_node_info_t node_info[1]; fd_node_info_read( node_info, ctx->node_info );
1062 0 : if( !fd_pubkey_check_zero( &node_info->identity ) ) {
1063 0 : update_metric_identity_balance( ctx, fork_id, &node_info->identity );
1064 0 : }
1065 :
1066 0 : if( !fd_pubkey_check_zero( &node_info->vote_account ) ) {
1067 0 : update_metric_vote_account( ctx, bank, fork_id, &node_info->vote_account );
1068 0 : update_metric_active_stake( bank, &node_info->vote_account );
1069 0 : }
1070 0 : FD_MGAUGE_SET( REPLAY, CLUSTER_ACTIVE_STAKE_LAMPORTS, bank->f.total_epoch_stake );
1071 0 : }
1072 :
1073 : /* try_advance_root attempts to advance root to finalized_block_id. For
1074 : something to be rooted, it must be BOTH finalized AND replayed. This
1075 : walks up the bank lineage in the block id map to find and set root.
1076 :
1077 : ON FINALIZED
1078 :
1079 : If finalized is ahead of replayed => root does not advance
1080 : If replayed is ahead of finalized => root advances
1081 :
1082 : ON REPLAYED
1083 :
1084 : If finalized is ahead of replayed => root advances
1085 : If replayed is ahead of finalized => root does not advance
1086 :
1087 : consensus_root moves straight to the finalized block, and
1088 : try_notify_consensus_root hands it off as REPLAY_SIG_ROOT_ADVANCED. */
1089 :
1090 : static void
1091 : try_advance_root( fd_replay_tile_t * ctx,
1092 0 : fd_block_id_t finalized_block_id ) {
1093 :
1094 0 : if( FD_UNLIKELY( finalized_block_id.slot<=ctx->consensus_root_slot ) ) return;
1095 :
1096 0 : if( FD_UNLIKELY( fd_bhm_map_ele_query( ctx->bhm_map, &finalized_block_id, NULL, ctx->bhm_pool ) ) ) {
1097 0 : FD_BASE58_ENCODE_32_BYTES( finalized_block_id.hash.uc, block_id_b58 );
1098 0 : FD_LOG_ERR(( "HARD FORK DETECTED for slot %lu block ID `%s`", finalized_block_id.slot, block_id_b58 ));
1099 0 : }
1100 :
1101 0 : fd_block_id_t ancestor_block_id = finalized_block_id;
1102 0 : fd_bank_t * finalized_bank = NULL;
1103 :
1104 0 : while( FD_LIKELY( ancestor_block_id.slot>ctx->consensus_root_slot ) ) {
1105 0 : fd_block_id_ele_t * ele = fd_block_id_map_ele_query( ctx->block_id_map, &ancestor_block_id, NULL, ctx->block_id_arr );
1106 0 : fd_bank_t * bank = ele ? fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, ele ) ) : NULL;
1107 0 : if( FD_UNLIKELY( !bank || bank->bank_seq!=ele->bank_seq || bank->state!=FD_BANK_STATE_FROZEN || bank->parent_idx==ULONG_MAX ) ) break; /* not replayed */
1108 0 : if( !finalized_bank ) finalized_bank = bank;
1109 0 : ancestor_block_id = ctx->block_id_arr[ bank->parent_idx ].block_info;
1110 0 : }
1111 :
1112 0 : if( FD_LIKELY( ancestor_block_id.slot==ctx->consensus_root_slot && !memcmp( &ancestor_block_id.hash, ctx->consensus_root.uc, sizeof(fd_hash_t) ) ) ) {
1113 0 : ctx->consensus_root_slot = finalized_block_id.slot;
1114 0 : memcpy( ctx->consensus_root.uc, &finalized_block_id.hash, sizeof(fd_hash_t) );
1115 0 : if( FD_UNLIKELY( ctx->next_leader_slot!=ULONG_MAX && finalized_block_id.slot>ctx->votor_leader->parent_slot ) ) ctx->next_leader_slot = ULONG_MAX;
1116 0 : update_metric_balances( ctx, finalized_bank ); /* Alpenglow has no optimistic confirmation */
1117 0 : return;
1118 0 : }
1119 :
1120 : /* When a block id is finalized ahead of replay, we need to cache it
1121 : and process it when replay catches up. Certificates can arrive
1122 : out of order, so lo keeps the oldest and hi the newest. */
1123 :
1124 0 : if( FD_UNLIKELY( finalized_block_id.slot<ctx->finalized_block_id_lo.slot || ctx->finalized_block_id_lo.slot<=ctx->consensus_root_slot ) ) {
1125 0 : if( FD_UNLIKELY( ctx->finalized_block_id_lo.slot>ctx->finalized_block_id_hi.slot ) ) fd_swap( ctx->finalized_block_id_lo, ctx->finalized_block_id_hi );
1126 0 : ctx->finalized_block_id_lo = finalized_block_id;
1127 0 : } else if( FD_LIKELY( finalized_block_id.slot>ctx->finalized_block_id_hi.slot ) ) {
1128 0 : ctx->finalized_block_id_hi = finalized_block_id;
1129 0 : }
1130 0 : }
1131 :
1132 : static void
1133 : publish_slot_completed( fd_replay_tile_t * ctx,
1134 : fd_stem_context_t * stem,
1135 : fd_bank_t * bank,
1136 : int is_initial,
1137 : int is_leader,
1138 : ulong execution_fees_pre_settle,
1139 : ulong priority_fees_pre_settle,
1140 0 : fd_block_footer_t const * footer ) {
1141 :
1142 0 : ulong slot = bank->f.slot;
1143 :
1144 0 : if( FD_UNLIKELY( slot>=ctx->reset_slot ) ) {
1145 0 : ctx->reset_slot = slot;
1146 : /* Do not touch next_leader_slot: try_fini_leader uses ULONG_MAX
1147 : to continue the leader window. */
1148 0 : refresh_epoch_info( ctx, bank, slot );
1149 0 : }
1150 :
1151 0 : if( FD_UNLIKELY( is_initial ) ) bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
1152 :
1153 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank->idx ];
1154 :
1155 : /* HACKY: hacky way of checking if we should send a null parent block
1156 : id */
1157 0 : fd_hash_t block_id = block_id_ele->dmr;
1158 0 : fd_hash_t parent_block_id = {0};
1159 0 : if( FD_LIKELY( !is_initial ) ) {
1160 0 : fd_block_id_ele_t const * parent_ele = &ctx->block_id_arr[ bank->parent_idx ];
1161 0 : parent_block_id = parent_ele->dmr;
1162 0 : }
1163 :
1164 0 : fd_hash_t const * bank_hash = &bank->f.bank_hash;
1165 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
1166 0 : FD_TEST( block_hash );
1167 :
1168 0 : if( FD_LIKELY( !is_initial ) ) fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
1169 :
1170 0 : fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
1171 0 : ulong slot_idx;
1172 0 : ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
1173 :
1174 0 : ctx->metrics.slots_total++;
1175 0 : ctx->metrics.transactions_total = bank->f.parent_txn_count + bank->f.txn_count;
1176 :
1177 : /* Caught up once replay completes a slot within a few slots of the
1178 : cluster tip. Require the tip to have advanced a few times first so
1179 : a brief view of the tip right after boot does not count. */
1180 0 : if( FD_UNLIKELY( !ctx->caught_up && !is_initial &&
1181 0 : ctx->catch_up_tip_advance_cnt>=12UL &&
1182 0 : ctx->catch_up_max_fec_slot<slot+3UL ) ) {
1183 0 : ctx->caught_up = 1;
1184 0 : FD_MGAUGE_SET( REPLAY, IS_CAUGHT_UP, 1UL );
1185 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_CAUGHT_UP, slot );
1186 0 : double boot_secs = (double)(fd_log_wallclock()-ctx->boot_timestamp_nanos)/1e9;
1187 0 : FD_LOG_NOTICE(( "caught up to cluster at slot %s%lu%s %s(%.1f seconds since boot)%s",
1188 0 : fd_log_style_bold(), slot, fd_log_style_normal(),
1189 0 : fd_log_style_dim(), boot_secs, fd_log_style_normal() ));
1190 0 : }
1191 :
1192 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1193 0 : slot_info->slot = slot;
1194 0 : slot_info->root_slot = ctx->consensus_root_slot;
1195 0 : slot_info->storage_slot = ctx->published_root_slot;
1196 0 : slot_info->epoch = epoch;
1197 0 : slot_info->slot_in_epoch = slot_idx;
1198 0 : slot_info->slots_per_epoch = fd_epoch_slot_cnt( epoch_schedule, epoch );
1199 0 : slot_info->block_height = bank->f.block_height;
1200 0 : slot_info->parent_slot = bank->f.parent_slot;
1201 0 : slot_info->block_id = block_id;
1202 0 : slot_info->parent_block_id = parent_block_id;
1203 0 : slot_info->bank_hash = *bank_hash;
1204 0 : slot_info->block_hash = *block_hash;
1205 0 : slot_info->transaction_count = bank->f.parent_txn_count + bank->f.txn_count;
1206 :
1207 0 : fd_inflation_t inflation = bank->f.inflation;
1208 0 : slot_info->inflation.foundation = inflation.foundation;
1209 0 : slot_info->inflation.foundation_term = inflation.foundation_term;
1210 0 : slot_info->inflation.terminal = inflation.terminal;
1211 0 : slot_info->inflation.initial = inflation.initial;
1212 0 : slot_info->inflation.taper = inflation.taper;
1213 :
1214 0 : fd_rent_t rent = bank->f.rent;
1215 0 : slot_info->rent.burn_percent = rent.burn_percent;
1216 0 : slot_info->rent.lamports_per_uint8_year = rent.lamports_per_uint8_year;
1217 0 : slot_info->rent.exemption_threshold = rent.exemption_threshold;
1218 :
1219 0 : slot_info->first_fec_set_received_nanos = bank->first_fec_set_received_nanos;
1220 0 : slot_info->preparation_begin_nanos = bank->preparation_begin_nanos;
1221 0 : slot_info->first_transaction_scheduled_nanos = bank->first_transaction_scheduled_nanos;
1222 0 : slot_info->last_transaction_finished_nanos = bank->last_transaction_finished_nanos;
1223 0 : slot_info->completion_time_nanos = fd_clock_tile_now( ctx->clock );
1224 0 : slot_info->footer = footer ? *footer : (fd_block_footer_t){0};
1225 0 : if( !slot_info->first_transaction_scheduled_nanos ) { /* edge case: empty slot */
1226 0 : slot_info->first_transaction_scheduled_nanos = slot_info->last_transaction_finished_nanos;
1227 0 : }
1228 :
1229 : /* refcnt should be incremented by 1 for each consumer that uses
1230 : `bank_idx`. Each consumer should decrement the bank's refcnt once
1231 : they are done using the bank. */
1232 0 : if( FD_LIKELY( ctx->rpc_enabled ) ) bank->refcnt++; /* rpc tile */
1233 0 : slot_info->bank_idx = bank->idx;
1234 0 : slot_info->bank_seq = bank->bank_seq;
1235 0 : slot_info->accdb_fork_id = bank->accdb_fork_id;
1236 :
1237 0 : fd_bank_t * parent_bank = fd_banks_get_parent( ctx->banks, bank );
1238 0 : slot_info->parent_bank_idx = parent_bank ? parent_bank->idx : ULONG_MAX;
1239 0 : slot_info->parent_bank_seq = parent_bank ? parent_bank->bank_seq : ULONG_MAX;
1240 0 : if( FD_LIKELY( parent_bank ) ) {
1241 0 : ulong total_txn_cnt = bank->f.txn_count;
1242 0 : ulong nonvote_txn_cnt = bank->f.nonvote_txn_count;
1243 0 : ulong failed_txn_cnt = bank->f.failed_txn_count;
1244 0 : ulong nonvote_failed_txn_cnt = bank->f.nonvote_failed_txn_count;
1245 :
1246 0 : slot_info->nonvote_success = nonvote_txn_cnt - nonvote_failed_txn_cnt;
1247 0 : slot_info->nonvote_failed = nonvote_failed_txn_cnt;
1248 0 : slot_info->vote_failed = failed_txn_cnt - nonvote_failed_txn_cnt;
1249 0 : slot_info->vote_success = total_txn_cnt - nonvote_txn_cnt - slot_info->vote_failed;
1250 0 : } else {
1251 0 : slot_info->vote_failed = ULONG_MAX;
1252 0 : slot_info->vote_success = ULONG_MAX;
1253 0 : slot_info->nonvote_success = ULONG_MAX;
1254 0 : slot_info->nonvote_failed = ULONG_MAX;
1255 0 : }
1256 :
1257 0 : slot_info->is_leader = is_leader;
1258 0 : slot_info->transaction_fee = execution_fees_pre_settle;
1259 0 : slot_info->transaction_fee -= (slot_info->transaction_fee>>1); /* burn */
1260 0 : slot_info->priority_fee = priority_fees_pre_settle;
1261 0 : slot_info->tips = bank->f.tips;
1262 0 : slot_info->shred_cnt = bank->f.shred_cnt;
1263 :
1264 0 : slot_info->voted = replay_reward_cert_voted( ctx, bank, &slot_info->voted_rank, &slot_info->vote_count );
1265 :
1266 0 : slot_info->vote_balance = ULONG_MAX;
1267 0 : slot_info->vote_commission = USHORT_MAX;
1268 0 : if( FD_UNLIKELY( ctx->has_vote_account && slot%512UL==0UL ) ) {
1269 0 : fd_acc_t acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, ctx->vote_account->uc );
1270 0 : if( FD_LIKELY( acc.lamports ) ) {
1271 0 : slot_info->vote_balance = acc.lamports;
1272 :
1273 0 : ushort bps;
1274 0 : if( FD_LIKELY( !fd_vote_account_commission_bps( acc.data, acc.data_len, FD_FEATURE_ACTIVE_BANK( bank, commission_rate_in_basis_points ), &bps ) ) ) slot_info->vote_commission = bps;
1275 0 : }
1276 0 : fd_accdb_unread_one( ctx->accdb, &acc );
1277 0 : }
1278 :
1279 0 : publish_replay_out( ctx, stem, REPLAY_SIG_SLOT_COMPLETED, sizeof(fd_replay_slot_completed_t) );
1280 :
1281 0 : fd_replay_next_leader_t * next_leader = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1282 0 : next_leader->slot = ctx->reset_slot;
1283 0 : next_leader->next_leader_slot = ctx->leader_bank ? ctx->leader_bank->f.slot :
1284 0 : query_next_leader_slot( ctx, fd_ulong_max( ctx->reset_slot+1UL, fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot+1UL ) ) );
1285 0 : publish_replay_out( ctx, stem, REPLAY_SIG_NEXT_LEADER, sizeof(fd_replay_next_leader_t) );
1286 :
1287 : /* Skip the telemetry event for the initial boot block (snapshot /
1288 : genesis): it was not replayed. */
1289 0 : if( FD_LIKELY( !is_initial ) ) report_block_completed( ctx, bank, is_leader, slot_info );
1290 0 : timing_slot_release( ctx, bank->idx );
1291 :
1292 0 : if( FD_UNLIKELY( slot==ctx->finalized_block_id_lo.slot && !memcmp( block_id.uc, &ctx->finalized_block_id_lo.hash, sizeof(fd_hash_t) ) ) ) { /* finalized ahead of replay, replay caught up */
1293 0 : try_advance_root( ctx, ctx->finalized_block_id_lo );
1294 0 : ctx->finalized_block_id_lo = ctx->finalized_block_id_hi; /* hi becomes lo once lo is reached */
1295 0 : }
1296 0 : }
1297 :
1298 : static void
1299 : report_block_incomplete( fd_replay_tile_t * ctx,
1300 : ulong slot,
1301 : fd_hash_t const * block_id,
1302 : fd_bank_t * bank,
1303 : int dead_reason,
1304 0 : int abandoned_reason ) {
1305 0 : if( FD_LIKELY( !fd_event_tl ) ) return;
1306 :
1307 0 : fd_event_block_completed_t * ev = ctx->block_completed_event;
1308 0 : memset( ev, 0, FD_EVENT_BLOCK_COMPLETED_PREFIX_SZ );
1309 0 : if( FD_LIKELY( bank ) ) {
1310 0 : block_completed_event_fill_bank( ctx, ev, bank );
1311 0 : ev->is_leader = !!bank->is_leader;
1312 0 : } else {
1313 0 : ev->slot = slot;
1314 0 : if( FD_LIKELY( ctx->notified_root_bank ) ) ev->epoch = fd_slot_to_epoch( &ctx->notified_root_bank->f.epoch_schedule, slot, NULL );
1315 0 : fd_memcpy( ev->block_id, block_id->uc, sizeof(ev->block_id) );
1316 0 : block_completed_event_fill_reception( ctx, ev, ULONG_MAX );
1317 0 : }
1318 0 : ev->root_slot = ctx->consensus_root_slot;
1319 0 : ev->storage_slot = ctx->published_root_slot;
1320 0 : ev->caught_up = !!ctx->caught_up;
1321 0 : ev->turbine_slot = ctx->catch_up_max_fec_slot==ULONG_MAX ? 0UL : ctx->catch_up_max_fec_slot;
1322 0 : ev->fork_width = ctx->banks->curr_fork_width;
1323 0 : ev->snapshot_in_progress = !!ctx->snapmk.active;
1324 0 : ev->live_bank_count = fd_banks_pool_used_cnt( ctx->banks );
1325 :
1326 0 : int dead = dead_reason !=FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD;
1327 0 : int abandoned = abandoned_reason!=FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
1328 0 : long now = fd_clock_tile_now( ctx->clock );
1329 :
1330 0 : ev->dead = dead;
1331 0 : ev->dead_reason = dead_reason;
1332 0 : ev->dead_time = dead ? (ulong)now : 0UL;
1333 0 : ev->abandoned = abandoned;
1334 0 : ev->abandoned_reason = abandoned_reason;
1335 0 : ev->abandoned_time = abandoned ? (ulong)now : 0UL;
1336 :
1337 0 : ev->pack_end_reason = FD_EVENT_BLOCK_COMPLETED_PACK_END_REASON_NOT_LEADER;
1338 0 : if( FD_UNLIKELY( bank && bank->is_leader ) ) {
1339 0 : block_completed_event_fill_leader( ctx, ev, bank );
1340 0 : block_completed_event_fill_leader_txn_timing( ctx, ev, bank );
1341 0 : }
1342 :
1343 0 : fd_event_report_block_completed( ev );
1344 0 : }
1345 :
1346 : static void
1347 : publish_slot_dead( fd_replay_tile_t * ctx,
1348 : fd_stem_context_t * stem,
1349 : ulong slot,
1350 : fd_hash_t const * block_id,
1351 : uint latest_fec_idx,
1352 0 : fd_block_footer_t const * footer ) {
1353 0 : fd_replay_slot_dead_t * slot_dead = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1354 0 : slot_dead->slot = slot;
1355 0 : slot_dead->block_id = *block_id;
1356 0 : slot_dead->latest_fec_idx = latest_fec_idx;
1357 0 : slot_dead->footer = footer ? *footer : (fd_block_footer_t){0};
1358 0 : publish_replay_out( ctx, stem, REPLAY_SIG_SLOT_DEAD, sizeof(fd_replay_slot_dead_t) );
1359 0 : }
1360 :
1361 : static void
1362 : publish_txn_executed( fd_replay_tile_t * ctx,
1363 : fd_stem_context_t * stem,
1364 : ulong bank_idx,
1365 0 : ulong txn_idx ) {
1366 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
1367 :
1368 0 : FD_TEST( txn_info->index_in_slot<ctx->max_txn_per_slot );
1369 0 : ulong tslot = ctx->timing_slot_of_bank[ bank_idx ];
1370 0 : if( FD_LIKELY( tslot!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) ) ) {
1371 0 : fd_replay_txn_timing_slot_t * slot = fd_timing_slot_pool_ele( ctx->timing_slot_pool, tslot );
1372 0 : fd_replay_txn_timing_t * t = ctx->timing_rec + tslot*ctx->max_txn_per_slot + txn_info->index_in_slot;
1373 0 : t->received_ns = txn_info->received_ns;
1374 0 : t->parsed_ticks = txn_info->tick_parsed;
1375 0 : t->sigverify_disp_ticks = txn_info->tick_sigverify_disp;
1376 0 : t->sigverify_done_ticks = txn_info->tick_sigverify_done;
1377 0 : t->exec_disp_ticks = txn_info->tick_exec_disp;
1378 0 : t->exec_done_ticks = txn_info->tick_exec_done;
1379 0 : slot->cnt = fd_ulong_max( slot->cnt, txn_info->index_in_slot+1UL );
1380 0 : }
1381 :
1382 0 : fd_replay_txn_executed_t * txn_executed = fd_type_pun( fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk ) );
1383 0 : *txn_executed->txn = *fd_sched_get_txn( ctx->sched, txn_idx );
1384 0 : txn_executed->txn_err = txn_info->txn_err;
1385 0 : txn_executed->is_committable = !!(txn_info->flags&FD_SCHED_TXN_IS_COMMITTABLE);
1386 0 : txn_executed->is_fees_only = !!(txn_info->flags&FD_SCHED_TXN_IS_FEES_ONLY);
1387 0 : txn_executed->is_noop = !!(txn_info->flags&FD_SCHED_TXN_IS_NOOP);
1388 0 : txn_executed->tick_parsed = txn_info->tick_parsed;
1389 0 : txn_executed->tick_sigverify_disp = txn_info->tick_sigverify_disp;
1390 0 : txn_executed->tick_sigverify_done = txn_info->tick_sigverify_done;
1391 0 : txn_executed->tick_exec_disp = txn_info->tick_exec_disp;
1392 0 : txn_executed->tick_exec_done = txn_info->tick_exec_done;
1393 :
1394 0 : txn_executed->tick_load_start = txn_info->tick_load_start;
1395 0 : txn_executed->tick_check_start = txn_info->tick_check_start;
1396 0 : txn_executed->tick_exec_start = txn_info->tick_exec_start;
1397 0 : txn_executed->tick_commit_start = txn_info->tick_commit_start;
1398 0 : txn_executed->tick_commit_end = txn_info->tick_commit_end;
1399 :
1400 0 : txn_executed->slot = txn_info->slot;
1401 0 : txn_executed->bank_seq = txn_info->bank_seq;
1402 0 : txn_executed->index_in_slot = txn_info->index_in_slot;
1403 0 : txn_executed->exec_tile_idx = txn_info->exec_tile_idx;
1404 0 : txn_executed->sigverify_exec_tile_idx = txn_info->sigverify_exec_tile_idx;
1405 0 : txn_executed->compute_units_consumed = txn_info->compute_units_consumed;
1406 0 : txn_executed->max_compute_units = txn_info->max_compute_units;
1407 0 : txn_executed->transaction_fee = txn_info->transaction_fee;
1408 0 : txn_executed->priority_fee = txn_info->priority_fee;
1409 0 : txn_executed->tips = txn_info->tips;
1410 0 : publish_replay_out( ctx, stem, REPLAY_SIG_TXN_EXECUTED, sizeof(*txn_executed) );
1411 0 : }
1412 :
1413 : /* Emit the runtime_block event for a finalized bank (replayed or
1414 : leader-produced). The fee arguments are the bank's values snapshotted
1415 : before fd_runtime_block_execute_finalize, which settles (zeroes) the
1416 : fee fields and applies the tip commission. */
1417 :
1418 : static void
1419 : replay_runtime_block_emit( fd_replay_tile_t * ctx,
1420 : fd_bank_t * bank,
1421 : ulong execution_fees,
1422 : ulong priority_fees,
1423 0 : ulong tips ) {
1424 0 : fd_block_id_ele_t const * ele = &ctx->block_id_arr[ bank->idx ];
1425 0 : fd_hash_t const * block_id = &ele->dmr;
1426 0 : fd_hash_t parent_block_id = {0};
1427 0 : if( FD_LIKELY( bank->parent_idx!=ULONG_MAX ) ) {
1428 0 : fd_block_id_ele_t const * parent_ele = &ctx->block_id_arr[ bank->parent_idx ];
1429 0 : parent_block_id = parent_ele->dmr;
1430 0 : }
1431 0 : fd_pubkey_t leader = {0};
1432 0 : fd_pubkey_t const * _leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, bank->f.slot );
1433 0 : if( FD_LIKELY( _leader ) ) leader = *_leader;
1434 0 : fd_sol_sysvar_clock_t clock = {0};
1435 0 : if( FD_UNLIKELY( !fd_sysvar_clock_read( ctx->accdb, bank->accdb_fork_id, &clock ) ) ) FD_LOG_ERR(( "failed to read clock sysvar for slot %lu", bank->f.slot ));
1436 0 : fd_event_runtime_block_emit( bank, block_id->uc, parent_block_id.uc, leader.uc,
1437 0 : execution_fees, priority_fees, tips, bank->f.shred_cnt, &clock,
1438 0 : ctx->fec_chain + bank->idx*FD_FEC_BLK_MAX,
1439 0 : ctx->block_id_arr[ bank->idx ].fec_cnt );
1440 0 : }
1441 :
1442 : static void
1443 : mark_bank_dead( fd_replay_tile_t * ctx,
1444 : fd_stem_context_t * stem,
1445 : ulong bank_idx,
1446 : int dead_reason,
1447 : int abandoned_reason,
1448 : fd_block_footer_t const * footer );
1449 :
1450 :
1451 : /**********************************************************************/
1452 : /* Leader bank management */
1453 : /**********************************************************************/
1454 :
1455 : static fd_bank_t *
1456 : prepare_leader_bank( fd_replay_tile_t * ctx,
1457 : fd_bank_t * parent_bank,
1458 : ulong slot,
1459 0 : long now ) {
1460 :
1461 : /* Make sure that we are not already leader. */
1462 0 : FD_TEST( ctx->leader_bank==NULL );
1463 :
1464 0 : ctx->leader_bank = fd_banks_new_bank( ctx->banks, parent_bank->idx, now, 1 );
1465 0 : if( FD_UNLIKELY( !ctx->leader_bank ) ) {
1466 0 : FD_LOG_CRIT(( "invariant violation: leader bank is NULL for slot %lu", slot ));
1467 0 : }
1468 :
1469 0 : ctx->leader_bank = fd_banks_clone_from_parent( ctx->banks, ctx->leader_bank->idx );
1470 0 : if( FD_UNLIKELY( !ctx->leader_bank ) ) {
1471 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for slot %lu", slot ));
1472 0 : }
1473 :
1474 : /* pin the cost tracker for the duration of the leader slot */
1475 0 : fd_cost_tracker_store_pin( fd_bank_cost_tracker( ctx->leader_bank ), ctx->leader_bank->cost_tracker_fork_id );
1476 :
1477 0 : ctx->leader_bank->preparation_begin_nanos = now;
1478 :
1479 0 : ctx->leader_bank->f.slot = slot;
1480 0 : ctx->leader_bank->leader = ctx->identity_pubkey[ 0 ];
1481 :
1482 : /* Clear the previous occupant of this reused bank idx from whichever
1483 : map it is currently keyed in. */
1484 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ ctx->leader_bank->idx ];
1485 0 : if( FD_LIKELY( fd_block_id_map_ele_query( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele ) ) {
1486 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ) );
1487 0 : }
1488 :
1489 0 : block_id_ele->block_id_seen = 0;
1490 0 : block_id_ele->slot = slot;
1491 0 : block_id_ele->bank_seq = ctx->leader_bank->bank_seq;
1492 0 : block_id_ele->latest_fec_idx = 0U;
1493 0 : block_id_ele->fec_cnt = 0U;
1494 0 : memset( &block_id_ele->latest_mr, 0, sizeof(fd_hash_t) );
1495 0 : memset( &block_id_ele->dmr, 0, sizeof(fd_hash_t) );
1496 :
1497 0 : ctx->leader_bank->txncache_fork_id = fd_txncache_attach_child ( ctx->txncache, parent_bank->txncache_fork_id );
1498 0 : ctx->leader_bank->progcache_fork_id = fd_progcache_attach_child( ctx->progcache, parent_bank->progcache_fork_id );
1499 0 : ctx->leader_bank->accdb_fork_id = fd_accdb_attach_child ( ctx->accdb, parent_bank->accdb_fork_id );
1500 0 : ctx->leader_bank->parent_accdb_fork_id = parent_bank->accdb_fork_id;
1501 :
1502 0 : int is_epoch_boundary = 0;
1503 0 : fd_runtime_block_execute_prepare( ctx->banks, ctx->leader_bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx, &is_epoch_boundary );
1504 0 : FD_CHECK_CRIT( !is_epoch_boundary || FD_FEATURE_ACTIVE_BANK( ctx->leader_bank, alpenglow ), "alpenglow feature is not active" );
1505 :
1506 0 : ulong max_tick_height;
1507 0 : if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS!=fd_runtime_compute_max_tick_height( parent_bank->f.ticks_per_slot, slot, &max_tick_height ) ) ) {
1508 0 : FD_LOG_CRIT(( "couldn't compute tick height/max tick height slot %lu ticks_per_slot %lu", slot, parent_bank->f.ticks_per_slot ));
1509 0 : }
1510 0 : ctx->leader_bank->f.max_tick_height = max_tick_height;
1511 :
1512 : /* Now that a bank has been created for the leader slot, increment the
1513 : reference count until we are done with the leader slot. */
1514 0 : ctx->leader_bank->refcnt++;
1515 :
1516 0 : return ctx->leader_bank;
1517 0 : }
1518 :
1519 : static inline void
1520 0 : maybe_switch_identity( fd_replay_tile_t * ctx ) {
1521 :
1522 0 : if( FD_LIKELY( fd_keyswitch_state_query( ctx->keyswitch )!=FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) return;
1523 :
1524 : /* Switch identity */
1525 :
1526 0 : FD_LOG_DEBUG(( "keyswitch: switching identity" ));
1527 :
1528 0 : memcpy( ctx->identity_pubkey, ctx->keyswitch->bytes, 32UL );
1529 0 : ctx->voter_rank_cached = 0;
1530 0 : ctx->identity_dirty = 1;
1531 :
1532 0 : ctx->metrics.voted_slot = ULONG_MAX;
1533 :
1534 0 : fd_node_info_write_begin( ctx->node_info );
1535 0 : ctx->node_info->info.identity = *ctx->identity_pubkey;
1536 0 : fd_node_info_write_end ( ctx->node_info );
1537 :
1538 0 : ctx->next_leader_query_start = ULONG_MAX;
1539 :
1540 0 : ctx->identity_vote_rooted = 0;
1541 0 : ctx->identity_idx++;
1542 0 : fd_vote_tracker_reset( ctx->vote_tracker );
1543 :
1544 : /* identity_vote_rooted was just reset, so the gate rests on the flag
1545 : alone and a surviving auto would read as armed (see fd_wfs.h). */
1546 0 : if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
1547 0 : FD_LOG_CRIT(( "wait_for_vote_to_start_leader unresolved (auto) at identity rotation" ));
1548 0 : }
1549 :
1550 : /* Save the current sequence so the voter knows what sequence to
1551 : consume up to. */
1552 0 : ctx->keyswitch->result = fd_mcache_seq_query( ctx->slot_out_seq );
1553 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
1554 0 : }
1555 :
1556 : static void
1557 : construct_footer_certs( fd_replay_tile_t const * ctx,
1558 : ulong leader_slot,
1559 : ulong migration_slot,
1560 0 : fd_block_footer_t * footer ) {
1561 0 : fd_votor_certed_t const * fin = ctx->votor_final;
1562 0 : int use_fast = fin->slot!=ULONG_MAX && fin->kind==FD_CERT_KIND_FAST_FINAL;
1563 0 : int use_slow = fin->slot!=ULONG_MAX && fin->kind==FD_CERT_KIND_FINAL;
1564 :
1565 0 : footer->has_fast_final_cert = use_fast;
1566 0 : footer->has_final_cert = use_slow;
1567 0 : if( footer->has_fast_final_cert ) {
1568 0 : footer->has_fast_final_cert = fd_block_footer_cert_from_agg( &footer->fast_final_cert, fin->slot, fin->block_id.uc, &fin->agg );
1569 0 : }
1570 0 : if( footer->has_final_cert ) {
1571 0 : footer->has_final_cert = fd_block_footer_cert_from_agg( &footer->final_cert, fin->slot, NULL, &fin->agg ) &&
1572 0 : fd_block_footer_cert_from_agg( &footer->notar_cert, fin->slot, fin->block_id.uc, &fin->agg2 );
1573 0 : }
1574 :
1575 0 : int reward_ok = migration_slot!=ULONG_MAX &&
1576 0 : leader_slot>=migration_slot+FD_NUM_SLOTS_FOR_REWARD+1UL;
1577 0 : if( FD_LIKELY( reward_ok ) ) {
1578 0 : ulong reward_slot = leader_slot-FD_NUM_SLOTS_FOR_REWARD;
1579 0 : fd_votor_reward_t const * reward = &ctx->votor_reward[ reward_slot%(FD_NUM_SLOTS_FOR_REWARD+FD_VOTOR_SLOTS_PER_WINDOW+1UL) ];
1580 0 : footer->has_notar_reward_cert = reward->slot==reward_slot && !fd_bls_set_is_null( reward->agg_notar.set ) && fd_block_footer_cert_from_agg( &footer->notar_reward_cert, reward_slot, reward->block_id.uc, &reward->agg_notar );
1581 0 : footer->has_skip_reward_cert = reward->slot==reward_slot && !fd_bls_set_is_null( reward->agg_skip.set ) && fd_block_footer_cert_from_agg( &footer->skip_reward_cert, reward_slot, NULL, &reward->agg_skip );
1582 0 : }
1583 0 : }
1584 :
1585 : static void
1586 : publish_leader_footer( fd_replay_tile_t * ctx,
1587 : fd_stem_context_t * stem,
1588 : ulong slot,
1589 0 : fd_block_footer_t const * footer ) {
1590 0 : fd_replay_leader_footer_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1591 0 : msg->slot = slot;
1592 0 : msg->footer = *footer;
1593 :
1594 0 : ulong sz = sizeof(fd_replay_leader_footer_t);
1595 0 : publish_replay_out( ctx, stem, REPLAY_SIG_LEADER_FOOTER, sz );
1596 0 : }
1597 :
1598 : static inline int
1599 : try_become_leader( fd_replay_tile_t * ctx,
1600 : fd_stem_context_t * stem,
1601 : uint reason,
1602 0 : ulong end_slot ) {
1603 :
1604 0 : if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX ||
1605 0 : ctx->is_leader ||
1606 : /* (!ctx->identity_vote_rooted && ctx->wait_for_vote_to_start_leader) || // deprecated */
1607 0 : ctx->replay_out->idx==ULONG_MAX ||
1608 0 : !ctx->wfs_complete ) ) {
1609 0 : return 0;
1610 0 : }
1611 :
1612 0 : ulong parent_slot = ctx->votor_leader->parent_slot;
1613 0 : fd_hash_t const * parent_block_id = &ctx->votor_leader->parent_block_id;
1614 0 : fd_block_id_t parent_key = fd_block_id( parent_slot, parent_block_id->uc );
1615 :
1616 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &parent_key, NULL, ctx->block_id_arr );
1617 0 : if( FD_UNLIKELY( !block_id_ele ) ) return 0;
1618 0 : fd_bank_t * reset_bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
1619 0 : if( FD_UNLIKELY( !reset_bank || reset_bank->bank_seq!=block_id_ele->bank_seq || reset_bank->state!=FD_BANK_STATE_FROZEN ) ) return 0;
1620 :
1621 : /* A finalization above the parent, or a root that excludes it, means
1622 : we should give up our leader slot. */
1623 0 : if( FD_UNLIKELY( ( ctx->votor_final->slot!=ULONG_MAX && ctx->votor_final->slot>parent_slot ) ||
1624 0 : ( ctx->consensus_root_slot!=ULONG_MAX && ctx->consensus_root_slot>parent_slot ) ||
1625 0 : ( ctx->consensus_root_slot==parent_slot && !fd_hash_eq( &ctx->consensus_root, parent_block_id ) ) ) ) {
1626 0 : ctx->next_leader_slot = ULONG_MAX;
1627 0 : return 0;
1628 0 : }
1629 :
1630 : /* Certs made a later window ParentReady after votor sent this one:
1631 : the cluster skipped it. */
1632 0 : if( FD_UNLIKELY( ctx->next_leader_slot<ctx->votor_parent_ready_slot ) ) {
1633 0 : ctx->metrics.leader_window_superseded++;
1634 0 : ctx->next_leader_slot = ULONG_MAX;
1635 0 : return 0;
1636 0 : }
1637 :
1638 0 : if( FD_UNLIKELY( !fd_banks_can_start_bank( ctx->banks ) ) ) return 0;
1639 0 : if( FD_UNLIKELY( ctx->halt_replay ) ) return 0;
1640 0 : if( !ctx->supports_leader ) return 0;
1641 :
1642 : /* Don't become leader if the slot is not scheduled for the identity.
1643 : This can only happen in cases where the identity just switched. */
1644 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, ctx->next_leader_slot );
1645 0 : if( FD_UNLIKELY( !leader || !fd_pubkey_eq( leader, ctx->identity_pubkey ) ) ) {
1646 0 : ctx->next_leader_slot = ULONG_MAX;
1647 0 : return 0;
1648 0 : }
1649 :
1650 : /* The cluster already produced a slot of this window, eg. our own
1651 : past slots replayed during catch-up after a restart. */
1652 0 : ulong window_end = ctx->next_leader_slot - ctx->next_leader_slot%FD_VOTOR_SLOTS_PER_WINDOW + FD_VOTOR_SLOTS_PER_WINDOW;
1653 0 : for( ulong idx=0UL; idx<ctx->block_id_len; idx++ ) {
1654 0 : fd_block_id_ele_t const * ele = &ctx->block_id_arr[ idx ];
1655 0 : if( FD_LIKELY( ele->slot<ctx->next_leader_slot || ele->slot>=window_end ) ) continue;
1656 0 : fd_bank_t const * bank = fd_banks_bank_query( ctx->banks, idx );
1657 0 : if( FD_UNLIKELY( bank && bank->bank_seq==ele->bank_seq && !bank->is_leader ) ) {
1658 0 : ctx->metrics.leader_slot_already_produced++;
1659 0 : ctx->next_leader_slot = ULONG_MAX;
1660 0 : return 0;
1661 0 : }
1662 0 : }
1663 :
1664 : /* In Alpenglow, the "reset" block is signaled by ParentReady (a state
1665 : transition in the Votor consensus logic). ParentReady can occur
1666 : ahead of replay, so we reset here, once we know we are certain we
1667 : can build off the ParentReady bank. The parent is published to
1668 : the leader pipeline as part of the became leader message below. */
1669 :
1670 0 : ctx->reset_slot = parent_slot;
1671 :
1672 0 : FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
1673 0 : long now = fd_tickcount();
1674 :
1675 0 : fd_hash_t parent_cmr = block_id_ele->latest_mr;
1676 0 : fd_hash_t parent_dmr = *parent_block_id;
1677 :
1678 0 : fd_blockhashes_t const * block_hash_queue = &reset_bank->f.block_hash_queue;
1679 0 : fd_hash_t const * last_hash = fd_blockhashes_peek_last_hash( block_hash_queue );
1680 0 : FD_TEST( last_hash );
1681 0 : fd_hash_t parent_blockhash = *last_hash;
1682 :
1683 0 : long now_nanos = fd_clock_epoch_y( ctx->clock->epoch, now );
1684 :
1685 0 : ctx->is_leader = 1;
1686 0 : ctx->recv_poh = 0;
1687 :
1688 0 : memset( &ctx->leader_stats, 0, sizeof(ctx->leader_stats) );
1689 0 : ctx->leader_stats.slot = ctx->next_leader_slot;
1690 0 : ctx->leader_stats.timing_table_idx = ULONG_MAX;
1691 0 : ctx->leader_stats.became_leader_nanos = now_nanos;
1692 0 : ctx->leader_stats.leader_slot_start_nanos = now_nanos;
1693 :
1694 0 : FD_TEST( ctx->highwater_leader_slot==ULONG_MAX || ctx->highwater_leader_slot<ctx->next_leader_slot );
1695 0 : ctx->highwater_leader_slot = ctx->next_leader_slot;
1696 :
1697 0 : FD_LOG_INFO(( "becoming leader for slot %lu, parent slot is %lu", ctx->next_leader_slot, ctx->reset_slot ));
1698 :
1699 0 : fd_bank_t * bank = prepare_leader_bank( ctx, reset_bank, ctx->next_leader_slot, now_nanos );
1700 :
1701 0 : fd_bundle_crank_tip_payment_config_t config[1] = { 0 };
1702 0 : fd_pubkey_t tip_receiver_owner = {0};
1703 :
1704 0 : if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
1705 0 : fd_acct_addr_t tip_payment_config[1];
1706 0 : fd_acct_addr_t tip_receiver[1];
1707 0 : fd_bundle_crank_get_addresses( ctx->bundle.gen, bank->f.epoch, tip_payment_config, tip_receiver );
1708 :
1709 0 : fd_acc_t tip_config_acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, tip_payment_config->b );
1710 0 : if( FD_UNLIKELY( !tip_config_acc.lamports ) ) {
1711 0 : FD_BASE58_ENCODE_32_BYTES( tip_payment_config->b, tip_config_acc_b58 );
1712 0 : FD_LOG_WARNING(( "tip payment config account %s does not exist", tip_config_acc_b58 ));
1713 0 : fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
1714 0 : } else if( FD_UNLIKELY( tip_config_acc.data_len<sizeof(fd_bundle_crank_tip_payment_config_t) ) ) {
1715 0 : FD_LOG_HEXDUMP_WARNING(( "invalid tip payment config account data", tip_config_acc.data, tip_config_acc.data_len ));
1716 0 : fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
1717 0 : } else {
1718 0 : memcpy( config, tip_config_acc.data, sizeof(fd_bundle_crank_tip_payment_config_t) );
1719 0 : fd_accdb_unread_one( ctx->accdb, &tip_config_acc );
1720 0 : }
1721 :
1722 : /* It is possible that the tip receiver account does not exist yet
1723 : if it is the first time in an epoch. */
1724 0 : fd_acc_t tip_receiver_acc = fd_accdb_read_one( ctx->accdb, bank->accdb_fork_id, tip_receiver->b );
1725 0 : if( FD_LIKELY( tip_receiver_acc.lamports ) ) {
1726 0 : fd_memcpy( tip_receiver_owner.uc, tip_receiver_acc.owner, 32UL );
1727 0 : }
1728 0 : fd_accdb_unread_one( ctx->accdb, &tip_receiver_acc );
1729 0 : }
1730 :
1731 :
1732 : /* Like Agave's block_timeout, the block ends relative to the window's
1733 : ParentReady. If that is already past, end at once rather than run
1734 : into votor's skip timeout. */
1735 0 : long slot_end_ns = ctx->votor_leader->parent_ready_ns
1736 0 : + (long)( ( ctx->next_leader_slot%FD_VOTOR_SLOTS_PER_WINDOW+1UL )*bank->f.slot_params.ns_per_slot )
1737 0 : - (long)FD_TARGET_SLOT_ADJUSTMENT_NS - FD_TIME_TO_COMPLETE_BROADCAST_NS;
1738 :
1739 0 : fd_became_leader_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1740 0 : msg->slot = ctx->next_leader_slot;
1741 0 : msg->parent_slot = ctx->reset_slot;
1742 0 : msg->parent_cmr = parent_cmr;
1743 0 : msg->parent_dmr = parent_dmr;
1744 0 : msg->parent_blockhash = parent_blockhash;
1745 0 : msg->block_height = bank->f.block_height;
1746 0 : msg->slot_start_ns = now_nanos;
1747 0 : msg->slot_end_ns = fd_long_max( slot_end_ns, now_nanos );
1748 0 : msg->bank_idx = bank->idx;
1749 0 : msg->bank_seq = bank->bank_seq;
1750 0 : msg->ticks_per_slot = bank->f.ticks_per_slot;
1751 0 : msg->tick_duration_ns = bank->f.slot_params.ns_per_slot_adjusted/msg->ticks_per_slot;
1752 0 : msg->bundle->config[0] = config[0];
1753 0 : memcpy( msg->bundle->last_blockhash, bank->f.poh.hash, sizeof(fd_hash_t) );
1754 0 : memcpy( msg->bundle->tip_receiver_owner, tip_receiver_owner.uc, sizeof(fd_pubkey_t) );
1755 :
1756 0 : msg->max_microblocks_in_slot = FD_POH_ALPENGLOW_MAX_MICROBLOCKS_PER_SLOT;
1757 :
1758 0 : msg->epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, ctx->next_leader_slot, NULL );
1759 :
1760 0 : fd_cost_tracker_t const * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
1761 :
1762 0 : msg->limits.slot_max_cost = cost_tracker->block_cost_limit;
1763 0 : msg->limits.slot_max_write_cost_per_acct = cost_tracker->account_cost_limit;
1764 0 : msg->limits.slot_max_allocated_data_per_block = cost_tracker->data_size_limit;
1765 0 : msg->limits.slot_max_data_shreds = bank->f.slot_params.max_shred_idx;
1766 :
1767 0 : publish_replay_out( ctx, stem, REPLAY_SIG_BECAME_LEADER, sizeof(fd_became_leader_t) );
1768 0 : metrics_trigger( ctx, reason, reason==FD_METRICS_SAMPLE_REASON_LEADER_START ? msg->slot : end_slot );
1769 :
1770 0 : ctx->leader_window_start_ns = ctx->votor_leader->parent_ready_ns;
1771 0 : ctx->next_leader_slot = ULONG_MAX;
1772 :
1773 0 : return 1;
1774 0 : }
1775 :
1776 : /* replay_block_complete checks the footer's bank hash, then freezes and
1777 : publishes a finalized bank, whose footer certs all verified. Returns
1778 : 1 if it marked the bank dead. */
1779 :
1780 : static int
1781 : replay_block_complete( fd_replay_tile_t * ctx,
1782 : fd_stem_context_t * stem,
1783 : fd_bank_t * bank,
1784 : fd_block_footer_t const * footer,
1785 : ulong execution_fees_pre_settle,
1786 0 : ulong priority_fees_pre_settle ) {
1787 0 : fd_hash_t const * footer_bank_hash = &footer->bank_hash;
1788 0 : if( FD_UNLIKELY( memcmp( footer_bank_hash->uc, bank->f.bank_hash.uc, sizeof(fd_hash_t) ) ) ) {
1789 0 : FD_BASE58_ENCODE_32_BYTES( footer_bank_hash->uc, footer_bank_hash_b58 );
1790 0 : FD_BASE58_ENCODE_32_BYTES( bank->f.bank_hash.uc, executed_bank_hash_b58 );
1791 0 : FD_LOG_WARNING(( "slot %lu: bank hash mismatch, footer declares %s but executed %s. ", bank->f.slot, footer_bank_hash_b58, executed_bank_hash_b58 ));
1792 :
1793 : /* A bank hash mismatch detected now is only considered a true BHM
1794 : if the block_id also becomes finalized. If the finalization
1795 : already arrived, ahead of replay, check the parked
1796 : finalized_block_id_lo/hi. Rotor may also have detected the block
1797 : is finalized, on its delivery or a redelivery, so check the
1798 : block_id_ele. */
1799 0 : fd_block_id_t const * block_id = &ctx->block_id_arr[ bank->idx ].block_info;
1800 0 : if( FD_UNLIKELY( fd_block_id_eq( block_id, &ctx->finalized_block_id_lo ) || fd_block_id_eq( block_id, &ctx->finalized_block_id_hi )
1801 0 : || ctx->block_id_arr[ bank->idx ].finalized ) ) {
1802 0 : FD_BASE58_ENCODE_32_BYTES( block_id->hash.uc, block_id_b58 );
1803 0 : FD_LOG_ERR(( "HARD FORK DETECTED for slot %lu block ID `%s`", block_id->slot, block_id_b58 ));
1804 0 : }
1805 :
1806 0 : if( FD_LIKELY( !fd_bhm_map_ele_query( ctx->bhm_map, block_id, NULL, ctx->bhm_pool ) ) ) {
1807 0 : if( FD_UNLIKELY( !fd_bhm_pool_free( ctx->bhm_pool ) ) ) {
1808 : /* Pool exhausted, evict the oldest entry. */
1809 0 : fd_bhm_ele_t * oldest = fd_bhm_dlist_ele_pop_head( ctx->bhm_dlist, ctx->bhm_pool );
1810 0 : FD_TEST( fd_bhm_map_ele_remove( ctx->bhm_map, &oldest->block_id, NULL, ctx->bhm_pool )==oldest );
1811 0 : fd_bhm_pool_ele_release( ctx->bhm_pool, oldest );
1812 0 : }
1813 :
1814 0 : fd_bhm_ele_t * ele = fd_bhm_pool_ele_acquire( ctx->bhm_pool );
1815 0 : ele->block_id = *block_id;
1816 0 : FD_TEST( fd_bhm_map_ele_insert( ctx->bhm_map, ele, ctx->bhm_pool ) );
1817 0 : fd_bhm_dlist_ele_push_tail( ctx->bhm_dlist, ele, ctx->bhm_pool );
1818 0 : }
1819 :
1820 0 : mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, footer );
1821 0 : return 1;
1822 0 : } else {
1823 0 : FD_BASE58_ENCODE_32_BYTES( footer_bank_hash->uc, footer_bank_hash_b58 );
1824 0 : FD_BASE58_ENCODE_32_BYTES( bank->f.bank_hash.uc, executed_bank_hash_b58 );
1825 0 : FD_LOG_INFO(( "slot %lu: bank hash matches, footer declares %s, executed %s", bank->f.slot, footer_bank_hash_b58, executed_bank_hash_b58 ));
1826 0 : }
1827 :
1828 : /* Copy out cost tracker fields before freezing */
1829 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
1830 0 : cost_tracker_snap( bank, slot_info );
1831 :
1832 : /* fetch identity balance infrequently and after set-identity. */
1833 0 : slot_info->identity_balance = ULONG_MAX;
1834 0 : if( FD_UNLIKELY( ctx->identity_dirty || bank->f.slot%4096UL==0UL ) ) {
1835 0 : ctx->identity_dirty = 0;
1836 0 : slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
1837 0 : }
1838 :
1839 : /* Mark the bank as frozen. */
1840 0 : fd_block_id_ele_t const * block_id_ele = &ctx->block_id_arr[ bank->idx ];
1841 0 : bank->f.block_id = block_id_ele->dmr;
1842 0 : fd_banks_mark_bank_frozen( bank );
1843 0 : bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
1844 :
1845 : /**********************************************************************/
1846 : /* Bank hash comparison, and halt if there's a mismatch after replay */
1847 : /**********************************************************************/
1848 :
1849 : /* Must be last so we can measure completion time correctly, even
1850 : though we could technically do this before the hash cmp. */
1851 0 : publish_slot_completed( ctx, stem, bank, 0, 0 /* is_leader */, execution_fees_pre_settle, priority_fees_pre_settle, footer );
1852 :
1853 : /* The footer's finalization cert can also finalize. */
1854 0 : if( footer->has_fast_final_cert ) { try_advance_root( ctx, fd_block_id( footer->fast_final_cert.slot, footer->fast_final_cert.block_id.uc ) ); }
1855 0 : else if( footer->has_final_cert ) { try_advance_root( ctx, fd_block_id( footer->final_cert.slot, footer->notar_cert.block_id.uc ) ); }
1856 :
1857 : /* If enabled, dump the block to a file and reset the dumping
1858 : context state */
1859 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
1860 0 : fd_dump_block_to_protobuf( ctx->block_dump_ctx, ctx->banks, bank, ctx->accdb, ctx->dump_proto_ctx, ctx->runtime_stack );
1861 0 : fd_block_dump_context_reset( ctx->block_dump_ctx );
1862 0 : }
1863 0 : return 0;
1864 0 : }
1865 :
1866 : /* Finishes a block whose footer tasks ran on exec tiles, once all of
1867 : them have returned. This updates the leader's vote account with the
1868 : reward slices' summed leader credits and the bank hash. Returns 1 if
1869 : it marked the bank dead. */
1870 :
1871 : static int
1872 : replay_block_end( fd_replay_tile_t * ctx,
1873 : fd_stem_context_t * stem,
1874 : fd_bank_t * bank,
1875 : fd_block_footer_t const * footer,
1876 : ulong execution_fees_pre_settle,
1877 0 : ulong priority_fees_pre_settle ) {
1878 0 : fd_replay_footer_end_t * end = ctx->footer_end + bank->idx;
1879 0 : fd_runtime_block_footer_end( bank, ctx->accdb, ctx->footer_cache, &end->plan, end->leader_credits );
1880 0 : return replay_block_complete( ctx, stem, bank, footer, execution_fees_pre_settle, priority_fees_pre_settle );
1881 0 : }
1882 :
1883 : /* Sends one footer cert of the bank to an exec tile as a cert verify
1884 : task. The bank holds a reference and counts the footer task until
1885 : the tile reports back. */
1886 :
1887 : static void
1888 : replay_cert_publish( fd_replay_tile_t * ctx,
1889 : fd_stem_context_t * stem,
1890 : fd_bank_t * bank,
1891 : fd_block_footer_cert_t const * cert,
1892 : uint tag,
1893 : ulong quorum,
1894 0 : ulong t ) {
1895 0 : FD_TEST( ctx->shred_version );
1896 0 : ctx->cert_payload_sz[ t ] = fd_alpenglow_cert_payload( cert, tag, ctx->shred_version, ctx->cert_payload[ t ] );
1897 :
1898 0 : fd_bls_hash_t const * hm = fd_footer_cache_hash_query( ctx->footer_cache, ctx->cert_payload[ t ], ctx->cert_payload_sz[ t ] );
1899 :
1900 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ t ];
1901 0 : fd_execrp_cert_verify_msg_t * msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
1902 0 : msg->bank_idx = bank->idx;
1903 0 : msg->quorum = quorum;
1904 0 : msg->tag = tag;
1905 0 : msg->shred_version = ctx->shred_version;
1906 0 : msg->has_hm = !!hm;
1907 0 : if( hm ) msg->hm = *hm;
1908 0 : *msg->cert = *cert;
1909 :
1910 0 : bank->refcnt++;
1911 0 : bank->footer_task_cnt++;
1912 :
1913 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_CERT_VERIFY<<32) | t, exec_out->chunk, sizeof(fd_execrp_cert_verify_msg_t), 0UL, 0UL, 0UL );
1914 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*msg), exec_out->chunk0, exec_out->wmark );
1915 0 : }
1916 :
1917 : static int
1918 : replay_block_finalize( fd_replay_tile_t * ctx,
1919 : fd_stem_context_t * stem,
1920 0 : fd_bank_t * bank ) {
1921 0 : bank->last_transaction_finished_nanos = fd_clock_tile_now( ctx->clock );
1922 :
1923 : /* Set poh hash in bank. */
1924 0 : fd_hash_t * poh = fd_sched_get_poh( ctx->sched, bank->idx );
1925 0 : bank->f.poh = *poh;
1926 :
1927 : /* Set shred count in bank. */
1928 0 : bank->f.shred_cnt = fd_sched_get_shred_cnt( ctx->sched, bank->idx );
1929 :
1930 0 : ulong execution_fees_pre_settle = bank->f.execution_fees;
1931 0 : ulong priority_fees_pre_settle = bank->f.priority_fees;
1932 0 : ulong tips_pre_settle = bank->f.tips;
1933 :
1934 0 : fd_block_footer_t const * footer = NULL;
1935 0 : footer = fd_sched_get_footer( ctx->sched, bank->idx );
1936 :
1937 : /* Revert to in-band hashing for the end-of-block writers (sysvars,
1938 : fee settlement, incinerator, footer). With the delta folded in,
1939 : the bank's lthash covers every account as it currently is on this
1940 : fork, so it is correct to subtract the current fork version on
1941 : write. An in-band block's lthash is complete as it is. */
1942 0 : if( FD_LIKELY( bank->lthash_mode==FD_BANK_LTHASH_MODE_OOB_SKIP ) ) {
1943 0 : fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
1944 0 : fd_lthash_add( lthash, fd_sched_lthash_delta( ctx->sched, bank->idx ) );
1945 0 : fd_bank_lthash_end_locking_modify( bank );
1946 0 : bank->lthash_mode = FD_BANK_LTHASH_MODE_INBAND;
1947 0 : } else {
1948 0 : FD_TEST( bank->lthash_mode==FD_BANK_LTHASH_MODE_INBAND );
1949 0 : }
1950 :
1951 : /* Footer certs not already out on exec tiles go to idle ones now.
1952 : The rest verify here. The footer's vote rewards go to the tiles
1953 : still idle as slices, while this tile freezes the sysvars. The
1954 : fees settle after the slices, since the leader's collector may be a
1955 : signer's vote account. The bank fully completes on its last footer
1956 : task done. */
1957 0 : fd_block_footer_cert_t const * cert [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
1958 0 : uint tag [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
1959 0 : ulong quorum[ FD_ALPENGLOW_FOOTER_CERT_MAX ];
1960 :
1961 0 : fd_replay_footer_end_t * end = ctx->footer_end + bank->idx;
1962 0 : ulong cnt = 0UL;
1963 0 : if( footer && !ctx->capture_ctx && !ctx->report_runtime_diffs ) {
1964 0 : cnt = fd_alpenglow_footer_certs( footer, cert, tag, quorum );
1965 0 : ulong tiles = fd_sched_footer_dispatch( ctx->sched, bank->idx, cnt-end->cert_next );
1966 0 : for( ; tiles; end->cert_next++, tiles=fd_ulong_pop_lsb( tiles ) ) {
1967 0 : replay_cert_publish( ctx, stem, bank, cert[ end->cert_next ], tag[ end->cert_next ], quorum[ end->cert_next ], (ulong)fd_ulong_find_lsb( tiles ) );
1968 0 : }
1969 0 : }
1970 :
1971 0 : if( FD_UNLIKELY( !end->cert_next ) ) {
1972 : /* No cert went to a tile. Finish the block serially right here,
1973 : footer and all. The footer cache stays out of it while another
1974 : bank's slices may be writing its memo. */
1975 0 : if( FD_UNLIKELY( fd_runtime_block_execute_finalize( bank, ctx->accdb, ctx->capture_ctx, footer, ctx->shred_version, ctx->rewards_in_flight_cnt ? NULL : ctx->footer_cache ) ) ) goto bad_footer;
1976 0 : if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) replay_runtime_block_emit( ctx, bank, execution_fees_pre_settle, priority_fees_pre_settle, tips_pre_settle );
1977 0 : return replay_block_complete( ctx, stem, bank, footer, execution_fees_pre_settle, priority_fees_pre_settle );
1978 0 : }
1979 :
1980 0 : bank->execution_fees_pre_settle = execution_fees_pre_settle;
1981 0 : bank->priority_fees_pre_settle = priority_fees_pre_settle;
1982 0 : for( ulong i=end->cert_next; i<cnt; i++ ) {
1983 0 : if( FD_UNLIKELY( fd_alpenglow_cert_verify( bank, cert[ i ], tag[ i ], quorum[ i ], ctx->shred_version, ctx->footer_cache, NULL, NULL ) ) ) goto bad_footer;
1984 0 : }
1985 :
1986 0 : if( FD_UNLIKELY( fd_runtime_block_footer_begin( bank, ctx->accdb, footer, &end->plan ) ) ) goto bad_footer;
1987 : /* The footer cache memo has one slot per rank shared by every bank,
1988 : and a slice keeps pointers into a slot until it has hashed the
1989 : image there. Slices of another bank, dead or not, may still be out
1990 : on tiles, so this footer stays off the memo while any are. */
1991 0 : end->plan.use_memo = !ctx->rewards_in_flight_cnt;
1992 0 : if( FD_LIKELY( end->plan.has_reward || end->plan.has_final ) ) {
1993 0 : ulong slice_tiles = fd_sched_footer_dispatch( ctx->sched, bank->idx, ULONG_MAX );
1994 0 : ulong slice_cnt = (ulong)fd_ulong_popcnt( slice_tiles );
1995 0 : if( FD_UNLIKELY( !slice_cnt ) ) {
1996 : /* No idle exec tile. Run the one slice here. */
1997 0 : fd_lthash_value_t delta[1];
1998 0 : fd_lthash_zero( delta );
1999 0 : if( FD_UNLIKELY( fd_alpenglow_rewards_apply_slice( bank, ctx->accdb, NULL, ctx->footer_cache, &end->plan, 0UL, 1UL, delta, &end->leader_credits ) ) ) goto bad_footer;
2000 0 : fd_lthash_add( fd_bank_lthash_locking_modify( bank ), delta );
2001 0 : fd_bank_lthash_end_locking_modify( bank );
2002 0 : }
2003 0 : for( ulong i=0UL; slice_tiles; i++, slice_tiles=fd_ulong_pop_lsb( slice_tiles ) ) {
2004 0 : ulong t = (ulong)fd_ulong_find_lsb( slice_tiles );
2005 :
2006 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ t ];
2007 0 : fd_execrp_rewards_msg_t * msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
2008 0 : msg->bank_idx = bank->idx;
2009 0 : msg->slice_idx = i;
2010 0 : msg->slice_cnt = slice_cnt;
2011 0 : msg->plan = end->plan;
2012 :
2013 0 : bank->refcnt++;
2014 :
2015 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_REWARDS<<32) | t, exec_out->chunk, sizeof(fd_execrp_rewards_msg_t), 0UL, 0UL, 0UL );
2016 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*msg), exec_out->chunk0, exec_out->wmark );
2017 0 : }
2018 0 : bank->footer_task_cnt += slice_cnt;
2019 0 : ctx->rewards_in_flight_cnt += slice_cnt;
2020 0 : }
2021 0 : fd_runtime_freeze_sysvars( bank, ctx->accdb, NULL );
2022 :
2023 0 : end->finalized = 1;
2024 0 : if( FD_LIKELY( bank->footer_task_cnt ) ) return 0;
2025 0 : return replay_block_end( ctx, stem, bank, footer, execution_fees_pre_settle, priority_fees_pre_settle );
2026 :
2027 0 : bad_footer:
2028 0 : mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
2029 0 : return 1;
2030 0 : }
2031 :
2032 : static int
2033 : try_fini_leader( fd_replay_tile_t * ctx,
2034 0 : fd_stem_context_t * stem ) {
2035 :
2036 : /* If we are leader, we can only unbecome the leader iff we have
2037 : received the poh hash from the poh tile and block id from reasm.
2038 : The block id entry is claimed for the leader slot in
2039 : prepare_leader_bank, so a slot mismatch here means the claim
2040 : discipline broke. */
2041 :
2042 0 : if( FD_LIKELY( !ctx->is_leader ) ) return 0;
2043 0 : if( !ctx->recv_poh ) return 0;
2044 :
2045 : /* The consensus root only advances through FROZEN banks and the
2046 : leader bank is frozen only below, so a root at or past our slot
2047 : can never include the leader bank.
2048 :
2049 : We can't include votor-only messages, because it's possible our
2050 : block was retransmitted but replay hasn't processed the FEC yet
2051 : (due to bespoke repair <=> replay concurrency). */
2052 :
2053 0 : if( FD_UNLIKELY( !ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen &&
2054 0 : ctx->consensus_root_slot!=ULONG_MAX &&
2055 0 : ctx->consensus_root_slot>=ctx->leader_bank->f.slot ) ) {
2056 0 : ulong bank_idx = ctx->leader_bank->idx;
2057 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, ctx->leader_bank->f.slot );
2058 0 : ctx->leader_bank->refcnt--;
2059 0 : ctx->leader_bank = NULL;
2060 0 : ctx->recv_poh = 0;
2061 0 : ctx->is_leader = 0;
2062 0 : mark_bank_dead( ctx, stem, bank_idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED, NULL );
2063 0 : return 1;
2064 0 : }
2065 :
2066 0 : if( !ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen ) return 0;
2067 0 : FD_TEST( ctx->block_id_arr[ ctx->leader_bank->idx ].slot==ctx->leader_bank->f.slot );
2068 :
2069 0 : ulong curr_slot = ctx->leader_bank->f.slot;
2070 :
2071 0 : ulong execution_fees_pre_settle;
2072 0 : ulong priority_fees_pre_settle;
2073 0 : ulong tips_pre_settle;
2074 :
2075 : /* Already finalized above, when the footer was published. */
2076 :
2077 0 : execution_fees_pre_settle = ctx->leader_execution_fees;
2078 0 : priority_fees_pre_settle = ctx->leader_priority_fees;
2079 0 : tips_pre_settle = ctx->leader_tips;
2080 :
2081 0 : ctx->leader_bank->f.shred_cnt = (ulong)ctx->block_id_arr[ ctx->leader_bank->idx ].fec_cnt*FD_FEC_SHRED_CNT;
2082 :
2083 0 : if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) replay_runtime_block_emit( ctx, ctx->leader_bank, execution_fees_pre_settle, priority_fees_pre_settle, tips_pre_settle );
2084 :
2085 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2086 0 : cost_tracker_snap( ctx->leader_bank, slot_info );
2087 0 : slot_info->identity_balance = ULONG_MAX;
2088 0 : if( FD_UNLIKELY( ctx->identity_dirty || curr_slot%4096UL==0UL ) ) {
2089 0 : ctx->identity_dirty = 0;
2090 0 : slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, ctx->leader_bank->accdb_fork_id, ctx->identity_pubkey->uc );
2091 0 : }
2092 :
2093 0 : ctx->leader_bank->f.block_id = ctx->block_id_arr[ ctx->leader_bank->idx ].dmr;
2094 0 : fd_banks_mark_bank_frozen( ctx->leader_bank );
2095 0 : ctx->leader_bank->block_completed_nanos = fd_clock_tile_now( ctx->clock );
2096 :
2097 0 : publish_slot_completed( ctx, stem, ctx->leader_bank, 0, 1 /* is_leader */, execution_fees_pre_settle, priority_fees_pre_settle, ctx->leader_footer );
2098 :
2099 : /* The reference on the bank is finally no longer needed. */
2100 0 : ctx->leader_bank->refcnt--;
2101 :
2102 0 : fd_bank_t * completed = ctx->leader_bank;
2103 :
2104 : /* We are no longer leader so we can clear the bank index we use for
2105 : being the leader. */
2106 0 : ctx->leader_bank = NULL;
2107 0 : ctx->recv_poh = 0;
2108 0 : ctx->is_leader = 0;
2109 :
2110 0 : if( FD_UNLIKELY( ctx->next_leader_slot==ULONG_MAX && (curr_slot+1UL)%FD_VOTOR_SLOTS_PER_WINDOW ) ) {
2111 0 : *ctx->votor_leader = (fd_votor_leader_t){
2112 0 : .slot = curr_slot+1UL,
2113 0 : .parent_slot = curr_slot,
2114 0 : .parent_block_id = ctx->block_id_arr[ completed->idx ].dmr,
2115 0 : .parent_ready_ns = ctx->leader_window_start_ns
2116 0 : };
2117 0 : ctx->next_leader_slot = curr_slot+1UL;
2118 0 : if( try_become_leader( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_END_START, curr_slot ) ) return 1;
2119 0 : }
2120 :
2121 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, curr_slot );
2122 0 : return 1;
2123 0 : }
2124 :
2125 : static void
2126 : publish_root_advanced( fd_replay_tile_t * ctx,
2127 : fd_stem_context_t * stem,
2128 0 : fd_bank_t * bank ) {
2129 :
2130 : /* If the new consensus root is in the next epoch from the one the
2131 : replay tile currently holds, send the next epoch's leader schedule.
2132 : We can't use the new root's parent slot safely here. */
2133 0 : if( FD_UNLIKELY( bank->f.epoch>fd_slot_to_epoch( &bank->f.epoch_schedule, ctx->notified_root_slot, NULL ) ) ) {
2134 0 : fd_runtime_update_next_leaders( bank, ctx->runtime_stack );
2135 0 : publish_epoch_info( ctx, stem, bank, 1 );
2136 0 : }
2137 :
2138 0 : if( ctx->rpc_enabled ) {
2139 0 : bank->refcnt++;
2140 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for rpc", bank->idx, bank->f.slot, bank->refcnt ));
2141 :
2142 : /* Under Alpenglow Agave treats a rooted bank as both confirmed
2143 : and finalized. The rpc tile holds an independent reference for
2144 : each alias. */
2145 0 : bank->refcnt++;
2146 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for rpc (confirmed alias)", bank->idx, bank->f.slot, bank->refcnt ));
2147 0 : }
2148 :
2149 : /* Increment the reference count on the consensus root bank to account
2150 : for the number of resolv tiles that are waiting on it. */
2151 0 : bank->refcnt += ctx->resolv_tile_cnt;
2152 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for resolv", bank->idx, bank->f.slot, bank->refcnt ));
2153 :
2154 0 : fd_replay_root_advanced_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2155 : /* f.* first: gcc 11 SLP otherwise materialises the 27 KB idx..f.slot gap element-wise */
2156 0 : msg->slot = bank->f.slot;
2157 0 : msg->bank_hash = bank->f.bank_hash;
2158 0 : msg->block_id = bank->f.block_id;
2159 0 : msg->bank_idx = bank->idx;
2160 0 : msg->bank_seq = bank->bank_seq;
2161 :
2162 0 : publish_replay_out( ctx, stem, REPLAY_SIG_ROOT_ADVANCED, sizeof(fd_replay_root_advanced_t) );
2163 :
2164 : /* Drop every FEC set below the new root from the store. */
2165 :
2166 0 : fd_store_publish( ctx->store, ctx->map_join, bank->f.slot );
2167 0 : }
2168 :
2169 : /* publish_boot_slot announces the snapshot slot, which the validator
2170 : did not replay, as completed and rooted. */
2171 :
2172 : static void
2173 : publish_boot_slot( fd_replay_tile_t * ctx,
2174 : fd_stem_context_t * stem,
2175 0 : fd_bank_t * bank ) {
2176 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2177 0 : cost_tracker_snap( bank, slot_info );
2178 0 : slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
2179 :
2180 0 : fd_block_footer_t footer = { .bank_hash = bank->f.bank_hash }; /* dummy footer for the snapshot slot */
2181 0 : publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */, 0, 0, &footer );
2182 0 : publish_root_advanced( ctx, stem, bank );
2183 0 : }
2184 :
2185 : /* Determine the default slot params to use for slots where no
2186 : reduce_slot_time feature gate is in effect. This is important for
2187 : the inflation calculations, which use the slot times for historical
2188 : slots as input. Therefore we need the same semantics as Agave, even
2189 : after the reduce_slot_time feature gates are active.
2190 : https://github.com/anza-xyz/agave/blob/4f8937b9568f7672ce0edcf67501666a95df015b/runtime/src/bank.rs#L2919-L2942 */
2191 : static fd_slot_params_t
2192 : restore_default_slot_params( fd_bank_t const * bank,
2193 0 : fd_genesis_t const * genesis ) {
2194 0 : fd_slot_params_t genesis_params = fd_slot_params_from_genesis( genesis );
2195 :
2196 : /* A reduction is effective if any reduce_slot_time gate with a slot
2197 : time no greater than the genesis slot time is effective.
2198 : https://github.com/anza-xyz/agave/blob/4f8937b9568f7672ce0edcf67501666a95df015b/runtime/src/slot_params.rs#L332-L349 */
2199 0 : int reduction_effective = fd_slot_params_any_reduction_effective( &bank->f.features,
2200 0 : &bank->f.epoch_schedule,
2201 0 : bank->f.slot,
2202 0 : genesis_params.ns_per_slot );
2203 :
2204 : /* If a reduction is effective, the restored bank's slot params no
2205 : longer describe the pre-reduction slots, so use the genesis slot
2206 : params as the default. */
2207 0 : if( reduction_effective ) return genesis_params;
2208 :
2209 : /* If a reduction is not effective, use the slot params from the
2210 : manifest. These may differ from genesis for values changed by
2211 : older feature gates (e.g. hashes_per_tick). */
2212 0 : return bank->f.slot_params;
2213 0 : }
2214 :
2215 : /* refresh_vote_account_staked recomputes vote_account_staked from the
2216 : root stake delegations and seeds vote_account_inadmissible if stake
2217 : effective in the root's epoch was not admitted at its boundary.
2218 : Admitted implies staked, so only a non-admitted vote account pays
2219 : for the scan. Called at boot and when the root crosses an epoch. */
2220 :
2221 : static void
2222 : refresh_vote_account_staked( fd_replay_tile_t * ctx,
2223 0 : fd_bank_t * bank ) {
2224 0 : fd_node_info_t node_info[1]; fd_node_info_read( node_info, ctx->node_info );
2225 0 : fd_pubkey_t const * vote_key = &node_info->vote_account;
2226 0 : int has_vote_key = !fd_pubkey_check_zero( vote_key );
2227 0 : int admitted = has_vote_key && fd_vote_stakes_query_t_1( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, vote_key, NULL );
2228 0 : ctx->vote_account_staked = admitted;
2229 0 : if( FD_LIKELY( admitted || !has_vote_key ) ) return;
2230 :
2231 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
2232 0 : fd_stake_history_t stake_history_[1];
2233 0 : fd_stake_history_t const * stake_history = fd_sysvar_stake_history_read( ctx->accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
2234 0 : fd_stake_history_entry_t status = fd_stake_delegations_vote_account_status(
2235 0 : fd_banks_stake_delegations_root_query( ctx->banks ), vote_key, bank->f.epoch, stake_history,
2236 0 : &bank->f.warmup_cooldown_rate_epoch );
2237 0 : ctx->vote_account_staked = status.effective || status.activating;
2238 0 : ctx->vote_account_inadmissible = !!status.effective;
2239 0 : }
2240 :
2241 : static void
2242 : init_after_snapshot( fd_replay_tile_t * ctx,
2243 0 : fd_stem_context_t * stem ) {
2244 : /* snapin built the root stake delegations while writing the accounts
2245 : db index. Refresh finalizes them in memory. */
2246 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
2247 0 : if( FD_UNLIKELY( !bank ) ) {
2248 0 : FD_LOG_CRIT(( "invariant violation: replay bank is NULL at bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
2249 0 : }
2250 :
2251 0 : char const * one_offs[ 16UL ];
2252 0 : for( ulong i=0UL; i<ctx->enable_features_cnt; i++ ) one_offs[ i ] = ctx->enable_features[ i ];
2253 0 : fd_features_enable_one_offs( &bank->f.features, one_offs, (uint)ctx->enable_features_cnt, 0UL );
2254 0 : FD_CHECK_CRIT( FD_FEATURE_ACTIVE_BANK( bank, alpenglow ), "alpenglow feature is not active" );
2255 0 : bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( bank, ctx->accdb );
2256 :
2257 : /* Set slot params based on the feature gates in the snapshot,
2258 : and assert that these are consistent with the values from the
2259 : manifest. These assertions match Agave:
2260 : https://github.com/anza-xyz/agave/blob/v4.2/runtime/src/bank.rs#L4839-L4869 */
2261 0 : fd_slot_params_t manifest_params = bank->f.slot_params;
2262 0 : bank->f.slot_params_default = restore_default_slot_params( bank, ctx->genesis );
2263 0 : bank->f.slot_params = fd_slot_params_at_slot( bank, bank->f.slot );
2264 0 : FD_TEST( bank->f.slot_params.ns_per_slot == manifest_params.ns_per_slot );
2265 0 : FD_TEST( bank->f.slot_params.slots_per_year == manifest_params.slots_per_year );
2266 :
2267 0 : fd_runtime_update_next_leaders( bank, ctx->runtime_stack );
2268 0 : fd_runtime_update_leaders( bank, ctx->runtime_stack );
2269 :
2270 : /* Typically, when we cross an epoch boundary during normal
2271 : operation, we publish the stake weights for the new epoch. But
2272 : since we are starting from a snapshot, we need to publish two
2273 : epochs worth of stake weights: the previous epoch (which is
2274 : needed for voting on the current epoch), and the current epoch
2275 : (which is needed for voting on the next epoch). */
2276 0 : publish_epoch_info( ctx, stem, bank, 0 );
2277 0 : publish_epoch_info( ctx, stem, bank, 1 );
2278 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, bank->f.slot );
2279 0 : FD_TEST( leader );
2280 0 : bank->leader = *leader;
2281 :
2282 : /* Agave asserts that the manifest leader_id matches the computed slot
2283 : leader. At slot 0, Agave uses the highest staked node instead.
2284 : https://github.com/anza-xyz/agave/blob/v4.2.0/runtime/src/bank.rs#L2056-L2082 */
2285 0 : if( FD_UNLIKELY( bank->f.slot && !fd_pubkey_eq( leader, &ctx->manifest_leader_id ) ) ) {
2286 0 : FD_BASE58_ENCODE_32_BYTES( ctx->manifest_leader_id.uc, manifest_b58 );
2287 0 : FD_BASE58_ENCODE_32_BYTES( leader->uc, computed_b58 );
2288 0 : FD_LOG_ERR(( "invalid snapshot: leader_id %s does not match computed slot leader %s at slot %lu",
2289 0 : manifest_b58, computed_b58, bank->f.slot ));
2290 0 : }
2291 :
2292 0 : fd_progcache_reset( ctx->progcache );
2293 0 : bank->progcache_fork_id = fd_progcache_fork_id_initial();
2294 :
2295 0 : bank->f.warmup_cooldown_rate_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, bank->f.features.reduce_stake_warmup_cooldown, NULL );
2296 0 : fd_stake_delegations_t * root_delegations = fd_banks_stake_delegations_root_query( ctx->banks );
2297 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
2298 0 : fd_stake_history_t stake_history_[1];
2299 0 : fd_stake_history_t const * stake_history = fd_sysvar_stake_history_read( ctx->accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
2300 0 : fd_stake_delegations_refresh(
2301 0 : root_delegations,
2302 0 : bank->f.epoch,
2303 0 : stake_history, /* may be NULL */
2304 0 : &bank->f.warmup_cooldown_rate_epoch,
2305 0 : FD_FEATURE_ACTIVE_BANK( bank, remove_inactive_stakes ) );
2306 0 : fd_stake_history_entry_t totals = fd_stake_delegations_totals( root_delegations );
2307 0 : bank->f.total_effective_stake = totals.effective;
2308 0 : bank->f.total_activating_stake = totals.activating;
2309 0 : bank->f.total_deactivating_stake = totals.deactivating;
2310 :
2311 : /* Emit the stake-delegations boot baseline from the finalized root
2312 : cache (snapshot accepted and refreshed, or genesis loaded), so the
2313 : rows match the post-reconciliation cache rather than the raw
2314 : account stream. */
2315 0 : if( FD_UNLIKELY( ctx->report_runtime_diffs ) ) {
2316 0 : fd_stake_delegations_iter_t iter_[1];
2317 0 : for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, root_delegations );
2318 0 : !fd_stake_delegations_iter_done( iter );
2319 0 : fd_stake_delegations_iter_next( iter ) ) {
2320 0 : fd_stake_delegation_t const * d = fd_stake_delegations_iter_ele( iter );
2321 0 : ulong ae = d->activation_epoch ==(ushort)USHORT_MAX ? ULONG_MAX : (ulong)d->activation_epoch;
2322 0 : ulong de = d->deactivation_epoch==(ushort)USHORT_MAX ? ULONG_MAX : (ulong)d->deactivation_epoch;
2323 0 : fd_event_runtime_stake_delegation_bootup_emit( bank->f.slot, bank->f.epoch, d->stake_account.uc,
2324 0 : d->vote_account.uc, d->stake, ae, de, d->credits_observed );
2325 0 : }
2326 0 : }
2327 :
2328 0 : fd_vote_stakes_refresh( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, ctx->accdb, bank->accdb_fork_id );
2329 :
2330 0 : refresh_vote_account_staked( ctx, bank );
2331 :
2332 : /* After both snapshots have been loaded in, we can determine if we should
2333 : start distributing rewards. */
2334 :
2335 0 : fd_rewards_recalculate_partitioned_rewards( bank, ctx->accdb, ctx->runtime_stack, ctx->capture_ctx );
2336 :
2337 : /* Signals fd_startup_gate */
2338 0 : FD_MGAUGE_SET( REPLAY, IS_RUNTIME_LOADED, 1UL );
2339 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_REPLAY_READY, bank->f.slot );
2340 0 : }
2341 :
2342 : static ulong
2343 : enforce_nanosecond_clock_bounds( fd_replay_tile_t * ctx,
2344 : fd_bank_t * bank,
2345 0 : ulong now_nanos ) {
2346 0 : long lo, hi;
2347 0 : fd_alpenglow_producer_time_bounds( bank, ctx->accdb, &lo, &hi );
2348 0 : return (ulong)fd_long_max( lo, fd_long_min( hi, (long)fd_ulong_min( now_nanos, (ulong)LONG_MAX ) ) );
2349 0 : }
2350 :
2351 : static void
2352 : process_motor_done( fd_replay_tile_t * ctx,
2353 : fd_stem_context_t * stem,
2354 0 : fd_poh_leader_slot_ended_t const * slot_ended ) {
2355 :
2356 0 : FD_TEST( ctx->is_booted );
2357 0 : FD_TEST( ctx->is_leader );
2358 0 : FD_TEST( ctx->leader_bank!=NULL );
2359 :
2360 0 : FD_TEST( ctx->highwater_leader_slot>=slot_ended->slot );
2361 0 : FD_TEST( ctx->next_leader_slot>ctx->highwater_leader_slot );
2362 :
2363 0 : if( FD_LIKELY( ctx->leader_stats.slot==slot_ended->slot ) ) {
2364 0 : ctx->leader_stats.microblock_count = slot_ended->microblock_count;
2365 0 : ctx->leader_stats.pack_block_cost = slot_ended->pack_block_cost;
2366 0 : ctx->leader_stats.pack_data_bytes = slot_ended->pack_data_bytes;
2367 0 : ctx->leader_stats.bundle_txn_count = slot_ended->bundle_txn_count;
2368 0 : ctx->leader_stats.pack_end_reason = slot_ended->pack_end_reason;
2369 0 : ctx->leader_stats.pack_start_nanos = slot_ended->pack_start_ns;
2370 0 : ctx->leader_stats.pack_end_nanos = slot_ended->pack_end_ns;
2371 0 : ctx->leader_stats.timing_table_idx = slot_ended->timing_table_idx;
2372 0 : }
2373 :
2374 : /* unpin when we know leader bank done executing txns */
2375 0 : fd_cost_tracker_store_unpin( fd_bank_cost_tracker( ctx->leader_bank ), ctx->leader_bank->cost_tracker_fork_id );
2376 :
2377 0 : if( FD_UNLIKELY( !slot_ended->completed ) ) {
2378 : /* The leader slot was aborted by a reset mid-production. The
2379 : block-complete entry was never emitted, so no slot-complete FEC
2380 : (and thus no block id) will ever arrive. */
2381 0 : ulong bank_idx = ctx->leader_bank->idx;
2382 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_LEADER_END, slot_ended->slot );
2383 0 : ctx->leader_bank->refcnt--;
2384 0 : ctx->leader_bank = NULL;
2385 0 : ctx->recv_poh = 0;
2386 0 : ctx->is_leader = 0;
2387 0 : mark_bank_dead( ctx, stem, bank_idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_RESET, NULL );
2388 0 : return;
2389 0 : }
2390 :
2391 : /* Update the poh hash in the bank. We will want to maintain a refcnt
2392 : on the bank until we have received the block id for the block after
2393 : it has been shredded. */
2394 :
2395 0 : memcpy( &ctx->leader_bank->f.poh, slot_ended->blockhash, sizeof(fd_hash_t) );
2396 :
2397 0 : ctx->recv_poh = 1;
2398 :
2399 0 : ctx->leader_bank->last_transaction_finished_nanos = fd_clock_tile_now( ctx->clock );
2400 0 : fd_sched_block_add_done( ctx->sched, ctx->leader_bank->idx, ctx->leader_bank->parent_idx, ctx->leader_bank->f.slot );
2401 :
2402 : /* cache fees before runtime zeros */
2403 :
2404 0 : ctx->leader_execution_fees = ctx->leader_bank->f.execution_fees;
2405 0 : ctx->leader_priority_fees = ctx->leader_bank->f.priority_fees;
2406 0 : ctx->leader_tips = ctx->leader_bank->f.tips;
2407 :
2408 0 : fd_block_footer_t * footer = ctx->leader_footer;
2409 0 : fd_memset( footer, 0, sizeof(fd_block_footer_t) );
2410 0 : footer->block_producer_time_nanos = enforce_nanosecond_clock_bounds( ctx, ctx->leader_bank, (ulong)fd_clock_tile_now( ctx->clock ) );
2411 :
2412 0 : ulong migration_slot = fd_alpenglow_migration_slot( ctx->leader_bank, ctx->accdb );
2413 0 : construct_footer_certs( ctx, ctx->leader_bank->f.slot, migration_slot, footer );
2414 :
2415 : /* The block goes out regardless: the certs are already committed to
2416 : the bank hash, so there is nothing left to fall back to. */
2417 : /* The footer cache stays out of this while another bank's slices may
2418 : be writing its memo. */
2419 0 : if( FD_UNLIKELY( fd_runtime_block_execute_finalize( ctx->leader_bank, ctx->accdb, ctx->capture_ctx, footer, ctx->shred_version, ctx->rewards_in_flight_cnt ? NULL : ctx->footer_cache ) ) ) {
2420 0 : FD_LOG_WARNING(( "slot %lu: our own block footer certs did not apply; the block we produce will be dead to the cluster", ctx->leader_bank->f.slot ));
2421 0 : }
2422 0 : footer->bank_hash = ctx->leader_bank->f.bank_hash;
2423 :
2424 0 : publish_leader_footer( ctx, stem, ctx->leader_bank->f.slot, footer );
2425 0 : }
2426 :
2427 : static void
2428 : boot_genesis( fd_replay_tile_t * ctx,
2429 : fd_stem_context_t * stem,
2430 0 : fd_genesis_meta_t const * meta ) {
2431 :
2432 0 : if( FD_UNLIKELY( ctx->wfs_enabled ) ) {
2433 0 : FD_LOG_ERR(( "wait_for_supermajority is enabled but the validator is booting from genesis. "
2434 0 : "Remove [consensus.wait_for_supermajority_at_slot] and "
2435 0 : "[consensus.wait_for_supermajority_with_bank_hash] configuration and restart." ));
2436 0 : }
2437 :
2438 : /* Firedancer only boots Alpenglow clusters from genesis. The
2439 : genesis certificate certifies slot 0 with a zero block id. */
2440 0 : fd_memset( &ctx->initial_block_id, 0, sizeof(fd_hash_t) );
2441 :
2442 : /* If we are bootstrapping, we can't wait to wait for our identity
2443 : vote to be rooted as this creates a circular dependency. */
2444 0 : ctx->identity_vote_rooted = 1;
2445 :
2446 0 : ctx->caught_up = 1;
2447 0 : FD_MGAUGE_SET( REPLAY, IS_CAUGHT_UP, 1UL );
2448 0 : metrics_trigger( ctx, FD_METRICS_SAMPLE_REASON_CAUGHT_UP, 0UL );
2449 :
2450 0 : ctx->hard_fork_cnt = 0UL;
2451 :
2452 0 : uchar const * genesis_blob = (uchar const *)( meta+1 );
2453 0 : FD_TEST( meta->bootstrap && meta->has_lthash );
2454 0 : FD_TEST( fd_genesis_parse( ctx->genesis, genesis_blob, meta->blob_sz ) );
2455 :
2456 0 : fd_bank_t * bank = fd_banks_init_bank( ctx->banks );
2457 0 : FD_TEST( bank );
2458 0 : bank->f.slot = 0UL;
2459 0 : FD_TEST( bank->idx==FD_REPLAY_BOOT_BANK_IDX );
2460 :
2461 0 : FD_TEST( meta->accdb_root.val!=USHORT_MAX );
2462 0 : bank->accdb_fork_id = meta->accdb_root;
2463 0 : bank->parent_accdb_fork_id = bank->accdb_fork_id;
2464 :
2465 0 : fd_runtime_read_genesis( ctx->banks, bank, ctx->accdb, NULL, &meta->genesis_hash, &meta->lthash, ctx->genesis, genesis_blob, ctx->runtime_stack );
2466 0 : FD_TEST( fd_sysvar_cache_validate( bank, ctx->accdb ) );
2467 :
2468 0 : fd_txncache_reset( ctx->txncache );
2469 0 : bank->txncache_fork_id = fd_txncache_attach_child ( ctx->txncache, (fd_txncache_fork_id_t){USHORT_MAX} );
2470 :
2471 0 : fd_hash_t const * block_hash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
2472 0 : fd_txncache_finalize_fork( ctx->txncache, bank->txncache_fork_id, 0UL, block_hash->uc );
2473 :
2474 : /* We call this after fd_runtime_read_genesis, which sets up the
2475 : slot_bank needed in blockstore_init. */
2476 0 : init_after_snapshot( ctx, stem );
2477 :
2478 0 : ctx->published_root_slot = 0UL;
2479 0 : fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, 0UL );
2480 :
2481 0 : bank->f.block_height = 1UL;
2482 :
2483 0 : ctx->consensus_root = ctx->initial_block_id;
2484 0 : ctx->consensus_root_slot = 0UL;
2485 0 : ctx->finalized_block_id_lo = fd_block_id( 0UL, ctx->initial_block_id.uc );
2486 0 : ctx->finalized_block_id_hi = ctx->finalized_block_id_lo;
2487 0 : ctx->notified_root = ctx->initial_block_id;
2488 0 : ctx->notified_root_slot = 0UL;
2489 0 : ctx->notified_root_bank = bank;
2490 0 : ctx->published_root_slot = 0UL;
2491 0 : ctx->published_root_bank_idx = 0UL;
2492 0 : if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks ) ) {
2493 0 : ulong interval = ctx->snapmk.full_interval_blocks;
2494 0 : ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
2495 0 : }
2496 0 : if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
2497 0 : ulong interval = ctx->snapmk.incremental_interval_blocks;
2498 0 : ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
2499 0 : }
2500 :
2501 0 : ctx->reset_slot = 0UL;
2502 0 : ctx->epoch_end_slot = 0UL;
2503 0 : ctx->slots_per_epoch = 0UL;
2504 0 : ctx->ns_per_slot = 0UL;
2505 :
2506 0 : ctx->has_cluster_type = 1;
2507 :
2508 0 : ctx->is_booted = 1;
2509 :
2510 0 : fd_hash_t initial_block_id = ctx->initial_block_id;
2511 :
2512 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
2513 0 : block_id_ele->latest_mr = initial_block_id;
2514 0 : block_id_ele->dmr = initial_block_id;
2515 0 : block_id_ele->slot = 0UL;
2516 0 : block_id_ele->bank_seq = bank->bank_seq;
2517 0 : block_id_ele->block_id_seen = 1;
2518 0 : block_id_ele->latest_fec_idx = 0U;
2519 0 : block_id_ele->block_info = fd_block_id( 0UL, initial_block_id.uc );
2520 0 : bank->f.block_id = initial_block_id;
2521 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
2522 :
2523 0 : fd_replay_slot_completed_t * slot_info = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
2524 0 : cost_tracker_snap( bank, slot_info );
2525 :
2526 0 : slot_info->identity_balance = fd_accdb_lamports( ctx->accdb, bank->accdb_fork_id, ctx->identity_pubkey->uc );
2527 :
2528 0 : FD_LOG_INFO(( "replay ready at slot %lu (%.3f s after snapshot done, %.3f s since boot)",
2529 0 : 0UL, 0.0, (double)(fd_log_wallclock()-ctx->boot_timestamp_nanos)/1e9 ));
2530 :
2531 0 : fd_block_footer_t footer = { .bank_hash = bank->f.bank_hash }; /* dummy footer for genesis slot */
2532 0 : publish_slot_completed( ctx, stem, bank, 1, 0 /* is_leader */, 0, 0, &footer );
2533 0 : publish_root_advanced( ctx, stem, bank );
2534 :
2535 0 : }
2536 :
2537 : static inline void
2538 0 : maybe_verify_cluster_type( fd_replay_tile_t * ctx ) {
2539 0 : if( FD_UNLIKELY( !ctx->has_cluster_type || !ctx->has_genesis_hash ) ) {
2540 0 : return;
2541 0 : }
2542 :
2543 0 : FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc, hash_cstr );
2544 0 : ulong cluster = fd_genesis_cluster_identify( hash_cstr );
2545 : /* Map pyth-related clusters to unknown. */
2546 0 : switch( cluster ) {
2547 0 : case FD_CLUSTER_PYTHNET:
2548 0 : case FD_CLUSTER_PYTHTEST:
2549 0 : cluster = FD_CLUSTER_UNKNOWN;
2550 0 : }
2551 :
2552 0 : if( FD_UNLIKELY( cluster!=ctx->cluster_type ) ) {
2553 0 : FD_LOG_ERR(( "Your genesis.bin file at `%s` has a genesis hash of `%s` which means the cluster is %s "
2554 0 : "but the snapshot you loaded is for a different cluster %s. If you are trying to join the "
2555 0 : "%s cluster, you can delete the genesis.bin file and restart the node to download the correct "
2556 0 : "genesis file automatically.",
2557 0 : ctx->genesis_path,
2558 0 : hash_cstr,
2559 0 : fd_genesis_cluster_name( cluster ),
2560 0 : fd_genesis_cluster_name( ctx->cluster_type ),
2561 0 : fd_genesis_cluster_name( cluster ) ));
2562 0 : }
2563 0 : }
2564 :
2565 : static void
2566 : on_snapshot_message( fd_replay_tile_t * ctx,
2567 : fd_stem_context_t * stem,
2568 : ulong in_idx,
2569 : ulong chunk,
2570 0 : ulong sig ) {
2571 0 : ulong msg = fd_ssmsg_sig_message( sig );
2572 0 : if( FD_LIKELY( msg==FD_SSMSG_DONE ) ) {
2573 0 : long snapshot_done_nanos = fd_log_wallclock();
2574 :
2575 : /* An end of message notification indicates the snapshot is loaded.
2576 : Replay is able to start executing from this point onwards. */
2577 : /* TODO: replay should finish booting. Could make replay a
2578 : state machine and set the state here accordingly. */
2579 0 : ctx->is_booted = 1;
2580 :
2581 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX );
2582 0 : if( FD_UNLIKELY( !bank ) ) {
2583 0 : FD_LOG_CRIT(( "invariant violation: bank is NULL for bank index %lu", FD_REPLAY_BOOT_BANK_IDX ));
2584 0 : }
2585 :
2586 0 : ulong snapshot_slot = bank->f.slot;
2587 :
2588 0 : fd_hash_t bank_hash = bank->f.bank_hash;
2589 0 : if( FD_UNLIKELY( ctx->wfs_enabled ) ) {
2590 : /* Classify boot against WFS (see fd_wfs.h). */
2591 0 : int wfs_mode = fd_wfs_mode( ctx->wait_for_supermajority_at_slot,
2592 0 : ctx->wfs_hash_is_zero,
2593 0 : (ulong)ctx->expected_shred_version,
2594 0 : snapshot_slot );
2595 0 : switch( wfs_mode ) {
2596 0 : case FD_WFS_MODE_MATCH: {
2597 0 : if( FD_UNLIKELY( memcmp( ctx->expected_bank_hash.uc, bank_hash.uc, sizeof(fd_hash_t) ) ) ) {
2598 0 : FD_BASE58_ENCODE_32_BYTES( ctx->expected_bank_hash.uc, expected_bank_hash_cstr );
2599 0 : FD_BASE58_ENCODE_32_BYTES( bank_hash.uc, actual_bank_hash_cstr );
2600 0 : FD_LOG_ERR(( "[consensus.wait_for_supermajority_with_bank_hash] expected_bank_hash=%s does not match snapshot slot"
2601 0 : "=%lu bank_hash=%s. If you are loading a snapshot from the network, check that the slot matches the "
2602 0 : "cluster restart slot.", expected_bank_hash_cstr, snapshot_slot, actual_bank_hash_cstr ));
2603 0 : }
2604 : /* Booting at the WFS target does not require our vote to be rooted. */
2605 0 : if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
2606 0 : FD_LOG_NOTICE(( "auto-overriding wait_for_vote_to_start_leader to false "
2607 0 : "(WFS target slot %lu, boot slot %lu)",
2608 0 : ctx->wait_for_supermajority_at_slot, snapshot_slot ));
2609 0 : ctx->wait_for_vote_to_start_leader = FD_BOOLAU_FALSE;
2610 0 : }
2611 0 : if( FD_LIKELY( !ctx->wfs_complete ) ) {
2612 0 : FD_LOG_NOTICE(( "waiting for supermajority at snapshot slot %lu", snapshot_slot ));
2613 0 : }
2614 0 : break;
2615 0 : }
2616 0 : case FD_WFS_MODE_ERROR: {
2617 0 : FD_LOG_ERR(( "snapshot slot %lu is behind [consensus.wait_for_supermajority_at_slot] %lu "
2618 0 : "and no incremental snapshot bridged the gap. "
2619 0 : "Provide a snapshot at that slot, or clear it and "
2620 0 : "[consensus.wait_for_supermajority_with_bank_hash] to disable WFS.",
2621 0 : snapshot_slot, ctx->wait_for_supermajority_at_slot ));
2622 0 : break;
2623 0 : }
2624 0 : case FD_WFS_MODE_NOOP: {
2625 0 : FD_LOG_WARNING(( "snapshot slot %lu is ahead of [consensus.wait_for_supermajority_at_slot] %lu, "
2626 0 : "skipping wait for supermajority and bank hash verification",
2627 0 : snapshot_slot, ctx->wait_for_supermajority_at_slot ));
2628 0 : ctx->wfs_complete = 1;
2629 0 : ctx->wait_for_vote_to_start_leader = wfs_resolve_leader_gate_auto( ctx->wait_for_vote_to_start_leader );
2630 0 : break;
2631 0 : }
2632 0 : default: {
2633 : /* DISABLED and UNRESOLVED are unreachable under wfs_enabled. */
2634 0 : FD_LOG_CRIT(( "unexpected WFS mode %d (%s) at boot slot %lu",
2635 0 : wfs_mode, fd_wfs_mode_str( wfs_mode ), snapshot_slot ));
2636 0 : }
2637 0 : }
2638 :
2639 0 : if( FD_UNLIKELY( ctx->wait_for_vote_to_start_leader==FD_BOOLAU_AUTO ) ) {
2640 0 : FD_LOG_CRIT(( "wait_for_vote_to_start_leader unresolved (auto) after WFS boot classification" ));
2641 0 : }
2642 0 : }
2643 :
2644 : /* Manifest message must arrive before DONE */
2645 0 : if( FD_UNLIKELY( !ctx->has_expected_genesis_timestamp ) ) {
2646 0 : FD_LOG_CRIT(( "snapshot DONE received before manifest" ));
2647 0 : }
2648 :
2649 : /* FIXME: This is a hack when the block id of the snapshot slot
2650 : is not provided in the snapshot (Agave versions <4.1). A
2651 : possible solution is to get the block id of the snapshot slot
2652 : from repair. */
2653 0 : fd_hash_t manifest_block_id = ctx->has_manifest_block_id ? ctx->manifest_block_id : ctx->initial_block_id;
2654 :
2655 0 : FD_TEST( fd_sysvar_cache_validate( bank, ctx->accdb ) );
2656 : /* Agave zeroes manifest rent_params; reload from sysvar account */
2657 0 : FD_TEST( fd_sysvar_rent_read( ctx->accdb, bank->accdb_fork_id, &bank->f.rent ) );
2658 :
2659 0 : ctx->consensus_root = manifest_block_id;
2660 0 : ctx->consensus_root_slot = snapshot_slot;
2661 0 : ctx->finalized_block_id_lo = fd_block_id( snapshot_slot, manifest_block_id.uc );
2662 0 : ctx->finalized_block_id_hi = ctx->finalized_block_id_lo;
2663 0 : ctx->notified_root = manifest_block_id;
2664 0 : ctx->notified_root_slot = snapshot_slot;
2665 0 : ctx->notified_root_bank = bank;
2666 0 : ctx->published_root_slot = ctx->consensus_root_slot;
2667 0 : ctx->published_root_bank_idx = 0UL;
2668 0 : if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks ) ) {
2669 0 : ulong interval = ctx->snapmk.full_interval_blocks;
2670 0 : ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
2671 0 : }
2672 0 : if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
2673 0 : ulong interval = ctx->snapmk.incremental_interval_blocks;
2674 0 : ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
2675 0 : }
2676 :
2677 0 : ctx->reset_slot = snapshot_slot;
2678 0 : refresh_epoch_info( ctx, bank, snapshot_slot );
2679 :
2680 0 : fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX, snapshot_slot );
2681 0 : FD_TEST( bank->idx==0UL );
2682 :
2683 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ 0 ];
2684 0 : block_id_ele->latest_mr = manifest_block_id;
2685 0 : block_id_ele->dmr = manifest_block_id;
2686 0 : block_id_ele->slot = snapshot_slot;
2687 0 : block_id_ele->bank_seq = bank->bank_seq;
2688 0 : block_id_ele->block_id_seen = 1;
2689 0 : block_id_ele->latest_fec_idx = 0U;
2690 0 : bank->f.block_id = manifest_block_id;
2691 :
2692 0 : block_id_ele->block_info = fd_block_id( snapshot_slot, manifest_block_id.uc );
2693 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
2694 :
2695 : /* We call this after fd_runtime_read_genesis, which sets up the
2696 : slot_bank needed in blockstore_init. */
2697 0 : init_after_snapshot( ctx, stem );
2698 :
2699 0 : long now = fd_log_wallclock();
2700 0 : FD_LOG_INFO(( "replay ready at slot %lu (%.3f s after snapshot done, %.3f s since boot)",
2701 0 : snapshot_slot, (double)(now-snapshot_done_nanos)/1e9, (double)(now-ctx->boot_timestamp_nanos)/1e9 ));
2702 :
2703 0 : publish_boot_slot( ctx, stem, bank );
2704 :
2705 :
2706 0 : return;
2707 0 : }
2708 :
2709 0 : switch( msg ) {
2710 0 : case FD_SSMSG_MANIFEST_FULL:
2711 0 : case FD_SSMSG_MANIFEST_INCREMENTAL: {
2712 : /* We may either receive a full snapshot manifest or an
2713 : incremental snapshot manifest. Note that this external message
2714 : id is only used temporarily because replay cannot yet receive
2715 : the firedancer-internal snapshot manifest message. */
2716 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
2717 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 ));
2718 :
2719 : /* Malformed manifests are rejected recoverably by snapin via
2720 : fd_ssload_manifest_validate. If recover fails here, then the
2721 : bank is partially mutated, and we must abort. */
2722 0 : if( FD_UNLIKELY( fd_ssload_recover( fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ),
2723 0 : ctx->banks,
2724 0 : fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX ),
2725 0 : ctx->blockhash_seed ) ) ) {
2726 0 : FD_LOG_ERR(( "Snapshot manifest recovery failed, aborting." ));
2727 0 : }
2728 :
2729 0 : ctx->has_cluster_type = 1;
2730 0 : ctx->cluster_type = fd_banks_bank_query( ctx->banks, FD_REPLAY_BOOT_BANK_IDX )->f.cluster_type;
2731 :
2732 0 : fd_snapshot_manifest_t const * manifest = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
2733 : /* hard_fork_cnt already validated by fd_ssload_recover. */
2734 0 : ctx->hard_fork_cnt = manifest->hard_fork_cnt;
2735 0 : for( ulong i=0UL; i<manifest->hard_fork_cnt; i++ ) {
2736 0 : ctx->hard_forks[ i ] = manifest->hard_forks[ i ];
2737 0 : }
2738 0 : ctx->has_expected_genesis_timestamp = 1;
2739 0 : ctx->expected_genesis_timestamp = manifest->creation_time_seconds;
2740 0 : ctx->has_manifest_block_id = manifest->has_block_id;
2741 0 : if( manifest->has_block_id ) memcpy( ctx->manifest_block_id.uc, manifest->block_id, 32UL );
2742 0 : memcpy( ctx->manifest_leader_id.uc, manifest->leader_id, 32UL );
2743 0 : if( FD_UNLIKELY( msg==FD_SSMSG_MANIFEST_FULL ) ) {
2744 0 : ctx->snapmk.base_slot = manifest->slot;
2745 0 : }
2746 0 : break;
2747 0 : }
2748 0 : default: {
2749 0 : FD_LOG_ERR(( "Received unknown snapshot message with msg %lu", msg ));
2750 0 : return;
2751 0 : }
2752 0 : }
2753 :
2754 0 : return;
2755 0 : }
2756 :
2757 : static void
2758 : dispatch_task( fd_replay_tile_t * ctx,
2759 : fd_stem_context_t * stem,
2760 0 : fd_sched_task_t * task ) {
2761 :
2762 0 : switch( task->task_type ) {
2763 0 : case FD_SCHED_TT_TXN_EXEC: {
2764 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, task->txn_exec->txn_idx );
2765 :
2766 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_exec->bank_idx );
2767 0 : FD_TEST( bank );
2768 :
2769 : /* Add the transaction to the block dumper if necessary. This
2770 : logic doesn't need to be fork-aware since it's only meant to
2771 : be used in backtest. */
2772 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_block_to_pb ) ) {
2773 0 : fd_dump_block_to_protobuf_collect_tx( ctx->block_dump_ctx, txn_p );
2774 0 : }
2775 :
2776 0 : bank->refcnt++;
2777 :
2778 0 : fd_cost_tracker_store_pin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
2779 :
2780 0 : if( FD_UNLIKELY( !bank->first_transaction_scheduled_nanos ) ) bank->first_transaction_scheduled_nanos = fd_clock_tile_now( ctx->clock );
2781 :
2782 0 : FD_TEST( task->txn_exec->exec_idx<ctx->exec_cnt );
2783 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->txn_exec->exec_idx ];
2784 0 : fd_execrp_txn_exec_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
2785 0 : fd_txn_p_copy( exec_msg->txn, txn_p );
2786 0 : exec_msg->bank_idx = task->txn_exec->bank_idx;
2787 0 : exec_msg->txn_idx = task->txn_exec->txn_idx;
2788 0 : memcpy( exec_msg->fec_merkle_root, ctx->block_id_arr[ task->txn_exec->bank_idx ].latest_mr.uc, 32UL );
2789 0 : fd_sched_txn_info_t const * txn_info = fd_sched_get_txn_info( ctx->sched, task->txn_exec->txn_idx );
2790 0 : exec_msg->index_in_slot = txn_info->index_in_slot;
2791 0 : exec_msg->want_alts = !!(txn_info->flags & FD_SCHED_TXN_ALT_UNRESOLVED);
2792 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) {
2793 0 : exec_msg->capture_txn_idx = ctx->capture_ctx->current_txn_idx++;
2794 0 : }
2795 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_EXEC<<32) | task->txn_exec->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
2796 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
2797 0 : break;
2798 0 : }
2799 0 : case FD_SCHED_TT_CERT_VERIFY: {
2800 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->cert_verify->bank_idx );
2801 0 : FD_TEST( bank );
2802 : /* Capture and runtime diff runs verify every cert on this tile at
2803 : block end, so the task completes with nothing sent. */
2804 0 : if( FD_UNLIKELY( ctx->capture_ctx || ctx->report_runtime_diffs ) ) {
2805 0 : FD_TEST( !fd_sched_task_done( ctx->sched, FD_SCHED_TT_CERT_VERIFY, ULONG_MAX, task->cert_verify->exec_idx, NULL ) );
2806 0 : break;
2807 0 : }
2808 0 : fd_block_footer_t const * footer = fd_sched_get_footer( ctx->sched, bank->idx );
2809 0 : FD_TEST( footer );
2810 0 : fd_block_footer_cert_t const * cert [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
2811 0 : uint tag [ FD_ALPENGLOW_FOOTER_CERT_MAX ];
2812 0 : ulong quorum[ FD_ALPENGLOW_FOOTER_CERT_MAX ];
2813 0 : ulong cnt = fd_alpenglow_footer_certs( footer, cert, tag, quorum );
2814 0 : ulong k = task->cert_verify->cert_idx;
2815 0 : FD_TEST( k<cnt && k==ctx->footer_end[ bank->idx ].cert_next );
2816 0 : replay_cert_publish( ctx, stem, bank, cert[ k ], tag[ k ], quorum[ k ], task->cert_verify->exec_idx );
2817 0 : ctx->footer_end[ bank->idx ].cert_next = k+1UL;
2818 0 : break;
2819 0 : }
2820 0 : case FD_SCHED_TT_TXN_SIGVERIFY: {
2821 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->txn_sigverify->bank_idx );
2822 0 : FD_TEST( bank );
2823 0 : bank->refcnt++;
2824 :
2825 0 : FD_TEST( task->txn_sigverify->exec_idx<ctx->exec_cnt );
2826 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->txn_sigverify->exec_idx ];
2827 0 : fd_execrp_txn_sigverify_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
2828 0 : FD_TEST( task->txn_sigverify->cnt && task->txn_sigverify->cnt<=FD_EXECRP_SIGVERIFY_SIG_PARA );
2829 0 : exec_msg->bank_idx = task->txn_sigverify->bank_idx;
2830 0 : exec_msg->cnt = task->txn_sigverify->cnt;
2831 0 : for( ulong i=0UL; i<task->txn_sigverify->cnt; i++ ) {
2832 0 : ulong txn_idx = task->txn_sigverify->txn_idx[ i ];
2833 0 : exec_msg->txn_idx[ i ] = txn_idx;
2834 0 : fd_txn_p_copy( exec_msg->txn+i, fd_sched_get_txn( ctx->sched, txn_idx ) );
2835 0 : }
2836 0 : ulong exec_msg_sz = offsetof( fd_execrp_txn_sigverify_msg_t, txn )+task->txn_sigverify->cnt*sizeof(fd_txn_p_t);
2837 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_TXN_SIGVERIFY<<32) | task->txn_sigverify->exec_idx, exec_out->chunk, exec_msg_sz, 0UL, 0UL, 0UL );
2838 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, exec_msg_sz, exec_out->chunk0, exec_out->wmark );
2839 0 : break;
2840 0 : };
2841 0 : case FD_SCHED_TT_POH_HASH: {
2842 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->poh_hash->bank_idx );
2843 0 : FD_TEST( bank );
2844 0 : bank->refcnt++;
2845 :
2846 0 : FD_TEST( task->poh_hash->exec_idx<ctx->exec_cnt );
2847 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->poh_hash->exec_idx ];
2848 0 : fd_execrp_poh_hash_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
2849 0 : exec_msg->bank_idx = task->poh_hash->bank_idx;
2850 0 : exec_msg->cnt = task->poh_hash->cnt;
2851 0 : exec_msg->hashcnt = task->poh_hash->hashcnt;
2852 0 : FD_STATIC_ASSERT( FD_SCHED_POH_PARA==FD_EXECRP_POH_PARA, poh batch width mismatch );
2853 0 : memcpy( exec_msg->hash, task->poh_hash->hash, task->poh_hash->cnt*sizeof(fd_hash_t) );
2854 0 : fd_stem_publish( stem, exec_out->idx, (FD_EXECRP_TT_POH_HASH<<32) | task->poh_hash->exec_idx, exec_out->chunk, sizeof(*exec_msg), 0UL, 0UL, 0UL );
2855 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sizeof(*exec_msg), exec_out->chunk0, exec_out->wmark );
2856 0 : break;
2857 0 : };
2858 0 : case FD_SCHED_TT_LTHASH_SUB:
2859 0 : case FD_SCHED_TT_LTHASH_ADD: {
2860 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->lthash->bank_idx );
2861 0 : FD_TEST( bank );
2862 0 : bank->refcnt++;
2863 :
2864 0 : FD_TEST( task->lthash->exec_idx<ctx->exec_cnt );
2865 0 : FD_TEST( task->lthash->cnt && task->lthash->cnt<=FD_SCHED_LTHASH_BATCH_MAX );
2866 0 : fd_replay_out_link_t * exec_out = &ctx->exec_out[ task->lthash->exec_idx ];
2867 0 : fd_execrp_lthash_msg_t * exec_msg = fd_chunk_to_laddr( exec_out->mem, exec_out->chunk );
2868 0 : exec_msg->bank_idx = task->lthash->bank_idx;
2869 0 : exec_msg->cnt = task->lthash->cnt;
2870 0 : fd_memcpy( exec_msg->acct, task->lthash->accts, task->lthash->cnt*sizeof(fd_acct_addr_t) );
2871 0 : ulong kind = task->task_type==FD_SCHED_TT_LTHASH_SUB ? FD_EXECRP_TT_LTHASH_SUB : FD_EXECRP_TT_LTHASH_ADD;
2872 0 : ulong sz = fd_execrp_lthash_msg_sz( task->lthash->cnt );
2873 0 : fd_stem_publish( stem, exec_out->idx, (kind<<32) | task->lthash->exec_idx, exec_out->chunk, sz, 0UL, 0UL, 0UL );
2874 0 : exec_out->chunk = fd_dcache_compact_next( exec_out->chunk, sz, exec_out->chunk0, exec_out->wmark );
2875 0 : break;
2876 0 : }
2877 0 : default: {
2878 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
2879 0 : }
2880 0 : }
2881 0 : }
2882 :
2883 : static void
2884 : mark_bank_dead( fd_replay_tile_t * ctx,
2885 : fd_stem_context_t * stem,
2886 : ulong bank_idx,
2887 : int dead_reason,
2888 : int abandoned_reason,
2889 0 : fd_block_footer_t const * footer ) {
2890 : /* UINT_MAX only when the bank knows FLH is off: repair treats the death
2891 : as final. A bank still in INIT was never cloned, so its
2892 : features are unknown: count it as a possible prefix. At worst
2893 : rotor finishes repairing a dead slot whose FEC sets replay drops.
2894 :
2895 : Remove this garbage after FLH is stable. */
2896 0 : fd_block_id_ele_t * block_id_ele = &ctx->block_id_arr[ bank_idx ];
2897 0 : fd_bank_t * dead_bank = fd_banks_bank_query( ctx->banks, bank_idx );
2898 0 : int flh_active = dead_bank->state==FD_BANK_STATE_INIT || FD_FEATURE_ACTIVE_BANK( dead_bank, alpenglow_fast_leader_handover );
2899 0 : uint latest_fec_idx = flh_active ? block_id_ele->latest_fec_idx : UINT_MAX;
2900 :
2901 0 : ulong * dead_idxs = ctx->dead_idxs;
2902 0 : ulong dead_idxs_cnt = 0UL;
2903 0 : fd_banks_mark_bank_dead( ctx->banks, bank_idx, dead_idxs, &dead_idxs_cnt );
2904 :
2905 0 : if( block_id_ele->block_id_seen ) publish_slot_dead( ctx, stem, block_id_ele->slot, &block_id_ele->dmr, latest_fec_idx, footer );
2906 0 : else if( dead_reason!=FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD && !memcmp( &block_id_ele->block_info.hash, ((fd_hash_t){0}).uc, sizeof(fd_hash_t) ) ) publish_slot_dead( ctx, stem, block_id_ele->slot, &(fd_hash_t){ 0 }, latest_fec_idx, footer ); /* an incomplete turbine copy, keyed {slot,0}: rotor frees its eager blk */
2907 :
2908 : /* Report each newly dead bank now (dead_idxs excludes already-dead,
2909 : already-reported subtrees): the failing bank with its real reason and
2910 : descendants as parent_dead, or the whole lineage with the caller's
2911 : abandoned flavor. Exactly one of the two reasons is set.
2912 : A previously PRUNABLE (evicted) bank converted by this walk reports
2913 : here like any other: eviction emits no row, and the sched-drain
2914 : emission is gated on the state still being PRUNABLE. Blocks still
2915 : receiving FECs dedup the slot-completion report via dead_reported. */
2916 0 : int abandoned = abandoned_reason!=FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
2917 0 : for( ulong i=0UL; i<dead_idxs_cnt; i++ ) {
2918 0 : fd_block_id_ele_t * ele = &ctx->block_id_arr[ dead_idxs[ i ] ];
2919 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, dead_idxs[ i ] );
2920 0 : timing_slot_release( ctx, dead_idxs[ i ] );
2921 0 : int dr = abandoned ? FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD
2922 0 : : dead_idxs[ i ]==bank_idx ? dead_reason
2923 0 : : FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_PARENT_DEAD;
2924 0 : fd_hash_t * block_id = ele->block_id_seen ? &ele->dmr : &ele->latest_mr;
2925 0 : report_block_incomplete( ctx, ele->slot, block_id, bank, dr, abandoned_reason );
2926 0 : }
2927 0 : }
2928 :
2929 : static int
2930 : try_replay( fd_replay_tile_t * ctx,
2931 0 : fd_stem_context_t * stem ) {
2932 :
2933 0 : if( FD_UNLIKELY( !ctx->is_booted ) ) return 0;
2934 :
2935 : /* Hold off executing until the computed shred version is known, so
2936 : footer certs verify under it. */
2937 0 : if( FD_UNLIKELY( !ctx->shred_version ) ) return 0;
2938 :
2939 0 : int charge_busy = 0;
2940 0 : fd_sched_task_t task[ 1 ];
2941 0 : if( FD_UNLIKELY( !fd_sched_task_next_ready( ctx->sched, task ) ) ) {
2942 0 : return charge_busy; /* Nothing to execute or do. */
2943 0 : }
2944 :
2945 0 : charge_busy = 1;
2946 :
2947 0 : switch( task->task_type ) {
2948 0 : case FD_SCHED_TT_BLOCK_START: {
2949 0 : replay_block_start( ctx, task->block_start->bank_idx, task->block_start->parent_bank_idx, task->block_start->slot, task->block_start->lthash_inband );
2950 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->block_start->bank_idx );
2951 0 : fd_replay_footer_end_t * end = ctx->footer_end + bank->idx;
2952 0 : end->leader_credits = 0UL;
2953 0 : end->cert_next = 0UL;
2954 0 : end->finalized = 0;
2955 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_START, ULONG_MAX, ULONG_MAX, NULL );
2956 0 : if( FD_UNLIKELY( res ) ) mark_bank_dead( ctx, stem, task->block_start->bank_idx, sched_dead_reason_to_event( res ), FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
2957 0 : break;
2958 0 : }
2959 0 : case FD_SCHED_TT_BLOCK_END: {
2960 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, task->block_end->bank_idx );
2961 0 : int dead = 0;
2962 0 : if( FD_LIKELY( bank->state==FD_BANK_STATE_REPLAYABLE ) ) dead = replay_block_finalize( ctx, stem, bank );
2963 0 : fd_sched_task_done( ctx->sched, FD_SCHED_TT_BLOCK_END, ULONG_MAX, ULONG_MAX, NULL );
2964 0 : if( FD_UNLIKELY( dead ) ) fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
2965 0 : break;
2966 0 : }
2967 0 : case FD_SCHED_TT_TXN_EXEC:
2968 0 : case FD_SCHED_TT_TXN_SIGVERIFY:
2969 0 : case FD_SCHED_TT_POH_HASH:
2970 0 : case FD_SCHED_TT_LTHASH_SUB:
2971 0 : case FD_SCHED_TT_LTHASH_ADD:
2972 0 : case FD_SCHED_TT_CERT_VERIFY: {
2973 0 : dispatch_task( ctx, stem, task );
2974 0 : break;
2975 0 : }
2976 0 : case FD_SCHED_TT_MARK_DEAD: {
2977 0 : int dr = sched_block_dead_reason_to_event( ctx, task->mark_dead->bank_idx );
2978 0 : int ar = dr==FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD ? FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED
2979 0 : : FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
2980 0 : mark_bank_dead( ctx, stem, task->mark_dead->bank_idx, dr, ar, NULL );
2981 0 : break;
2982 0 : }
2983 0 : default: {
2984 0 : FD_LOG_CRIT(( "unexpected task type %lu", task->task_type ));
2985 0 : }
2986 0 : }
2987 :
2988 0 : return charge_busy;
2989 0 : }
2990 :
2991 0 : #define PROCESS_FEC_WAIT 0 /* Put back on dcache for later retry */
2992 0 : #define PROCESS_FEC_OK 1 /* Process FEC set */
2993 0 : #define PROCESS_FEC_DROP 2 /* Drop FEC set and notify rotor to start redelivery */
2994 0 : #define PROCESS_FEC_SKIP 3 /* Good to skip this FEC set, drop from dcache */
2995 :
2996 : /* live_block_id_ele returns ele if it is backed by a live bank (same
2997 : bank_seq, not PRUNABLE), else NULL. Map entries outlive their banks
2998 : through eviction. TODO if we remove block_id_eles with banks, we
2999 : can get rid of this */
3000 : static fd_block_id_ele_t *
3001 : live_block_id_ele( fd_replay_tile_t * ctx,
3002 0 : fd_block_id_ele_t * ele ) {
3003 0 : if( FD_UNLIKELY( !ele ) ) return NULL;
3004 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, ele ) );
3005 0 : if( FD_UNLIKELY( !bank || bank->bank_seq!=ele->bank_seq || bank->state==FD_BANK_STATE_PRUNABLE ) ) return NULL;
3006 0 : return ele;
3007 0 : }
3008 :
3009 : static int
3010 : can_process_rotor_fec( fd_replay_tile_t * ctx,
3011 : fd_repair_out_fec_t * fec,
3012 0 : int * evict_banks_out ) {
3013 : /* We can process a FEC set if a few conditions are met:
3014 : - sched has capacity
3015 : - banks has capacity. Evict if we don't (see below)
3016 : Our leader FECs bind to the existing leader bank and never enter
3017 : sched, so neither capacity gate applies to them. One that is not
3018 : for the current leader slot has no bank to bind to. */
3019 :
3020 0 : if( FD_UNLIKELY( fec->is_leader && ( !ctx->leader_bank || ctx->leader_bank->f.slot!=fec->slot ) ) ) return PROCESS_FEC_SKIP;
3021 :
3022 : /* The FEC sets before an UpdateParent are the abandoned prefix,
3023 : built on the leader's optimistic parent. The block replays from
3024 : the UpdateParent FEC set onto the new parent. */
3025 0 : if( FD_UNLIKELY( fec->fec_set_idx<fec->update_parent_fec_idx ) ) {
3026 0 : FD_LOG_DEBUG(( "skipping FEC set (slot=%lu, fec_set_idx=%u) before the UpdateParent at fec set idx %u", fec->slot, fec->fec_set_idx, fec->update_parent_fec_idx ));
3027 0 : return PROCESS_FEC_SKIP;
3028 0 : }
3029 :
3030 0 : if( FD_UNLIKELY( !fec->is_leader && fd_sched_can_ingest_cnt( ctx->sched )==0UL ) ) {
3031 0 : ctx->metrics.sched_full++;
3032 0 : return PROCESS_FEC_WAIT;
3033 0 : }
3034 :
3035 : /* Recovery redelivery re-publishes the entire ancestry path from the
3036 : rotor root, but most of those blocks are already replayed. A
3037 : fully replayed block is always keyed by {slot, block_id_real}; a
3038 : turbine block still in flight is keyed by {slot, 0}. Rotor fills
3039 : in block_id on redelivered FECs whenever it knows it, so try the id
3040 : key first and fall back to the slot key for a turbine version.
3041 :
3042 : A turbine FEC only counts a {slot, block_id} hit if that block is
3043 : complete. An incomplete one is the repaired copy of the same
3044 : block, a different stream that may be ahead of or behind the
3045 : turbine copy: rotor merges the two copies into the turbine one
3046 : without changing its is_known, so the turbine copy keeps building
3047 : its own {slot, 0} bank, and the repaired one is abandoned once the
3048 : turbine bank completes and takes its {slot, block_id} key. */
3049 0 : {
3050 0 : fd_block_id_ele_t * self = NULL;
3051 0 : if( FD_LIKELY( !fd_hash_check_zero( &fec->block_id ) ) ) {
3052 0 : fd_block_id_t self_key = fd_block_id( fec->slot, fec->block_id.uc );
3053 0 : self = live_block_id_ele( ctx, fd_block_id_map_ele_query( ctx->block_id_map, &self_key, NULL, ctx->block_id_arr ) );
3054 0 : if( FD_UNLIKELY( self && !self->block_id_seen && !fec->is_known ) ) self = NULL;
3055 0 : }
3056 0 : if( FD_LIKELY( !self && !fec->is_known ) ) {
3057 0 : fd_block_id_t self_key = { .slot = fec->slot };
3058 0 : self = live_block_id_ele( ctx, fd_block_id_map_ele_query( ctx->block_id_map, &self_key, NULL, ctx->block_id_arr ) );
3059 0 : }
3060 0 : if( FD_UNLIKELY( self && ( self->block_id_seen || self->latest_fec_idx>=fec->fec_set_idx ) ) ) return PROCESS_FEC_SKIP;
3061 0 : }
3062 :
3063 : /* Rooted past: the block is rooted or on a dead fork, so no
3064 : redelivery can help. A DROP would ask for another, and during
3065 : catch-up that one also lands behind the backlog, rooted past. */
3066 :
3067 0 : if( FD_UNLIKELY( ctx->published_root_slot!=ULONG_MAX &&
3068 0 : ( fec->slot<=ctx->published_root_slot ||
3069 0 : fec->parent_slot<ctx->published_root_slot ) ) ) {
3070 0 : ctx->metrics.fec_rooted_past++;
3071 0 : return PROCESS_FEC_SKIP;
3072 0 : }
3073 :
3074 0 : ulong parent_bank_idx = UINT_MAX;
3075 0 : fd_block_id_ele_t * parent = NULL;
3076 0 : if( FD_UNLIKELY( fec->fec_set_idx==fec->update_parent_fec_idx ) ) {
3077 0 : fd_block_id_t parent_key = fd_block_id( fec->parent_slot, fec->parent_block_id.uc );
3078 0 : parent = fd_block_id_map_ele_query( ctx->block_id_map, &parent_key, NULL, ctx->block_id_arr );
3079 0 : if( FD_UNLIKELY( !parent ) ) {
3080 0 : FD_BASE58_ENCODE_32_BYTES( fec->parent_block_id.uc, parent_key_b58 );
3081 0 : FD_LOG_INFO(( "parent bank not found for slot %lu fec set idx %u, parent slot %lu parent block_id %s", fec->slot, fec->fec_set_idx, fec->parent_slot, parent_key_b58 ));
3082 :
3083 : /* We cannot tell here why the parent is missing -- dropped on a
3084 : dead lineage, evicted with its tracking slot reused, or rooted
3085 : past -- and the latter two are routine and recoverable, so do
3086 : not emit events; they would be emitted in bulk during the
3087 : redelivery that recovers them. */
3088 0 : ctx->metrics.parent_unavailable++;
3089 0 : return PROCESS_FEC_DROP;
3090 0 : }
3091 0 : parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
3092 0 : } else {
3093 0 : fd_block_id_t key = { .slot = fec->slot };
3094 0 : if( FD_LIKELY( fec->is_known ) ) key = fd_block_id( fec->slot, fec->block_id.uc );
3095 :
3096 0 : parent = fd_block_id_map_ele_query( ctx->block_id_map, &key, NULL, ctx->block_id_arr );
3097 0 : if( FD_UNLIKELY( !parent ) ) {
3098 0 : FD_BASE58_ENCODE_32_BYTES( fec->block_id.uc, block_id_b58 );
3099 0 : FD_LOG_INFO(( "parent bank not found for slot %lu fec set idx %u slot_bid %s. parent slot %lu", fec->slot, fec->fec_set_idx, block_id_b58, fec->parent_slot ));
3100 0 : ctx->metrics.parent_unavailable++;
3101 0 : return PROCESS_FEC_DROP; // either pruned or bank evicted
3102 0 : }
3103 0 : parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
3104 0 : }
3105 :
3106 0 : ctx->metrics.reasm_latest_slot = fec->slot;
3107 0 : ctx->metrics.reasm_latest_fec_idx = fec->fec_set_idx;
3108 :
3109 : /* If the FEC we are building off of is for a prunable bank, we must
3110 : wait to process the FEC until the bank has been evicted. */
3111 0 : fd_bank_t * parent_fec_bank = parent_bank_idx==UINT_MAX ? NULL : fd_banks_bank_query( ctx->banks, parent_bank_idx );
3112 0 : if( FD_UNLIKELY( parent_fec_bank && parent_fec_bank->bank_seq==parent->bank_seq && parent_fec_bank->state==FD_BANK_STATE_PRUNABLE ) ) {
3113 0 : FD_LOG_DEBUG(( "waiting to process FEC set (slot=%lu, fec_set_idx=%u) because parent bank is being pruned", fec->slot, fec->fec_set_idx ));
3114 0 : return PROCESS_FEC_WAIT;
3115 0 : }
3116 :
3117 0 : if( FD_UNLIKELY( ctx->is_leader &&
3118 0 : fec->fec_set_idx==fec->update_parent_fec_idx &&
3119 0 : parent_bank_idx==ctx->leader_bank->idx &&
3120 0 : parent->bank_seq==ctx->leader_bank->bank_seq ) ) {
3121 : /* This guards against a rare race where we receive the FEC set for
3122 : the slot right after our leader rotation before we freeze the
3123 : bank for the last slot in our leader rotation. Leader slot
3124 : freezing happens only after if we've received the final PoH hash
3125 : from the poh tile as well as the final FEC set for the leader
3126 : slot. So the race happens when FEC sets are delivered and
3127 : processed sooner than the PoH hash, aka when the
3128 : poh=>shred=>replay path for the block id beats the poh=>replay
3129 : path for the poh hash. To mitigate this race, we must block on
3130 : ingesting the FEC set for the ensuing slot before the leader
3131 : bank freezes, because that would violate ordering invariants in
3132 : banks and sched. Both the bank index and sequence must match
3133 : because bank indices are recycled after eviction. */
3134 0 : FD_TEST( ctx->block_id_arr[ ctx->leader_bank->idx ].block_id_seen );
3135 0 : FD_TEST( !ctx->recv_poh );
3136 0 : ctx->metrics.leader_bid_wait++;
3137 0 : return PROCESS_FEC_WAIT;
3138 0 : }
3139 :
3140 : /* Should we evict banks if there are no more free banks? The answer
3141 : is it depends. Eviction should only happen if we can make no
3142 : forward replay progress. This can only happen if:
3143 : 1. banks are full
3144 : 2. sched is drained: pending txns could complete a block and
3145 : eventually advance the root.
3146 : AND
3147 : 3. next reasm FEC needs a new bank. A fec that chains off of a
3148 : bank that is already allocated can be processed. A FEC can
3149 : require a new bank in three ways:
3150 : - fec_set_idx==0: we don't have any free banks to provision a
3151 : new bank for this FEC.
3152 : - equivocation: a FEC may be in the middle of a block, but if
3153 : it's the first equivocating FEC detected, we need to allocate
3154 : a new bank for the version of the block.
3155 : - backfill: the parent FEC's bank was never created or has been
3156 : evicted and must be reconstructed.
3157 :
3158 :
3159 : - Without reasm there is no backfill: if the parent bank was
3160 : evicted or its bank idx was reused, this FEC can't be
3161 : processed, so drop it and notify rotor to redeliver.
3162 : - Rotor equivocating slots are guaranteed to be delivered starting
3163 : from FEC 0, so even equivocations mid-slot are delivered from
3164 : fec 0 */
3165 :
3166 0 : int invalid_parent = !parent_fec_bank || parent_fec_bank->bank_seq!=parent->bank_seq;
3167 0 : if( FD_UNLIKELY( invalid_parent ) ) {
3168 0 : FD_LOG_INFO(( "parent bank evicted for slot %lu fec set idx %u, parent slot %lu", fec->slot, fec->fec_set_idx, fec->parent_slot ));
3169 0 : ctx->metrics.parent_unavailable++;
3170 0 : return PROCESS_FEC_DROP;
3171 0 : } else if( FD_UNLIKELY( fec->fec_set_idx!=fec->update_parent_fec_idx && parent->latest_fec_idx!=fec->fec_set_idx - FD_FEC_SHRED_CNT ) ) {
3172 : /* Similar to the very first condition in can_process_rotor_fec,
3173 : but this would hit if we were halfway through replaying a slot
3174 : and then requested redelivery from rotor. Then we can skip
3175 : replaying the first half of the slot. */
3176 0 : FD_LOG_INFO(( "fec redelivered for slot %lu fec set idx %u, parent slot %lu. bank_idx %lu, latest_fec_idx %u", fec->slot, fec->fec_set_idx, fec->parent_slot, fd_block_id_ele_get_idx( ctx->block_id_arr, parent ), parent->latest_fec_idx ));
3177 0 : return PROCESS_FEC_SKIP; // context for slot exists, but this is an earlier or non-contiguous FEC. Safe to skip.
3178 0 : }
3179 :
3180 0 : if( FD_UNLIKELY( !fd_banks_can_start_bank( ctx->banks ) ) ) {
3181 0 : int is_new_block = fec->fec_set_idx==fec->update_parent_fec_idx && !fec->is_leader;
3182 0 : if( FD_UNLIKELY( is_new_block ) ) {
3183 0 : ctx->metrics.banks_full++;
3184 0 : if( FD_UNLIKELY( fd_sched_is_drained( ctx->sched ) && !ctx->snapmk.active ) ) *evict_banks_out = 1; /* see can_process_fec */
3185 0 : return PROCESS_FEC_WAIT;
3186 0 : }
3187 0 : }
3188 :
3189 : /* A block's first FEC set needs a staging lane. This comes after the
3190 : eviction decision so that a wait for a lane doesn't shadow it. Our
3191 : own leader FECs never enter sched, so they don't need to wait for a
3192 : lane. */
3193 0 : if( FD_UNLIKELY( fec->fec_set_idx==0U && !fec->is_leader && fd_sched_fec_waits_for_lane( ctx->sched, parent_bank_idx, 1 ) ) ) {
3194 0 : ctx->metrics.lane_wait++;
3195 0 : return PROCESS_FEC_WAIT;
3196 0 : }
3197 :
3198 : /* Otherwise, banks may not be full, so we can always create a new
3199 : bank if needed. Or, if banks are full, the current fec set's
3200 : ancestor (idx 0) already created a bank for this slot. */
3201 0 : return PROCESS_FEC_OK;
3202 0 : }
3203 :
3204 : static int
3205 : try_notify_consensus_root( fd_replay_tile_t * ctx,
3206 0 : fd_stem_context_t * stem ) {
3207 :
3208 : /* Other tiles cannot prune while we are leader - replay may still be
3209 : waiting for the remaining FEC sets of our own slots to be
3210 : delivered, and if the upstream tiles prune too early, they may fail
3211 : to forward our own leader FECs. Then we would be permanently stuck
3212 : in the leader state. */
3213 0 : if( FD_UNLIKELY( ctx->is_leader ) ) return 0;
3214 :
3215 0 : if( FD_LIKELY( ctx->notified_root_slot==ctx->consensus_root_slot &&
3216 0 : fd_hash_eq( &ctx->notified_root, &ctx->consensus_root ) ) ) return 0;
3217 :
3218 0 : fd_block_id_t root_key = fd_block_id( ctx->consensus_root_slot, ctx->consensus_root.uc );
3219 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &root_key, NULL, ctx->block_id_arr );
3220 0 : if( FD_UNLIKELY( !block_id_ele ) ) return 0;
3221 :
3222 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
3223 0 : if( FD_UNLIKELY( !bank ||
3224 0 : bank->bank_seq!=block_id_ele->bank_seq ||
3225 0 : !fd_hash_eq( &bank->f.block_id, &ctx->consensus_root ) ||
3226 0 : bank->state==FD_BANK_STATE_PRUNABLE ) ) return 0;
3227 :
3228 0 : fd_sched_root_notify( ctx->sched, bank->idx );
3229 0 : publish_root_advanced( ctx, stem, bank );
3230 :
3231 0 : ctx->notified_root = ctx->consensus_root;
3232 0 : ctx->notified_root_slot = ctx->consensus_root_slot;
3233 0 : ctx->notified_root_bank = bank;
3234 0 : return 1;
3235 0 : }
3236 :
3237 : /* Periodic snapshots use block height, matching Agave. Manually
3238 : scheduled snapshots remain slot based. */
3239 :
3240 : static int
3241 : snapshot_due_for_root( fd_replay_tile_t * ctx,
3242 : ulong published_root_block_height,
3243 : ulong advanceable_root_slot,
3244 : ulong advanceable_root_block_height,
3245 : ulong consensus_root_block_height,
3246 0 : int * out_incremental ) {
3247 0 : *out_incremental = 0;
3248 0 : if( FD_LIKELY( !ctx->snapmk.supported || ctx->snapmk.active ) ) return 0;
3249 :
3250 0 : int caught_up = ctx->caught_up; /* suspend periodic snaps until caught up */
3251 0 : ulong full_target = ULONG_MAX;
3252 0 : ulong interval = ctx->snapmk.full_interval_blocks;
3253 0 : if( FD_UNLIKELY( interval && caught_up ) ) {
3254 0 : if( FD_UNLIKELY( ctx->snapmk.next_full_block_height==ULONG_MAX ) ) {
3255 0 : ctx->snapmk.next_full_block_height = ((published_root_block_height/interval)+1UL)*interval;
3256 0 : }
3257 :
3258 : /* If snapshot production fell behind by more than one interval,
3259 : skip ahead to the latest due interval. */
3260 0 : full_target = fd_ulong_max( ctx->snapmk.next_full_block_height,
3261 0 : (consensus_root_block_height/interval)*interval );
3262 0 : }
3263 :
3264 : /* Manual slot scheduling always requests a full snapshot. */
3265 0 : if( FD_UNLIKELY( advanceable_root_slot>=ctx->snapmk.scheduled_at_slot ) ) return 1;
3266 :
3267 : /* An incremental snapshot is only possible once a full snapshot
3268 : exists to serve as its base. */
3269 0 : ulong incremental_target = ULONG_MAX;
3270 0 : ulong incr_interval = ctx->snapmk.incremental_interval_blocks;
3271 0 : if( FD_UNLIKELY( incr_interval && caught_up && ctx->snapmk.base_slot!=ULONG_MAX ) ) {
3272 0 : if( FD_UNLIKELY( ctx->snapmk.next_incremental_block_height==ULONG_MAX ) ) {
3273 0 : ctx->snapmk.next_incremental_block_height = ((published_root_block_height/incr_interval)+1UL)*incr_interval;
3274 0 : }
3275 0 : incremental_target = fd_ulong_max( ctx->snapmk.next_incremental_block_height,
3276 0 : (consensus_root_block_height/incr_interval)*incr_interval );
3277 0 : }
3278 :
3279 : /* A full snapshot due at the same block height supersedes the
3280 : incremental snapshot. */
3281 0 : ulong target = full_target;
3282 0 : int incremental = incremental_target<full_target;
3283 0 : if( FD_UNLIKELY( incremental ) ) target = incremental_target;
3284 0 : if( FD_LIKELY( advanceable_root_block_height<target ) ) return 0;
3285 0 : *out_incremental = incremental;
3286 0 : return 1;
3287 0 : }
3288 :
3289 : static void
3290 : snapmk_start( fd_replay_tile_t * ctx,
3291 : fd_stem_context_t * stem,
3292 : int incremental );
3293 :
3294 : static int
3295 : try_advance_published_root( fd_replay_tile_t * ctx,
3296 0 : fd_stem_context_t * stem ) {
3297 :
3298 0 : if( FD_LIKELY( ctx->published_root_slot==ctx->consensus_root_slot ) ) return 0;
3299 :
3300 : /* accdb does not root while producing a snapshot. */
3301 0 : if( FD_UNLIKELY( ctx->snapmk.active ) ) return 0;
3302 :
3303 : /* If the new root is not available because the bank is/has been
3304 : evicted, we can't advance the root. Try again later. */
3305 :
3306 0 : fd_block_id_t consensus_root_key = fd_block_id( ctx->consensus_root_slot, ctx->consensus_root.uc );
3307 0 : fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &consensus_root_key, NULL, ctx->block_id_arr );
3308 0 : if( FD_UNLIKELY( !block_id_ele ) ) return 0;
3309 0 : fd_bank_t * target_bank = fd_banks_bank_query( ctx->banks, fd_block_id_ele_get_idx( ctx->block_id_arr, block_id_ele ) );
3310 0 : if( FD_UNLIKELY( !target_bank ||
3311 0 : target_bank->bank_seq!=block_id_ele->bank_seq ||
3312 0 : !fd_hash_eq( &target_bank->f.block_id, &ctx->consensus_root ) ||
3313 0 : target_bank->state==FD_BANK_STATE_PRUNABLE ) ) {
3314 0 : return 0;
3315 0 : }
3316 :
3317 0 : fd_bank_t * published_root_bank = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx );
3318 0 : FD_TEST( published_root_bank );
3319 0 : ulong published_root_block_height = published_root_bank->f.block_height;
3320 0 : ulong consensus_root_block_height = target_bank->f.block_height;
3321 :
3322 : /* If the identity vote has been seen on a bank that should be rooted,
3323 : then we are now ready to produce blocks. */
3324 0 : if( FD_UNLIKELY( !ctx->identity_vote_rooted ) ) {
3325 0 : if( target_bank->f.identity_vote_idx==ctx->identity_idx ) ctx->identity_vote_rooted = 1;
3326 0 : }
3327 :
3328 0 : ulong advanceable_root_idx = ULONG_MAX;
3329 0 : if( FD_UNLIKELY( !fd_banks_advance_root_prepare( ctx->banks, target_bank->idx, &advanceable_root_idx ) ) ) {
3330 0 : ctx->metrics.storage_root_behind++;
3331 0 : return 0;
3332 0 : }
3333 :
3334 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, advanceable_root_idx );
3335 0 : FD_TEST( bank );
3336 :
3337 0 : if( FD_UNLIKELY( advanceable_root_idx>=ctx->block_id_len ) ) {
3338 0 : FD_LOG_CRIT(( "invariant violation: advanceable root ele out of bounds [0, %lu) index %lu", ctx->block_id_len, advanceable_root_idx ));
3339 0 : }
3340 : /* Telemetry: banks still mid-replay (started but never completed) on
3341 : forks not descending from the new root are about to be cancelled by
3342 : fd_banks_advance_root without ever emitting a block_completed row.
3343 : Emit them as abandoned with reason `pruned`: the block was not
3344 : invalid, it lost the fork race. Every live bank is linked in the
3345 : bank tree under the current root, so one preorder pass that skips
3346 : the new root's subtree visits exactly the banks about to go. */
3347 0 : if( FD_UNLIKELY( fd_event_tl ) ) {
3348 0 : ulong root_idx = fd_banks_root( ctx->banks )->idx;
3349 0 : ulong s = root_idx;
3350 0 : fd_bank_t * b = fd_banks_root( ctx->banks );
3351 0 : for(;;) {
3352 0 : if( FD_LIKELY( s!=advanceable_root_idx ) ) {
3353 0 : if( FD_UNLIKELY( b->state==FD_BANK_STATE_INIT || b->state==FD_BANK_STATE_REPLAYABLE ) ) {
3354 0 : fd_block_id_ele_t * ele = &ctx->block_id_arr[ s ];
3355 0 : report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, b, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED );
3356 0 : }
3357 0 : if( FD_LIKELY( b->child_idx!=ULONG_MAX ) ) { s = b->child_idx; b = fd_banks_bank_query( ctx->banks, s ); continue; }
3358 0 : }
3359 0 : while( s!=root_idx && b->sibling_idx==ULONG_MAX ) { s = b->parent_idx; b = fd_banks_bank_query( ctx->banks, s ); }
3360 0 : if( s==root_idx ) break;
3361 0 : s = b->sibling_idx; b = fd_banks_bank_query( ctx->banks, s );
3362 0 : }
3363 0 : }
3364 :
3365 0 : ulong advanceable_root_slot = bank->f.slot;
3366 0 : int root_epoch_changed = bank->f.epoch!=published_root_bank->f.epoch;
3367 0 : uchar stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
3368 0 : fd_stake_history_t stake_history_[1];
3369 0 : fd_stake_history_t const * stake_history = fd_sysvar_stake_history_read( ctx->accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
3370 0 : fd_txncache_advance_root( ctx->txncache, bank->txncache_fork_id );
3371 0 : fd_progcache_advance_root( ctx->progcache, bank->progcache_fork_id );
3372 0 : fd_accdb_advance_root( ctx->accdb, bank->accdb_fork_id );
3373 0 : fd_sched_advance_root( ctx->sched, advanceable_root_idx );
3374 0 : fd_banks_advance_root( ctx->banks, advanceable_root_idx, stake_history );
3375 0 : if( FD_UNLIKELY( root_epoch_changed ) ) refresh_vote_account_staked( ctx, bank );
3376 :
3377 0 : for( ulong b=0UL; b<ctx->max_live_slots; b++ ) {
3378 0 : if( FD_UNLIKELY( ctx->timing_slot_of_bank[ b ]!=fd_timing_slot_pool_idx_null( ctx->timing_slot_pool ) && !fd_banks_bank_query( ctx->banks, b ) ) ) timing_slot_release( ctx, b );
3379 0 : }
3380 :
3381 0 : int snap_incremental;
3382 0 : int snap_due = snapshot_due_for_root( ctx,
3383 0 : published_root_block_height,
3384 0 : advanceable_root_slot,
3385 0 : bank->f.block_height,
3386 0 : consensus_root_block_height,
3387 0 : &snap_incremental );
3388 :
3389 0 : ctx->published_root_slot = advanceable_root_slot;
3390 0 : ctx->published_root_bank_idx = advanceable_root_idx;
3391 :
3392 0 : if( FD_UNLIKELY( snap_due ) ) {
3393 0 : snapmk_start( ctx, stem, snap_incremental );
3394 0 : if( FD_UNLIKELY( !snap_incremental ) ) {
3395 0 : if( FD_UNLIKELY( ctx->snapmk.full_interval_blocks &&
3396 0 : bank->f.block_height>=ctx->snapmk.next_full_block_height ) ) {
3397 0 : ulong interval = ctx->snapmk.full_interval_blocks;
3398 0 : ctx->snapmk.next_full_block_height = ((bank->f.block_height/interval)+1UL)*interval;
3399 0 : }
3400 0 : if( FD_UNLIKELY( advanceable_root_slot>=ctx->snapmk.scheduled_at_slot ) ) {
3401 0 : ctx->snapmk.scheduled_at_slot = ULONG_MAX;
3402 0 : }
3403 0 : }
3404 0 : if( FD_UNLIKELY( ctx->snapmk.incremental_interval_blocks ) ) {
3405 0 : ulong interval = ctx->snapmk.incremental_interval_blocks;
3406 0 : ctx->snapmk.next_incremental_block_height = ((bank->f.block_height/interval)+1UL)*interval;
3407 0 : }
3408 0 : }
3409 :
3410 0 : return 1;
3411 0 : }
3412 :
3413 : static int
3414 0 : try_prune_sched( fd_replay_tile_t * ctx ) {
3415 0 : ulong bank_idx;
3416 0 : int pruned = 0;
3417 0 : while( (bank_idx=fd_sched_pruned_block_next( ctx->sched ) )!=ULONG_MAX ) {
3418 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
3419 0 : FD_TEST( bank );
3420 : /* An evicted bank drains here with its verdict final: every
3421 : in-flight task has resolved, so still PRUNABLE means none ruled
3422 : it invalid (that would have converted it to DEAD and reported a
3423 : dead row), and this is the block's only row. A frozen victim
3424 : already reported a completed row. */
3425 0 : if( FD_UNLIKELY( bank->state==FD_BANK_STATE_PRUNABLE && !bank->block_completed_nanos ) ) {
3426 0 : fd_block_id_ele_t * ele = &ctx->block_id_arr[ bank_idx ];
3427 0 : report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, bank,
3428 0 : FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD,
3429 0 : FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_EVICTED );
3430 0 : }
3431 0 : bank->refcnt--;
3432 0 : FD_LOG_DEBUG(( "bank (idx=%lu) refcnt decremented to %lu for sched", bank->idx, bank->refcnt ));
3433 0 : pruned = 1;
3434 0 : }
3435 0 : return pruned;
3436 0 : }
3437 :
3438 : static int
3439 0 : try_prune_bank( fd_replay_tile_t * ctx ) {
3440 0 : fd_banks_prune_cancel_info_t cancel_info[ 1 ];
3441 :
3442 0 : int pruned = fd_banks_prune_one_bank( ctx->banks, cancel_info );
3443 0 : switch( pruned ) {
3444 0 : case 2: { /* pruning bank + cancellation is needed */
3445 0 : fd_txncache_cancel_fork( ctx->txncache, cancel_info->txncache_fork_id );
3446 0 : fd_progcache_cancel_fork( ctx->progcache, cancel_info->progcache_fork_id );
3447 0 : fd_accdb_purge( ctx->accdb, cancel_info->accdb_fork_id );
3448 0 : __attribute__((fallthrough));
3449 0 : }
3450 0 : case 1: { /* pruning bank + no cancellation is needed */
3451 : /* A sched block exists, and can be marked dead, for a bank as
3452 : soon as its first FEC has been ingested, which can happen
3453 : before the bank ever set itself up for actual execution (e.g. a
3454 : block that parses as bad on its very first FEC). So always
3455 : instruct sched to prune the block whenever banks prunes the
3456 : bank. The txncache/progcache/accdb forks, on the other hand,
3457 : are only created once the bank started actual execution. */
3458 0 : fd_sched_cancel( ctx->sched, cancel_info->bank_idx );
3459 0 : timing_slot_release( ctx, cancel_info->bank_idx );
3460 0 : return 1;
3461 0 : }
3462 0 : case 0: /* no bank to prune */
3463 0 : return 0;
3464 0 : default:
3465 0 : FD_LOG_ERR(( "unreachable" ));
3466 0 : }
3467 0 : }
3468 :
3469 : static void
3470 : after_credit( fd_replay_tile_t * ctx,
3471 : fd_stem_context_t * stem,
3472 : int * opt_poll_in,
3473 0 : int * charge_busy ) {
3474 : /* Publish slot state intervals before the halt checks. */
3475 0 : fd_replay_slot_states_t * states = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
3476 0 : states->cnt = fd_sched_slot_states_drain( ctx->sched, states->states );
3477 0 : if( FD_UNLIKELY( states->cnt ) ) publish_replay_out( ctx, stem, REPLAY_SIG_SLOT_STATES, sizeof(fd_replay_slot_states_t) );
3478 :
3479 0 : if( FD_UNLIKELY( ctx->halt_replay && !ctx->is_leader ) ) return;
3480 0 : if( FD_UNLIKELY( !ctx->is_booted || !ctx->wfs_complete ) ) return;
3481 :
3482 : /* The overall priority for the replay tile in order is:
3483 : 1. Make sure replay has room to progress:
3484 : a. evicting pending FECs from the reassembler
3485 : b. queueing up evictable banks for pruning if needed
3486 : c. clearing any pending bank eviction victims.
3487 : 2. Drain outstanding bank references from the scheduler. This
3488 : happens after a block gets completed or a fork gets pruned.
3489 : 3. Notify sched and bank consumers of a new consensus root, then
3490 : advance the storage root once old references drain.
3491 : 4. Replay. If there is work to do for replay, do it. This is
3492 : more important than ingesting more FEC sets.
3493 : 5. If replay has nothing to do, ingest more FEC sets.
3494 : WARNING: The ordering here is VERY load bearing and it should not
3495 : be changed without extreme caution. */
3496 :
3497 0 : if( FD_UNLIKELY( try_prune_sched( ctx ) ) ) {
3498 0 : *charge_busy = 1;
3499 0 : *opt_poll_in = 0;
3500 0 : return;
3501 0 : }
3502 :
3503 0 : if( FD_UNLIKELY( try_notify_consensus_root( ctx, stem ) ) ) {
3504 0 : *charge_busy = 1;
3505 0 : *opt_poll_in = 0;
3506 0 : return;
3507 0 : }
3508 :
3509 0 : if( FD_UNLIKELY( try_prune_bank( ctx ) ) ) {
3510 0 : *charge_busy = 1;
3511 0 : *opt_poll_in = 0;
3512 0 : return;
3513 0 : }
3514 :
3515 0 : if( FD_UNLIKELY( try_become_leader( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_START, 0UL ) ) ) {
3516 0 : *charge_busy = 1;
3517 0 : *opt_poll_in = 0;
3518 0 : return;
3519 0 : }
3520 :
3521 0 : if( FD_UNLIKELY( try_fini_leader( ctx, stem ) ) ) {
3522 0 : *charge_busy = 1;
3523 0 : *opt_poll_in = 0;
3524 0 : return;
3525 0 : }
3526 :
3527 0 : if( FD_UNLIKELY( try_advance_published_root( ctx, stem ) ) ) {
3528 0 : *charge_busy = 1;
3529 0 : *opt_poll_in = 0;
3530 0 : return;
3531 0 : }
3532 :
3533 0 : if( FD_LIKELY( try_replay( ctx, stem ) ) ) {
3534 0 : *charge_busy = 1;
3535 0 : *opt_poll_in = 0;
3536 0 : return;
3537 0 : }
3538 :
3539 0 : ctx->execrp_idle_cnt++;
3540 0 : }
3541 :
3542 : static int
3543 : before_frag( fd_replay_tile_t * ctx,
3544 : ulong in_idx,
3545 : ulong seq FD_PARAM_UNUSED,
3546 0 : ulong sig ) {
3547 :
3548 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP_OUT && sig!=FD_GOSSIP_UPDATE_TAG_WFS_DONE ) ) return 1;
3549 0 : return 0;
3550 0 : }
3551 :
3552 : static void
3553 : process_exec_task_done( fd_replay_tile_t * ctx,
3554 : fd_stem_context_t * stem,
3555 : fd_execrp_task_done_msg_t * msg,
3556 : ulong sz,
3557 0 : ulong sig ) {
3558 :
3559 0 : ulong exec_tile_idx = sig&0xFFFFFFFFUL;
3560 0 : FD_TEST( sz==sizeof(fd_execrp_task_done_msg_t) );
3561 :
3562 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
3563 0 : FD_TEST( bank );
3564 0 : bank->refcnt--;
3565 :
3566 0 : switch( sig>>32 ) {
3567 0 : case FD_EXECRP_TT_TXN_EXEC: {
3568 0 : ulong txn_idx = msg->txn_exec->txn_idx;
3569 0 : ulong max_compute_units = fd_cost_tracker_store_peek( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id )->block_cost_limit;
3570 0 : fd_cost_tracker_store_unpin( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
3571 :
3572 0 : if( FD_UNLIKELY( !ctx->identity_vote_rooted ) ) {
3573 : /* Query the txn signature against our recently generated vote
3574 : txn signatures. If the query is successful, then we have
3575 : seen our own vote transaction land and this should be marked
3576 : in the bank. We go through this exercise until we've seen
3577 : our vote rooted. */
3578 0 : fd_txn_p_t * txn_p = fd_sched_get_txn( ctx->sched, txn_idx );
3579 :
3580 0 : fd_pubkey_t * identity_pubkey_out = NULL;
3581 0 : if( fd_vote_tracker_query_sig( ctx->vote_tracker, fd_type_pun_const( txn_p->payload+TXN( txn_p )->signature_off ), &identity_pubkey_out ) && fd_pubkey_eq( identity_pubkey_out, ctx->identity_pubkey ) ) {
3582 0 : bank->f.identity_vote_idx = ctx->identity_idx;
3583 0 : }
3584 0 : }
3585 0 : if( FD_UNLIKELY( !msg->txn_exec->is_committable && bank->state!=FD_BANK_STATE_DEAD) ) {
3586 : /* Every transaction in a valid block has to execute.
3587 : Otherwise, we should mark the block as dead. Non-committable
3588 : means the txn failed before account loading or failed the cost
3589 : tracker; txn_err distinguishes the cases for telemetry. */
3590 0 : int dead_reason;
3591 0 : switch( msg->txn_exec->txn_err ) {
3592 0 : case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_BLOCK_COST_LIMIT:
3593 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BLOCK_COST_LIMIT;
3594 0 : break;
3595 0 : case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_ACCOUNT_COST_LIMIT:
3596 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ACCOUNT_COST_LIMIT;
3597 0 : break;
3598 0 : case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_MAX_VOTE_COST_LIMIT:
3599 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_VOTE_COST_LIMIT;
3600 0 : break;
3601 0 : case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT:
3602 0 : case FD_RUNTIME_TXN_ERR_WOULD_EXCEED_ACCOUNT_DATA_TOTAL_LIMIT:
3603 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_ACCOUNT_DATA_LIMIT;
3604 0 : break;
3605 0 : case FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE: /* exec-path duplicate, same taxonomy as the chkdup ruling */
3606 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_DUPLICATE_ACCOUNT;
3607 0 : break;
3608 0 : default:
3609 0 : dead_reason = FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_TXN_FAILED_TO_LOAD;
3610 0 : }
3611 0 : mark_bank_dead( ctx, stem, bank->idx, dead_reason, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3612 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3613 0 : }
3614 0 : fd_sched_txn_alts_t alts[ 1 ] = { { .cnt = msg->txn_exec->alt_writable_cnt, .addrs = fd_type_pun_const( ctx->execrp_result[ exec_tile_idx ].alt_writable ) } };
3615 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_EXEC, txn_idx, exec_tile_idx, alts->cnt ? alts : NULL );
3616 0 : if( FD_UNLIKELY( res && bank->state!=FD_BANK_STATE_DEAD ) ) {
3617 0 : mark_bank_dead( ctx, stem, bank->idx, sched_dead_reason_to_event( res ), FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3618 0 : }
3619 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
3620 0 : txn_info->flags |= FD_SCHED_TXN_EXEC_DONE;
3621 0 : txn_info->bank_seq = msg->txn_exec->bank_seq;
3622 :
3623 0 : txn_info->tick_load_start = msg->txn_exec->tick_load_start;
3624 0 : txn_info->tick_check_start = msg->txn_exec->tick_check_start;
3625 0 : txn_info->tick_exec_start = msg->txn_exec->tick_exec_start;
3626 0 : txn_info->tick_commit_start = msg->txn_exec->tick_commit_start;
3627 0 : txn_info->tick_commit_end = msg->txn_exec->tick_commit_end;
3628 :
3629 0 : txn_info->compute_units_consumed = msg->txn_exec->compute_units_consumed;
3630 0 : txn_info->max_compute_units = max_compute_units;
3631 0 : txn_info->transaction_fee = msg->txn_exec->transaction_fee;
3632 0 : txn_info->priority_fee = msg->txn_exec->priority_fee;
3633 0 : txn_info->tips = msg->txn_exec->tips;
3634 0 : if( FD_LIKELY( !(txn_info->flags&FD_SCHED_TXN_SIGVERIFY_DONE)||!txn_info->txn_err ) ) { /* Set execution status if sigverify hasn't happened yet or if sigverify was a success. */
3635 0 : txn_info->txn_err = msg->txn_exec->txn_err;
3636 0 : txn_info->flags |= fd_ulong_if( msg->txn_exec->is_committable, FD_SCHED_TXN_IS_COMMITTABLE, 0UL );
3637 0 : txn_info->flags |= fd_ulong_if( msg->txn_exec->is_fees_only, FD_SCHED_TXN_IS_FEES_ONLY, 0UL );
3638 0 : txn_info->flags |= fd_ulong_if( msg->txn_exec->is_noop, FD_SCHED_TXN_IS_NOOP, 0UL );
3639 0 : }
3640 0 : if( FD_UNLIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) { /* UNLIKELY because generally exec happens before sigverify. */
3641 0 : publish_txn_executed( ctx, stem, bank->idx, txn_idx );
3642 0 : }
3643 0 : break;
3644 0 : }
3645 0 : case FD_EXECRP_TT_TXN_SIGVERIFY: {
3646 0 : ulong cnt = msg->txn_sigverify->cnt;
3647 0 : FD_TEST( cnt && cnt<=FD_EXECRP_SIGVERIFY_SIG_PARA );
3648 0 : int err = msg->txn_sigverify->err;
3649 0 : for( ulong i=0UL; i<cnt; i++ ) {
3650 0 : fd_sched_txn_info_t * txn_info = fd_sched_get_txn_info( ctx->sched, msg->txn_sigverify->txn_idx[ i ] );
3651 0 : txn_info->flags |= FD_SCHED_TXN_SIGVERIFY_DONE;
3652 0 : if( FD_UNLIKELY( err ) ) {
3653 : /* The block dies on any failed signature, so every
3654 : transaction of the task carries the failure. */
3655 0 : txn_info->txn_err = FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
3656 0 : txn_info->flags &= ~FD_SCHED_TXN_IS_COMMITTABLE;
3657 0 : txn_info->flags &= ~FD_SCHED_TXN_IS_FEES_ONLY;
3658 0 : txn_info->flags &= ~FD_SCHED_TXN_IS_NOOP;
3659 0 : }
3660 0 : }
3661 0 : if( FD_UNLIKELY( err && bank->state!=FD_BANK_STATE_DEAD ) ) {
3662 : /* Every transaction in a valid block has to sigverify.
3663 : Otherwise, we should mark the block as dead. Also freeze the
3664 : bank if possible. */
3665 0 : mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_SIGVERIFY_FAILED, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3666 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3667 0 : }
3668 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_TXN_SIGVERIFY, ULONG_MAX, exec_tile_idx, NULL );
3669 0 : FD_TEST( res==0 );
3670 0 : for( ulong i=0UL; i<cnt; i++ ) {
3671 0 : ulong txn_idx = msg->txn_sigverify->txn_idx[ i ];
3672 0 : fd_sched_txn_info_t const * txn_info = fd_sched_get_txn_info( ctx->sched, txn_idx );
3673 0 : if( FD_LIKELY( (txn_info->flags&FD_SCHED_TXN_REPLAY_DONE)==FD_SCHED_TXN_REPLAY_DONE ) ) {
3674 0 : publish_txn_executed( ctx, stem, bank->idx, txn_idx );
3675 0 : }
3676 0 : }
3677 0 : break;
3678 0 : }
3679 0 : case FD_EXECRP_TT_POH_HASH: {
3680 0 : int res = fd_sched_task_done( ctx->sched, FD_SCHED_TT_POH_HASH, ULONG_MAX, exec_tile_idx, msg->poh_hash );
3681 0 : if( FD_UNLIKELY( res && bank->state!=FD_BANK_STATE_DEAD ) ) {
3682 0 : mark_bank_dead( ctx, stem, bank->idx, sched_dead_reason_to_event( res ), FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3683 0 : }
3684 0 : break;
3685 0 : }
3686 0 : case FD_EXECRP_TT_CERT_VERIFY: {
3687 0 : FD_TEST( bank->footer_task_cnt );
3688 0 : bank->footer_task_cnt--;
3689 0 : if( msg->cert_verify->has_hm ) fd_footer_cache_hash_insert( ctx->footer_cache, ctx->cert_payload[ exec_tile_idx ], ctx->cert_payload_sz[ exec_tile_idx ], &msg->cert_verify->hm );
3690 0 : if( FD_UNLIKELY( msg->cert_verify->err && bank->state!=FD_BANK_STATE_DEAD ) ) {
3691 0 : mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3692 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3693 0 : }
3694 : /* The last footer task back finishes and publishes the block,
3695 : before sched can start its child. */
3696 0 : if( !bank->footer_task_cnt && ctx->footer_end[ bank->idx ].finalized && bank->state==FD_BANK_STATE_REPLAYABLE && !fd_sched_block_is_discarded( ctx->sched, bank->idx ) ) {
3697 0 : if( FD_UNLIKELY( replay_block_end( ctx, stem, bank, fd_sched_get_footer( ctx->sched, bank->idx ), bank->execution_fees_pre_settle, bank->priority_fees_pre_settle ) ) ) {
3698 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3699 0 : }
3700 0 : }
3701 0 : FD_TEST( !fd_sched_task_done( ctx->sched, FD_SCHED_TT_CERT_VERIFY, ULONG_MAX, exec_tile_idx, NULL ) );
3702 0 : break;
3703 0 : }
3704 0 : case FD_EXECRP_TT_REWARDS: {
3705 0 : FD_TEST( bank->footer_task_cnt && ctx->rewards_in_flight_cnt );
3706 0 : bank->footer_task_cnt--;
3707 0 : ctx->rewards_in_flight_cnt--;
3708 0 : if( FD_UNLIKELY( msg->rewards->err ) ) {
3709 0 : if( bank->state!=FD_BANK_STATE_DEAD ) {
3710 0 : mark_bank_dead( ctx, stem, bank->idx, FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_BAD_FOOTER, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED, NULL );
3711 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3712 0 : }
3713 0 : } else {
3714 0 : fd_replay_footer_end_t * end = ctx->footer_end + bank->idx;
3715 0 : end->leader_credits = fd_ulong_sat_add( end->leader_credits, msg->rewards->leader_credits );
3716 0 : fd_lthash_add( fd_bank_lthash_locking_modify( bank ), &ctx->execrp_result[ exec_tile_idx ].lthash );
3717 0 : fd_bank_lthash_end_locking_modify( bank );
3718 0 : }
3719 : /* The last footer task back finishes and publishes the block,
3720 : before sched can start its child. */
3721 0 : if( !bank->footer_task_cnt && ctx->footer_end[ bank->idx ].finalized && bank->state==FD_BANK_STATE_REPLAYABLE && !fd_sched_block_is_discarded( ctx->sched, bank->idx ) ) {
3722 0 : if( FD_UNLIKELY( replay_block_end( ctx, stem, bank, fd_sched_get_footer( ctx->sched, bank->idx ), bank->execution_fees_pre_settle, bank->priority_fees_pre_settle ) ) ) {
3723 0 : fd_sched_block_abandon( ctx->sched, bank->idx, FD_SCHED_ABANDON_INVALID );
3724 0 : }
3725 0 : }
3726 0 : FD_TEST( !fd_sched_task_done( ctx->sched, FD_SCHED_TT_REWARDS, ULONG_MAX, exec_tile_idx, NULL ) );
3727 0 : break;
3728 0 : }
3729 0 : case FD_EXECRP_TT_LTHASH_SUB:
3730 0 : case FD_EXECRP_TT_LTHASH_ADD: {
3731 0 : ulong task_type = (sig>>32)==FD_EXECRP_TT_LTHASH_SUB ? FD_SCHED_TT_LTHASH_SUB : FD_SCHED_TT_LTHASH_ADD;
3732 0 : int res = fd_sched_task_done( ctx->sched, task_type, ULONG_MAX, exec_tile_idx, &ctx->execrp_result[ exec_tile_idx ].lthash );
3733 0 : FD_TEST( res==0 );
3734 0 : break;
3735 0 : }
3736 0 : default: FD_LOG_CRIT(( "unexpected sig 0x%lx", sig ));
3737 0 : }
3738 :
3739 : /* Reference counter just decreased, and an exec tile just got freed
3740 : up. If there's a need to be more aggressively pruning, we could
3741 : check here if more slots just became publishable and publish. Not
3742 : publishing here shouldn't bloat the fork tree too much though. We
3743 : mark minority forks dead as soon as we can, and execution dispatch
3744 : stops on dead blocks. So shortly afterwards, dead blocks should be
3745 : eligible for pruning as in-flight transactions retire from the
3746 : execution pipeline. */
3747 :
3748 0 : }
3749 :
3750 : /* Essentially an inlined version of try_process_fec that reads off the
3751 : dcache directly, instead of the reasm out_queue.
3752 : All FECs processed by this function must be safe to be forever
3753 : removed from the dcache. */
3754 : static void
3755 : process_rotor_fec( fd_replay_tile_t * ctx,
3756 : fd_stem_context_t * stem,
3757 0 : fd_repair_out_fec_t * fec ) {
3758 0 : if( FD_LIKELY( fec->highest_fec_complete_slot && ( ctx->catch_up_max_fec_slot==ULONG_MAX || fec->highest_fec_complete_slot>ctx->catch_up_max_fec_slot ) ) ) {
3759 0 : ctx->catch_up_max_fec_slot = fec->highest_fec_complete_slot;
3760 0 : ctx->catch_up_tip_advance_cnt++;
3761 0 : }
3762 :
3763 0 : ulong parent_bank_idx = ULONG_MAX;
3764 0 : if( FD_UNLIKELY( fec->fec_set_idx==fec->update_parent_fec_idx ) ) {
3765 0 : fd_block_id_t parent_key = fd_block_id( fec->parent_slot, fec->parent_block_id.uc );
3766 0 : fd_block_id_ele_t * parent = fd_block_id_map_ele_query( ctx->block_id_map, &parent_key, NULL, ctx->block_id_arr );
3767 0 : FD_TEST( parent );
3768 0 : parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
3769 0 : } else {
3770 : /* Known blocks use {slot, block_id}; turbine blocks use {slot, 0}
3771 : until their slot-complete FEC rekeys the bank. */
3772 0 : fd_block_id_t key = { .slot = fec->slot };
3773 0 : if( FD_LIKELY( fec->is_known ) ) {
3774 0 : key = fd_block_id( fec->slot, fec->block_id.uc );
3775 0 : }
3776 :
3777 0 : fd_block_id_ele_t * parent = fd_block_id_map_ele_query( ctx->block_id_map, &key, NULL, ctx->block_id_arr );
3778 0 : FD_TEST( parent );
3779 0 : parent_bank_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, parent );
3780 0 : }
3781 :
3782 0 : fd_bank_t * parent_bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
3783 0 : FD_TEST( parent_bank );
3784 :
3785 0 : if( FD_UNLIKELY( parent_bank->state==FD_BANK_STATE_DEAD ) ) {
3786 0 : FD_LOG_WARNING(( "parent bank is dead for slot %lu, fec set idx %u, parent slot %lu, dropping", fec->slot, fec->fec_set_idx, fec->parent_slot ));
3787 0 : return;
3788 0 : }
3789 :
3790 0 : long work = fd_tickcount();
3791 :
3792 0 : fd_store_fec_data_view_t data_view[1];
3793 0 : fd_rwlock_read( &ctx->store->fec_lock );
3794 0 : fd_store_fec_t * store_fec = fd_store_query( ctx->map_join, &fec->mr );
3795 0 : ctx->metrics.store_query_cnt++;
3796 0 : int view_err = store_fec ? fd_store_fec_data_view( ctx->store, ctx->store_disk_fd, store_fec, data_view ) : -1;
3797 0 : fd_rwlock_unread( &ctx->store->fec_lock );
3798 :
3799 : /* A missing store entry is expected: replay removes FEC sets when
3800 : publishing the root, so a FEC delivered for a slice that has
3801 : since been pruned/rooted is no longer in the store. The lookup and
3802 : payload pin are one lifetime transaction under fec_lock: remove may
3803 : unlink the key only after the view has pinned the pool element. */
3804 0 : if( FD_UNLIKELY( !store_fec || view_err ) ) {
3805 0 : ctx->metrics.store_query_missing_cnt++;
3806 0 : FD_BASE58_ENCODE_32_BYTES( fec->mr.key, key_b58 );
3807 0 : FD_LOG_INFO(( "store fec for slot: %lu unavailable (pruned by publish); abandoning slice. root: %lu. merkle: %s", fec->slot, ctx->consensus_root_slot, key_b58 ));
3808 0 : return;
3809 0 : }
3810 :
3811 0 : long now = fd_log_wallclock();
3812 0 : fd_block_id_ele_t * block_id_ele;
3813 0 : fd_bank_t * bank;
3814 0 : if( FD_UNLIKELY( fec->fec_set_idx==fec->update_parent_fec_idx ) ) {
3815 : /* Give up on the prefix bank if it exists: the block is not at
3816 : fault, so its sched block is discarded, not invalid, and
3817 : SLOT_DEAD is not published. */
3818 0 : fd_block_id_t prefix_key = fec->is_known ? fd_block_id( fec->slot, fec->block_id.uc ) : (fd_block_id_t){ .slot = fec->slot };
3819 0 : fd_block_id_ele_t * prefix = fec->update_parent_fec_idx && !fec->is_leader ? live_block_id_ele( ctx, fd_block_id_map_ele_query( ctx->block_id_map, &prefix_key, NULL, ctx->block_id_arr ) ) : NULL;
3820 0 : if( FD_UNLIKELY( prefix ) ) {
3821 0 : ulong opt_idx = fd_block_id_ele_get_idx( ctx->block_id_arr, prefix );
3822 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &prefix->block_info, NULL, ctx->block_id_arr )==prefix );
3823 0 : fd_sched_block_abandon( ctx->sched, opt_idx, FD_SCHED_ABANDON_DISCARDED );
3824 0 : ulong dead_idxs_cnt = 0UL;
3825 0 : fd_banks_mark_bank_dead( ctx->banks, opt_idx, ctx->dead_idxs, &dead_idxs_cnt );
3826 0 : for( ulong i=0UL; i<dead_idxs_cnt; i++ ) {
3827 0 : fd_block_id_ele_t * dead = &ctx->block_id_arr[ ctx->dead_idxs[ i ] ];
3828 0 : timing_slot_release( ctx, ctx->dead_idxs[ i ] );
3829 0 : report_block_incomplete( ctx, dead->slot, dead->block_id_seen ? &dead->dmr : &dead->latest_mr, fd_banks_bank_query( ctx->banks, ctx->dead_idxs[ i ] ),
3830 0 : FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_REPARENTED );
3831 0 : }
3832 0 : FD_LOG_INFO(( "abandoned optimistic bank (idx=%lu, slot=%lu), replaying the slot from its UpdateParent", opt_idx, fec->slot ));
3833 0 : try_prune_sched( ctx );
3834 0 : }
3835 0 : bank = fec->is_leader ? ctx->leader_bank : fd_banks_new_bank( ctx->banks, parent_bank_idx, now, 0 );
3836 0 : if( FD_UNLIKELY( fec->is_leader && ctx->leader_stats.slot==fec->slot ) ) {
3837 0 : ctx->leader_stats.first_fec_returned_nanos = now;
3838 0 : }
3839 :
3840 0 : block_id_ele = &ctx->block_id_arr[ bank->idx ];
3841 : /* Clear the previous occupant of this reused bank idx */
3842 0 : if( FD_LIKELY( fd_block_id_map_ele_query( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele ) ) {
3843 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ) );
3844 0 : }
3845 :
3846 0 : block_id_ele->block_id_seen = 0;
3847 0 : block_id_ele->bank_seq = bank->bank_seq;
3848 0 : block_id_ele->slot = fec->slot;
3849 0 : block_id_ele->latest_fec_idx = fec->fec_set_idx;
3850 0 : block_id_ele->fec_cnt = 0U;
3851 0 : block_id_ele->finalized = 0;
3852 0 : block_id_ele->block_info = fec->is_known ? fd_block_id( fec->slot, fec->block_id.uc ) : (fd_block_id_t){ .slot = fec->slot };
3853 :
3854 : /* If this fec 0 is rebuilding an evicted block (recovery redelivery),
3855 : the evicted bank's stale map entry may still hold this key at a
3856 : DIFFERENT idx. */
3857 0 : fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr ); // evicted / stale entry
3858 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
3859 0 : } else { /* FEC for the middle or end of a block */
3860 : /* Assign bank idx + seqno to the FEC. Update block id pool ele.
3861 : The block stays keyed by {slot, 0} in the compound map. */
3862 0 : block_id_ele = &ctx->block_id_arr[ parent_bank_idx ];
3863 0 : block_id_ele->latest_fec_idx = fec->fec_set_idx;
3864 0 : bank = fd_banks_bank_query( ctx->banks, parent_bank_idx );
3865 0 : }
3866 :
3867 0 : block_id_ele->latest_mr = fec->mr;
3868 :
3869 0 : if( FD_UNLIKELY( ctx->report_runtime_diffs && block_id_ele->fec_cnt<FD_FEC_BLK_MAX ) ) {
3870 0 : ctx->fec_chain[ bank->idx*FD_FEC_BLK_MAX + block_id_ele->fec_cnt ] = fec->mr;
3871 0 : }
3872 0 : block_id_ele->fec_cnt++;
3873 :
3874 0 : fd_reception_stats_t * stats = &ctx->reception_stats[ bank->idx ];
3875 0 : stats->bank_seq = bank->bank_seq;
3876 0 : stats->fec_set_idx = fec->fec_set_idx;
3877 0 : stats->votor_repaired = fec->votor_repaired;
3878 :
3879 0 : if( FD_UNLIKELY( fec->slot_complete ) ) {
3880 0 : FD_BASE58_ENCODE_32_BYTES( fec->block_id.uc, block_id_b58 );
3881 0 : FD_LOG_INFO(( "slot %lu fec set idx %u slot_bid %s is complete, inserted to map. finalized: %d", fec->slot, fec->fec_set_idx, block_id_b58, fec->is_finalized ));
3882 0 : block_id_ele->block_id_seen = 1;
3883 0 : block_id_ele->dmr = fec->block_id;
3884 0 : block_id_ele->finalized = !!fec->is_finalized;
3885 :
3886 : /* Block is complete and DMR block id is now known, so re-key. */
3887 0 : FD_TEST( fd_block_id_map_ele_remove( ctx->block_id_map, &block_id_ele->block_info, NULL, ctx->block_id_arr )==block_id_ele );
3888 0 : fd_block_id_t key = fd_block_id( fec->slot, fec->block_id.uc );
3889 0 : block_id_ele->block_info = key;
3890 0 : fd_block_id_map_ele_remove( ctx->block_id_map, &key, NULL, ctx->block_id_arr ); // evicted / stale entry
3891 0 : FD_TEST( fd_block_id_map_ele_insert( ctx->block_id_map, block_id_ele, ctx->block_id_arr ) );
3892 0 : }
3893 :
3894 : /* For leader FECs, don't insert the FEC into the scheduler. */
3895 0 : if( FD_UNLIKELY( fec->is_leader ) ) {
3896 0 : fd_store_fec_data_view_release( ctx->store, data_view );
3897 0 : return;
3898 0 : }
3899 :
3900 : /* Forks form a partial ordering over FEC sets. The Rotor tile
3901 : delivers FEC sets in-order per fork, but FEC set ordering across
3902 : forks is arbitrary */
3903 0 : fd_sched_fec_t sched_fec[ 1 ];
3904 0 : sched_fec->shred_cnt = FD_FEC_SHRED_CNT;
3905 0 : sched_fec->is_last_in_batch = !!fec->data_complete;
3906 0 : sched_fec->is_last_in_block = !!fec->slot_complete;
3907 0 : sched_fec->bank_idx = bank->idx;
3908 0 : sched_fec->parent_bank_idx = bank->parent_idx;
3909 0 : sched_fec->slot = fec->slot;
3910 0 : sched_fec->parent_slot = fec->parent_slot;
3911 0 : sched_fec->is_first_in_block = fec->fec_set_idx==fec->update_parent_fec_idx;
3912 0 : sched_fec->starts_at_update_parent = sched_fec->is_first_in_block && fec->update_parent_fec_idx!=0U; /* the feature is checked at BLOCK_START, after the bank is cloned */
3913 0 : sched_fec->fec = store_fec;
3914 0 : sched_fec->data = data_view->data;
3915 0 : sched_fec->alut_ctx->fork_id = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx )->accdb_fork_id;
3916 0 : sched_fec->alut_ctx->accdb = ctx->accdb;
3917 0 : sched_fec->alut_ctx->els = ctx->published_root_slot;
3918 0 : sched_fec->completed_ns = fec->fec_completed_ts_nanos;
3919 :
3920 0 : if( sched_fec->is_first_in_block ) {
3921 0 : bank->refcnt++;
3922 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt incremented to %lu for sched", bank->idx, sched_fec->slot, bank->refcnt ));
3923 0 : }
3924 :
3925 0 : int ingested = fd_sched_fec_ingest( ctx->sched, sched_fec );
3926 0 : fd_store_fec_data_view_release( ctx->store, data_view );
3927 :
3928 0 : if( FD_UNLIKELY( !ingested ) ) {
3929 0 : int dr = sched_block_dead_reason_to_event( ctx, sched_fec->bank_idx );
3930 0 : int ar = dr==FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD ? FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED
3931 0 : : FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_NOT_ABANDONED;
3932 0 : mark_bank_dead( ctx, stem, sched_fec->bank_idx, dr, ar, NULL );
3933 0 : return;
3934 0 : }
3935 :
3936 0 : fd_histf_sample( ctx->metrics.store_query_work, (ulong)( fd_tickcount() - work ) );
3937 0 : ctx->execrp_idle_cnt = 0UL;
3938 0 : return;
3939 0 : }
3940 :
3941 : static void
3942 0 : process_resolv_slot_completed( fd_replay_tile_t * ctx, ulong bank_idx ) {
3943 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, bank_idx );
3944 0 : FD_TEST( bank );
3945 0 : bank->refcnt--;
3946 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for resolv", bank->idx, bank->f.slot, bank->refcnt ));
3947 0 : }
3948 :
3949 : static inline void
3950 0 : maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
3951 0 : if( FD_LIKELY( ctx->has_genesis_hash && ctx->hard_fork_cnt!=ULONG_MAX ) ) {
3952 0 : ctx->shred_version = compute_shred_version( ctx->genesis_hash->uc, ctx->hard_forks, ctx->hard_fork_cnt );
3953 0 : }
3954 :
3955 0 : if( FD_LIKELY( ctx->expected_shred_version && ctx->ipecho_shred_version ) ) {
3956 0 : if( FD_UNLIKELY( ctx->expected_shred_version!=ctx->ipecho_shred_version ) ) {
3957 0 : FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from ipecho", ctx->expected_shred_version, ctx->ipecho_shred_version ) );
3958 0 : }
3959 0 : }
3960 :
3961 : /* The configured shred version is the post-restart one. Compare it
3962 : only on the boot bank: before boot the hard forks are the full
3963 : snapshot's, predating the restart. */
3964 0 : if( FD_UNLIKELY( ctx->wfs_enabled && !ctx->is_booted && ctx->expected_shred_version ) ) return;
3965 :
3966 0 : if( FD_LIKELY( ctx->shred_version && (ctx->expected_shred_version || ctx->ipecho_shred_version) ) ) {
3967 0 : ushort expected_shred_version = ctx->expected_shred_version ? ctx->expected_shred_version : ctx->ipecho_shred_version;
3968 0 : ushort actual_shred_version = ctx->shred_version;
3969 :
3970 0 : if( FD_UNLIKELY( expected_shred_version!=actual_shred_version ) ) {
3971 0 : char const * expected_source = ctx->expected_shred_version ?
3972 0 : "[consensus.expected_shred_version]" : "the boot entrypoint";
3973 0 : FD_LOG_ERR(( "genesis.bin at `%s` plus the loaded snapshot's hard_forks give shred version %hu, "
3974 0 : "but %s reports %hu. Delete genesis.bin to re-download it, or the snapshot if it "
3975 0 : "predates the latest hard fork, and restart.",
3976 0 : ctx->genesis_path, actual_shred_version, expected_source, expected_shred_version ));
3977 0 : }
3978 0 : }
3979 0 : }
3980 :
3981 : static inline void
3982 0 : maybe_verify_genesis_timestamp( fd_replay_tile_t * ctx ) {
3983 0 : if( FD_LIKELY( !ctx->has_expected_genesis_timestamp || !ctx->has_genesis_timestamp ) ) return;
3984 0 : if( FD_LIKELY( ctx->genesis_timestamp==ctx->expected_genesis_timestamp ) ) return;
3985 :
3986 0 : FD_LOG_ERR(( "Your genesis.bin file at `%s` has a genesis timestamp of %lu but the snapshot you loaded has a genesis "
3987 0 : "timestamp of %lu. This either means that the genesis.bin file you have is for a different cluster than "
3988 0 : "the one you are trying to connect to, or you have loaded a snapshot for the wrong cluster. In either "
3989 0 : "case, you can delete the problematic file and restart the node to download the correct one automatically.",
3990 0 : ctx->genesis_path, ctx->genesis_timestamp, ctx->expected_genesis_timestamp ));
3991 0 : }
3992 :
3993 : /* snapmk_start instructs the snapmk tile to start producing a snapshot. */
3994 :
3995 : static void
3996 : snapmk_start( fd_replay_tile_t * ctx,
3997 : fd_stem_context_t * stem,
3998 0 : int incremental ) {
3999 :
4000 0 : FD_CHECK_CRIT( !ctx->snapmk.active, "snapshot creation already in progress" );
4001 :
4002 : /* pin current produced bank */
4003 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, ctx->published_root_bank_idx );
4004 0 : FD_CHECK_CRIT( bank, "invalid published_root_bank_idx" );
4005 :
4006 0 : if( FD_UNLIKELY( incremental ) ) {
4007 0 : FD_CHECK_CRIT( ctx->snapmk.base_slot!=ULONG_MAX, "incremental snapshot without a base full snapshot" );
4008 0 : FD_CHECK_CRIT( bank->f.slot>ctx->snapmk.base_slot, "incremental snapshot at or below its base slot" );
4009 0 : }
4010 :
4011 0 : bank->refcnt++;
4012 0 : ctx->snapmk.bank_idx = bank->idx;
4013 0 : ctx->snapmk.incremental = !!incremental;
4014 :
4015 : /* Snapmk reads the bank's epoch credits in place, so they stay
4016 : pinned in memory until snapmk_done. */
4017 0 : FD_TEST( fd_epoch_credits_view_init( ctx->snapmk.epoch_credits_view, fd_bank_epoch_credits( bank ), bank->epoch_credits_fork_id ) );
4018 :
4019 : /* Send SNAP_START message to snapmk. */
4020 0 : fd_replay_snap_start_t * msg = fd_chunk_to_laddr( ctx->snapmk_out->mem, ctx->snapmk_out->chunk );
4021 0 : *msg = (fd_replay_snap_start_t) {
4022 0 : .bank_idx = ctx->published_root_bank_idx,
4023 0 : .base_slot = incremental ? ctx->snapmk.base_slot : bank->f.slot,
4024 0 : .slot = bank->f.slot,
4025 0 : .leader = bank->leader
4026 0 : };
4027 0 : ulong out_idx = ctx->snapmk_out->idx;
4028 0 : ulong sig = REPLAY_SIG_SNAP_START;
4029 0 : ulong chunk = ctx->snapmk_out->chunk;
4030 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
4031 0 : ulong sz = sizeof(fd_replay_snap_start_t);
4032 0 : ulong seq = fd_stem_publish( stem, out_idx, sig, chunk, sz, 0UL, 0UL, tspub );
4033 0 : ctx->snapmk_out->chunk = fd_dcache_compact_next( ctx->snapmk_out->chunk, sz, ctx->snapmk_out->chunk0, ctx->snapmk_out->wmark );
4034 :
4035 : /* wake up the snapmk tile */
4036 0 : fd_frag_meta_t * replay_snapmk = stem->mcaches[ out_idx ];
4037 0 : ulong * snap_sync = fd_mcache_seq_laddr( replay_snapmk );
4038 0 : fd_mcache_seq_update( snap_sync, fd_seq_inc( seq, 1UL ) );
4039 0 : long ret = syscall( SYS_futex, snap_sync, FUTEX_WAKE, 1 );
4040 0 : if( FD_UNLIKELY( ret<0 ) ) {
4041 0 : FD_LOG_ERR(( "FUTEX_WAKE(snap_sync,seq=%u) failed (%i-%s)", (uint)seq, errno, fd_io_strerror( errno ) ));
4042 0 : }
4043 :
4044 : /* update internal state */
4045 0 : ctx->snapmk.active = 1;
4046 0 : }
4047 :
4048 : /* snapmk_done reacts to the snapmk tile reporting completion. */
4049 :
4050 : static void
4051 : snapmk_done( fd_replay_tile_t * ctx,
4052 : fd_stem_context_t * stem,
4053 0 : int success ) {
4054 0 : (void)stem;
4055 :
4056 0 : FD_CHECK_CRIT( ctx->snapmk.active, "spurious snap complete msg (not creating snapshot)" );
4057 :
4058 : /* release bank */
4059 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, ctx->snapmk.bank_idx );
4060 0 : FD_CHECK_CRIT( bank, "invalid snapmk.bank_idx" );
4061 0 : FD_CHECK_CRIT( bank->refcnt > 0UL, "invalid snapmk.bank_idx refcnt" );
4062 :
4063 : /* A completed full snapshot becomes the base of later incrementals. */
4064 0 : if( FD_LIKELY( success && !ctx->snapmk.incremental ) ) {
4065 0 : ctx->snapmk.base_slot = bank->f.slot;
4066 0 : }
4067 :
4068 0 : if( FD_LIKELY( success ) ) {
4069 0 : if( FD_UNLIKELY( ctx->snapmk.incremental ) ) {
4070 0 : ctx->snapmk.snap_finished_incr = bank->f.slot;
4071 0 : ctx->snapmk.snap_produced_incr_cnt++;
4072 0 : } else {
4073 0 : ctx->snapmk.snap_finished_full = bank->f.slot;
4074 0 : }
4075 0 : }
4076 :
4077 0 : fd_epoch_credits_view_fini( ctx->snapmk.epoch_credits_view );
4078 0 : bank->refcnt--;
4079 0 : ctx->snapmk.active = 0;
4080 0 : }
4081 :
4082 : static void
4083 : msg_snapmk( fd_replay_tile_t * ctx,
4084 : fd_stem_context_t * stem,
4085 : ulong msg_type,
4086 : ulong in_idx,
4087 0 : ulong chunk ) {
4088 0 : switch( msg_type ) {
4089 0 : case FD_SNAPMK_MSG_FOUND: {
4090 0 : fd_snapmk_msg_found_t const * found = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4091 0 : if( FD_UNLIKELY( found->base_slot==ULONG_MAX ) ) {
4092 0 : if( found->slot > ctx->snapmk.snap_finished_full ) ctx->snapmk.snap_finished_full = found->slot;
4093 0 : } else {
4094 0 : if( found->slot > ctx->snapmk.snap_finished_incr ) ctx->snapmk.snap_finished_incr = found->slot;
4095 0 : }
4096 0 : break;
4097 0 : }
4098 0 : case FD_SNAPMK_MSG_DELETED: {
4099 : /* Zeroing loses older surviving snapshots, but keeping a stale
4100 : watermark would claim one that no longer exists. */
4101 0 : fd_snapmk_msg_deleted_t const * deleted = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4102 0 : if( FD_UNLIKELY( deleted->base_slot==ULONG_MAX ) ) {
4103 0 : if( deleted->slot==ctx->snapmk.snap_finished_full ) ctx->snapmk.snap_finished_full = 0UL;
4104 0 : } else {
4105 0 : if( deleted->slot==ctx->snapmk.snap_finished_incr ) ctx->snapmk.snap_finished_incr = 0UL;
4106 0 : }
4107 0 : break;
4108 0 : }
4109 0 : case FD_SNAPMK_MSG_CREATED:
4110 0 : snapmk_done( ctx, stem, 1 );
4111 0 : break;
4112 0 : case FD_SNAPMK_MSG_FAILED:
4113 0 : snapmk_done( ctx, stem, 0 );
4114 0 : break;
4115 0 : default:
4116 0 : break;
4117 0 : }
4118 0 : }
4119 :
4120 : /* admin command handlers
4121 : every admin command must trigger one response frag */
4122 :
4123 : static void
4124 : admin_respond( fd_replay_tile_t * ctx,
4125 : fd_stem_context_t * stem,
4126 : ulong orig,
4127 0 : ulong err ) {
4128 0 : ulong ctl = fd_frag_meta_ctl( orig, 0, 0, !!err );
4129 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
4130 0 : fd_stem_publish( stem, ctx->admin_out_idx, err, 0UL, 0UL, ctl, 0UL, tspub );
4131 0 : }
4132 :
4133 : static void
4134 : admin_snap_create( fd_replay_tile_t * ctx,
4135 : fd_stem_context_t * stem,
4136 0 : ulong sig ) {
4137 0 : ulong target_slot = sig;
4138 :
4139 0 : if( FD_UNLIKELY( !ctx->snapmk.supported ) ) {
4140 0 : FD_LOG_WARNING(( "admin requested snapshot creation, but current config cannot create snapshots. increase [layout.snapzp_tile_count]?" ));
4141 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_UNSUPPORTED );
4142 0 : return;
4143 0 : }
4144 :
4145 0 : if( FD_UNLIKELY( !ctx->is_booted ) ) {
4146 0 : FD_LOG_WARNING(( "admin requested snapshot creation, but client has not yet started" ));
4147 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_NOT_READY );
4148 0 : return;
4149 0 : }
4150 :
4151 0 : if( FD_UNLIKELY( target_slot ) ) {
4152 0 : if( FD_UNLIKELY( target_slot<=ctx->published_root_slot ) ) {
4153 0 : FD_LOG_WARNING(( "admin requested snapshot creation at slot %lu, but rooting is already past it (published root slot %lu)", target_slot, ctx->published_root_slot ));
4154 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_SLOT_IN_PAST );
4155 0 : return;
4156 0 : }
4157 :
4158 :
4159 0 : if( FD_UNLIKELY( ctx->snapmk.scheduled_at_slot!=ULONG_MAX &&
4160 0 : ctx->snapmk.scheduled_at_slot!=target_slot ) ) {
4161 0 : FD_LOG_WARNING(( "admin requested snapshot creation at slot %lu, but a snapshot is already scheduled at slot %lu. ignoring ...", target_slot, ctx->snapmk.scheduled_at_slot ));
4162 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_BUSY );
4163 0 : return;
4164 0 : }
4165 :
4166 0 : ctx->snapmk.scheduled_at_slot = target_slot;
4167 0 : FD_LOG_NOTICE(( "snapshot creation scheduled at slot %lu", target_slot ));
4168 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_SUCCESS );
4169 0 : return;
4170 0 : }
4171 :
4172 0 : if( FD_UNLIKELY( ctx->snapmk.active ) ) {
4173 0 : FD_LOG_WARNING(( "admin requested snapshot creation, but currently busy creating another snapshot. ignoring ..." ));
4174 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_SNAPSHOT_CREATE_RESULT_BUSY );
4175 0 : return;
4176 0 : }
4177 :
4178 0 : snapmk_start( ctx, stem, 0 );
4179 0 : admin_respond( ctx, stem, FD_ADMINCTL_CMD_SNAP_CREATE, FD_ADMINCTL_RESULT_SUCCESS );
4180 0 : }
4181 :
4182 : static void
4183 : msg_admin( fd_replay_tile_t * ctx,
4184 : fd_stem_context_t * stem,
4185 : ulong orig,
4186 0 : ulong sig ) {
4187 0 : switch( orig ) {
4188 0 : case FD_ADMINCTL_CMD_SNAP_CREATE:
4189 0 : admin_snap_create( ctx, stem, sig );
4190 0 : break;
4191 0 : default:
4192 0 : FD_LOG_CRIT(( "unknown admin cmd (orig=%lu, sig=%lu)", orig, sig ));
4193 0 : }
4194 0 : }
4195 :
4196 : static inline int
4197 : returnable_frag( fd_replay_tile_t * ctx,
4198 : ulong in_idx,
4199 : ulong seq,
4200 : ulong sig,
4201 : ulong chunk,
4202 : ulong sz,
4203 : ulong ctl,
4204 : ulong tsorig,
4205 : ulong tspub,
4206 0 : fd_stem_context_t * stem ) {
4207 0 : (void)seq;
4208 0 : (void)ctl;
4209 0 : (void)tsorig;
4210 0 : (void)tspub;
4211 :
4212 0 : if( FD_UNLIKELY( sz!=0UL && (chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) ) )
4213 0 : FD_LOG_CRIT(( "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 ));
4214 :
4215 0 : switch( ctx->in_kind[in_idx] ) {
4216 0 : case IN_KIND_GENESIS: {
4217 0 : fd_genesis_meta_t const * meta = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4218 0 : ctx->has_genesis_hash = 1;
4219 0 : ctx->has_genesis_timestamp = 1;
4220 0 : ctx->genesis_timestamp = meta->creation_time_seconds;
4221 0 : *ctx->genesis_hash = meta->genesis_hash;
4222 0 : fd_node_info_write_begin( ctx->node_info );
4223 0 : ctx->node_info->info.genesis_hash = *ctx->genesis_hash;
4224 0 : fd_node_info_write_end( ctx->node_info );
4225 0 : if( FD_LIKELY( meta->bootstrap ) ) {
4226 0 : boot_genesis( ctx, stem, meta );
4227 0 : } else {
4228 0 : uchar const * genesis_blob = (uchar const *)( meta+1 );
4229 0 : FD_TEST( fd_genesis_parse( ctx->genesis, genesis_blob, meta->blob_sz ) );
4230 0 : }
4231 0 : ctx->has_genesis_timestamp = 1;
4232 0 : ctx->genesis_timestamp = ctx->genesis->creation_time;
4233 :
4234 0 : maybe_verify_cluster_type( ctx );
4235 0 : maybe_verify_shred_version( ctx );
4236 0 : maybe_verify_genesis_timestamp( ctx );
4237 0 : break;
4238 0 : }
4239 0 : case IN_KIND_IPECHO: {
4240 0 : FD_TEST( sig && sig<=USHORT_MAX );
4241 0 : ctx->ipecho_shred_version = (ushort)sig;
4242 0 : maybe_verify_shred_version( ctx );
4243 0 : break;
4244 0 : }
4245 0 : case IN_KIND_SNAP: {
4246 : /* Booting from the snapshot needs the parsed genesis (see
4247 : restore_default_slot_params). genesi_out and snapin_manif are
4248 : independent links, so hold DONE until genesis has arrived. */
4249 0 : if( FD_UNLIKELY( fd_ssmsg_sig_message( sig )==FD_SSMSG_DONE && !ctx->has_genesis_hash ) ) return 1;
4250 0 : on_snapshot_message( ctx, stem, in_idx, chunk, sig );
4251 0 : maybe_verify_cluster_type( ctx );
4252 0 : maybe_verify_shred_version( ctx );
4253 0 : maybe_verify_genesis_timestamp( ctx );
4254 0 : break;
4255 0 : }
4256 0 : case IN_KIND_EXECRP: {
4257 0 : process_exec_task_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ), sz, sig );
4258 0 : ctx->execrp_idle_cnt = 0UL;
4259 0 : break;
4260 0 : }
4261 0 : case IN_KIND_MOTOR: {
4262 0 : process_motor_done( ctx, stem, fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk ) );
4263 0 : break;
4264 0 : }
4265 0 : case IN_KIND_RESOLV: {
4266 0 : fd_resolv_slot_exchanged_t * exchanged_slot = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4267 0 : process_resolv_slot_completed( ctx, exchanged_slot->bank_idx );
4268 0 : break;
4269 0 : }
4270 0 : case IN_KIND_VOTOR: {
4271 0 : if( FD_UNLIKELY( sig==FD_VOTOR_SIG_LEADER ) ) {
4272 0 : fd_votor_leader_t const * leader = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4273 0 : *ctx->votor_leader = *leader;
4274 0 : ctx->next_leader_slot = leader->slot;
4275 0 : try_become_leader( ctx, stem, FD_METRICS_SAMPLE_REASON_LEADER_START, 0UL );
4276 0 : } else if( FD_LIKELY( sig==FD_VOTOR_SIG_CERTED ) ) {
4277 0 : fd_votor_certed_t const * certed = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4278 0 : fd_votor_certed_t * fin = ctx->votor_final;
4279 0 : switch( certed->kind ) {
4280 0 : case FD_CERT_KIND_FINAL:
4281 0 : if( fin->slot==ULONG_MAX || certed->slot>fin->slot ) *fin = *certed;
4282 0 : try_advance_root( ctx, fd_block_id( certed->slot, certed->block_id.uc ) );
4283 0 : break;
4284 0 : case FD_CERT_KIND_FAST_FINAL: /* fast beats slow at the same slot */
4285 0 : if( fin->slot==ULONG_MAX || certed->slot>fin->slot || ( certed->slot==fin->slot && fin->kind==FD_CERT_KIND_FINAL ) ) *fin = *certed;
4286 0 : try_advance_root( ctx, fd_block_id( certed->slot, certed->block_id.uc ) );
4287 0 : break;
4288 0 : default: break;
4289 0 : }
4290 0 : } else if( FD_UNLIKELY( sig==FD_VOTOR_SIG_PARENT_READY ) ) {
4291 0 : fd_votor_parent_ready_t const * pr = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4292 0 : ctx->votor_parent_ready_slot = fd_ulong_max( ctx->votor_parent_ready_slot, pr->slot );
4293 0 : } else if( FD_UNLIKELY( sig==FD_VOTOR_SIG_QUORUM ) ) {
4294 0 : fd_votor_quorum_t const * quorum = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4295 0 : if( FD_UNLIKELY( quorum->kind==FD_VOTOR_QUORUM_KIND_IMPLICITLY_FINALIZED ) ) {
4296 0 : fd_block_id_t block_id = fd_block_id( quorum->slot, quorum->block_id.uc );
4297 0 : if( FD_UNLIKELY( fd_bhm_map_ele_query( ctx->bhm_map, &block_id, NULL, ctx->bhm_pool ) ) ) {
4298 0 : FD_BASE58_ENCODE_32_BYTES( block_id.hash.uc, block_id_b58 );
4299 0 : FD_LOG_ERR(( "HARD FORK DETECTED for slot %lu block ID `%s`", block_id.slot, block_id_b58 ));
4300 0 : }
4301 0 : }
4302 0 : } else if( FD_UNLIKELY( sig==FD_VOTOR_SIG_REWARD ) ) {
4303 0 : fd_votor_reward_t const * reward = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4304 0 : fd_votor_reward_t * ring = &ctx->votor_reward[ reward->slot%(FD_NUM_SLOTS_FOR_REWARD+FD_VOTOR_SLOTS_PER_WINDOW+1UL) ];
4305 0 : if( ring->slot==ULONG_MAX || reward->slot>=ring->slot ) *ring = *reward;
4306 0 : }
4307 0 : break;
4308 0 : }
4309 0 : case IN_KIND_REPAIR: {
4310 0 : if( FD_UNLIKELY( sig!=REPAIR_SIG_FEC ) ) break;
4311 :
4312 : /* process rotor incoming FECs. 1 to keep frag for retry, returning 0 consumes it. */
4313 :
4314 0 : fd_repair_out_fec_t * fec = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
4315 0 : if( FD_UNLIKELY( !( ctx->execrp_idle_cnt>=2UL*ctx->in_cnt || ctx->is_leader ) ) ) return 1; /* pace behind exec notifications, retry */
4316 :
4317 : /* Rare case where we replayed a slot completely, but on re-delivery
4318 : we discover the FEC is finalized. */
4319 0 : if( FD_UNLIKELY( fec->is_finalized && fec->slot_complete ) ) {
4320 0 : fd_block_id_t key = fd_block_id( fec->slot, fec->block_id.uc );
4321 0 : if( FD_UNLIKELY( fd_bhm_map_ele_query( ctx->bhm_map, &key, NULL, ctx->bhm_pool ) ) ) {
4322 0 : FD_BASE58_ENCODE_32_BYTES( key.hash.uc, block_id_b58 );
4323 0 : FD_LOG_ERR(( "HARD FORK DETECTED for slot %lu block ID `%s`", key.slot, block_id_b58 ));
4324 0 : }
4325 0 : fd_block_id_ele_t * ele = live_block_id_ele( ctx, fd_block_id_map_ele_query( ctx->block_id_map, &key, NULL, ctx->block_id_arr ) );
4326 0 : if( FD_LIKELY( ele && ele->block_id_seen ) ) ele->finalized = 1;
4327 0 : }
4328 :
4329 0 : int evict_banks = 0;
4330 0 : int res = can_process_rotor_fec( ctx, fec, &evict_banks );
4331 :
4332 : /* drain_rotor_fecs: a bank eviction broke the replayable chain of
4333 : FECs delivered from rotor (we lost a parent we needed to
4334 : replay off of). While draining, ignore DROP/SKIP FECs until
4335 : one is replayable again (PROCESS_FEC_OK, i.e. its parent
4336 : context is present), then resume normal processing. WAIT must
4337 : fall through to the retry/eviction logic below: WAIT's contract
4338 : is keep-and-retry, and the redelivered FEC that rebuilds the
4339 : evicted block can itself return WAIT+evict_banks (banks full) --
4340 : swallowing it would consume the frag, never queue the eviction,
4341 : and wedge recovery. */
4342 0 : if( FD_UNLIKELY( ctx->drain_rotor_fecs ) ) {
4343 0 : if( FD_LIKELY( res==PROCESS_FEC_OK ) ) {
4344 0 : ctx->drain_rotor_fecs = 0; /* chain re-established, resume */
4345 0 : }
4346 0 : else if( res!=PROCESS_FEC_WAIT ) return 0; /* swallow DROPs/SKIPs, do evict_banks if needed */
4347 0 : }
4348 :
4349 0 : switch( res ) {
4350 0 : case PROCESS_FEC_OK: {
4351 0 : process_rotor_fec( ctx, stem, fec );
4352 0 : return 0;
4353 0 : }
4354 0 : case PROCESS_FEC_DROP: {
4355 : /* enter drain state */
4356 0 : ctx->drain_rotor_fecs = 1;
4357 0 : publish_replay_out( ctx, stem, REPLAY_SIG_MISSING_FEC, 0 );
4358 0 : return 0;
4359 0 : }
4360 0 : case PROCESS_FEC_SKIP: { return 0; }
4361 0 : case PROCESS_FEC_WAIT: {
4362 : /* queue an eviction, then retry the frag. */
4363 0 : if( FD_UNLIKELY( evict_banks ) ) {
4364 0 : ulong evictable_bank_idx = fd_banks_get_evictable_bank( ctx->banks, ctx->notified_root_bank );
4365 0 : if( FD_UNLIKELY( evictable_bank_idx==ULONG_MAX ) ) {
4366 0 : FD_LOG_DEBUG(( "replay has no banks to mark as prunable, it's possible that there is one bank already marked as prunable" ));
4367 0 : return 1;
4368 0 : }
4369 :
4370 0 : FD_LOG_WARNING(( "banks full, evicting bank (idx=%lu)", evictable_bank_idx ));
4371 :
4372 0 : timing_slot_release( ctx, evictable_bank_idx );
4373 :
4374 0 : if( FD_UNLIKELY( fd_sched_block_is_discarded( ctx->sched, evictable_bank_idx ) ) ) {
4375 0 : fd_block_id_ele_t * ele = &ctx->block_id_arr[ evictable_bank_idx ];
4376 0 : report_block_incomplete( ctx, ele->slot, ele->block_id_seen ? &ele->dmr : &ele->latest_mr, fd_banks_bank_query( ctx->banks, evictable_bank_idx ),
4377 0 : FD_EVENT_BLOCK_COMPLETED_DEAD_REASON_NOT_DEAD, FD_EVENT_BLOCK_COMPLETED_ABANDONED_REASON_PRUNED );
4378 0 : }
4379 :
4380 : /* Send a notification to other tiles to drop a reference to the
4381 : evictable bank. The RPC tile is the only tile which holds onto
4382 : non-rooted banks, non-transiently. */
4383 0 : fd_replay_drop_bank_ref_t * msg = fd_chunk_to_laddr( ctx->replay_out->mem, ctx->replay_out->chunk );
4384 0 : fd_sched_block_abandon( ctx->sched, evictable_bank_idx, FD_SCHED_ABANDON_DISCARDED );
4385 0 : msg->bank_idx = evictable_bank_idx;
4386 0 : publish_replay_out( ctx, stem, REPLAY_SIG_DROP_BANK_REF, sizeof(fd_replay_drop_bank_ref_t) );
4387 0 : }
4388 0 : return 1;
4389 0 : }
4390 0 : default:
4391 0 : FD_LOG_CRIT(( "unhandled process_rotor_fec result: %d", res ));
4392 0 : }
4393 0 : break;
4394 0 : }
4395 0 : case IN_KIND_GOSSIP_OUT: {
4396 0 : FD_TEST( sig==FD_GOSSIP_UPDATE_TAG_WFS_DONE );
4397 0 : if( FD_UNLIKELY( ctx->wfs_complete ) ) break;
4398 0 : ctx->wfs_complete = 1;
4399 0 : maybe_verify_shred_version( ctx );
4400 :
4401 0 : FD_LOG_NOTICE(( "Done waiting for supermajority. More than 80 percent of cluster stake has joined." ));
4402 :
4403 0 : if( FD_LIKELY( ctx->replay_out->idx!=ULONG_MAX ) ) {
4404 0 : publish_replay_out( ctx, stem, REPLAY_SIG_WFS_DONE, 0UL );
4405 0 : }
4406 0 : break;
4407 0 : }
4408 0 : case IN_KIND_RPC: {
4409 0 : fd_bank_t * bank = fd_banks_bank_query( ctx->banks, sig );
4410 0 : FD_TEST( bank );
4411 0 : bank->refcnt--;
4412 0 : FD_LOG_DEBUG(( "bank (idx=%lu, slot=%lu) refcnt decremented to %lu for %s", bank->idx, bank->f.slot, bank->refcnt, ctx->in_kind[ in_idx ]==IN_KIND_RPC ? "rpc" : "gui" ));
4413 0 : break;
4414 0 : }
4415 0 : case IN_KIND_SNAPMK:
4416 0 : msg_snapmk( ctx, stem, sig, in_idx, chunk );
4417 0 : break;
4418 0 : case IN_KIND_ADMIN:
4419 0 : msg_admin( ctx, stem, fd_frag_meta_ctl_orig( ctl ), sig );
4420 0 : break;
4421 0 : default:
4422 0 : FD_LOG_ERR(( "unhandled kind %d", ctx->in_kind[ in_idx ] ));
4423 0 : }
4424 :
4425 0 : return 0;
4426 0 : }
4427 :
4428 : #undef PROCESS_FEC_DROP
4429 : #undef PROCESS_FEC_WAIT
4430 : #undef PROCESS_FEC_SKIP
4431 : #undef PROCESS_FEC_OK
4432 :
4433 : static inline fd_replay_out_link_t
4434 : out1( fd_topo_t const * topo,
4435 : fd_topo_tile_t const * tile,
4436 0 : char const * name ) {
4437 0 : ulong idx = ULONG_MAX;
4438 :
4439 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
4440 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
4441 0 : if( !strcmp( link->name, name ) ) {
4442 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 ));
4443 0 : idx = i;
4444 0 : }
4445 0 : }
4446 :
4447 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) return (fd_replay_out_link_t){ .idx = ULONG_MAX, .mem = NULL, .chunk0 = 0, .wmark = 0, .chunk = 0 };
4448 :
4449 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
4450 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
4451 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
4452 :
4453 0 : return (fd_replay_out_link_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
4454 0 : }
4455 :
4456 : static void
4457 : privileged_init( fd_topo_t const * topo,
4458 0 : fd_topo_tile_t const * tile ) {
4459 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
4460 :
4461 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
4462 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
4463 :
4464 0 : if( FD_UNLIKELY( !strcmp( tile->replay.identity_key_path, "" ) ) ) FD_LOG_ERR(( "identity_key_path not set" ));
4465 :
4466 0 : ctx->identity_pubkey[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->replay.identity_key_path, /* pubkey only: */ 1 ) );
4467 0 : ctx->identity_idx = 0UL;
4468 0 : ctx->voter_rank_cached = 0;
4469 0 : ctx->identity_dirty = 0;
4470 :
4471 0 : ctx->vote_account_staked = 0;
4472 0 : ctx->vote_account_inadmissible = 0;
4473 :
4474 0 : ctx->metrics.voted_slot = ULONG_MAX;
4475 :
4476 0 : ctx->has_vote_account = !!tile->replay.vote_account_path[ 0 ];
4477 0 : if( FD_LIKELY( ctx->has_vote_account ) ) {
4478 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.vote_account_path, ctx->vote_account->uc ) ) ) {
4479 0 : uchar const * vote_key = fd_keyload_load( tile->replay.vote_account_path, /* pubkey only: */ 1 );
4480 0 : fd_memcpy( ctx->vote_account->uc, vote_key, sizeof(fd_pubkey_t) );
4481 0 : }
4482 0 : }
4483 :
4484 0 : ctx->bundle.enabled = tile->replay.bundle.enabled;
4485 0 : if( FD_UNLIKELY( !tile->replay.bundle.vote_account_path[0] ) ) {
4486 0 : ctx->bundle.enabled = 0;
4487 0 : }
4488 :
4489 0 : if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
4490 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->replay.bundle.vote_account_path, ctx->bundle.vote_account.uc ) ) ) {
4491 0 : const uchar * vote_key = fd_keyload_load( tile->replay.bundle.vote_account_path, /* pubkey only: */ 1 );
4492 0 : fd_memcpy( ctx->bundle.vote_account.uc, vote_key, 32UL );
4493 0 : }
4494 0 : }
4495 :
4496 0 : FD_TEST( fd_rng_secure( &ctx->rng_seed, sizeof(ctx->rng_seed) ) );
4497 0 : FD_TEST( fd_rng_secure( &ctx->blockhash_seed, sizeof(ulong) ) );
4498 0 : FD_TEST( fd_rng_secure( &ctx->vote_tracker_seed, sizeof(ulong) ) );
4499 0 : FD_TEST( fd_rng_secure( &ctx->block_id_map_seed, sizeof(ulong) ) );
4500 0 : FD_TEST( fd_rng_secure( &ctx->bhm_map_seed, sizeof(ulong) ) );
4501 0 : FD_TEST( fd_rng_secure( &ctx->initial_block_id, sizeof(fd_hash_t) ) );
4502 0 : FD_TEST( fd_rng_secure( &ctx->runtime_stack_seed, sizeof(ulong) ) );
4503 :
4504 0 : ctx->store_disk_fd = -1;
4505 0 : ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
4506 0 : if( FD_LIKELY( store_obj_id!=ULONG_MAX ) ) {
4507 0 : fd_store_t * store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
4508 0 : FD_TEST( store && store->magic==FD_STORE_MAGIC );
4509 0 : ctx->store_disk_fd = FD_STORE_FD_RO;
4510 0 : if( FD_UNLIKELY( fcntl( ctx->store_disk_fd, F_GETFD )<0 ) )
4511 0 : FD_LOG_ERR(( "store file descriptor was not inherited (%i-%s)", errno, fd_io_strerror( errno ) ));
4512 0 : }
4513 0 : }
4514 :
4515 : static FD_TL fd_event_reporter_t replay_diff_reporter[1];
4516 :
4517 : static void
4518 : unprivileged_init( fd_topo_t const * topo,
4519 0 : fd_topo_tile_t const * tile ) {
4520 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
4521 0 : void * block_id_map_mem = NULL;
4522 0 : ulong chain_cnt = fd_block_id_map_chain_cnt_est( tile->replay.max_live_slots );
4523 :
4524 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
4525 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
4526 0 : void * genesis_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_genesis_align(), fd_genesis_footprint( fd_genesis_account_max( tile->replay.genesis_max_message_size ) ) );
4527 0 : void * runtime_stack_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_runtime_stack_align(), fd_runtime_stack_footprint( FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS ) );
4528 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 );
4529 0 : void * dead_idxs_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), sizeof(ulong) * tile->replay.max_live_slots );
4530 0 : void * fec_chain_mem = tile->replay.report_runtime_diffs ?
4531 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_hash_t), sizeof(fd_hash_t) * FD_FEC_BLK_MAX * tile->replay.max_live_slots ) : NULL;
4532 0 : void * slot_diffs_mem = tile->replay.report_runtime_diffs ?
4533 0 : FD_SCRATCH_ALLOC_APPEND( l, 8UL, FD_EVENT_RUNTIME_SLOT_DIFFS_FOOTPRINT * tile->replay.max_live_slots ) : NULL;
4534 0 : block_id_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_block_id_map_align(), fd_block_id_map_footprint( chain_cnt ) );
4535 0 : void * bhm_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_bhm_map_align(), fd_bhm_map_footprint( chain_cnt ) );
4536 0 : void * bhm_pool_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_bhm_pool_align(), fd_bhm_pool_footprint( tile->replay.max_live_slots ) );
4537 0 : void * bhm_dlist_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_bhm_dlist_align(), fd_bhm_dlist_footprint() );
4538 :
4539 0 : void * _txncache = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->replay.max_live_slots ) );
4540 0 : void * _accdb = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->replay.max_live_slots, 0 ) );
4541 0 : void * recp_stats_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_reception_stats_t), sizeof(fd_reception_stats_t)*tile->replay.max_live_slots );
4542 0 : void * sched_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_sched_align(), fd_sched_footprint( tile->replay.sched_depth, tile->replay.max_live_slots, tile->replay.max_shreds_per_block, tile->replay.max_txn_per_slot ) );
4543 0 : void * lthash_rec_keys = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_pubkey_t), FD_SCHED_LTHASH_START_WRITES_MAX*sizeof(fd_pubkey_t) );
4544 0 : void * vote_tracker_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_tracker_align(), fd_vote_tracker_footprint() );
4545 0 : void * _capture_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
4546 0 : void * dump_proto_ctx_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
4547 0 : void * block_completed_ev = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_event_block_completed_t), sizeof(fd_event_block_completed_t) );
4548 0 : void * timing_pool_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_timing_slot_pool_align(), fd_timing_slot_pool_footprint( FD_REPLAY_TXN_TIMING_SLOTS ) );
4549 0 : void * timing_rec_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_txn_timing_t), FD_REPLAY_TXN_TIMING_SLOTS*tile->replay.max_txn_per_slot*sizeof(fd_replay_txn_timing_t) );
4550 0 : void * timing_of_bank_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(ulong), tile->replay.max_live_slots*sizeof(ulong) );
4551 0 : void * footer_end_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_footer_end_t), tile->replay.max_live_slots*sizeof(fd_replay_footer_end_t) );
4552 0 : void * block_dump_ctx = NULL;
4553 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
4554 0 : block_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_block_dump_context_align(), fd_block_dump_context_footprint() );
4555 0 : }
4556 :
4557 0 : ctx->genesis = fd_genesis_new( genesis_mem, fd_genesis_account_max( tile->replay.genesis_max_message_size ) );
4558 0 : FD_TEST( ctx->genesis );
4559 :
4560 0 : ctx->runtime_stack = fd_runtime_stack_join( fd_runtime_stack_new( runtime_stack_mem, FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKED_VOTE_ACCOUNTS, FD_RUNTIME_MAX_STAKE_ACCOUNTS, ctx->runtime_stack_seed ) );
4561 0 : FD_TEST( ctx->runtime_stack );
4562 :
4563 0 : ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
4564 :
4565 0 : ulong store_obj_id = fd_pod_query_ulong( topo->props, "store", ULONG_MAX );
4566 0 : FD_TEST( store_obj_id!=ULONG_MAX );
4567 0 : ctx->store = fd_store_join( fd_topo_obj_laddr( topo, store_obj_id ) );
4568 0 : FD_TEST( ctx->store );
4569 0 : FD_TEST( fd_store_map_ljoin( ctx->store, ctx->map_join ) );
4570 :
4571 0 : ulong banks_obj_id = fd_pod_query_ulong( topo->props, "banks", ULONG_MAX );
4572 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
4573 :
4574 0 : ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
4575 0 : FD_TEST( ctx->banks );
4576 :
4577 0 : ulong footer_cache_obj_id = fd_pod_query_ulong( topo->props, "footer_cache", ULONG_MAX );
4578 0 : ctx->footer_cache = NULL;
4579 0 : if( footer_cache_obj_id!=ULONG_MAX ) {
4580 0 : ctx->footer_cache = fd_footer_cache_join( fd_topo_obj_laddr( topo, footer_cache_obj_id ) );
4581 0 : FD_TEST( ctx->footer_cache );
4582 0 : }
4583 :
4584 0 : ctx->leader_txn_timing = NULL;
4585 0 : ulong ldr_tt_obj_id = fd_pod_query_ulong( topo->props, "ldr_tt", ULONG_MAX );
4586 0 : if( FD_LIKELY( ldr_tt_obj_id!=ULONG_MAX ) ) ctx->leader_txn_timing = fd_topo_obj_laddr( topo, ldr_tt_obj_id );
4587 :
4588 0 : ulong node_info_obj_id = fd_pod_query_ulong( topo->props, "node_info", ULONG_MAX );
4589 0 : FD_TEST( node_info_obj_id!=ULONG_MAX );
4590 0 : ctx->node_info = fd_node_info_box_join( fd_topo_obj_laddr( topo, node_info_obj_id ) );
4591 0 : FD_TEST( ctx->node_info );
4592 0 : fd_node_info_write_begin( ctx->node_info );
4593 0 : ctx->node_info->info.identity = *ctx->identity_pubkey;
4594 0 : if( FD_LIKELY( ctx->has_vote_account ) ) ctx->node_info->info.vote_account = *ctx->vote_account;
4595 0 : fd_node_info_write_end( ctx->node_info );
4596 :
4597 0 : ulong wait_info_obj_id = fd_pod_query_ulong( topo->props, "wait_info", ULONG_MAX );
4598 0 : if( FD_LIKELY( wait_info_obj_id!=ULONG_MAX ) ) {
4599 0 : ctx->wait_info = fd_wait_info_box_join( fd_topo_obj_laddr( topo, wait_info_obj_id ) );
4600 0 : FD_TEST( ctx->wait_info );
4601 0 : } else {
4602 0 : ctx->wait_info = NULL;
4603 0 : }
4604 :
4605 0 : FD_MGAUGE_SET( REPLAY, BANK_CAPACITY, fd_banks_pool_max_cnt( ctx->banks ) );
4606 :
4607 0 : ctx->delinquent_known = 0;
4608 0 : ctx->delinquent_sample_slot = 0UL;
4609 0 : ctx->delinquent_stake_lamports = 0UL;
4610 0 : ctx->cluster_active_stake_lamports = 0UL;
4611 0 : ctx->ag_last_voted_epoch = ULONG_MAX;
4612 0 : ctx->ag_last_settled = 0UL;
4613 0 : ctx->ag_stamp_since = ULONG_MAX;
4614 0 : ctx->consensus_root_slot = ULONG_MAX;
4615 0 : ctx->consensus_root = ctx->initial_block_id;
4616 0 : ctx->finalized_block_id_lo = fd_block_id( ULONG_MAX, ctx->initial_block_id.uc );
4617 0 : ctx->finalized_block_id_hi = ctx->finalized_block_id_lo;
4618 0 : ctx->notified_root_slot = ULONG_MAX;
4619 0 : ctx->notified_root = ctx->initial_block_id;
4620 0 : ctx->notified_root_bank = NULL;
4621 0 : ctx->published_root_slot = ULONG_MAX;
4622 :
4623 0 : ctx->expected_shred_version = tile->replay.expected_shred_version;
4624 0 : ctx->ipecho_shred_version = 0;
4625 0 : ctx->shred_version = 0;
4626 0 : fd_memcpy( ctx->genesis_path, tile->replay.genesis_path, sizeof(ctx->genesis_path) );
4627 0 : ctx->has_genesis_hash = 0;
4628 0 : ctx->has_cluster_type = 0;
4629 0 : ctx->has_genesis_timestamp = 0;
4630 0 : ctx->has_expected_genesis_timestamp = 0;
4631 0 : ctx->cluster_type = FD_CLUSTER_UNKNOWN;
4632 0 : ctx->hard_fork_cnt = ULONG_MAX;
4633 0 : ctx->has_manifest_block_id = 0;
4634 :
4635 0 : if( FD_UNLIKELY( ctx->bundle.enabled ) ) {
4636 0 : if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->bundle.gen,
4637 0 : (fd_acct_addr_t const *)tile->replay.bundle.tip_distribution_program_addr,
4638 0 : (fd_acct_addr_t const *)tile->replay.bundle.tip_payment_program_addr,
4639 0 : (fd_acct_addr_t const *)ctx->bundle.vote_account.uc,
4640 0 : (fd_acct_addr_t const *)ctx->bundle.vote_account.uc, "NAN", 0UL ) ) ) {
4641 0 : FD_LOG_ERR(( "failed to initialize bundle crank gen" ));
4642 0 : }
4643 0 : }
4644 :
4645 0 : FD_TEST( tile->replay.enable_features_cnt<=sizeof(ctx->enable_features)/sizeof(ctx->enable_features[0]) );
4646 0 : ctx->enable_features_cnt = tile->replay.enable_features_cnt;
4647 0 : for( ulong i=0UL; i<tile->replay.enable_features_cnt; i++ ) {
4648 0 : fd_memcpy( ctx->enable_features[ i ], tile->replay.enable_features[ i ], FD_BASE58_ENCODED_32_SZ );
4649 0 : }
4650 :
4651 0 : ulong progcache_obj_id; FD_TEST( (progcache_obj_id = fd_pod_query_ulong( topo->props, "progcache", ULONG_MAX ) )!=ULONG_MAX );
4652 0 : FD_TEST( fd_progcache_shmem_join( ctx->progcache, fd_topo_obj_laddr( topo, progcache_obj_id ) ) );
4653 :
4654 0 : void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->replay.txncache_obj_id );
4655 0 : fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
4656 0 : FD_TEST( txncache_shmem );
4657 0 : ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem, FD_TXNCACHE_FD ) );
4658 0 : FD_TEST( ctx->txncache );
4659 :
4660 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->replay.accdb_obj_id );
4661 0 : fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
4662 0 : FD_TEST( accdb_shmem );
4663 0 : fd_sleep_t * accdb_sleep = topo->sleep_obj_id!=ULONG_MAX ? fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) ) : NULL;
4664 0 : ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, 0UL, NULL, accdb_sleep, fd_topo_find_tile( topo, "accdb", 0UL ), 0 ) );
4665 0 : FD_TEST( ctx->accdb );
4666 :
4667 0 : ctx->capture_ctx = NULL;
4668 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
4669 0 : ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
4670 0 : ctx->capture_ctx->solcap_start_slot = tile->replay.capture_start_slot;
4671 0 : ctx->capture_ctx->capture_solcap = 1;
4672 0 : }
4673 :
4674 0 : ctx->block_completed_event = block_completed_ev;
4675 :
4676 0 : ctx->timing_slot_pool = fd_timing_slot_pool_join( fd_timing_slot_pool_new( timing_pool_mem, FD_REPLAY_TXN_TIMING_SLOTS ) );
4677 0 : FD_TEST( ctx->timing_slot_pool );
4678 0 : ctx->timing_rec = timing_rec_mem;
4679 0 : ctx->timing_slot_of_bank = timing_of_bank_mem;
4680 0 : for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) ctx->timing_slot_of_bank[ i ] = fd_timing_slot_pool_idx_null( ctx->timing_slot_pool );
4681 :
4682 0 : ctx->dump_proto_ctx = NULL;
4683 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.dump_proto_dir ) ) ) {
4684 0 : ctx->dump_proto_ctx = dump_proto_ctx_mem;
4685 0 : ctx->dump_proto_ctx->dump_proto_output_dir = tile->replay.dump_proto_dir;
4686 0 : if( FD_LIKELY( tile->replay.dump_block_to_pb ) ) {
4687 0 : ctx->dump_proto_ctx->dump_block_to_pb = !!tile->replay.dump_block_to_pb;
4688 0 : }
4689 0 : }
4690 :
4691 0 : if( FD_UNLIKELY( tile->replay.dump_block_to_pb ) ) {
4692 0 : ctx->block_dump_ctx = fd_block_dump_context_join( fd_block_dump_context_new( block_dump_ctx ) );
4693 0 : } else {
4694 0 : ctx->block_dump_ctx = NULL;
4695 0 : }
4696 :
4697 0 : ctx->is_booted = 0;
4698 :
4699 0 : fd_clock_tile_init( ctx->clock );
4700 :
4701 0 : ctx->max_txn_per_slot = tile->replay.max_txn_per_slot;
4702 0 : ctx->max_shreds_per_block = tile->replay.max_shreds_per_block;
4703 :
4704 0 : FD_TEST( fd_rng_new( ctx->rng, ctx->rng_seed, 0UL ) );
4705 :
4706 0 : ctx->reception_stats = recp_stats_mem;
4707 0 : ctx->reception_stats_cnt = tile->replay.max_live_slots;
4708 0 : FD_TEST( ctx->reception_stats_cnt );
4709 0 : memset( ctx->reception_stats, 0, sizeof(fd_reception_stats_t)*ctx->reception_stats_cnt );
4710 :
4711 0 : ctx->leader_stats.slot = ULONG_MAX;
4712 0 : ctx->sched = fd_sched_join( fd_sched_new( sched_mem, ctx->rng, tile->replay.sched_depth, tile->replay.max_live_slots, ctx->max_shreds_per_block, ctx->max_txn_per_slot, fd_topo_tile_name_cnt( topo, "execrp" ) ) );
4713 0 : FD_TEST( ctx->sched );
4714 :
4715 0 : ctx->lthash_rec->cnt = 0UL;
4716 0 : ctx->lthash_rec->max = FD_SCHED_LTHASH_START_WRITES_MAX;
4717 0 : ctx->lthash_rec->keys = lthash_rec_keys;
4718 0 : ulong execrp_result_obj_id = fd_pod_query_ulong( topo->props, "execrp_result", ULONG_MAX );
4719 0 : FD_TEST( execrp_result_obj_id!=ULONG_MAX );
4720 0 : ctx->execrp_result = fd_topo_obj_laddr( topo, execrp_result_obj_id );
4721 :
4722 0 : ctx->in_cnt = tile->in_cnt;
4723 0 : ctx->execrp_idle_cnt = 0UL;
4724 :
4725 :
4726 0 : ctx->footer_end = footer_end_mem;
4727 0 : ctx->rewards_in_flight_cnt = 0UL;
4728 :
4729 0 : ctx->vote_tracker = fd_vote_tracker_join( fd_vote_tracker_new( vote_tracker_mem, ctx->vote_tracker_seed ) );
4730 0 : FD_TEST( ctx->vote_tracker );
4731 :
4732 0 : ctx->identity_vote_rooted = 0;
4733 :
4734 0 : ctx->wait_for_vote_to_start_leader = tile->replay.wait_for_vote_to_start_leader;
4735 :
4736 0 : ctx->wfs_hash_is_zero = !memcmp( tile->replay.wait_for_supermajority_with_bank_hash.uc, ((fd_hash_t){0}).uc, sizeof(fd_hash_t) );
4737 0 : ctx->wfs_enabled = fd_wfs_configured( tile->replay.wait_for_supermajority_at_slot,
4738 0 : ctx->wfs_hash_is_zero,
4739 0 : (ulong)tile->replay.expected_shred_version );
4740 0 : ctx->wait_for_supermajority_at_slot = tile->replay.wait_for_supermajority_at_slot;
4741 0 : ctx->expected_bank_hash = tile->replay.wait_for_supermajority_with_bank_hash;
4742 0 : ctx->wfs_complete = !ctx->wfs_enabled;
4743 :
4744 : /* With WFS, auto is resolved at boot instead (on_snapshot_message). */
4745 0 : if( FD_UNLIKELY( !ctx->wfs_enabled ) ) {
4746 0 : ctx->wait_for_vote_to_start_leader = wfs_resolve_leader_gate_auto( ctx->wait_for_vote_to_start_leader );
4747 0 : }
4748 :
4749 0 : ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
4750 0 : FD_TEST( ctx->mleaders );
4751 0 : ctx->next_leader_query_start = ULONG_MAX;
4752 :
4753 0 : ctx->is_leader = 0;
4754 0 : ctx->drain_rotor_fecs = 0;
4755 0 : ctx->supports_leader = fd_topo_find_tile( topo, "pack", 0UL )!=ULONG_MAX;
4756 0 : ctx->snapmk.active = 0;
4757 0 : ctx->snapmk.supported = fd_topo_find_tile( topo, "snapmk", 0UL )!=ULONG_MAX;
4758 0 : ctx->snapmk.scheduled_at_slot = ULONG_MAX;
4759 0 : ctx->snapmk.full_interval_blocks = tile->replay.full_snapshot_interval_blocks;
4760 0 : ctx->snapmk.next_full_block_height = ULONG_MAX;
4761 0 : ctx->snapmk.incremental_interval_blocks = tile->replay.incremental_snapshot_interval_blocks;
4762 0 : ctx->snapmk.next_incremental_block_height = ULONG_MAX;
4763 0 : ctx->snapmk.base_slot = ULONG_MAX;
4764 0 : ctx->snapmk.snap_finished_full = 0UL;
4765 0 : ctx->snapmk.snap_finished_incr = 0UL;
4766 0 : ctx->snapmk.snap_produced_incr_cnt = 0UL;
4767 0 : if( FD_UNLIKELY( !ctx->snapmk.supported ) ) {
4768 0 : ctx->snapmk.full_interval_blocks = 0UL;
4769 0 : ctx->snapmk.incremental_interval_blocks = 0UL;
4770 0 : }
4771 0 : ctx->reset_slot = 0UL;
4772 0 : ctx->epoch_end_slot = 0UL;
4773 0 : ctx->slots_per_epoch = 0UL;
4774 0 : ctx->ns_per_slot = 0UL;
4775 0 : ctx->next_leader_slot = ULONG_MAX;
4776 0 : ctx->highwater_leader_slot = ULONG_MAX;
4777 :
4778 0 : ctx->votor_final->slot = ULONG_MAX;
4779 0 : ctx->votor_parent_ready_slot = 0UL;
4780 0 : for( ulong i=0UL; i<FD_NUM_SLOTS_FOR_REWARD+FD_VOTOR_SLOTS_PER_WINDOW+1UL; i++ ) ctx->votor_reward[ i ].slot = ULONG_MAX;
4781 :
4782 0 : ctx->caught_up = 0;
4783 0 : ctx->catch_up_max_fec_slot = ULONG_MAX;
4784 0 : ctx->catch_up_tip_advance_cnt = 0UL;
4785 0 : ctx->boot_timestamp_nanos = tile->replay.boot_timestamp_nanos;
4786 0 : ctx->leader_bank = NULL;
4787 :
4788 0 : ctx->block_id_len = tile->replay.max_live_slots;
4789 0 : ctx->max_live_slots = tile->replay.max_live_slots;
4790 0 : ctx->block_id_arr = (fd_block_id_ele_t *)block_id_arr_mem;
4791 0 : ctx->dead_idxs = (ulong *)dead_idxs_mem;
4792 :
4793 0 : ctx->fec_chain = (fd_hash_t *)fec_chain_mem;
4794 0 : if( FD_UNLIKELY( slot_diffs_mem ) ) fd_event_runtime_slot_diffs_init( slot_diffs_mem, tile->replay.max_live_slots );
4795 :
4796 0 : ctx->block_id_map = fd_block_id_map_join( fd_block_id_map_new( block_id_map_mem, chain_cnt, ctx->block_id_map_seed ) );
4797 0 : FD_TEST( ctx->block_id_map );
4798 0 : ctx->bhm_map = fd_bhm_map_join( fd_bhm_map_new( bhm_map_mem, chain_cnt, ctx->bhm_map_seed ) );
4799 0 : FD_TEST( ctx->bhm_map );
4800 0 : ctx->bhm_pool = fd_bhm_pool_join( fd_bhm_pool_new( bhm_pool_mem, tile->replay.max_live_slots ) );
4801 0 : FD_TEST( ctx->bhm_pool );
4802 0 : ctx->bhm_dlist = fd_bhm_dlist_join( fd_bhm_dlist_new( bhm_dlist_mem ) );
4803 0 : FD_TEST( ctx->bhm_dlist );
4804 :
4805 :
4806 0 : for( ulong i=0UL; i<tile->replay.max_live_slots; i++ ) {
4807 0 : ctx->block_id_arr[ i ].block_id_seen = 0;
4808 0 : ctx->block_id_arr[ i ].fec_cnt = 0U;
4809 0 : memset( &ctx->block_id_arr[ i ].block_info, 0, sizeof(fd_block_id_t) );
4810 0 : }
4811 :
4812 0 : ctx->resolv_tile_cnt = fd_topo_tile_name_cnt( topo, "resolv" );
4813 :
4814 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
4815 0 : FD_TEST( ctx->keyswitch );
4816 0 : ctx->halt_replay = 0;
4817 :
4818 0 : FD_TEST( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]) );
4819 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
4820 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
4821 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
4822 :
4823 0 : if( FD_LIKELY( link->dcache ) ) {
4824 0 : ctx->in[ i ].mem = link_wksp->wksp;
4825 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
4826 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
4827 0 : ctx->in[ i ].mtu = link->mtu;
4828 0 : }
4829 :
4830 0 : if( !strcmp( link->name, "genesi_out" ) ) ctx->in_kind[ i ] = IN_KIND_GENESIS;
4831 0 : else if( !strcmp( link->name, "ipecho_out" ) ) ctx->in_kind[ i ] = IN_KIND_IPECHO;
4832 0 : else if( !strcmp( link->name, "snapin_manif" ) ) ctx->in_kind[ i ] = IN_KIND_SNAP;
4833 0 : else if( !strcmp( link->name, "execrp_replay" ) ) ctx->in_kind[ i ] = IN_KIND_EXECRP;
4834 0 : else if( !strcmp( link->name, "poh_replay" ) ) ctx->in_kind[ i ] = IN_KIND_MOTOR;
4835 0 : else if( !strcmp( link->name, "resolv_replay" ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
4836 0 : else if( !strcmp( link->name, "shred_out" ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR;
4837 0 : else if( !strcmp( link->name, "repair_out" ) ) ctx->in_kind[ i ] = IN_KIND_REPAIR;
4838 0 : else if( !strcmp( link->name, "rpc_replay" ) ) ctx->in_kind[ i ] = IN_KIND_RPC;
4839 0 : else if( !strcmp( link->name, "gossip_misc" ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP_OUT;
4840 0 : else if( !strcmp( link->name, "snapmk_out" ) ) ctx->in_kind[ i ] = IN_KIND_SNAPMK;
4841 0 : else if( !strcmp( link->name, "admin_replay" ) ) ctx->in_kind[ i ] = IN_KIND_ADMIN;
4842 0 : else if( !strcmp( link->name, "votor_out" ) ) ctx->in_kind[ i ] = IN_KIND_VOTOR;
4843 0 : else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
4844 :
4845 0 : if( ctx->in_kind[ i ]==IN_KIND_ADMIN ) {
4846 0 : FD_TEST( ( ctx->admin_out_idx = fd_topo_find_tile_out_link( topo, tile, "replay_admin", 0UL ) )!=ULONG_MAX );
4847 0 : }
4848 0 : }
4849 :
4850 0 : *ctx->epoch_out = out1( topo, tile, "replay_epoch" ); FD_TEST( ctx->epoch_out->idx!=ULONG_MAX );
4851 0 : *ctx->replay_out = out1( topo, tile, "replay_out" ); FD_TEST( ctx->replay_out->idx!=ULONG_MAX );
4852 0 : *ctx->slot_out = out1( topo, tile, "replay_slot" ); FD_TEST( ctx->slot_out->idx!=ULONG_MAX );
4853 0 : *ctx->rotor_out = out1( topo, tile, "replay_repair" );
4854 0 : *ctx->snapmk_out = out1( topo, tile, "replay_snapmk" ); FD_TEST( ctx->snapmk.supported == (ctx->snapmk_out->idx!=ULONG_MAX) );
4855 :
4856 0 : ctx->exec_cnt = 0UL;
4857 0 : for( ulong i=0UL; i<FD_SCHED_MAX_EXEC_TILE_CNT; i++ ) ctx->exec_out[ i ].idx = ULONG_MAX;
4858 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
4859 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
4860 0 : if( strcmp( link->name, "replay_execrp" ) ) continue;
4861 0 : FD_TEST( link->kind_id<FD_SCHED_MAX_EXEC_TILE_CNT && ctx->exec_out[ link->kind_id ].idx==ULONG_MAX );
4862 0 : void * mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
4863 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, link->dcache );
4864 0 : ctx->exec_out[ link->kind_id ] = (fd_replay_out_link_t){
4865 0 : .idx = i,
4866 0 : .mem = mem,
4867 0 : .chunk0 = chunk0,
4868 0 : .wmark = fd_dcache_compact_wmark( mem, link->dcache, link->mtu ),
4869 0 : .chunk = chunk0,
4870 0 : };
4871 0 : ctx->exec_cnt++;
4872 0 : }
4873 0 : FD_TEST( ctx->exec_cnt && ctx->exec_cnt==fd_topo_tile_name_cnt( topo, "execrp" ) );
4874 0 : for( ulong i=0UL; i<ctx->exec_cnt; i++ ) FD_TEST( ctx->exec_out[ i ].idx!=ULONG_MAX );
4875 :
4876 0 : ctx->slot_out_seq = fd_mcache_seq_laddr_const( topo->links[ tile->out_link_id[ ctx->slot_out->idx ] ].mcache );
4877 :
4878 0 : ctx->rpc_enabled = fd_topo_find_tile( topo, "rpc", 0UL )!=ULONG_MAX;
4879 :
4880 0 : if( FD_UNLIKELY( strcmp( "", tile->replay.solcap_capture ) ) ) {
4881 0 : ulong idx = fd_topo_find_tile_out_link( topo, tile, "cap_repl", 0UL );
4882 0 : FD_TEST( idx!=ULONG_MAX );
4883 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ idx ] ];
4884 :
4885 :
4886 0 : fd_capture_link_buf_t * cap_repl_out = ctx->cap_repl_out;
4887 0 : cap_repl_out->base.vt = &fd_capture_link_buf_vt;
4888 0 : cap_repl_out->idx = idx;
4889 0 : cap_repl_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
4890 0 : cap_repl_out->chunk0 = fd_dcache_compact_chunk0( cap_repl_out->mem, link->dcache );
4891 0 : cap_repl_out->wmark = fd_dcache_compact_wmark( cap_repl_out->mem, link->dcache, link->mtu );
4892 0 : cap_repl_out->chunk = cap_repl_out->chunk0;
4893 0 : cap_repl_out->mcache = link->mcache;
4894 0 : cap_repl_out->depth = fd_mcache_depth( link->mcache );
4895 0 : cap_repl_out->seq = 0UL;
4896 :
4897 0 : ctx->capture_ctx->capctx_type.buf = cap_repl_out;
4898 0 : ctx->capture_ctx->capture_link = &cap_repl_out->base;
4899 0 : ctx->capture_ctx->current_txn_idx = 0UL;
4900 :
4901 :
4902 0 : ulong consumer_tile_idx = fd_topo_find_tile( topo, "solcap", 0UL );
4903 0 : fd_topo_tile_t const * consumer_tile = &topo->tiles[ consumer_tile_idx ];
4904 0 : cap_repl_out->fseq = NULL;
4905 0 : for( ulong j = 0UL; j < consumer_tile->in_cnt; j++ ) {
4906 0 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ] == link->id ) ) {
4907 0 : cap_repl_out->fseq = fd_fseq_join( fd_topo_obj_laddr( topo, consumer_tile->in_link_fseq_obj_id[ j ] ) );
4908 0 : FD_TEST( cap_repl_out->fseq );
4909 0 : break;
4910 0 : }
4911 0 : }
4912 0 : }
4913 :
4914 0 : fd_memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
4915 :
4916 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->metrics.store_query_work, FD_MHIST_EDGES_NS( REPLAY, STORE_QUERY_DURATION_NANOS ) ) );
4917 :
4918 : /* Ensure precompiles are available, crash fast otherwise */
4919 0 : fd_precompiles();
4920 :
4921 0 : ctx->report_runtime_diffs = tile->replay.report_runtime_diffs;
4922 0 : ctx->banks->report_runtime_diffs = tile->replay.report_runtime_diffs;
4923 :
4924 0 : ulong diff_link_id = fd_topo_find_link( topo, "replay_diff", 0UL );
4925 0 : if( FD_UNLIKELY( diff_link_id!=ULONG_MAX ) ) fd_event_runtime_diff_tl = fd_event_reporter_join( replay_diff_reporter, topo, diff_link_id );
4926 :
4927 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
4928 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
4929 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
4930 0 : }
4931 :
4932 : static ulong
4933 : populate_allowed_seccomp( fd_topo_t const * topo,
4934 : fd_topo_tile_t const * tile,
4935 : ulong out_cnt,
4936 0 : struct sock_filter * out ) {
4937 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
4938 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
4939 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
4940 0 : populate_sock_filter_policy_fd_replay_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), FD_ACCDB_FD_RW, (uint)ctx->store_disk_fd, FD_STAKE_DELEGATIONS_FD, FD_EPOCH_CREDITS_FD, FD_COST_TRACKER_FD, FD_VOTE_STAKES_FD, (uint)FD_TXNCACHE_FD );
4941 0 : return sock_filter_policy_fd_replay_tile_instr_cnt;
4942 0 : }
4943 :
4944 : static ulong
4945 : populate_allowed_fds( fd_topo_t const * topo,
4946 : fd_topo_tile_t const * tile,
4947 : ulong out_fds_cnt,
4948 0 : int * out_fds ) {
4949 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
4950 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
4951 0 : fd_replay_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_replay_tile_t), sizeof(fd_replay_tile_t) );
4952 0 : if( FD_UNLIKELY( out_fds_cnt<9UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
4953 :
4954 0 : ulong out_cnt = 0UL;
4955 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
4956 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
4957 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
4958 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db */
4959 0 : out_fds[ out_cnt++ ] = FD_STAKE_DELEGATIONS_FD; /* stake delegation disk spill */
4960 0 : out_fds[ out_cnt++ ] = FD_EPOCH_CREDITS_FD; /* epoch credits disk spill */
4961 0 : out_fds[ out_cnt++ ] = FD_COST_TRACKER_FD; /* cost tracker disk spill */
4962 0 : out_fds[ out_cnt++ ] = FD_VOTE_STAKES_FD; /* vote stakes disk spill */
4963 0 : out_fds[ out_cnt++ ] = FD_TXNCACHE_FD; /* transaction cache spill */
4964 0 : if( FD_LIKELY( ctx->store_disk_fd>=0 ) )
4965 0 : out_fds[ out_cnt++ ] = ctx->store_disk_fd;
4966 :
4967 0 : return out_cnt;
4968 0 : }
4969 :
4970 : static inline void
4971 0 : during_housekeeping( fd_replay_tile_t * ctx ) {
4972 0 : wait_info_publish( ctx );
4973 :
4974 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) fd_clock_tile_recal( ctx->clock );
4975 :
4976 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
4977 0 : FD_CHECK_CRIT( ctx->halt_replay, "state machine corruption" );
4978 0 : FD_LOG_DEBUG(( "keyswitch: unhalting replay" ));
4979 0 : ctx->halt_replay = 0;
4980 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
4981 0 : }
4982 :
4983 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
4984 0 : if( FD_LIKELY( !ctx->halt_replay ) ) FD_LOG_DEBUG(( "keyswitch: halting replay" ));
4985 0 : ctx->halt_replay = 1;
4986 0 : if( !ctx->is_leader && ctx->is_booted ) maybe_switch_identity( ctx );
4987 0 : }
4988 0 : }
4989 :
4990 : #undef DEBUG_LOGGING
4991 :
4992 : /* Credits are checked against the out link with the fewest, so the
4993 : burst is the most frags one stem iteration publishes to any one out
4994 : link. after_credit publishes at most 3, from boot (slot completed,
4995 : next leader, root advanced) or try_fini_leader chaining into the
4996 : next leader slot (slot completed, next leader, became leader).
4997 : returnable_frag publishes the most on a completed sigverify task:
4998 : slot dead plus one txn_executed per transaction. Every
4999 : after_credit path that publishes returns nonzero, which skips the
5000 : input poll, except REPLAY_SIG_SLOT_STATES, which adds one to each. */
5001 0 : #define STEM_BURST (fd_ulong_max( 5UL, FD_EXECRP_SIGVERIFY_SIG_PARA+2UL ))
5002 :
5003 : /* Housekeeping interval in ticks, tuned independently of output-link
5004 : depths. */
5005 0 : #define STEM_LAZY ((long)36865)
5006 :
5007 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_replay_tile_t
5008 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_replay_tile_t)
5009 :
5010 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
5011 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
5012 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
5013 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
5014 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
5015 :
5016 : #include "../../disco/stem/fd_stem.c"
5017 :
5018 : static ulong
5019 0 : max_event_sz( fd_topo_tile_t const * tile ) {
5020 : /* replay always emits block_completed; runtime_block (the largest
5021 : runtime event) is added when runtime diffs are on. */
5022 0 : ulong sz = fd_ulong_max( sizeof(fd_event_block_completed_t), sizeof(fd_event_metrics_trigger_t) );
5023 0 : if( tile->replay.report_runtime_diffs && sizeof(fd_event_runtime_block_t)>sz ) sz = sizeof(fd_event_runtime_block_t);
5024 0 : return sz;
5025 0 : }
5026 :
5027 : fd_topo_run_tile_t fd_tile_replay = {
5028 : .name = "replay",
5029 : .max_event_sz = max_event_sz,
5030 : .populate_allowed_seccomp = populate_allowed_seccomp,
5031 : .populate_allowed_fds = populate_allowed_fds,
5032 : .scratch_align = scratch_align,
5033 : .scratch_footprint = scratch_footprint,
5034 : .privileged_init = privileged_init,
5035 : .unprivileged_init = unprivileged_init,
5036 : .run = stem_run,
5037 : };
|