Line data Source code
1 : #include "../tiles.h"
2 :
3 : #include <linux/futex.h>
4 : #include "generated/fd_pack_tile_seccomp.h"
5 :
6 : #include "../../util/pod/fd_pod_format.h"
7 : #include "../../discof/replay/fd_replay_tile.h" // layering violation
8 : #include "../fd_txn_m.h"
9 : #include "../keyguard/fd_keyload.h"
10 : #include "../keyguard/fd_keyswitch.h"
11 : #include "../keyguard/fd_keyguard.h"
12 : #include "../keyguard/fd_keyguard_client.h"
13 : #include "../metrics/fd_metrics.h"
14 : #include "../pack/fd_pack.h"
15 : #include "../pack/fd_pack_cost.h"
16 : #include "../pack/fd_pack_pacing.h"
17 : #include "../fd_clock_tile.h"
18 :
19 : #include <string.h>
20 :
21 : /* fd_pack is responsible for taking verified transactions, and
22 : arranging them into "microblocks" (groups) of transactions to
23 : be executed serially. It can try to do clever things so that
24 : multiple microblocks can execute in parallel, if they don't
25 : write to the same accounts. */
26 :
27 0 : #define IN_KIND_RESOLV (0UL)
28 0 : #define IN_KIND_POH (1UL)
29 0 : #define IN_KIND_EXECLE (2UL)
30 0 : #define IN_KIND_SIGN (3UL)
31 0 : #define IN_KIND_REPLAY (4UL)
32 0 : #define IN_KIND_EXECUTED_TXN (5UL)
33 :
34 : /* Pace microblocks, but only slightly. This helps keep performance
35 : more stable. This limit is 2,000 microblocks/second/execle. At
36 : MAX_TXN_PER_MICROBLOCK transactions/microblock, that's
37 : 2000*MAX_TXN_PER_MICROBLOCK txn/sec/execle. */
38 0 : #define MICROBLOCK_DURATION_NS (0L)
39 :
40 : /* There are 151 accepted blockhashes. The execle tile does the final
41 : check, but making this as precise as possible limits spam. When we
42 : aren't leader, we don't have much information about what replay is
43 : doing, but we still want to be able to expire transactions before we
44 : become leader, so we snoop on the block heights of the recent
45 : blockhashes of transactions coming from resolv. There's a small
46 : possibility that these are on a different fork than the one we become
47 : leader for, so we aren't fully strict until becoming leader. */
48 0 : #define TRANSACTION_LIFETIME_SLOTS 151UL
49 0 : #define TRANSACTION_LIFETIME_SLOTS_NOT_STRICT (151UL+8UL)
50 :
51 : /* Time is normally a long, but pack expects a ulong. Add -LONG_MIN to
52 : the time values so that LONG_MIN maps to 0, LONG_MAX maps to
53 : ULONG_MAX, and everything in between maps linearly with a slope of 1.
54 : Just subtracting LONG_MIN results in signed integer overflow, which
55 : is U.B. */
56 : #define TIME_OFFSET 0x8000000000000000UL
57 : FD_STATIC_ASSERT( (ulong)LONG_MIN+TIME_OFFSET==0UL, time_offset );
58 : FD_STATIC_ASSERT( (ulong)LONG_MAX+TIME_OFFSET==ULONG_MAX, time_offset );
59 :
60 : /* 1.6 M cost units, enough for 1 max size transaction */
61 : const ulong CUS_PER_MICROBLOCK = 1600000UL;
62 :
63 : const float VOTE_FRACTION = 1.0f; /* schedule all available votes first */
64 0 : #define EFFECTIVE_TXN_PER_MICROBLOCK 1UL
65 :
66 :
67 :
68 : /* Sync with src/app/shared/fd_config.c */
69 0 : #define FD_PACK_STRATEGY_PERF 0
70 0 : #define FD_PACK_STRATEGY_BALANCED 1
71 :
72 : static char const * const schedule_strategy_strings[2] = { "PRF", "BAL" };
73 :
74 :
75 : typedef struct {
76 : fd_acct_addr_t commission_pubkey[1];
77 : ulong commission;
78 : } block_builder_info_t;
79 :
80 : typedef struct {
81 : long time;
82 : ulong sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_CNT ];
83 : } fd_pack_sched_results_snap_t;
84 :
85 : typedef struct {
86 : fd_wksp_t * mem;
87 : ulong chunk0;
88 : ulong wmark;
89 : } fd_pack_in_ctx_t;
90 :
91 : typedef struct {
92 : fd_wksp_t * mem;
93 : ulong chunk0;
94 : ulong wmark;
95 : ulong chunk;
96 : ulong out_idx;
97 : } fd_pack_out_ctx_t;
98 :
99 : typedef struct {
100 : fd_pack_t * pack;
101 : fd_txn_e_t * cur_spot;
102 : int is_bundle; /* is the current transaction a bundle */
103 :
104 : uchar executed_txn_sig[ 64UL ];
105 : uchar txn_committed;
106 :
107 : /* One of the FD_PACK_STRATEGY_* values defined above */
108 : int strategy;
109 :
110 : /* The value passed to fd_pack_new, etc. */
111 : ulong max_pending_transactions;
112 :
113 : /* The leader slot we are currently packing for, or ULONG_MAX if we
114 : are not the leader. */
115 : ulong leader_slot;
116 : void const * leader_bank;
117 : ulong leader_bank_idx;
118 : ulong leader_bank_seq;
119 :
120 : /* The block height of the last slot for which we became leader */
121 : ulong block_height;
122 :
123 : fd_became_leader_t _became_leader[1];
124 :
125 : /* The number of microblocks we have packed for the current leader
126 : slot. Will always be <= slot_max_microblocks. We must track
127 : this so that when we are done we can tell the PoH tile how many
128 : microblocks to expect in the slot. */
129 : ulong slot_microblock_cnt;
130 :
131 : /* Counter which increments when we've finished packing for a slot */
132 : uint pack_idx;
133 :
134 : ulong pack_txn_cnt; /* total num transactions packed since startup */
135 :
136 : long slot_pack_start_ns; /* wallclock ns production began for leader_slot */
137 : ulong slot_bundle_txn_cnt; /* bundled txns scheduled into leader_slot */
138 :
139 : /* The maximum number of microblocks that can be packed in this slot.
140 : Provided by the PoH tile when we become leader.*/
141 : ulong slot_max_microblocks;
142 :
143 : /* Cap (in bytes) of the amount of transaction data we produce in each
144 : block to avoid hitting the shred limits. See where this is set for
145 : more explanation. */
146 : ulong slot_max_data;
147 :
148 : /* Consensus critical slot cost limits. */
149 : struct {
150 : ulong slot_max_cost;
151 : ulong slot_max_vote_cost;
152 : ulong slot_max_write_cost_per_acct;
153 : ulong slot_max_allocated_data_per_block;
154 : ulong slot_max_data_shreds;
155 : } limits;
156 : ulong bench_max_shreds_per_block; /* [development.bench], floors the leader's slot_max_data_shreds */
157 :
158 : /* If drain_execle is non-zero, then the pack tile must wait until all
159 : execle are idle before scheduling any more microblocks. This is
160 : primarily helpful in irregular leader transitions, e.g. while being
161 : leader for slot N, we switch forks to a slot M (!=N+1) in which we
162 : are also leader. We don't want to execute microblocks for
163 : different slots concurrently. */
164 : int drain_execle;
165 :
166 : fd_rng_t * rng;
167 :
168 : uint rng_seed;
169 : ulong rng_idx;
170 :
171 : /* The end wallclock time of the leader slot we are currently packing
172 : for, if we are currently packing for a slot.*/
173 : long slot_end_ns;
174 :
175 : /* The current dynamic upper bound on total microblocks for this slot.
176 : Monotonically decreasing over the slot lifetime. */
177 : ulong slot_dynamic_max_microblocks;
178 :
179 : /* PoH mixin positions per tick (0 when PoH has no hash budget) and
180 : tick length for the leader slot, the rate at which PoH can absorb
181 : microblocks. */
182 : ulong slot_mixin_per_tick;
183 : ulong slot_tick_duration_ns;
184 :
185 : /* Set by during_housekeeping when the dynamic bound drops below
186 : slot_max_microblocks. Consumed by after_credit which publishes
187 : the updated bound to POH over the pack_poh link. */
188 : int pending_reduce_mb_bound;
189 :
190 : /* pacer is used for pacing CUs through the slot, i.e. deciding when
191 : to schedule a microblock given the number of CUs that have been
192 : consumed so far. It is an opaque pacing object, initialized when
193 : the pack tile is packing a slot. */
194 : fd_pack_pacing_t pacer[1];
195 :
196 : fd_clock_tile_t clock[1];
197 :
198 : /* last_successful_insert stores the tickcount of the last
199 : successful transaction insert. */
200 : long last_successful_insert;
201 :
202 : /* highest_observed_block_height stores the highest block height we've
203 : seen from any transaction coming from the resolv tile. When this
204 : increases, we expire old transactions. */
205 : ulong highest_observed_block_height;
206 :
207 : /* microblock_duration_ns scaled to be in ticks instead of nanoseconds */
208 : ulong microblock_duration_ticks;
209 :
210 : fd_pack_in_ctx_t in[ 32 ];
211 : int in_kind[ 32 ];
212 :
213 : ulong execle_cnt;
214 : ulong execle_idle_bitset; /* bit i is 1 if we've observed *execle_current[i]==execle_expect[i] */
215 : int poll_cursor; /* in [0, execle_cnt), the next execle to poll */
216 : int use_consumed_cus;
217 : long skip_cnt;
218 : ulong * execle_current[ FD_PACK_MAX_EXECLE_TILES ];
219 : ulong execle_expect[ FD_PACK_MAX_EXECLE_TILES ];
220 : /* execle_ready_at[x] means don't check execle x until tickcount is at
221 : least execle_ready_at[x]. */
222 : long execle_ready_at[ FD_PACK_MAX_EXECLE_TILES ];
223 :
224 : fd_pack_out_ctx_t execle_out[ FD_PACK_MAX_EXECLE_TILES ];
225 : fd_pack_out_ctx_t poh_out;
226 :
227 : ulong insert_result[ FD_PACK_INSERT_RETVAL_CNT ];
228 : fd_histf_t schedule_duration[ 1 ];
229 : fd_histf_t no_sched_duration[ 1 ];
230 : fd_histf_t insert_duration [ 1 ];
231 : fd_histf_t complete_duration[ 1 ];
232 :
233 : struct {
234 : uint metric_state;
235 : long metric_state_begin;
236 : long metric_timing[ 16 ];
237 : };
238 :
239 : /* last_sched_metrics is a snapshot of the schedule outcome counters
240 : during the last schedule which included at least one successful
241 : outcome. */
242 : fd_pack_sched_results_snap_t last_sched_metrics[ 1 ];
243 :
244 : /* last_sched_metrics is a snapshot of the schedule outcome counters
245 : at the last start-of-leader-block event. */
246 : fd_pack_sched_results_snap_t start_block_sched_metrics[ 1 ];
247 :
248 : struct {
249 : ulong id;
250 : ulong txn_cnt;
251 : ulong txn_received;
252 : ulong min_blockhash_height;
253 : fd_txn_e_t * _txn[ FD_PACK_MAX_TXN_PER_BUNDLE ];
254 : fd_txn_e_t * const * bundle; /* points to _txn when non-NULL */
255 : } current_bundle[1];
256 :
257 : block_builder_info_t blk_engine_cfg[1];
258 :
259 : struct {
260 : int enabled;
261 : int ib_inserted; /* in this slot */
262 : fd_acct_addr_t vote_pubkey[1];
263 : fd_acct_addr_t identity_pubkey[1];
264 : fd_bundle_crank_gen_t gen[1];
265 : fd_acct_addr_t tip_receiver_owner[1];
266 : ulong epoch;
267 : fd_bundle_crank_tip_payment_config_t prev_config[1]; /* as of start of slot, then updated */
268 : uchar recent_blockhash[32];
269 : fd_ed25519_sig_t last_sig[1];
270 :
271 : fd_keyswitch_t * keyswitch;
272 : fd_keyguard_client_t keyguard_client[1];
273 :
274 : ulong metrics[4];
275 : } crank[1];
276 :
277 :
278 : /* Used between during_frag and after_frag */
279 : ulong pending_rebate_sz;
280 : union{ fd_pack_rebate_t rebate[1]; uchar footprint[USHORT_MAX]; } rebate[1];
281 : } fd_pack_ctx_t;
282 :
283 0 : #define BUNDLE_META_SZ 40UL
284 : FD_STATIC_ASSERT( sizeof(block_builder_info_t)==BUNDLE_META_SZ, blk_engine_cfg );
285 :
286 0 : #define FD_PACK_METRIC_STATE_TRANSACTIONS 0
287 0 : #define FD_PACK_METRIC_STATE_EXECLES 1
288 0 : #define FD_PACK_METRIC_STATE_LEADER 2
289 0 : #define FD_PACK_METRIC_STATE_MICROBLOCKS 3
290 :
291 : /* Updates one component of the metric state. If the state has changed,
292 : records the change. */
293 : static inline void
294 : update_metric_state( fd_pack_ctx_t * ctx,
295 : long effective_as_of,
296 : int type,
297 0 : int status ) {
298 0 : uint current_state = fd_uint_insert_bit( ctx->metric_state, type, status );
299 0 : if( FD_UNLIKELY( current_state!=ctx->metric_state ) ) {
300 0 : ctx->metric_timing[ ctx->metric_state ] += effective_as_of - ctx->metric_state_begin;
301 0 : ctx->metric_state_begin = effective_as_of;
302 0 : ctx->metric_state = current_state;
303 0 : }
304 0 : }
305 :
306 : static inline void
307 0 : remove_ib( fd_pack_ctx_t * ctx ) {
308 : /* It's likely the initializer bundle is long scheduled, but we want to
309 : try deleting it just in case. */
310 0 : if( FD_UNLIKELY( ctx->crank->enabled & ctx->crank->ib_inserted ) ) {
311 0 : ulong deleted = fd_pack_delete_transaction( ctx->pack, (fd_ed25519_sig_t const *)ctx->crank->last_sig );
312 0 : FD_MCNT_INC( PACK, TXN_EVICTED, deleted );
313 0 : }
314 0 : ctx->crank->ib_inserted = 0;
315 0 : }
316 :
317 :
318 : FD_FN_CONST static inline ulong
319 0 : scratch_align( void ) {
320 0 : return 4096UL;
321 0 : }
322 :
323 : FD_FN_PURE static inline ulong
324 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
325 0 : fd_pack_limits_t limits[1] = {{
326 0 : .max_cost_per_block = tile->pack.max_cost_per_block,
327 0 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
328 0 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
329 0 : .max_data_bytes_per_block = FD_PACK_MAX_DATA_PER_BLOCK*(tile->pack.max_shreds_per_block/FD_SHRED_BLK_MAX),
330 0 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
331 0 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
332 0 : .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
333 0 : }};
334 :
335 0 : ulong l = FD_LAYOUT_INIT;
336 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
337 0 : l = FD_LAYOUT_APPEND( l, fd_rng_align(), fd_rng_footprint() );
338 0 : l = FD_LAYOUT_APPEND( l, fd_pack_align(), fd_pack_footprint( tile->pack.max_pending_transactions,
339 0 : BUNDLE_META_SZ,
340 0 : tile->pack.execle_tile_count,
341 0 : limits ) );
342 0 : return FD_LAYOUT_FINI( l, scratch_align() );
343 0 : }
344 :
345 : static inline void
346 : log_end_block_metrics( fd_pack_ctx_t * ctx,
347 : long now,
348 : char const * reason,
349 0 : ulong cus_consumed_in_block ) {
350 0 : #define DELTA( m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TXN_SCHEDULE_ATTEMPTED_##m) ] - ctx->last_sched_metrics->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_##m##_IDX ])
351 0 : #define AVAIL( m ) (fd_metrics_tl[ MIDX(GAUGE, PACK, TXN_PENDING_USED_##m) ])
352 0 : FD_LOG_INFO(( "pack_end_block(slot=%lu,%s,%lx,ticks_since_last_schedule=%ld,reasons=%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu;remaining=%lu+%lu+%lu+%lu;smallest=%lu;cus=%lu->%lu)",
353 0 : ctx->leader_slot, reason, ctx->execle_idle_bitset, now-ctx->last_sched_metrics->time,
354 0 : DELTA( TAKEN ), DELTA( CU_LIMIT ), DELTA( CONFLICT_FAST ), DELTA( BYTE_LIMIT ), DELTA( ALLOC_LIMIT ), DELTA( WRITE_COST ), DELTA( CONFLICT_SLOW ), DELTA( WRITE_COST_DEFERRED ),
355 0 : AVAIL(REGULAR), AVAIL(VOTE), AVAIL(BUNDLE), AVAIL(CONFLICTING),
356 0 : (fd_metrics_tl[ MIDX(GAUGE, PACK, TXN_PENDING_SMALLEST_COST) ]),
357 0 : (cus_consumed_in_block),
358 0 : (fd_metrics_tl[ MIDX(GAUGE, PACK, BLOCK_COST) ])
359 0 : ));
360 0 : #undef AVAIL
361 0 : #undef DELTA
362 0 : }
363 :
364 : static inline void
365 0 : get_done_packing( fd_pack_ctx_t * ctx, fd_done_packing_t * done_packing, int reason ) {
366 0 : fd_pack_metrics_write( ctx->pack );
367 :
368 0 : done_packing->microblocks_in_slot = ctx->slot_microblock_cnt;
369 0 : done_packing->end_slot_reason = reason;
370 0 : fd_pack_get_block_limits( ctx->pack, done_packing->limits_usage, done_packing->limits );
371 :
372 0 : #define DELTA( mem, m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TXN_SCHEDULE_ATTEMPTED_##m) ] - ctx->mem->sched_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_##m##_IDX ])
373 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_TAKEN_IDX ] = DELTA( start_block_sched_metrics, TAKEN );
374 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CU_LIMIT_IDX ] = DELTA( start_block_sched_metrics, CU_LIMIT );
375 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CONFLICT_FAST_IDX ] = DELTA( start_block_sched_metrics, CONFLICT_FAST );
376 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_BYTE_LIMIT_IDX ] = DELTA( start_block_sched_metrics, BYTE_LIMIT );
377 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_ALLOC_LIMIT_IDX ] = DELTA( start_block_sched_metrics, ALLOC_LIMIT );
378 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_WRITE_COST_IDX ] = DELTA( start_block_sched_metrics, WRITE_COST );
379 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CONFLICT_SLOW_IDX ] = DELTA( start_block_sched_metrics, CONFLICT_SLOW );
380 0 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_WRITE_COST_DEFERRED_IDX ] = DELTA( start_block_sched_metrics, WRITE_COST_DEFERRED );
381 :
382 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_TAKEN_IDX ] = DELTA( last_sched_metrics, TAKEN );
383 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CU_LIMIT_IDX ] = DELTA( last_sched_metrics, CU_LIMIT );
384 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CONFLICT_FAST_IDX ] = DELTA( last_sched_metrics, CONFLICT_FAST );
385 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_BYTE_LIMIT_IDX ] = DELTA( last_sched_metrics, BYTE_LIMIT );
386 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_ALLOC_LIMIT_IDX ] = DELTA( last_sched_metrics, ALLOC_LIMIT );
387 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_WRITE_COST_IDX ] = DELTA( last_sched_metrics, WRITE_COST );
388 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_CONFLICT_SLOW_IDX ] = DELTA( last_sched_metrics, CONFLICT_SLOW );
389 0 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_RESULT_V_WRITE_COST_DEFERRED_IDX ] = DELTA( last_sched_metrics, WRITE_COST_DEFERRED );
390 0 : #undef DELTA
391 :
392 0 : fd_pack_get_pending_smallest( ctx->pack, done_packing->pending_smallest, done_packing->pending_votes_smallest );
393 :
394 0 : done_packing->bundle_txn_count = ctx->slot_bundle_txn_cnt;
395 0 : done_packing->pack_start_ns = ctx->slot_pack_start_ns;
396 0 : done_packing->pack_end_ns = fd_clock_tile_now( ctx->clock );
397 0 : }
398 :
399 : static inline void
400 0 : metrics_write( fd_pack_ctx_t * ctx ) {
401 0 : FD_MCNT_ENUM_COPY( PACK, TXN_INSERT_ATTEMPTED, ctx->insert_result );
402 0 : FD_MCNT_ENUM_COPY( PACK, SCHEDULE_REGIME_DURATION_NANOS, ((ulong*)ctx->metric_timing) );
403 0 : FD_MCNT_ENUM_COPY( PACK, BUNDLE_CRANK_ATTEMPTED, ctx->crank->metrics );
404 0 : FD_MHIST_COPY( PACK, MICROBLOCK_SCHEDULE_DURATION_NANOS, ctx->schedule_duration );
405 0 : FD_MHIST_COPY( PACK, MICROBLOCK_SCHEDULE_EMPTY_DURATION_NANOS, ctx->no_sched_duration );
406 0 : FD_MHIST_COPY( PACK, TXN_INSERT_DURATION_NANOS, ctx->insert_duration );
407 0 : FD_MHIST_COPY( PACK, MICROBLOCK_COMPLETE_DURATION_NANOS, ctx->complete_duration );
408 :
409 0 : fd_pack_metrics_write( ctx->pack );
410 0 : }
411 :
412 : /* compute_dynamic_max_microblocks: Computes the upper bound on total
413 : microblocks based on remaining time, bank count and PoH hash rate.
414 :
415 : The basic idea here is that if there is 1ms left in the slot, we
416 : don't expect to schedule 130k microblocks. We can reduce our
417 : remaining budget which allows POH to advance hashing a bit further
418 : and avoid having to do a lot of hashing after the slot ends.
419 :
420 : The fastest we can execute a transaction is about 1us. With n
421 : execle tiles, that means we can execute at most n txn/us. If we have
422 : k ms left in the block, only reserve up to k*n*1000 microblocks.
423 :
424 : PoH mixes each microblock into one hash position and has
425 : hashcnt_per_tick-1 of them per tick, so with k ns left it can absorb
426 : at most k*(hashcnt_per_tick-1)/tick_duration_ns more microblocks.
427 : Bounding by that keeps PoH on its wall clock schedule however large
428 : the slot maximum is: the reserve tracks the time left instead of
429 : piling up as a hash wall at the end of the slot. */
430 :
431 : static inline ulong
432 0 : compute_dynamic_max_microblocks( fd_pack_ctx_t * ctx ) {
433 0 : long now = fd_clock_tile_now( ctx->clock );
434 0 : long end = ctx->slot_end_ns;
435 :
436 : /* If the slot has ended, don't reserve any more microblocks. */
437 0 : if( FD_UNLIKELY( now>=end ) ) return ctx->slot_microblock_cnt;
438 :
439 : /* remaining_ns * n / 1000 = (remaining_ns/1e6 ms) * n * 1000.
440 :
441 : Overflow: remaining_ns is at most ~4e8, n at most 64 and
442 : mixin_per_tick at most 62,499 (FD_RUNTIME_MAX_HASHES_PER_TICK), so
443 : the products are < 3e13. */
444 :
445 0 : ulong remaining_ns = (ulong)(end - now);
446 0 : ulong n = ctx->execle_cnt;
447 0 : ulong cnt = ctx->slot_microblock_cnt;
448 0 : ulong R = ctx->slot_max_microblocks - cnt;
449 0 : ulong can_execute = remaining_ns * n / 1000UL;
450 0 : ulong can_mixin = fd_ulong_if( !!ctx->slot_mixin_per_tick, remaining_ns * ctx->slot_mixin_per_tick / ctx->slot_tick_duration_ns, ULONG_MAX );
451 :
452 0 : return cnt + fd_ulong_min( R, fd_ulong_min( can_execute, can_mixin ) );
453 0 : }
454 :
455 : static inline void
456 0 : during_housekeeping( fd_pack_ctx_t * ctx ) {
457 0 : if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->clock ) ) ) {
458 0 : fd_clock_tile_recal( ctx->clock );
459 0 : }
460 :
461 0 : if( FD_UNLIKELY( ctx->crank->enabled && fd_keyswitch_state_query( ctx->crank->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
462 0 : fd_memcpy( ctx->crank->identity_pubkey, ctx->crank->keyswitch->bytes, 32UL );
463 0 : fd_keyswitch_state( ctx->crank->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
464 0 : }
465 :
466 0 : if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
467 0 : ulong raw = compute_dynamic_max_microblocks( ctx );
468 0 : ulong prev = ctx->slot_dynamic_max_microblocks;
469 :
470 : /* Enforce monotonically decreasing. This ensures pack's bound is
471 : always smaller than POH's. */
472 0 : ctx->slot_dynamic_max_microblocks = fd_ulong_min( raw, prev );
473 :
474 : /* If the bound decreased, we must update pack's internal scheduling
475 : limit. Otherwise, pack could schedule a microblock that exceeds
476 : the remaining capacity from the tile's and poh's perspectives
477 : (e.g. pack might produce a 5-transaction microblock when only 4
478 : microblocks worth of space remain). */
479 0 : if( FD_UNLIKELY( ctx->slot_dynamic_max_microblocks < prev ) ) {
480 0 : fd_pack_limits_t limits[1];
481 0 : limits->max_cost_per_block = ctx->limits.slot_max_cost;
482 0 : limits->max_data_bytes_per_block = ctx->slot_max_data;
483 0 : limits->max_microblocks_per_block = ctx->slot_dynamic_max_microblocks;
484 0 : limits->max_vote_cost_per_block = ctx->limits.slot_max_vote_cost;
485 0 : limits->max_write_cost_per_acct = ctx->limits.slot_max_write_cost_per_acct;
486 0 : limits->max_txn_per_microblock = ULONG_MAX; /* unused */
487 0 : limits->max_allocated_data_per_block = ctx->limits.slot_max_allocated_data_per_block;
488 0 : fd_pack_set_block_limits( ctx->pack, limits );
489 :
490 0 : ctx->pending_reduce_mb_bound = 1; /* publish bound decrease */
491 0 : }
492 0 : }
493 0 : }
494 :
495 : static inline void
496 : before_credit( fd_pack_ctx_t * ctx,
497 : fd_stem_context_t * stem,
498 0 : int * charge_busy ) {
499 0 : (void)stem;
500 :
501 0 : if( FD_UNLIKELY( (ctx->cur_spot!=NULL) & !ctx->is_bundle ) ) {
502 0 : *charge_busy = 1;
503 :
504 : /* If we were overrun while processing a frag from an in, then
505 : cur_spot is left dangling and not cleaned up, so clean it up here
506 : (by returning the slot to the pool of free slots). If the last
507 : transaction was a bundle, then we don't want to return it. When
508 : we try to process the first transaction in the next bundle, we'll
509 : see we never got the full bundle and cancel the whole last
510 : bundle, returning all the storage to the pool. */
511 0 : fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
512 0 : ctx->cur_spot = NULL;
513 0 : }
514 0 : }
515 :
516 : static inline int
517 0 : prevent_park( fd_pack_ctx_t * ctx ) {
518 : /* skip_cnt counts loop iterations, not time, never park through it */
519 0 : return ctx->skip_cnt>0L;
520 0 : }
521 :
522 : static inline long
523 0 : next_deadline( fd_pack_ctx_t * ctx ) {
524 0 : if( FD_LIKELY( ctx->leader_slot==ULONG_MAX ) ) return LONG_MAX;
525 0 : long now = fd_tickcount();
526 0 : long slot_end = fd_clock_tile_wallclock_to_tickcount( ctx->clock, ctx->slot_end_ns );
527 0 : ulong enabled = fd_ulong_min( fd_pack_pacing_enabled_bank_cnt( ctx->pacer, now ), ctx->execle_cnt );
528 0 : if( FD_UNLIKELY( enabled>=ctx->execle_cnt ) ) return slot_end;
529 0 : return fd_long_min( slot_end, fd_pack_pacing_next_enable( ctx->pacer, enabled ) );
530 0 : }
531 :
532 : static inline void
533 : after_credit( fd_pack_ctx_t * ctx,
534 : fd_stem_context_t * stem,
535 : int * opt_poll_in FD_PARAM_UNUSED,
536 0 : int * charge_busy ) {
537 0 : if( FD_UNLIKELY( (ctx->skip_cnt--)>0L ) ) return; /* It would take ages for this to hit LONG_MIN */
538 :
539 0 : long now = fd_tickcount();
540 :
541 0 : ulong execle_cnt = ctx->execle_cnt;
542 :
543 0 : int pacing_execle_cnt = (int)fd_ulong_min( fd_pack_pacing_enabled_bank_cnt( ctx->pacer, now ), execle_cnt );
544 :
545 : /* If any execle are busy, check one of the busy ones see if it is
546 : still busy. */
547 0 : if( FD_LIKELY( ctx->execle_idle_bitset!=fd_ulong_mask_lsb( (int)execle_cnt ) ) ) {
548 0 : int poll_cursor = ctx->poll_cursor;
549 0 : ulong busy_bitset = (~ctx->execle_idle_bitset) & fd_ulong_mask_lsb( (int)execle_cnt );
550 :
551 : /* Suppose execle_cnt is 4 and idle_bitset looks something like this
552 : (pretending it's a uchar):
553 : 0000 1001
554 : ^ busy cursor is 1
555 : Then busy_bitset is
556 : 0000 0110
557 : Rotate it right by 2 bits
558 : 1000 0001
559 : Find lsb returns 0, so busy cursor remains 2, and we poll
560 : execle 2.
561 :
562 : If instead idle_bitset were
563 : 0000 1110
564 : ^
565 : The rotated version would be
566 : 0100 0000
567 : Find lsb will return 6, so busy cursor would be set to 0, and
568 : we'd poll execle 0, which is the right one. */
569 0 : poll_cursor++;
570 0 : poll_cursor = (poll_cursor + fd_ulong_find_lsb( fd_ulong_rotate_right( busy_bitset, (poll_cursor&63) ) )) & 63;
571 :
572 0 : if( FD_UNLIKELY(
573 : /* if microblock duration is 0, bypass the execle_ready_at check
574 : to avoid a potential cache miss. Can't use an ifdef here
575 : because FD_UNLIKELY is a macro, but the compiler should
576 : eliminate the check easily. */
577 0 : ( (MICROBLOCK_DURATION_NS==0L) || (ctx->execle_ready_at[poll_cursor]<now) ) &&
578 0 : (fd_fseq_query( ctx->execle_current[poll_cursor] )==ctx->execle_expect[poll_cursor]) ) ) {
579 0 : *charge_busy = 1;
580 0 : ctx->execle_idle_bitset |= 1UL<<poll_cursor;
581 :
582 0 : long complete_duration = -fd_tickcount();
583 0 : int completed = fd_pack_microblock_complete( ctx->pack, (ulong)poll_cursor );
584 0 : complete_duration += fd_tickcount();
585 0 : if( FD_LIKELY( completed ) ) fd_histf_sample( ctx->complete_duration, (ulong)complete_duration );
586 0 : }
587 :
588 0 : ctx->poll_cursor = poll_cursor;
589 0 : }
590 :
591 :
592 : /* If we time out on our slot, then stop being leader. */
593 0 : if( FD_UNLIKELY( ctx->leader_slot!=ULONG_MAX &&
594 0 : fd_clock_tile_tickcount_to_wallclock( ctx->clock, now )>=ctx->slot_end_ns ) ) {
595 0 : *charge_busy = 1;
596 :
597 0 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out.mem, ctx->poh_out.chunk );
598 0 : get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_TIME ); /* needs to be called before fd_pack_end_block */
599 0 : log_end_block_metrics( ctx, now, "time", done_packing->limits_usage->block_cost ); /* reads gauges fd_pack_end_block resets */
600 0 : fd_pack_end_block( ctx->pack );
601 0 : fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers ); /* needs to be called after fd_pack_end_block */
602 :
603 0 : metrics_write( ctx );
604 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out.chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
605 0 : ctx->poh_out.chunk = fd_dcache_compact_next( ctx->poh_out.chunk, sizeof(fd_done_packing_t), ctx->poh_out.chunk0, ctx->poh_out.wmark );
606 0 : ctx->pack_idx++;
607 :
608 0 : ctx->drain_execle = 1;
609 0 : ctx->leader_slot = ULONG_MAX;
610 0 : ctx->slot_microblock_cnt = 0UL;
611 0 : remove_ib( ctx );
612 :
613 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER, 0 );
614 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES, 0 );
615 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, 0 );
616 0 : return;
617 0 : }
618 :
619 : /* Am I in drain mode? If so, check if I can exit it */
620 0 : if( FD_UNLIKELY( ctx->drain_execle ) ) {
621 0 : if( FD_LIKELY( ctx->execle_idle_bitset==fd_ulong_mask_lsb( (int)execle_cnt ) ) ) {
622 0 : ctx->drain_execle = 0;
623 :
624 : /* Pack notifies poh when execle are drained so that poh can
625 : relinquish pack's ownership over the slot execle (by decrementing
626 : its Arc). We do this by sending a FD_PACK_MSG_DONE_DRAINING
627 : sig over the pack_poh mcache.
628 :
629 : TODO: This is only needed for Frankendancer, not Firedancer,
630 : which manages bank lifetime different. */
631 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, FD_PACK_MSG_DONE_DRAINING, 0UL, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
632 0 : *charge_busy = 1;
633 0 : return;
634 0 : } else {
635 0 : return;
636 0 : }
637 0 : }
638 :
639 : /* Am I leader? */
640 0 : if( FD_UNLIKELY( ctx->leader_slot==ULONG_MAX ) ) {
641 0 : return;
642 0 : }
643 :
644 0 : if( FD_UNLIKELY( ctx->pending_reduce_mb_bound ) ) {
645 0 : ctx->pending_reduce_mb_bound = 0;
646 0 : ulong * dst = fd_chunk_to_laddr( ctx->poh_out.mem, ctx->poh_out.chunk );
647 0 : *dst = ctx->slot_dynamic_max_microblocks;
648 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, FD_PACK_MSG_REDUCE_MB_BOUND, ctx->poh_out.chunk, sizeof(ulong), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
649 0 : ctx->poh_out.chunk = fd_dcache_compact_next( ctx->poh_out.chunk, sizeof(ulong), ctx->poh_out.chunk0, ctx->poh_out.wmark );
650 0 : *charge_busy = 1;
651 0 : return;
652 0 : }
653 :
654 : /* Have I sent the max allowed microblocks? Nothing to do. */
655 0 : if( FD_UNLIKELY( ctx->slot_microblock_cnt>=ctx->slot_dynamic_max_microblocks ) ) return;
656 :
657 0 : int any_ready = 0;
658 0 : int any_scheduled = 0;
659 :
660 0 : *charge_busy = 1;
661 :
662 0 : if( FD_LIKELY( ctx->crank->enabled ) ) {
663 0 : block_builder_info_t const * top_meta = fd_pack_peek_bundle_meta( ctx->pack );
664 0 : if( FD_UNLIKELY( top_meta ) ) {
665 : /* Have bundles, in a reasonable state to crank. */
666 :
667 0 : fd_txn_e_t * _bundle[ 1UL ];
668 0 : fd_txn_e_t * const * bundle = fd_pack_insert_bundle_init( ctx->pack, _bundle, 1UL );
669 :
670 0 : ulong txn_sz = fd_bundle_crank_generate( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
671 0 : ctx->crank->identity_pubkey, ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission,
672 0 : bundle[0]->txnp->payload, TXN( bundle[0]->txnp ) );
673 :
674 0 : if( FD_LIKELY( txn_sz==0UL ) ) { /* Everything in good shape! */
675 0 : fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
676 0 : fd_pack_set_initializer_bundles_ready( ctx->pack );
677 0 : ctx->crank->metrics[ 0 ]++; /* BUNDLE_CRANK_ATTEMPTED_UP_TO_DATE */
678 0 : }
679 0 : else if( FD_LIKELY( txn_sz<ULONG_MAX ) ) {
680 0 : bundle[0]->txnp->payload_sz = (ushort)txn_sz;
681 0 : bundle[0]->txnp->source_tpu = FD_TXN_M_TPU_SOURCE_BUNDLE;
682 0 : bundle[0]->txnp->source_ipv4 = 0; /* not applicable */
683 0 : bundle[0]->txnp->scheduler_arrival_time_nanos = fd_clock_tile_now( ctx->clock );
684 0 : bundle[0]->txnp->first_seen_nanos = bundle[0]->txnp->scheduler_arrival_time_nanos;
685 0 : memcpy( bundle[0]->txnp->payload+TXN(bundle[0]->txnp)->recent_blockhash_off, ctx->crank->recent_blockhash, 32UL );
686 :
687 0 : fd_keyguard_client_sign( ctx->crank->keyguard_client, bundle[0]->txnp->payload+1UL,
688 0 : bundle[0]->txnp->payload+65UL, txn_sz-65UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
689 :
690 0 : memcpy( ctx->crank->last_sig, bundle[0]->txnp->payload+1UL, 64UL );
691 :
692 0 : ctx->crank->ib_inserted = 1;
693 0 : ulong deleted;
694 0 : int retval = fd_pack_insert_bundle_fini( ctx->pack, bundle, 1UL, ctx->block_height-1UL, 1, NULL, &deleted );
695 0 : FD_MCNT_INC( PACK, TXN_EVICTED, deleted );
696 0 : ctx->insert_result[ retval + FD_PACK_INSERT_RETVAL_OFF ]++;
697 0 : if( FD_UNLIKELY( retval<0 ) ) {
698 0 : ctx->crank->metrics[ 3 ]++; /* BUNDLE_CRANK_ATTEMPTED_INSERTION_FAILED */
699 0 : FD_LOG_WARNING(( "inserting initializer bundle returned %i", retval ));
700 0 : } else {
701 : /* Update the cached copy of the on-chain state. This seems a
702 : little dangerous, since we're updating it as if the bundle
703 : succeeded without knowing if that's true, but here's why
704 : it's safe:
705 :
706 : From now until we get the rebate call for this initializer
707 : bundle (which lets us know if it succeeded or failed), pack
708 : will be in [Pending] state, which means peek_bundle_meta
709 : will return NULL, so we won't read this state.
710 :
711 : Then, if the initializer bundle failed, we'll go into
712 : [Failed] IB state until the end of the block, which will
713 : cause top_meta to remain NULL so we don't read these values
714 : again.
715 :
716 : Otherwise, the initializer bundle succeeded, which means
717 : that these are the right values to use. */
718 0 : fd_bundle_crank_apply( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
719 0 : ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission );
720 0 : ctx->crank->metrics[ 1 ]++; /* BUNDLE_CRANK_ATTEMPTED_INSERTED */
721 0 : }
722 0 : } else {
723 : /* Already logged a warning in this case */
724 0 : fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
725 0 : ctx->crank->metrics[ 2 ]++; /* BUNDLE_CRANK_ATTEMPTED_CREATION_FAILED */
726 0 : }
727 0 : }
728 0 : }
729 :
730 : /* Try to schedule the next microblock. */
731 0 : if( FD_LIKELY( ctx->execle_idle_bitset ) ) { /* Optimize for schedule */
732 0 : any_ready = 1;
733 :
734 0 : int i = fd_ulong_find_lsb( ctx->execle_idle_bitset );
735 :
736 0 : int flags;
737 :
738 0 : switch( ctx->strategy ) {
739 0 : default:
740 0 : case FD_PACK_STRATEGY_PERF:
741 0 : flags = FD_PACK_SCHEDULE_VOTE | FD_PACK_SCHEDULE_BUNDLE | FD_PACK_SCHEDULE_TXN;
742 0 : break;
743 0 : case FD_PACK_STRATEGY_BALANCED:
744 : /* We want to exempt votes from pacing, so we always allow
745 : scheduling votes. It doesn't really make much sense to pace
746 : bundles, because they get scheduled in FIFO order. However,
747 : we keep pacing for normal transactions. For example, if
748 : pacing_execle_cnt is 0, then pack won't schedule normal
749 : transactions to any execle tile. */
750 0 : flags = FD_PACK_SCHEDULE_VOTE | fd_int_if( i==0, FD_PACK_SCHEDULE_BUNDLE, 0 )
751 0 : | fd_int_if( i<pacing_execle_cnt, FD_PACK_SCHEDULE_TXN, 0 );
752 0 : break;
753 0 : }
754 :
755 0 : fd_pack_out_ctx_t * execle_out = &ctx->execle_out[ i ];
756 0 : fd_txn_e_t * microblock_dst = fd_chunk_to_laddr( execle_out->mem, execle_out->chunk );
757 0 : long schedule_duration = -fd_tickcount();
758 0 : ulong schedule_cnt = fd_pack_schedule_next_microblock( ctx->pack, CUS_PER_MICROBLOCK, VOTE_FRACTION, (ulong)i, flags, microblock_dst );
759 0 : schedule_duration += fd_tickcount();
760 0 : fd_histf_sample( (schedule_cnt>0UL) ? ctx->schedule_duration : ctx->no_sched_duration, (ulong)schedule_duration );
761 :
762 0 : if( FD_LIKELY( schedule_cnt ) ) {
763 0 : any_scheduled = 1;
764 0 : long now2 = fd_tickcount();
765 0 : ulong tsorig = (ulong)fd_frag_meta_ts_comp( now ); /* A bound on when we observed execle was idle */
766 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( now2 );
767 0 : ulong chunk = execle_out->chunk;
768 0 : ulong msg_sz = schedule_cnt*sizeof(fd_txn_e_t);
769 0 : fd_microblock_execle_trailer_t * trailer = (fd_microblock_execle_trailer_t*)(microblock_dst+schedule_cnt);
770 0 : trailer->bank = ctx->leader_bank;
771 0 : trailer->bank_idx = ctx->leader_bank_idx;
772 0 : trailer->bank_seq = ctx->leader_bank_seq;
773 0 : trailer->microblock_idx = ctx->slot_microblock_cnt;
774 0 : trailer->pack_idx = ctx->pack_idx;
775 0 : trailer->pack_txn_idx = ctx->pack_txn_cnt;
776 0 : trailer->is_bundle = !!(microblock_dst->txnp->flags & FD_TXN_P_FLAGS_BUNDLE);
777 :
778 : /* When sending MAX_TXN_PER_MICROBLOCK transactions as fd_txn_e_t
779 : to execle, there must be room for the trailer at the end. */
780 0 : FD_STATIC_ASSERT( MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_e_t)+sizeof(fd_microblock_execle_trailer_t)<=MAX_MICROBLOCK_SZ, pack_execle_mtu );
781 :
782 0 : ulong sig = fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_MICROBLOCK, (ulong)i );
783 0 : ctx->execle_expect[ i ] = fd_stem_publish( stem, execle_out->out_idx, sig, chunk, msg_sz+sizeof(fd_microblock_execle_trailer_t), 0UL, tsorig, tspub );
784 0 : ctx->execle_ready_at[i] = now2 + (long)ctx->microblock_duration_ticks;
785 0 : execle_out->chunk = fd_dcache_compact_next( execle_out->chunk, msg_sz+sizeof(fd_microblock_execle_trailer_t), execle_out->chunk0, execle_out->wmark );
786 0 : ctx->slot_microblock_cnt += fd_ulong_if( trailer->is_bundle, schedule_cnt, 1UL );
787 0 : ctx->pack_idx += fd_uint_if( trailer->is_bundle, (uint)schedule_cnt, 1U );
788 0 : ctx->pack_txn_cnt += schedule_cnt;
789 0 : ctx->slot_bundle_txn_cnt += fd_ulong_if( trailer->is_bundle, schedule_cnt, 0UL );
790 :
791 0 : ctx->execle_idle_bitset = fd_ulong_pop_lsb( ctx->execle_idle_bitset );
792 0 : ctx->skip_cnt = (long)schedule_cnt * fd_long_if( ctx->use_consumed_cus, (long)execle_cnt/2L, 1L );
793 0 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now2 );
794 :
795 0 : ctx->last_sched_metrics->time = now2;
796 0 : fd_pack_get_sched_metrics( ctx->pack, ctx->last_sched_metrics->sched_results );
797 :
798 : /* If we're using CU rebates, then we have one in for each execle
799 : in addition to the two normal ones. We want to skip schedule
800 : attempts for (execle_cnt + 1) link polls after a successful
801 : schedule attempt. */
802 0 : fd_long_store_if( ctx->use_consumed_cus, &(ctx->skip_cnt), (long)(ctx->execle_cnt + 1) );
803 0 : }
804 0 : }
805 :
806 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES, any_ready );
807 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, any_scheduled );
808 0 : now = fd_tickcount();
809 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
810 :
811 : /* Did we send the maximum allowed microblocks? Then end the slot. */
812 0 : if( FD_UNLIKELY( ctx->slot_microblock_cnt==ctx->slot_dynamic_max_microblocks )) {
813 0 : *charge_busy = 1;
814 :
815 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER, 0 );
816 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES, 0 );
817 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, 0 );
818 : /* The pack object also does this accounting and increases this
819 : metric, but we end the slot early so won't see it unless we also
820 : increment it here. */
821 0 : FD_MCNT_INC( PACK, BLOCK_MICROBLOCK_LIMIT_REACHED, 1UL );
822 :
823 0 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out.mem, ctx->poh_out.chunk );
824 0 : get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_MICROBLOCK );
825 0 : log_end_block_metrics( ctx, now, "microblock", done_packing->limits_usage->block_cost );
826 0 : fd_pack_end_block( ctx->pack );
827 0 : fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers );
828 :
829 0 : metrics_write( ctx ); /* final for the slot before poh ends it */
830 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out.chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
831 0 : ctx->poh_out.chunk = fd_dcache_compact_next( ctx->poh_out.chunk, sizeof(fd_done_packing_t), ctx->poh_out.chunk0, ctx->poh_out.wmark );
832 0 : ctx->pack_idx++;
833 :
834 0 : ctx->drain_execle = 1;
835 0 : ctx->leader_slot = ULONG_MAX;
836 0 : ctx->slot_microblock_cnt = 0UL;
837 0 : remove_ib( ctx );
838 :
839 0 : return;
840 0 : }
841 0 : }
842 :
843 :
844 : /* At this point, we have started receiving frag seq with details in
845 : mline at time now. Speculatively process it here. */
846 :
847 : static inline void
848 : during_frag( fd_pack_ctx_t * ctx,
849 : ulong in_idx,
850 : ulong seq FD_PARAM_UNUSED,
851 : ulong sig,
852 : ulong chunk,
853 : ulong sz,
854 0 : ulong ctl FD_PARAM_UNUSED ) {
855 :
856 0 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
857 :
858 0 : switch( ctx->in_kind[ in_idx ] ) {
859 0 : case IN_KIND_REPLAY: {
860 0 : if( FD_LIKELY( sig==REPLAY_SIG_TXN_EXECUTED ) ) {
861 0 : fd_replay_txn_executed_t const * txn_executed = fd_type_pun_const( dcache_entry );
862 0 : ctx->txn_committed = !!txn_executed->is_committable;
863 0 : if( FD_UNLIKELY( !txn_executed->is_committable ) ) return;
864 0 : memcpy( ctx->executed_txn_sig, fd_txn_get_signatures( TXN(txn_executed->txn), txn_executed->txn->payload ), FD_TXN_SIGNATURE_SZ );
865 0 : return;
866 0 : }
867 0 : if( FD_LIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
868 :
869 : /* There was a leader transition. Handle it. */
870 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
871 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
872 :
873 0 : fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
874 0 : return;
875 0 : }
876 0 : case IN_KIND_POH: {
877 : /* Not interested in stamped microblocks, only leader updates. */
878 0 : if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
879 :
880 : /* There was a leader transition. Handle it. */
881 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
882 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
883 :
884 0 : fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
885 0 : return;
886 0 : }
887 0 : case IN_KIND_EXECLE: {
888 0 : FD_TEST( ctx->use_consumed_cus );
889 : /* For a previous slot */
890 0 : if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
891 :
892 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<FD_PACK_REBATE_MIN_SZ
893 0 : || sz>FD_PACK_REBATE_MAX_SZ ) )
894 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
895 :
896 0 : ctx->pending_rebate_sz = sz;
897 0 : fd_memcpy( ctx->rebate, dcache_entry, sz );
898 0 : return;
899 0 : }
900 0 : case IN_KIND_RESOLV: {
901 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_TPU_RESOLVED_MTU ) )
902 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
903 :
904 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)dcache_entry;
905 0 : ulong payload_sz = txnm->payload_sz;
906 0 : ulong txn_t_sz = txnm->txn_t_sz;
907 0 : uint source_ipv4 = txnm->source_ipv4;
908 0 : uchar source_tpu = txnm->source_tpu;
909 0 : FD_TEST( payload_sz<=FD_TPU_MTU );
910 0 : FD_TEST( txn_t_sz <=FD_TXN_MAX_SZ );
911 0 : fd_txn_t * txn = fd_txn_m_txn_t( txnm );
912 :
913 0 : ulong addr_table_sz = 32UL*txn->addr_table_adtl_cnt;
914 0 : FD_TEST( addr_table_sz<=32UL*FD_TXN_ACCT_ADDR_MAX );
915 :
916 0 : if( FD_UNLIKELY( (ctx->leader_slot==ULONG_MAX) & (sig>ctx->highest_observed_block_height) ) ) {
917 : /* Using the resolv tile's knowledge of the current block height
918 : is a bit of a hack, since we don't get any info if there are no
919 : transactions and we're not leader. We're actually in exactly
920 : the case where that's okay though. The point of calling
921 : expire_before long before we become leader is so that we don't
922 : drop new but low-fee-paying transactions when pack is clogged
923 : with expired but high-fee-paying transactions. That can only
924 : happen if we are getting transactions.
925 :
926 : We aren't as strict here, because there's the possibility we
927 : could replay a transaction that landed in one fork, then end up
928 : building off a different fork. */
929 0 : ctx->highest_observed_block_height = sig;
930 0 : ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->highest_observed_block_height, TRANSACTION_LIFETIME_SLOTS_NOT_STRICT )-TRANSACTION_LIFETIME_SLOTS_NOT_STRICT );
931 0 : FD_MCNT_INC( PACK, TXN_DROPPED_EXPIRED, exp_cnt );
932 0 : }
933 :
934 :
935 0 : ulong bundle_id = txnm->block_engine.bundle_id;
936 0 : if( FD_UNLIKELY( bundle_id ) ) {
937 0 : ctx->is_bundle = 1;
938 0 : if( FD_LIKELY( bundle_id!=ctx->current_bundle->id ) ) {
939 0 : if( FD_UNLIKELY( ctx->current_bundle->bundle ) ) {
940 0 : FD_MCNT_INC( PACK, TXN_DROPPED_PARTIAL_BUNDLE, ctx->current_bundle->txn_received );
941 0 : fd_pack_insert_bundle_cancel( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt );
942 0 : }
943 0 : ctx->current_bundle->id = bundle_id;
944 0 : ctx->current_bundle->txn_cnt = txnm->block_engine.bundle_txn_cnt;
945 0 : ctx->current_bundle->min_blockhash_height = ULONG_MAX;
946 0 : ctx->current_bundle->txn_received = 0UL;
947 :
948 0 : if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) {
949 0 : FD_MCNT_INC( PACK, TXN_DROPPED_PARTIAL_BUNDLE, 1UL );
950 0 : ctx->current_bundle->id = 0UL;
951 0 : return;
952 0 : }
953 0 : ctx->blk_engine_cfg->commission = txnm->block_engine.commission;
954 0 : memcpy( ctx->blk_engine_cfg->commission_pubkey->b, txnm->block_engine.commission_pubkey, 32UL );
955 :
956 0 : ctx->current_bundle->bundle = fd_pack_insert_bundle_init( ctx->pack, ctx->current_bundle->_txn, ctx->current_bundle->txn_cnt );
957 0 : }
958 0 : ctx->cur_spot = ctx->current_bundle->bundle[ ctx->current_bundle->txn_received ];
959 0 : int is_nonce = fd_disco_tpu_is_durable_nonce( txn, fd_txn_m_payload( txnm ) );
960 : /* If it's a nonce transaction, we don't want to include the slot
961 : of the nonce in the min calculation, so we use the highest
962 : block height we know about, which might be block_height if
963 : we've been leader for a while in a row. */
964 0 : ulong block_height_to_use = fd_ulong_if( is_nonce, fd_ulong_max( ctx->block_height, ctx->highest_observed_block_height ), sig );
965 0 : ctx->current_bundle->min_blockhash_height = fd_ulong_min( ctx->current_bundle->min_blockhash_height, block_height_to_use );
966 0 : } else {
967 0 : ctx->is_bundle = 0;
968 0 : ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
969 0 : }
970 :
971 : /* We get transactions from the resolv tile.
972 : The transactions should have been parsed and verified. */
973 0 : FD_MCNT_INC( PACK, TXN_RX, 1UL );
974 :
975 0 : fd_memcpy( ctx->cur_spot->txnp->payload, fd_txn_m_payload( txnm ), payload_sz );
976 0 : fd_memcpy( TXN(ctx->cur_spot->txnp), txn, txn_t_sz );
977 0 : fd_memcpy( ctx->cur_spot->alt_accts, fd_txn_m_alut( txnm ), addr_table_sz );
978 0 : ctx->cur_spot->txnp->scheduler_arrival_time_nanos = fd_clock_tile_now( ctx->clock );
979 0 : ctx->cur_spot->txnp->first_seen_nanos = txnm->first_seen_nanos;
980 0 : ctx->cur_spot->txnp->payload_sz = (ushort)payload_sz;
981 0 : ctx->cur_spot->txnp->source_ipv4 = source_ipv4;
982 0 : ctx->cur_spot->txnp->source_tpu = source_tpu;
983 :
984 0 : break;
985 0 : }
986 0 : case IN_KIND_EXECUTED_TXN: {
987 0 : FD_TEST( sz==64UL );
988 0 : fd_memcpy( ctx->executed_txn_sig, dcache_entry, sz );
989 0 : break;
990 0 : }
991 0 : }
992 0 : }
993 :
994 :
995 : /* After the transaction has been fully received, and we know we were
996 : not overrun while reading it, insert it into pack. */
997 :
998 : static inline void
999 : after_frag( fd_pack_ctx_t * ctx,
1000 : ulong in_idx,
1001 : ulong seq,
1002 : ulong sig,
1003 : ulong sz,
1004 : ulong tsorig,
1005 : ulong tspub,
1006 0 : fd_stem_context_t * stem ) {
1007 0 : (void)seq;
1008 0 : (void)sz;
1009 0 : (void)tsorig;
1010 0 : (void)tspub;
1011 0 : (void)stem;
1012 :
1013 0 : long now = fd_tickcount();
1014 :
1015 0 : ulong leader_slot = ULONG_MAX;
1016 0 : switch( ctx->in_kind[ in_idx ] ) {
1017 0 : case IN_KIND_REPLAY:
1018 0 : if( FD_LIKELY( sig==REPLAY_SIG_TXN_EXECUTED && ctx->txn_committed ) ) {
1019 0 : ulong deleted = fd_pack_delete_transaction( ctx->pack, fd_type_pun( ctx->executed_txn_sig ) );
1020 0 : FD_MCNT_INC( PACK, TXN_DROPPED_ALREADY_EXECUTED, deleted );
1021 0 : }
1022 0 : if( FD_UNLIKELY( sig==REPLAY_SIG_RESET && ctx->leader_slot!=ULONG_MAX ) ) {
1023 0 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out.mem, ctx->poh_out.chunk );
1024 0 : get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_ABANDONED );
1025 0 : log_end_block_metrics( ctx, now, "reset", done_packing->limits_usage->block_cost );
1026 0 : fd_pack_end_block( ctx->pack );
1027 0 : fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers );
1028 :
1029 0 : metrics_write( ctx ); /* final for the slot before poh ends it */
1030 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out.chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1031 0 : ctx->poh_out.chunk = fd_dcache_compact_next( ctx->poh_out.chunk, sizeof(fd_done_packing_t), ctx->poh_out.chunk0, ctx->poh_out.wmark );
1032 0 : ctx->pack_idx++;
1033 :
1034 0 : FD_LOG_WARNING(( "consensus reset while packing for slot %lu, ending block early", ctx->leader_slot ));
1035 0 : ctx->drain_execle = 1;
1036 0 : ctx->leader_slot = ULONG_MAX;
1037 0 : ctx->slot_microblock_cnt = 0UL;
1038 0 : remove_ib( ctx );
1039 :
1040 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER, 0 );
1041 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_EXECLES, 0 );
1042 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, 0 );
1043 0 : return;
1044 0 : }
1045 0 : if( FD_UNLIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
1046 0 : leader_slot = ctx->_became_leader->slot;
1047 :
1048 0 : ctx->start_block_sched_metrics->time = now;
1049 0 : fd_pack_get_sched_metrics( ctx->pack, ctx->start_block_sched_metrics->sched_results );
1050 0 : break;
1051 0 : case IN_KIND_POH:
1052 0 : if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
1053 0 : leader_slot = fd_disco_poh_sig_slot( sig );
1054 0 : break;
1055 0 : default:
1056 0 : break;
1057 0 : }
1058 :
1059 0 : switch( ctx->in_kind[ in_idx ] ) {
1060 0 : case IN_KIND_REPLAY:
1061 0 : case IN_KIND_POH: {
1062 0 : long now_ticks = fd_tickcount();
1063 0 : long now_ns = fd_log_wallclock();
1064 :
1065 0 : if( FD_UNLIKELY( ctx->leader_slot!=ULONG_MAX ) ) {
1066 0 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out.mem, ctx->poh_out.chunk );
1067 0 : get_done_packing( ctx, done_packing, FD_PACK_END_SLOT_REASON_ABANDONED );
1068 0 : log_end_block_metrics( ctx, now_ticks, "switch", done_packing->limits_usage->block_cost );
1069 0 : fd_pack_end_block( ctx->pack );
1070 0 : fd_pack_get_top_writers( ctx->pack, done_packing->limits_usage->top_writers );
1071 :
1072 0 : metrics_write( ctx ); /* final for the slot before poh ends it */
1073 0 : fd_stem_publish( stem, ctx->poh_out.out_idx, fd_disco_execle_sig( ctx->leader_slot, ctx->pack_idx ), ctx->poh_out.chunk, sizeof(fd_done_packing_t), 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
1074 0 : ctx->poh_out.chunk = fd_dcache_compact_next( ctx->poh_out.chunk, sizeof(fd_done_packing_t), ctx->poh_out.chunk0, ctx->poh_out.wmark );
1075 0 : ctx->pack_idx++;
1076 :
1077 0 : FD_LOG_WARNING(( "switching to slot %lu while packing for slot %lu. Draining execle tiles.", leader_slot, ctx->leader_slot ));
1078 0 : ctx->drain_execle = 1;
1079 0 : ctx->leader_slot = ULONG_MAX;
1080 0 : ctx->slot_microblock_cnt = 0UL;
1081 0 : remove_ib( ctx );
1082 0 : }
1083 0 : ctx->leader_slot = leader_slot;
1084 0 : ctx->block_height = ctx->_became_leader->block_height;
1085 :
1086 0 : ctx->slot_pack_start_ns = now_ns;
1087 0 : ctx->slot_bundle_txn_cnt = 0UL;
1088 :
1089 0 : ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->block_height, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
1090 0 : FD_MCNT_INC( PACK, TXN_DROPPED_EXPIRED, exp_cnt );
1091 :
1092 0 : ctx->leader_bank = ctx->_became_leader->bank;
1093 0 : ctx->leader_bank_idx = ctx->_became_leader->bank_idx;
1094 0 : ctx->leader_bank_seq = ctx->_became_leader->bank_seq;
1095 0 : ctx->slot_max_microblocks = ctx->_became_leader->max_microblocks_in_slot;
1096 :
1097 : /* Cap pack's entry bytes at the worst case: how many entry bytes
1098 : fit in max_shred_idx given our shredding. We fill a batch until
1099 : the next microblock would not fit in two FEC sets, then pad out
1100 : that batch. Empty ticks are subtracted below. */
1101 0 : ulong shreds = fd_ulong_max( ctx->_became_leader->limits.slot_max_data_shreds, ctx->bench_max_shreds_per_block );
1102 0 : ulong max_microblock_sz = sizeof(fd_entry_batch_header_t) + EFFECTIVE_TXN_PER_MICROBLOCK*FD_TPU_MTU;
1103 0 : ulong base_max_data = fd_shred_batch_pack_data_max( shreds, max_microblock_sz );
1104 0 : FD_TEST( base_max_data );
1105 : /* Reserve some space in the block for ticks */
1106 0 : ctx->slot_max_data = base_max_data
1107 0 : - 48UL*(ctx->_became_leader->ticks_per_slot+ctx->_became_leader->total_skipped_ticks);
1108 :
1109 0 : ctx->limits.slot_max_cost = ctx->_became_leader->limits.slot_max_cost;
1110 0 : ctx->limits.slot_max_vote_cost = ctx->_became_leader->limits.slot_max_vote_cost;
1111 0 : ctx->limits.slot_max_write_cost_per_acct = ctx->_became_leader->limits.slot_max_write_cost_per_acct;
1112 0 : ctx->limits.slot_max_allocated_data_per_block = ctx->_became_leader->limits.slot_max_allocated_data_per_block;
1113 0 : ctx->limits.slot_max_data_shreds = shreds;
1114 :
1115 0 : double tick_per_ns = ctx->clock->epoch->w;
1116 0 : long end_ticks = now_ticks + (long)((double)fd_long_max( ctx->_became_leader->slot_end_ns - now_ns, 1L )*tick_per_ns);
1117 : /* We may still get overrun, but then we'll never use this and just
1118 : reinitialize it the next time when we actually become leader. */
1119 0 : fd_pack_pacing_init( ctx->pacer, now_ticks, end_ticks, (float)tick_per_ns, ctx->limits.slot_max_cost );
1120 :
1121 0 : if( FD_UNLIKELY( ctx->crank->enabled ) ) {
1122 : /* If we get overrun, we'll just never use these values, but the
1123 : old values aren't really useful either. */
1124 0 : ctx->crank->epoch = ctx->_became_leader->epoch;
1125 0 : *(ctx->crank->prev_config) = *(ctx->_became_leader->bundle->config);
1126 0 : memcpy( ctx->crank->recent_blockhash, ctx->_became_leader->bundle->last_blockhash, 32UL );
1127 0 : memcpy( ctx->crank->tip_receiver_owner, ctx->_became_leader->bundle->tip_receiver_owner, 32UL );
1128 0 : }
1129 :
1130 0 : FD_LOG_INFO(( "pack_became_leader(slot=%lu,ends_at=%ld)", ctx->leader_slot, ctx->_became_leader->slot_end_ns ));
1131 :
1132 0 : update_metric_state( ctx, fd_tickcount(), FD_PACK_METRIC_STATE_LEADER, 1 );
1133 :
1134 0 : ctx->slot_end_ns = ctx->_became_leader->slot_end_ns;
1135 0 : ctx->slot_dynamic_max_microblocks = ctx->slot_max_microblocks;
1136 0 : ctx->slot_mixin_per_tick = fd_ulong_if( ctx->_became_leader->hashcnt_per_tick>1UL, ctx->_became_leader->hashcnt_per_tick-1UL, 0UL ); /* 0: low power / alpenglow, no hash budget */
1137 0 : ctx->slot_tick_duration_ns = ctx->_became_leader->tick_duration_ns;
1138 0 : ctx->pending_reduce_mb_bound = 0;
1139 0 : fd_pack_limits_t limits[ 1 ];
1140 0 : limits->max_cost_per_block = ctx->limits.slot_max_cost;
1141 0 : limits->max_data_bytes_per_block = ctx->slot_max_data;
1142 0 : limits->max_microblocks_per_block = ctx->slot_max_microblocks;
1143 0 : limits->max_vote_cost_per_block = ctx->limits.slot_max_vote_cost;
1144 0 : limits->max_write_cost_per_acct = ctx->limits.slot_max_write_cost_per_acct;
1145 0 : limits->max_txn_per_microblock = ULONG_MAX; /* unused */
1146 0 : limits->max_allocated_data_per_block = ctx->limits.slot_max_allocated_data_per_block;
1147 0 : fd_pack_set_block_limits( ctx->pack, limits );
1148 0 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
1149 :
1150 0 : break;
1151 0 : }
1152 0 : case IN_KIND_EXECLE: {
1153 : /* For a previous slot */
1154 0 : if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
1155 :
1156 0 : fd_pack_rebate_cus( ctx->pack, ctx->rebate->rebate );
1157 0 : ctx->pending_rebate_sz = 0UL;
1158 0 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
1159 0 : break;
1160 0 : }
1161 0 : case IN_KIND_RESOLV: {
1162 : /* Normal transaction case */
1163 0 : if( FD_UNLIKELY( ctx->is_bundle ) ) {
1164 0 : if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) return;
1165 0 : if( FD_UNLIKELY( ++(ctx->current_bundle->txn_received)==ctx->current_bundle->txn_cnt ) ) {
1166 0 : ulong deleted;
1167 0 : long insert_duration = -fd_tickcount();
1168 0 : int result = fd_pack_insert_bundle_fini( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt, ctx->current_bundle->min_blockhash_height, 0, ctx->blk_engine_cfg, &deleted );
1169 0 : insert_duration += fd_tickcount();
1170 0 : FD_MCNT_INC( PACK, TXN_EVICTED, deleted );
1171 0 : ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ] += ctx->current_bundle->txn_received;
1172 0 : fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
1173 0 : ctx->current_bundle->bundle = NULL;
1174 0 : }
1175 0 : } else {
1176 0 : ulong blockhash_height = sig;
1177 0 : ulong deleted;
1178 0 : long insert_duration = -fd_tickcount();
1179 0 : int result = fd_pack_insert_txn_fini( ctx->pack, ctx->cur_spot, blockhash_height, &deleted );
1180 0 : insert_duration += fd_tickcount();
1181 0 : FD_MCNT_INC( PACK, TXN_EVICTED, deleted );
1182 0 : ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
1183 0 : fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
1184 0 : if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
1185 0 : }
1186 :
1187 0 : ctx->cur_spot = NULL;
1188 0 : break;
1189 0 : }
1190 0 : case IN_KIND_EXECUTED_TXN: {
1191 0 : ulong deleted = fd_pack_delete_transaction( ctx->pack, fd_type_pun( ctx->executed_txn_sig ) );
1192 0 : FD_MCNT_INC( PACK, TXN_DROPPED_ALREADY_EXECUTED, deleted );
1193 0 : break;
1194 0 : }
1195 0 : }
1196 :
1197 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
1198 0 : }
1199 :
1200 : static void
1201 : privileged_init( fd_topo_t const * topo,
1202 0 : fd_topo_tile_t const * tile ) {
1203 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1204 :
1205 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1206 0 : fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
1207 :
1208 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->rng_seed, sizeof(uint) ) ) ) {
1209 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
1210 0 : }
1211 0 : if( FD_UNLIKELY( !fd_rng_secure( &ctx->rng_idx, sizeof(ulong) ) ) ) {
1212 0 : FD_LOG_CRIT(( "fd_rng_secure failed" ));
1213 0 : }
1214 :
1215 0 : if( FD_LIKELY( !tile->pack.bundle.enabled ) ) return;
1216 0 : if( FD_UNLIKELY( !tile->pack.bundle.vote_account_path[0] ) ) {
1217 0 : FD_LOG_WARNING(( "Disabling bundle crank because no vote account was specified" ));
1218 0 : return;
1219 0 : }
1220 :
1221 0 : if( FD_UNLIKELY( !strcmp( tile->pack.bundle.identity_key_path, "" ) ) )
1222 0 : FD_LOG_ERR(( "identity_key_path not set" ));
1223 :
1224 0 : const uchar * identity_key = fd_keyload_load( tile->pack.bundle.identity_key_path, /* pubkey only: */ 1 );
1225 0 : fd_memcpy( ctx->crank->identity_pubkey->b, identity_key, 32UL );
1226 :
1227 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->pack.bundle.vote_account_path, ctx->crank->vote_pubkey->b ) ) ) {
1228 0 : const uchar * vote_key = fd_keyload_load( tile->pack.bundle.vote_account_path, /* pubkey only: */ 1 );
1229 0 : fd_memcpy( ctx->crank->vote_pubkey->b, vote_key, 32UL );
1230 0 : }
1231 0 : }
1232 :
1233 : static void
1234 : unprivileged_init( fd_topo_t const * topo,
1235 0 : fd_topo_tile_t const * tile ) {
1236 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1237 :
1238 0 : if( FD_UNLIKELY( tile->pack.max_pending_transactions >= USHORT_MAX-10UL ) ) FD_LOG_ERR(( "pack tile supports up to %lu pending transactions", USHORT_MAX-11UL ));
1239 :
1240 0 : fd_pack_limits_t limits_upper[1] = {{
1241 0 : .max_cost_per_block = tile->pack.max_cost_per_block,
1242 0 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
1243 0 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
1244 0 : .max_data_bytes_per_block = FD_PACK_MAX_DATA_PER_BLOCK*(tile->pack.max_shreds_per_block/FD_SHRED_BLK_MAX),
1245 0 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
1246 0 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
1247 0 : .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
1248 0 : }};
1249 :
1250 0 : ulong pack_footprint = fd_pack_footprint( tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.execle_tile_count, limits_upper );
1251 :
1252 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1253 0 : fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
1254 0 : fd_rng_t * rng = fd_rng_join( fd_rng_new( FD_SCRATCH_ALLOC_APPEND( l, fd_rng_align(), fd_rng_footprint() ), ctx->rng_seed, ctx->rng_idx ) );
1255 0 : if( FD_UNLIKELY( !rng ) ) FD_LOG_ERR(( "fd_rng_new failed" ));
1256 :
1257 0 : fd_pack_limits_t limits_lower[1] = {{
1258 0 : .max_cost_per_block = FD_PACK_MAX_COST_PER_BLOCK_LOWER_BOUND, /* replaced by the chain's at become-leader */
1259 0 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_LOWER_BOUND,
1260 0 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_LOWER_BOUND,
1261 0 : .max_data_bytes_per_block = FD_PACK_MAX_DATA_PER_BLOCK*(tile->pack.max_shreds_per_block/FD_SHRED_BLK_MAX),
1262 0 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
1263 0 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
1264 0 : .max_allocated_data_per_block = FD_PACK_MAX_ALLOCATED_DATA_PER_BLOCK,
1265 0 : }};
1266 :
1267 0 : ctx->pack = fd_pack_join( fd_pack_new( FD_SCRATCH_ALLOC_APPEND( l, fd_pack_align(), pack_footprint ),
1268 0 : tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.execle_tile_count,
1269 0 : limits_upper,
1270 0 : fd_type_pun_const( tile->pack.acct_blocklist ), tile->pack.acct_blocklist_cnt,
1271 0 : rng ) );
1272 0 : if( FD_UNLIKELY( !ctx->pack ) ) FD_LOG_ERR(( "fd_pack_new failed" ));
1273 :
1274 0 : fd_pack_set_block_limits( ctx->pack, limits_lower );
1275 :
1276 0 : ulong in_max = sizeof(ctx->in)/sizeof(ctx->in[0]);
1277 0 : if( FD_UNLIKELY( tile->in_cnt>in_max ) )
1278 0 : FD_LOG_ERR(( "Too many input links (%lu>%lu) to pack tile", tile->in_cnt, in_max ));
1279 0 : FD_TEST( in_max==sizeof(ctx->in_kind)/sizeof(ctx->in_kind[0]) );
1280 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1281 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1282 :
1283 0 : if( FD_LIKELY( !strcmp( link->name, "resolv_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
1284 0 : else if( FD_LIKELY( !strcmp( link->name, "resolh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
1285 0 : else if( FD_LIKELY( !strcmp( link->name, "poh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
1286 0 : else if( FD_LIKELY( !strcmp( link->name, "pohh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
1287 0 : else if( FD_LIKELY( !strcmp( link->name, "bank_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE;
1288 0 : else if( FD_LIKELY( !strcmp( link->name, "execle_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE;
1289 0 : else if( FD_LIKELY( !strcmp( link->name, "sign_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
1290 0 : else if( FD_LIKELY( !strcmp( link->name, "replay_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
1291 0 : else if( FD_LIKELY( !strcmp( link->name, "executed_txn" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECUTED_TXN;
1292 0 : else FD_LOG_ERR(( "pack tile has unexpected input link %lu %s", i, link->name ));
1293 0 : }
1294 :
1295 0 : if( FD_UNLIKELY( tile->pack.execle_tile_count>FD_PACK_MAX_EXECLE_TILES ) ) FD_LOG_ERR(( "pack tile connects to too many execle tiles" ));
1296 :
1297 0 : FD_TEST( (tile->pack.schedule_strategy>=0) & (tile->pack.schedule_strategy<=FD_PACK_STRATEGY_BALANCED) );
1298 :
1299 0 : ctx->crank->enabled = tile->pack.bundle.enabled;
1300 0 : if( FD_UNLIKELY( tile->pack.bundle.enabled ) ) {
1301 0 : if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->crank->gen, (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_program_addr,
1302 0 : (fd_acct_addr_t const *)tile->pack.bundle.tip_payment_program_addr,
1303 0 : (fd_acct_addr_t const *)ctx->crank->vote_pubkey->b,
1304 0 : (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_authority,
1305 0 : schedule_strategy_strings[ tile->pack.schedule_strategy ],
1306 0 : tile->pack.bundle.commission_bps ) ) ) {
1307 0 : FD_LOG_ERR(( "constructing bundle generator failed" ));
1308 0 : }
1309 :
1310 0 : ulong sign_in_idx = fd_topo_find_tile_in_link ( topo, tile, "sign_pack", tile->kind_id );
1311 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_sign", tile->kind_id );
1312 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
1313 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
1314 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
1315 :
1316 0 : fd_sleep_t * sleep = NULL;
1317 0 : if( FD_UNLIKELY( topo->sleep_obj_id!=ULONG_MAX ) ) {
1318 0 : sleep = fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) );
1319 0 : FD_TEST( sleep );
1320 0 : }
1321 :
1322 0 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->crank->keyguard_client,
1323 0 : sign_out->mcache,
1324 0 : sign_out->dcache,
1325 0 : sign_in->mcache,
1326 0 : sign_in->dcache,
1327 0 : sign_out->mtu,
1328 0 : sign_in->mtu,
1329 0 : sleep,
1330 0 : sign_out->id,
1331 0 : fd_topo_find_link_consumer( topo, sign_out ) ) ) ) ) {
1332 0 : FD_LOG_ERR(( "failed to construct keyguard" ));
1333 0 : }
1334 : /* Initialize enough of the prev config that it produces a
1335 : transaction */
1336 0 : ctx->crank->prev_config->discriminator = 0x82ccfa1ee0aa0c9bUL;
1337 0 : ctx->crank->prev_config->tip_receiver->b[1] = 1;
1338 0 : ctx->crank->prev_config->block_builder->b[2] = 1;
1339 :
1340 0 : memset( ctx->crank->tip_receiver_owner, '\0', 32UL );
1341 0 : memset( ctx->crank->recent_blockhash, '\0', 32UL );
1342 0 : memset( ctx->crank->last_sig, '\0', 64UL );
1343 0 : ctx->crank->ib_inserted = 0;
1344 0 : ctx->crank->epoch = 0UL;
1345 0 : ctx->crank->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
1346 0 : FD_TEST( ctx->crank->keyswitch );
1347 0 : } else {
1348 0 : memset( ctx->crank, '\0', sizeof(ctx->crank) );
1349 0 : }
1350 :
1351 :
1352 0 : ctx->cur_spot = NULL;
1353 0 : ctx->is_bundle = 0;
1354 0 : ctx->strategy = tile->pack.schedule_strategy;
1355 0 : ctx->max_pending_transactions = tile->pack.max_pending_transactions;
1356 0 : ctx->leader_slot = ULONG_MAX;
1357 0 : ctx->block_height = 0UL;
1358 0 : ctx->leader_bank = NULL;
1359 0 : ctx->leader_bank_idx = ULONG_MAX;
1360 0 : ctx->leader_bank_seq = ULONG_MAX;
1361 0 : ctx->pack_idx = 0UL;
1362 0 : ctx->slot_microblock_cnt = 0UL;
1363 0 : ctx->pack_txn_cnt = 0UL;
1364 0 : ctx->slot_max_microblocks = 0UL;
1365 0 : ctx->slot_dynamic_max_microblocks = 0UL;
1366 0 : ctx->pending_reduce_mb_bound = 0;
1367 0 : ctx->slot_max_data = 0UL;
1368 0 : ctx->drain_execle = 0;
1369 0 : ctx->rng = rng;
1370 0 : fd_clock_tile_init( ctx->clock );
1371 0 : double tick_per_ns = ctx->clock->epoch->w;
1372 0 : ctx->last_successful_insert = 0L;
1373 0 : ctx->highest_observed_block_height = 0UL;
1374 0 : ctx->microblock_duration_ticks = (ulong)(tick_per_ns*(double)MICROBLOCK_DURATION_NS + 0.5);
1375 0 : ctx->use_consumed_cus = tile->pack.use_consumed_cus;
1376 0 : ctx->bench_max_shreds_per_block = tile->pack.bench_max_shreds_per_block;
1377 0 : ctx->crank->enabled = tile->pack.bundle.enabled;
1378 :
1379 0 : ctx->limits.slot_max_cost = limits_lower->max_cost_per_block;
1380 0 : ctx->limits.slot_max_vote_cost = limits_lower->max_vote_cost_per_block;
1381 0 : ctx->limits.slot_max_write_cost_per_acct = limits_lower->max_write_cost_per_acct;
1382 :
1383 0 : ctx->execle_cnt = tile->pack.execle_tile_count;
1384 0 : ctx->poll_cursor = 0;
1385 0 : ctx->skip_cnt = 0L;
1386 0 : ctx->execle_idle_bitset = fd_ulong_mask_lsb( (int)tile->pack.execle_tile_count );
1387 0 : for( ulong i=0UL; i<tile->pack.execle_tile_count; i++ ) {
1388 0 : ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "execle_busy.%lu", i );
1389 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
1390 0 : ctx->execle_current[ i ] = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
1391 0 : ctx->execle_expect[ i ] = ULONG_MAX;
1392 0 : if( FD_UNLIKELY( !ctx->execle_current[ i ] ) ) FD_LOG_ERR(( "execle tile %lu has no busy flag", i ));
1393 0 : ctx->execle_ready_at[ i ] = 0L;
1394 0 : FD_TEST( ULONG_MAX==fd_fseq_query( ctx->execle_current[ i ] ) );
1395 0 : }
1396 :
1397 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1398 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1399 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
1400 :
1401 0 : ctx->in[ i ].mem = link_wksp->wksp;
1402 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
1403 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
1404 0 : }
1405 :
1406 0 : char const * execle_out_name = fd_topo_find_tile_out_link( topo, tile, "pack_execle", 0UL )!=ULONG_MAX ? "pack_execle" : "pack_bank";
1407 0 : for( ulong i=0UL; i<ctx->execle_cnt; i++ ) {
1408 0 : fd_pack_out_ctx_t * out = &ctx->execle_out[ i ];
1409 0 : out->out_idx = fd_topo_find_tile_out_link( topo, tile, execle_out_name, i );
1410 0 : FD_TEST( out->out_idx!=ULONG_MAX );
1411 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ out->out_idx ] ];
1412 0 : out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
1413 0 : out->chunk0 = fd_dcache_compact_chunk0( out->mem, link->dcache );
1414 0 : out->wmark = fd_dcache_compact_wmark ( out->mem, link->dcache, link->mtu );
1415 0 : out->chunk = out->chunk0;
1416 0 : }
1417 :
1418 0 : ctx->poh_out.out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_poh", 0UL );
1419 0 : if( FD_UNLIKELY( ctx->poh_out.out_idx==ULONG_MAX ) ) ctx->poh_out.out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_pohh", 0UL );
1420 0 : FD_TEST( ctx->poh_out.out_idx!=ULONG_MAX );
1421 0 : fd_topo_link_t const * poh_out_link = &topo->links[ tile->out_link_id[ ctx->poh_out.out_idx ] ];
1422 0 : ctx->poh_out.mem = topo->workspaces[ topo->objs[ poh_out_link->dcache_obj_id ].wksp_id ].wksp;
1423 0 : ctx->poh_out.chunk0 = fd_dcache_compact_chunk0( ctx->poh_out.mem, poh_out_link->dcache );
1424 0 : ctx->poh_out.wmark = fd_dcache_compact_wmark ( ctx->poh_out.mem, poh_out_link->dcache, poh_out_link->mtu );
1425 0 : ctx->poh_out.chunk = ctx->poh_out.chunk0;
1426 :
1427 : /* Initialize metrics storage */
1428 0 : memset( ctx->insert_result, '\0', FD_PACK_INSERT_RETVAL_CNT * sizeof(ulong) );
1429 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->schedule_duration, FD_MHIST_EDGES_NS( PACK, MICROBLOCK_SCHEDULE_DURATION_NANOS ) ) );
1430 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->no_sched_duration, FD_MHIST_EDGES_NS( PACK, MICROBLOCK_SCHEDULE_EMPTY_DURATION_NANOS ) ) );
1431 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->insert_duration, FD_MHIST_EDGES_NS( PACK, TXN_INSERT_DURATION_NANOS ) ) );
1432 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->complete_duration, FD_MHIST_EDGES_NS( PACK, MICROBLOCK_COMPLETE_DURATION_NANOS ) ) );
1433 0 : ctx->metric_state = 0;
1434 0 : ctx->metric_state_begin = fd_tickcount();
1435 0 : memset( ctx->metric_timing, '\0', 16*sizeof(long) );
1436 0 : memset( ctx->current_bundle, '\0', sizeof(ctx->current_bundle) );
1437 0 : memset( ctx->blk_engine_cfg, '\0', sizeof(ctx->blk_engine_cfg) );
1438 0 : memset( ctx->last_sched_metrics, '\0', sizeof(ctx->last_sched_metrics) );
1439 0 : memset( ctx->start_block_sched_metrics, '\0', sizeof(ctx->start_block_sched_metrics) );
1440 0 : memset( ctx->crank->metrics, '\0', sizeof(ctx->crank->metrics) );
1441 :
1442 0 : FD_LOG_INFO(( "packing microblocks of at most %lu transactions to %lu execle tiles using strategy %i", EFFECTIVE_TXN_PER_MICROBLOCK, tile->pack.execle_tile_count, ctx->strategy ));
1443 :
1444 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
1445 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1446 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1447 0 : }
1448 :
1449 : static ulong
1450 : populate_allowed_seccomp( fd_topo_t const * topo,
1451 : fd_topo_tile_t const * tile,
1452 : ulong out_cnt,
1453 0 : struct sock_filter * out ) {
1454 0 : (void)topo;
1455 0 : (void)tile;
1456 :
1457 0 : populate_sock_filter_policy_fd_pack_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
1458 0 : return sock_filter_policy_fd_pack_tile_instr_cnt;
1459 0 : }
1460 :
1461 : static ulong
1462 : populate_allowed_fds( fd_topo_t const * topo,
1463 : fd_topo_tile_t const * tile,
1464 : ulong out_fds_cnt,
1465 0 : int * out_fds ) {
1466 0 : (void)topo;
1467 0 : (void)tile;
1468 :
1469 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1470 :
1471 0 : ulong out_cnt = 0UL;
1472 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1473 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1474 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1475 0 : return out_cnt;
1476 0 : }
1477 :
1478 : /* Pack can publish a frag in the following scenarios:
1479 :
1480 : after_credit:
1481 : A. TIMED_OUT. Sets ctx->leader_slot=ULONG_MAX. return.
1482 : B. DONE_DRAINING. *after* DONE_PACKING. return.
1483 : C. REDUCE_MB_BOUND. return.
1484 : D. SCHEDULE_MB. *doesn't* return.
1485 : E. EXHAUST_MICROBLOCKS. Sets ctx->leader_slot=ULONG_MAX. return.
1486 : after_frag:
1487 : F. ABANDONED. Requires ctx->leader_slot!=ULONG_MAX
1488 :
1489 : It isn't possible to get a burst of 3, but a burst of 2 is possible
1490 : in these situations.
1491 :
1492 : C -> F
1493 : D -> F
1494 : D -> E
1495 : */
1496 0 : #define STEM_BURST (2UL)
1497 :
1498 : /* We want lazy (measured in ns) to be small enough that the producer
1499 : and the consumer never have to wait for credits. For most tango
1500 : links, we use a default worst case speed coming from 100 Gbps
1501 : Ethernet. That's not very suitable for microblocks that go from
1502 : pack to bank. Instead we manually estimate the very aggressive
1503 : 1000ns per microblock, and then reduce it further (in line with the
1504 : default lazy value computation) to ensure the random value chosen
1505 : based on this won't lead to credit return stalls. */
1506 0 : #define STEM_LAZY (128L*3000L)
1507 :
1508 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_pack_ctx_t
1509 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_pack_ctx_t)
1510 :
1511 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1512 : #define STEM_CALLBACK_PREVENT_PARK prevent_park
1513 0 : #define STEM_CALLBACK_NEXT_DEADLINE next_deadline
1514 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
1515 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
1516 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1517 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1518 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1519 :
1520 : #include "../stem/fd_stem.c"
1521 :
1522 : fd_topo_run_tile_t fd_tile_pack = {
1523 : .name = "pack",
1524 : .populate_allowed_seccomp = populate_allowed_seccomp,
1525 : .populate_allowed_fds = populate_allowed_fds,
1526 : .scratch_align = scratch_align,
1527 : .scratch_footprint = scratch_footprint,
1528 : .privileged_init = privileged_init,
1529 : .unprivileged_init = unprivileged_init,
1530 : .run = stem_run,
1531 : };
|