LCOV - code coverage report
Current view: top level - app/shared - fd_config.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 74 500 14.8 %
Date: 2026-03-03 06:00:28 Functions: 2 13 15.4 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_config.h"
       3             : #include "fd_config_private.h"
       4             : 
       5             : #include "../platform/fd_net_util.h"
       6             : #include "../platform/fd_sys_util.h"
       7             : #include "../../ballet/toml/fd_toml.h"
       8             : #include "../../disco/genesis/fd_genesis_cluster.h"
       9             : 
      10             : #include <unistd.h>
      11             : #include <errno.h>
      12             : #include <stdlib.h> /* strtoul */
      13             : #include <sys/utsname.h>
      14             : #include <sys/mman.h>
      15             : 
      16             : /* TODO: Rewrite this ... */
      17             : 
      18             : static inline void
      19             : replace( char *       in,
      20             :          const char * pat,
      21           0 :          const char * sub ) {
      22           0 :   char * replace = strstr( in, pat );
      23           0 :   if( FD_LIKELY( replace ) ) {
      24           0 :     ulong pat_len = strlen( pat );
      25           0 :     ulong sub_len = strlen( sub );
      26           0 :     ulong in_len  = strlen( in );
      27           0 :     if( FD_UNLIKELY( pat_len > in_len ) ) return;
      28             : 
      29           0 :     ulong total_len = in_len - pat_len + sub_len;
      30           0 :     if( FD_UNLIKELY( total_len >= PATH_MAX ) )
      31           0 :       FD_LOG_ERR(( "configuration scratch directory path too long: `%s`", in ));
      32             : 
      33           0 :     uchar after[PATH_MAX] = {0};
      34           0 :     fd_memcpy( after, replace + pat_len, strlen( replace + pat_len ) );
      35           0 :     fd_memcpy( replace, sub, sub_len );
      36           0 :     ulong after_len = strlen( ( const char * ) after );
      37           0 :     fd_memcpy( replace + sub_len, after, after_len );
      38           0 :     in[ total_len ] = '\0';
      39           0 :   }
      40           0 : }
      41             : 
      42             : 
      43             : FD_FN_CONST static inline int
      44           0 : parse_log_level( char const * level ) {
      45           0 :   if( FD_UNLIKELY( !strcmp( level, "DEBUG" ) ) )    return 0;
      46           0 :   if( FD_UNLIKELY( !strcmp( level, "INFO"  ) ) )    return 1;
      47           0 :   if( FD_UNLIKELY( !strcmp( level, "NOTICE"  ) ) )  return 2;
      48           0 :   if( FD_UNLIKELY( !strcmp( level, "WARNING"  ) ) ) return 3;
      49           0 :   if( FD_UNLIKELY( !strcmp( level, "ERR" ) ) )      return 4;
      50           0 :   if( FD_UNLIKELY( !strcmp( level, "CRIT" ) ) )     return 5;
      51           0 :   if( FD_UNLIKELY( !strcmp( level, "ALERT" ) ) )    return 6;
      52           0 :   if( FD_UNLIKELY( !strcmp( level, "EMERG" ) ) )    return 7;
      53           0 :   return -1;
      54           0 : }
      55             : 
      56             : FD_FN_CONST static inline int
      57           0 : parse_core_dump_level( char const * level ) {
      58           0 :   if( FD_UNLIKELY( !strcmp( level, "disabled" ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_DISABLED;
      59           0 :   if( FD_UNLIKELY( !strcmp( level, "minimal"  ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_MINIMAL;
      60           0 :   if( FD_UNLIKELY( !strcmp( level, "regular"  ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_REGULAR;
      61           0 :   if( FD_UNLIKELY( !strcmp( level, "full"     ) ) )       return FD_TOPO_CORE_DUMP_LEVEL_FULL;
      62           0 :   return -1;
      63           0 : }
      64             : 
      65             : void
      66             : fd_config_load_buf( fd_config_t * out,
      67             :                     char const *  buf,
      68             :                     ulong         sz,
      69           0 :                     char const *  path ) {
      70           0 :   static uchar pod_mem[ 1UL<<26 ];
      71           0 :   uchar * pod = fd_pod_join( fd_pod_new( pod_mem, sizeof(pod_mem) ) );
      72             : 
      73           0 :   fd_toml_err_info_t toml_err[1];
      74           0 :   uchar scratch[ 4096 ];
      75           0 :   int toml_errc = fd_toml_parse( buf, sz, pod, scratch, sizeof(scratch), toml_err );
      76           0 :   if( FD_UNLIKELY( toml_errc!=FD_TOML_SUCCESS ) ) {
      77           0 :     switch( toml_errc ) {
      78           0 :     case FD_TOML_ERR_POD:
      79           0 :       FD_LOG_ERR(( "Failed to parse config file (%s): ran out of buffer space while parsing", path ));
      80           0 :       break;
      81           0 :     case FD_TOML_ERR_SCRATCH:
      82           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: ran out of scratch space while parsing", path, toml_err->line ));
      83           0 :       break;
      84           0 :     case FD_TOML_ERR_KEY:
      85           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: oversize key", path, toml_err->line ));
      86           0 :       break;
      87           0 :     case FD_TOML_ERR_DUP:
      88           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: duplicate key", path, toml_err->line ));
      89           0 :       break;
      90           0 :     case FD_TOML_ERR_RANGE:
      91           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu: invalid value for key", path, toml_err->line ));
      92           0 :       break;
      93           0 :     case FD_TOML_ERR_PARSE:
      94           0 :       FD_LOG_ERR(( "Failed to parse config file (%s) at line %lu", path, toml_err->line ));
      95           0 :       break;
      96           0 :     default:
      97           0 :       FD_LOG_ERR(( "Failed to parse config file (%s): %s", path, fd_toml_strerror( toml_errc ) ));
      98           0 :       break;
      99           0 :     }
     100           0 :   }
     101             : 
     102           0 :   if( FD_UNLIKELY( !fd_config_extract_pod( pod, out ) ) ) FD_LOG_ERR(( "Failed to parse config file (%s): there are unrecognized keys logged above", path ));
     103             : 
     104           0 :   fd_pod_delete( fd_pod_leave( pod ) );
     105           0 : }
     106             : 
     107             : static void
     108           0 : fd_config_fillf( fd_config_t * config ) {
     109           0 :   if( FD_UNLIKELY( strcmp( config->paths.accounts, "" ) ) ) {
     110           0 :     replace( config->paths.accounts, "{user}", config->user );
     111           0 :     replace( config->paths.accounts, "{name}", config->name );
     112           0 :   } else {
     113           0 :     FD_TEST( fd_cstr_printf_check( config->paths.accounts, sizeof(config->paths.accounts), NULL, "%s/accounts.db", config->paths.base ) );
     114           0 :   }
     115             : 
     116           0 :   for( ulong i=0UL; i<config->firedancer.paths.authorized_voter_paths_cnt; i++ ) {
     117           0 :     replace( config->firedancer.paths.authorized_voter_paths[ i ], "{user}", config->user );
     118           0 :     replace( config->firedancer.paths.authorized_voter_paths[ i ], "{name}", config->name );
     119           0 :   }
     120           0 : }
     121             : 
     122             : static void
     123           0 : fd_config_fillh( fd_config_t * config ) {
     124           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.paths.accounts_path, "" ) ) ) {
     125           0 :     replace( config->frankendancer.paths.accounts_path, "{user}", config->user );
     126           0 :     replace( config->frankendancer.paths.accounts_path, "{name}", config->name );
     127           0 :   }
     128             : 
     129           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.paths.ledger, "" ) ) ) {
     130           0 :     replace( config->frankendancer.paths.ledger, "{user}", config->user );
     131           0 :     replace( config->frankendancer.paths.ledger, "{name}", config->name );
     132           0 :   } else {
     133           0 :     FD_TEST( fd_cstr_printf_check( config->frankendancer.paths.ledger, sizeof(config->frankendancer.paths.ledger), NULL, "%s/ledger", config->paths.base ) );
     134           0 :   }
     135             : 
     136           0 :   if( FD_UNLIKELY( strcmp( config->frankendancer.snapshots.path, "" ) ) ) {
     137           0 :     replace( config->frankendancer.snapshots.path, "{user}", config->user );
     138           0 :     replace( config->frankendancer.snapshots.path, "{name}", config->name );
     139           0 :   } else {
     140           0 :     strncpy( config->frankendancer.snapshots.path, config->frankendancer.paths.ledger, sizeof(config->frankendancer.snapshots.path) );
     141           0 :   }
     142             : 
     143           0 :   for( ulong i=0UL; i<config->frankendancer.paths.authorized_voter_paths_cnt; i++ ) {
     144           0 :     replace( config->frankendancer.paths.authorized_voter_paths[ i ], "{user}", config->user );
     145           0 :     replace( config->frankendancer.paths.authorized_voter_paths[ i ], "{name}", config->name );
     146           0 :   }
     147             : 
     148           0 :   if( FD_UNLIKELY( config->tiles.quic.quic_transaction_listen_port!=config->tiles.quic.regular_transaction_listen_port+6 ) )
     149           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.quic_transaction_listen_port] `%hu`. "
     150           0 :                  "This must be 6 more than [tiles.quic.regular_transaction_listen_port] `%hu`",
     151           0 :                  config->tiles.quic.quic_transaction_listen_port,
     152           0 :                  config->tiles.quic.regular_transaction_listen_port ));
     153             : 
     154           0 :   char dynamic_port_range[ 32 ];
     155           0 :   fd_memcpy( dynamic_port_range, config->frankendancer.dynamic_port_range, sizeof(dynamic_port_range) );
     156             : 
     157           0 :   char * dash = strstr( dynamic_port_range, "-" );
     158           0 :   if( FD_UNLIKELY( !dash ) )
     159           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     160           0 :                  "This must be formatted like `<min>-<max>`",
     161           0 :                  config->frankendancer.dynamic_port_range ));
     162             : 
     163           0 :   *dash = '\0';
     164           0 :   char * endptr;
     165           0 :   ulong agave_port_min = strtoul( dynamic_port_range, &endptr, 10 );
     166           0 :   if( FD_UNLIKELY( *endptr != '\0' || agave_port_min > USHORT_MAX ) )
     167           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     168           0 :                  "This must be formatted like `<min>-<max>`",
     169           0 :                  config->frankendancer.dynamic_port_range ));
     170           0 :   ulong agave_port_max = strtoul( dash + 1, &endptr, 10 );
     171           0 :   if( FD_UNLIKELY( *endptr != '\0' || agave_port_max > USHORT_MAX ) )
     172           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     173           0 :                  "This must be formatted like `<min>-<max>`",
     174           0 :                  config->frankendancer.dynamic_port_range ));
     175           0 :   if( FD_UNLIKELY( agave_port_min > agave_port_max ) )
     176           0 :     FD_LOG_ERR(( "configuration specifies invalid [dynamic_port_range] `%s`. "
     177           0 :                  "The minimum port must be less than or equal to the maximum port",
     178           0 :                  config->frankendancer.dynamic_port_range ));
     179             : 
     180           0 :   if( FD_UNLIKELY( config->tiles.quic.regular_transaction_listen_port >= agave_port_min &&
     181           0 :                    config->tiles.quic.regular_transaction_listen_port < agave_port_max ) )
     182           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.transaction_listen_port] `%hu`. "
     183           0 :                  "This must be outside the dynamic port range `%s`",
     184           0 :                  config->tiles.quic.regular_transaction_listen_port,
     185           0 :                  config->frankendancer.dynamic_port_range ));
     186             : 
     187           0 :   if( FD_UNLIKELY( config->tiles.quic.quic_transaction_listen_port >= agave_port_min &&
     188           0 :                    config->tiles.quic.quic_transaction_listen_port < agave_port_max ) )
     189           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.quic.quic_transaction_listen_port] `%hu`. "
     190           0 :                  "This must be outside the dynamic port range `%s`",
     191           0 :                  config->tiles.quic.quic_transaction_listen_port,
     192           0 :                  config->frankendancer.dynamic_port_range ));
     193             : 
     194           0 :   if( FD_UNLIKELY( config->tiles.shred.shred_listen_port >= agave_port_min &&
     195           0 :                    config->tiles.shred.shred_listen_port < agave_port_max ) )
     196           0 :     FD_LOG_ERR(( "configuration specifies invalid [tiles.shred.shred_listen_port] `%hu`. "
     197           0 :                  "This must be outside the dynamic port range `%s`",
     198           0 :                  config->tiles.shred.shred_listen_port,
     199           0 :                  config->frankendancer.dynamic_port_range ));
     200           0 : }
     201             : 
     202             : static void
     203           0 : fd_config_fill_net( fd_config_t * config ) {
     204           0 :   if( FD_UNLIKELY( !strcmp( config->net.interface, "" ) ) ) {
     205           0 :     uint ifindex;
     206           0 :     int result = fd_net_util_internet_ifindex( &ifindex );
     207           0 :     if( FD_UNLIKELY( -1==result && errno!=ENODEV ) ) FD_LOG_ERR(( "could not get network device index (%i-%s)", errno, fd_io_strerror( errno ) ));
     208           0 :     else if( FD_UNLIKELY( -1==result ) )
     209           0 :       FD_LOG_ERR(( "no network device found which routes to 8.8.8.8. If no network "
     210           0 :                    "interface is specified in the configuration file, Firedancer "
     211           0 :                    "tries to use the first network interface found which routes to "
     212           0 :                    "8.8.8.8. You can see what this is by running `ip route get 8.8.8.8` "
     213           0 :                    "You can fix this error by specifying a network interface to bind to in "
     214           0 :                    "your configuration file under [net.interface]" ));
     215             : 
     216           0 :     if( FD_UNLIKELY( !if_indextoname( ifindex, config->net.interface ) ) )
     217           0 :       FD_LOG_ERR(( "could not get name of interface with index %u", ifindex ));
     218           0 :   }
     219             : 
     220           0 :   if( FD_UNLIKELY( !if_nametoindex( config->net.interface ) ) )
     221           0 :     FD_LOG_ERR(( "configuration specifies network interface `%s` which does not exist", config->net.interface ));
     222           0 :   uint iface_ip;
     223           0 :   if( FD_UNLIKELY( -1==fd_net_util_if_addr( config->net.interface, &iface_ip ) ) )
     224           0 :     FD_LOG_ERR(( "could not get IP address for interface `%s`", config->net.interface ));
     225             : 
     226           0 :   if( FD_UNLIKELY( config->is_firedancer ) ) {
     227           0 :     if( FD_UNLIKELY( strcmp( config->firedancer.gossip.host, "" ) ) ) {
     228           0 :       uint gossip_ip_addr = iface_ip;
     229           0 :       int  has_gossip_ip4 = 0;
     230           0 :       if( FD_UNLIKELY( strlen( config->firedancer.gossip.host )<=15UL ) ) {
     231             :         /* Only sets gossip_ip_addr if it's a valid IPv4 address, otherwise assume it's a DNS name */
     232           0 :         has_gossip_ip4 = fd_cstr_to_ip4_addr( config->firedancer.gossip.host, &gossip_ip_addr );
     233           0 :       }
     234           0 :       if( FD_UNLIKELY( !fd_ip4_addr_is_public( gossip_ip_addr ) && config->is_live_cluster && has_gossip_ip4 ) )
     235           0 :         FD_LOG_ERR(( "Trying to use [gossip.host] " FD_IP4_ADDR_FMT " for listening to incoming "
     236           0 :                      "transactions, but it is part of a private network and will not be routable "
     237           0 :                      "for other Solana network nodes.", FD_IP4_ADDR_FMT_ARGS( gossip_ip_addr ) ));
     238           0 :     } else if( FD_UNLIKELY( !fd_ip4_addr_is_public( iface_ip ) && config->is_live_cluster ) ) {
     239           0 :       FD_LOG_ERR(( "Trying to use network interface `%s` for listening to incoming transactions, "
     240           0 :                    "but it has IPv4 address " FD_IP4_ADDR_FMT " which is part of a private network "
     241           0 :                    "and will not be routable for other Solana network nodes. If you are running "
     242           0 :                    "behind a NAT and this interface is publicly reachable, you can continue by "
     243           0 :                    "manually specifying the IP address to advertise in your configuration under "
     244           0 :                    "[gossip.host].", config->net.interface, FD_IP4_ADDR_FMT_ARGS( iface_ip ) ));
     245           0 :     }
     246           0 :   }
     247             : 
     248           0 :   config->net.ip_addr = iface_ip;
     249           0 : }
     250             : 
     251             : void
     252             : fd_config_fill( fd_config_t * config,
     253           0 :                 int           is_local_cluster ) {
     254           0 :   struct utsname utsname;
     255           0 :   if( FD_UNLIKELY( -1==uname( &utsname ) ) )
     256           0 :     FD_LOG_ERR(( "could not get uname (%i-%s)", errno, fd_io_strerror( errno ) ));
     257           0 :   strncpy( config->hostname, utsname.nodename, sizeof(config->hostname) );
     258           0 :   config->hostname[ sizeof(config->hostname)-1UL ] = '\0'; /* Just truncate the name if it's too long to fit */
     259             : 
     260           0 :   ulong cluster = FD_CLUSTER_UNKNOWN;
     261           0 :   if( FD_UNLIKELY( !config->is_firedancer ) ) {
     262           0 :     cluster = fd_genesis_cluster_identify( config->frankendancer.consensus.expected_genesis_hash );
     263           0 :   }
     264           0 :   config->is_live_cluster = cluster!=FD_CLUSTER_UNKNOWN;
     265           0 :   strcpy( config->cluster, fd_genesis_cluster_name( cluster ) );
     266             : 
     267           0 :   if( FD_UNLIKELY( !strcmp( config->user, "" ) ) ) {
     268           0 :     const char * user = fd_sys_util_login_user();
     269           0 :     if( FD_UNLIKELY( !user ) )                                                                 FD_LOG_ERR(( "could not automatically determine a user to run Firedancer as. You must specify a [user] in your configuration TOML file." ));
     270           0 :     if( FD_UNLIKELY( strlen( user )>=sizeof( config->user ) ) )                                FD_LOG_ERR(( "user name `%s` is too long", user ));
     271           0 :     strncpy( config->user, user, sizeof(config->user) );
     272           0 :   }
     273             : 
     274           0 :   if( FD_UNLIKELY( -1==fd_sys_util_user_to_uid( config->user, &config->uid, &config->gid ) ) ) FD_LOG_ERR(( "configuration file wants firedancer to run as user `%s` but it does not exist", config->user ));
     275           0 :   if( FD_UNLIKELY( !config->uid || !config->gid ) )                                            FD_LOG_ERR(( "firedancer cannot run as root. please specify a non-root user in the configuration file" ));
     276           0 :   if( FD_UNLIKELY( getuid()!=0U && config->uid!=getuid() ) )                                   FD_LOG_ERR(( "running as uid %u, but config specifies uid %u", getuid(), config->uid ));
     277           0 :   if( FD_UNLIKELY( getgid()!=0U && config->gid!=getgid() ) )                                   FD_LOG_ERR(( "running as gid %u, but config specifies gid %u", getgid(), config->gid ));
     278             : 
     279           0 :   FD_TEST( fd_cstr_printf_check( config->hugetlbfs.gigantic_page_mount_path,
     280           0 :     sizeof(config->hugetlbfs.gigantic_page_mount_path),
     281           0 :     NULL,
     282           0 :     "%s/.gigantic",
     283           0 :     config->hugetlbfs.mount_path ) );
     284           0 :   FD_TEST( fd_cstr_printf_check( config->hugetlbfs.huge_page_mount_path,
     285           0 :     sizeof(config->hugetlbfs.huge_page_mount_path),
     286           0 :     NULL,
     287           0 :     "%s/.huge",
     288           0 :     config->hugetlbfs.mount_path ) );
     289             : 
     290           0 :   ulong max_page_sz = fd_cstr_to_shmem_page_sz( config->hugetlbfs.max_page_size );
     291           0 :   if( FD_UNLIKELY( max_page_sz!=FD_SHMEM_HUGE_PAGE_SZ && max_page_sz!=FD_SHMEM_GIGANTIC_PAGE_SZ ) ) FD_LOG_ERR(( "[hugetlbfs.max_page_size] must be \"huge\" or \"gigantic\"" ));
     292             : 
     293           0 :   replace( config->log.path, "{user}", config->user );
     294           0 :   replace( config->log.path, "{name}", config->name );
     295             : 
     296           0 :   if( FD_LIKELY( !strcmp( "auto", config->log.colorize ) ) )       config->log.colorize1 = 2;
     297           0 :   else if( FD_LIKELY( !strcmp( "true", config->log.colorize ) ) )  config->log.colorize1 = 1;
     298           0 :   else if( FD_LIKELY( !strcmp( "false", config->log.colorize ) ) ) config->log.colorize1 = 0;
     299           0 :   else  FD_LOG_ERR(( "[log.colorize] must be one of \"auto\", \"true\", or \"false\"" ));
     300             : 
     301           0 :   if( FD_LIKELY( 2==config->log.colorize1 ) ) {
     302           0 :     char const * cstr = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "COLORTERM", NULL );
     303           0 :     int truecolor = cstr && !strcmp( cstr, "truecolor" );
     304             : 
     305           0 :     cstr = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "TERM", NULL );
     306           0 :     int color256 = cstr && strstr( cstr, "256color" );
     307             : 
     308           0 :     config->log.colorize1 = truecolor || color256;
     309           0 :   }
     310             : 
     311           0 :   config->log.level_logfile1 = parse_log_level( config->log.level_logfile );
     312           0 :   config->log.level_stderr1  = parse_log_level( config->log.level_stderr );
     313           0 :   config->log.level_flush1   = parse_log_level( config->log.level_flush );
     314           0 :   if( FD_UNLIKELY( -1==config->log.level_logfile1 ) ) FD_LOG_ERR(( "unrecognized [log.level_logfile] `%s`", config->log.level_logfile ));
     315           0 :   if( FD_UNLIKELY( -1==config->log.level_stderr1 ) )  FD_LOG_ERR(( "unrecognized [log.level_stderr] `%s`", config->log.level_stderr ));
     316           0 :   if( FD_UNLIKELY( -1==config->log.level_flush1 ) )   FD_LOG_ERR(( "unrecognized [log.level_flush] `%s`", config->log.level_flush ));
     317             : 
     318           0 :   config->development.core_dump_level = parse_core_dump_level( config->development.core_dump );
     319           0 :   if( FD_UNLIKELY( -1==config->development.core_dump_level ) ) FD_LOG_ERR(( "unrecognized [development.core_dump] `%s`", config->development.core_dump ));
     320             : 
     321           0 :   replace( config->paths.base, "{user}", config->user );
     322           0 :   replace( config->paths.base, "{name}", config->name );
     323             : 
     324           0 :   if( FD_UNLIKELY( !strcmp( config->paths.identity_key, "" ) ) ) {
     325           0 :     if( FD_UNLIKELY( config->is_live_cluster ) ) FD_LOG_ERR(( "configuration file must specify [consensus.identity_path] when joining a live cluster" ));
     326             : 
     327           0 :     FD_TEST( fd_cstr_printf_check( config->paths.identity_key,
     328           0 :                                    sizeof(config->paths.identity_key),
     329           0 :                                    NULL,
     330           0 :                                    "%s/identity.json",
     331           0 :                                    config->paths.base ) );
     332           0 :   } else {
     333           0 :     replace( config->paths.identity_key, "{user}", config->user );
     334           0 :     replace( config->paths.identity_key, "{name}", config->name );
     335           0 :   }
     336             : 
     337           0 :   replace( config->paths.vote_account, "{user}", config->user );
     338           0 :   replace( config->paths.vote_account, "{name}", config->name );
     339             : 
     340           0 :   if( FD_UNLIKELY( strcmp( config->paths.snapshots, "" ) ) ) {
     341           0 :     replace( config->paths.snapshots, "{user}", config->user );
     342           0 :     replace( config->paths.snapshots, "{name}", config->name );
     343           0 :   } else {
     344           0 :     FD_TEST( fd_cstr_printf_check( config->paths.snapshots, sizeof(config->paths.snapshots), NULL, "%s/snapshots", config->paths.base ) );
     345           0 :   }
     346             : 
     347           0 :   if( FD_UNLIKELY( strcmp( config->paths.genesis, "" ) ) ) {
     348           0 :     replace( config->paths.genesis, "{user}", config->user );
     349           0 :     replace( config->paths.genesis, "{name}", config->name );
     350           0 :   } else {
     351           0 :     FD_TEST( fd_cstr_printf_check( config->paths.genesis, sizeof(config->paths.genesis), NULL, "%s/genesis.bin", config->paths.base ) );
     352           0 :   }
     353             : 
     354           0 :   long ts = -fd_log_wallclock();
     355           0 :   config->tick_per_ns_mu = fd_tempo_tick_per_ns( &config->tick_per_ns_sigma );
     356           0 :   FD_LOG_INFO(( "calibrating fd_tempo tick_per_ns took %ld ms", (fd_log_wallclock()+ts)/(1000L*1000L) ));
     357             : 
     358           0 :   if( 0!=strcmp( config->net.bind_address, "" ) ) {
     359           0 :     if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->net.bind_address, &config->net.bind_address_parsed ) ) ) {
     360           0 :       FD_LOG_ERR(( "`net.bind_address` is not a valid IPv4 address" ));
     361           0 :     }
     362           0 :   }
     363             : 
     364           0 :   if(      FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "perf"     ) ) ) config->tiles.pack.schedule_strategy_enum = 0;
     365           0 :   else if( FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "balanced" ) ) ) config->tiles.pack.schedule_strategy_enum = 1;
     366           0 :   else if( FD_LIKELY( !strcmp( config->tiles.pack.schedule_strategy, "revenue"  ) ) ) {
     367           0 :     FD_LOG_WARNING(( "the revenue scheduler is deprecated and will be removed in a future version" ));
     368           0 :     config->tiles.pack.schedule_strategy_enum = 2;
     369           0 :   }
     370           0 :   else FD_LOG_ERR(( "[tiles.pack.schedule_strategy] %s not recognized", config->tiles.pack.schedule_strategy ));
     371             : 
     372           0 :   fd_config_fill_net( config );
     373             : 
     374           0 :   if( FD_UNLIKELY( config->is_firedancer ) ) {
     375           0 :     fd_config_fillf( config );
     376           0 :   } else {
     377           0 :     fd_config_fillh( config );
     378           0 :   }
     379             : 
     380             : 
     381           0 :   if(      FD_LIKELY( !strcmp( config->development.gui.frontend_release_channel, "stable" ) ) ) config->development.gui.frontend_release_channel_enum = 0;
     382           0 :   else if( FD_LIKELY( !strcmp( config->development.gui.frontend_release_channel, "alpha"  ) ) ) config->development.gui.frontend_release_channel_enum = 1;
     383           0 :   else if( FD_LIKELY( !strcmp( config->development.gui.frontend_release_channel, "dev"    ) ) ) config->development.gui.frontend_release_channel_enum = 2;
     384           0 :   else FD_LOG_ERR(( "[development.gui.release_channel] %s not recognized", config->development.gui.frontend_release_channel ));
     385             : 
     386           0 :   if( FD_LIKELY( config->is_live_cluster) ) {
     387           0 :     if( FD_UNLIKELY( !config->development.sandbox ) )                            FD_LOG_ERR(( "trying to join a live cluster, but configuration disables the sandbox which is a development only feature" ));
     388           0 :     if( FD_UNLIKELY( config->development.no_clone ) )                            FD_LOG_ERR(( "trying to join a live cluster, but configuration disables multiprocess which is a development only feature" ));
     389           0 :     if( FD_UNLIKELY( config->development.bench.larger_max_cost_per_block ) )     FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.larger_max_cost_per_block] which is a development only feature" ));
     390           0 :     if( FD_UNLIKELY( config->development.bench.larger_shred_limits_per_block ) ) FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.larger_shred_limits_per_block] which is a development only feature" ));
     391           0 :     if( FD_UNLIKELY( config->development.bench.disable_blockstore_from_slot ) )  FD_LOG_ERR(( "trying to join a live cluster, but configuration has a non-zero value for [development.bench.disable_blockstore_from_slot] which is a development only feature" ));
     392           0 :     if( FD_UNLIKELY( config->development.bench.disable_status_cache ) )          FD_LOG_ERR(( "trying to join a live cluster, but configuration enables [development.bench.disable_status_cache] which is a development only feature" ));
     393           0 :   }
     394             : 
     395             :   /* When running a local cluster, some options are overriden by default
     396             :      to make starting and running in development environments a little
     397             :      easier and less strict. */
     398           0 :   if( FD_UNLIKELY( is_local_cluster ) ) {
     399           0 :     if( FD_LIKELY( !strcmp( config->paths.vote_account, "" ) ) ) {
     400           0 :       FD_TEST( fd_cstr_printf_check( config->paths.vote_account,
     401           0 :                                      sizeof( config->paths.vote_account ),
     402           0 :                                      NULL,
     403           0 :                                      "%s/vote-account.json",
     404           0 :                                      config->paths.base ) );
     405           0 :     }
     406             : 
     407           0 :     strncpy( config->cluster, "development", sizeof(config->cluster) );
     408             : 
     409           0 :     if( FD_UNLIKELY( !config->is_firedancer ) ) {
     410             :       /* By default only_known is true for validators to ensure secure
     411             :         snapshot download, but in development it doesn't matter and
     412             :         often the developer does not provide known peers. */
     413           0 :       config->frankendancer.rpc.only_known = 0;
     414             : 
     415             :       /* When starting from a new genesis block, this needs to be off else
     416             :         the validator will get stuck forever. */
     417           0 :       config->frankendancer.consensus.wait_for_vote_to_start_leader = 0;
     418             : 
     419             :       /* We have to wait until we get a snapshot before we can join a
     420             :         second validator to this one, so make this smaller than the
     421             :         default.  */
     422           0 :       config->frankendancer.snapshots.full_snapshot_interval_slots = fd_uint_min( config->frankendancer.snapshots.full_snapshot_interval_slots, 200U );
     423           0 :     }
     424           0 :   }
     425             : 
     426           0 :   if( FD_UNLIKELY( config->is_firedancer && strcmp( config->firedancer.consensus.wait_for_supermajority_with_bank_hash, "" ) && (!config->consensus.expected_shred_version || config->consensus.wait_for_vote_to_start_leader) ) ) {
     427           0 :     FD_LOG_ERR(( "Config option [consensus.wait_for_supermajority_with_bank_hash] requires consensus.expected_shred_version!=0 and consensus.wait_for_vote_to_start_leader==false." ));
     428           0 :   }
     429             : 
     430           0 :   if( FD_UNLIKELY( config->is_firedancer && config->is_live_cluster && cluster!=FD_CLUSTER_TESTNET ) )
     431           0 :     FD_LOG_ERR(( "Attempted to start against live cluster `%s`. Firedancer is not "
     432           0 :                  "ready for production deployment, has not been tested, and is "
     433           0 :                  "missing consensus critical functionality. Joining a live Solana "
     434           0 :                  "cluster may destabilize the network. Please do not attempt. You "
     435           0 :                  "can start against the testnet cluster by specifying the testnet "
     436           0 :                  "entrypoints from https://docs.solana.com/clusters under "
     437           0 :                  "[gossip.entrypoints] in your configuration file.", fd_genesis_cluster_name( cluster ) ));
     438           0 : }
     439             : 
     440          48 : #define CFG_HAS_NON_EMPTY( key ) do {                  \
     441          48 :   if( !strnlen( config->key, sizeof(config->key) ) ) { \
     442           0 :     FD_LOG_ERR(( "missing `%s`", #key ));              \
     443           0 :   }                                                    \
     444          48 : } while(0)
     445             : 
     446          90 : #define CFG_HAS_NON_ZERO( key ) do {                           \
     447          90 :   if( !config->key ) { FD_LOG_ERR(( "missing `%s`", #key )); } \
     448          90 : } while(0)
     449             : 
     450           6 : #define CFG_HAS_POW2( key ) do {                       \
     451           6 :   ulong value = (ulong)( config -> key );              \
     452           6 :   if( !value || !fd_ulong_is_pow2( value ) ) {         \
     453           0 :     FD_LOG_ERR(( "`%s` must be a power of 2", #key )); \
     454           0 :   }                                                    \
     455           6 : } while(0)
     456             : 
     457             : static void
     458           0 : fd_config_validatef( fd_configf_t const * config ) {
     459           0 :   CFG_HAS_NON_ZERO( layout.sign_tile_count );
     460           0 :   CFG_HAS_NON_ZERO( layout.resolv_tile_count );
     461           0 :   CFG_HAS_NON_ZERO( layout.execle_tile_count );
     462           0 :   CFG_HAS_NON_ZERO( layout.snapshot_hash_tile_count );
     463           0 :   CFG_HAS_NON_ZERO( layout.snapwr_tile_count );
     464           0 :   if( FD_UNLIKELY( config->layout.sign_tile_count < 2 ) ) {
     465           0 :     FD_LOG_ERR(( "layout.sign_tile_count must be >= 2" ));
     466           0 :   }
     467             : 
     468           0 :   if( FD_UNLIKELY( config->snapshots.sources.gossip.allow_any && config->snapshots.sources.gossip.allow_list_cnt>0UL ) ) {
     469           0 :     FD_LOG_ERR(( "`snapshots.sources.gossip` has an explicit list of %lu allowed peer(s) in `allow_list` "
     470           0 :                  "but also allows any peer with `allow_any=true`. `allow_list` has no effect and may "
     471           0 :                  "give a false sense of security. Please modify one of the two options and restart.",
     472           0 :                  config->snapshots.sources.gossip.allow_list_cnt ));
     473           0 :   }
     474           0 :   for( ulong i=0UL; i<config->snapshots.sources.gossip.allow_list_cnt; i++ ) {
     475           0 :     for( ulong j=0UL; j<config->snapshots.sources.gossip.block_list_cnt; j++ ) {
     476           0 :       if( FD_UNLIKELY( 0==strcmp( config->snapshots.sources.gossip.allow_list[ i ], config->snapshots.sources.gossip.block_list[ j ] ) ) ) {
     477           0 :         FD_LOG_ERR(( "`snapshots.sources.gossip` has repeated public key `%s` in both allow[%lu] and block[%lu] lists.  "
     478           0 :                      "Please modify one of the two options and restart.", config->snapshots.sources.gossip.allow_list[ i ], i, j ));
     479             : 
     480           0 :       }
     481           0 :     }
     482           0 :   }
     483             : 
     484           0 :   CFG_HAS_NON_ZERO( accounts.max_accounts           );
     485           0 :   CFG_HAS_NON_ZERO( accounts.file_size_gib          );
     486           0 :   CFG_HAS_NON_ZERO( accounts.mean_account_footprint );
     487           0 :   if( FD_UNLIKELY( !config->accounts.in_memory_only ) ) {
     488           0 :     CFG_HAS_NON_ZERO( accounts.cache_size_gib                );
     489           0 :     CFG_HAS_NON_ZERO( accounts.max_unrooted_account_size_gib );
     490           0 :   }
     491             : 
     492           0 :   if( 0!=strcmp( config->accounts.io_provider, "io_uring" ) &&
     493           0 :       0!=strcmp( config->accounts.io_provider, "portable" ) ) {
     494           0 :     FD_LOG_ERR(( "unsupported `accounts.io_provider`: \"%s\"", config->accounts.io_provider ));
     495           0 :   }
     496           0 : }
     497             : 
     498             : static void
     499           3 : fd_config_validateh( fd_configh_t const * config ) {
     500           3 :   CFG_HAS_NON_EMPTY( dynamic_port_range );
     501             : 
     502           3 :   CFG_HAS_NON_EMPTY( ledger.snapshot_archive_format );
     503             : 
     504           3 :   CFG_HAS_NON_ZERO( snapshots.full_snapshot_interval_slots );
     505           3 :   CFG_HAS_NON_ZERO( snapshots.incremental_snapshot_interval_slots );
     506           3 :   CFG_HAS_NON_ZERO( snapshots.minimum_snapshot_download_speed );
     507           3 :   CFG_HAS_NON_ZERO( snapshots.maximum_snapshot_download_abort );
     508             : 
     509           3 :   CFG_HAS_NON_EMPTY( layout.agave_affinity );
     510           3 :   CFG_HAS_NON_ZERO ( layout.resolh_tile_count );
     511           3 :   CFG_HAS_NON_ZERO ( layout.bank_tile_count );
     512           3 : }
     513             : 
     514             : void
     515           3 : fd_config_validate( fd_config_t const * config ) {
     516           3 :   if( FD_LIKELY( config->is_firedancer ) ) {
     517           0 :     fd_config_validatef( &config->firedancer );
     518           3 :   } else {
     519           3 :     fd_config_validateh( &config->frankendancer );
     520           3 :   }
     521             : 
     522           3 :   CFG_HAS_NON_EMPTY( name );
     523           3 :   CFG_HAS_NON_EMPTY( paths.base );
     524             : 
     525           3 :   CFG_HAS_NON_EMPTY( log.colorize );
     526           3 :   CFG_HAS_NON_EMPTY( log.level_logfile );
     527           3 :   CFG_HAS_NON_EMPTY( log.level_stderr );
     528           3 :   CFG_HAS_NON_EMPTY( log.level_flush );
     529             : 
     530           3 :   CFG_HAS_NON_EMPTY( layout.affinity );
     531           3 :   CFG_HAS_NON_EMPTY( layout.blocklist_cores );
     532           3 :   CFG_HAS_NON_ZERO ( layout.net_tile_count );
     533           3 :   CFG_HAS_NON_ZERO ( layout.quic_tile_count );
     534           3 :   CFG_HAS_NON_ZERO ( layout.verify_tile_count );
     535           3 :   CFG_HAS_NON_ZERO ( layout.shred_tile_count );
     536             : 
     537           3 :   CFG_HAS_NON_EMPTY( hugetlbfs.mount_path );
     538           3 :   CFG_HAS_NON_EMPTY( hugetlbfs.max_page_size );
     539             : 
     540           3 :   CFG_HAS_NON_ZERO( net.ingress_buffer_size );
     541           3 :   if( 0==strcmp( config->net.provider, "xdp" ) ) {
     542           3 :     CFG_HAS_NON_EMPTY( net.xdp.xdp_mode );
     543           3 :     CFG_HAS_POW2     ( net.xdp.xdp_rx_queue_size );
     544           3 :     CFG_HAS_POW2     ( net.xdp.xdp_tx_queue_size );
     545           3 :     if( 0!=strcmp( config->net.xdp.rss_queue_mode, "dedicated" ) &&
     546           3 :         0!=strcmp( config->net.xdp.rss_queue_mode, "simple" ) &&
     547           3 :         0!=strcmp( config->net.xdp.rss_queue_mode, "auto" ) ) {
     548           0 :       FD_LOG_ERR(( "invalid `net.xdp.rss_queue_mode`: \"%s\"; must be \"simple\", \"dedicated\", or \"auto\"",
     549           0 :                    config->net.xdp.rss_queue_mode  ));
     550           0 :     }
     551           3 :   } else if( 0==strcmp( config->net.provider, "socket" ) ) {
     552           0 :     CFG_HAS_NON_ZERO( net.socket.receive_buffer_size );
     553           0 :     CFG_HAS_NON_ZERO( net.socket.send_buffer_size );
     554           0 :   } else {
     555           0 :     FD_LOG_ERR(( "invalid `net.provider`: must be \"xdp\" or \"socket\"" ));
     556           0 :   }
     557             : 
     558           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_routes           );
     559           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_peer_routes      );
     560           3 :   CFG_HAS_NON_ZERO( tiles.netlink.max_neighbors        );
     561             : 
     562           3 :   CFG_HAS_NON_ZERO( tiles.quic.max_concurrent_connections );
     563           3 :   CFG_HAS_NON_ZERO( tiles.quic.txn_reassembly_count );
     564           3 :   CFG_HAS_NON_ZERO( tiles.quic.max_concurrent_handshakes );
     565           3 :   CFG_HAS_NON_ZERO( tiles.quic.idle_timeout_millis );
     566             : 
     567           3 :   CFG_HAS_NON_ZERO( tiles.verify.signature_cache_size );
     568           3 :   CFG_HAS_NON_ZERO( tiles.verify.receive_buffer_size );
     569             : 
     570           3 :   CFG_HAS_NON_ZERO( tiles.dedup.signature_cache_size );
     571             : 
     572           3 :   CFG_HAS_NON_ZERO( tiles.pack.max_pending_transactions );
     573             : 
     574           3 :   CFG_HAS_NON_ZERO( tiles.shred.max_pending_shred_sets );
     575             : 
     576           3 :   if( config->is_firedancer ) {
     577           0 :     CFG_HAS_POW2( tiles.repair.slot_max );
     578           0 :   }
     579             : 
     580           3 :   if( FD_UNLIKELY( config->tiles.bundle.keepalive_interval_millis <    3000 ||
     581           3 :                    config->tiles.bundle.keepalive_interval_millis > 3600000 ) ) {
     582           0 :     FD_LOG_ERR(( "`tiles.bundle.keepalive_interval_millis` must be in range [3000, 3,600,000]" ));
     583           0 :   }
     584             : 
     585           3 :   CFG_HAS_NON_EMPTY( development.core_dump );
     586             : 
     587           3 :   CFG_HAS_NON_ZERO( development.genesis.target_tick_duration_micros );
     588           3 :   CFG_HAS_NON_ZERO( development.genesis.ticks_per_slot );
     589           3 :   CFG_HAS_NON_ZERO( development.genesis.fund_initial_accounts );
     590           3 :   CFG_HAS_NON_ZERO( development.genesis.fund_initial_amount_lamports );
     591             : 
     592           3 :   CFG_HAS_NON_ZERO ( development.bench.benchg_tile_count );
     593           3 :   CFG_HAS_NON_ZERO ( development.bench.benchs_tile_count );
     594           3 :   CFG_HAS_NON_EMPTY( development.bench.affinity );
     595             : 
     596           3 :   CFG_HAS_NON_ZERO( development.bundle.ssl_heap_size_mib );
     597           3 : }
     598             : 
     599             : #undef CFG_HAS_NON_EMPTY
     600             : #undef CFG_HAS_NON_ZERO
     601             : #undef CFG_HAS_POW2
     602             : 
     603             : void
     604             : fd_config_load( int           is_firedancer,
     605             :                 int           is_local_cluster,
     606             :                 char const *  default_config,
     607             :                 ulong         default_config_sz,
     608             :                 char const *  override_config,
     609             :                 char const *  override_config_path,
     610             :                 ulong         override_config_sz,
     611             :                 char const *  user_config,
     612             :                 ulong         user_config_sz,
     613             :                 char const *  user_config_path,
     614           0 :                 fd_config_t * config ) {
     615           0 :   memset( config, 0, sizeof(config_t) );
     616           0 :   config->is_firedancer = is_firedancer;
     617           0 :   config->boot_timestamp_nanos = fd_log_wallclock();
     618             : 
     619           0 :   if( FD_UNLIKELY( is_firedancer ) ) {
     620           0 :     fd_cstr_printf_check( config->development.gui.frontend_release_channel, sizeof(config->development.gui.frontend_release_channel), NULL, "dev" );
     621           0 :   } else {
     622           0 :     fd_cstr_printf_check( config->development.gui.frontend_release_channel, sizeof(config->development.gui.frontend_release_channel), NULL, "stable" );
     623           0 :   }
     624             : 
     625           0 :   fd_config_load_buf( config, default_config, default_config_sz, "default.toml" );
     626           0 :   fd_config_validate( config );
     627           0 :   if( FD_UNLIKELY( override_config ) ) {
     628           0 :     fd_config_load_buf( config, override_config, override_config_sz, override_config_path );
     629           0 :     fd_config_validate( config );
     630           0 :   }
     631           0 :   if( FD_LIKELY( user_config ) ) {
     632           0 :     fd_config_load_buf( config, user_config, user_config_sz, user_config_path );
     633           0 :     fd_config_validate( config );
     634           0 :   }
     635             : 
     636           0 :   fd_config_fill( config, is_local_cluster );
     637           0 : }
     638             : 
     639             : int
     640           0 : fd_config_to_memfd( fd_config_t const * config ) {
     641           0 :   int config_memfd = memfd_create( "fd_config", 0 );
     642           0 :   if( FD_UNLIKELY( -1==config_memfd ) ) return -1;
     643           0 :   if( FD_UNLIKELY( -1==ftruncate( config_memfd, sizeof( config_t ) ) ) ) {
     644           0 :     if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_WARNING(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     645           0 :     return -1;
     646           0 :   }
     647             : 
     648           0 :   uchar * bytes = mmap( NULL, sizeof( config_t ), PROT_READ|PROT_WRITE, MAP_SHARED, config_memfd, 0 );
     649           0 :   if( FD_UNLIKELY( bytes==MAP_FAILED ) ) {
     650           0 :     if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_WARNING(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     651           0 :     return -1;
     652           0 :   }
     653             : 
     654           0 :   fd_memcpy( bytes, config, sizeof( config_t ) );
     655           0 :   if( FD_UNLIKELY( munmap( bytes, sizeof( config_t ) ) ) ) {
     656           0 :     FD_LOG_WARNING(( "munmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     657           0 :     return -1;
     658           0 :   }
     659             : 
     660           0 :   return config_memfd;
     661           0 : }

Generated by: LCOV version 1.14