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