Line data Source code
1 : #include "../tiles.h"
2 :
3 : #include "generated/fd_pack_tile_seccomp.h"
4 :
5 : #include "../../util/pod/fd_pod_format.h"
6 : #include "../../discof/replay/fd_replay_tile.h" // layering violation
7 : #include "../fd_txn_m.h"
8 : #include "../keyguard/fd_keyload.h"
9 : #include "../keyguard/fd_keyswitch.h"
10 : #include "../keyguard/fd_keyguard.h"
11 : #include "../metrics/fd_metrics.h"
12 : #include "../pack/fd_pack.h"
13 : #include "../pack/fd_pack_cost.h"
14 : #include "../pack/fd_pack_pacing.h"
15 :
16 : #include <linux/unistd.h>
17 : #include <string.h>
18 :
19 : /* fd_pack is responsible for taking verified transactions, and
20 : arranging them into "microblocks" (groups) of transactions to
21 : be executed serially. It can try to do clever things so that
22 : multiple microblocks can execute in parallel, if they don't
23 : write to the same accounts. */
24 :
25 6504 : #define IN_KIND_RESOLV (0UL)
26 1047 : #define IN_KIND_POH (1UL)
27 138 : #define IN_KIND_BANK (2UL)
28 30 : #define IN_KIND_SIGN (3UL)
29 0 : #define IN_KIND_REPLAY (4UL)
30 30 : #define IN_KIND_EXECUTED_TXN (5UL)
31 :
32 : /* Pace microblocks, but only slightly. This helps keep performance
33 : more stable. This limit is 2,000 microblocks/second/bank. At 31
34 : transactions/microblock, that's 62k txn/sec/bank. */
35 30 : #define MICROBLOCK_DURATION_NS (0L)
36 :
37 : /* There are 151 accepted blockhashes, but those don't include skips.
38 : This check is neither precise nor accurate, but just good enough.
39 : The bank tile does the final check. We give a little margin for a
40 : few percent skip rate. */
41 1017 : #define TRANSACTION_LIFETIME_SLOTS 160UL
42 :
43 : /* Time is normally a long, but pack expects a ulong. Add -LONG_MIN to
44 : the time values so that LONG_MIN maps to 0, LONG_MAX maps to
45 : ULONG_MAX, and everything in between maps linearly with a slope of 1.
46 : Just subtracting LONG_MIN results in signed integer overflow, which
47 : is U.B. */
48 : #define TIME_OFFSET 0x8000000000000000UL
49 : FD_STATIC_ASSERT( (ulong)LONG_MIN+TIME_OFFSET==0UL, time_offset );
50 : FD_STATIC_ASSERT( (ulong)LONG_MAX+TIME_OFFSET==ULONG_MAX, time_offset );
51 :
52 : /* 1.6 M cost units, enough for 1 max size transaction */
53 : const ulong CUS_PER_MICROBLOCK = 1600000UL;
54 :
55 : #define SMALL_MICROBLOCKS 1
56 :
57 : #if SMALL_MICROBLOCKS
58 : const float VOTE_FRACTION = 1.0f; /* schedule all available votes first */
59 93 : #define EFFECTIVE_TXN_PER_MICROBLOCK 1UL
60 : #else
61 : const float VOTE_FRACTION = 0.75f; /* TODO: Is this the right value? */
62 : #define EFFECTIVE_TXN_PER_MICROBLOCK MAX_TXN_PER_MICROBLOCK
63 : #endif
64 :
65 : /* There's overhead associated with each microblock the bank tile tries
66 : to execute it, so the optimal strategy is not to produce a microblock
67 : with a single transaction as soon as we receive it. Basically, if we
68 : have less than 31 transactions, we want to wait a little to see if we
69 : receive additional transactions before we schedule a microblock. We
70 : can model the optimum amount of time to wait, but the equation is
71 : complicated enough that we want to compute it before compile time.
72 : wait_duration[i] for i in [0, 31] gives the time in nanoseconds pack
73 : should wait after receiving its most recent transaction before
74 : scheduling if it has i transactions available. Unsurprisingly,
75 : wait_duration[31] is 0. wait_duration[0] is ULONG_MAX, so we'll
76 : always wait if we have 0 transactions. */
77 : FD_IMPORT( wait_duration, "src/disco/pack/pack_delay.bin", ulong, 6, "" );
78 :
79 :
80 :
81 : #if FD_PACK_USE_EXTRA_STORAGE
82 : /* When we are done being leader for a slot and we are leader in the
83 : very next slot, it can still take some time to transition. This is
84 : because the bank has to be finalized, a hash calculated, and various
85 : other things done in the replay stage to create the new child bank.
86 :
87 : During that time, pack cannot send transactions to banks so it needs
88 : to be able to buffer. Typically, these so called "leader
89 : transitions" are short (<15 millis), so a low value here would
90 : suffice. However, in some cases when there is memory pressure on the
91 : NUMA node or when the operating system context switches relevant
92 : threads out, it can take significantly longer.
93 :
94 : To prevent drops in these cases and because we assume banks are fast
95 : enough to drain this buffer once we do become leader, we set this
96 : buffer size to be quite large. */
97 :
98 : #define DEQUE_NAME extra_txn_deq
99 : #define DEQUE_T fd_txn_e_t
100 : #define DEQUE_MAX (128UL*1024UL)
101 : #include "../../../../util/tmpl/fd_deque.c"
102 :
103 : #endif
104 :
105 : /* Sync with src/app/shared/fd_config.c */
106 1124 : #define FD_PACK_STRATEGY_PERF 0
107 0 : #define FD_PACK_STRATEGY_BALANCED 1
108 0 : #define FD_PACK_STRATEGY_BUNDLE 2
109 :
110 : static char const * const schedule_strategy_strings[3] = { "PRF", "BAL", "BUN" };
111 :
112 :
113 : typedef struct {
114 : fd_acct_addr_t commission_pubkey[1];
115 : ulong commission;
116 : } block_builder_info_t;
117 :
118 : typedef struct {
119 : fd_wksp_t * mem;
120 : ulong chunk0;
121 : ulong wmark;
122 : } fd_pack_in_ctx_t;
123 :
124 : typedef struct {
125 : fd_pack_t * pack;
126 : fd_txn_e_t * cur_spot;
127 : int is_bundle; /* is the current transaction a bundle */
128 :
129 : uchar executed_txn_sig[ 64UL ];
130 :
131 : /* One of the FD_PACK_STRATEGY_* values defined above */
132 : int strategy;
133 :
134 : /* The value passed to fd_pack_new, etc. */
135 : ulong max_pending_transactions;
136 :
137 : /* The leader slot we are currently packing for, or ULONG_MAX if we
138 : are not the leader. */
139 : ulong leader_slot;
140 : void const * leader_bank;
141 : ulong leader_bank_idx;
142 :
143 : fd_became_leader_t _became_leader[1];
144 :
145 : /* The number of microblocks we have packed for the current leader
146 : slot. Will always be <= slot_max_microblocks. We must track
147 : this so that when we are done we can tell the PoH tile how many
148 : microblocks to expect in the slot. */
149 : ulong slot_microblock_cnt;
150 :
151 : /* Counter which increments when we've finished packing for a slot */
152 : uint pack_idx;
153 :
154 : ulong pack_txn_cnt; /* total num transactions packed since startup */
155 :
156 : /* The maximum number of microblocks that can be packed in this slot.
157 : Provided by the PoH tile when we become leader.*/
158 : ulong slot_max_microblocks;
159 :
160 : /* Cap (in bytes) of the amount of transaction data we produce in each
161 : block to avoid hitting the shred limits. See where this is set for
162 : more explanation. */
163 : ulong slot_max_data;
164 : int larger_shred_limits_per_block;
165 :
166 : /* Consensus critical slot cost limits. */
167 : struct {
168 : ulong slot_max_cost;
169 : ulong slot_max_vote_cost;
170 : ulong slot_max_write_cost_per_acct;
171 : } limits;
172 :
173 : /* If drain_banks is non-zero, then the pack tile must wait until all
174 : banks are idle before scheduling any more microblocks. This is
175 : primarily helpful in irregular leader transitions, e.g. while being
176 : leader for slot N, we switch forks to a slot M (!=N+1) in which we
177 : are also leader. We don't want to execute microblocks for
178 : different slots concurrently. */
179 : int drain_banks;
180 :
181 : /* Updated during housekeeping and used only for checking if the
182 : leader slot has ended. Might be off by one housekeeping duration,
183 : but that should be small relative to a slot duration. */
184 : long approx_wallclock_ns;
185 :
186 : /* approx_tickcount is updated in during_housekeeping() with
187 : fd_tickcount() and will match approx_wallclock_ns. This is done
188 : because we need to include an accurate nanosecond timestamp in
189 : every fd_txn_p_t but don't want to have to call the expensive
190 : fd_log_wallclock() in in the critical path. We can use
191 : fd_tempo_tick_per_ns() to convert from ticks to nanoseconds over
192 : small periods of time. */
193 : long approx_tickcount;
194 :
195 : fd_rng_t * rng;
196 :
197 : /* The end wallclock time of the leader slot we are currently packing
198 : for, if we are currently packing for a slot.*/
199 : long slot_end_ns;
200 :
201 : /* pacer and ticks_per_ns are used for pacing CUs through the slot,
202 : i.e. deciding when to schedule a microblock given the number of CUs
203 : that have been consumed so far. pacer is an opaque pacing object,
204 : which is initialized when the pack tile is packing a slot.
205 : ticks_per_ns is the cached value from tempo. */
206 : fd_pack_pacing_t pacer[1];
207 : double ticks_per_ns;
208 :
209 : /* last_successful_insert stores the tickcount of the last
210 : successful transaction insert. */
211 : long last_successful_insert;
212 :
213 : /* highest_observed_slot stores the highest slot number we've seen
214 : from any transaction coming from the resolv tile. When this
215 : increases, we expire old transactions. */
216 : ulong highest_observed_slot;
217 :
218 : /* microblock_duration_ns, and wait_duration
219 : respectively scaled to be in ticks instead of nanoseconds */
220 : ulong microblock_duration_ticks;
221 : ulong wait_duration_ticks[ MAX_TXN_PER_MICROBLOCK+1UL ];
222 :
223 : #if FD_PACK_USE_EXTRA_STORAGE
224 : /* In addition to the available transactions that pack knows about, we
225 : also store a larger ring buffer for handling cases when pack is
226 : full. This is an fd_deque. */
227 : fd_txn_e_t * extra_txn_deq;
228 : int insert_to_extra; /* whether the last insert was into pack or the extra deq */
229 : #endif
230 :
231 : fd_pack_in_ctx_t in[ 32 ];
232 : int in_kind[ 32 ];
233 :
234 : ulong bank_cnt;
235 : ulong bank_idle_bitset; /* bit i is 1 if we've observed *bank_current[i]==bank_expect[i] */
236 : int poll_cursor; /* in [0, bank_cnt), the next bank to poll */
237 : int use_consumed_cus;
238 : long skip_cnt;
239 : ulong * bank_current[ FD_PACK_MAX_BANK_TILES ];
240 : ulong bank_expect[ FD_PACK_MAX_BANK_TILES ];
241 : /* bank_ready_at[x] means don't check bank x until tickcount is at
242 : least bank_ready_at[x]. */
243 : long bank_ready_at[ FD_PACK_MAX_BANK_TILES ];
244 :
245 : fd_wksp_t * bank_out_mem;
246 : ulong bank_out_chunk0;
247 : ulong bank_out_wmark;
248 : ulong bank_out_chunk;
249 :
250 : fd_wksp_t * poh_out_mem;
251 : ulong poh_out_chunk0;
252 : ulong poh_out_wmark;
253 : ulong poh_out_chunk;
254 :
255 : ulong insert_result[ FD_PACK_INSERT_RETVAL_CNT ];
256 : fd_histf_t schedule_duration[ 1 ];
257 : fd_histf_t no_sched_duration[ 1 ];
258 : fd_histf_t insert_duration [ 1 ];
259 : fd_histf_t complete_duration[ 1 ];
260 :
261 : struct {
262 : uint metric_state;
263 : long metric_state_begin;
264 : long metric_timing[ 16 ];
265 : };
266 :
267 : struct {
268 : long time;
269 : ulong all[ FD_METRICS_TOTAL_SZ ];
270 : } last_sched_metrics[1];
271 :
272 : struct {
273 : long time;
274 : ulong all[ FD_METRICS_TOTAL_SZ ];
275 : } start_block_sched_metrics[1];
276 :
277 : struct {
278 : ulong id;
279 : ulong txn_cnt;
280 : ulong txn_received;
281 : ulong min_blockhash_slot;
282 : fd_txn_e_t * _txn[ FD_PACK_MAX_TXN_PER_BUNDLE ];
283 : fd_txn_e_t * const * bundle; /* points to _txn when non-NULL */
284 : } current_bundle[1];
285 :
286 : block_builder_info_t blk_engine_cfg[1];
287 :
288 : struct {
289 : int enabled;
290 : int ib_inserted; /* in this slot */
291 : fd_acct_addr_t vote_pubkey[1];
292 : fd_acct_addr_t identity_pubkey[1];
293 : fd_bundle_crank_gen_t gen[1];
294 : fd_acct_addr_t tip_receiver_owner[1];
295 : ulong epoch;
296 : fd_bundle_crank_tip_payment_config_t prev_config[1]; /* as of start of slot, then updated */
297 : uchar recent_blockhash[32];
298 : fd_ed25519_sig_t last_sig[1];
299 :
300 : fd_keyswitch_t * keyswitch;
301 : fd_keyguard_client_t keyguard_client[1];
302 :
303 : ulong metrics[4];
304 : } crank[1];
305 :
306 :
307 : /* Used between during_frag and after_frag */
308 : ulong pending_rebate_sz;
309 : union{ fd_pack_rebate_t rebate[1]; uchar footprint[USHORT_MAX]; } rebate[1];
310 : } fd_pack_ctx_t;
311 :
312 60 : #define BUNDLE_META_SZ 40UL
313 : FD_STATIC_ASSERT( sizeof(block_builder_info_t)==BUNDLE_META_SZ, blk_engine_cfg );
314 :
315 4763 : #define FD_PACK_METRIC_STATE_TRANSACTIONS 0
316 1436 : #define FD_PACK_METRIC_STATE_BANKS 1
317 651 : #define FD_PACK_METRIC_STATE_LEADER 2
318 1436 : #define FD_PACK_METRIC_STATE_MICROBLOCKS 3
319 :
320 : /* Updates one component of the metric state. If the state has changed,
321 : records the change. */
322 : static inline void
323 : update_metric_state( fd_pack_ctx_t * ctx,
324 : long effective_as_of,
325 : int type,
326 8286 : int status ) {
327 8286 : uint current_state = fd_uint_insert_bit( ctx->metric_state, type, status );
328 8286 : if( FD_UNLIKELY( current_state!=ctx->metric_state ) ) {
329 1992 : ctx->metric_timing[ ctx->metric_state ] += effective_as_of - ctx->metric_state_begin;
330 1992 : ctx->metric_state_begin = effective_as_of;
331 1992 : ctx->metric_state = current_state;
332 1992 : }
333 8286 : }
334 :
335 : static inline void
336 312 : remove_ib( fd_pack_ctx_t * ctx ) {
337 : /* It's likely the initializer bundle is long scheduled, but we want to
338 : try deleting it just in case. */
339 312 : if( FD_UNLIKELY( ctx->crank->enabled & ctx->crank->ib_inserted ) ) {
340 0 : ulong deleted = fd_pack_delete_transaction( ctx->pack, (fd_ed25519_sig_t const *)ctx->crank->last_sig );
341 0 : FD_MCNT_INC( PACK, TRANSACTION_DELETED, deleted );
342 0 : }
343 312 : ctx->crank->ib_inserted = 0;
344 312 : }
345 :
346 :
347 : FD_FN_CONST static inline ulong
348 69 : scratch_align( void ) {
349 69 : return 4096UL;
350 69 : }
351 :
352 : FD_FN_PURE static inline ulong
353 33 : scratch_footprint( fd_topo_tile_t const * tile ) {
354 33 : fd_pack_limits_t limits[1] = {{
355 33 : .max_cost_per_block = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_UPPER_BOUND,
356 33 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
357 33 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
358 33 : .max_data_bytes_per_block = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
359 33 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
360 33 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
361 33 : }};
362 :
363 33 : ulong l = FD_LAYOUT_INIT;
364 33 : l = FD_LAYOUT_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
365 33 : l = FD_LAYOUT_APPEND( l, fd_rng_align(), fd_rng_footprint() );
366 33 : l = FD_LAYOUT_APPEND( l, fd_pack_align(), fd_pack_footprint( tile->pack.max_pending_transactions,
367 33 : BUNDLE_META_SZ,
368 33 : tile->pack.bank_tile_count,
369 33 : limits ) );
370 : #if FD_PACK_USE_EXTRA_STORAGE
371 : l = FD_LAYOUT_APPEND( l, extra_txn_deq_align(), extra_txn_deq_footprint() );
372 : #endif
373 33 : return FD_LAYOUT_FINI( l, scratch_align() );
374 33 : }
375 :
376 : static inline void
377 : log_end_block_metrics( fd_pack_ctx_t * ctx,
378 : long now,
379 312 : char const * reason ) {
380 312 : #define DELTA( m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ] - ctx->last_sched_metrics->all[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ])
381 312 : #define AVAIL( m ) (fd_metrics_tl[ MIDX(GAUGE, PACK, AVAILABLE_TRANSACTIONS_##m) ])
382 312 : FD_LOG_INFO(( "pack_end_block(slot=%lu,%s,%lx,ticks_since_last_schedule=%ld,reasons=%lu,%lu,%lu,%lu,%lu,%lu,%lu;remaining=%lu+%lu+%lu+%lu;smallest=%lu;cus=%lu->%lu)",
383 312 : ctx->leader_slot, reason, ctx->bank_idle_bitset, now-ctx->last_sched_metrics->time,
384 312 : DELTA( TAKEN ), DELTA( CU_LIMIT ), DELTA( FAST_PATH ), DELTA( BYTE_LIMIT ), DELTA( WRITE_COST ), DELTA( SLOW_PATH ), DELTA( DEFER_SKIP ),
385 312 : AVAIL(REGULAR), AVAIL(VOTES), AVAIL(BUNDLES), AVAIL(CONFLICTING),
386 312 : (fd_metrics_tl[ MIDX(GAUGE, PACK, SMALLEST_PENDING_TRANSACTION) ]),
387 312 : (ctx->last_sched_metrics->all[ MIDX(GAUGE, PACK, CUS_CONSUMED_IN_BLOCK) ]),
388 312 : (fd_metrics_tl [ MIDX(GAUGE, PACK, CUS_CONSUMED_IN_BLOCK) ])
389 312 : ));
390 312 : #undef AVAIL
391 312 : #undef DELTA
392 312 : }
393 :
394 : static inline void
395 312 : get_done_packing( fd_pack_ctx_t * ctx, fd_done_packing_t * done_packing ) {
396 312 : done_packing->microblocks_in_slot = ctx->slot_microblock_cnt;
397 312 : fd_pack_get_block_limits( ctx->pack, done_packing->limits_usage, done_packing->limits );
398 :
399 4368 : #define DELTA( mem, m ) (fd_metrics_tl[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ] - ctx->mem->all[ MIDX(COUNTER, PACK, TRANSACTION_SCHEDULE_##m) ])
400 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_TAKEN_IDX ] = DELTA( start_block_sched_metrics, TAKEN );
401 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX ] = DELTA( start_block_sched_metrics, CU_LIMIT );
402 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX ] = DELTA( start_block_sched_metrics, FAST_PATH );
403 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX ] = DELTA( start_block_sched_metrics, BYTE_LIMIT );
404 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX ] = DELTA( start_block_sched_metrics, WRITE_COST );
405 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX ] = DELTA( start_block_sched_metrics, SLOW_PATH );
406 312 : done_packing->block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX ] = DELTA( start_block_sched_metrics, DEFER_SKIP );
407 :
408 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_TAKEN_IDX ] = DELTA( last_sched_metrics, TAKEN );
409 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_CU_LIMIT_IDX ] = DELTA( last_sched_metrics, CU_LIMIT );
410 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_FAST_PATH_IDX ] = DELTA( last_sched_metrics, FAST_PATH );
411 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_BYTE_LIMIT_IDX ] = DELTA( last_sched_metrics, BYTE_LIMIT );
412 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_WRITE_COST_IDX ] = DELTA( last_sched_metrics, WRITE_COST );
413 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_SLOW_PATH_IDX ] = DELTA( last_sched_metrics, SLOW_PATH );
414 312 : done_packing->end_block_results[ FD_METRICS_ENUM_PACK_TXN_SCHEDULE_V_DEFER_SKIP_IDX ] = DELTA( last_sched_metrics, DEFER_SKIP );
415 312 : #undef DELTA
416 :
417 312 : fd_pack_get_pending_smallest( ctx->pack, done_packing->pending_smallest, done_packing->pending_votes_smallest );
418 312 : }
419 :
420 : static inline void
421 0 : metrics_write( fd_pack_ctx_t * ctx ) {
422 0 : FD_MCNT_ENUM_COPY( PACK, TRANSACTION_INSERTED, ctx->insert_result );
423 0 : FD_MCNT_ENUM_COPY( PACK, METRIC_TIMING, ((ulong*)ctx->metric_timing) );
424 0 : FD_MCNT_ENUM_COPY( PACK, BUNDLE_CRANK_STATUS, ctx->crank->metrics );
425 0 : FD_MHIST_COPY( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS, ctx->schedule_duration );
426 0 : FD_MHIST_COPY( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS, ctx->no_sched_duration );
427 0 : FD_MHIST_COPY( PACK, INSERT_TRANSACTION_DURATION_SECONDS, ctx->insert_duration );
428 0 : FD_MHIST_COPY( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS, ctx->complete_duration );
429 :
430 0 : fd_pack_metrics_write( ctx->pack );
431 0 : }
432 :
433 : static inline void
434 6360 : during_housekeeping( fd_pack_ctx_t * ctx ) {
435 6360 : ctx->approx_wallclock_ns = fd_log_wallclock();
436 6360 : ctx->approx_tickcount = fd_tickcount();
437 :
438 6360 : if( FD_UNLIKELY( ctx->crank->enabled && fd_keyswitch_state_query( ctx->crank->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
439 0 : fd_memcpy( ctx->crank->identity_pubkey, ctx->crank->keyswitch->bytes, 32UL );
440 0 : fd_keyswitch_state( ctx->crank->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
441 0 : }
442 6360 : }
443 :
444 : static inline void
445 : before_credit( fd_pack_ctx_t * ctx,
446 : fd_stem_context_t * stem,
447 7308 : int * charge_busy ) {
448 7308 : (void)stem;
449 :
450 7308 : if( FD_UNLIKELY( (ctx->cur_spot!=NULL) & !ctx->is_bundle ) ) {
451 6 : *charge_busy = 1;
452 :
453 : /* If we were overrun while processing a frag from an in, then
454 : cur_spot is left dangling and not cleaned up, so clean it up here
455 : (by returning the slot to the pool of free slots). If the last
456 : transaction was a bundle, then we don't want to return it. When
457 : we try to process the first transaction in the next bundle, we'll
458 : see we never got the full bundle and cancel the whole last
459 : bundle, returning all the storage to the pool. */
460 : #if FD_PACK_USE_EXTRA_STORAGE
461 : if( FD_LIKELY( !ctx->insert_to_extra ) ) fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
462 : else extra_txn_deq_remove_tail( ctx->extra_txn_deq );
463 : #else
464 6 : fd_pack_insert_txn_cancel( ctx->pack, ctx->cur_spot );
465 6 : #endif
466 6 : ctx->cur_spot = NULL;
467 6 : }
468 7308 : }
469 :
470 : #if FD_PACK_USE_EXTRA_STORAGE
471 : /* insert_from_extra: helper method to pop the transaction at the head
472 : off the extra txn deque and insert it into pack. Requires that
473 : ctx->extra_txn_deq is non-empty, but it's okay to call it if pack is
474 : full. Returns the result of fd_pack_insert_txn_fini. */
475 : static inline int
476 : insert_from_extra( fd_pack_ctx_t * ctx ) {
477 : fd_txn_e_t * spot = fd_pack_insert_txn_init( ctx->pack );
478 : fd_txn_e_t const * insert = extra_txn_deq_peek_head( ctx->extra_txn_deq );
479 : fd_txn_t const * insert_txn = TXN(insert->txnp);
480 : fd_memcpy( spot->txnp->payload, insert->txnp->payload, insert->txnp->payload_sz );
481 : fd_memcpy( TXN(spot->txnp), insert_txn, fd_txn_footprint( insert_txn->instr_cnt, insert_txn->addr_table_lookup_cnt ) );
482 : fd_memcpy( spot->alt_accts, insert->alt_accts, insert_txn->addr_table_adtl_cnt*sizeof(fd_acct_addr_t) );
483 : spot->txnp->payload_sz = insert->txnp->payload_sz;
484 : spot->txnp->source_tpu = insert->txnp->source_tpu;
485 : spot->txnp->source_ipv4 = insert->txnp->source_ipv4;
486 : spot->txnp->scheduler_arrival_time_nanos = insert->txnp->scheduler_arrival_time_nanos;
487 : extra_txn_deq_remove_head( ctx->extra_txn_deq );
488 :
489 : ulong blockhash_slot = insert->txnp->blockhash_slot;
490 :
491 : ulong deleted;
492 : long insert_duration = -fd_tickcount();
493 : int result = fd_pack_insert_txn_fini( ctx->pack, spot, blockhash_slot, &deleted );
494 : insert_duration += fd_tickcount();
495 :
496 : FD_MCNT_INC( PACK, TRANSACTION_DELETED, deleted );
497 : ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
498 : fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
499 : FD_MCNT_INC( PACK, TRANSACTION_INSERTED_FROM_EXTRA, 1UL );
500 : return result;
501 : }
502 : #endif
503 :
504 : static inline void
505 : after_credit( fd_pack_ctx_t * ctx,
506 : fd_stem_context_t * stem,
507 : int * opt_poll_in,
508 7308 : int * charge_busy ) {
509 7308 : (void)opt_poll_in;
510 :
511 7308 : if( FD_UNLIKELY( (ctx->skip_cnt--)>0L ) ) return; /* It would take ages for this to hit LONG_MIN */
512 :
513 5078 : long now = fd_tickcount();
514 :
515 5078 : int pacing_bank_cnt = (int)fd_pack_pacing_enabled_bank_cnt( ctx->pacer, now );
516 :
517 5078 : ulong bank_cnt = ctx->bank_cnt;
518 :
519 :
520 : /* If any banks are busy, check one of the busy ones see if it is
521 : still busy. */
522 5078 : if( FD_LIKELY( ctx->bank_idle_bitset!=fd_ulong_mask_lsb( (int)bank_cnt ) ) ) {
523 1109 : int poll_cursor = ctx->poll_cursor;
524 1109 : ulong busy_bitset = (~ctx->bank_idle_bitset) & fd_ulong_mask_lsb( (int)bank_cnt );
525 :
526 : /* Suppose bank_cnt is 4 and idle_bitset looks something like this
527 : (pretending it's a uchar):
528 : 0000 1001
529 : ^ busy cursor is 1
530 : Then busy_bitset is
531 : 0000 0110
532 : Rotate it right by 2 bits
533 : 1000 0001
534 : Find lsb returns 0, so busy cursor remains 2, and we poll bank 2.
535 :
536 : If instead idle_bitset were
537 : 0000 1110
538 : ^
539 : The rotated version would be
540 : 0100 0000
541 : Find lsb will return 6, so busy cursor would be set to 0, and
542 : we'd poll bank 0, which is the right one. */
543 1109 : poll_cursor++;
544 1109 : poll_cursor = (poll_cursor + fd_ulong_find_lsb( fd_ulong_rotate_right( busy_bitset, (poll_cursor&63) ) )) & 63;
545 :
546 1109 : if( FD_UNLIKELY(
547 : /* if microblock duration is 0, bypass the bank_ready_at check
548 : to avoid a potential cache miss. Can't use an ifdef here
549 : because FD_UNLIKELY is a macro, but the compiler should
550 : eliminate the check easily. */
551 1109 : ( (MICROBLOCK_DURATION_NS==0L) || (ctx->bank_ready_at[poll_cursor]<now) ) &&
552 1109 : (fd_fseq_query( ctx->bank_current[poll_cursor] )==ctx->bank_expect[poll_cursor]) ) ) {
553 1109 : *charge_busy = 1;
554 1109 : ctx->bank_idle_bitset |= 1UL<<poll_cursor;
555 :
556 1109 : long complete_duration = -fd_tickcount();
557 1109 : int completed = fd_pack_microblock_complete( ctx->pack, (ulong)poll_cursor );
558 1109 : complete_duration += fd_tickcount();
559 1109 : if( FD_LIKELY( completed ) ) fd_histf_sample( ctx->complete_duration, (ulong)complete_duration );
560 1109 : }
561 :
562 1109 : ctx->poll_cursor = poll_cursor;
563 1109 : }
564 :
565 :
566 : /* If we time out on our slot, then stop being leader. This can only
567 : happen in the first after_credit after a housekeeping. */
568 5078 : if( FD_UNLIKELY( ctx->approx_wallclock_ns>=ctx->slot_end_ns && ctx->leader_slot!=ULONG_MAX ) ) {
569 312 : *charge_busy = 1;
570 :
571 312 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
572 312 : get_done_packing( ctx, done_packing );
573 :
574 312 : fd_stem_publish( stem, 1UL, fd_disco_bank_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() ) );
575 312 : 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 );
576 312 : ctx->pack_idx++;
577 :
578 312 : log_end_block_metrics( ctx, now, "time" );
579 312 : ctx->drain_banks = 1;
580 312 : ctx->leader_slot = ULONG_MAX;
581 312 : ctx->slot_microblock_cnt = 0UL;
582 312 : fd_pack_end_block( ctx->pack );
583 312 : remove_ib( ctx );
584 :
585 312 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER, 0 );
586 312 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS, 0 );
587 312 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, 0 );
588 312 : return;
589 312 : }
590 :
591 : /* Am I leader? If not, see about inserting at most one transaction
592 : from extra storage. It's important not to insert too many
593 : transactions here, or we won't end up servicing dedup_pack enough.
594 : If extra storage is empty or pack is full, do nothing. */
595 4766 : if( FD_UNLIKELY( ctx->leader_slot==ULONG_MAX ) ) {
596 : #if FD_PACK_USE_EXTRA_STORAGE
597 : if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) &&
598 : fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
599 : *charge_busy = 1;
600 :
601 : int result = insert_from_extra( ctx );
602 : if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
603 : }
604 : #endif
605 3582 : return;
606 3582 : }
607 :
608 : /* Am I in drain mode? If so, check if I can exit it */
609 1184 : if( FD_UNLIKELY( ctx->drain_banks ) ) {
610 312 : if( FD_LIKELY( ctx->bank_idle_bitset==fd_ulong_mask_lsb( (int)bank_cnt ) ) ) {
611 312 : ctx->drain_banks = 0;
612 :
613 : /* Pack notifies poh when banks are drained so that poh can
614 : relinquish pack's ownership over the slot bank (by decrementing
615 : its Arc). We do this by sending a ULONG_MAX sig over the
616 : pack_poh mcache.
617 :
618 : TODO: This is only needed for Frankendancer, not Firedancer,
619 : which manages bank lifetime different. */
620 312 : fd_stem_publish( stem, 1UL, ULONG_MAX, 0UL, 0UL, 0UL, 0UL, fd_frag_meta_ts_comp( fd_tickcount() ) );
621 312 : } else {
622 0 : return;
623 0 : }
624 312 : }
625 :
626 : /* Have I sent the max allowed microblocks? Nothing to do. */
627 1184 : if( FD_UNLIKELY( ctx->slot_microblock_cnt>=ctx->slot_max_microblocks ) ) return;
628 :
629 : /* Do I have enough transactions and/or have I waited enough time? */
630 1184 : if( FD_UNLIKELY( (ulong)(now-ctx->last_successful_insert) <
631 1184 : ctx->wait_duration_ticks[ fd_ulong_min( fd_pack_avail_txn_cnt( ctx->pack ), MAX_TXN_PER_MICROBLOCK ) ] ) ) {
632 60 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, 0 );
633 60 : return;
634 60 : }
635 :
636 1124 : int any_ready = 0;
637 1124 : int any_scheduled = 0;
638 :
639 1124 : *charge_busy = 1;
640 :
641 1124 : if( FD_LIKELY( ctx->crank->enabled ) ) {
642 1124 : block_builder_info_t const * top_meta = fd_pack_peek_bundle_meta( ctx->pack );
643 1124 : if( FD_UNLIKELY( top_meta ) ) {
644 : /* Have bundles, in a reasonable state to crank. */
645 :
646 15 : fd_txn_e_t * _bundle[ 1UL ];
647 15 : fd_txn_e_t * const * bundle = fd_pack_insert_bundle_init( ctx->pack, _bundle, 1UL );
648 :
649 15 : ulong txn_sz = fd_bundle_crank_generate( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
650 15 : ctx->crank->identity_pubkey, ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission,
651 15 : bundle[0]->txnp->payload, TXN( bundle[0]->txnp ) );
652 :
653 15 : if( FD_LIKELY( txn_sz==0UL ) ) { /* Everything in good shape! */
654 6 : fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
655 6 : fd_pack_set_initializer_bundles_ready( ctx->pack );
656 6 : ctx->crank->metrics[ 0 ]++; /* BUNDLE_CRANK_STATUS_NOT_NEEDED */
657 6 : }
658 9 : else if( FD_LIKELY( txn_sz<ULONG_MAX ) ) {
659 9 : bundle[0]->txnp->payload_sz = (ushort)txn_sz;
660 9 : bundle[0]->txnp->source_tpu = FD_TXN_M_TPU_SOURCE_BUNDLE;
661 9 : bundle[0]->txnp->source_ipv4 = 0; /* not applicable */
662 9 : bundle[0]->txnp->scheduler_arrival_time_nanos = ctx->approx_wallclock_ns + (long)((double)(fd_tickcount() - ctx->approx_tickcount) / ctx->ticks_per_ns);
663 9 : memcpy( bundle[0]->txnp->payload+TXN(bundle[0]->txnp)->recent_blockhash_off, ctx->crank->recent_blockhash, 32UL );
664 :
665 9 : fd_keyguard_client_sign( ctx->crank->keyguard_client, bundle[0]->txnp->payload+1UL,
666 9 : bundle[0]->txnp->payload+65UL, txn_sz-65UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
667 :
668 9 : memcpy( ctx->crank->last_sig, bundle[0]->txnp->payload+1UL, 64UL );
669 :
670 9 : ctx->crank->ib_inserted = 1;
671 9 : ulong deleted;
672 9 : int retval = fd_pack_insert_bundle_fini( ctx->pack, bundle, 1UL, ctx->leader_slot-1UL, 1, NULL, &deleted );
673 9 : FD_MCNT_INC( PACK, TRANSACTION_DELETED, deleted );
674 9 : ctx->insert_result[ retval + FD_PACK_INSERT_RETVAL_OFF ]++;
675 9 : if( FD_UNLIKELY( retval<0 ) ) {
676 0 : ctx->crank->metrics[ 3 ]++; /* BUNDLE_CRANK_STATUS_INSERTION_FAILED */
677 0 : FD_LOG_WARNING(( "inserting initializer bundle returned %i", retval ));
678 9 : } else {
679 : /* Update the cached copy of the on-chain state. This seems a
680 : little dangerous, since we're updating it as if the bundle
681 : succeeded without knowing if that's true, but here's why
682 : it's safe:
683 :
684 : From now until we get the rebate call for this initializer
685 : bundle (which lets us know if it succeeded or failed), pack
686 : will be in [Pending] state, which means peek_bundle_meta
687 : will return NULL, so we won't read this state.
688 :
689 : Then, if the initializer bundle failed, we'll go into
690 : [Failed] IB state until the end of the block, which will
691 : cause top_meta to remain NULL so we don't read these values
692 : again.
693 :
694 : Otherwise, the initializer bundle succeeded, which means
695 : that these are the right values to use. */
696 9 : fd_bundle_crank_apply( ctx->crank->gen, ctx->crank->prev_config, top_meta->commission_pubkey,
697 9 : ctx->crank->tip_receiver_owner, ctx->crank->epoch, top_meta->commission );
698 9 : ctx->crank->metrics[ 1 ]++; /* BUNDLE_CRANK_STATUS_INSERTED */
699 9 : }
700 9 : } else {
701 : /* Already logged a warning in this case */
702 0 : fd_pack_insert_bundle_cancel( ctx->pack, bundle, 1UL );
703 0 : ctx->crank->metrics[ 2 ]++; /* BUNDLE_CRANK_STATUS_CREATION_FAILED' */
704 0 : }
705 15 : }
706 1124 : }
707 :
708 : /* Try to schedule the next microblock. */
709 1124 : if( FD_LIKELY( ctx->bank_idle_bitset ) ) { /* Optimize for schedule */
710 1124 : any_ready = 1;
711 :
712 1124 : int i = fd_ulong_find_lsb( ctx->bank_idle_bitset );
713 :
714 1124 : int flags;
715 :
716 1124 : switch( ctx->strategy ) {
717 0 : default:
718 1124 : case FD_PACK_STRATEGY_PERF:
719 1124 : flags = FD_PACK_SCHEDULE_VOTE | FD_PACK_SCHEDULE_BUNDLE | FD_PACK_SCHEDULE_TXN;
720 1124 : break;
721 0 : case FD_PACK_STRATEGY_BALANCED:
722 : /* We want to exempt votes from pacing, so we always allow
723 : scheduling votes. It doesn't really make much sense to pace
724 : bundles, because they get scheduled in FIFO order. However,
725 : we keep pacing for normal transactions. For example, if
726 : pacing_bank_cnt is 0, then pack won't schedule normal
727 : transactions to any bank tile. */
728 0 : flags = FD_PACK_SCHEDULE_VOTE | fd_int_if( i==0, FD_PACK_SCHEDULE_BUNDLE, 0 )
729 0 : | fd_int_if( i<pacing_bank_cnt, FD_PACK_SCHEDULE_TXN, 0 );
730 0 : break;
731 0 : case FD_PACK_STRATEGY_BUNDLE:
732 0 : flags = FD_PACK_SCHEDULE_VOTE | FD_PACK_SCHEDULE_BUNDLE
733 0 : | fd_int_if( ctx->slot_end_ns - ctx->approx_wallclock_ns<50000000L, FD_PACK_SCHEDULE_TXN, 0 );
734 0 : break;
735 1124 : }
736 :
737 1124 : fd_txn_p_t * microblock_dst = fd_chunk_to_laddr( ctx->bank_out_mem, ctx->bank_out_chunk );
738 1124 : long schedule_duration = -fd_tickcount();
739 1124 : ulong schedule_cnt = fd_pack_schedule_next_microblock( ctx->pack, CUS_PER_MICROBLOCK, VOTE_FRACTION, (ulong)i, flags, microblock_dst );
740 1124 : schedule_duration += fd_tickcount();
741 1124 : fd_histf_sample( (schedule_cnt>0UL) ? ctx->schedule_duration : ctx->no_sched_duration, (ulong)schedule_duration );
742 :
743 1124 : if( FD_LIKELY( schedule_cnt ) ) {
744 1124 : any_scheduled = 1;
745 1124 : long now2 = fd_tickcount();
746 1124 : ulong tsorig = (ulong)fd_frag_meta_ts_comp( now ); /* A bound on when we observed bank was idle */
747 1124 : ulong tspub = (ulong)fd_frag_meta_ts_comp( now2 );
748 1124 : ulong chunk = ctx->bank_out_chunk;
749 1124 : ulong msg_sz = schedule_cnt*sizeof(fd_txn_p_t);
750 1124 : fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t*)(microblock_dst+schedule_cnt);
751 1124 : trailer->bank = ctx->leader_bank;
752 1124 : trailer->bank_idx = ctx->leader_bank_idx;
753 1124 : trailer->microblock_idx = ctx->slot_microblock_cnt;
754 1124 : trailer->pack_idx = ctx->pack_idx;
755 1124 : trailer->pack_txn_idx = ctx->pack_txn_cnt;
756 1124 : trailer->is_bundle = !!(microblock_dst->flags & FD_TXN_P_FLAGS_BUNDLE);
757 :
758 1124 : ulong sig = fd_disco_poh_sig( ctx->leader_slot, POH_PKT_TYPE_MICROBLOCK, (ulong)i );
759 1124 : fd_stem_publish( stem, 0UL, sig, chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), 0UL, tsorig, tspub );
760 1124 : ctx->bank_expect[ i ] = stem->seqs[0]-1UL;
761 1124 : ctx->bank_ready_at[i] = now2 + (long)ctx->microblock_duration_ticks;
762 1124 : ctx->bank_out_chunk = fd_dcache_compact_next( ctx->bank_out_chunk, msg_sz+sizeof(fd_microblock_bank_trailer_t), ctx->bank_out_chunk0, ctx->bank_out_wmark );
763 1124 : ctx->slot_microblock_cnt += fd_ulong_if( trailer->is_bundle, schedule_cnt, 1UL );
764 1124 : ctx->pack_idx += fd_uint_if( trailer->is_bundle, (uint)schedule_cnt, 1U );
765 1124 : ctx->pack_txn_cnt += schedule_cnt;
766 :
767 1124 : ctx->bank_idle_bitset = fd_ulong_pop_lsb( ctx->bank_idle_bitset );
768 1124 : ctx->skip_cnt = (long)schedule_cnt * fd_long_if( ctx->use_consumed_cus, (long)bank_cnt/2L, 1L );
769 1124 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now2 );
770 :
771 1124 : memcpy( ctx->last_sched_metrics->all, (ulong const *)fd_metrics_tl, sizeof(ctx->last_sched_metrics->all) );
772 1124 : ctx->last_sched_metrics->time = now2;
773 :
774 : /* If we're using CU rebates, then we have one in for each bank in
775 : addition to the two normal ones. We want to skip schedule attempts
776 : for (bank_cnt + 1) link polls after a successful schedule attempt.
777 : */
778 1124 : fd_long_store_if( ctx->use_consumed_cus, &(ctx->skip_cnt), (long)(ctx->bank_cnt + 1) );
779 1124 : }
780 1124 : }
781 :
782 1124 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS, any_ready );
783 1124 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, any_scheduled );
784 1124 : now = fd_tickcount();
785 1124 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
786 :
787 : #if FD_PACK_USE_EXTRA_STORAGE
788 : if( FD_UNLIKELY( !extra_txn_deq_empty( ctx->extra_txn_deq ) ) ) {
789 : /* Don't start pulling from the extra storage until the available
790 : transaction count drops below half. */
791 : ulong avail_space = (ulong)fd_long_max( 0L, (long)(ctx->max_pending_transactions>>1)-(long)fd_pack_avail_txn_cnt( ctx->pack ) );
792 : ulong qty_to_insert = fd_ulong_min( 10UL, fd_ulong_min( extra_txn_deq_cnt( ctx->extra_txn_deq ), avail_space ) );
793 : int any_successes = 0;
794 : for( ulong i=0UL; i<qty_to_insert; i++ ) any_successes |= (0<=insert_from_extra( ctx ));
795 : if( FD_LIKELY( any_successes ) ) ctx->last_successful_insert = now;
796 : }
797 : #endif
798 :
799 : /* Did we send the maximum allowed microblocks? Then end the slot. */
800 1124 : if( FD_UNLIKELY( ctx->slot_microblock_cnt==ctx->slot_max_microblocks )) {
801 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_LEADER, 0 );
802 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_BANKS, 0 );
803 0 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_MICROBLOCKS, 0 );
804 : /* The pack object also does this accounting and increases this
805 : metric, but we end the slot early so won't see it unless we also
806 : increment it here. */
807 0 : FD_MCNT_INC( PACK, MICROBLOCK_PER_BLOCK_LIMIT, 1UL );
808 0 : log_end_block_metrics( ctx, now, "microblock" );
809 :
810 0 : fd_done_packing_t * done_packing = fd_chunk_to_laddr( ctx->poh_out_mem, ctx->poh_out_chunk );
811 0 : get_done_packing( ctx, done_packing );
812 :
813 0 : fd_stem_publish( stem, 1UL, fd_disco_bank_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() ) );
814 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 );
815 0 : ctx->pack_idx++;
816 :
817 0 : ctx->drain_banks = 1;
818 0 : ctx->leader_slot = ULONG_MAX;
819 0 : ctx->slot_microblock_cnt = 0UL;
820 0 : fd_pack_end_block( ctx->pack );
821 0 : remove_ib( ctx );
822 :
823 0 : }
824 1124 : }
825 :
826 :
827 : /* At this point, we have started receiving frag seq with details in
828 : mline at time now. Speculatively process it here. */
829 :
830 : static inline void
831 : during_frag( fd_pack_ctx_t * ctx,
832 : ulong in_idx,
833 : ulong seq FD_PARAM_UNUSED,
834 : ulong sig,
835 : ulong chunk,
836 : ulong sz,
837 3591 : ulong ctl FD_PARAM_UNUSED ) {
838 :
839 3591 : uchar const * dcache_entry = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
840 :
841 3591 : switch( ctx->in_kind[ in_idx ] ) {
842 0 : case IN_KIND_REPLAY: {
843 0 : if( FD_LIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
844 :
845 : /* There was a leader transition. Handle it. */
846 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
847 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 ));
848 :
849 0 : fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
850 0 : return;
851 0 : }
852 339 : case IN_KIND_POH: {
853 : /* Not interested in stamped microblocks, only leader updates. */
854 339 : if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
855 :
856 : /* There was a leader transition. Handle it. */
857 339 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz!=sizeof(fd_became_leader_t) ) )
858 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 ));
859 :
860 339 : fd_memcpy( ctx->_became_leader, dcache_entry, sizeof(fd_became_leader_t) );
861 339 : return;
862 339 : }
863 9 : case IN_KIND_BANK: {
864 9 : FD_TEST( ctx->use_consumed_cus );
865 : /* For a previous slot */
866 9 : if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
867 :
868 9 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz<FD_PACK_REBATE_MIN_SZ
869 9 : || sz>FD_PACK_REBATE_MAX_SZ ) )
870 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 ));
871 :
872 9 : ctx->pending_rebate_sz = sz;
873 9 : fd_memcpy( ctx->rebate, dcache_entry, sz );
874 9 : return;
875 9 : }
876 3243 : case IN_KIND_RESOLV: {
877 3243 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>FD_TPU_RESOLVED_MTU ) )
878 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 ));
879 :
880 3243 : fd_txn_m_t * txnm = (fd_txn_m_t *)dcache_entry;
881 3243 : ulong payload_sz = txnm->payload_sz;
882 3243 : ulong txn_t_sz = txnm->txn_t_sz;
883 3243 : uint source_ipv4 = txnm->source_ipv4;
884 3243 : uchar source_tpu = txnm->source_tpu;
885 3243 : FD_TEST( payload_sz<=FD_TPU_MTU );
886 3243 : FD_TEST( txn_t_sz <=FD_TXN_MAX_SZ );
887 3243 : fd_txn_t * txn = fd_txn_m_txn_t( txnm );
888 :
889 3243 : ulong addr_table_sz = 32UL*txn->addr_table_adtl_cnt;
890 3243 : FD_TEST( addr_table_sz<=32UL*FD_TXN_ACCT_ADDR_MAX );
891 :
892 3243 : if( FD_UNLIKELY( (ctx->leader_slot==ULONG_MAX) & (sig>ctx->highest_observed_slot) ) ) {
893 : /* Using the resolv tile's knowledge of the current slot is a bit
894 : of a hack, since we don't get any info if there are no
895 : transactions and we're not leader. We're actually in exactly
896 : the case where that's okay though. The point of calling
897 : expire_before long before we become leader is so that we don't
898 : drop new but low-fee-paying transactions when pack is clogged
899 : with expired but high-fee-paying transactions. That can only
900 : happen if we are getting transactions. */
901 0 : ctx->highest_observed_slot = sig;
902 0 : ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->highest_observed_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
903 0 : FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
904 0 : }
905 :
906 :
907 3243 : ulong bundle_id = txnm->block_engine.bundle_id;
908 3243 : if( FD_UNLIKELY( bundle_id ) ) {
909 63 : ctx->is_bundle = 1;
910 63 : if( FD_LIKELY( bundle_id!=ctx->current_bundle->id ) ) {
911 15 : if( FD_UNLIKELY( ctx->current_bundle->bundle ) ) {
912 6 : FD_MCNT_INC( PACK, TRANSACTION_DROPPED_PARTIAL_BUNDLE, ctx->current_bundle->txn_received );
913 6 : fd_pack_insert_bundle_cancel( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt );
914 6 : }
915 15 : ctx->current_bundle->id = bundle_id;
916 15 : ctx->current_bundle->txn_cnt = txnm->block_engine.bundle_txn_cnt;
917 15 : ctx->current_bundle->min_blockhash_slot = ULONG_MAX;
918 15 : ctx->current_bundle->txn_received = 0UL;
919 :
920 15 : if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) {
921 0 : FD_MCNT_INC( PACK, TRANSACTION_DROPPED_PARTIAL_BUNDLE, 1UL );
922 0 : ctx->current_bundle->id = 0UL;
923 0 : return;
924 0 : }
925 15 : ctx->blk_engine_cfg->commission = txnm->block_engine.commission;
926 15 : memcpy( ctx->blk_engine_cfg->commission_pubkey->b, txnm->block_engine.commission_pubkey, 32UL );
927 :
928 15 : ctx->current_bundle->bundle = fd_pack_insert_bundle_init( ctx->pack, ctx->current_bundle->_txn, ctx->current_bundle->txn_cnt );
929 15 : }
930 63 : ctx->cur_spot = ctx->current_bundle->bundle[ ctx->current_bundle->txn_received ];
931 63 : ctx->current_bundle->min_blockhash_slot = fd_ulong_min( ctx->current_bundle->min_blockhash_slot, sig );
932 3180 : } else {
933 3180 : ctx->is_bundle = 0;
934 : #if FD_PACK_USE_EXTRA_STORAGE
935 : if( FD_LIKELY( ctx->leader_slot!=ULONG_MAX || fd_pack_avail_txn_cnt( ctx->pack )<ctx->max_pending_transactions ) ) {
936 : ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
937 : ctx->insert_to_extra = 0;
938 : } else {
939 : if( FD_UNLIKELY( extra_txn_deq_full( ctx->extra_txn_deq ) ) ) {
940 : extra_txn_deq_remove_head( ctx->extra_txn_deq );
941 : FD_MCNT_INC( PACK, TRANSACTION_DROPPED_FROM_EXTRA, 1UL );
942 : }
943 : ctx->cur_spot = extra_txn_deq_peek_tail( extra_txn_deq_insert_tail( ctx->extra_txn_deq ) );
944 : /* We want to store the current time in cur_spot so that we can
945 : track its expiration better. We just stash it in the CU
946 : fields, since those aren't important right now. */
947 : ctx->cur_spot->txnp->blockhash_slot = sig;
948 : ctx->insert_to_extra = 1;
949 : FD_MCNT_INC( PACK, TRANSACTION_INSERTED_TO_EXTRA, 1UL );
950 : }
951 : #else
952 3180 : ctx->cur_spot = fd_pack_insert_txn_init( ctx->pack );
953 3180 : #endif
954 3180 : }
955 :
956 : /* We get transactions from the resolv tile.
957 : The transactions should have been parsed and verified. */
958 3243 : FD_MCNT_INC( PACK, NORMAL_TRANSACTION_RECEIVED, 1UL );
959 :
960 3243 : fd_memcpy( ctx->cur_spot->txnp->payload, fd_txn_m_payload( txnm ), payload_sz );
961 3243 : fd_memcpy( TXN(ctx->cur_spot->txnp), txn, txn_t_sz );
962 3243 : fd_memcpy( ctx->cur_spot->alt_accts, fd_txn_m_alut( txnm ), addr_table_sz );
963 3243 : ctx->cur_spot->txnp->scheduler_arrival_time_nanos = ctx->approx_wallclock_ns + (long)((double)(fd_tickcount() - ctx->approx_tickcount) / ctx->ticks_per_ns);
964 3243 : ctx->cur_spot->txnp->payload_sz = payload_sz;
965 3243 : ctx->cur_spot->txnp->source_ipv4 = source_ipv4;
966 3243 : ctx->cur_spot->txnp->source_tpu = source_tpu;
967 :
968 3243 : break;
969 3243 : }
970 0 : case IN_KIND_EXECUTED_TXN: {
971 0 : FD_TEST( sz==64UL );
972 0 : fd_memcpy( ctx->executed_txn_sig, dcache_entry, sz );
973 0 : break;
974 0 : }
975 3591 : }
976 3591 : }
977 :
978 :
979 : /* After the transaction has been fully received, and we know we were
980 : not overrun while reading it, insert it into pack. */
981 :
982 : static inline void
983 : after_frag( fd_pack_ctx_t * ctx,
984 : ulong in_idx,
985 : ulong seq,
986 : ulong sig,
987 : ulong sz,
988 : ulong tsorig,
989 : ulong tspub,
990 3579 : fd_stem_context_t * stem ) {
991 3579 : (void)seq;
992 3579 : (void)sz;
993 3579 : (void)tsorig;
994 3579 : (void)tspub;
995 3579 : (void)stem;
996 :
997 3579 : long now = fd_tickcount();
998 :
999 3579 : ulong leader_slot = ULONG_MAX;
1000 3579 : switch( ctx->in_kind[ in_idx ] ) {
1001 0 : case IN_KIND_REPLAY:
1002 0 : if( FD_UNLIKELY( sig!=REPLAY_SIG_BECAME_LEADER ) ) return;
1003 0 : leader_slot = ctx->_became_leader->slot;
1004 :
1005 0 : memcpy( ctx->start_block_sched_metrics->all, (ulong const *)fd_metrics_tl, sizeof(ctx->start_block_sched_metrics->all) );
1006 0 : ctx->start_block_sched_metrics->time = now;
1007 0 : break;
1008 339 : case IN_KIND_POH:
1009 339 : if( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_BECAME_LEADER ) return;
1010 339 : leader_slot = fd_disco_poh_sig_slot( sig );
1011 339 : break;
1012 3240 : default:
1013 3240 : break;
1014 3579 : }
1015 :
1016 3579 : switch( ctx->in_kind[ in_idx ] ) {
1017 0 : case IN_KIND_REPLAY:
1018 339 : case IN_KIND_POH: {
1019 339 : long now_ticks = fd_tickcount();
1020 339 : long now_ns = fd_log_wallclock();
1021 :
1022 339 : if( FD_UNLIKELY( 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 );
1025 :
1026 0 : fd_stem_publish( stem, 1UL, fd_disco_bank_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() ) );
1027 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 );
1028 0 : ctx->pack_idx++;
1029 :
1030 0 : FD_LOG_WARNING(( "switching to slot %lu while packing for slot %lu. Draining bank tiles.", leader_slot, ctx->leader_slot ));
1031 0 : log_end_block_metrics( ctx, now_ticks, "switch" );
1032 0 : ctx->drain_banks = 1;
1033 0 : ctx->leader_slot = ULONG_MAX;
1034 0 : ctx->slot_microblock_cnt = 0UL;
1035 0 : fd_pack_end_block( ctx->pack );
1036 0 : remove_ib( ctx );
1037 0 : }
1038 339 : ctx->leader_slot = leader_slot;
1039 :
1040 339 : ulong exp_cnt = fd_pack_expire_before( ctx->pack, fd_ulong_max( ctx->leader_slot, TRANSACTION_LIFETIME_SLOTS )-TRANSACTION_LIFETIME_SLOTS );
1041 339 : FD_MCNT_INC( PACK, TRANSACTION_EXPIRED, exp_cnt );
1042 :
1043 339 : ctx->leader_bank = ctx->_became_leader->bank;
1044 339 : ctx->leader_bank_idx = ctx->_became_leader->bank_idx;
1045 339 : ctx->slot_max_microblocks = ctx->_became_leader->max_microblocks_in_slot;
1046 : /* Reserve some space in the block for ticks */
1047 339 : ctx->slot_max_data = (ctx->larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK)
1048 339 : - 48UL*(ctx->_became_leader->ticks_per_slot+ctx->_became_leader->total_skipped_ticks);
1049 :
1050 339 : ctx->limits.slot_max_cost = ctx->_became_leader->limits.slot_max_cost;
1051 339 : ctx->limits.slot_max_vote_cost = ctx->_became_leader->limits.slot_max_vote_cost;
1052 339 : ctx->limits.slot_max_write_cost_per_acct = ctx->_became_leader->limits.slot_max_write_cost_per_acct;
1053 :
1054 : /* ticks_per_ns is probably relatively stable over 400ms, but not
1055 : over several hours, so we need to compute the slot duration in
1056 : milliseconds first and then convert to ticks. This doesn't need
1057 : to be super accurate, but we don't want it to vary wildly. */
1058 339 : long end_ticks = now_ticks + (long)((double)fd_long_max( ctx->_became_leader->slot_end_ns - now_ns, 1L )*ctx->ticks_per_ns);
1059 : /* We may still get overrun, but then we'll never use this and just
1060 : reinitialize it the next time when we actually become leader. */
1061 339 : fd_pack_pacing_init( ctx->pacer, now_ticks, end_ticks, (float)ctx->ticks_per_ns, ctx->limits.slot_max_cost );
1062 :
1063 339 : if( FD_UNLIKELY( ctx->crank->enabled ) ) {
1064 : /* If we get overrun, we'll just never use these values, but the
1065 : old values aren't really useful either. */
1066 339 : ctx->crank->epoch = ctx->_became_leader->epoch;
1067 339 : *(ctx->crank->prev_config) = *(ctx->_became_leader->bundle->config);
1068 339 : memcpy( ctx->crank->recent_blockhash, ctx->_became_leader->bundle->last_blockhash, 32UL );
1069 339 : memcpy( ctx->crank->tip_receiver_owner, ctx->_became_leader->bundle->tip_receiver_owner, 32UL );
1070 339 : }
1071 :
1072 339 : FD_LOG_INFO(( "pack_became_leader(slot=%lu,ends_at=%ld)", ctx->leader_slot, ctx->_became_leader->slot_end_ns ));
1073 :
1074 339 : update_metric_state( ctx, fd_tickcount(), FD_PACK_METRIC_STATE_LEADER, 1 );
1075 :
1076 339 : ctx->slot_end_ns = ctx->_became_leader->slot_end_ns;
1077 339 : fd_pack_limits_t limits[ 1 ];
1078 339 : limits->max_cost_per_block = ctx->limits.slot_max_cost;
1079 339 : limits->max_data_bytes_per_block = ctx->slot_max_data;
1080 339 : limits->max_microblocks_per_block = ctx->slot_max_microblocks;
1081 339 : limits->max_vote_cost_per_block = ctx->limits.slot_max_vote_cost;
1082 339 : limits->max_write_cost_per_acct = ctx->limits.slot_max_write_cost_per_acct;
1083 339 : limits->max_txn_per_microblock = ULONG_MAX; /* unused */
1084 339 : fd_pack_set_block_limits( ctx->pack, limits );
1085 339 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
1086 :
1087 339 : break;
1088 0 : }
1089 9 : case IN_KIND_BANK: {
1090 : /* For a previous slot */
1091 9 : if( FD_UNLIKELY( sig!=ctx->leader_slot ) ) return;
1092 :
1093 9 : fd_pack_rebate_cus( ctx->pack, ctx->rebate->rebate );
1094 9 : ctx->pending_rebate_sz = 0UL;
1095 9 : fd_pack_pacing_update_consumed_cus( ctx->pacer, fd_pack_current_block_cost( ctx->pack ), now );
1096 9 : break;
1097 9 : }
1098 3231 : case IN_KIND_RESOLV: {
1099 : /* Normal transaction case */
1100 : #if FD_PACK_USE_EXTRA_STORAGE
1101 : if( FD_LIKELY( !ctx->insert_to_extra ) ) {
1102 : #else
1103 3231 : if( 1 ) {
1104 3231 : #endif
1105 3231 : if( FD_UNLIKELY( ctx->is_bundle ) ) {
1106 57 : if( FD_UNLIKELY( ctx->current_bundle->txn_cnt==0UL ) ) return;
1107 57 : if( FD_UNLIKELY( ++(ctx->current_bundle->txn_received)==ctx->current_bundle->txn_cnt ) ) {
1108 9 : ulong deleted;
1109 9 : long insert_duration = -fd_tickcount();
1110 9 : int result = fd_pack_insert_bundle_fini( ctx->pack, ctx->current_bundle->bundle, ctx->current_bundle->txn_cnt, ctx->current_bundle->min_blockhash_slot, 0, ctx->blk_engine_cfg, &deleted );
1111 9 : insert_duration += fd_tickcount();
1112 9 : FD_MCNT_INC( PACK, TRANSACTION_DELETED, deleted );
1113 9 : ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ] += ctx->current_bundle->txn_received;
1114 9 : fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
1115 9 : ctx->current_bundle->bundle = NULL;
1116 9 : }
1117 3174 : } else {
1118 3174 : ulong blockhash_slot = sig;
1119 3174 : ulong deleted;
1120 3174 : long insert_duration = -fd_tickcount();
1121 3174 : int result = fd_pack_insert_txn_fini( ctx->pack, ctx->cur_spot, blockhash_slot, &deleted );
1122 3174 : insert_duration += fd_tickcount();
1123 3174 : FD_MCNT_INC( PACK, TRANSACTION_DELETED, deleted );
1124 3174 : ctx->insert_result[ result + FD_PACK_INSERT_RETVAL_OFF ]++;
1125 3174 : fd_histf_sample( ctx->insert_duration, (ulong)insert_duration );
1126 3174 : if( FD_LIKELY( result>=0 ) ) ctx->last_successful_insert = now;
1127 3174 : }
1128 3231 : }
1129 :
1130 3231 : ctx->cur_spot = NULL;
1131 3231 : break;
1132 3231 : }
1133 0 : case IN_KIND_EXECUTED_TXN: {
1134 0 : ulong deleted = fd_pack_delete_transaction( ctx->pack, fd_type_pun( ctx->executed_txn_sig ) );
1135 0 : FD_MCNT_INC( PACK, TRANSACTION_ALREADY_EXECUTED, deleted );
1136 0 : break;
1137 3231 : }
1138 3579 : }
1139 :
1140 3579 : update_metric_state( ctx, now, FD_PACK_METRIC_STATE_TRANSACTIONS, fd_pack_avail_txn_cnt( ctx->pack )>0 );
1141 3579 : }
1142 :
1143 : static void
1144 : privileged_init( fd_topo_t * topo,
1145 0 : fd_topo_tile_t * tile ) {
1146 0 : if( FD_LIKELY( !tile->pack.bundle.enabled ) ) return;
1147 0 : if( FD_UNLIKELY( !tile->pack.bundle.vote_account_path[0] ) ) {
1148 0 : FD_LOG_WARNING(( "Disabling bundle crank because no vote account was specified" ));
1149 0 : return;
1150 0 : }
1151 :
1152 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1153 :
1154 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1155 0 : fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
1156 :
1157 0 : if( FD_UNLIKELY( !strcmp( tile->pack.bundle.identity_key_path, "" ) ) )
1158 0 : FD_LOG_ERR(( "identity_key_path not set" ));
1159 :
1160 0 : const uchar * identity_key = fd_keyload_load( tile->pack.bundle.identity_key_path, /* pubkey only: */ 1 );
1161 0 : fd_memcpy( ctx->crank->identity_pubkey->b, identity_key, 32UL );
1162 :
1163 0 : if( FD_UNLIKELY( !fd_base58_decode_32( tile->pack.bundle.vote_account_path, ctx->crank->vote_pubkey->b ) ) ) {
1164 0 : const uchar * vote_key = fd_keyload_load( tile->pack.bundle.vote_account_path, /* pubkey only: */ 1 );
1165 0 : fd_memcpy( ctx->crank->vote_pubkey->b, vote_key, 32UL );
1166 0 : }
1167 0 : }
1168 :
1169 : static void
1170 : unprivileged_init( fd_topo_t * topo,
1171 30 : fd_topo_tile_t * tile ) {
1172 30 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
1173 :
1174 30 : 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 ));
1175 :
1176 30 : fd_pack_limits_t limits_upper[1] = {{
1177 30 : .max_cost_per_block = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_UPPER_BOUND,
1178 30 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_UPPER_BOUND,
1179 30 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_UPPER_BOUND,
1180 30 : .max_data_bytes_per_block = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
1181 30 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
1182 30 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
1183 30 : }};
1184 :
1185 30 : ulong pack_footprint = fd_pack_footprint( tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.bank_tile_count, limits_upper );
1186 :
1187 30 : FD_SCRATCH_ALLOC_INIT( l, scratch );
1188 30 : fd_pack_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_pack_ctx_t ), sizeof( fd_pack_ctx_t ) );
1189 30 : fd_rng_t * rng = fd_rng_join( fd_rng_new( FD_SCRATCH_ALLOC_APPEND( l, fd_rng_align(), fd_rng_footprint() ), 0U, 0UL ) );
1190 30 : if( FD_UNLIKELY( !rng ) ) FD_LOG_ERR(( "fd_rng_new failed" ));
1191 :
1192 30 : fd_pack_limits_t limits_lower[1] = {{
1193 30 : .max_cost_per_block = tile->pack.larger_max_cost_per_block ? LARGER_MAX_COST_PER_BLOCK : FD_PACK_MAX_COST_PER_BLOCK_LOWER_BOUND,
1194 30 : .max_vote_cost_per_block = FD_PACK_MAX_VOTE_COST_PER_BLOCK_LOWER_BOUND,
1195 30 : .max_write_cost_per_acct = FD_PACK_MAX_WRITE_COST_PER_ACCT_LOWER_BOUND,
1196 30 : .max_data_bytes_per_block = tile->pack.larger_shred_limits_per_block ? LARGER_MAX_DATA_PER_BLOCK : FD_PACK_MAX_DATA_PER_BLOCK,
1197 30 : .max_txn_per_microblock = EFFECTIVE_TXN_PER_MICROBLOCK,
1198 30 : .max_microblocks_per_block = (ulong)UINT_MAX, /* Limit not known yet */
1199 30 : }};
1200 :
1201 30 : ctx->pack = fd_pack_join( fd_pack_new( FD_SCRATCH_ALLOC_APPEND( l, fd_pack_align(), pack_footprint ),
1202 30 : tile->pack.max_pending_transactions, BUNDLE_META_SZ, tile->pack.bank_tile_count,
1203 30 : limits_lower, rng ) );
1204 30 : if( FD_UNLIKELY( !ctx->pack ) ) FD_LOG_ERR(( "fd_pack_new failed" ));
1205 :
1206 30 : if( FD_UNLIKELY( tile->in_cnt>32UL ) ) FD_LOG_ERR(( "Too many input links (%lu>32) to pack tile", tile->in_cnt ));
1207 :
1208 30 : FD_TEST( tile->in_cnt<sizeof( ctx->in_kind )/sizeof( ctx->in_kind[0] ) );
1209 270 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1210 240 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
1211 :
1212 240 : if( FD_LIKELY( !strcmp( link->name, "resolv_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
1213 210 : else if( FD_LIKELY( !strcmp( link->name, "dedup_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_RESOLV;
1214 210 : else if( FD_LIKELY( !strcmp( link->name, "poh_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_POH;
1215 180 : else if( FD_LIKELY( !strcmp( link->name, "bank_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_BANK;
1216 60 : else if( FD_LIKELY( !strcmp( link->name, "sign_pack" ) ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
1217 30 : else if( FD_LIKELY( !strcmp( link->name, "replay_out" ) ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
1218 30 : else if( FD_LIKELY( !strcmp( link->name, "executed_txn" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECUTED_TXN;
1219 0 : else if( FD_LIKELY( !strcmp( link->name, "exec_sig" ) ) ) ctx->in_kind[ i ] = IN_KIND_EXECUTED_TXN;
1220 0 : else FD_LOG_ERR(( "pack tile has unexpected input link %lu %s", i, link->name ));
1221 240 : }
1222 :
1223 30 : ulong bank_cnt = 0UL;
1224 750 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
1225 720 : fd_topo_tile_t const * consumer_tile = &topo->tiles[ i ];
1226 720 : if( FD_UNLIKELY( strcmp( consumer_tile->name, "bank" ) && strcmp( consumer_tile->name, "replay" ) ) ) continue;
1227 240 : for( ulong j=0UL; j<consumer_tile->in_cnt; j++ ) {
1228 120 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ]==tile->out_link_id[ 0 ] ) ) bank_cnt++;
1229 120 : }
1230 120 : }
1231 :
1232 : // if( FD_UNLIKELY( !bank_cnt ) ) FD_LOG_ERR(( "pack tile connects to no banking tiles" ));
1233 30 : if( FD_UNLIKELY( bank_cnt>FD_PACK_MAX_BANK_TILES ) ) FD_LOG_ERR(( "pack tile connects to too many banking tiles" ));
1234 : // if( FD_UNLIKELY( bank_cnt!=tile->pack.bank_tile_count ) ) FD_LOG_ERR(( "pack tile connects to %lu banking tiles, but tile->pack.bank_tile_count is %lu", bank_cnt, tile->pack.bank_tile_count ));
1235 :
1236 30 : FD_TEST( (tile->pack.schedule_strategy>=0) & (tile->pack.schedule_strategy<=FD_PACK_STRATEGY_BUNDLE) );
1237 :
1238 30 : ctx->crank->enabled = tile->pack.bundle.enabled;
1239 30 : if( FD_UNLIKELY( tile->pack.bundle.enabled ) ) {
1240 30 : if( FD_UNLIKELY( !fd_bundle_crank_gen_init( ctx->crank->gen, (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_program_addr,
1241 30 : (fd_acct_addr_t const *)tile->pack.bundle.tip_payment_program_addr,
1242 30 : (fd_acct_addr_t const *)ctx->crank->vote_pubkey->b,
1243 30 : (fd_acct_addr_t const *)tile->pack.bundle.tip_distribution_authority,
1244 30 : schedule_strategy_strings[ tile->pack.schedule_strategy ],
1245 30 : tile->pack.bundle.commission_bps ) ) ) {
1246 0 : FD_LOG_ERR(( "constructing bundle generator failed" ));
1247 0 : }
1248 :
1249 30 : ulong sign_in_idx = fd_topo_find_tile_in_link ( topo, tile, "sign_pack", tile->kind_id );
1250 30 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "pack_sign", tile->kind_id );
1251 30 : FD_TEST( sign_in_idx!=ULONG_MAX );
1252 30 : fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
1253 30 : fd_topo_link_t * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
1254 30 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->crank->keyguard_client,
1255 30 : sign_out->mcache,
1256 30 : sign_out->dcache,
1257 30 : sign_in->mcache,
1258 30 : sign_in->dcache,
1259 30 : sign_out->mtu ) ) ) ) {
1260 0 : FD_LOG_ERR(( "failed to construct keyguard" ));
1261 0 : }
1262 : /* Initialize enough of the prev config that it produces a
1263 : transaction */
1264 30 : ctx->crank->prev_config->discriminator = 0x82ccfa1ee0aa0c9bUL;
1265 30 : ctx->crank->prev_config->tip_receiver->b[1] = 1;
1266 30 : ctx->crank->prev_config->block_builder->b[2] = 1;
1267 :
1268 30 : memset( ctx->crank->tip_receiver_owner, '\0', 32UL );
1269 30 : memset( ctx->crank->recent_blockhash, '\0', 32UL );
1270 30 : memset( ctx->crank->last_sig, '\0', 64UL );
1271 30 : ctx->crank->ib_inserted = 0;
1272 30 : ctx->crank->epoch = 0UL;
1273 30 : ctx->crank->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
1274 30 : FD_TEST( ctx->crank->keyswitch );
1275 30 : } else {
1276 0 : memset( ctx->crank, '\0', sizeof(ctx->crank) );
1277 0 : }
1278 :
1279 :
1280 : #if FD_PACK_USE_EXTRA_STORAGE
1281 : ctx->extra_txn_deq = extra_txn_deq_join( extra_txn_deq_new( FD_SCRATCH_ALLOC_APPEND( l, extra_txn_deq_align(),
1282 : extra_txn_deq_footprint() ) ) );
1283 : #endif
1284 :
1285 30 : ctx->cur_spot = NULL;
1286 30 : ctx->is_bundle = 0;
1287 30 : ctx->strategy = tile->pack.schedule_strategy;
1288 30 : ctx->max_pending_transactions = tile->pack.max_pending_transactions;
1289 30 : ctx->leader_slot = ULONG_MAX;
1290 30 : ctx->leader_bank = NULL;
1291 30 : ctx->leader_bank_idx = ULONG_MAX;
1292 30 : ctx->pack_idx = 0UL;
1293 30 : ctx->slot_microblock_cnt = 0UL;
1294 30 : ctx->pack_txn_cnt = 0UL;
1295 30 : ctx->slot_max_microblocks = 0UL;
1296 30 : ctx->slot_max_data = 0UL;
1297 30 : ctx->larger_shred_limits_per_block = tile->pack.larger_shred_limits_per_block;
1298 30 : ctx->drain_banks = 0;
1299 30 : ctx->approx_wallclock_ns = fd_log_wallclock();
1300 30 : ctx->approx_tickcount = fd_tickcount();
1301 30 : ctx->rng = rng;
1302 30 : ctx->ticks_per_ns = fd_tempo_tick_per_ns( NULL );
1303 30 : ctx->last_successful_insert = 0L;
1304 30 : ctx->highest_observed_slot = 0UL;
1305 30 : ctx->microblock_duration_ticks = (ulong)(fd_tempo_tick_per_ns( NULL )*(double)MICROBLOCK_DURATION_NS + 0.5);
1306 : #if FD_PACK_USE_EXTRA_STORAGE
1307 : ctx->insert_to_extra = 0;
1308 : #endif
1309 30 : ctx->use_consumed_cus = tile->pack.use_consumed_cus;
1310 30 : ctx->crank->enabled = tile->pack.bundle.enabled;
1311 :
1312 30 : ctx->wait_duration_ticks[ 0 ] = ULONG_MAX;
1313 930 : for( ulong i=1UL; i<MAX_TXN_PER_MICROBLOCK+1UL; i++ ) {
1314 900 : ctx->wait_duration_ticks[ i ]=(ulong)(fd_tempo_tick_per_ns( NULL )*(double)wait_duration[ i ] + 0.5);
1315 900 : }
1316 :
1317 30 : ctx->limits.slot_max_cost = limits_lower->max_cost_per_block;
1318 30 : ctx->limits.slot_max_vote_cost = limits_lower->max_vote_cost_per_block;
1319 30 : ctx->limits.slot_max_write_cost_per_acct = limits_lower->max_write_cost_per_acct;
1320 :
1321 30 : ctx->bank_cnt = tile->pack.bank_tile_count;
1322 30 : ctx->poll_cursor = 0;
1323 30 : ctx->skip_cnt = 0L;
1324 30 : ctx->bank_idle_bitset = fd_ulong_mask_lsb( (int)tile->pack.bank_tile_count );
1325 60 : for( ulong i=0UL; i<tile->pack.bank_tile_count; i++ ) {
1326 30 : ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "bank_busy.%lu", i );
1327 30 : FD_TEST( busy_obj_id!=ULONG_MAX );
1328 30 : ctx->bank_current[ i ] = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
1329 30 : ctx->bank_expect[ i ] = ULONG_MAX;
1330 30 : if( FD_UNLIKELY( !ctx->bank_current[ i ] ) ) FD_LOG_ERR(( "banking tile %lu has no busy flag", i ));
1331 30 : ctx->bank_ready_at[ i ] = 0L;
1332 30 : FD_TEST( ULONG_MAX==fd_fseq_query( ctx->bank_current[ i ] ) );
1333 30 : }
1334 :
1335 270 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
1336 240 : fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
1337 240 : fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
1338 :
1339 240 : ctx->in[ i ].mem = link_wksp->wksp;
1340 240 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
1341 240 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
1342 240 : }
1343 :
1344 30 : ctx->bank_out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
1345 30 : ctx->bank_out_chunk0 = fd_dcache_compact_chunk0( ctx->bank_out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
1346 30 : ctx->bank_out_wmark = fd_dcache_compact_wmark ( ctx->bank_out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
1347 30 : ctx->bank_out_chunk = ctx->bank_out_chunk0;
1348 :
1349 30 : ctx->poh_out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
1350 30 : ctx->poh_out_chunk0 = fd_dcache_compact_chunk0( ctx->poh_out_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
1351 30 : ctx->poh_out_wmark = fd_dcache_compact_wmark ( ctx->poh_out_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache, topo->links[ tile->out_link_id[ 1 ] ].mtu );
1352 30 : ctx->poh_out_chunk = ctx->poh_out_chunk0;
1353 :
1354 : /* Initialize metrics storage */
1355 30 : memset( ctx->insert_result, '\0', FD_PACK_INSERT_RETVAL_CNT * sizeof(ulong) );
1356 30 : fd_histf_join( fd_histf_new( ctx->schedule_duration, FD_MHIST_SECONDS_MIN( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ),
1357 30 : FD_MHIST_SECONDS_MAX( PACK, SCHEDULE_MICROBLOCK_DURATION_SECONDS ) ) );
1358 30 : fd_histf_join( fd_histf_new( ctx->no_sched_duration, FD_MHIST_SECONDS_MIN( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS ),
1359 30 : FD_MHIST_SECONDS_MAX( PACK, NO_SCHED_MICROBLOCK_DURATION_SECONDS ) ) );
1360 30 : fd_histf_join( fd_histf_new( ctx->insert_duration, FD_MHIST_SECONDS_MIN( PACK, INSERT_TRANSACTION_DURATION_SECONDS ),
1361 30 : FD_MHIST_SECONDS_MAX( PACK, INSERT_TRANSACTION_DURATION_SECONDS ) ) );
1362 30 : fd_histf_join( fd_histf_new( ctx->complete_duration, FD_MHIST_SECONDS_MIN( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS ),
1363 30 : FD_MHIST_SECONDS_MAX( PACK, COMPLETE_MICROBLOCK_DURATION_SECONDS ) ) );
1364 30 : ctx->metric_state = 0;
1365 30 : ctx->metric_state_begin = fd_tickcount();
1366 30 : memset( ctx->metric_timing, '\0', 16*sizeof(long) );
1367 30 : memset( ctx->current_bundle, '\0', sizeof(ctx->current_bundle) );
1368 30 : memset( ctx->blk_engine_cfg, '\0', sizeof(ctx->blk_engine_cfg) );
1369 30 : memset( ctx->last_sched_metrics, '\0', sizeof(ctx->last_sched_metrics) );
1370 30 : memset( ctx->start_block_sched_metrics, '\0', sizeof(ctx->start_block_sched_metrics) );
1371 30 : memset( ctx->crank->metrics, '\0', sizeof(ctx->crank->metrics) );
1372 :
1373 30 : FD_LOG_INFO(( "packing microblocks of at most %lu transactions to %lu bank tiles using strategy %i", EFFECTIVE_TXN_PER_MICROBLOCK, tile->pack.bank_tile_count, ctx->strategy ));
1374 :
1375 30 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
1376 30 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
1377 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
1378 :
1379 30 : }
1380 :
1381 : static ulong
1382 : populate_allowed_seccomp( fd_topo_t const * topo,
1383 : fd_topo_tile_t const * tile,
1384 : ulong out_cnt,
1385 0 : struct sock_filter * out ) {
1386 0 : (void)topo;
1387 0 : (void)tile;
1388 :
1389 0 : populate_sock_filter_policy_fd_pack_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
1390 0 : return sock_filter_policy_fd_pack_tile_instr_cnt;
1391 0 : }
1392 :
1393 : static ulong
1394 : populate_allowed_fds( fd_topo_t const * topo,
1395 : fd_topo_tile_t const * tile,
1396 : ulong out_fds_cnt,
1397 0 : int * out_fds ) {
1398 0 : (void)topo;
1399 0 : (void)tile;
1400 :
1401 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
1402 :
1403 0 : ulong out_cnt = 0UL;
1404 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
1405 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
1406 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
1407 0 : return out_cnt;
1408 0 : }
1409 :
1410 0 : #define STEM_BURST (1UL)
1411 :
1412 : /* We want lazy (measured in ns) to be small enough that the producer
1413 : and the consumer never have to wait for credits. For most tango
1414 : links, we use a default worst case speed coming from 100 Gbps
1415 : Ethernet. That's not very suitable for microblocks that go from
1416 : pack to bank. Instead we manually estimate the very aggressive
1417 : 1000ns per microblock, and then reduce it further (in line with the
1418 : default lazy value computation) to ensure the random value chosen
1419 : based on this won't lead to credit return stalls. */
1420 0 : #define STEM_LAZY (128L*3000L)
1421 :
1422 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_pack_ctx_t
1423 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_pack_ctx_t)
1424 :
1425 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
1426 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
1427 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
1428 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
1429 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
1430 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
1431 :
1432 : #include "../stem/fd_stem.c"
1433 :
1434 : fd_topo_run_tile_t fd_tile_pack = {
1435 : .name = "pack",
1436 : .populate_allowed_seccomp = populate_allowed_seccomp,
1437 : .populate_allowed_fds = populate_allowed_fds,
1438 : .scratch_align = scratch_align,
1439 : .scratch_footprint = scratch_footprint,
1440 : .privileged_init = privileged_init,
1441 : .unprivileged_init = unprivileged_init,
1442 : .run = stem_run,
1443 : };
|