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