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