Line data Source code
1 : #ifndef HEADER_fd_src_app_fddev_genesis_hash_h 2 : #define HEADER_fd_src_app_fddev_genesis_hash_h 3 : 4 : #include "../../../../ballet/sha256/fd_sha256.h" 5 : 6 : #include <errno.h> 7 : #include <stdio.h> 8 : 9 : static inline ushort 10 : compute_shred_version( char const * genesis_path, 11 0 : uchar * opt_gen_hash ) { 12 : /* Compute the shred version and the genesis hash */ 13 0 : fd_sha256_t _sha[ 1 ]; fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) ); 14 0 : fd_sha256_init( sha ); 15 0 : uchar buffer[ 4096 ]; 16 : 17 0 : FILE * genesis_file = fopen( genesis_path, "r" ); 18 0 : if( FD_UNLIKELY( !genesis_file ) ) { 19 0 : if( FD_LIKELY( errno==ENOENT ) ) return (ushort)0; 20 : 21 0 : FD_LOG_ERR(( "Opening genesis file (%s) failed (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) )); 22 0 : } 23 : 24 0 : while( !feof( genesis_file ) ) { 25 0 : ulong read = fread( buffer, 1UL, sizeof(buffer), genesis_file ); 26 0 : if( FD_UNLIKELY( ferror( genesis_file ) ) ) 27 0 : FD_LOG_ERR(( "fread failed `%s` (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) )); 28 : 29 0 : fd_sha256_append( sha, buffer, read ); 30 0 : } 31 : 32 0 : if( FD_UNLIKELY( fclose( genesis_file ) ) ) 33 0 : FD_LOG_ERR(( "fclose failed `%s` (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) )); 34 : 35 0 : union { 36 0 : uchar c[ 32 ]; 37 0 : ushort s[ 16 ]; 38 0 : } hash; 39 0 : fd_sha256_fini( sha, hash.c ); 40 0 : fd_sha256_delete( fd_sha256_leave( sha ) ); 41 : 42 0 : if( FD_LIKELY( opt_gen_hash ) ) memcpy( opt_gen_hash, hash.c, 32UL ); 43 : 44 0 : ushort xor = 0; 45 0 : for( ulong i=0UL; i<16UL; i++ ) xor ^= hash.s[ i ]; 46 : 47 0 : xor = fd_ushort_bswap( xor ); 48 0 : return fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX ); 49 0 : } 50 : 51 : #endif /* HEADER_fd_src_app_fddev_genesis_hash_h */