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

Generated by: LCOV version 1.14