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