LCOV - code coverage report
Current view: top level - disco/verify - fd_verify_tile.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 32 34 94.1 %
Date: 2025-08-05 05:04:49 Functions: 1 3 33.3 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_disco_verify_fd_verify_tile_h
       2             : #define HEADER_fd_src_disco_verify_fd_verify_tile_h
       3             : 
       4             : /* The verify tile verifies that the cryptographic signatures of
       5             :    incoming transactions match the data being signed.  Transactions with
       6             :    invalid signatures are filtered out of the frag stream. */
       7             : 
       8             : #include "../topo/fd_topo.h"
       9             : 
      10          21 : #define FD_TXN_VERIFY_SUCCESS  0
      11          21 : #define FD_TXN_VERIFY_FAILED  -1
      12          15 : #define FD_TXN_VERIFY_DEDUP   -2
      13             : 
      14             : extern fd_topo_run_tile_t fd_tile_verify;
      15             : 
      16             : /* fd_verify_in_ctx_t is a context object for each in (producer) mcache
      17             :    connected to the verify tile. */
      18             : 
      19             : typedef struct {
      20             :   fd_wksp_t * mem;
      21             :   ulong       chunk0;
      22             :   ulong       wmark;
      23             : } fd_verify_in_ctx_t;
      24             : 
      25             : typedef struct {
      26             :   /* TODO switch to fd_sha512_batch_t? */
      27             :   fd_sha512_t * sha[ FD_TXN_ACTUAL_SIG_MAX ];
      28             : 
      29             :   int   bundle_failed;
      30             :   ulong bundle_id;
      31             : 
      32             :   ulong round_robin_idx;
      33             :   ulong round_robin_cnt;
      34             : 
      35             :   ulong   tcache_depth;
      36             :   ulong   tcache_map_cnt;
      37             :   ulong * tcache_sync;
      38             :   ulong * tcache_ring;
      39             :   ulong * tcache_map;
      40             : 
      41             :   ulong              in_kind[ 32 ];
      42             :   fd_verify_in_ctx_t in[ 32 ];
      43             : 
      44             :   fd_wksp_t * out_mem;
      45             :   ulong       out_chunk0;
      46             :   ulong       out_wmark;
      47             :   ulong       out_chunk;
      48             : 
      49             :   ulong       hashmap_seed;
      50             : 
      51             :   struct {
      52             :     ulong parse_fail_cnt;
      53             :     ulong verify_fail_cnt;
      54             :     ulong dedup_fail_cnt;
      55             :     ulong bundle_peer_fail_cnt;
      56             :   } metrics;
      57             : } fd_verify_ctx_t;
      58             : 
      59             : static inline int
      60             : fd_txn_verify( fd_verify_ctx_t * ctx,
      61             :                uchar const *     udp_payload,
      62             :                ushort const      payload_sz,
      63             :                fd_txn_t const *  txn,
      64             :                int               dedup,
      65          57 :                ulong *           opt_sig ) {
      66             : 
      67             :   /* We do not want to deref any non-data field from the txn struct more than once */
      68          57 :   uchar  signature_cnt = txn->signature_cnt;
      69          57 :   ushort signature_off = txn->signature_off;
      70          57 :   ushort acct_addr_off = txn->acct_addr_off;
      71          57 :   ushort message_off   = txn->message_off;
      72             : 
      73          57 :   uchar const * signatures = udp_payload + signature_off;
      74          57 :   uchar const * pubkeys = udp_payload + acct_addr_off;
      75          57 :   uchar const * msg = udp_payload + message_off;
      76          57 :   ulong msg_sz = (ulong)payload_sz - message_off;
      77             : 
      78             :   /* The first signature is the transaction id, i.e. a unique identifier.
      79             :      So use this to do a quick dedup of ha traffic. */
      80             : 
      81          57 :   ulong ha_dedup_tag = fd_hash( ctx->hashmap_seed, signatures, 64UL );
      82          57 :   int ha_dup = 0;
      83          57 :   if( FD_LIKELY( dedup ) ) {
      84          48 :     FD_FN_UNUSED ulong tcache_map_idx = 0; /* ignored */
      85          48 :     FD_TCACHE_QUERY( ha_dup, tcache_map_idx, ctx->tcache_map, ctx->tcache_map_cnt, ha_dedup_tag );
      86          48 :     if( FD_UNLIKELY( ha_dup ) ) {
      87          15 :       return FD_TXN_VERIFY_DEDUP;
      88          15 :     }
      89          48 :   }
      90             : 
      91             :   /* Verify signatures */
      92          42 :   int res = fd_ed25519_verify_batch_single_msg( msg, msg_sz, signatures, pubkeys, ctx->sha, signature_cnt );
      93          42 :   if( FD_UNLIKELY( res != FD_ED25519_SUCCESS ) ) {
      94          21 :     return FD_TXN_VERIFY_FAILED;
      95          21 :   }
      96             : 
      97             :   /* Insert into the tcache to dedup ha traffic.
      98             :      The dedup check is repeated to guard against duped txs verifying signatures at the same time */
      99          21 :   if( FD_LIKELY( dedup ) ) {
     100          15 :     FD_TCACHE_INSERT( ha_dup, *ctx->tcache_sync, ctx->tcache_ring, ctx->tcache_depth, ctx->tcache_map, ctx->tcache_map_cnt, ha_dedup_tag );
     101          15 :     if( FD_UNLIKELY( ha_dup ) ) {
     102           0 :       return FD_TXN_VERIFY_DEDUP;
     103           0 :     }
     104          15 :   }
     105             : 
     106          21 :   *opt_sig = ha_dedup_tag;
     107          21 :   return FD_TXN_VERIFY_SUCCESS;
     108          21 : }
     109             : 
     110             : #endif /* HEADER_fd_src_disco_verify_fd_verify_tile_h */

Generated by: LCOV version 1.14