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