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