LCOV - code coverage report
Current view: top level - disco/verify - fd_verify_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 85 164 51.8 %
Date: 2026-08-19 04:32:02 Functions: 7 20 35.0 %

          Line data    Source code
       1             : #include "fd_verify_tile.h"
       2             : #include "../fd_txn_m.h"
       3             : #include "../metrics/fd_metrics.h"
       4             : #include "generated/fd_verify_tile_seccomp.h"
       5             : #include "../../flamenco/gossip/fd_gossip_message.h"
       6             : 
       7           3 : #define IN_KIND_QUIC   (0UL)
       8          87 : #define IN_KIND_BUNDLE (1UL)
       9           3 : #define IN_KIND_GOSSIP (2UL)
      10           3 : #define IN_KIND_TXSEND (3UL)
      11             : 
      12             : FD_FN_CONST static inline ulong
      13          21 : scratch_align( void ) {
      14          21 :   return FD_TCACHE_ALIGN;
      15          21 : }
      16             : 
      17             : FD_FN_PURE static inline ulong
      18           6 : scratch_footprint( fd_topo_tile_t const * tile ) {
      19           6 :   ulong l = FD_LAYOUT_INIT;
      20           6 :   l = FD_LAYOUT_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
      21           6 :   l = FD_LAYOUT_APPEND( l, fd_tcache_align(), fd_tcache_footprint( tile->verify.tcache_depth, 0UL ) );
      22          78 :   for( ulong i=0; i<FD_TXN_SIG_MAX; i++ ) {
      23          72 :     l = FD_LAYOUT_APPEND( l, fd_sha512_align(), fd_sha512_footprint() );
      24          72 :   }
      25           6 :   return FD_LAYOUT_FINI( l, scratch_align() );
      26           6 : }
      27             : 
      28             : static inline void
      29           0 : metrics_write( fd_verify_ctx_t * ctx ) {
      30           0 :   FD_MCNT_ENUM_COPY( VERIFY, TXN_RESULT, ctx->metrics.verify_tile_result );
      31           0 :   FD_MCNT_SET( VERIFY, VOTE_GOSSIP_RX,  ctx->metrics.gossiped_votes_cnt );
      32           0 : }
      33             : 
      34             : static int
      35             : before_frag( fd_verify_ctx_t * ctx,
      36             :              ulong             in_idx,
      37             :              ulong             seq,
      38          42 :              ulong             sig ) {
      39             :   /* Bundle tile can produce both "bundles" and "packets", a packet is a
      40             :      regular transaction and should be round-robined between verify
      41             :      tiles, while bundles need to go through verify:0 currently to
      42             :      prevent interleaving of bundle streams. */
      43          42 :   int is_bundle_packet = (ctx->in_kind[ in_idx ]==IN_KIND_BUNDLE && !sig);
      44             : 
      45          42 :   if( FD_LIKELY( is_bundle_packet || ctx->in_kind[ in_idx ]==IN_KIND_QUIC ) ) {
      46          30 :     return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx;
      47          30 :   } else if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_BUNDLE ) ) {
      48          12 :     return ctx->round_robin_idx!=0UL;
      49          12 :   } else if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
      50           0 :       return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx ||
      51           0 :              sig!=FD_GOSSIP_UPDATE_TAG_VOTE;
      52           0 :   }
      53             : 
      54           0 :   return 0;
      55          42 : }
      56             : 
      57             : /* during_frag is called between pairs for sequence number checks, as
      58             :    we are reading incoming frags.  We don't actually need to copy the
      59             :    fragment here, see fd_dedup.c for why we do this.*/
      60             : 
      61             : static inline void
      62             : during_frag( fd_verify_ctx_t * ctx,
      63             :              ulong             in_idx,
      64             :              ulong             seq FD_PARAM_UNUSED,
      65             :              ulong             sig FD_PARAM_UNUSED,
      66             :              ulong             chunk,
      67             :              ulong             sz,
      68           0 :              ulong             ctl FD_PARAM_UNUSED ) {
      69             : 
      70           0 :   ulong in_kind = ctx->in_kind[ in_idx ];
      71           0 :   if( FD_UNLIKELY( in_kind==IN_KIND_BUNDLE || in_kind==IN_KIND_QUIC || in_kind==IN_KIND_TXSEND ) ) {
      72           0 :     if( FD_UNLIKELY( chunk<ctx->in[in_idx].chunk0 || chunk>ctx->in[in_idx].wmark || sz>FD_TPU_RAW_MTU ) )
      73           0 :       FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu,%lu]", chunk, sz, ctx->in[in_idx].chunk0, ctx->in[in_idx].wmark, FD_TPU_RAW_MTU ));
      74             : 
      75           0 :     uchar * src = fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
      76           0 :     uchar * dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
      77           0 :     fd_memcpy( dst, src, sz );
      78           0 :   } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
      79           0 :     if( FD_UNLIKELY( chunk<ctx->in[in_idx].chunk0 || chunk>ctx->in[in_idx].wmark || sz>2048UL ) )
      80           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 ));
      81             : 
      82           0 :     fd_gossip_update_message_t const * msg = fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
      83           0 :     fd_txn_m_t * dst = fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
      84             : 
      85           0 :     dst->payload_sz = (ushort)msg->vote->value->transaction_len;
      86           0 :     dst->block_engine.bundle_id = 0UL;
      87           0 :     dst->first_seen_nanos = msg->vote->first_seen_nanos;
      88           0 :     dst->source_ipv4 = msg->vote->socket->is_ipv6 ? 0U : msg->vote->socket->ip4;
      89           0 :     dst->source_tpu = FD_TXN_M_TPU_SOURCE_GOSSIP;
      90           0 :     fd_memcpy( fd_txn_m_payload( dst ), msg->vote->value->transaction, msg->vote->value->transaction_len );
      91           0 :   }
      92           0 : }
      93             : 
      94             : static inline void
      95             : after_frag( fd_verify_ctx_t *   ctx,
      96             :             ulong               in_idx,
      97             :             ulong               seq,
      98             :             ulong               sig,
      99             :             ulong               sz,
     100             :             ulong               tsorig,
     101             :             ulong               _tspub,
     102           0 :             fd_stem_context_t * stem ) {
     103           0 :   (void)in_idx;
     104           0 :   (void)seq;
     105           0 :   (void)sig;
     106           0 :   (void)sz;
     107           0 :   (void)_tspub;
     108             : 
     109           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP || ctx->in_kind[ in_idx ]==IN_KIND_TXSEND ) ) ctx->metrics.gossiped_votes_cnt++;
     110             : 
     111           0 :   fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
     112           0 :   if( FD_UNLIKELY( txnm->payload_sz>FD_TPU_MTU ) ) {
     113           0 :     FD_LOG_ERR(( "verify: txn payload size %hu exceeds max %lu", txnm->payload_sz, FD_TPU_MTU ));
     114           0 :   }
     115           0 :   fd_txn_t *  txnt = fd_txn_m_txn_t( txnm );
     116           0 :   txnm->txn_t_sz = (ushort)fd_txn_parse( fd_txn_m_payload( txnm ), txnm->payload_sz, txnt, NULL );
     117             : 
     118           0 :   int is_bundle = !!txnm->block_engine.bundle_id;
     119             : 
     120           0 :   if( FD_UNLIKELY( is_bundle & (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
     121           0 :     ctx->bundle_failed = 0;
     122           0 :     ctx->bundle_id     = txnm->block_engine.bundle_id;
     123           0 :   }
     124             : 
     125           0 :   if( FD_UNLIKELY( is_bundle & (!!ctx->bundle_failed) ) ) {
     126           0 :     ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_TILE_RESULT_V_BUNDLE_PEER_FAILURE_IDX ]++;
     127           0 :     return;
     128           0 :   }
     129             : 
     130           0 :   if( FD_UNLIKELY( !txnm->txn_t_sz ) ) {
     131           0 :     if( FD_UNLIKELY( is_bundle ) ) ctx->bundle_failed = 1;
     132           0 :     ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_TILE_RESULT_V_PARSE_FAILURE_IDX ]++;
     133           0 :     return;
     134           0 :   }
     135             : 
     136             :   /* Users sometimes send transactions as part of a bundle (with a tip)
     137             :      and via the normal path (without a tip).  Regardless of which
     138             :      arrives first, we want to pack the one with the tip.  Thus, we
     139             :      exempt bundles from the normal HA dedup checks.  The dedup tile
     140             :      will still do a full-bundle dedup check to make sure to drop any
     141             :      identical bundles. */
     142           0 :   ulong _txn_sig;
     143           0 :   int res = fd_txn_verify( ctx, fd_txn_m_payload( txnm ), txnm->payload_sz, txnt, !is_bundle, &_txn_sig );
     144           0 :   if( FD_UNLIKELY( res!=FD_TXN_VERIFY_SUCCESS ) ) {
     145           0 :     if( FD_UNLIKELY( is_bundle ) ) ctx->bundle_failed = 1;
     146             : 
     147           0 :     if( FD_LIKELY( res==FD_TXN_VERIFY_DEDUP ) ) ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_TILE_RESULT_V_DEDUP_FAILURE_IDX ]++;
     148           0 :     else                                        ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_TILE_RESULT_V_VERIFY_FAILURE_IDX ]++;
     149             : 
     150           0 :     return;
     151           0 :   }
     152             : 
     153           0 :   ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 0 );
     154           0 :   ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
     155           0 :   fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, realized_sz, 0UL, tsorig, tspub );
     156           0 :   ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, realized_sz, ctx->out_chunk0, ctx->out_wmark );
     157             : 
     158           0 :   ctx->metrics.verify_tile_result[ FD_METRICS_ENUM_VERIFY_TILE_RESULT_V_SUCCESS_IDX ]++;
     159           0 : }
     160             : 
     161             : static void
     162             : privileged_init( fd_topo_t const *      topo,
     163           3 :                  fd_topo_tile_t const * tile ) {
     164           3 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     165             : 
     166           3 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     167           3 :   fd_verify_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
     168           3 :   FD_TEST( fd_rng_secure( &ctx->hashmap_seed, 8U ) );
     169           3 : }
     170             : 
     171             : static void
     172             : unprivileged_init( fd_topo_t const *      topo,
     173           3 :                    fd_topo_tile_t const * tile ) {
     174           3 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     175             : 
     176           3 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     177           3 :   fd_verify_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_verify_ctx_t ), sizeof( fd_verify_ctx_t ) );
     178           3 :   fd_tcache_t * tcache = fd_tcache_join( fd_tcache_new( FD_SCRATCH_ALLOC_APPEND( l, FD_TCACHE_ALIGN, FD_TCACHE_FOOTPRINT( tile->verify.tcache_depth, 0UL ) ), tile->verify.tcache_depth, 0UL ) );
     179           3 :   if( FD_UNLIKELY( !tcache ) ) FD_LOG_ERR(( "fd_tcache_join failed" ));
     180             : 
     181           3 :   ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
     182           3 :   ctx->round_robin_idx = tile->kind_id;
     183             : 
     184          39 :   for ( ulong i=0; i<FD_TXN_SIG_MAX; i++ ) {
     185          36 :     fd_sha512_t * sha = fd_sha512_join( fd_sha512_new( FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sha512_t ), sizeof( fd_sha512_t ) ) ) );
     186          36 :     if( FD_UNLIKELY( !sha ) ) FD_LOG_ERR(( "fd_sha512_join failed" ));
     187          36 :     ctx->sha[i] = sha;
     188          36 :   }
     189             : 
     190           3 :   ctx->bundle_failed = 0;
     191           3 :   ctx->bundle_id     = 0UL;
     192             : 
     193           3 :   memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
     194             : 
     195           3 :   ctx->tcache_depth   = fd_tcache_depth       ( tcache );
     196           3 :   ctx->tcache_map_cnt = fd_tcache_map_cnt     ( tcache );
     197           3 :   ctx->tcache_sync    = fd_tcache_oldest_laddr( tcache );
     198           3 :   ctx->tcache_ring    = fd_tcache_ring_laddr  ( tcache );
     199           3 :   ctx->tcache_map     = fd_tcache_map_laddr   ( tcache );
     200             : 
     201          15 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     202          12 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     203             : 
     204          12 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     205          12 :     ctx->in[i].mem = link_wksp->wksp;
     206          12 :     ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
     207          12 :     ctx->in[i].wmark  = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
     208             : 
     209          12 :     if(      !strcmp( link->name, "quic_verify"  ) ) ctx->in_kind[ i ] = IN_KIND_QUIC;
     210           9 :     else if( !strcmp( link->name, "bundle_verif" ) ) ctx->in_kind[ i ] = IN_KIND_BUNDLE;
     211           6 :     else if( !strcmp( link->name, "txsend_out"   ) ) ctx->in_kind[ i ] = IN_KIND_TXSEND;
     212           3 :     else if( !strcmp( link->name, "gossip_out"   ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP;
     213           0 :     else FD_LOG_ERR(( "unexpected link name %s", link->name ));
     214          12 :   }
     215             : 
     216           3 :   ctx->out_mem    = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
     217           3 :   ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
     218           3 :   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 );
     219           3 :   ctx->out_chunk  = ctx->out_chunk0;
     220             : 
     221           3 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     222           3 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     223           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     224           3 : }
     225             : 
     226             : static ulong
     227             : populate_allowed_seccomp( fd_topo_t const *      topo,
     228             :                           fd_topo_tile_t const * tile,
     229             :                           ulong                  out_cnt,
     230           3 :                           struct sock_filter *   out ) {
     231           3 :   (void)topo;
     232           3 :   (void)tile;
     233             : 
     234           3 :   populate_sock_filter_policy_fd_verify_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     235           3 :   return sock_filter_policy_fd_verify_tile_instr_cnt;
     236           3 : }
     237             : 
     238             : static ulong
     239             : populate_allowed_fds( fd_topo_t const *      topo,
     240             :                       fd_topo_tile_t const * tile,
     241             :                       ulong                  out_fds_cnt,
     242           3 :                       int *                  out_fds ) {
     243           3 :   (void)topo;
     244           3 :   (void)tile;
     245             : 
     246           3 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     247             : 
     248           3 :   ulong out_cnt = 0UL;
     249           3 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     250           3 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     251           3 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     252           3 :   return out_cnt;
     253           3 : }
     254             : 
     255           0 : #define STEM_BURST (1UL)
     256             : 
     257           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_verify_ctx_t
     258           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_verify_ctx_t)
     259             : 
     260           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     261           0 : #define STEM_CALLBACK_BEFORE_FRAG   before_frag
     262           0 : #define STEM_CALLBACK_DURING_FRAG   during_frag
     263           0 : #define STEM_CALLBACK_AFTER_FRAG    after_frag
     264             : 
     265             : #include "../stem/fd_stem.c"
     266             : 
     267             : #ifndef FD_TILE_TEST
     268             : fd_topo_run_tile_t fd_tile_verify = {
     269             :   .name                     = "verify",
     270             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     271             :   .populate_allowed_fds     = populate_allowed_fds,
     272             :   .scratch_align            = scratch_align,
     273             :   .scratch_footprint        = scratch_footprint,
     274             :   .privileged_init          = privileged_init,
     275             :   .unprivileged_init        = unprivileged_init,
     276             :   .run                      = stem_run,
     277             : };
     278             : #endif

Generated by: LCOV version 1.14