LCOV - code coverage report
Current view: top level - app/shared_dev/commands - txn.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 133 0.0 %
Date: 2026-11-15 04:25:17 Functions: 0 6 0.0 %

          Line data    Source code
       1             : #include "../../shared/fd_config.h"
       2             : #include "../../shared/fd_action.h"
       3             : 
       4             : #include "../../platform/fd_sys_util.h"
       5             : #include "../../platform/fd_net_util.h"
       6             : #include "../../shared/commands/ready.h"
       7             : #include "../../../ballet/base64/fd_base64.h"
       8             : #include "../../../waltz/quic/fd_quic.h"
       9             : #include "../../../waltz/quic/tests/fd_quic_test_helpers.h"
      10             : #include "../../../waltz/tls/test_tls_helper.h"
      11             : #include "../../../util/net/fd_ip4.h"
      12             : 
      13             : #include <errno.h>
      14             : #include <sys/random.h>
      15             : #include <linux/capability.h>
      16             : 
      17             : FD_IMPORT_BINARY(sample_transaction, "src/waltz/quic/tests/quic_txn.bin");
      18             : 
      19             : static int g_conn_hs_complete = 0;
      20             : static int g_conn_final = 0;
      21             : 
      22           0 : #define MAX_TXN_COUNT 128
      23             : 
      24             : /* Packet numbers of the stream packets awaiting an ack */
      25             : static ulong g_pkt_num[ MAX_TXN_COUNT ];
      26             : static ulong g_pkt_cnt   = 0UL;
      27             : static ulong g_pkt_acked = 0UL;
      28             : 
      29             : void
      30             : txn_cmd_args( int *    pargc,
      31             :               char *** pargv,
      32           0 :               args_t * args ) {
      33           0 :   args->txn.payload_base64 = fd_env_strip_cmdline_cstr( pargc, pargv, "--payload-base64-encoded", NULL, NULL );
      34           0 :   args->txn.count = fd_env_strip_cmdline_ulong( pargc, pargv, "--count", NULL, 1 );
      35           0 :   if( FD_UNLIKELY( !args->txn.count || args->txn.count > MAX_TXN_COUNT ) )
      36           0 :     FD_LOG_ERR(( "count must be between 1 and %d", MAX_TXN_COUNT ));
      37             : 
      38           0 :   args->txn.dst_ip = fd_env_strip_cmdline_cstr( pargc, pargv, "--dst-ip", NULL, 0 );
      39           0 :   args->txn.dst_port = fd_env_strip_cmdline_ushort( pargc, pargv, "--dst-port", NULL, 0 );
      40           0 : }
      41             : 
      42             : static void
      43             : cb_conn_hs_complete( fd_quic_conn_t * conn,
      44           0 :                      void *           quic_ctx ) {
      45           0 :   (void)conn;
      46           0 :   (void)quic_ctx;
      47           0 :   g_conn_hs_complete = 1;
      48           0 : }
      49             : 
      50             : static void
      51             : cb_conn_final( fd_quic_conn_t * conn,
      52           0 :                void *           quic_ctx ) {
      53           0 :   (void)conn;
      54           0 :   (void)quic_ctx;
      55           0 :   g_conn_final = 1;
      56           0 : }
      57             : 
      58             : static void
      59             : cb_ack_range( fd_quic_conn_t * conn,
      60             :               ulong            pkt_num_lo,
      61             :               ulong            pkt_num_hi,
      62           0 :               void *           quic_ctx ) {
      63           0 :   (void)conn;
      64           0 :   (void)quic_ctx;
      65           0 :   for( ulong i=0UL; i<g_pkt_cnt; i++ ) {
      66           0 :     if( g_pkt_num[ i ]<pkt_num_lo || g_pkt_num[ i ]>pkt_num_hi ) continue;
      67           0 :     g_pkt_num[ i ] = ULONG_MAX;
      68           0 :     g_pkt_acked++;
      69           0 :   }
      70           0 : }
      71             : 
      72             : static void
      73             : send_quic_transactions( fd_quic_t *         quic,
      74             :                         fd_quic_udpsock_t * udpsock,
      75             :                         ulong               count,
      76             :                         uint                dst_ip,
      77             :                         ushort              dst_port,
      78           0 :                         fd_aio_pkt_info_t * pkt ) {
      79           0 :   fd_quic_set_aio_net_tx( quic, udpsock->aio );
      80           0 :   FD_TEST( fd_quic_init( quic ) );
      81             : 
      82           0 :   quic->cb.conn_final       = cb_conn_final;
      83           0 :   quic->cb.conn_hs_complete = cb_conn_hs_complete;
      84           0 :   quic->cb.ack_range        = cb_ack_range;
      85             : 
      86           0 :   fd_quic_conn_t * conn = fd_quic_connect( quic, dst_ip, dst_port, 0U, (ushort)udpsock->listen_port, fd_log_wallclock() );
      87           0 :   while( FD_LIKELY( !( g_conn_hs_complete || g_conn_final ) ) ) {
      88           0 :     fd_quic_service( quic, fd_log_wallclock() );
      89           0 :     fd_quic_udpsock_service( udpsock );
      90           0 :   }
      91           0 :   FD_TEST( conn );
      92           0 :   if( FD_UNLIKELY( conn->state != FD_QUIC_CONN_STATE_ACTIVE ) )
      93           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 ));
      94             : 
      95             :   /* One stream per transaction */
      96           0 :   for( ulong sent=0UL; sent<count && !g_conn_final; sent++ ) {
      97           0 :     fd_aio_pkt_info_t * chunk     = pkt + sent;
      98           0 :     ulong               stream_id = FD_QUIC_STREAM_TYPE_UNI_CLIENT + 4UL*sent;
      99             : 
     100             :     /* wait for the peer to grant the stream (MAX_STREAMS) */
     101           0 :     while( !g_conn_final && stream_id>=conn->tx_sup_stream_id ) {
     102           0 :       fd_quic_service( quic, fd_log_wallclock() );
     103           0 :       fd_quic_udpsock_service( udpsock );
     104           0 :     }
     105           0 :     if( FD_UNLIKELY( g_conn_final ) ) break;
     106             : 
     107           0 :     if( FD_UNLIKELY( !fd_quic_test_stream_send( conn, stream_id, chunk->buf, chunk->buf_sz, &g_pkt_num[ g_pkt_cnt ] ) ) )
     108           0 :       FD_LOG_ERR(( "failed to send transaction (%u bytes)", (uint)chunk->buf_sz ));
     109           0 :     g_pkt_cnt++;
     110             : 
     111           0 :     fd_quic_service( quic, fd_log_wallclock() );
     112           0 :     fd_quic_udpsock_service( udpsock );
     113           0 :   }
     114             : 
     115             :   /* An unacked packet ends this wait at the conn's idle timeout */
     116           0 :   while( FD_LIKELY( g_pkt_acked!=g_pkt_cnt && !g_conn_final ) ) {
     117           0 :     fd_quic_service( quic, fd_log_wallclock() );
     118           0 :     fd_quic_udpsock_service( udpsock );
     119           0 :   }
     120           0 :   if( FD_UNLIKELY( g_pkt_acked!=g_pkt_cnt ) ) FD_LOG_WARNING(( "%lu of %lu packets were not acknowledged", g_pkt_cnt-g_pkt_acked, g_pkt_cnt ));
     121             : 
     122             :   /* close and wait for connection to complete */
     123           0 :   if( !g_conn_final ) {
     124           0 :     fd_quic_conn_close( conn, 0 );
     125           0 :     while( !g_conn_final ) {
     126           0 :       fd_quic_service( quic, fd_log_wallclock() );
     127           0 :       fd_quic_udpsock_service( udpsock );
     128           0 :     }
     129           0 :   }
     130             : 
     131           0 :   fd_quic_fini( quic );
     132           0 : }
     133             : 
     134             : void
     135             : txn_cmd_fn( args_t *   args,
     136           0 :             config_t * config ) {
     137             :   /* wait until validator is ready to receive txns before sending */
     138           0 :   args_t ready_args = {0};
     139           0 :   ready_cmd_fn( &ready_args, config );
     140             : 
     141           0 :   fd_quic_limits_t quic_limits = {
     142           0 :     .conn_cnt           =  1UL,
     143           0 :     .handshake_cnt      =  1UL,
     144           0 :     .conn_id_cnt        =  4UL,
     145           0 :     .inflight_frame_cnt =  2UL*MAX_TXN_COUNT,
     146           0 :   };
     147           0 :   ulong quic_footprint = fd_quic_footprint( &quic_limits );
     148           0 :   FD_TEST( quic_footprint );
     149             : 
     150           0 :   fd_wksp_t * wksp = fd_wksp_new_anonymous( FD_SHMEM_NORMAL_PAGE_SZ,
     151           0 :                                             1UL << 10,
     152           0 :                                             fd_shmem_cpu_idx( 0 ),
     153           0 :                                             "wksp",
     154           0 :                                             0UL );
     155           0 :   FD_TEST( wksp );
     156           0 :   void * mem = fd_wksp_alloc_laddr( wksp, fd_quic_align(), quic_footprint, 1UL );
     157           0 :   fd_quic_t * quic = fd_quic_join( fd_quic_new( mem, &quic_limits ) );
     158           0 :   FD_TEST( quic );
     159             : 
     160             :   /* Signer */
     161           0 :   fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) );
     162           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 );
     163           0 :   fd_tls_test_sign_ctx( sign_ctx, rng );
     164             : 
     165           0 :   fd_memcpy( quic->config.identity_public_key, sign_ctx->public_key, 32UL );
     166           0 :   quic->config.sign_ctx = sign_ctx;
     167           0 :   quic->config.sign     = fd_tls_test_sign_sign;
     168             : 
     169           0 :   fd_quic_udpsock_t _udpsock;
     170           0 :   fd_quic_udpsock_t * udpsock = fd_quic_client_create_udpsock( &_udpsock, wksp, fd_quic_get_aio_net_rx( quic ), 0 );
     171           0 :   FD_TEST( udpsock == &_udpsock );
     172             : 
     173           0 :   fd_quic_config_t * client_cfg = &quic->config;
     174           0 :   client_cfg->role = FD_QUIC_ROLE_CLIENT;
     175           0 :   client_cfg->idle_timeout = 200UL * 1000UL * 1000UL; /* 5000 millis */
     176           0 :   client_cfg->initial_rx_max_stream_data = 0; /* doesn't receive */
     177             : 
     178           0 :   fd_aio_pkt_info_t pkt[ MAX_TXN_COUNT ];
     179             : 
     180           0 :   if( FD_LIKELY( !args->txn.payload_base64 ) ) {
     181           0 :     FD_LOG_INFO(( "Transaction payload not specified, using hardcoded sample payload" ));
     182           0 :     for( ulong i=0; i<args->txn.count; i++ ) {
     183           0 :       pkt[ i ].buf    = (void * )sample_transaction;
     184           0 :       pkt[ i ].buf_sz = (ushort )sample_transaction_sz;
     185           0 :     }
     186           0 :   } else {
     187           0 :     ulong payload_b64_sz = strlen( args->txn.payload_base64 );
     188             : 
     189           0 :     static uchar buf[ 1UL << 15UL ];
     190           0 :     if( FD_UNLIKELY( FD_BASE64_DEC_SZ( payload_b64_sz ) > sizeof(buf) ) )
     191           0 :       FD_LOG_ERR(( "Input payload is too large (max %lu bytes)", sizeof(buf) ));
     192             : 
     193           0 :     long buf_sz = fd_base64_decode( buf, args->txn.payload_base64, payload_b64_sz );
     194           0 :     if( FD_UNLIKELY( buf_sz<0L ) ) FD_LOG_ERR(( "bad payload input `%s`", args->txn.payload_base64 ));
     195           0 :     if( FD_UNLIKELY( (ulong)buf_sz>FD_QUIC_STREAM_TX_MAX ) )
     196           0 :       FD_LOG_ERR(( "Transaction is too large to send in one QUIC packet (%ld bytes, max %lu)", buf_sz, FD_QUIC_STREAM_TX_MAX ));
     197             : 
     198           0 :     for( ulong i=0; i<args->txn.count; i++ ) {
     199           0 :       pkt[ i ].buf    = (void * )buf;
     200           0 :       pkt[ i ].buf_sz = (ushort )buf_sz;
     201           0 :     }
     202           0 :   }
     203             : 
     204           0 :   uint dst_ip = config->net.ip_addr;
     205           0 :   if( FD_UNLIKELY( args->txn.dst_ip ) )
     206           0 :     if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( args->txn.dst_ip, &dst_ip  ) ) ) FD_LOG_ERR(( "invalid --dst-ip" ));
     207             : 
     208           0 :   ushort dst_port = config->tiles.quic.quic_transaction_listen_port;
     209           0 :   if( FD_UNLIKELY( args->txn.dst_port ) ) dst_port = args->txn.dst_port;
     210             : 
     211           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 ));
     212             : 
     213           0 :   send_quic_transactions( quic, udpsock, args->txn.count, dst_ip, dst_port, pkt );
     214           0 :   fd_sys_util_exit_group( 0 );
     215           0 : }
     216             : 
     217             : action_t fd_action_txn = {
     218             :   .name = "txn",
     219             :   .args = txn_cmd_args,
     220             :   .fn   = txn_cmd_fn,
     221             :   .description = "Send a transaction to a running development validator (see the `dev` command)",
     222             :   .usage = "txn [OPTIONS]"
     223             : };

Generated by: LCOV version 1.14