LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_tile_private.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 6 100.0 %
Date: 2025-08-05 05:04:49 Functions: 2 4 50.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_disco_bundle_fd_bundle_tile_private_h
       2             : #define HEADER_fd_src_disco_bundle_fd_bundle_tile_private_h
       3             : 
       4             : #include "fd_bundle_auth.h"
       5             : #include "fd_keepalive.h"
       6             : #include "../stem/fd_stem.h"
       7             : #include "../keyguard/fd_keyswitch.h"
       8             : #include "../keyguard/fd_keyguard_client.h"
       9             : #include "../../waltz/grpc/fd_grpc_client.h"
      10             : #include "../../waltz/resolv/fd_netdb.h"
      11             : #include "../../waltz/fd_rtt_est.h"
      12             : #include "../../util/alloc/fd_alloc.h"
      13             : #include "../../util/hist/fd_histf.h"
      14             : 
      15             : #if FD_HAS_OPENSSL
      16             : #include <openssl/ssl.h> /* SSL_CTX */
      17             : #endif
      18             : 
      19             : struct fd_bundle_out_ctx {
      20             :   ulong       idx;
      21             :   fd_wksp_t * mem;
      22             :   ulong       chunk0;
      23             :   ulong       wmark;
      24             :   ulong       chunk;
      25             : };
      26             : 
      27             : typedef struct fd_bundle_out_ctx fd_bundle_out_ctx_t;
      28             : 
      29             : /* fd_bundle_metrics_t contains private metric counters.  These get
      30             :    published to fd_metrics periodically. */
      31             : 
      32             : struct fd_bundle_metrics {
      33             :   ulong txn_received_cnt;
      34             :   ulong bundle_received_cnt;
      35             :   ulong packet_received_cnt;
      36             :   ulong shredstream_heartbeat_cnt;
      37             :   ulong ping_ack_cnt;
      38             : 
      39             :   ulong decode_fail_cnt;
      40             :   ulong transport_fail_cnt;
      41             :   ulong missing_builder_info_fail_cnt;
      42             : 
      43             :   fd_histf_t msg_rx_delay[1];
      44             : };
      45             : 
      46             : typedef struct fd_bundle_metrics fd_bundle_metrics_t;
      47             : 
      48             : /* fd_bundle_tile_t is the context object provided to callbacks from
      49             :    stem, and contains all state needed to progress the tile. */
      50             : 
      51             : struct fd_bundle_tile {
      52             :   /* Key switch */
      53             :   fd_keyswitch_t * keyswitch;
      54             : 
      55             :   /* Key guard */
      56             :   fd_keyguard_client_t keyguard_client[1];
      57             : 
      58             :   uint is_ssl : 1;
      59             :   int  keylog_fd;
      60             : # if FD_HAS_OPENSSL
      61             :   /* OpenSSL */
      62             :   SSL_CTX *    ssl_ctx;
      63             :   SSL *        ssl;
      64             :   fd_alloc_t * ssl_alloc;
      65             : # endif /* FD_HAS_OPENSSL */
      66             : 
      67             :   /* Config */
      68             :   char   server_fqdn[ 256 ]; /* cstr */
      69             :   ulong  server_fqdn_len;
      70             :   char   server_sni[ 256 ]; /* cstr */
      71             :   ulong  server_sni_len;
      72             :   ushort server_tcp_port;
      73             : 
      74             :   /* Resolver */
      75             :   fd_netdb_fds_t netdb_fds[1];
      76             :   uint server_ip4_addr; /* last DNS lookup result */
      77             : 
      78             :   /* TCP socket */
      79             :   int  tcp_sock;
      80             :   int  so_rcvbuf;
      81             :   uint tcp_sock_connected : 1;
      82             :   uint defer_reset : 1;
      83             :   long cached_ts;
      84             : 
      85             :   /* Keepalive via HTTP/2 PINGs (randomized) */
      86             :   long              keepalive_interval;
      87             :   fd_keepalive_t    keepalive[1];
      88             :   fd_rtt_estimate_t rtt[1];
      89             : 
      90             :   /* gRPC client */
      91             :   void *                   grpc_client_mem;
      92             :   ulong                    grpc_buf_max;
      93             :   fd_grpc_client_t *       grpc_client;
      94             :   fd_grpc_client_metrics_t grpc_metrics[1];
      95             :   ulong                    map_seed;
      96             : 
      97             :   /* Bundle authenticator */
      98             :   fd_bundle_auther_t auther;
      99             : 
     100             :   /* Bundle block builder info */
     101             :   uchar builder_pubkey[ 32 ];
     102             :   uchar builder_commission;  /* in [0,100] (percent) */
     103             :   uchar builder_info_avail : 1;  /* Block builder info available? (potentially stale) */
     104             :   uchar builder_info_wait  : 1;  /* Request already in-flight? */
     105             :   long  builder_info_valid_until;
     106             : 
     107             :   /* Bundle subscriptions */
     108             :   uchar packet_subscription_live : 1;  /* Want to subscribe to a stream? */
     109             :   uchar packet_subscription_wait : 1;  /* Request already in-flight? */
     110             :   uchar bundle_subscription_live : 1;
     111             :   uchar bundle_subscription_wait : 1;
     112             : 
     113             :   /* Bundle state */
     114             :   ulong bundle_seq;
     115             :   ulong bundle_txn_cnt;
     116             : 
     117             :   /* Error backoff */
     118             :   fd_rng_t rng[1];
     119             :   uint     backoff_iter;
     120             :   long     backoff_until;
     121             :   long     backoff_reset;
     122             : 
     123             :   /* Stem publish */
     124             :   fd_stem_context_t * stem;
     125             :   fd_bundle_out_ctx_t verify_out;
     126             :   fd_bundle_out_ctx_t plugin_out;
     127             : 
     128             :   /* App metrics */
     129             :   fd_bundle_metrics_t metrics;
     130             : 
     131             :   /* Check engine light */
     132             :   uchar bundle_status_recent;  /* most recently observed 'check engine light' */
     133             :   uchar bundle_status_plugin;  /* last 'plugin' update written */
     134             :   uchar bundle_status_logged;
     135             :   long  last_bundle_status_log_nanos;
     136             : };
     137             : 
     138             : typedef struct fd_bundle_tile fd_bundle_tile_t;
     139             : 
     140             : /* Define 'request_ctx' IDs to identify different types of gRPC calls */
     141             : 
     142          57 : #define FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets            4
     143          87 : #define FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles            5
     144          30 : #define FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo      6
     145             : 
     146             : FD_PROTOTYPES_BEGIN
     147             : 
     148             : /* fd_bundle_now is an externally linked function wrapping
     149             :    fd_log_wallclock.  This is backed by a weak symbol, allowing tests to
     150             :    override the clock source. */
     151             : 
     152             : long
     153             : fd_bundle_now( void );
     154             : 
     155             : /* fd_bundle_client_grpc_callbacks provides callbacks for grpc_client. */
     156             : 
     157             : extern fd_grpc_client_callbacks_t fd_bundle_client_grpc_callbacks;
     158             : 
     159             : /* fd_bundle_client_step is an all-in-one routine to drive client logic.
     160             :    As long as the tile calls this periodically, the client will
     161             :    reconnect to the bundle server, authenticate, and subscribe to
     162             :    packets and bundles. */
     163             : 
     164             : void
     165             : fd_bundle_client_step( fd_bundle_tile_t * bundle,
     166             :                        int *              charge_busy );
     167             : 
     168             : /* fd_bundle_client_step_reconnect drives the 'reconnect' state machine.
     169             :    Once the HTTP/2 conn is established (SETTINGS exchanged), this
     170             :    function drives the auth logic, requests block builder info, sets up
     171             :    packet and bundle subscriptions, and PINGs. */
     172             : 
     173             : int
     174             : fd_bundle_client_step_reconnect( fd_bundle_tile_t * ctx,
     175             :                                  long               now );
     176             : 
     177             : /* fd_bundle_tile_backoff is called whenever an error occurs.  Stalls
     178             :    forward progress for a randomized amount of time to prevent error
     179             :    floods. */
     180             : 
     181             : void
     182             : fd_bundle_tile_backoff( fd_bundle_tile_t * ctx,
     183             :                         long               now );
     184             : 
     185             : /* fd_bundle_tile_should_stall returns 1 if forward progress should be
     186             :    temporarily prevented due to an error. */
     187             : 
     188             : FD_FN_PURE static inline int
     189             : fd_bundle_tile_should_stall( fd_bundle_tile_t const * ctx,
     190          54 :                              long                     now ) {
     191          54 :   return now < ctx->backoff_until;
     192          54 : }
     193             : 
     194             : /* fd_bundle_tile_housekeeping runs periodically at a low frequency. */
     195             : 
     196             : void
     197             : fd_bundle_tile_housekeeping( fd_bundle_tile_t * ctx );
     198             : 
     199             : /* fd_bundle_client_grpc_rx_start is the first RX callback of a stream. */
     200             : 
     201             : void
     202             : fd_bundle_client_grpc_rx_start(
     203             :     void * app_ctx,
     204             :     ulong  request_ctx
     205             : ) ;
     206             : 
     207             : /* fd_bundle_client_grpc_rx_msg is called by grpc_client when a gRPC
     208             :    message arrives (unary or server-streaming response). */
     209             : 
     210             : void
     211             : fd_bundle_client_grpc_rx_msg(
     212             :     void *       app_ctx,      /* (fd_bundle_tile_t *) */
     213             :     void const * protobuf,
     214             :     ulong        protobuf_sz,
     215             :     ulong        request_ctx   /* FD_BUNDLE_CLIENT_REQ_{...} */
     216             : );
     217             : 
     218             : /* fd_bundle_client_grpc_rx_end is called by grpc_client when a gRPC
     219             :    server-streaming response finishes. */
     220             : 
     221             : void
     222             : fd_bundle_client_grpc_rx_end(
     223             :     void *                app_ctx,
     224             :     ulong                 request_ctx,
     225             :     fd_grpc_resp_hdrs_t * resp
     226             : );
     227             : 
     228             : /* fd_bundle_client_grpc_rx_timeout is called by grpc_client when a
     229             :    gRPC request deadline gets exceeded. */
     230             : 
     231             : void
     232             : fd_bundle_client_grpc_rx_timeout(
     233             :     void * app_ctx,
     234             :     ulong  request_ctx, /* FD_BUNDLE_CLIENT_REQ_{...} */
     235             :     int    deadline_kind /* FD_GRPC_DEADLINE_{HEADER|RX_END} */
     236             : );
     237             : 
     238             : /* fd_bundle_client_status provides a "check engine light".
     239             : 
     240             :    Returns 0 if the client has recently failed and is currently backing
     241             :    off from a reconnect attempt.
     242             : 
     243             :    Returns 1 if the client is currently reconnecting.
     244             : 
     245             :    Returns 2 if all of the following conditions are met:
     246             :    - TCP socket is alive
     247             :    - SSL session is not in an error state
     248             :    - HTTP/2 connection is established (SETTINGS exchange done)
     249             :    - gRPC bundle and packet subscriptions are live
     250             :    - HTTP/2 PING exchange was done recently
     251             : 
     252             :    Return codes are compatible with FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE_STATUS_{...}. */
     253             : 
     254             : int
     255             : fd_bundle_client_status( fd_bundle_tile_t const * ctx );
     256             : 
     257             : /* fd_bundle_request_ctx_cstr returns the gRPC method name for a
     258             :    FD_BUNDLE_CLIENT_REQ_* ID.  Returns "unknown" the ID is not
     259             :    recognized. */
     260             : 
     261             : FD_FN_CONST char const *
     262             : fd_bundle_request_ctx_cstr( ulong request_ctx );
     263             : 
     264             : /* fd_bundle_client_reset frees all connection-related resources. */
     265             : 
     266             : void
     267             : fd_bundle_client_reset( fd_bundle_tile_t * ctx );
     268             : 
     269             : /* fd_bundle_client_ping_tx enqueues a PING frame for sending.  Returns
     270             :    1 on success and 0 on failure (occurs when frame_tx buf is full). */
     271             : 
     272             : void
     273             : fd_bundle_client_send_ping( fd_bundle_tile_t * ctx );
     274             : 
     275             : FD_PROTOTYPES_END
     276             : 
     277             : #endif /* HEADER_fd_src_disco_bundle_fd_bundle_tile_private_h */

Generated by: LCOV version 1.14