LCOV - code coverage report
Current view: top level - discof/resolv - fd_resolv_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 309 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 12 0.0 %

          Line data    Source code
       1             : #include "fd_resolv_tile.h"
       2             : #include "../../disco/fd_txn_m.h"
       3             : #include "../../disco/topo/fd_topo.h"
       4             : #include "../bank/fd_bank_err.h"
       5             : #include "../replay/fd_replay_tile.h"
       6             : #include "generated/fd_resolv_tile_seccomp.h"
       7             : #include "../../disco/metrics/fd_metrics.h"
       8             : #include "../../flamenco/runtime/fd_system_ids_pp.h"
       9             : #include "../../flamenco/runtime/fd_runtime.h"
      10             : #include "../../flamenco/runtime/fd_bank.h"
      11             : #include "../../util/pod/fd_pod_format.h"
      12             : 
      13             : #if FD_HAS_AVX
      14             : #include "../../util/simd/fd_avx.h"
      15             : #endif
      16             : 
      17           0 : #define IN_KIND_DEDUP  (0)
      18           0 : #define IN_KIND_REPLAY (1)
      19             : 
      20             : struct blockhash {
      21             :   uchar b[ 32 ];
      22             : };
      23             : 
      24             : typedef struct blockhash blockhash_t;
      25             : 
      26             : struct blockhash_map {
      27             :   blockhash_t key;
      28             :   ulong       slot;
      29             : };
      30             : 
      31             : typedef struct blockhash_map blockhash_map_t;
      32             : 
      33             : static const blockhash_t null_blockhash = { 0 };
      34             : 
      35             : /* The blockhash ring holds recent blockhashes, so we can identify when
      36             :    a transaction arrives, what slot it will expire (and can no longer be
      37             :    packed) in.  This is useful so we don't send transactions to pack
      38             :    that are no longer packable.
      39             : 
      40             :    Unfortunately, poorly written transaction senders frequently send
      41             :    transactions from millions of slots ago, so we need a large ring to
      42             :    be able to determine and evict these.  The highest practically useful
      43             :    value here is around 22, which works out to 19 days of blockhash
      44             :    history.  Beyond this, the validator is likely to be restarted, and
      45             :    lose the history anyway. */
      46             : 
      47           0 : #define BLOCKHASH_LG_RING_CNT 22UL
      48           0 : #define BLOCKHASH_RING_LEN   (1UL<<BLOCKHASH_LG_RING_CNT)
      49             : 
      50             : #define MAP_NAME              map
      51           0 : #define MAP_T                 blockhash_map_t
      52           0 : #define MAP_KEY_T             blockhash_t
      53           0 : #define MAP_LG_SLOT_CNT       (BLOCKHASH_LG_RING_CNT+1UL)
      54           0 : #define MAP_KEY_NULL          null_blockhash
      55             : #if FD_HAS_AVX
      56           0 : # define MAP_KEY_INVAL(k)     _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
      57             : #else
      58             : # define MAP_KEY_INVAL(k)     MAP_KEY_EQUAL(k, null_blockhash)
      59             : #endif
      60           0 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp((k0).b,(k1).b, 32UL))
      61             : #define MAP_MEMOIZE           0
      62             : #define MAP_KEY_EQUAL_IS_SLOW 1
      63           0 : #define MAP_KEY_HASH(key)     fd_uint_load_4( (key).b )
      64             : #define MAP_QUERY_OPT         1
      65             : 
      66             : #include "../../util/tmpl/fd_map.c"
      67             : 
      68             : typedef struct {
      69             :   union {
      70             :     ulong pool_next; /* Used when it's released */
      71             :     ulong lru_next;  /* Used when it's acquired */
      72             :   };                 /* .. so it's okay to store them in the same memory */
      73             :   ulong lru_prev;
      74             : 
      75             :   ulong map_next;
      76             :   ulong map_prev;
      77             : 
      78             :   blockhash_t * blockhash;
      79             :   uchar _[ FD_TPU_PARSED_MTU ] __attribute__((aligned(alignof(fd_txn_m_t))));
      80             : } fd_stashed_txn_m_t;
      81             : 
      82             : #define POOL_NAME      pool
      83           0 : #define POOL_T         fd_stashed_txn_m_t
      84           0 : #define POOL_NEXT      pool_next
      85             : #define POOL_IDX_T     ulong
      86             : 
      87             : #include "../../util/tmpl/fd_pool.c"
      88             : 
      89             : /* We'll push at the head, which means the tail is the oldest. */
      90             : #define DLIST_NAME  lru_list
      91             : #define DLIST_ELE_T fd_stashed_txn_m_t
      92           0 : #define DLIST_PREV  lru_prev
      93           0 : #define DLIST_NEXT  lru_next
      94             : 
      95             : #include "../../util/tmpl/fd_dlist.c"
      96             : 
      97             : #define MAP_NAME          map_chain
      98           0 : #define MAP_ELE_T         fd_stashed_txn_m_t
      99             : #define MAP_KEY_T         blockhash_t *
     100           0 : #define MAP_KEY           blockhash
     101           0 : #define MAP_IDX_T         ulong
     102           0 : #define MAP_NEXT          map_next
     103           0 : #define MAP_PREV          map_prev
     104           0 : #define MAP_KEY_HASH(k,s) ((s) ^ fd_ulong_load_8( (*(k))->b ))
     105           0 : #define MAP_KEY_EQ(k0,k1) (!memcmp((*(k0))->b, (*(k1))->b, 32UL))
     106             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
     107             : #define MAP_MULTI         1
     108             : 
     109             : #include "../../util/tmpl/fd_map_chain.c"
     110             : 
     111             : typedef struct {
     112             :   int         kind;
     113             : 
     114             :   fd_wksp_t * mem;
     115             :   ulong       chunk0;
     116             :   ulong       wmark;
     117             :   ulong       mtu;
     118             : } fd_resolv_in_ctx_t;
     119             : 
     120             : typedef struct {
     121             :   fd_wksp_t * mem;
     122             :   ulong       chunk0;
     123             :   ulong       wmark;
     124             :   ulong       chunk;
     125             : } fd_resolv_out_ctx_t;
     126             : 
     127             : typedef struct {
     128             :   ulong round_robin_idx;
     129             :   ulong round_robin_cnt;
     130             : 
     131             :   int   bundle_failed;
     132             :   ulong bundle_id;
     133             : 
     134             :   blockhash_map_t * blockhash_map;
     135             : 
     136             :   ulong flushing_slot;
     137             :   ulong flush_pool_idx;
     138             : 
     139             :   /* In the full client, the resolv tile is passed only a rooted bank
     140             :      index from replay whenever the root is advanced.
     141             : 
     142             :      This is enough to query the accounts database for that bank and
     143             :      retrieve the address lookup tables.  Because of lifetime concerns
     144             :      around bank ownership, the replay tile is solely responsible for
     145             :      freeing the bank when it is no longer needed.  To facilitate this,
     146             :      the resolv tile sends a message to replay when it is done with a
     147             :      rooted bank (after exchanging it for a new rooted bank). */
     148             :   fd_banks_t * banks;
     149             :   fd_bank_t *  bank;
     150             :   fd_funk_t    funk[1];
     151             : 
     152             :   fd_stashed_txn_m_t * pool;
     153             :   map_chain_t *        map_chain;
     154             :   lru_list_t           lru_list[1];
     155             : 
     156             :   ulong completed_slot;
     157             :   ulong blockhash_ring_idx;
     158             :   blockhash_t blockhash_ring[ BLOCKHASH_RING_LEN ];
     159             : 
     160             :   fd_replay_root_advanced_t  _rooted_slot_msg;
     161             :   fd_replay_slot_completed_t _completed_slot_msg;
     162             : 
     163             :   struct {
     164             :     ulong lut[ FD_METRICS_COUNTER_RESOLV_LUT_RESOLVED_CNT ];
     165             :     ulong blockhash_expired;
     166             :     ulong bundle_peer_failure;
     167             :     ulong stash[ FD_METRICS_COUNTER_RESOLV_STASH_OPERATION_CNT ];
     168             :   } metrics;
     169             : 
     170             :   fd_resolv_in_ctx_t in[ 64UL ];
     171             : 
     172             :   fd_resolv_out_ctx_t out_pack[ 1UL ];
     173             :   fd_resolv_out_ctx_t out_replay[ 1UL ];
     174             : } fd_resolv_ctx_t;
     175             : 
     176             : FD_FN_CONST static inline ulong
     177           0 : scratch_align( void ) {
     178           0 :   return alignof( fd_resolv_ctx_t );
     179           0 : }
     180             : 
     181             : FD_FN_PURE static inline ulong
     182           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     183           0 :   (void)tile;
     184           0 :   ulong l = FD_LAYOUT_INIT;
     185           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t )        );
     186           0 :   l = FD_LAYOUT_APPEND( l, pool_align(),               pool_footprint     ( 1UL<<16UL ) );
     187           0 :   l = FD_LAYOUT_APPEND( l, map_chain_align(),          map_chain_footprint( 8192UL    ) );
     188           0 :   l = FD_LAYOUT_APPEND( l, map_align(),                map_footprint()                  );
     189           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     190           0 : }
     191             : 
     192             : static inline void
     193           0 : metrics_write( fd_resolv_ctx_t * ctx ) {
     194           0 :   FD_MCNT_SET(       RESOLF, BLOCKHASH_EXPIRED,               ctx->metrics.blockhash_expired );
     195           0 :   FD_MCNT_ENUM_COPY( RESOLF, LUT_RESOLVED,                    ctx->metrics.lut );
     196           0 :   FD_MCNT_ENUM_COPY( RESOLF, STASH_OPERATION,                 ctx->metrics.stash );
     197           0 :   FD_MCNT_SET(       RESOLF, TRANSACTION_BUNDLE_PEER_FAILURE, ctx->metrics.bundle_peer_failure );
     198           0 : }
     199             : 
     200             : static int
     201             : before_frag( fd_resolv_ctx_t * ctx,
     202             :              ulong             in_idx,
     203             :              ulong             seq,
     204           0 :              ulong             sig ) {
     205           0 :   (void)sig;
     206             : 
     207           0 :   if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) ) return 0;
     208             : 
     209           0 :   return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx;
     210           0 : }
     211             : 
     212             : static inline void
     213             : during_frag( fd_resolv_ctx_t * ctx,
     214             :              ulong             in_idx,
     215             :              ulong             seq FD_PARAM_UNUSED,
     216             :              ulong             sig FD_PARAM_UNUSED,
     217             :              ulong             chunk,
     218             :              ulong             sz,
     219           0 :              ulong             ctl FD_PARAM_UNUSED ) {
     220             : 
     221           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     222           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     223             : 
     224           0 :   switch( ctx->in[in_idx].kind ) {
     225           0 :     case IN_KIND_DEDUP: {
     226           0 :       uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
     227           0 :       uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     228           0 :       fd_memcpy( dst, src, sz );
     229           0 :       break;
     230           0 :     }
     231           0 :     case IN_KIND_REPLAY: {
     232           0 :       if( FD_UNLIKELY( sig==REPLAY_SIG_ROOT_ADVANCED ) ) {
     233           0 :         ctx->_rooted_slot_msg = *(fd_replay_root_advanced_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
     234           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
     235           0 :         ctx->_completed_slot_msg = *(fd_replay_slot_completed_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
     236           0 :       }
     237           0 :       break;
     238           0 :     }
     239           0 :     default:
     240           0 :       FD_LOG_ERR(( "unknown in kind %d", ctx->in[in_idx].kind ));
     241           0 :   }
     242           0 : }
     243             : 
     244             : static inline int
     245             : publish_txn( fd_resolv_ctx_t *          ctx,
     246             :              fd_stem_context_t *        stem,
     247           0 :              fd_stashed_txn_m_t const * stashed ) {
     248           0 :   fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     249           0 :   fd_memcpy( txnm, stashed->_, fd_txn_m_realized_footprint( (fd_txn_m_t *)stashed->_, 1, 0 ) );
     250             : 
     251           0 :   fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
     252             : 
     253           0 :   txnm->reference_slot = ctx->flushing_slot;
     254             : 
     255           0 :   if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
     256           0 :     if( FD_UNLIKELY( !ctx->bank ) ) {
     257           0 :       FD_MCNT_INC( RESOLF, NO_BANK_DROP, 1 );
     258           0 :       return 0;
     259           0 :     }
     260             : 
     261           0 :     fd_sysvar_cache_t const * sysvar_cache = fd_bank_sysvar_cache_query( ctx->bank );
     262           0 :     FD_TEST( sysvar_cache );
     263             : 
     264           0 :     fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( ctx->bank ), fd_bank_slot_get( ctx->bank ) } };
     265             : 
     266           0 :     fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( sysvar_cache );
     267             : 
     268           0 :     int result = fd_runtime_load_txn_address_lookup_tables( txnt,
     269           0 :                                                             fd_txn_m_payload( txnm ),
     270           0 :                                                             ctx->funk,
     271           0 :                                                             &xid,
     272           0 :                                                             fd_bank_slot_get( ctx->bank ),
     273           0 :                                                             slot_hashes,
     274           0 :                                                             fd_txn_m_alut( txnm ) );
     275           0 :     fd_sysvar_cache_slot_hashes_leave_const( sysvar_cache, slot_hashes );
     276           0 :     ctx->metrics.lut[ result ]++;
     277           0 :     if( FD_UNLIKELY( result ) ) return 0;
     278           0 :   }
     279             : 
     280           0 :   ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
     281           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     282           0 :   fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, 0UL, tspub );
     283           0 :   ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
     284             : 
     285           0 :   return 1;
     286           0 : }
     287             : 
     288             : static inline void
     289             : after_credit( fd_resolv_ctx_t *   ctx,
     290             :               fd_stem_context_t * stem,
     291             :               int *               opt_poll_in,
     292           0 :               int *               charge_busy ) {
     293           0 :   if( FD_LIKELY( ctx->flush_pool_idx==ULONG_MAX ) ) return;
     294             : 
     295           0 :   *charge_busy = 1;
     296           0 :   *opt_poll_in = 0;
     297             : 
     298           0 :   ulong next = map_chain_idx_next_const( ctx->flush_pool_idx, ULONG_MAX, ctx->pool );
     299           0 :   map_chain_idx_remove_fast( ctx->map_chain, ctx->flush_pool_idx, ctx->pool );
     300           0 :   if( FD_LIKELY( publish_txn( ctx, stem, pool_ele( ctx->pool, ctx->flush_pool_idx ) ) ) ) {
     301           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_PUBLISHED_IDX ]++;
     302           0 :   } else {
     303           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_REMOVED_IDX ]++;
     304           0 :   }
     305           0 :   lru_list_idx_remove( ctx->lru_list, ctx->flush_pool_idx, ctx->pool );
     306           0 :   pool_idx_release( ctx->pool, ctx->flush_pool_idx );
     307           0 :   ctx->flush_pool_idx = next;
     308           0 : }
     309             : 
     310             : /* Returns 0 if not a durable nonce transaction and 1 if it may be a
     311             :    durable nonce transaction */
     312             : 
     313             : FD_FN_PURE static inline int
     314             : fd_resolv_is_durable_nonce( fd_txn_t const * txn,
     315           0 :                             uchar    const * payload ) {
     316           0 :   if( FD_UNLIKELY( txn->instr_cnt==0 ) ) return 0;
     317             : 
     318           0 :   fd_txn_instr_t const * ix0 = &txn->instr[ 0 ];
     319           0 :   fd_acct_addr_t const * prog0 = fd_txn_get_acct_addrs( txn, payload ) + ix0->program_id;
     320             :   /* First instruction must be SystemProgram nonceAdvance instruction */
     321           0 :   fd_acct_addr_t const system_program[1] = { { { SYS_PROG_ID } } };
     322           0 :   if( FD_LIKELY( memcmp( prog0, system_program, sizeof(fd_acct_addr_t) ) ) )        return 0;
     323             : 
     324             :   /* instruction with three accounts and a four byte instruction data, a
     325             :      little-endian uint value 4 */
     326           0 :   if( FD_UNLIKELY( (ix0->data_sz!=4) | (ix0->acct_cnt!=3) ) ) return 0;
     327             : 
     328           0 :   return fd_uint_load_4( payload + ix0->data_off )==4U;
     329           0 : }
     330             : 
     331             : static inline void
     332             : after_frag( fd_resolv_ctx_t *   ctx,
     333             :             ulong               in_idx,
     334             :             ulong               seq,
     335             :             ulong               sig,
     336             :             ulong               sz,
     337             :             ulong               tsorig,
     338             :             ulong               _tspub,
     339           0 :             fd_stem_context_t * stem ) {
     340           0 :   (void)seq;
     341           0 :   (void)sz;
     342           0 :   (void)_tspub;
     343             : 
     344           0 :   if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) ) {
     345           0 :     switch( sig ) {
     346           0 :       case REPLAY_SIG_SLOT_COMPLETED: {
     347           0 :         fd_replay_slot_completed_t const * msg = &ctx->_completed_slot_msg;
     348             : 
     349             :         /* blockhash_ring is initalized to all zeros. blockhash=0 is an illegal map query */
     350           0 :         if( FD_UNLIKELY( memcmp( &ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], (uchar[ 32UL ]){ 0UL }, sizeof(blockhash_t) ) ) ) {
     351           0 :           blockhash_map_t * entry = map_query( ctx->blockhash_map, ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], NULL );
     352           0 :           if( FD_LIKELY( entry ) ) map_remove( ctx->blockhash_map, entry );
     353           0 :         }
     354             : 
     355           0 :         memcpy( ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ].b, msg->block_hash.uc, 32UL );
     356           0 :         ctx->blockhash_ring_idx++;
     357             : 
     358           0 :         blockhash_map_t * blockhash = map_insert( ctx->blockhash_map, *(blockhash_t *)msg->block_hash.uc );
     359           0 :         blockhash->slot = msg->slot;
     360             : 
     361           0 :         blockhash_t * hash = (blockhash_t *)msg->block_hash.uc;
     362           0 :         ctx->flush_pool_idx  = map_chain_idx_query_const( ctx->map_chain, &hash, ULONG_MAX, ctx->pool );
     363           0 :         ctx->flushing_slot   = msg->slot;
     364             : 
     365           0 :         ctx->completed_slot = msg->slot;
     366           0 :         break;
     367           0 :       }
     368           0 :       case REPLAY_SIG_ROOT_ADVANCED: {
     369           0 :         fd_replay_root_advanced_t const * msg = &ctx->_rooted_slot_msg;
     370             : 
     371             :         /* Replace current bank with new bank */
     372           0 :         fd_bank_t * prev_bank = ctx->bank;
     373             : 
     374           0 :         ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
     375           0 :         FD_TEST( ctx->bank );
     376             : 
     377             :         /* Send slot completed message back to replay, so it can
     378             :            decrement the reference count of the previous bank. */
     379           0 :         if( FD_LIKELY( prev_bank ) ) {
     380           0 :           ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     381           0 :           fd_resolv_slot_exchanged_t * slot_exchanged =
     382           0 :             fd_type_pun( fd_chunk_to_laddr( ctx->out_replay->mem, ctx->out_replay->chunk ) );
     383           0 :           slot_exchanged->bank_idx = prev_bank->idx;
     384           0 :           fd_stem_publish( stem, 1UL, 0UL, ctx->out_replay->chunk, sizeof(fd_resolv_slot_exchanged_t), 0UL, tsorig, tspub );
     385           0 :           ctx->out_replay->chunk = fd_dcache_compact_next( ctx->out_replay->chunk, sizeof(fd_resolv_slot_exchanged_t), ctx->out_replay->chunk0, ctx->out_replay->wmark );
     386           0 :         }
     387             : 
     388           0 :         break;
     389           0 :       }
     390           0 :       default: break;
     391           0 :     }
     392           0 :     return;
     393           0 :   }
     394             : 
     395           0 :   fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     396           0 :   FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
     397           0 :   FD_TEST( txnm->txn_t_sz<=FD_TXN_MAX_SZ );
     398           0 :   fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
     399             : 
     400             :   /* If we find the recent blockhash, life is simple.  We drop
     401             :      transactions that couldn't possibly execute any more, and forward
     402             :      to pack ones that could.
     403             : 
     404             :      If we can't find the recent blockhash ... it means one of four
     405             :      things,
     406             : 
     407             :      (1) The blockhash is really old (more than 19 days) or just
     408             :          non-existent.
     409             :      (2) The blockhash is not that old, but was created before this
     410             :          validator was started.
     411             :      (3) It's really new (we haven't seen the bank yet).
     412             :      (4) It's a durable nonce transaction, or part of a bundle (just let
     413             :          it pass).
     414             : 
     415             :     For durable nonce transactions, there isn't much we can do except
     416             :     pass them along and see if they execute.
     417             : 
     418             :     For the other three cases ... we don't want to flood pack with what
     419             :     might be junk transactions, so we accumulate them into a local
     420             :     buffer.  If we later see the blockhash come to exist, we forward any
     421             :     buffered transactions to back. */
     422             : 
     423           0 :   if( FD_UNLIKELY( txnm->block_engine.bundle_id && (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
     424           0 :     ctx->bundle_failed = 0;
     425           0 :     ctx->bundle_id     = txnm->block_engine.bundle_id;
     426           0 :   }
     427             : 
     428           0 :   if( FD_UNLIKELY( txnm->block_engine.bundle_id && ctx->bundle_failed ) ) {
     429           0 :     ctx->metrics.bundle_peer_failure++;
     430           0 :     return;
     431           0 :   }
     432             : 
     433           0 :   txnm->reference_slot = ctx->completed_slot;
     434           0 :   blockhash_map_t const * blockhash = map_query_const( ctx->blockhash_map, *(blockhash_t*)( fd_txn_m_payload( txnm )+txnt->recent_blockhash_off ), NULL );
     435           0 :   if( FD_LIKELY( blockhash ) ) {
     436           0 :     txnm->reference_slot = blockhash->slot;
     437           0 :     if( FD_UNLIKELY( txnm->reference_slot+151UL<ctx->completed_slot ) ) {
     438           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     439           0 :       ctx->metrics.blockhash_expired++;
     440           0 :       return;
     441           0 :     }
     442           0 :   }
     443             : 
     444           0 :   int is_bundle_member = !!txnm->block_engine.bundle_id;
     445           0 :   int is_durable_nonce = fd_resolv_is_durable_nonce( txnt, fd_txn_m_payload( txnm ) );
     446             : 
     447           0 :   if( FD_UNLIKELY( !is_bundle_member && !is_durable_nonce && !blockhash ) ) {
     448           0 :     ulong pool_idx;
     449           0 :     if( FD_UNLIKELY( !pool_free( ctx->pool ) ) ) {
     450           0 :       pool_idx = lru_list_idx_pop_tail( ctx->lru_list, ctx->pool );
     451           0 :       map_chain_idx_remove_fast( ctx->map_chain, pool_idx, ctx->pool );
     452           0 :       ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_OVERRUN_IDX ]++;
     453           0 :     } else {
     454           0 :       pool_idx = pool_idx_acquire( ctx->pool );
     455           0 :     }
     456             : 
     457           0 :     fd_stashed_txn_m_t * stash_txn = pool_ele( ctx->pool, pool_idx );
     458             :     /* There's a compiler bug in GCC version 12 (at least 12.1, 12.3 and
     459             :        12.4) that cause it to think stash_txn is a null pointer.  It
     460             :        then complains that the memcpy is bad and refuses to compile the
     461             :        memcpy below.  It is possible for pool_ele to return NULL, but
     462             :        that can't happen because if pool_free is 0, then all the pool
     463             :        elements must be in the LRU list, so idx_pop_tail won't return
     464             :        IDX_NULL; and if pool_free returns non-zero, then
     465             :        pool_idx_acquire won't return POOL_IDX_NULL. */
     466           0 :     FD_COMPILER_FORGET( stash_txn );
     467           0 :     fd_memcpy( stash_txn->_, txnm, fd_txn_m_realized_footprint( txnm, 1, 0 ) );
     468           0 :     stash_txn->blockhash = (blockhash_t *)(fd_txn_m_payload( (fd_txn_m_t *)(stash_txn->_) ) + txnt->recent_blockhash_off);
     469           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_INSERTED_IDX ]++;
     470             : 
     471           0 :     map_chain_ele_insert( ctx->map_chain, stash_txn, ctx->pool );
     472           0 :     lru_list_idx_push_head( ctx->lru_list, pool_idx, ctx->pool );
     473             : 
     474           0 :     return;
     475           0 :   }
     476             : 
     477           0 :   if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
     478           0 :     if( FD_UNLIKELY( !ctx->bank ) ) {
     479           0 :       FD_MCNT_INC( RESOLF, NO_BANK_DROP, 1 );
     480           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     481           0 :       return;
     482           0 :     }
     483             : 
     484           0 :     fd_sysvar_cache_t const * sysvar_cache = fd_bank_sysvar_cache_query( ctx->bank );
     485           0 :     FD_TEST( sysvar_cache );
     486           0 :     fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( sysvar_cache );
     487           0 :     FD_TEST( slot_hashes );
     488             : 
     489           0 :     fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( ctx->bank ), fd_bank_slot_get( ctx->bank ) } };
     490             : 
     491           0 :     int result = fd_runtime_load_txn_address_lookup_tables( txnt,
     492           0 :                                                             fd_txn_m_payload( txnm ),
     493           0 :                                                             ctx->funk,
     494           0 :                                                             &xid,
     495           0 :                                                             fd_bank_slot_get( ctx->bank ),
     496           0 :                                                             slot_hashes,
     497           0 :                                                             fd_txn_m_alut( txnm ) );
     498           0 :     fd_sysvar_cache_slot_hashes_leave_const( sysvar_cache, slot_hashes );
     499           0 :     ctx->metrics.lut[ -fd_bank_lut_err_from_runtime_err( result ) ]++;
     500           0 :     if( FD_UNLIKELY( result ) ) {
     501           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     502           0 :       return;
     503           0 :     }
     504           0 :   }
     505             : 
     506           0 :   ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
     507           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     508           0 :   fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, tsorig, tspub );
     509           0 :   ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
     510           0 : }
     511             : 
     512             : static void
     513             : unprivileged_init( fd_topo_t *      topo,
     514           0 :                    fd_topo_tile_t * tile ) {
     515           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     516             : 
     517           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     518           0 :   fd_resolv_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t ) );
     519             : 
     520           0 :   ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
     521           0 :   ctx->round_robin_idx = tile->kind_id;
     522             : 
     523           0 :   ctx->bundle_failed = 0;
     524           0 :   ctx->bundle_id     = 0UL;
     525             : 
     526           0 :   ctx->completed_slot = 0UL;
     527           0 :   ctx->blockhash_ring_idx = 0UL;
     528             : 
     529           0 :   ctx->flush_pool_idx = ULONG_MAX;
     530             : 
     531           0 :   ctx->pool = pool_join( pool_new( FD_SCRATCH_ALLOC_APPEND( l, pool_align(), pool_footprint( 1UL<<16UL ) ), 1UL<<16UL ) );
     532           0 :   FD_TEST( ctx->pool );
     533             : 
     534           0 :   ctx->map_chain = map_chain_join( map_chain_new( FD_SCRATCH_ALLOC_APPEND( l, map_chain_align(), map_chain_footprint( 8192ULL ) ), 8192UL , 0UL ) );
     535           0 :   FD_TEST( ctx->map_chain );
     536             : 
     537           0 :   FD_TEST( ctx->lru_list==lru_list_join( lru_list_new( ctx->lru_list ) ) );
     538             : 
     539           0 :   memset( ctx->blockhash_ring, 0, sizeof( ctx->blockhash_ring ) );
     540           0 :   memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
     541             : 
     542           0 :   ctx->blockhash_map = map_join( map_new( FD_SCRATCH_ALLOC_APPEND( l, map_align(), map_footprint() ) ) );
     543           0 :   FD_TEST( ctx->blockhash_map );
     544             : 
     545           0 :   FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
     546           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     547           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
     548           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     549             : 
     550           0 :     if( FD_LIKELY(      !strcmp( link->name, "replay_out"   ) ) ) ctx->in[ i ].kind = IN_KIND_REPLAY;
     551           0 :     else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in[ i ].kind = IN_KIND_DEDUP;
     552           0 :     else FD_LOG_ERR(( "unknown in link name '%s'", link->name ));
     553             : 
     554           0 :     ctx->in[i].mem    = link_wksp->wksp;
     555           0 :     ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
     556           0 :     ctx->in[i].wmark  = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
     557           0 :     ctx->in[i].mtu    = link->mtu;
     558           0 :   }
     559             : 
     560           0 :   ctx->out_pack->mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
     561           0 :   ctx->out_pack->chunk0 = fd_dcache_compact_chunk0( ctx->out_pack->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
     562           0 :   ctx->out_pack->wmark  = fd_dcache_compact_wmark ( ctx->out_pack->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
     563           0 :   ctx->out_pack->chunk  = ctx->out_pack->chunk0;
     564             : 
     565           0 :   ctx->out_replay->mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
     566           0 :   ctx->out_replay->chunk0 = fd_dcache_compact_chunk0( ctx->out_replay->mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
     567           0 :   ctx->out_replay->wmark  = fd_dcache_compact_wmark ( ctx->out_replay->mem, topo->links[ tile->out_link_id[ 1 ] ].dcache, topo->links[ tile->out_link_id[ 1 ] ].mtu );
     568           0 :   ctx->out_replay->chunk  = ctx->out_replay->chunk0;
     569             : 
     570           0 :   FD_TEST( fd_funk_join( ctx->funk, fd_topo_obj_laddr( topo, tile->resolv.funk_obj_id ) ) );
     571             : 
     572           0 :   ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
     573           0 :   FD_TEST( banks_obj_id!=ULONG_MAX );
     574           0 :   ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
     575           0 :   FD_TEST( ctx->banks );
     576           0 :   ctx->bank = NULL;
     577             : 
     578           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     579           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     580           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     581           0 : }
     582             : 
     583             : static ulong
     584             : populate_allowed_seccomp( fd_topo_t const *      topo,
     585             :                           fd_topo_tile_t const * tile,
     586             :                           ulong                  out_cnt,
     587           0 :                           struct sock_filter *   out ) {
     588           0 :   (void)topo;
     589           0 :   (void)tile;
     590             : 
     591           0 :   populate_sock_filter_policy_fd_resolv_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     592           0 :   return sock_filter_policy_fd_resolv_tile_instr_cnt;
     593           0 : }
     594             : 
     595             : static ulong
     596             : populate_allowed_fds( fd_topo_t const *      topo,
     597             :                       fd_topo_tile_t const * tile,
     598             :                       ulong                  out_fds_cnt,
     599           0 :                       int *                  out_fds ) {
     600           0 :   (void)topo;
     601           0 :   (void)tile;
     602             : 
     603           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     604             : 
     605           0 :   ulong out_cnt = 0UL;
     606           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     607           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     608           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     609           0 :   return out_cnt;
     610           0 : }
     611             : 
     612           0 : #define STEM_BURST (1UL)
     613             : 
     614           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_resolv_ctx_t
     615           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_resolv_ctx_t)
     616             : 
     617           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     618           0 : #define STEM_CALLBACK_AFTER_CREDIT  after_credit
     619           0 : #define STEM_CALLBACK_BEFORE_FRAG   before_frag
     620           0 : #define STEM_CALLBACK_DURING_FRAG   during_frag
     621           0 : #define STEM_CALLBACK_AFTER_FRAG    after_frag
     622             : 
     623             : #include "../../disco/stem/fd_stem.c"
     624             : 
     625             : fd_topo_run_tile_t fd_tile_resolv = {
     626             :   .name                     = "resolv",
     627             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     628             :   .populate_allowed_fds     = populate_allowed_fds,
     629             :   .scratch_align            = scratch_align,
     630             :   .scratch_footprint        = scratch_footprint,
     631             :   .unprivileged_init        = unprivileged_init,
     632             :   .run                      = stem_run,
     633             : };

Generated by: LCOV version 1.14