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