LCOV - code coverage report
Current view: top level - discof/resolv - fd_resolv_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 355 0.0 %
Date: 2026-09-17 04:28:31 Functions: 0 30 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 "../replay/fd_replay_tile.h"
       5             : #include "../../discof/fd_startup.h"
       6             : #include "../../disco/metrics/fd_metrics.h"
       7             : #include "../../flamenco/accdb/fd_accdb.h"
       8             : #include "../../flamenco/accdb/fd_accdb_shmem.h"
       9             : #include "../../flamenco/runtime/fd_alut.h"
      10             : #include "../../flamenco/runtime/fd_runtime_const.h"
      11             : #include "../../flamenco/runtime/fd_system_ids_pp.h"
      12             : #include "../../flamenco/runtime/fd_bank.h"
      13             : #include "../../tango/fseq/fd_fseq.h"
      14             : #include "../../util/pod/fd_pod_format.h"
      15             : #include "../../util/fd_hash32.h"
      16             : 
      17             : #include <time.h>
      18             : #include "generated/fd_resolv_tile_seccomp.h"
      19             : 
      20             : #if FD_HAS_AVX
      21             : #include "../../util/simd/fd_avx.h"
      22             : #endif
      23             : 
      24           0 : #define IN_KIND_DEDUP  (0)
      25           0 : #define IN_KIND_REPLAY (1)
      26             : 
      27             : struct blockhash {
      28             :   uchar b[ 32 ];
      29             : };
      30             : 
      31             : typedef struct blockhash blockhash_t;
      32             : 
      33             : struct blockhash_map {
      34             :   blockhash_t key;
      35             :   ulong       slot;
      36             : };
      37             : 
      38             : typedef struct blockhash_map blockhash_map_t;
      39             : 
      40             : static const blockhash_t null_blockhash = { 0 };
      41             : 
      42             : /* The blockhash ring holds recent blockhashes, so we can identify when
      43             :    a transaction arrives, what slot it will expire (and can no longer be
      44             :    packed) in.  This is useful so we don't send transactions to pack
      45             :    that are no longer packable.
      46             : 
      47             :    Unfortunately, poorly written transaction senders frequently send
      48             :    transactions from millions of slots ago, so we need a large ring to
      49             :    be able to determine and evict these.  The highest practically useful
      50             :    value here is around 22, which works out to 19 days of blockhash
      51             :    history.  Beyond this, the validator is likely to be restarted, and
      52             :    lose the history anyway. */
      53             : 
      54           0 : #define BLOCKHASH_LG_RING_CNT 22UL
      55           0 : #define BLOCKHASH_RING_LEN   (1UL<<BLOCKHASH_LG_RING_CNT)
      56             : 
      57             : #define MAP_NAME               map
      58           0 : #define MAP_T                  blockhash_map_t
      59           0 : #define MAP_KEY_T              blockhash_t
      60           0 : #define MAP_LG_SLOT_CNT        (BLOCKHASH_LG_RING_CNT+1UL)
      61           0 : #define MAP_KEY_NULL           null_blockhash
      62             : #if FD_HAS_AVX
      63           0 : # define MAP_KEY_INVAL(k)      _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
      64             : #else
      65             : # define MAP_KEY_INVAL(k)      MAP_KEY_EQUAL(k, null_blockhash)
      66             : #endif
      67           0 : #define MAP_KEY_EQUAL(k0,k1)   (!memcmp((k0).b,(k1).b, 32UL))
      68             : #define MAP_MEMOIZE            0
      69             : #define MAP_KEY_EQUAL_IS_SLOW  1
      70           0 : #define MAP_KEY_HASH(key,seed) ((uint)fd_hash32( (key).b, (seed) ))
      71             : #define MAP_QUERY_OPT          1
      72             : 
      73             : #include "../../util/tmpl/fd_map_dynamic.c"
      74             : 
      75             : typedef struct {
      76             :   union {
      77             :     ulong pool_next; /* Used when it's released */
      78             :     ulong lru_next;  /* Used when it's acquired */
      79             :   };                 /* .. so it's okay to store them in the same memory */
      80             :   ulong lru_prev;
      81             : 
      82             :   ulong map_next;
      83             :   ulong map_prev;
      84             : 
      85             :   blockhash_t * blockhash;
      86             :   uchar _[ FD_TPU_PARSED_MTU ] __attribute__((aligned(alignof(fd_txn_m_t))));
      87             : } fd_stashed_txn_m_t;
      88             : 
      89             : #define POOL_NAME      pool
      90           0 : #define POOL_T         fd_stashed_txn_m_t
      91           0 : #define POOL_NEXT      pool_next
      92             : #define POOL_IDX_T     ulong
      93             : 
      94             : #include "../../util/tmpl/fd_pool.c"
      95             : 
      96             : /* We'll push at the head, which means the tail is the oldest. */
      97             : #define DLIST_NAME  lru_list
      98             : #define DLIST_ELE_T fd_stashed_txn_m_t
      99           0 : #define DLIST_PREV  lru_prev
     100           0 : #define DLIST_NEXT  lru_next
     101             : 
     102             : #include "../../util/tmpl/fd_dlist.c"
     103             : 
     104             : #define MAP_NAME          map_chain
     105           0 : #define MAP_ELE_T         fd_stashed_txn_m_t
     106             : #define MAP_KEY_T         blockhash_t *
     107           0 : #define MAP_KEY           blockhash
     108           0 : #define MAP_IDX_T         ulong
     109           0 : #define MAP_NEXT          map_next
     110           0 : #define MAP_PREV          map_prev
     111           0 : #define MAP_KEY_HASH(k,s) fd_hash32( (*(k))->b, (s) )
     112           0 : #define MAP_KEY_EQ(k0,k1) (!memcmp((*(k0))->b, (*(k1))->b, 32UL))
     113             : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
     114             : #define MAP_MULTI         1
     115             : 
     116             : #include "../../util/tmpl/fd_map_chain.c"
     117             : 
     118             : typedef struct {
     119             :   int         kind;
     120             : 
     121             :   fd_wksp_t * mem;
     122             :   ulong       chunk0;
     123             :   ulong       wmark;
     124             :   ulong       mtu;
     125             : } fd_resolv_in_ctx_t;
     126             : 
     127             : typedef struct {
     128             :   fd_wksp_t * mem;
     129             :   ulong       chunk0;
     130             :   ulong       wmark;
     131             :   ulong       chunk;
     132             : } fd_resolv_out_ctx_t;
     133             : 
     134             : typedef struct {
     135             :   ulong round_robin_idx;
     136             :   ulong round_robin_cnt;
     137             :   ulong map_seed;
     138             : 
     139             :   int   bundle_failed;
     140             :   ulong bundle_id;
     141             : 
     142             :   blockhash_map_t * blockhash_map;
     143             : 
     144             :   ulong flushing_slot;
     145             :   ulong flush_pool_idx;
     146             : 
     147             :   /* In the full client, the resolv tile is passed only a rooted bank
     148             :      index from replay whenever the root is advanced.
     149             : 
     150             :      This is enough to query the accounts database for that bank and
     151             :      retrieve the address lookup tables.  Because of lifetime concerns
     152             :      around bank ownership, the replay tile is solely responsible for
     153             :      freeing the bank when it is no longer needed.  To facilitate this,
     154             :      the resolv tile sends a message to replay when it is done with a
     155             :      rooted bank (after exchanging it for a new rooted bank). */
     156             :   fd_banks_t * banks;
     157             :   fd_bank_t * bank;
     158             :   fd_accdb_t * accdb;
     159             : 
     160             :   fd_stashed_txn_m_t * pool;
     161             :   map_chain_t *        map_chain;
     162             :   lru_list_t           lru_list[1];
     163             : 
     164             :   fd_startup_gate_t startup_gate[1];
     165             : 
     166             :   ulong completed_slot;
     167             :   ulong blockhash_ring_idx;
     168             :   blockhash_t blockhash_ring[ BLOCKHASH_RING_LEN ];
     169             : 
     170             :   fd_replay_root_advanced_t  _rooted_slot_msg;
     171             :   fd_replay_slot_completed_t _completed_slot_msg;
     172             : 
     173             :   struct {
     174             :     ulong lut[ FD_METRICS_COUNTER_RESOLV_LUT_RESOLVED_CNT ];
     175             :     ulong blockhash_expired;
     176             :     ulong bundle_peer_failure;
     177             :     ulong stash[ FD_METRICS_COUNTER_RESOLV_STASH_OPERATION_CNT ];
     178             :   } metrics;
     179             : 
     180             :   fd_resolv_in_ctx_t in[ 64UL ];
     181             : 
     182             :   fd_resolv_out_ctx_t out_pack[ 1UL ];
     183             :   fd_resolv_out_ctx_t out_replay[ 1UL ];
     184             : 
     185             :   /* Scratch buffers for fd_accdb_read_one_nocache.  RO accdb joiners
     186             :      must use the nocache API (see fd_accdb.h), which writes the account
     187             :      data into caller-provided buffers rather than returning a pointer
     188             :      into the cache.  Reused across alut reads; peek_alut consumes the
     189             :      bytes synchronously inside fd_alut_interp_next. */
     190             :   uchar alut_owner[ 32UL ];
     191             :   uchar alut_data[ FD_RUNTIME_ACC_SZ_MAX ];
     192             : } fd_resolv_ctx_t;
     193             : 
     194             : FD_FN_CONST static inline ulong
     195           0 : scratch_align( void ) {
     196           0 :   return fd_ulong_max( fd_ulong_max( alignof( fd_resolv_ctx_t ), pool_align() ), fd_ulong_max( map_chain_align(), map_align() ) );
     197           0 : }
     198             : 
     199             : FD_FN_PURE static inline ulong
     200           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     201           0 :   ulong l = FD_LAYOUT_INIT;
     202           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t )                          );
     203           0 :   l = FD_LAYOUT_APPEND( l, pool_align(),               pool_footprint     ( 1UL<<16UL )                   );
     204           0 :   l = FD_LAYOUT_APPEND( l, map_chain_align(),          map_chain_footprint( 8192UL    )                   );
     205           0 :   l = FD_LAYOUT_APPEND( l, map_align(),                map_footprint( MAP_LG_SLOT_CNT )                   );
     206           0 :   l = FD_LAYOUT_APPEND( l, fd_accdb_align(),           fd_accdb_footprint( tile->resolv.max_live_slots )  );
     207           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     208           0 : }
     209             : 
     210             : static inline void
     211           0 : metrics_write( fd_resolv_ctx_t * ctx ) {
     212           0 :   FD_MCNT_SET(       RESOLV, BLOCKHASH_EXPIRED,               ctx->metrics.blockhash_expired );
     213           0 :   FD_MCNT_ENUM_COPY( RESOLV, LUT_RESOLVED,                    ctx->metrics.lut );
     214           0 :   FD_MCNT_ENUM_COPY( RESOLV, STASH_OPERATION,                 ctx->metrics.stash );
     215           0 :   FD_MCNT_SET(       RESOLV, TXN_BUNDLE_PEER_FAILED, ctx->metrics.bundle_peer_failure );
     216             : 
     217           0 :   FD_ACCDB_METRICS_WRITE_RO( RESOLV, fd_accdb_metrics( ctx->accdb ) );
     218           0 : }
     219             : 
     220             : static int
     221             : before_frag( fd_resolv_ctx_t * ctx,
     222             :              ulong             in_idx,
     223             :              ulong             seq,
     224           0 :              ulong             sig ) {
     225           0 :   fd_startup_gate_busy( ctx->startup_gate );
     226             : 
     227           0 :   if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) )
     228           0 :     return sig!=REPLAY_SIG_SLOT_COMPLETED && sig!=REPLAY_SIG_ROOT_ADVANCED;
     229             : 
     230             :   /* Bundle transactions (sig==1) must arrive at pack in order.  Route
     231             :      all bundle traffic to resolv:0. */
     232           0 :   if( FD_UNLIKELY( sig ) ) return ctx->round_robin_idx!=0UL;
     233             : 
     234           0 :   return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx;
     235           0 : }
     236             : 
     237             : static inline void
     238             : during_frag( fd_resolv_ctx_t * ctx,
     239             :              ulong             in_idx,
     240             :              ulong             seq FD_PARAM_UNUSED,
     241             :              ulong             sig FD_PARAM_UNUSED,
     242             :              ulong             chunk,
     243             :              ulong             sz,
     244           0 :              ulong             ctl FD_PARAM_UNUSED ) {
     245             : 
     246           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     247           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 ));
     248             : 
     249           0 :   switch( ctx->in[in_idx].kind ) {
     250           0 :     case IN_KIND_DEDUP: {
     251           0 :       uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
     252           0 :       uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     253           0 :       fd_memcpy( dst, src, sz );
     254           0 :       break;
     255           0 :     }
     256           0 :     case IN_KIND_REPLAY: {
     257           0 :       if( FD_UNLIKELY( sig==REPLAY_SIG_ROOT_ADVANCED ) ) {
     258           0 :         ctx->_rooted_slot_msg = *(fd_replay_root_advanced_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
     259           0 :       } else if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
     260           0 :         ctx->_completed_slot_msg = *(fd_replay_slot_completed_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
     261           0 :       }
     262           0 :       break;
     263           0 :     }
     264           0 :     default:
     265           0 :       FD_LOG_ERR(( "unknown in kind %d", ctx->in[in_idx].kind ));
     266           0 :   }
     267           0 : }
     268             : 
     269             : /* peek_alut reads a single address lookup table from database cache. */
     270             : 
     271             : static int
     272             : peek_alut( fd_resolv_ctx_t *  ctx,
     273             :            fd_txn_m_t *       txnm,
     274             :            fd_alut_interp_t * interp,
     275           0 :            ulong              alut_idx ) {
     276           0 :   fd_txn_t const * txn         = fd_txn_m_txn_t_const  ( txnm );
     277           0 :   uchar const *    txn_payload = fd_txn_m_payload_const( txnm );
     278           0 :   fd_txn_acct_addr_lut_t const * addr_lut = &fd_txn_get_address_tables_const( txn )[ alut_idx ];
     279           0 :   fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, txn_payload+addr_lut->addr_off );
     280             : 
     281             :   /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94
     282             : 
     283             :      The resolv tile maps accdb read-only and so must use the nocache
     284             :      read API; fd_accdb_read_one would mutate writer-only shmem. */
     285           0 :   ulong lamports;
     286           0 :   int   executable;
     287           0 :   ulong data_len;
     288           0 :   fd_accdb_read_one_nocache( ctx->accdb, ctx->bank->accdb_fork_id, addr_lut_acc.uc,
     289           0 :                              &lamports, &executable, ctx->alut_owner, ctx->alut_data, &data_len );
     290           0 :   if( FD_UNLIKELY( !lamports ) ) return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
     291             : 
     292           0 :   return fd_alut_interp_next( interp, &addr_lut_acc, ctx->alut_owner, ctx->alut_data, data_len );
     293           0 : }
     294             : 
     295             : /* peek_aluts reads address lookup tables from database cache.
     296             :    Gracefully recovers from data races and missing accounts. */
     297             : 
     298             : static int
     299             : peek_aluts( fd_resolv_ctx_t * ctx,
     300           0 :             fd_txn_m_t *      txnm ) {
     301             :   /* Unpack context */
     302           0 :   fd_txn_t const *          txn          = fd_txn_m_txn_t_const  ( txnm );
     303           0 :   uchar const *             txn_payload  = fd_txn_m_payload_const( txnm );
     304           0 :   ulong const               alut_cnt     = txn->addr_table_lookup_cnt;
     305           0 :   ulong const               slot         = ctx->bank->f.slot;
     306           0 :   fd_sysvar_cache_t const * sysvar_cache = &ctx->bank->f.sysvar_cache;
     307           0 :   fd_slot_hashes_t slot_hashes_view[1];
     308           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_slot_hashes_view( sysvar_cache, slot_hashes_view ) ) ) {
     309           0 :     FD_LOG_ERR(( "slot hashes sysvar cache is invalid" ));
     310           0 :   }
     311             : 
     312             :   /* Write indirect addrs into here */
     313           0 :   fd_acct_addr_t * indir_addrs = fd_txn_m_alut( txnm );
     314             : 
     315           0 :   int err = FD_RUNTIME_EXECUTE_SUCCESS;
     316           0 :   fd_alut_interp_t interp[1];
     317           0 :   fd_alut_interp_new( interp, indir_addrs, txn, txn_payload, slot_hashes_view, slot );
     318           0 :   for( ulong i=0UL; i<alut_cnt; i++ ) {
     319           0 :     err = peek_alut( ctx, txnm, interp, i );
     320           0 :     if( FD_UNLIKELY( err ) ) break;
     321           0 :   }
     322             : 
     323           0 :   ulong ctr_idx;
     324           0 :   switch( err ) {
     325           0 :   case FD_RUNTIME_EXECUTE_SUCCESS:                            ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_SUCCESS_IDX;               break;
     326           0 :   case FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND:     ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_ACCOUNT_NOT_FOUND_IDX;     break;
     327           0 :   case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_OWNER: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_ACCOUNT_OWNER_IDX; break;
     328           0 :   case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA:  ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_ACCOUNT_DATA_IDX;  break;
     329           0 :   case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_INDEX: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_LOOKUP_INDEX_IDX;  break;
     330           0 :   default:                                                    ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_ACCOUNT_UNINITIALIZED_IDX; break;
     331           0 :   }
     332           0 :   ctx->metrics.lut[ ctr_idx ]++;
     333           0 :   return err;
     334           0 : }
     335             : 
     336             : static int
     337             : publish_txn( fd_resolv_ctx_t *          ctx,
     338             :              fd_stem_context_t *        stem,
     339           0 :              fd_stashed_txn_m_t const * stashed ) {
     340           0 :   fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     341           0 :   fd_memcpy( txnm, stashed->_, fd_txn_m_realized_footprint( (fd_txn_m_t *)stashed->_, 1, 0 ) );
     342             : 
     343           0 :   fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
     344             : 
     345           0 :   txnm->reference_slot = ctx->flushing_slot;
     346             : 
     347           0 :   if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
     348           0 :     if( FD_UNLIKELY( !ctx->bank ) ) {
     349           0 :       FD_MCNT_INC( RESOLV, TXN_NO_BANK, 1 );
     350           0 :       return 0;
     351           0 :     }
     352           0 :     int err = peek_aluts( ctx, txnm );
     353           0 :     if( FD_UNLIKELY( err ) ) return 0;
     354           0 :   }
     355             : 
     356           0 :   ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
     357           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     358           0 :   fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, 0UL, tspub );
     359           0 :   ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
     360             : 
     361           0 :   return 1;
     362           0 : }
     363             : 
     364             : static inline void
     365             : after_credit( fd_resolv_ctx_t *   ctx,
     366             :               fd_stem_context_t * stem,
     367             :               int *               opt_poll_in,
     368           0 :               int *               charge_busy ) {
     369           0 :   if( FD_UNLIKELY( !fd_startup_gate_idle( ctx->startup_gate ) ) ) return;
     370             : 
     371           0 :   if( FD_LIKELY( ctx->flush_pool_idx==ULONG_MAX ) ) return;
     372             : 
     373           0 :   *charge_busy = 1;
     374           0 :   *opt_poll_in = 0;
     375             : 
     376           0 :   ulong next = map_chain_idx_next_const( ctx->flush_pool_idx, ULONG_MAX, ctx->pool );
     377           0 :   map_chain_idx_remove_fast( ctx->map_chain, ctx->flush_pool_idx, ctx->pool );
     378           0 :   if( FD_LIKELY( publish_txn( ctx, stem, pool_ele( ctx->pool, ctx->flush_pool_idx ) ) ) ) {
     379           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_PUBLISHED_IDX ]++;
     380           0 :   } else {
     381           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_REMOVED_IDX ]++;
     382           0 :   }
     383           0 :   lru_list_idx_remove( ctx->lru_list, ctx->flush_pool_idx, ctx->pool );
     384           0 :   pool_idx_release( ctx->pool, ctx->flush_pool_idx );
     385           0 :   ctx->flush_pool_idx = next;
     386           0 : }
     387             : 
     388             : /* Returns 0 if not a durable nonce transaction and 1 if it may be a
     389             :    durable nonce transaction */
     390             : 
     391             : FD_FN_PURE static inline int
     392             : fd_resolv_is_durable_nonce( fd_txn_t const * txn,
     393           0 :                             uchar    const * payload ) {
     394           0 :   if( FD_UNLIKELY( txn->instr_cnt==0 ) ) return 0;
     395             : 
     396           0 :   fd_txn_instr_t const * ix0 = &txn->instr[ 0 ];
     397           0 :   fd_acct_addr_t const * prog0 = fd_txn_get_acct_addrs( txn, payload ) + ix0->program_id;
     398             :   /* First instruction must be SystemProgram nonceAdvance instruction */
     399           0 :   fd_acct_addr_t const system_program[1] = { { { SYS_PROG_ID } } };
     400           0 :   if( FD_LIKELY( memcmp( prog0, system_program, sizeof(fd_acct_addr_t) ) ) )        return 0;
     401             : 
     402             :   /* instruction with three accounts and a four byte instruction data, a
     403             :      little-endian uint value 4 */
     404           0 :   if( FD_UNLIKELY( (ix0->data_sz!=4) | (ix0->acct_cnt!=3) ) ) return 0;
     405             : 
     406           0 :   return fd_uint_load_4( payload + ix0->data_off )==4U;
     407           0 : }
     408             : 
     409             : static inline void
     410             : after_frag( fd_resolv_ctx_t *   ctx,
     411             :             ulong               in_idx,
     412             :             ulong               seq,
     413             :             ulong               sig,
     414             :             ulong               sz,
     415             :             ulong               tsorig,
     416             :             ulong               _tspub,
     417           0 :             fd_stem_context_t * stem ) {
     418           0 :   (void)seq;
     419           0 :   (void)sz;
     420           0 :   (void)_tspub;
     421             : 
     422           0 :   if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) ) {
     423           0 :     switch( sig ) {
     424           0 :       case REPLAY_SIG_SLOT_COMPLETED: {
     425           0 :         fd_replay_slot_completed_t const * msg = &ctx->_completed_slot_msg;
     426             : 
     427             :         /* Equivocating slot with same blockhash, ignore.  See fd_txncache.h on how this is possible.
     428             :            TODO make sure matches how agave handles it */
     429           0 :         if( FD_UNLIKELY( map_query( ctx->blockhash_map, *(blockhash_t *)msg->block_hash.uc, NULL ) ) ) {
     430           0 :           FD_LOG_WARNING(( "slot with same blockhash, ignoring: %lu", msg->slot ));
     431           0 :           return;
     432           0 :         }
     433             : 
     434             :         /* blockhash_ring is initialized to all zeros. blockhash=0 is an illegal map query */
     435           0 :         if( FD_UNLIKELY( memcmp( &ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], (uchar[ 32UL ]){ 0UL }, sizeof(blockhash_t) ) ) ) {
     436           0 :           blockhash_map_t * entry = map_query( ctx->blockhash_map, ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], NULL );
     437           0 :           if( FD_LIKELY( entry ) ) map_remove( ctx->blockhash_map, entry );
     438           0 :         }
     439             : 
     440           0 :         memcpy( ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ].b, msg->block_hash.uc, 32UL );
     441           0 :         ctx->blockhash_ring_idx++;
     442             : 
     443           0 :         blockhash_map_t * blockhash = map_insert( ctx->blockhash_map, *(blockhash_t *)msg->block_hash.uc );
     444           0 :         blockhash->slot = msg->slot;
     445             : 
     446           0 :         blockhash_t * hash = (blockhash_t *)msg->block_hash.uc;
     447           0 :         ctx->flush_pool_idx  = map_chain_idx_query_const( ctx->map_chain, &hash, ULONG_MAX, ctx->pool );
     448           0 :         ctx->flushing_slot   = msg->slot;
     449             : 
     450           0 :         ctx->completed_slot = msg->slot;
     451           0 :         break;
     452           0 :       }
     453           0 :       case REPLAY_SIG_ROOT_ADVANCED: {
     454           0 :         fd_replay_root_advanced_t const * msg = &ctx->_rooted_slot_msg;
     455             : 
     456             :         /* Replace current bank with new bank */
     457           0 :         fd_bank_t * prev_bank = ctx->bank;
     458             : 
     459           0 :         ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
     460           0 :         FD_TEST( ctx->bank );
     461             : 
     462             :         /* Send slot completed message back to replay, so it can
     463             :            decrement the reference count of the previous bank. */
     464           0 :         if( FD_LIKELY( prev_bank ) ) {
     465           0 :           ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     466           0 :           fd_resolv_slot_exchanged_t * slot_exchanged =
     467           0 :             fd_type_pun( fd_chunk_to_laddr( ctx->out_replay->mem, ctx->out_replay->chunk ) );
     468           0 :           slot_exchanged->bank_idx = prev_bank->idx;
     469           0 :           fd_stem_publish( stem, 1UL, 0UL, ctx->out_replay->chunk, sizeof(fd_resolv_slot_exchanged_t), 0UL, tsorig, tspub );
     470           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 );
     471           0 :         }
     472             : 
     473           0 :         break;
     474           0 :       }
     475           0 :       default: break;
     476           0 :     }
     477           0 :     return;
     478           0 :   }
     479             : 
     480           0 :   fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
     481           0 :   FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
     482           0 :   FD_TEST( txnm->txn_t_sz<=FD_TXN_MAX_SZ );
     483           0 :   fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
     484             : 
     485             :   /* If we find the recent blockhash, life is simple.  We drop
     486             :      transactions that couldn't possibly execute any more, and forward
     487             :      to pack ones that could.
     488             : 
     489             :      If we can't find the recent blockhash ... it means one of four
     490             :      things,
     491             : 
     492             :      (1) The blockhash is really old (more than 19 days) or just
     493             :          non-existent.
     494             :      (2) The blockhash is not that old, but was created before this
     495             :          validator was started.
     496             :      (3) It's really new (we haven't seen the bank yet).
     497             :      (4) It's a durable nonce transaction, or part of a bundle (just let
     498             :          it pass).
     499             : 
     500             :     For durable nonce transactions, there isn't much we can do except
     501             :     pass them along and see if they execute.
     502             : 
     503             :     For the other three cases ... we don't want to flood pack with what
     504             :     might be junk transactions, so we accumulate them into a local
     505             :     buffer.  If we later see the blockhash come to exist, we forward any
     506             :     buffered transactions to back. */
     507             : 
     508           0 :   if( FD_UNLIKELY( txnm->block_engine.bundle_id && (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
     509           0 :     ctx->bundle_failed = 0;
     510           0 :     ctx->bundle_id     = txnm->block_engine.bundle_id;
     511           0 :   }
     512             : 
     513           0 :   if( FD_UNLIKELY( txnm->block_engine.bundle_id && ctx->bundle_failed ) ) {
     514           0 :     ctx->metrics.bundle_peer_failure++;
     515           0 :     return;
     516           0 :   }
     517             : 
     518           0 :   txnm->reference_slot = ctx->completed_slot;
     519             : 
     520           0 :   blockhash_t const * recent_blockhash = (blockhash_t const *)( fd_txn_m_payload( txnm )+txnt->recent_blockhash_off );
     521           0 :   blockhash_map_t const * blockhash = NULL;
     522           0 :   if( FD_LIKELY( !map_key_inval( *recent_blockhash ) ) ) {
     523           0 :     blockhash = map_query( ctx->blockhash_map, *recent_blockhash, NULL );
     524           0 :   }
     525           0 :   if( FD_LIKELY( blockhash ) ) {
     526           0 :     txnm->reference_slot = blockhash->slot;
     527           0 :     if( FD_UNLIKELY( txnm->reference_slot+151UL<ctx->completed_slot ) ) {
     528           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     529           0 :       ctx->metrics.blockhash_expired++;
     530           0 :       return;
     531           0 :     }
     532           0 :   }
     533             : 
     534           0 :   int is_bundle_member = !!txnm->block_engine.bundle_id;
     535           0 :   int is_durable_nonce = fd_resolv_is_durable_nonce( txnt, fd_txn_m_payload( txnm ) );
     536             : 
     537           0 :   if( FD_UNLIKELY( !is_bundle_member && !is_durable_nonce && !blockhash ) ) {
     538           0 :     ulong pool_idx;
     539           0 :     if( FD_UNLIKELY( !pool_free( ctx->pool ) ) ) {
     540           0 :       pool_idx = lru_list_idx_pop_tail( ctx->lru_list, ctx->pool );
     541           0 :       map_chain_idx_remove_fast( ctx->map_chain, pool_idx, ctx->pool );
     542           0 :       ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_OVERRUN_IDX ]++;
     543           0 :     } else {
     544           0 :       pool_idx = pool_idx_acquire( ctx->pool );
     545           0 :     }
     546             : 
     547           0 :     fd_stashed_txn_m_t * stash_txn = pool_ele( ctx->pool, pool_idx );
     548             :     /* There's a compiler bug in GCC version 12 (at least 12.1, 12.3 and
     549             :        12.4) that cause it to think stash_txn is a null pointer.  It
     550             :        then complains that the memcpy is bad and refuses to compile the
     551             :        memcpy below.  It is possible for pool_ele to return NULL, but
     552             :        that can't happen because if pool_free is 0, then all the pool
     553             :        elements must be in the LRU list, so idx_pop_tail won't return
     554             :        IDX_NULL; and if pool_free returns non-zero, then
     555             :        pool_idx_acquire won't return POOL_IDX_NULL. */
     556           0 :     FD_COMPILER_FORGET( stash_txn );
     557           0 :     fd_memcpy( stash_txn->_, txnm, fd_txn_m_realized_footprint( txnm, 1, 0 ) );
     558           0 :     stash_txn->blockhash = (blockhash_t *)(fd_txn_m_payload( (fd_txn_m_t *)(stash_txn->_) ) + txnt->recent_blockhash_off);
     559           0 :     ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_INSERTED_IDX ]++;
     560             : 
     561           0 :     map_chain_ele_insert( ctx->map_chain, stash_txn, ctx->pool );
     562           0 :     lru_list_idx_push_head( ctx->lru_list, pool_idx, ctx->pool );
     563             : 
     564           0 :     return;
     565           0 :   }
     566             : 
     567           0 :   if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
     568           0 :     if( FD_UNLIKELY( !ctx->bank ) ) {
     569           0 :       FD_MCNT_INC( RESOLV, TXN_NO_BANK, 1 );
     570           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     571           0 :       return;
     572           0 :     }
     573             : 
     574           0 :     int result = peek_aluts( ctx, txnm );
     575           0 :     if( FD_UNLIKELY( result ) ) {
     576           0 :       if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
     577           0 :       return;
     578           0 :     }
     579           0 :   }
     580             : 
     581           0 :   ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
     582           0 :   ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
     583           0 :   fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, tsorig, tspub );
     584           0 :   ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
     585           0 : }
     586             : 
     587             : static void
     588             : privileged_init( fd_topo_t const *      topo,
     589           0 :                  fd_topo_tile_t const * tile ) {
     590           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     591             : 
     592           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     593           0 :   fd_resolv_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t ) );
     594           0 :   FD_TEST( fd_rng_secure( &ctx->map_seed, sizeof(ctx->map_seed) ) );
     595           0 : }
     596             : 
     597             : static void
     598             : unprivileged_init( fd_topo_t const *      topo,
     599           0 :                    fd_topo_tile_t const * tile ) {
     600           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     601             : 
     602           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     603           0 :   fd_resolv_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t ) );
     604             : 
     605           0 :   ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
     606           0 :   ctx->round_robin_idx = tile->kind_id;
     607             : 
     608           0 :   ctx->bundle_failed = 0;
     609           0 :   ctx->bundle_id     = 0UL;
     610             : 
     611           0 :   ctx->completed_slot = 0UL;
     612           0 :   ctx->blockhash_ring_idx = 0UL;
     613             : 
     614           0 :   ctx->flush_pool_idx = ULONG_MAX;
     615             : 
     616           0 :   ctx->pool = pool_join( pool_new( FD_SCRATCH_ALLOC_APPEND( l, pool_align(), pool_footprint( 1UL<<16UL ) ), 1UL<<16UL ) );
     617           0 :   FD_TEST( ctx->pool );
     618             : 
     619           0 :   ctx->map_chain = map_chain_join( map_chain_new( FD_SCRATCH_ALLOC_APPEND( l, map_chain_align(), map_chain_footprint( 8192ULL ) ), 8192UL, ctx->map_seed ) );
     620           0 :   FD_TEST( ctx->map_chain );
     621             : 
     622           0 :   FD_TEST( ctx->lru_list==lru_list_join( lru_list_new( ctx->lru_list ) ) );
     623             : 
     624           0 :   memset( ctx->blockhash_ring, 0, sizeof( ctx->blockhash_ring ) );
     625           0 :   memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
     626             : 
     627           0 :   ctx->blockhash_map = map_join( map_new( FD_SCRATCH_ALLOC_APPEND( l, map_align(), map_footprint( MAP_LG_SLOT_CNT ) ), MAP_LG_SLOT_CNT, ctx->map_seed ) );
     628           0 :   FD_TEST( ctx->blockhash_map );
     629             : 
     630           0 :   FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
     631           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     632           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     633           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     634             : 
     635           0 :     if( FD_LIKELY(      !strcmp( link->name, "replay_out"   ) ) ) ctx->in[ i ].kind = IN_KIND_REPLAY;
     636           0 :     else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in[ i ].kind = IN_KIND_DEDUP;
     637           0 :     else FD_LOG_ERR(( "unknown in link name '%s'", link->name ));
     638             : 
     639           0 :     ctx->in[i].mem    = link_wksp->wksp;
     640           0 :     ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
     641           0 :     ctx->in[i].wmark  = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
     642           0 :     ctx->in[i].mtu    = link->mtu;
     643           0 :   }
     644             : 
     645           0 :   ctx->out_pack->mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
     646           0 :   ctx->out_pack->chunk0 = fd_dcache_compact_chunk0( ctx->out_pack->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
     647           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 );
     648           0 :   ctx->out_pack->chunk  = ctx->out_pack->chunk0;
     649             : 
     650           0 :   ctx->out_replay->mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
     651           0 :   ctx->out_replay->chunk0 = fd_dcache_compact_chunk0( ctx->out_replay->mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
     652           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 );
     653           0 :   ctx->out_replay->chunk  = ctx->out_replay->chunk0;
     654             : 
     655           0 :   ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
     656           0 :   FD_TEST( banks_obj_id!=ULONG_MAX );
     657           0 :   ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
     658           0 :   FD_TEST( ctx->banks );
     659           0 :   ctx->bank = NULL;
     660             : 
     661             :   /* Read-only join to accdb.  The accdb workspace is mapped PROT_READ
     662             :      in this tile (see topology); the only writable external mapping
     663             :      is our private epoch fseq.  FD_ACCDB_FD_RO is the O_RDONLY dup
     664             :      of the accdb data file. */
     665           0 :   void * _accdb_join = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->resolv.max_live_slots ) );
     666           0 :   void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->resolv.accdb_obj_id );
     667           0 :   fd_accdb_shmem_t * accdb_shmem_ro = fd_accdb_shmem_join( _accdb_shmem );
     668           0 :   FD_TEST( accdb_shmem_ro );
     669           0 :   ulong * epoch_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->resolv.accdb_epoch_fseq_obj_id ) );
     670           0 :   FD_TEST( epoch_fseq );
     671           0 :   ctx->accdb = fd_accdb_join_readonly( _accdb_join, accdb_shmem_ro, epoch_fseq, FD_ACCDB_FD_RO );
     672           0 :   FD_TEST( ctx->accdb );
     673             : 
     674           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     675           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     676           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     677             : 
     678           0 :   fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
     679           0 : }
     680             : 
     681             : static ulong
     682             : populate_allowed_seccomp( fd_topo_t const *      topo,
     683             :                           fd_topo_tile_t const * tile,
     684             :                           ulong                  out_cnt,
     685           0 :                           struct sock_filter *   out ) {
     686           0 :   (void)topo;
     687           0 :   (void)tile;
     688             : 
     689           0 :   populate_sock_filter_policy_fd_resolv_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RO );
     690           0 :   return sock_filter_policy_fd_resolv_tile_instr_cnt;
     691           0 : }
     692             : 
     693             : static ulong
     694             : populate_allowed_fds( fd_topo_t const *      topo,
     695             :                       fd_topo_tile_t const * tile,
     696             :                       ulong                  out_fds_cnt,
     697           0 :                       int *                  out_fds ) {
     698           0 :   (void)topo;
     699           0 :   (void)tile;
     700             : 
     701           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     702             : 
     703           0 :   ulong out_cnt = 0UL;
     704           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     705           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     706           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     707           0 :   out_fds[ out_cnt++ ] = FD_ACCDB_FD_RO; /* accounts db readonly fd */
     708           0 :   return out_cnt;
     709           0 : }
     710             : 
     711           0 : #define STEM_BURST (1UL)
     712             : 
     713             : /* The default STEM_LAZY is derived from cr_max, which is the minimum
     714             :    depth among all reliably-consumed output links.  The resolv_replay
     715             :    link (depth 4096) dominates this, even though it only carries ~2-3
     716             :    msgs/s.  This makes housekeeping fire ~16x more often than necessary.
     717             :    We override with roughly what the default would be without accounting
     718             :    for it. */
     719           0 : #define STEM_LAZY (128000L) /* 128 us */
     720             : 
     721           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_resolv_ctx_t
     722           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_resolv_ctx_t)
     723             : 
     724           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     725           0 : #define STEM_CALLBACK_AFTER_CREDIT  after_credit
     726           0 : #define STEM_CALLBACK_BEFORE_FRAG   before_frag
     727           0 : #define STEM_CALLBACK_DURING_FRAG   during_frag
     728           0 : #define STEM_CALLBACK_AFTER_FRAG    after_frag
     729             : 
     730             : #include "../../disco/stem/fd_stem.c"
     731             : 
     732             : fd_topo_run_tile_t fd_tile_resolv = {
     733             :   .name                     = "resolv",
     734             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     735             :   .populate_allowed_fds     = populate_allowed_fds,
     736             :   .scratch_align            = scratch_align,
     737             :   .scratch_footprint        = scratch_footprint,
     738             :   .privileged_init          = privileged_init,
     739             :   .unprivileged_init        = unprivileged_init,
     740             :   .run                      = stem_run,
     741             : };

Generated by: LCOV version 1.14