LCOV - code coverage report
Current view: top level - discof/bank - fd_bank_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 299 0.0 %
Date: 2025-09-20 04:42:25 Functions: 0 12 0.0 %

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

Generated by: LCOV version 1.14