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