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