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 : #include "../tiles.h" 5 : 6 21 : #define FD_TXN_VERIFY_SUCCESS 0 7 21 : #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 : int bundle_failed; 24 : ulong bundle_id; 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 : ulong in_kind[ 32 ]; 36 : fd_verify_in_ctx_t in[ 32 ]; 37 : 38 : fd_wksp_t * out_mem; 39 : ulong out_chunk0; 40 : ulong out_wmark; 41 : ulong out_chunk; 42 : 43 : ulong hashmap_seed; 44 : 45 : struct { 46 : ulong parse_fail_cnt; 47 : ulong verify_fail_cnt; 48 : ulong dedup_fail_cnt; 49 : ulong bundle_peer_fail_cnt; 50 : } metrics; 51 : } fd_verify_ctx_t; 52 : 53 : static inline int 54 : fd_txn_verify( fd_verify_ctx_t * ctx, 55 : uchar const * udp_payload, 56 : ushort const payload_sz, 57 : fd_txn_t const * txn, 58 : int dedup, 59 57 : ulong * opt_sig ) { 60 : 61 : /* We do not want to deref any non-data field from the txn struct more than once */ 62 57 : uchar signature_cnt = txn->signature_cnt; 63 57 : ushort signature_off = txn->signature_off; 64 57 : ushort acct_addr_off = txn->acct_addr_off; 65 57 : ushort message_off = txn->message_off; 66 : 67 57 : uchar const * signatures = udp_payload + signature_off; 68 57 : uchar const * pubkeys = udp_payload + acct_addr_off; 69 57 : uchar const * msg = udp_payload + message_off; 70 57 : ulong msg_sz = (ulong)payload_sz - message_off; 71 : 72 : /* The first signature is the transaction id, i.e. a unique identifier. 73 : So use this to do a quick dedup of ha traffic. */ 74 : 75 57 : ulong ha_dedup_tag = fd_hash( ctx->hashmap_seed, signatures, 64UL ); 76 57 : int ha_dup = 0; 77 57 : if( FD_LIKELY( dedup ) ) { 78 48 : FD_FN_UNUSED ulong tcache_map_idx = 0; /* ignored */ 79 48 : FD_TCACHE_QUERY( ha_dup, tcache_map_idx, ctx->tcache_map, ctx->tcache_map_cnt, ha_dedup_tag ); 80 48 : if( FD_UNLIKELY( ha_dup ) ) { 81 15 : return FD_TXN_VERIFY_DEDUP; 82 15 : } 83 48 : } 84 : 85 : /* Verify signatures */ 86 42 : int res = fd_ed25519_verify_batch_single_msg( msg, msg_sz, signatures, pubkeys, ctx->sha, signature_cnt ); 87 42 : if( FD_UNLIKELY( res != FD_ED25519_SUCCESS ) ) { 88 21 : return FD_TXN_VERIFY_FAILED; 89 21 : } 90 : 91 : /* Insert into the tcache to dedup ha traffic. 92 : The dedup check is repeated to guard against duped txs verifying signatures at the same time */ 93 21 : if( FD_LIKELY( dedup ) ) { 94 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 ); 95 15 : if( FD_UNLIKELY( ha_dup ) ) { 96 0 : return FD_TXN_VERIFY_DEDUP; 97 0 : } 98 15 : } 99 : 100 21 : *opt_sig = ha_dedup_tag; 101 21 : return FD_TXN_VERIFY_SUCCESS; 102 21 : } 103 : 104 : #endif /* HEADER_fd_src_disco_verify_fd_verify_tile_h */