Line data Source code
1 : #include "fd_bank_abi.h"
2 :
3 : #include "../../disco/tiles.h"
4 : #include "../../disco/pack/fd_pack.h"
5 : #include "../../ballet/blake3/fd_blake3.h"
6 : #include "../../ballet/bmtree/fd_bmtree.h"
7 : #include "../../disco/metrics/fd_metrics.h"
8 : #include "../../util/pod/fd_pod_format.h"
9 : #include "../../disco/pack/fd_pack_rebate_sum.h"
10 : #include "../../disco/metrics/generated/fd_metrics_bank.h"
11 :
12 0 : #define FD_BANK_TRANSACTION_LANDED 1
13 : #define FD_BANK_TRANSACTION_EXECUTED 2
14 :
15 : #define REBATE_BATCH_IDLE_LOOPS (128UL)
16 : #define REBATE_BATCH_MAX_MICROBLOCKS (4UL)
17 :
18 : FD_STATIC_ASSERT( REBATE_BATCH_MAX_MICROBLOCKS*FD_PACK_REBATE_MAX_ENTRIES<=FD_PACK_REBATE_SUM_CAPACITY,
19 : rebate_batch_fits_rebater );
20 :
21 : typedef struct {
22 : ulong kind_id;
23 :
24 : fd_blake3_t * blake3;
25 : void * bmtree;
26 :
27 : uchar * txn_abi_mem;
28 : uchar * txn_sidecar_mem;
29 :
30 : void const * _bank;
31 : ulong _pack_idx;
32 : ulong _txn_idx;
33 : int _is_bundle;
34 :
35 : fd_acct_addr_t _alt_accts[MAX_TXN_PER_MICROBLOCK][FD_TXN_ACCT_ADDR_MAX];
36 :
37 : ulong * busy_fseq;
38 :
39 : fd_wksp_t * pack_in_mem;
40 : ulong pack_in_chunk0;
41 : ulong pack_in_wmark;
42 :
43 : fd_wksp_t * out_mem;
44 : ulong out_chunk0;
45 : ulong out_wmark;
46 : ulong out_chunk;
47 :
48 : fd_wksp_t * rebate_mem;
49 : ulong rebate_chunk0;
50 : ulong rebate_wmark;
51 : ulong rebate_chunk;
52 : ulong rebates_for_slot;
53 : ulong rebate_tsorig;
54 : ulong rebate_microblock_cnt;
55 : ulong rebate_idle_loop_cnt;
56 : int rebate_draining;
57 : ulong rebate_seed;
58 : fd_pack_rebate_sum_t rebater[ 1 ];
59 :
60 : float ns_per_tick;
61 :
62 : struct {
63 : ulong txn_load_address_lookup_tables[ 6 ];
64 : ulong transaction_result[ 41 ];
65 : ulong processing_failed;
66 : ulong fee_only;
67 : ulong exec_failed;
68 : ulong success;
69 : } metrics;
70 : } fd_bank_ctx_t;
71 :
72 : FD_FN_CONST static inline ulong
73 0 : scratch_align( void ) {
74 0 : return 128UL;
75 0 : }
76 :
77 : FD_FN_PURE static inline ulong
78 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
79 0 : (void)tile;
80 0 : ulong l = FD_LAYOUT_INIT;
81 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_bank_ctx_t ), sizeof( fd_bank_ctx_t ) );
82 0 : l = FD_LAYOUT_APPEND( l, FD_BLAKE3_ALIGN, FD_BLAKE3_FOOTPRINT );
83 0 : l = FD_LAYOUT_APPEND( l, FD_BMTREE_COMMIT_ALIGN, FD_BMTREE_COMMIT_FOOTPRINT(0) );
84 0 : l = FD_LAYOUT_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT );
85 0 : l = FD_LAYOUT_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR_MAX );
86 0 : return FD_LAYOUT_FINI( l, scratch_align() );
87 0 : }
88 :
89 : static inline void
90 0 : metrics_write( fd_bank_ctx_t * ctx ) {
91 0 : FD_MCNT_ENUM_COPY( BANK, TXN_LOAD_ADDRESS_TABLE, ctx->metrics.txn_load_address_lookup_tables );
92 0 : FD_MCNT_ENUM_COPY( BANK, TXN_RESULT, ctx->metrics.transaction_result );
93 :
94 0 : FD_MCNT_SET( BANK, TXN_PROCESSING_FAILED, ctx->metrics.processing_failed );
95 0 : FD_MCNT_SET( BANK, TXN_FEE_ONLY, ctx->metrics.fee_only );
96 0 : ulong txn_executed[ FD_METRICS_ENUM_TXN_EXECUTE_RESULT_CNT ];
97 0 : txn_executed[ FD_METRICS_ENUM_TXN_EXECUTE_RESULT_V_SUCCESS_IDX ] = ctx->metrics.success;
98 0 : txn_executed[ FD_METRICS_ENUM_TXN_EXECUTE_RESULT_V_FAILED_IDX ] = ctx->metrics.exec_failed;
99 0 : FD_MCNT_ENUM_COPY( BANK, TXN_EXECUTED, txn_executed );
100 0 : }
101 :
102 : static inline void
103 : after_credit( fd_bank_ctx_t * ctx,
104 : fd_stem_context_t * stem,
105 : int * opt_poll_in,
106 0 : int * charge_busy ) {
107 0 : if( FD_UNLIKELY( !ctx->rebate_microblock_cnt ) ) return;
108 :
109 0 : if( FD_LIKELY( !ctx->rebate_draining ) ) {
110 0 : if( FD_LIKELY( ctx->rebate_microblock_cnt<REBATE_BATCH_MAX_MICROBLOCKS &&
111 0 : ctx->rebate_idle_loop_cnt<REBATE_BATCH_IDLE_LOOPS &&
112 0 : !ctx->rebater->ib_result ) ) {
113 0 : ctx->rebate_idle_loop_cnt++;
114 0 : return;
115 0 : }
116 0 : ctx->rebate_draining = 1;
117 0 : }
118 :
119 0 : ulong written_sz = fd_pack_rebate_sum_report( ctx->rebater, fd_chunk_to_laddr( ctx->rebate_mem, ctx->rebate_chunk ) );
120 0 : if( FD_UNLIKELY( written_sz ) ) {
121 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
122 0 : fd_stem_publish( stem, 1UL, ctx->rebates_for_slot, ctx->rebate_chunk, written_sz, 0UL, ctx->rebate_tsorig, tspub );
123 0 : ctx->rebate_chunk = fd_dcache_compact_next( ctx->rebate_chunk, written_sz, ctx->rebate_chunk0, ctx->rebate_wmark );
124 0 : *opt_poll_in = 0;
125 0 : *charge_busy = 1;
126 0 : } else {
127 0 : ctx->rebate_microblock_cnt = 0UL;
128 0 : ctx->rebate_idle_loop_cnt = 0UL;
129 0 : ctx->rebate_draining = 0;
130 0 : }
131 0 : }
132 :
133 : static int
134 : before_frag( fd_bank_ctx_t * ctx,
135 : ulong in_idx,
136 : ulong seq,
137 0 : ulong sig ) {
138 0 : (void)in_idx;
139 0 : (void)seq;
140 :
141 : /* Pack also outputs "leader slot done" which we can ignore. */
142 0 : if( FD_UNLIKELY( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_MICROBLOCK ) ) return 1;
143 :
144 0 : ulong target_bank_idx = fd_disco_poh_sig_execle_tile( sig );
145 0 : if( FD_UNLIKELY( target_bank_idx!=ctx->kind_id ) ) return 1;
146 :
147 0 : return 0;
148 0 : }
149 :
150 : extern int fd_ext_bank_execute_and_commit_bundle( void const * bank, void * txns, ulong txn_cnt, int * out_transaction_err, uint * actual_execution_cus, uint * actual_acct_data_cus, ulong * out_timestamps, ulong * out_tips, int * remove_simple_vote_from_cost_model );
151 : extern void * fd_ext_bank_load_and_execute_txns( void const * bank, void * txns, ulong txn_cnt, int * out_processing_results, int * out_transaction_err, uint * out_consumed_exec_cus, uint * out_consumed_acct_data_cus, ulong * out_timestamps, ulong * out_tips, int * remove_simple_vote_from_cost_model );
152 : extern void fd_ext_bank_commit_txns( void const * bank, void const * txns, ulong txn_cnt , void * load_and_execute_output );
153 : extern void fd_ext_bank_release_thunks( void * load_and_execute_output );
154 :
155 : static inline void
156 : during_frag( fd_bank_ctx_t * ctx,
157 : ulong in_idx FD_PARAM_UNUSED,
158 : ulong seq FD_PARAM_UNUSED,
159 : ulong sig FD_PARAM_UNUSED,
160 : ulong chunk,
161 : ulong sz,
162 0 : ulong ctl FD_PARAM_UNUSED ) {
163 :
164 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->pack_in_mem, chunk );
165 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
166 :
167 0 : if( FD_UNLIKELY( chunk<ctx->pack_in_chunk0 || chunk>ctx->pack_in_wmark || sz>USHORT_MAX ) )
168 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->pack_in_chunk0, ctx->pack_in_wmark ));
169 :
170 : /* Pack sends fd_txn_e_t (with ALT accounts), but PoH expects fd_txn_p_t.
171 : We copy the fd_txn_p_t portion to the PoH output buffer, and copy the
172 : ALT accounts to the tile context for rebates. */
173 0 : ulong txn_cnt = (sz-sizeof(fd_microblock_execle_trailer_t))/sizeof(fd_txn_e_t);
174 0 : fd_txn_e_t const * src_txn_e = (fd_txn_e_t const *)src;
175 0 : fd_txn_p_t * dst_txn_p = (fd_txn_p_t *)dst;
176 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
177 0 : fd_memcpy( dst_txn_p + i, src_txn_e[i].txnp, sizeof(fd_txn_p_t) );
178 0 : ulong alt_cnt = fd_ulong_min( (ulong)TXN(src_txn_e[i].txnp)->addr_table_adtl_cnt, FD_TXN_ACCT_ADDR_MAX );
179 0 : fd_memcpy( ctx->_alt_accts[i], src_txn_e[i].alt_accts, alt_cnt * sizeof(fd_acct_addr_t) );
180 0 : }
181 :
182 0 : fd_microblock_execle_trailer_t * trailer = (fd_microblock_execle_trailer_t *)( src+sz-sizeof(fd_microblock_execle_trailer_t) );
183 0 : ctx->_bank = trailer->bank;
184 0 : ctx->_pack_idx = trailer->pack_idx;
185 0 : ctx->_txn_idx = trailer->pack_txn_idx;
186 0 : ctx->_is_bundle = trailer->is_bundle;
187 0 : }
188 :
189 : static void
190 : hash_transactions( void * mem,
191 : fd_txn_p_t * txns,
192 : ulong txn_cnt,
193 0 : uchar * mixin ) {
194 0 : fd_bmtree_commit_t * bmtree = fd_bmtree_commit_init( mem, 32UL, 1UL, 0UL );
195 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
196 0 : fd_txn_p_t * _txn = txns + i;
197 0 : if( FD_UNLIKELY( !(_txn->flags & FD_TXN_P_FLAGS_EXECUTE_SUCCESS) ) ) continue;
198 :
199 0 : fd_txn_t * txn = TXN(_txn);
200 0 : for( ulong j=0; j<txn->signature_cnt; j++ ) {
201 0 : fd_bmtree_node_t node[1];
202 0 : fd_bmtree_hash_leaf( node, _txn->payload+txn->signature_off+64UL*j, 64UL, 1UL );
203 0 : fd_bmtree_commit_append( bmtree, node, 1UL );
204 0 : }
205 0 : }
206 0 : if( FD_LIKELY( fd_bmtree_commit_leaf_cnt( bmtree ) ) ) {
207 0 : uchar * root = fd_bmtree_commit_fini( bmtree );
208 0 : fd_memcpy( mixin, root, 32UL );
209 0 : } else {
210 0 : fd_memset( mixin, 0, 32UL );
211 : /* Poison so we can detect if this is ever accessed upstream, even though it should never be. */
212 0 : fd_msan_poison( mixin, 32UL );
213 0 : }
214 0 : }
215 :
216 : static inline void
217 : handle_microblock( fd_bank_ctx_t * ctx,
218 : ulong seq,
219 : ulong sig,
220 : ulong sz,
221 : ulong begin_tspub,
222 0 : fd_stem_context_t * stem ) {
223 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
224 :
225 0 : ulong slot = fd_disco_poh_sig_slot( sig );
226 0 : ulong txn_cnt = (sz-sizeof(fd_microblock_execle_trailer_t))/sizeof(fd_txn_e_t);
227 :
228 : /* Use ALT accounts copied to context for rebates. These were resolved
229 : by resolv_tile and are needed because the LUT may be deactivated by
230 : the time we get here. Execution still uses the bank's current view
231 : of the LUT via fd_bank_abi_txn_init. */
232 0 : fd_acct_addr_t const * writable_alt[ MAX_TXN_PER_MICROBLOCK ];
233 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
234 0 : writable_alt[i] = ctx->_alt_accts[i];
235 0 : }
236 :
237 0 : ulong sanitized_txn_cnt = 0UL;
238 0 : ulong sidecar_footprint_bytes = 0UL;
239 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
240 0 : fd_txn_p_t * txn = (fd_txn_p_t *)( dst + (i*sizeof(fd_txn_p_t)) );
241 :
242 0 : void * abi_txn = ctx->txn_abi_mem + (sanitized_txn_cnt*FD_BANK_ABI_TXN_FOOTPRINT);
243 0 : void * abi_txn_sidecar = ctx->txn_sidecar_mem + sidecar_footprint_bytes;
244 0 : txn->flags &= ~FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
245 :
246 0 : int result = fd_bank_abi_txn_init( abi_txn, abi_txn_sidecar, ctx->_bank, slot, ctx->blake3, txn->payload, txn->payload_sz, TXN(txn), !!(txn->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE) );
247 0 : ctx->metrics.txn_load_address_lookup_tables[ (ulong)((long)FD_METRICS_COUNTER_BANK_TXN_LOAD_ADDRESS_TABLE_CNT+result-1L) ]++;
248 0 : if( FD_UNLIKELY( result!=FD_BANK_ABI_TXN_INIT_SUCCESS ) ) continue;
249 :
250 0 : txn->flags |= FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
251 :
252 0 : fd_txn_t * txn1 = TXN(txn);
253 0 : sidecar_footprint_bytes += FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR( txn1->acct_addr_cnt, txn1->addr_table_adtl_cnt, txn1->instr_cnt, txn1->addr_table_lookup_cnt );
254 0 : sanitized_txn_cnt++;
255 0 : }
256 :
257 : /* Just because a transaction was executed doesn't mean it succeeded,
258 : but all executed transactions get committed. */
259 0 : int processing_results [ MAX_TXN_PER_MICROBLOCK ] = { 0 };
260 0 : int transaction_err [ MAX_TXN_PER_MICROBLOCK ] = { 0 };
261 0 : uint consumed_exec_cus [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
262 0 : uint consumed_acct_data_cus[ MAX_TXN_PER_MICROBLOCK ] = { 0U };
263 0 : ulong out_timestamps [ 4*MAX_TXN_PER_MICROBLOCK ] = { 0U };
264 0 : ulong out_tips [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
265 0 : int remove_simple_vote_from_cost_model;
266 :
267 0 : void * load_and_execute_output = fd_ext_bank_load_and_execute_txns( ctx->_bank,
268 0 : ctx->txn_abi_mem,
269 0 : sanitized_txn_cnt,
270 0 : processing_results,
271 0 : transaction_err,
272 0 : consumed_exec_cus,
273 0 : consumed_acct_data_cus,
274 0 : out_timestamps,
275 0 : out_tips,
276 0 : &remove_simple_vote_from_cost_model );
277 0 : FD_TEST( remove_simple_vote_from_cost_model==1 );
278 :
279 0 : ulong sanitized_idx = 0UL;
280 0 : int skip_commit = 0;
281 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
282 0 : fd_txn_p_t * txn = (fd_txn_p_t *)( dst + (i*sizeof(fd_txn_p_t)) );
283 :
284 0 : uint requested_exec_plus_acct_data_cus = txn->pack_cu.requested_exec_plus_acct_data_cus;
285 0 : uint non_execution_cus = txn->pack_cu.non_execution_cus;
286 :
287 : /* Assume failure, set below if success. If it doesn't land cin the
288 : block, rebate the non-execution CUs too. */
289 0 : txn->execle_cu.actual_consumed_cus = 0U;
290 0 : txn->execle_cu.rebated_cus = requested_exec_plus_acct_data_cus + non_execution_cus;
291 0 : txn->flags &= ~FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
292 0 : if( FD_UNLIKELY( !(txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS) ) ) continue;
293 :
294 0 : sanitized_idx++;
295 :
296 : /* Stash the result in the flags value so that pack can inspect it. */
297 0 : txn->flags = (txn->flags & 0x00FFFFFFU) | ((uint)transaction_err[ sanitized_idx-1UL ]<<24);
298 :
299 0 : ctx->metrics.transaction_result[ transaction_err [ sanitized_idx-1UL ] ]++;
300 :
301 0 : ctx->metrics.processing_failed += (ulong)(processing_results[ sanitized_idx-1UL ]==0 );
302 :
303 0 : if( FD_UNLIKELY( !(processing_results[ sanitized_idx-1UL ] & FD_BANK_TRANSACTION_LANDED) ) ) continue;
304 :
305 0 : uint actual_execution_cus = consumed_exec_cus[ sanitized_idx-1UL ];
306 0 : uint actual_acct_data_cus = consumed_acct_data_cus[ sanitized_idx-1UL ];
307 :
308 :
309 : /* Check for FeesOnly transactions where actual CUs exceed
310 : requested. This can happen for durable nonce transactions
311 : with oversized nonce accounts. Skip the commit to prevent
312 : underflow in the rebate computation. The transaction keeps its
313 : default full rebate set at the top of the loop. */
314 0 : if( FD_UNLIKELY( !(processing_results[ sanitized_idx-1UL ] & FD_BANK_TRANSACTION_EXECUTED) && ( actual_execution_cus + actual_acct_data_cus > requested_exec_plus_acct_data_cus ) ) ) {
315 0 : FD_LOG_WARNING(( "FeesOnly txn actual CUs (%u+%u) exceed requested (%u), dropping",
316 0 : actual_execution_cus, actual_acct_data_cus, requested_exec_plus_acct_data_cus ));
317 0 : skip_commit = 1;
318 0 : ctx->metrics.processing_failed++;
319 0 : continue;
320 0 : }
321 :
322 : /* The VM will stop executing and fail an instruction immediately if
323 : it exceeds its requested CUs. A transaction which requests less
324 : account data than it actually consumes will fail in the account
325 : loading stage. */
326 0 : if( FD_UNLIKELY( actual_execution_cus + actual_acct_data_cus > requested_exec_plus_acct_data_cus ) ) {
327 0 : uchar * _sig = (uchar *)txn->payload + TXN(txn)->signature_off;
328 0 : FD_BASE58_ENCODE_64_BYTES( _sig, _sig_b58 );
329 0 : FD_LOG_HEXDUMP_WARNING(( "txn", txn->payload, txn->payload_sz ));
330 0 : FD_LOG_ERR(( "transaction %s actual CUs (%u+%u) exceeded requested (%u) despite pack guaranteeing it would fit",
331 0 : _sig_b58, actual_execution_cus, actual_acct_data_cus, requested_exec_plus_acct_data_cus ));
332 0 : }
333 :
334 0 : ctx->metrics.fee_only += (ulong)(processing_results[ sanitized_idx-1UL ]==FD_BANK_TRANSACTION_LANDED);
335 :
336 0 : txn->execle_cu.rebated_cus = requested_exec_plus_acct_data_cus - ( actual_execution_cus + actual_acct_data_cus );
337 0 : txn->execle_cu.actual_consumed_cus = non_execution_cus + actual_execution_cus + actual_acct_data_cus;
338 :
339 : /* TXN_P_FLAGS_EXECUTE_SUCCESS means that it should be included in
340 : the block. It's a bit of a misnomer now that there are fee-only
341 : transactions. */
342 0 : txn->flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
343 :
344 0 : if( FD_UNLIKELY( !(processing_results[ sanitized_idx-1UL ] & FD_BANK_TRANSACTION_EXECUTED) ) ) continue;
345 :
346 0 : if( transaction_err[ sanitized_idx-1UL ] ) ctx->metrics.exec_failed++;
347 0 : else ctx->metrics.success++;
348 0 : }
349 :
350 0 : if( FD_UNLIKELY( skip_commit ) ) {
351 0 : FD_TEST( txn_cnt==1UL ); /* see comment about FeesOnly nonce transactions above */
352 0 : fd_ext_bank_release_thunks( load_and_execute_output );
353 0 : } else {
354 : /* Commit must succeed so no failure path. This function takes
355 : ownership of the load_and_execute_output and will free it
356 : before it returns. They should not be reused. Once commit
357 : is called, the transactions MUST be mixed into the PoH
358 : otherwise we will fork and diverge, so the link from here
359 : til PoH mixin must be completely reliable with nothing
360 : dropped. */
361 0 : fd_ext_bank_commit_txns( ctx->_bank, ctx->txn_abi_mem, sanitized_txn_cnt, load_and_execute_output );
362 0 : }
363 0 : load_and_execute_output = NULL;
364 :
365 : /* Indicate to pack tile we are done processing the transactions so
366 : it can pack new microblocks using these accounts. */
367 0 : fd_fseq_update( ctx->busy_fseq, seq );
368 :
369 : /* Prepare the rebate */
370 0 : fd_pack_rebate_sum_add_txn( ctx->rebater, (fd_txn_p_t const *)dst, writable_alt, txn_cnt );
371 :
372 : /* Now produce the merkle hash of the transactions for inclusion
373 : (mixin) to the PoH hash. This is done on the bank tile because
374 : it shards / scales horizontally here, while PoH does not. */
375 0 : fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( dst + txn_cnt*sizeof(fd_txn_p_t) );
376 0 : hash_transactions( ctx->bmtree, (fd_txn_p_t*)dst, txn_cnt, trailer->hash );
377 0 : trailer->pack_txn_idx = ctx->_txn_idx;
378 0 : trailer->tips = 0;
379 0 : for( ulong i=0UL; i<txn_cnt; i++ ) trailer->tips += out_tips[ i ];
380 :
381 0 : long const tickcount = fd_tickcount();
382 0 : long const microblock_start_ticks = fd_frag_meta_ts_decomp( begin_tspub, tickcount );
383 :
384 0 : long const tx_preload_end_ticks = (long)out_timestamps[ 3 ];
385 0 : long const tx_start_ticks = (long)out_timestamps[ 0 ];
386 0 : long const tx_load_end_ticks = (long)out_timestamps[ 1 ];
387 0 : long const tx_end_ticks = (long)out_timestamps[ 2 ];
388 :
389 0 : trailer->txn_ns_dt.check_start = (float)fd_long_max( 0L, tx_preload_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
390 0 : trailer->txn_ns_dt.load_start = (float)fd_long_max( 0L, tx_start_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
391 0 : trailer->txn_ns_dt.exec_start = (float)fd_long_max( 0L, tx_load_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
392 0 : trailer->txn_ns_dt.commit_start = (float)fd_long_max( 0L, tx_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
393 0 : trailer->txn_ns_dt.commit_end = (float)fd_long_max( 0L, fd_tickcount() - microblock_start_ticks ) * ctx->ns_per_tick;
394 0 : trailer->exec_start_ticks = 0L;
395 0 : trailer->exec_end_ticks = 0L;
396 :
397 :
398 : /* When sending MAX_TXN_PER_MICROBLOCK transactions as fd_txn_p_t to PoH,
399 : there's always extra bytes at the end to stash the trailer. */
400 0 : FD_STATIC_ASSERT( MAX_MICROBLOCK_SZ-(MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_p_t))>=sizeof(fd_microblock_trailer_t), poh_shred_mtu );
401 0 : FD_STATIC_ASSERT( MAX_MICROBLOCK_SZ-(MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_p_t))>=sizeof(fd_microblock_execle_trailer_t), poh_shred_mtu );
402 :
403 : /* We have a race window with the GUI, where if the slot is ending it
404 : will snap these metrics to draw the waterfall, but see them outdated
405 : because housekeeping hasn't run. For now just update them here, but
406 : PoH should eventually flush the pipeline before ending the slot. */
407 0 : metrics_write( ctx );
408 :
409 0 : ulong bank_sig = fd_disco_execle_sig( slot, ctx->_pack_idx );
410 :
411 : /* We always need to publish, even if there are no successfully executed
412 : transactions so the PoH tile can keep an accurate count of microblocks
413 : it has seen. */
414 0 : ulong new_sz = txn_cnt*sizeof(fd_txn_p_t) + sizeof(fd_microblock_trailer_t);
415 0 : fd_stem_publish( stem, 0UL, bank_sig, ctx->out_chunk, new_sz, 0UL, 0UL, (ulong)fd_frag_meta_ts_comp( tickcount ) );
416 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, new_sz, ctx->out_chunk0, ctx->out_wmark );
417 0 : }
418 :
419 : static inline void
420 : handle_bundle( fd_bank_ctx_t * ctx,
421 : ulong seq,
422 : ulong sig,
423 : ulong sz,
424 : ulong begin_tspub,
425 0 : fd_stem_context_t * stem ) {
426 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
427 0 : fd_txn_p_t * txns = (fd_txn_p_t *)dst;
428 :
429 0 : ulong slot = fd_disco_poh_sig_slot( sig );
430 0 : ulong txn_cnt = (sz-sizeof(fd_microblock_execle_trailer_t))/sizeof(fd_txn_e_t);
431 :
432 : /* Use ALT accounts copied to context for rebates. These were resolved
433 : by resolv_tile and are needed because the LUT may be deactivated by
434 : the time we get here. Execution still uses the bank's current view
435 : of the LUT via fd_bank_abi_txn_init. */
436 0 : fd_acct_addr_t const * writable_alt[ MAX_TXN_PER_MICROBLOCK ];
437 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
438 0 : writable_alt[i] = ctx->_alt_accts[i];
439 0 : }
440 :
441 0 : int execution_success = 1;
442 :
443 0 : ulong sidecar_footprint_bytes = 0UL;
444 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
445 0 : fd_txn_p_t * txn = txns+i;
446 :
447 0 : void * abi_txn = ctx->txn_abi_mem + (i*FD_BANK_ABI_TXN_FOOTPRINT);
448 0 : void * abi_txn_sidecar = ctx->txn_sidecar_mem + sidecar_footprint_bytes;
449 0 : txn->flags &= ~(FD_TXN_P_FLAGS_SANITIZE_SUCCESS | FD_TXN_P_FLAGS_EXECUTE_SUCCESS);
450 :
451 0 : int result = fd_bank_abi_txn_init( abi_txn, abi_txn_sidecar, ctx->_bank, slot, ctx->blake3, txn->payload, txn->payload_sz, TXN(txn), !!(txn->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE) );
452 0 : ctx->metrics.txn_load_address_lookup_tables[ (ulong)((long)FD_METRICS_COUNTER_BANK_TXN_LOAD_ADDRESS_TABLE_CNT+result-1L) ]++;
453 0 : if( FD_UNLIKELY( result!=FD_BANK_ABI_TXN_INIT_SUCCESS ) ) {
454 0 : execution_success = 0;
455 0 : continue;
456 0 : }
457 :
458 0 : txn->flags |= FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
459 :
460 0 : fd_txn_t * txn1 = TXN(txn);
461 0 : sidecar_footprint_bytes += FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR( txn1->acct_addr_cnt, txn1->addr_table_adtl_cnt, txn1->instr_cnt, txn1->addr_table_lookup_cnt );
462 0 : }
463 :
464 0 : int transaction_err [ MAX_TXN_PER_MICROBLOCK ];
465 0 : for( ulong i=0UL; i<txn_cnt; i++ ) transaction_err[ i ] = FD_METRICS_ENUM_TRANSACTION_ERROR_V_BUNDLE_PEER_IDX;
466 :
467 0 : uint actual_execution_cus [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
468 0 : uint actual_acct_data_cus [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
469 0 : uint consumed_cus [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
470 0 : ulong out_timestamps [ 4*MAX_TXN_PER_MICROBLOCK ] = { 0U };
471 0 : ulong tips [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
472 0 : int remove_simple_vote_from_cost_model;
473 0 : if( FD_LIKELY( execution_success ) ) {
474 0 : execution_success = fd_ext_bank_execute_and_commit_bundle( ctx->_bank, ctx->txn_abi_mem, txn_cnt, transaction_err, actual_execution_cus, actual_acct_data_cus, out_timestamps, tips, &remove_simple_vote_from_cost_model );
475 0 : FD_TEST( remove_simple_vote_from_cost_model==1 );
476 0 : }
477 :
478 0 : if( FD_LIKELY( execution_success ) ) {
479 0 : ctx->metrics.success += txn_cnt;
480 0 : ctx->metrics.transaction_result[ FD_METRICS_ENUM_TRANSACTION_ERROR_V_SUCCESS_IDX ] += txn_cnt;
481 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
482 0 : txns[ i ].flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
483 0 : txns[ i ].flags = (txns[ i ].flags & 0x00FFFFFFU); /* Clear error bits to indicate success */
484 0 : }
485 0 : } else {
486 : /* If any transaction fails in a bundle ... they all fail */
487 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
488 0 : fd_txn_p_t * txn = txns+i;
489 :
490 : /* Don't double count metrics for transactions that failed to
491 : sanitize. */
492 0 : if( FD_UNLIKELY( !(txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS) ) ) continue;
493 :
494 0 : txn->flags &= ~FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
495 0 : txn->flags = (txn->flags & 0x00FFFFFFU) | ((uint)transaction_err[ i ]<<24);
496 0 : ctx->metrics.processing_failed++;
497 0 : ctx->metrics.transaction_result[ transaction_err[ i ] ]++;
498 0 : }
499 0 : }
500 :
501 : /* Indicate to pack tile we are done processing the transactions so
502 : it can pack new microblocks using these accounts. */
503 0 : fd_fseq_update( ctx->busy_fseq, seq );
504 :
505 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
506 0 : fd_txn_p_t * txn = txns+i;
507 :
508 0 : uint requested_exec_plus_acct_data_cus = txn->pack_cu.requested_exec_plus_acct_data_cus;
509 0 : uint non_execution_cus = txn->pack_cu.non_execution_cus;
510 :
511 : /* Note that some transactions will have 0 consumed cus because
512 : they were never actually executed, due to an earlier
513 : transaction failing. */
514 0 : consumed_cus[ i ] = actual_execution_cus[ i ] + actual_acct_data_cus[ i ];
515 :
516 : /* Assume failure, set below if success. If it doesn't land in the
517 : block, rebate the non-execution CUs too. */
518 0 : txn->execle_cu.rebated_cus = requested_exec_plus_acct_data_cus + non_execution_cus;
519 :
520 : /* We want to include consumed CUs for successful txns from failed
521 : bundles for monitoring, even though they aren't included in the
522 : block. This is safe because the poh tile first checks if a txn
523 : is included in the block before counting its
524 : "actual_consumed_cus" towards the block tally. */
525 0 : txn->execle_cu.actual_consumed_cus = fd_uint_if( transaction_err[ i ], 0U, non_execution_cus+consumed_cus[ i ] );
526 :
527 0 : if( FD_LIKELY( execution_success ) ) {
528 0 : if( FD_UNLIKELY( consumed_cus[ i ] > requested_exec_plus_acct_data_cus ) ) {
529 0 : uchar * _sig = (uchar *)txn->payload + TXN(txn)->signature_off;
530 0 : FD_BASE58_ENCODE_64_BYTES( _sig, _sig_b58 );
531 0 : FD_LOG_HEXDUMP_WARNING(( "txn", txn->payload, txn->payload_sz ));
532 0 : FD_LOG_ERR(( "transaction %s in bundle consumed %u CUs > requested %u CUs despite pack guaranteeing it would fit",
533 0 : _sig_b58, consumed_cus[ i ], requested_exec_plus_acct_data_cus ));
534 0 : }
535 :
536 0 : txn->execle_cu.actual_consumed_cus = non_execution_cus + consumed_cus[ i ];
537 0 : txn->execle_cu.rebated_cus = requested_exec_plus_acct_data_cus - consumed_cus[ i ];
538 0 : }
539 0 : }
540 :
541 0 : fd_pack_rebate_sum_add_txn( ctx->rebater, txns, writable_alt, txn_cnt );
542 :
543 : /* We need to publish each transaction separately into its own
544 : microblock, so make a temporary copy on the stack so we can move
545 : all the data around. */
546 0 : fd_txn_p_t bundle_txn_temp[ 5UL ];
547 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
548 0 : bundle_txn_temp[ i ] = txns[ i ];
549 0 : }
550 :
551 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
552 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
553 0 : fd_memcpy( dst, bundle_txn_temp+i, sizeof(fd_txn_p_t) );
554 :
555 0 : fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( dst+sizeof(fd_txn_p_t) );
556 0 : hash_transactions( ctx->bmtree, (fd_txn_p_t*)dst, 1UL, trailer->hash );
557 0 : trailer->pack_txn_idx = ctx->_txn_idx + i;
558 0 : trailer->tips = tips[ i ];
559 :
560 0 : ulong bank_sig = fd_disco_execle_sig( slot, ctx->_pack_idx+i );
561 :
562 0 : long const tickcount = fd_tickcount();
563 0 : long const microblock_start_ticks = fd_frag_meta_ts_decomp( begin_tspub, tickcount );
564 :
565 0 : long const tx_preload_end_ticks = (long)out_timestamps[ 4*i + 3 ];
566 0 : long const tx_start_ticks = (long)out_timestamps[ 4*i + 0 ];
567 0 : long const tx_load_end_ticks = (long)out_timestamps[ 4*i + 1 ];
568 0 : long const tx_end_ticks = (long)out_timestamps[ 4*i + 2 ];
569 :
570 0 : trailer->txn_ns_dt.check_start = (float)fd_long_max( 0L, tx_preload_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
571 0 : trailer->txn_ns_dt.load_start = (float)fd_long_max( 0L, tx_start_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
572 0 : trailer->txn_ns_dt.exec_start = (float)fd_long_max( 0L, tx_load_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
573 0 : trailer->txn_ns_dt.commit_start = (float)fd_long_max( 0L, tx_end_ticks - microblock_start_ticks ) * ctx->ns_per_tick;
574 0 : trailer->txn_ns_dt.commit_end = (float)fd_long_max( 0L, fd_tickcount() - microblock_start_ticks ) * ctx->ns_per_tick;
575 0 : trailer->exec_start_ticks = 0L;
576 0 : trailer->exec_end_ticks = 0L;
577 :
578 0 : ulong new_sz = sizeof(fd_txn_p_t) + sizeof(fd_microblock_trailer_t);
579 0 : fd_stem_publish( stem, 0UL, bank_sig, ctx->out_chunk, new_sz, 0UL, 0UL, (ulong)fd_frag_meta_ts_comp( tickcount ) );
580 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, new_sz, ctx->out_chunk0, ctx->out_wmark );
581 0 : }
582 :
583 0 : metrics_write( ctx );
584 0 : }
585 :
586 : static inline void
587 : after_frag( fd_bank_ctx_t * ctx,
588 : ulong in_idx,
589 : ulong seq,
590 : ulong sig,
591 : ulong sz,
592 : ulong tsorig,
593 : ulong tspub,
594 0 : fd_stem_context_t * stem ) {
595 0 : (void)in_idx;
596 :
597 0 : ulong slot = fd_disco_poh_sig_slot( sig );
598 0 : if( FD_UNLIKELY( slot!=ctx->rebates_for_slot ) ) {
599 : /* If pack has already moved on to a new slot, the rebates are no
600 : longer useful. */
601 0 : fd_pack_rebate_sum_clear( ctx->rebater );
602 0 : ctx->rebate_microblock_cnt = 0UL;
603 0 : ctx->rebate_idle_loop_cnt = 0UL;
604 0 : ctx->rebate_draining = 0;
605 0 : ctx->rebates_for_slot = slot;
606 0 : }
607 0 : if( FD_UNLIKELY( !ctx->rebate_microblock_cnt ) ) {
608 0 : ctx->rebate_tsorig = tsorig;
609 0 : }
610 0 : ctx->rebate_idle_loop_cnt = 0UL;
611 :
612 0 : if( FD_UNLIKELY( ctx->_is_bundle ) ) handle_bundle( ctx, seq, sig, sz, tspub, stem );
613 0 : else handle_microblock( ctx, seq, sig, sz, tspub, stem );
614 0 : ulong txn_cnt = (sz-sizeof(fd_microblock_execle_trailer_t))/sizeof(fd_txn_e_t);
615 0 : ctx->rebate_microblock_cnt += fd_ulong_if( ctx->_is_bundle, txn_cnt, 1UL );
616 0 : }
617 :
618 : static void
619 : privileged_init( fd_topo_t const * topo,
620 0 : fd_topo_tile_t const * tile ) {
621 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
622 :
623 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
624 0 : fd_bank_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_bank_ctx_t), sizeof(fd_bank_ctx_t) );
625 0 : FD_TEST( fd_rng_secure( &ctx->rebate_seed, sizeof(ctx->rebate_seed) ) );
626 0 : }
627 :
628 : static void
629 : unprivileged_init( fd_topo_t const * topo,
630 0 : fd_topo_tile_t const * tile ) {
631 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
632 :
633 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
634 0 : fd_bank_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_bank_ctx_t ), sizeof( fd_bank_ctx_t ) );
635 0 : void * blake3 = FD_SCRATCH_ALLOC_APPEND( l, FD_BLAKE3_ALIGN, FD_BLAKE3_FOOTPRINT );
636 0 : void * bmtree = FD_SCRATCH_ALLOC_APPEND( l, FD_BMTREE_COMMIT_ALIGN, FD_BMTREE_COMMIT_FOOTPRINT(0) );
637 0 : ctx->txn_abi_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT );
638 0 : ctx->txn_sidecar_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR_MAX );
639 :
640 0 : #define NONNULL( x ) (__extension__({ \
641 0 : __typeof__((x)) __x = (x); \
642 0 : if( FD_UNLIKELY( !__x ) ) FD_LOG_ERR(( #x " was unexpectedly NULL" )); \
643 0 : __x; }))
644 :
645 0 : ctx->kind_id = tile->kind_id;
646 0 : ctx->blake3 = NONNULL( fd_blake3_join( fd_blake3_new( blake3 ) ) );
647 0 : ctx->bmtree = NONNULL( bmtree );
648 :
649 0 : NONNULL( fd_pack_rebate_sum_join( fd_pack_rebate_sum_new( ctx->rebater, ctx->rebate_seed ) ) );
650 0 : ctx->rebates_for_slot = 0UL;
651 0 : ctx->rebate_microblock_cnt = 0UL;
652 0 : ctx->rebate_idle_loop_cnt = 0UL;
653 0 : ctx->rebate_draining = 0;
654 :
655 0 : ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "execle_busy.%lu", tile->kind_id );
656 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
657 0 : ctx->busy_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
658 0 : if( FD_UNLIKELY( !ctx->busy_fseq ) ) FD_LOG_ERR(( "banking tile %lu has no busy flag", tile->kind_id ));
659 :
660 0 : memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
661 :
662 0 : ctx->pack_in_mem = topo->workspaces[ topo->objs[ topo->links[ tile->in_link_id[ 0UL ] ].dcache_obj_id ].wksp_id ].wksp;
663 0 : ctx->pack_in_chunk0 = fd_dcache_compact_chunk0( ctx->pack_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache );
664 0 : ctx->pack_in_wmark = fd_dcache_compact_wmark ( ctx->pack_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache, topo->links[ tile->in_link_id[ 0UL ] ].mtu );
665 :
666 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
667 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
668 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
669 0 : ctx->out_chunk = ctx->out_chunk0;
670 :
671 :
672 0 : ctx->rebate_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
673 0 : ctx->rebate_chunk0 = fd_dcache_compact_chunk0( ctx->rebate_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
674 0 : ctx->rebate_wmark = fd_dcache_compact_wmark ( ctx->rebate_mem, topo->links[ tile->out_link_id[ 1 ] ].dcache, topo->links[ tile->out_link_id[ 1 ] ].mtu );
675 0 : ctx->rebate_chunk = ctx->rebate_chunk0;
676 :
677 0 : ctx->ns_per_tick = 1.f / (float)fd_tempo_tick_per_ns( NULL );
678 0 : }
679 :
680 : /* For a bundle, one bundle might burst into at most 5 separate PoH mixins, since the
681 : microblocks cannot be conflicting. */
682 :
683 0 : #define STEM_BURST (5UL)
684 :
685 : /* See explanation in fd_pack */
686 0 : #define STEM_LAZY (128L*3000L)
687 :
688 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_bank_ctx_t
689 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bank_ctx_t)
690 :
691 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
692 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
693 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
694 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
695 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
696 :
697 : #include "../../disco/stem/fd_stem.c"
698 :
699 : fd_topo_run_tile_t fd_tile_bank = {
700 : .name = "bank",
701 : .scratch_align = scratch_align,
702 : .scratch_footprint = scratch_footprint,
703 : .privileged_init = privileged_init,
704 : .unprivileged_init = unprivileged_init,
705 : .run = stem_run,
706 : };
|