LCOV - code coverage report
Current view: top level - app/fddev - txn.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 145 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 8 0.0 %

          Line data    Source code
       1             : #include "fddev.h"
       2             : 
       3             : #include "../fdctl/configure/configure.h"
       4             : #include "../../ballet/base64/fd_base64.h"
       5             : #include "../../waltz/quic/fd_quic.h"
       6             : #include "../../waltz/quic/tests/fd_quic_test_helpers.h"
       7             : #include "../../waltz/tls/test_tls_helper.h"
       8             : #include "../../util/net/fd_ip4.h"
       9             : 
      10             : #include <linux/capability.h>
      11             : #include <sys/random.h>
      12             : 
      13             : FD_IMPORT_BINARY(sample_transaction, "src/waltz/quic/tests/quic_txn.bin");
      14             : 
      15             : static int g_conn_hs_complete = 0;
      16             : static int g_conn_final = 0;
      17             : static ulong g_stream_notify = 0UL;
      18             : 
      19             : #define MAX_TXN_COUNT 128
      20             : 
      21             : void
      22             : txn_cmd_perm( args_t *         args,
      23             :               fd_caps_ctx_t *  caps,
      24           0 :               config_t * const config ) {
      25           0 :   (void)args;
      26             : 
      27           0 :   if( FD_UNLIKELY( config->development.netns.enabled ) )
      28           0 :     fd_caps_check_capability( caps, "txn", CAP_SYS_ADMIN, "enter a network namespace by calling `setns(2)`" );
      29           0 : }
      30             : 
      31             : void
      32             : txn_cmd_args( int *    pargc,
      33             :               char *** pargv,
      34           0 :               args_t * args ) {
      35           0 :   args->txn.payload_base64 = fd_env_strip_cmdline_cstr( pargc, pargv, "--payload-base64-encoded", NULL, NULL );
      36           0 :   args->txn.count = fd_env_strip_cmdline_ulong( pargc, pargv, "--count", NULL, 1 );
      37           0 :   if( FD_UNLIKELY( !args->txn.count || args->txn.count > MAX_TXN_COUNT ) )
      38           0 :     FD_LOG_ERR(( "count must be between 1 and %d", MAX_TXN_COUNT ));
      39             : 
      40           0 :   args->txn.dst_ip = fd_env_strip_cmdline_cstr( pargc, pargv, "--dst-ip", NULL, 0 );
      41           0 :   args->txn.dst_port = fd_env_strip_cmdline_ushort( pargc, pargv, "--dst-port", NULL, 0 );
      42           0 : }
      43             : 
      44             : static ulong
      45           0 : cb_now( void * context ) {
      46           0 :   (void)context;
      47           0 :   return (ulong)fd_log_wallclock();
      48           0 : }
      49             : 
      50             : static void
      51             : cb_conn_hs_complete( fd_quic_conn_t * conn,
      52           0 :                      void *           quic_ctx ) {
      53           0 :   (void)conn;
      54           0 :   (void)quic_ctx;
      55           0 :   g_conn_hs_complete = 1;
      56           0 : }
      57             : 
      58             : static void
      59             : cb_conn_final( fd_quic_conn_t * conn,
      60           0 :                void *           quic_ctx ) {
      61           0 :   (void)conn;
      62           0 :   (void)quic_ctx;
      63           0 :   g_conn_final = 1;
      64           0 : }
      65             : 
      66             : static void
      67             : cb_stream_notify( fd_quic_stream_t * stream,
      68             :                   void *             stream_ctx,
      69           0 :                   int                notify_type ) {
      70           0 :   (void)stream;
      71           0 :   (void)stream_ctx;
      72           0 :   (void)notify_type;
      73           0 :   g_stream_notify += 1;
      74           0 : }
      75             : 
      76             : static void
      77             : send_quic_transactions( fd_quic_t *         quic,
      78             :                         fd_quic_udpsock_t * udpsock,
      79             :                         ulong               count,
      80             :                         uint                dst_ip,
      81             :                         ushort              dst_port,
      82           0 :                         fd_aio_pkt_info_t * pkt ) {
      83           0 :   fd_quic_set_aio_net_tx( quic, udpsock->aio );
      84           0 :   FD_TEST( fd_quic_init( quic ) );
      85             : 
      86           0 :   quic->cb.now              = cb_now;
      87           0 :   quic->cb.conn_final       = cb_conn_final;
      88           0 :   quic->cb.conn_hs_complete = cb_conn_hs_complete;
      89           0 :   quic->cb.stream_notify    = cb_stream_notify;
      90             : 
      91           0 :   fd_quic_conn_t * conn = fd_quic_connect( quic, dst_ip, dst_port, NULL );
      92           0 :   while ( FD_LIKELY( !( g_conn_hs_complete || g_conn_final ) ) ) {
      93           0 :     fd_quic_service( quic );
      94           0 :     fd_quic_udpsock_service( udpsock );
      95           0 :   }
      96           0 :   FD_TEST( conn );
      97           0 :   if( FD_UNLIKELY( conn->state != FD_QUIC_CONN_STATE_ACTIVE ) )
      98           0 :     FD_LOG_ERR(( "unable to connect to QUIC endpoint at "FD_IP4_ADDR_FMT":%hu, is it running? state is %u", FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port, conn->state ));
      99             : 
     100           0 :   ulong sent = 0;
     101           0 :   while( sent < count && !g_conn_final ) {
     102           0 :     fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
     103           0 :     if( FD_UNLIKELY( !stream ) ) {
     104           0 :       fd_quic_service( quic );
     105           0 :       fd_quic_udpsock_service( udpsock );
     106           0 :       continue;
     107           0 :     }
     108             : 
     109           0 :     fd_aio_pkt_info_t * chunk = pkt + sent;
     110           0 :     int res = fd_quic_stream_send( stream, chunk->buf, chunk->buf_sz, 1 );
     111           0 :     if( FD_UNLIKELY( res != FD_QUIC_SUCCESS ) ) FD_LOG_ERR(( "fd_quic_stream_send failed (%d)", res ));
     112           0 :     sent += 1UL;
     113             : 
     114           0 :     fd_quic_service( quic );
     115           0 :     fd_quic_udpsock_service( udpsock );
     116           0 :   }
     117             : 
     118           0 :   while( FD_LIKELY( g_stream_notify!=count && !g_conn_final ) ) {
     119           0 :     fd_quic_service( quic );
     120           0 :     fd_quic_udpsock_service( udpsock );
     121           0 :   }
     122             : 
     123             :   /* close and wait for connection to complete */
     124           0 :   if( !g_conn_final ) {
     125           0 :     fd_quic_conn_close( conn, 0 );
     126           0 :     while( !g_conn_final ) {
     127           0 :       fd_quic_service( quic );
     128           0 :       fd_quic_udpsock_service( udpsock );
     129           0 :     }
     130           0 :   }
     131             : 
     132           0 :   fd_quic_fini( quic );
     133           0 : }
     134             : 
     135             : void
     136             : txn_cmd_fn( args_t *         args,
     137           0 :             config_t * const config ) {
     138           0 :   if( FD_UNLIKELY( config->development.netns.enabled ) )
     139           0 :     enter_network_namespace( config->development.netns.interface1 );
     140             : 
     141             :   /* wait until validator is ready to receive txns before sending */
     142           0 :   ready_cmd_fn( args, config );
     143             : 
     144           0 :   fd_quic_limits_t quic_limits = {
     145           0 :     .conn_cnt         = 1UL,
     146           0 :     .handshake_cnt    = 1UL,
     147           0 :     .conn_id_cnt      = 4UL,
     148           0 :     .rx_stream_cnt    = 1UL,
     149           0 :     .inflight_pkt_cnt = 64UL,
     150           0 :     .tx_buf_sz        = fd_ulong_pow2_up( FD_TXN_MTU ),
     151           0 :     .stream_pool_cnt  = 16
     152           0 :   };
     153           0 :   ulong quic_footprint = fd_quic_footprint( &quic_limits );
     154           0 :   FD_TEST( quic_footprint );
     155             : 
     156           0 :   fd_wksp_t * wksp = fd_wksp_new_anonymous( FD_SHMEM_NORMAL_PAGE_SZ,
     157           0 :                                             1UL << 10,
     158           0 :                                             fd_shmem_cpu_idx( 0 ),
     159           0 :                                             "wksp",
     160           0 :                                             0UL );
     161           0 :   FD_TEST( wksp );
     162           0 :   void * mem = fd_wksp_alloc_laddr( wksp, fd_quic_align(), quic_footprint, 1UL );
     163           0 :   fd_quic_t * quic = fd_quic_join( fd_quic_new( mem, &quic_limits ) );
     164           0 :   FD_TEST( quic );
     165             : 
     166             :   /* Signer */
     167           0 :   fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) );
     168           0 :   fd_tls_test_sign_ctx_t * sign_ctx = fd_wksp_alloc_laddr( wksp, alignof(fd_tls_test_sign_ctx_t), sizeof(fd_tls_test_sign_ctx_t), 1UL );
     169           0 :   *sign_ctx = fd_tls_test_sign_ctx( rng );
     170             : 
     171           0 :   fd_memcpy( quic->config.identity_public_key, sign_ctx->public_key, 32UL );
     172           0 :   quic->config.sign_ctx = sign_ctx;
     173           0 :   quic->config.sign     = fd_tls_test_sign_sign;
     174             : 
     175           0 :   fd_quic_udpsock_t _udpsock;
     176           0 :   fd_quic_udpsock_t * udpsock = fd_quic_client_create_udpsock( &_udpsock, wksp, fd_quic_get_aio_net_rx( quic ), 0 );
     177           0 :   FD_TEST( udpsock == &_udpsock );
     178             : 
     179           0 :   fd_quic_config_t * client_cfg = &quic->config;
     180           0 :   client_cfg->role = FD_QUIC_ROLE_CLIENT;
     181           0 :   memcpy( client_cfg->link.dst_mac_addr, config->tiles.net.mac_addr, 6UL );
     182           0 :   client_cfg->net.ip_addr           = udpsock->listen_ip;
     183           0 :   client_cfg->net.ephem_udp_port.lo = (ushort)udpsock->listen_port;
     184           0 :   client_cfg->net.ephem_udp_port.hi = (ushort)(udpsock->listen_port + 1);
     185           0 :   client_cfg->idle_timeout = 200UL * 1000UL * 1000UL; /* 5000 millis */
     186           0 :   client_cfg->initial_rx_max_stream_data = 0; /* doesn't receive */
     187             : 
     188           0 :   fd_aio_pkt_info_t pkt[ MAX_TXN_COUNT ];
     189             : 
     190           0 :   if( FD_LIKELY( !args->txn.payload_base64 ) ) {
     191           0 :     FD_LOG_INFO(( "Transaction payload not specified, using hardcoded sample payload" ));
     192           0 :     for( ulong i=0; i<args->txn.count; i++ ) {
     193           0 :       pkt[ i ].buf    = (void * )sample_transaction;
     194           0 :       pkt[ i ].buf_sz = (ushort )sample_transaction_sz;
     195           0 :     }
     196           0 :   } else {
     197           0 :     ulong payload_b64_sz = strlen( args->txn.payload_base64 );
     198             : 
     199           0 :     static uchar buf[ 1UL << 15UL ];
     200           0 :     if( FD_UNLIKELY( FD_BASE64_DEC_SZ( payload_b64_sz ) > sizeof(buf) ) )
     201           0 :       FD_LOG_ERR(( "Input payload is too large (max %lu bytes)", sizeof(buf) ));
     202             : 
     203           0 :     long buf_sz = fd_base64_decode( buf, args->txn.payload_base64, payload_b64_sz );
     204           0 :     if( FD_UNLIKELY( buf_sz<0L ) ) FD_LOG_ERR(( "bad payload input `%s`", args->txn.payload_base64 ));
     205             : 
     206           0 :     for( ulong i=0; i<args->txn.count; i++ ) {
     207           0 :       pkt[ i ].buf    = (void * )buf;
     208           0 :       pkt[ i ].buf_sz = (ushort )buf_sz;
     209           0 :     }
     210           0 :   }
     211             : 
     212           0 :   uint dst_ip = config->tiles.net.ip_addr;
     213           0 :   if( FD_UNLIKELY( args->txn.dst_ip ) )
     214           0 :     if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( args->txn.dst_ip, &dst_ip  ) ) ) FD_LOG_ERR(( "invalid --dst-ip" ));
     215             : 
     216           0 :   ushort dst_port = config->tiles.quic.quic_transaction_listen_port;
     217           0 :   if( FD_UNLIKELY( args->txn.dst_port ) ) dst_port = args->txn.dst_port;
     218             : 
     219           0 :   FD_LOG_NOTICE(( "sending %lu transactions to "FD_IP4_ADDR_FMT":%hu", args->txn.count, FD_IP4_ADDR_FMT_ARGS(dst_ip), dst_port ));
     220             : 
     221           0 :   send_quic_transactions( quic, udpsock, args->txn.count, dst_ip, dst_port, pkt );
     222           0 :   exit_group( 0 );
     223           0 : }

Generated by: LCOV version 1.14