LCOV - code coverage report
Current view: top level - discoh/bank - fd_bank_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 329 0.0 %
Date: 2025-07-13 05:02:02 Functions: 0 10 0.0 %

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

Generated by: LCOV version 1.14