Line data Source code
1 : #ifndef HEADER_fd_src_discof_ipecho_genesis_hash_h 2 : #define HEADER_fd_src_discof_ipecho_genesis_hash_h 3 : 4 : #include "../../ballet/sha256/fd_sha256.h" 5 : 6 : #include <errno.h> 7 : #include <fcntl.h> 8 : #include <unistd.h> 9 : 10 : static inline int 11 : compute_shred_version( char const * genesis_path, 12 : ushort * opt_shred_version, 13 0 : uchar * opt_gen_hash ) { 14 0 : fd_sha256_t _sha[ 1 ]; fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) ); 15 0 : fd_sha256_init( sha ); 16 0 : uchar buffer[ 4096 ]; 17 : 18 0 : int fd = open( genesis_path, O_RDONLY ); 19 0 : if( FD_UNLIKELY( -1==fd ) ) return -1; 20 : 21 0 : for(;;) { 22 0 : long result = read( fd, buffer, sizeof(buffer) ); 23 0 : if( FD_UNLIKELY( -1==result ) ) { 24 0 : if( FD_UNLIKELY( -1==close( fd ) ) ) FD_LOG_ERR(( "close failed `%s` (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) )); 25 0 : return -1; 26 0 : } else if( FD_UNLIKELY( !result ) ) break; 27 : 28 0 : fd_sha256_append( sha, buffer, (ulong)result ); 29 0 : } 30 : 31 0 : if( FD_UNLIKELY( -1==close( fd ) ) ) FD_LOG_ERR(( "close failed `%s` (%i-%s)", genesis_path, errno, fd_io_strerror( errno ) )); 32 : 33 0 : union { 34 0 : uchar c[ 32 ]; 35 0 : ushort s[ 16 ]; 36 0 : } hash; 37 : 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 : xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX ); 48 : 49 0 : if( FD_LIKELY( opt_shred_version ) ) *opt_shred_version = xor; 50 : 51 0 : return 0; 52 0 : } 53 : 54 : #endif /* HEADER_fd_src_discof_ipecho_genesis_hash_h */