LCOV - code coverage report
Current view: top level - discof/genesis - fd_genesi_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 426 0.0 %
Date: 2026-08-14 04:54:57 Functions: 0 16 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "fd_genesi_tile.h"
       3             : #include "fd_genesis_client.h"
       4             : #include "../../disco/topo/fd_topo.h"
       5             : #include "../../disco/topo/fd_dns_resolve.h"
       6             : #include "../../flamenco/accdb/fd_accdb.h"
       7             : #include "../../flamenco/accdb/fd_accdb_shmem.h"
       8             : #include "../../flamenco/genesis/fd_genesis_parse.h"
       9             : #include "../../disco/events/generated/fd_event_gen.h"
      10             : #include "../../third_party/bzip2/bzlib.h"
      11             : #include "../../ballet/sha256/fd_sha256.h"
      12             : #include "../../flamenco/runtime/fd_hashes.h"
      13             : #include "../../util/archive/fd_tar.h"
      14             : 
      15             : #include <stdio.h>
      16             : #include <errno.h>
      17             : #include <fcntl.h>
      18             : #include <poll.h>
      19             : #include <sys/syscall.h>
      20             : #include <unistd.h>
      21             : #include <netinet/in.h>
      22             : #include <linux/fs.h>
      23             : 
      24             : #include <sys/socket.h>
      25             : #include "generated/fd_genesi_tile_seccomp.h"
      26             : 
      27             : static void *
      28             : bz2_malloc( void * opaque,
      29             :             int    items,
      30           0 :             int    size ) {
      31           0 :   fd_alloc_t * alloc = (fd_alloc_t *)opaque;
      32             : 
      33           0 :   if( FD_UNLIKELY( items<=0 || size<=0 ) ) {
      34           0 :     FD_LOG_WARNING(( "bz2_malloc: invalid items=%d or size=%d", items, size ));
      35           0 :     return NULL;
      36           0 :   }
      37           0 :   void * result = fd_alloc_malloc( alloc, alignof(max_align_t), (ulong)items*(ulong)size );
      38           0 :   if( FD_UNLIKELY( !result ) ) return NULL;
      39           0 :   return result;
      40           0 : }
      41             : 
      42             : static void
      43             : bz2_free( void * opaque,
      44           0 :           void * addr ) {
      45           0 :   fd_alloc_t * alloc = (fd_alloc_t *)opaque;
      46             : 
      47           0 :   if( FD_UNLIKELY( !addr ) ) return;
      48           0 :   fd_alloc_free( alloc, addr );
      49           0 : }
      50             : 
      51             : struct fd_genesi_tile {
      52             :   fd_accdb_t * accdb;
      53             : 
      54             :   fd_hash_t genesis_hash[1];
      55             : 
      56             :   fd_genesis_client_t * client;
      57             : 
      58             :   fd_ip4_port_t entrypoints[ FD_TOPO_GOSSIP_ENTRYPOINTS_MAX ];
      59             :   ulong         entrypoints_cnt;
      60             : 
      61             :   fd_lthash_value_t lthash[1];
      62             : 
      63             :   int local_genesis;
      64             :   int bootstrap;
      65             :   int shutdown;
      66             : 
      67             :   int has_expected_genesis_hash;
      68             :   uchar expected_genesis_hash[ 32UL ];
      69             :   ushort expected_shred_version;
      70             :   int validate_genesis_hash;
      71             : 
      72             :   char genesis_path[ PATH_MAX ];
      73             : 
      74             :   int in_fd;
      75             :   int out_fd;
      76             :   int out_dir_fd;
      77             : 
      78             :   struct {
      79             :     fd_wksp_t * mem;
      80             :     ulong       chunk0;
      81             :   } out;
      82             : 
      83             :   fd_alloc_t * bz2_alloc;
      84             : 
      85             :   fd_genesis_t genesis[1];
      86             :   uchar *      genesis_blob;
      87             :   ulong        genesis_blob_sz;
      88             :   ulong        max_message_size;
      89             : };
      90             : 
      91             : typedef struct fd_genesi_tile fd_genesi_tile_t;
      92             : 
      93             : FD_FN_CONST static inline ulong
      94           0 : scratch_align( void ) {
      95           0 :   ulong a = alignof( fd_genesi_tile_t );
      96           0 :   a = fd_ulong_max( a, fd_genesis_client_align() );
      97           0 :   a = fd_ulong_max( a, fd_alloc_align() );
      98           0 :   return a;
      99           0 : }
     100             : 
     101             : FD_FN_PURE static inline ulong
     102           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
     103           0 :   ulong l = FD_LAYOUT_INIT;
     104           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_genesi_tile_t ), sizeof( fd_genesi_tile_t )    );
     105           0 :   l = FD_LAYOUT_APPEND( l, fd_genesis_client_align(),   fd_genesis_client_footprint() );
     106           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),            fd_alloc_footprint()          );
     107           0 :   if( FD_UNLIKELY( !tile->genesi.entrypoints_cnt ) ) {
     108           0 :     l = FD_LAYOUT_APPEND( l, fd_accdb_align(),          fd_accdb_footprint( tile->genesi.max_live_slots ) );
     109           0 :   }
     110           0 :   l = FD_LAYOUT_APPEND( l, alignof(uchar),              tile->genesi.max_message_size + 4UL*FD_TAR_BLOCK_SZ );
     111           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
     112           0 : }
     113             : 
     114             : FD_FN_CONST static inline ulong
     115           0 : loose_footprint( fd_topo_tile_t const * tile ) {
     116           0 :   (void)tile;
     117             :   /* Leftover space for bzip2 allocations */
     118           0 :   return 1UL<<26; /* 64 MiB */
     119           0 : }
     120             : 
     121             : static inline int
     122           0 : should_shutdown( fd_genesi_tile_t * ctx ) {
     123           0 :   return ctx->shutdown;
     124           0 : }
     125             : 
     126             : static void
     127             : initialize_accdb( fd_accdb_t *         accdb,
     128             :                   fd_genesis_t const * genesis,
     129             :                   uchar const *        genesis_blob,
     130           0 :                   fd_lthash_value_t *  lthash ) {
     131           0 :   fd_accdb_fork_id_t fork_id = fd_accdb_attach_child( accdb, (fd_accdb_fork_id_t) {.val = USHORT_MAX } );
     132             : 
     133           0 :   for( ulong i=0UL; i<genesis->account_cnt; i++ ) {
     134           0 :     fd_genesis_account_t account[1];
     135           0 :     fd_genesis_account( genesis, genesis_blob, account, i );
     136             : 
     137           0 :     fd_acc_t acc = fd_accdb_write_one( accdb, fork_id, account->pubkey.key );
     138           0 :     fd_memcpy( acc.owner, account->owner.uc, 32UL );
     139           0 :     acc.lamports = account->lamports;
     140           0 :     acc.executable = !!account->executable;
     141           0 :     acc.data_len = account->data_len;
     142           0 :     fd_memcpy( acc.data, account->data, account->data_len );
     143             : 
     144           0 :     fd_lthash_value_t new_hash[1];
     145           0 :     fd_hashes_account_lthash_simple( account->pubkey.uc, acc.owner, acc.lamports, acc.executable, account->data, account->data_len, new_hash );
     146           0 :     fd_lthash_add( lthash, new_hash );
     147             : 
     148           0 :     acc.commit = 1;
     149           0 :     fd_accdb_unwrite_one( accdb, &acc );
     150           0 :   }
     151           0 : }
     152             : 
     153             : static inline void
     154             : verify_cluster_type( fd_genesis_t const * genesis,
     155             :                      fd_hash_t const *    genesis_hash,
     156           0 :                      char const *         genesis_path ) {
     157             : 
     158           0 :   fd_hash_t mainnet_hash[1];
     159           0 :   FD_TEST( fd_base58_decode_32( "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d", mainnet_hash->uc ) );
     160             : 
     161           0 :   fd_hash_t testnet_hash[1];
     162           0 :   FD_TEST( fd_base58_decode_32( "4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY", testnet_hash->uc ) );
     163             : 
     164           0 :   fd_hash_t devnet_hash[1];
     165           0 :   FD_TEST( fd_base58_decode_32( "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG", devnet_hash->uc ) );
     166             : 
     167           0 :   switch( genesis->cluster_type ) {
     168           0 :     case FD_GENESIS_TYPE_MAINNET: {
     169           0 :       if( FD_UNLIKELY( !fd_hash_eq( genesis_hash, mainnet_hash ) ) ) {
     170           0 :         FD_BASE58_ENCODE_32_BYTES( genesis_hash->uc, genesis_hash_b58 );
     171           0 :         FD_LOG_ERR(( "genesis file `%s` has cluster type MAINNET but unexpected genesis hash `%s`",
     172           0 :                      genesis_path, genesis_hash_b58 ));
     173           0 :       }
     174           0 :       break;
     175           0 :     }
     176           0 :     case FD_GENESIS_TYPE_TESTNET: {
     177           0 :       if( FD_UNLIKELY( !fd_hash_eq( genesis_hash, testnet_hash ) ) ) {
     178           0 :         FD_BASE58_ENCODE_32_BYTES( genesis_hash->uc, genesis_hash_b58 );
     179           0 :         FD_LOG_ERR(( "genesis file `%s` has cluster type TESTNET but unexpected genesis hash `%s`",
     180           0 :                      genesis_path, genesis_hash_b58 ));
     181           0 :       }
     182           0 :       break;
     183           0 :     }
     184           0 :     case FD_GENESIS_TYPE_DEVNET: {
     185           0 :       if( FD_UNLIKELY( !fd_hash_eq( genesis_hash, devnet_hash ) ) ) {
     186           0 :         FD_BASE58_ENCODE_32_BYTES( genesis_hash->uc, genesis_hash_b58 );
     187           0 :         FD_LOG_ERR(( "genesis file `%s` has cluster type DEVNET but unexpected genesis hash `%s`",
     188           0 :                      genesis_path, genesis_hash_b58 ));
     189           0 :       }
     190           0 :       break;
     191           0 :     }
     192           0 :     default:
     193           0 :       break;
     194           0 :   }
     195           0 : }
     196             : 
     197             : static void
     198             : after_credit( fd_genesi_tile_t *  ctx,
     199             :               fd_stem_context_t * stem,
     200             :               int *               opt_poll_in,
     201           0 :               int *               charge_busy ) {
     202           0 :   (void)opt_poll_in;
     203             : 
     204           0 :   if( FD_UNLIKELY( ctx->shutdown ) ) return;
     205             : 
     206           0 :   if( FD_LIKELY( ctx->local_genesis ) ) {
     207           0 :     FD_TEST( -1!=ctx->in_fd );
     208             : 
     209           0 :     if( FD_UNLIKELY( ctx->genesis_blob_sz>ctx->max_message_size ) ) {
     210           0 :       FD_LOG_ERR(( "Genesis file `%s` exceeds maximum size (blob_sz=%lu max=%lu)",
     211           0 :                    ctx->genesis_path, ctx->genesis_blob_sz, ctx->max_message_size ));
     212           0 :     }
     213           0 :     ulong msg_sz = sizeof(fd_genesis_meta_t) + ctx->genesis_blob_sz;
     214           0 :     if( FD_UNLIKELY( msg_sz>fd_genesi_tile_mtu( ctx->max_message_size ) ) ) {
     215           0 :       FD_LOG_ERR(( "The genesis file `%s` is too large for this Firedancer configuration (msg_sz=%lu max=%lu).\n"
     216           0 :                    "Cannot start Firedancer. Please use a different genesis config or increase `development.genesis.max_file_size_mib`.",
     217           0 :                    ctx->genesis_path, msg_sz, fd_genesi_tile_mtu( ctx->max_message_size ) ));
     218           0 :     }
     219             : 
     220           0 :     fd_genesis_meta_t * dst = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk0 );
     221           0 :     memset( dst, 0, sizeof(fd_genesis_meta_t) );
     222           0 :     dst->creation_time_seconds = ctx->genesis->creation_time;
     223           0 :     dst->genesis_hash = *ctx->genesis_hash;
     224             : 
     225           0 :     if( FD_UNLIKELY( ctx->bootstrap ) ) {
     226           0 :       dst->bootstrap  = 1;
     227           0 :       dst->has_lthash = 1;
     228           0 :       dst->lthash     = *ctx->lthash;
     229           0 :     }
     230             : 
     231           0 :     uchar * dst_blob = (uchar *)( dst+1 );
     232           0 :     dst->blob_sz = ctx->genesis_blob_sz;
     233           0 :     fd_memcpy( dst_blob, ctx->genesis_blob, ctx->genesis_blob_sz );
     234             : 
     235           0 :     fd_stem_publish( stem, 0UL, msg_sz, ctx->out.chunk0, msg_sz, 0UL, 0UL, 0UL );
     236           0 :     *charge_busy = 1;
     237           0 :     FD_LOG_NOTICE(( "loaded local genesis.bin from file %s%s%s", fd_log_style_dim(), ctx->genesis_path, fd_log_style_normal() ));
     238             : 
     239           0 :     ctx->shutdown = 1;
     240           0 :   } else {
     241           0 :     uchar * buffer;
     242           0 :     ulong buffer_sz;
     243           0 :     fd_ip4_port_t peer;
     244           0 :     int result = fd_genesis_client_poll( ctx->client, &peer, &buffer, &buffer_sz, charge_busy );
     245           0 :     if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "failed to retrieve genesis.bin from any configured gossip entrypoints" ));
     246           0 :     if( FD_LIKELY( 1==result ) ) return;
     247             : 
     248           0 :     uchar * decompressed = ctx->genesis_blob;
     249           0 :     ulong   actual_decompressed_sz = 0UL;
     250           0 :     bz_stream bzstrm = {0};
     251           0 :     bzstrm.bzalloc = bz2_malloc;
     252           0 :     bzstrm.bzfree  = bz2_free;
     253           0 :     bzstrm.opaque  = ctx->bz2_alloc;
     254           0 :     int bzerr = BZ2_bzDecompressInit( &bzstrm, 0, 0 );
     255           0 :     if( FD_UNLIKELY( BZ_OK!=bzerr ) ) FD_LOG_ERR(( "BZ2_bzDecompressInit() failed (%d)", bzerr ));
     256             : 
     257           0 :     ulong decompressed_sz = ctx->max_message_size + 4UL*FD_TAR_BLOCK_SZ;
     258             : 
     259           0 :     bzstrm.next_in   = (char *)buffer;
     260           0 :     bzstrm.avail_in  = (uint)buffer_sz;
     261           0 :     bzstrm.next_out  = (char *)decompressed;
     262           0 :     bzstrm.avail_out = (uint)decompressed_sz;
     263           0 :     bzerr = BZ2_bzDecompress( &bzstrm );
     264             :     /* genesis.bin is the first archive entry.  Stop after filling the
     265             :        bounded prefix buffer instead of retaining the trailing RocksDB
     266             :        entries included by Agave. */
     267           0 :     if( FD_UNLIKELY( BZ_STREAM_END!=bzerr && !(BZ_OK==bzerr && !bzstrm.avail_out) ) ) {
     268           0 :       FD_LOG_ERR(( "BZ2_bzDecompress() failed or input was truncated (%d)", bzerr ));
     269           0 :     }
     270             : 
     271           0 :     actual_decompressed_sz = decompressed_sz - (ulong)bzstrm.avail_out;
     272             : 
     273           0 :     bzerr = BZ2_bzDecompressEnd( &bzstrm );
     274           0 :     if( FD_UNLIKELY( BZ_OK!=bzerr ) ) FD_LOG_ERR(( "BZ2_bzDecompressEnd() failed (%d)", bzerr ));
     275             : 
     276           0 :     FD_TEST( actual_decompressed_sz>=FD_TAR_BLOCK_SZ );
     277             : 
     278           0 :     fd_tar_meta_t const * meta = (fd_tar_meta_t const *)decompressed;
     279           0 :     FD_TEST( !strcmp( meta->name, "genesis.bin" ) );
     280           0 :     uchar const * blob    = decompressed+FD_TAR_BLOCK_SZ;
     281           0 :     ulong         blob_sz = fd_tar_meta_get_size( meta );
     282           0 :     if( FD_UNLIKELY( blob_sz==ULONG_MAX ) ) {
     283           0 :       FD_LOG_ERR(( "Genesis blob from peer at `http://" FD_IP4_ADDR_FMT ":%hu` has invalid tar header size",
     284           0 :                    FD_IP4_ADDR_FMT_ARGS( peer.addr ), fd_ushort_bswap( peer.port ) ));
     285           0 :     }
     286           0 :     if( FD_UNLIKELY( blob_sz>ctx->max_message_size ) ) {
     287           0 :       FD_LOG_ERR(( "Genesis blob from peer at `http://" FD_IP4_ADDR_FMT ":%hu` exceeds maximum size (blob_sz=%lu max=%lu)",
     288           0 :                    FD_IP4_ADDR_FMT_ARGS( peer.addr ), fd_ushort_bswap( peer.port ), blob_sz, ctx->max_message_size ));
     289           0 :     }
     290           0 :     FD_TEST( actual_decompressed_sz>=fd_ulong_sat_add( FD_TAR_BLOCK_SZ, blob_sz ) );
     291             : 
     292           0 :     fd_hash_t hash[1];
     293           0 :     fd_sha256_hash( blob, blob_sz, hash->uc );
     294             : 
     295             :     /* Can't verify expected_shred_version here because it needs to be
     296             :        mixed in with hard_forks from the snapshot.  Replay tile will
     297             :        combine them and do this verification. */
     298             : 
     299           0 :     if( FD_LIKELY( ctx->has_expected_genesis_hash && memcmp( hash, ctx->expected_genesis_hash, 32UL ) ) ) {
     300           0 :       FD_BASE58_ENCODE_32_BYTES( ctx->expected_genesis_hash, expected_genesis_hash_b58 );
     301           0 :       FD_BASE58_ENCODE_32_BYTES( hash->uc, hash_b58 );
     302           0 :       FD_LOG_ERR(( "An expected genesis hash of `%s` has been set in your configuration file at [consensus.expected_genesis_hash] "
     303           0 :                    "but the genesis hash derived from the peer at `http://" FD_IP4_ADDR_FMT ":%hu` has unexpected hash `%s`",
     304           0 :                    expected_genesis_hash_b58, FD_IP4_ADDR_FMT_ARGS( peer.addr ), fd_ushort_bswap( peer.port ), hash_b58 ));
     305           0 :     }
     306             : 
     307           0 :     FD_TEST( !ctx->bootstrap );
     308             : 
     309           0 :     fd_genesis_t * genesis = fd_genesis_parse( ctx->genesis, blob, blob_sz );
     310           0 :     if( FD_UNLIKELY( !genesis ) ) {
     311           0 :       FD_LOG_ERR(( "unable to decode downloaded solana genesis file due to violated hardcoded limits" ));
     312           0 :     }
     313             : 
     314           0 :     if( FD_LIKELY( ctx->validate_genesis_hash ) ) {
     315           0 :       verify_cluster_type( genesis, hash, ctx->genesis_path );
     316           0 :     }
     317             : 
     318           0 :     ulong msg_sz = sizeof(fd_genesis_meta_t) + blob_sz;
     319           0 :     if( FD_UNLIKELY( msg_sz>fd_genesi_tile_mtu( ctx->max_message_size ) ) ) {
     320           0 :       FD_LOG_ERR(( "The genesis blob downloaded from peer at `http://" FD_IP4_ADDR_FMT ":%hu` is too large for this Firedancer configuration (msg_sz=%lu max=%lu).\n"
     321           0 :                    "Cannot start Firedancer. Please use a different genesis config or increase `development.genesis.max_file_size_mib`.",
     322           0 :                    FD_IP4_ADDR_FMT_ARGS( peer.addr ), fd_ushort_bswap( peer.port ), msg_sz, fd_genesi_tile_mtu( ctx->max_message_size ) ));
     323           0 :     }
     324             : 
     325           0 :     fd_genesis_meta_t * dst = fd_chunk_to_laddr( ctx->out.mem, ctx->out.chunk0 );
     326           0 :     memset( dst, 0, sizeof(fd_genesis_meta_t) );
     327           0 :     dst->creation_time_seconds = genesis->creation_time;
     328           0 :     dst->genesis_hash = *hash;
     329             : 
     330           0 :     uchar * dst_blob = (uchar *)( dst+1 );
     331           0 :     dst->blob_sz = blob_sz;
     332           0 :     fd_memcpy( dst_blob, blob, blob_sz );
     333             : 
     334           0 :     fd_stem_publish( stem, 0UL, msg_sz, ctx->out.chunk0, 0UL, 0UL, 0UL, 0UL );
     335             : 
     336           0 :     ulong bytes_written = 0UL;
     337           0 :     while( bytes_written<blob_sz ) {
     338           0 :       long result = write( ctx->out_fd, blob+bytes_written, blob_sz-bytes_written );
     339           0 :       if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "write() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     340           0 :       bytes_written += (ulong)result;
     341           0 :     }
     342             : 
     343           0 :     char basename[ PATH_MAX ];
     344           0 :     const char * last_slash = strrchr( ctx->genesis_path, '/' );
     345           0 :     if( FD_LIKELY( last_slash ) ) FD_TEST( fd_cstr_printf_check( basename, PATH_MAX, NULL, "%s", last_slash+1UL ) );
     346           0 :     else                          FD_TEST( fd_cstr_printf_check( basename, PATH_MAX, NULL, "%s", ctx->genesis_path ) );
     347             : 
     348           0 :     char basename_partial[ PATH_MAX ];
     349           0 :     FD_TEST( fd_cstr_printf_check( basename_partial, PATH_MAX, NULL, "%s.partial", basename ) );
     350             : 
     351           0 :     int err = renameat2( ctx->out_dir_fd, basename_partial, ctx->out_dir_fd, basename, RENAME_NOREPLACE );
     352           0 :     if( FD_UNLIKELY( -1==err ) ) FD_LOG_ERR(( "renameat2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     353             : 
     354           0 :     FD_LOG_NOTICE(( "retrieved genesis %s%s%s from peer at %shttp://" FD_IP4_ADDR_FMT ":%hu/genesis.tar.bz2%s",
     355           0 :                     fd_log_style_dim(), ctx->genesis_path, fd_log_style_normal(),
     356           0 :                     fd_log_style_dim(), FD_IP4_ADDR_FMT_ARGS( peer.addr ), peer.port, fd_log_style_normal() ));
     357             : 
     358           0 :     ctx->shutdown = 1;
     359           0 :   }
     360           0 : }
     361             : 
     362             : static void
     363             : process_local_genesis( fd_genesi_tile_t * ctx,
     364           0 :                        char const *       genesis_path ) {
     365           0 :   ctx->genesis_blob_sz = 0UL;
     366           0 :   for(;;) {
     367           0 :     if( FD_UNLIKELY( ctx->genesis_blob_sz==ctx->max_message_size ) ) {
     368           0 :       uchar extra;
     369           0 :       long result = read( ctx->in_fd, &extra, 1UL );
     370           0 :       if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "read() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     371           0 :       if( FD_UNLIKELY( result ) ) {
     372           0 :         FD_LOG_ERR(( "The genesis file at `%s` exceeds `development.genesis.max_file_size_mib` (%lu MiB).",
     373           0 :                      genesis_path, ctx->max_message_size>>20 ));
     374           0 :       }
     375           0 :       break;
     376           0 :     }
     377           0 :     long result = read( ctx->in_fd, ctx->genesis_blob+ctx->genesis_blob_sz, ctx->max_message_size-ctx->genesis_blob_sz );
     378           0 :     if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "read() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     379           0 :     if( FD_UNLIKELY( !result ) ) break;
     380           0 :     ctx->genesis_blob_sz += (ulong)result;
     381           0 :   }
     382             : 
     383           0 :   if( FD_UNLIKELY( -1==close( ctx->in_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     384             : 
     385           0 :   fd_genesis_t * genesis = fd_genesis_parse( ctx->genesis, ctx->genesis_blob, ctx->genesis_blob_sz );
     386           0 :   if( FD_UNLIKELY( !genesis ) ) {
     387           0 :     FD_LOG_ERR(( "unable to decode solana genesis from local file due to violated hardcoded limits" ));
     388           0 :   }
     389             : 
     390           0 :   fd_sha256_hash( ctx->genesis_blob, ctx->genesis_blob_sz, ctx->genesis_hash );
     391           0 :   if( FD_LIKELY( ctx->validate_genesis_hash ) ) {
     392           0 :     verify_cluster_type( genesis, ctx->genesis_hash, genesis_path );
     393           0 :   }
     394             : 
     395           0 :   if( FD_UNLIKELY( ctx->bootstrap && ctx->expected_shred_version ) ) {
     396           0 :     ushort xor = 0;
     397           0 :     for( ulong i=0UL; i<16UL; i++ ) xor ^= ctx->genesis_hash->us[ i ];
     398             : 
     399           0 :     xor = fd_ushort_bswap( xor );
     400           0 :     xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX );
     401             : 
     402           0 :     FD_TEST( xor );
     403             : 
     404           0 :     if( FD_UNLIKELY( xor!=ctx->expected_shred_version ) ) {
     405           0 :       FD_LOG_ERR(( "This node is bootstrapping the cluster as it has no gossip entrypoints provided, but "
     406           0 :                    "a [consensus.expected_shred_version] of %hu is provided which does not match the shred "
     407           0 :                    "version of %hu computed from the genesis.bin file at `%s`",
     408           0 :                    ctx->expected_shred_version, xor, genesis_path ));
     409           0 :     }
     410           0 :   }
     411             : 
     412           0 :   if( FD_LIKELY( ctx->has_expected_genesis_hash && memcmp( ctx->genesis_hash, ctx->expected_genesis_hash, 32UL ) ) ) {
     413           0 :     FD_BASE58_ENCODE_32_BYTES( ctx->expected_genesis_hash, expected_genesis_hash_b58 );
     414           0 :     FD_BASE58_ENCODE_32_BYTES( ctx->genesis_hash->uc,      genesis_hash_b58          );
     415           0 :     FD_LOG_ERR(( "An expected genesis hash of %s%s%s has been set in your configuration file at [consensus.expected_genesis_hash] "
     416           0 :                  "but the genesis hash derived from the genesis file at %s%s%s has unexpected hash %s%s%s",
     417           0 :                  fd_log_style_bold(), expected_genesis_hash_b58, fd_log_style_normal(),
     418           0 :                  fd_log_style_dim(), genesis_path, fd_log_style_normal(),
     419           0 :                  fd_log_style_bold(), genesis_hash_b58, fd_log_style_normal() ));
     420           0 :   }
     421           0 : }
     422             : 
     423             : static void
     424             : privileged_init( fd_topo_t const *      topo,
     425           0 :                  fd_topo_tile_t const * tile ) {
     426           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     427             : 
     428           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     429           0 :   fd_genesi_tile_t * ctx        = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_genesi_tile_t ), sizeof( fd_genesi_tile_t )    );
     430           0 :   fd_genesis_client_t * _client = FD_SCRATCH_ALLOC_APPEND( l, fd_genesis_client_align(),   fd_genesis_client_footprint() );
     431             : 
     432           0 :   fd_memset( ctx, 0, sizeof( fd_genesi_tile_t ) );
     433             : 
     434           0 :   ctx->local_genesis = 1;
     435           0 :   ctx->in_fd = open( tile->genesi.genesis_path, O_RDONLY|O_CLOEXEC );
     436           0 :   if( FD_UNLIKELY( -1==ctx->in_fd ) ) {
     437           0 :     if( FD_LIKELY( errno==ENOENT  ) ) {
     438           0 :       FD_LOG_INFO(( "no local genesis.bin file found at `%s`", tile->genesi.genesis_path ));
     439             : 
     440           0 :       if( FD_UNLIKELY( !tile->genesi.entrypoints_cnt ) ) {
     441           0 :         FD_LOG_ERR(( "This node is bootstrapping the cluster as it has no gossip entrypoints provided, but "
     442           0 :                      "the genesis.bin file at `%s` does not exist.  Please provide a valid genesis.bin "
     443           0 :                      "file by running genesis, or join an existing cluster.",
     444           0 :                      tile->genesi.genesis_path ));
     445           0 :       } else {
     446           0 :         if( FD_UNLIKELY( !tile->genesi.allow_download ) ) {
     447           0 :           FD_LOG_ERR(( "There is no genesis.bin file at `%s` and automatic downloading is disabled as "
     448           0 :                        "genesis_download is false in your configuration file.  Please either provide a valid "
     449           0 :                        "genesis.bin file locally, or allow downloading from a gossip entrypoint.",
     450           0 :                        tile->genesi.genesis_path ));
     451           0 :         } else {
     452           0 :           char basename[ PATH_MAX ];
     453           0 :           fd_cstr_ncpy( basename, tile->genesi.genesis_path, PATH_MAX );
     454           0 :           char * last_slash = strrchr( basename, '/' );
     455           0 :           if( FD_LIKELY( last_slash ) ) *last_slash = '\0';
     456             : 
     457           0 :           ctx->out_dir_fd = open( basename, O_RDONLY|O_CLOEXEC|O_DIRECTORY );
     458           0 :           if( FD_UNLIKELY( -1==ctx->out_dir_fd ) ) FD_LOG_ERR(( "open() failed for genesis dir `%s` (%i-%s)", basename, errno, fd_io_strerror( errno ) ));
     459             : 
     460             :           /* Switch to non-root uid/gid for file creation.  Permissions checks
     461             :             are still done as root. */
     462           0 :           gid_t gid = getgid();
     463           0 :           uid_t uid = getuid();
     464           0 :           if( FD_LIKELY( !gid && -1==syscall( __NR_setresgid, -1, tile->genesi.target_gid, -1 ) ) ) FD_LOG_ERR(( "setresgid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     465           0 :           if( FD_LIKELY( !uid && -1==syscall( __NR_setresuid, -1, tile->genesi.target_uid, -1 ) ) ) FD_LOG_ERR(( "setresuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     466             : 
     467           0 :           char const * genesis_basename = strrchr( tile->genesi.genesis_path, '/' );
     468           0 :           genesis_basename = genesis_basename ? genesis_basename+1UL : tile->genesi.genesis_path;
     469             : 
     470           0 :           char partialname[ PATH_MAX ];
     471           0 :           FD_TEST( fd_cstr_printf_check( partialname, PATH_MAX, NULL, "%s.partial", genesis_basename ) );
     472           0 :           ctx->out_fd = openat( ctx->out_dir_fd, partialname, O_CREAT|O_WRONLY|O_CLOEXEC|O_TRUNC, S_IRUSR|S_IWUSR );
     473           0 :           if( FD_UNLIKELY( -1==ctx->out_fd ) ) FD_LOG_ERR(( "openat() failed for genesis file `%s.partial` (%i-%s)", tile->genesi.genesis_path, errno, fd_io_strerror( errno ) ));
     474             : 
     475           0 :           if( FD_UNLIKELY( -1==syscall( __NR_setresuid, -1, uid, -1 ) ) ) FD_LOG_ERR(( "setresuid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     476           0 :           if( FD_UNLIKELY( -1==syscall( __NR_setresgid, -1, gid, -1 ) ) ) FD_LOG_ERR(( "setresgid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     477             : 
     478           0 :           ctx->local_genesis = 0;
     479           0 :           ctx->client = fd_genesis_client_join( fd_genesis_client_new( _client ) );
     480           0 :           FD_TEST( ctx->client );
     481             : 
     482           0 :           fd_dns_resolve_peers( tile->genesi.entrypoints[ 0 ], sizeof(tile->genesi.entrypoints[ 0 ]), tile->genesi.entrypoints_cnt, "gossip.entrypoints", ctx->entrypoints );
     483           0 :           ctx->entrypoints_cnt = tile->genesi.entrypoints_cnt;
     484             : 
     485           0 :           fd_genesis_client_init( ctx->client, ctx->entrypoints, ctx->entrypoints_cnt );
     486           0 :         }
     487           0 :       }
     488           0 :     } else {
     489           0 :       FD_LOG_ERR(( "could not open genesis.bin file at `%s` (%i-%s)", tile->genesi.genesis_path, errno, fd_io_strerror( errno ) ));
     490           0 :     }
     491           0 :   }
     492           0 : }
     493             : 
     494             : static void
     495             : unprivileged_init( fd_topo_t const *      topo,
     496           0 :                    fd_topo_tile_t const * tile ) {
     497           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     498             : 
     499           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     500           0 :   fd_genesi_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_genesi_tile_t ), sizeof( fd_genesi_tile_t )                        );
     501           0 :                            FD_SCRATCH_ALLOC_APPEND( l, fd_genesis_client_align(),   fd_genesis_client_footprint()                     );
     502           0 :   void * _alloc          = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),            fd_alloc_footprint()                              );
     503           0 :   void * _accdb          = !tile->genesi.entrypoints_cnt ?
     504           0 :                            FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(),            fd_accdb_footprint( tile->genesi.max_live_slots ) ) :
     505           0 :                            NULL;
     506           0 :   void * _genesis_blob   = FD_SCRATCH_ALLOC_APPEND( l, alignof(uchar),              tile->genesi.max_message_size + 4UL*FD_TAR_BLOCK_SZ );
     507             : 
     508           0 :   fd_lthash_zero( ctx->lthash );
     509             : 
     510           0 :   ctx->genesis_blob = _genesis_blob;
     511           0 :   ctx->max_message_size = tile->genesi.max_message_size;
     512           0 :   ctx->shutdown = 0;
     513           0 :   ctx->bootstrap = !tile->genesi.entrypoints_cnt;
     514           0 :   ctx->expected_shred_version = tile->genesi.expected_shred_version;
     515           0 :   ctx->has_expected_genesis_hash = tile->genesi.has_expected_genesis_hash;
     516           0 :   ctx->validate_genesis_hash = tile->genesi.validate_genesis_hash;
     517           0 :   fd_memcpy( ctx->expected_genesis_hash, tile->genesi.expected_genesis_hash, 32UL );
     518             : 
     519           0 :   ctx->accdb = NULL;
     520           0 :   if( FD_UNLIKELY( ctx->bootstrap ) ) {
     521           0 :     void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->genesi.accdb_obj_id );
     522           0 :     fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
     523           0 :     FD_TEST( accdb_shmem );
     524           0 :     ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, 0UL, NULL ) );
     525           0 :     FD_TEST( ctx->accdb );
     526           0 :   }
     527             : 
     528           0 :   if( FD_LIKELY( -1!=ctx->in_fd ) ) {
     529           0 :     process_local_genesis( ctx, tile->genesi.genesis_path );
     530           0 :     if( FD_UNLIKELY( ctx->bootstrap ) ) {
     531           0 :       initialize_accdb( ctx->accdb, ctx->genesis, ctx->genesis_blob, ctx->lthash );
     532           0 :       fd_accdb_flush_metrics( ctx->accdb );
     533           0 :     }
     534           0 :   }
     535             : 
     536           0 :   FD_TEST( fd_cstr_printf_check( ctx->genesis_path, PATH_MAX, NULL, "%s", tile->genesi.genesis_path ) );
     537             : 
     538           0 :   FD_TEST( tile->out_cnt==1UL );
     539           0 :   fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ 0 ] ];
     540           0 :   FD_TEST( out_link->depth==1UL );  /* buffer holds a single message (dcache not a ring buffer) */
     541           0 :   FD_TEST( out_link->mtu>=fd_genesi_tile_mtu( tile->genesi.max_message_size ) );
     542           0 :   ctx->out.mem    = fd_wksp_containing( out_link->dcache );
     543           0 :   ctx->out.chunk0 = fd_dcache_compact_chunk0( ctx->out.mem, out_link->dcache );
     544             : 
     545           0 :   ctx->bz2_alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
     546           0 :   FD_TEST( ctx->bz2_alloc );
     547             : 
     548           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     549           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     550           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     551           0 : }
     552             : 
     553             : static ulong
     554             : rlimit_file_cnt( fd_topo_t const *      topo FD_PARAM_UNUSED,
     555           0 :                  fd_topo_tile_t const * tile ) {
     556           0 :   return 1UL +                         /* stderr */
     557           0 :          1UL +                         /* logfile */
     558           0 :          1UL +                         /* genesis file */
     559           0 :          1UL +                         /* genesis dir */
     560           0 :          tile->genesi.entrypoints_cnt; /* for the client */
     561           0 : }
     562             : 
     563             : static ulong
     564             : populate_allowed_seccomp( fd_topo_t const *      topo,
     565             :                           fd_topo_tile_t const * tile,
     566             :                           ulong                  out_cnt,
     567           0 :                           struct sock_filter *   out ) {
     568             : 
     569           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     570             : 
     571           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     572           0 :   fd_genesi_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_genesi_tile_t ), sizeof( fd_genesi_tile_t ) );
     573             : 
     574           0 :   uint in_fd, out_fd, out_dir_fd;
     575           0 :   if( FD_LIKELY( -1!=ctx->in_fd ) ) {
     576           0 :     in_fd      = (uint)ctx->in_fd;
     577           0 :     out_fd     = (uint)-1;
     578           0 :     out_dir_fd = (uint)-1;
     579           0 :   } else {
     580           0 :     in_fd      = (uint)-1;
     581           0 :     out_fd     = (uint)ctx->out_fd;
     582           0 :     out_dir_fd = (uint)ctx->out_dir_fd;
     583           0 :   }
     584             : 
     585           0 :   uint accounts_fd = !tile->genesi.entrypoints_cnt ? (uint)FD_ACCDB_FD_RW : (uint)-1;
     586           0 :   populate_sock_filter_policy_fd_genesi_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), in_fd, out_fd, out_dir_fd, accounts_fd );
     587           0 :   return sock_filter_policy_fd_genesi_tile_instr_cnt;
     588           0 : }
     589             : 
     590             : static ulong
     591             : populate_allowed_fds( fd_topo_t const *      topo,
     592             :                       fd_topo_tile_t const * tile,
     593             :                       ulong                  out_fds_cnt,
     594           0 :                       int *                  out_fds ) {
     595           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     596             : 
     597           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     598           0 :   fd_genesi_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_genesi_tile_t ), sizeof( fd_genesi_tile_t ) );
     599             : 
     600           0 :   if( FD_UNLIKELY( out_fds_cnt<tile->genesi.entrypoints_cnt+6UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     601             : 
     602           0 :   ulong out_cnt = 0UL;
     603           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     604           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     605           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     606           0 :   if( FD_UNLIKELY( !tile->genesi.entrypoints_cnt ) )
     607           0 :     out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db */
     608             : 
     609           0 :   if( FD_UNLIKELY( -1==ctx->in_fd ) ) {
     610           0 :     FD_TEST( -1!=ctx->out_dir_fd );
     611           0 :     FD_TEST( -1!=ctx->out_fd );
     612           0 :     out_fds[ out_cnt++ ] = ctx->out_dir_fd;
     613           0 :     out_fds[ out_cnt++ ] = ctx->out_fd;
     614             : 
     615           0 :     for( ulong i=0UL; i<tile->genesi.entrypoints_cnt; i++ ) {
     616           0 :       int fd = fd_genesis_client_get_pollfds( ctx->client )[ i ].fd;
     617           0 :       if( FD_LIKELY( -1!=fd ) ) out_fds[ out_cnt++ ] = fd;
     618           0 :     }
     619           0 :   } else {
     620           0 :     FD_TEST( -1!=ctx->in_fd );
     621           0 :     out_fds[ out_cnt++ ] = ctx->in_fd;
     622           0 :   }
     623             : 
     624           0 :   return out_cnt;
     625           0 : }
     626             : 
     627           0 : #define STEM_BURST (1UL)
     628             : 
     629           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_genesi_tile_t
     630           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_genesi_tile_t)
     631             : 
     632           0 : #define STEM_CALLBACK_AFTER_CREDIT    after_credit
     633             : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
     634           0 : #define STEM_LAZY                     ((long)1e5) /* 0.1ms */
     635             : 
     636             : #include "../../disco/stem/fd_stem.c"
     637             : 
     638             : static ulong
     639           0 : max_event_sz( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
     640           0 :   return sizeof(fd_event_accdb_partition_added_t);
     641           0 : }
     642             : 
     643             : fd_topo_run_tile_t fd_tile_genesi = {
     644             :   .name                     = "genesi",
     645             :   .rlimit_file_cnt_fn       = rlimit_file_cnt,
     646             :   .allow_connect            = 1,
     647             :   .allow_renameat           = 1,
     648             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     649             :   .populate_allowed_fds     = populate_allowed_fds,
     650             :   .loose_footprint          = loose_footprint,
     651             :   .scratch_align            = scratch_align,
     652             :   .scratch_footprint        = scratch_footprint,
     653             :   .privileged_init          = privileged_init,
     654             :   .unprivileged_init        = unprivileged_init,
     655             :   .max_event_sz             = max_event_sz,
     656             :   .run                      = stem_run,
     657             : };

Generated by: LCOV version 1.14