@ -37,6 +37,10 @@ std::string bytesToHexString(const uint8_t *str, const uint64_t s) {
return ret . str ( ) ;
return ret . str ( ) ;
}
}
std : : string blockhashToHexString ( ethash_blockhash_t * hash ) {
return bytesToHexString ( ( uint8_t * ) hash , 32 ) ;
}
BOOST_AUTO_TEST_CASE ( fnv_hash_check ) {
BOOST_AUTO_TEST_CASE ( fnv_hash_check ) {
uint32_t x = 1235U ;
uint32_t x = 1235U ;
const uint32_t
const uint32_t
@ -52,12 +56,13 @@ BOOST_AUTO_TEST_CASE(fnv_hash_check) {
}
}
BOOST_AUTO_TEST_CASE ( SHA256_check ) {
BOOST_AUTO_TEST_CASE ( SHA256_check ) {
uint8_t input [ 32 ] , out [ 32 ] ;
ethash_blockhash_t input ;
memcpy ( input , " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
ethash_blockhash_t out ;
SHA3_256 ( out , input , 32 ) ;
memcpy ( & input , " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
SHA3_256 ( & out , ( uint8_t * ) & input , 32 ) ;
const std : : string
const std : : string
expected = " 2b5ddf6f4d21c23de216f44d5e4bdc68e044b71897837ea74c83908be7037cd7 " ,
expected = " 2b5ddf6f4d21c23de216f44d5e4bdc68e044b71897837ea74c83908be7037cd7 " ,
actual = bytesToHexString ( out , 32 ) ;
actual = bytesToHexString ( ( uint8_t * ) & out , 32 ) ;
BOOST_REQUIRE_MESSAGE ( expected = = actual ,
BOOST_REQUIRE_MESSAGE ( expected = = actual ,
" \n expected: " < < expected . c_str ( ) < < " \n "
" \n expected: " < < expected . c_str ( ) < < " \n "
< < " actual: " < < actual . c_str ( ) < < " \n " ) ;
< < " actual: " < < actual . c_str ( ) < < " \n " ) ;
@ -104,23 +109,26 @@ BOOST_AUTO_TEST_CASE(ethash_params_init_genesis_calcifide_check) {
BOOST_AUTO_TEST_CASE ( light_and_full_client_checks ) {
BOOST_AUTO_TEST_CASE ( light_and_full_client_checks ) {
ethash_params params ;
ethash_params params ;
uint8_t seed [ 32 ] , hash [ 32 ] , difficulty [ 32 ] ;
ethash_blockhash_t seed ;
ethash_return_value light_out , full_out ;
ethash_blockhash_t hash ;
memcpy ( seed , " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
ethash_blockhash_t difficulty ;
memcpy ( hash , " ~~~X~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
ethash_return_value light_out ;
ethash_return_value full_out ;
memcpy ( & seed , " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
memcpy ( & hash , " ~~~X~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " , 32 ) ;
// Set the difficulty
// Set the difficulty
difficulty [ 0 ] = 197 ;
ethash_blockhash_set ( & difficulty , 0 , 197 ) ;
difficulty [ 1 ] = 90 ;
ethash_blockhash_set ( & difficulty , 1 , 90 ) ;
for ( int i = 2 ; i < 32 ; i + + )
for ( int i = 2 ; i < 32 ; i + + )
difficulty [ i ] = ( uint8_t ) 255 ;
ethash_blockhash_set ( & difficulty , i , 255 ) ;
ethash_params_init ( & params , 0 ) ;
ethash_params_init ( & params , 0 ) ;
params . cache_size = 1024 ;
params . cache_size = 1024 ;
params . full_size = 1024 * 32 ;
params . full_size = 1024 * 32 ;
ethash_cache cache ;
ethash_cache cache ;
cache . mem = our_alloca ( params . cache_size ) ;
cache . mem = our_alloca ( params . cache_size ) ;
ethash_mkcache ( & cache , & params , seed ) ;
ethash_mkcache ( & cache , & params , & seed ) ;
node * full_mem = ( node * ) our_alloca ( params . full_size ) ;
node * full_mem = ( node * ) our_alloca ( params . full_size ) ;
ethash_compute_full_data ( full_mem , & params , & cache ) ;
ethash_compute_full_data ( full_mem , & params , & cache ) ;
@ -164,78 +172,76 @@ BOOST_AUTO_TEST_CASE(light_and_full_client_checks) {
{
{
uint64_t nonce = 0x7c7c597c ;
uint64_t nonce = 0x7c7c597c ;
ethash_full ( & full_out , full_mem , & params , hash , nonce ) ;
ethash_full ( & full_out , full_mem , & params , & hash , nonce ) ;
ethash_light ( & light_out , & cache , & params , hash , nonce ) ;
ethash_light ( & light_out , & cache , & params , & hash , nonce ) ;
const std : : string
const std : : string
light_result_string = bytesToHexString ( light_out . result , 32 ) ,
light_result_string = blockhashToHexString ( & light_out . result ) ,
full_result_string = bytesToHexString ( full_out . result , 32 ) ;
full_result_string = blockhashToHexString ( & full_out . result ) ;
BOOST_REQUIRE_MESSAGE ( light_result_string = = full_result_string ,
BOOST_REQUIRE_MESSAGE ( light_result_string = = full_result_string ,
" \n light result: " < < light_result_string . c_str ( ) < < " \n "
" \n light result: " < < light_result_string . c_str ( ) < < " \n "
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
const std : : string
const std : : string
light_mix_hash_string = bytesToHexString ( light_out . mix_hash , 32 ) ,
light_mix_hash_string = blockhashToHexString ( & light_out . mix_hash ) ,
full_mix_hash_string = bytesToHexString ( full_out . mix_hash , 32 ) ;
full_mix_hash_string = blockhashToHexString ( & full_out . mix_hash ) ;
BOOST_REQUIRE_MESSAGE ( full_mix_hash_string = = light_mix_hash_string ,
BOOST_REQUIRE_MESSAGE ( full_mix_hash_string = = light_mix_hash_string ,
" \n light mix hash: " < < light_mix_hash_string . c_str ( ) < < " \n "
" \n light mix hash: " < < light_mix_hash_string . c_str ( ) < < " \n "
< < " full mix hash: " < < full_mix_hash_string . c_str ( ) < < " \n " ) ;
< < " full mix hash: " < < full_mix_hash_string . c_str ( ) < < " \n " ) ;
uint8_t check_hash [ 32 ] ;
ethash_blockhash_t check_hash ;
ethash_quick_hash ( check_hash , hash , nonce , full_out . mix_hash ) ;
ethash_quick_hash ( & check_hash , & hash , nonce , & full_out . mix_hash ) ;
const std : : string check_hash_string = bytesToHexString ( check_hash , 32 ) ;
const std : : string check_hash_string = blockhashToHexString ( & check_hash ) ;
BOOST_REQUIRE_MESSAGE ( check_hash_string = = full_result_string ,
BOOST_REQUIRE_MESSAGE ( check_hash_string = = full_result_string ,
" \n check hash string: " < < check_hash_string . c_str ( ) < < " \n "
" \n check hash string: " < < check_hash_string . c_str ( ) < < " \n "
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
}
}
{
{
ethash_full ( & full_out , full_mem , & params , hash , 5 ) ;
ethash_full ( & full_out , full_mem , & params , & hash , 5 ) ;
std : : string
std : : string
light_result_string = bytesToHexString ( light_out . result , 32 ) ,
light_result_string = blockhashToHexString ( & light_out . result ) ,
full_result_string = bytesToHexString ( full_out . result , 32 ) ;
full_result_string = blockhashToHexString ( & full_out . result ) ;
BOOST_REQUIRE_MESSAGE ( light_result_string ! = full_result_string ,
BOOST_REQUIRE_MESSAGE ( light_result_string ! = full_result_string ,
" \n light result and full result should differ: " < < light_result_string . c_str ( ) < < " \n " ) ;
" \n light result and full result should differ: " < < light_result_string . c_str ( ) < < " \n " ) ;
ethash_light ( & light_out , & cache , & params , hash , 5 ) ;
ethash_light ( & light_out , & cache , & params , & hash , 5 ) ;
light_result_string = bytesToHexString ( light_out . result , 32 ) ;
light_result_string = blockhashToHexString ( & light_out . result ) ;
BOOST_REQUIRE_MESSAGE ( light_result_string = = full_result_string ,
BOOST_REQUIRE_MESSAGE ( light_result_string = = full_result_string ,
" \n light result and full result should be the same \n "
" \n light result and full result should be the same \n "
< < " light result: " < < light_result_string . c_str ( ) < < " \n "
< < " light result: " < < light_result_string . c_str ( ) < < " \n "
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
< < " full result: " < < full_result_string . c_str ( ) < < " \n " ) ;
std : : string
std : : string
light_mix_hash_string = bytesToHexString ( light_out . mix_hash , 32 ) ,
light_mix_hash_string = blockhashToHexString ( & light_out . mix_hash ) ,
full_mix_hash_string = bytesToHexString ( full_out . mix_hash , 32 ) ;
full_mix_hash_string = blockhashToHexString ( & full_out . mix_hash ) ;
BOOST_REQUIRE_MESSAGE ( full_mix_hash_string = = light_mix_hash_string ,
BOOST_REQUIRE_MESSAGE ( full_mix_hash_string = = light_mix_hash_string ,
" \n light mix hash: " < < light_mix_hash_string . c_str ( ) < < " \n "
" \n light mix hash: " < < light_mix_hash_string . c_str ( ) < < " \n "
< < " full mix hash: " < < full_mix_hash_string . c_str ( ) < < " \n " ) ;
< < " full mix hash: " < < full_mix_hash_string . c_str ( ) < < " \n " ) ;
BOOST_REQUIRE_MESSAGE ( ethash_check_difficulty ( full_out . result , difficulty ) ,
BOOST_REQUIRE_MESSAGE ( ethash_check_difficulty ( & full_out . result , & difficulty ) ,
" ethash_check_difficulty failed "
" ethash_check_difficulty failed "
) ;
) ;
BOOST_REQUIRE_MESSAGE ( ethash_quick_check_difficulty ( hash , 5U , full_out . mix_hash , difficulty ) ,
BOOST_REQUIRE_MESSAGE ( ethash_quick_check_difficulty ( & hash , 5U , & full_out . mix_hash , & difficulty ) ,
" ethash_quick_check_difficulty failed "
" ethash_quick_check_difficulty failed "
) ;
) ;
}
}
}
}
BOOST_AUTO_TEST_CASE ( ethash_check_difficulty_check ) {
BOOST_AUTO_TEST_CASE ( ethash_check_difficulty_check ) {
uint8_t hash [ 32 ] , target [ 32 ] ;
ethash_blockhash_t hash ;
memset ( hash , 0 , 32 ) ;
ethash_blockhash_t target ;
memset ( target , 0 , 32 ) ;
memcpy ( & hash , " 11111111111111111111111111111111 " , 32 ) ;
memcpy ( & target , " 22222222222222222222222222222222 " , 32 ) ;
memcpy ( hash , " 11111111111111111111111111111111 " , 32 ) ;
memcpy ( target , " 22222222222222222222222222222222 " , 32 ) ;
BOOST_REQUIRE_MESSAGE (
BOOST_REQUIRE_MESSAGE (
ethash_check_difficulty ( hash , target ) ,
ethash_check_difficulty ( & hash , & target ) ,
" \n expected \" " < < std : : string ( ( char * ) hash , 32 ) . c_str ( ) < < " \" to have the same or less difficulty than \" " < < std : : string ( ( char * ) target , 32 ) . c_str ( ) < < " \" \n " ) ;
" \n expected \" " < < std : : string ( ( char * ) & hash , 32 ) . c_str ( ) < < " \" to have the same or less difficulty than \" " < < std : : string ( ( char * ) & target , 32 ) . c_str ( ) < < " \" \n " ) ;
BOOST_REQUIRE_MESSAGE (
BOOST_REQUIRE_MESSAGE (
ethash_check_difficulty ( hash , hash ) ,
ethash_check_difficulty ( & hash , & hash ) , " " ) ;
" \n expected \" " < < hash < < " \" to have the same or less difficulty than \" " < < hash < < " \" \n " ) ;
// "\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << hash << "\"\n");
memcpy ( target , " 11111111111111111111111111111112 " , 32 ) ;
memcpy ( & target , " 11111111111111111111111111111112 " , 32 ) ;
BOOST_REQUIRE_MESSAGE (
BOOST_REQUIRE_MESSAGE (
ethash_check_difficulty ( hash , target ) ,
ethash_check_difficulty ( & hash , & target ) , " " ) ;
" \n expected \" " < < hash < < " \" to have the same or less difficulty than \" " < < target < < " \" \n " ) ;
// "\nexpected \"" << hash << "\" to have the same or less difficulty than \"" << target << "\"\n");
memcpy ( target , " 11111111111111111111111111111110 " , 32 ) ;
memcpy ( & target , " 11111111111111111111111111111110 " , 32 ) ;
BOOST_REQUIRE_MESSAGE (
BOOST_REQUIRE_MESSAGE (
! ethash_check_difficulty ( hash , target ) ,
! ethash_check_difficulty ( & hash , & target ) , " " ) ;
" \n expected \" " < < hash < < " \" to have more difficulty than \" " < < target < < " \" \n " ) ;
// "\nexpected \"" << hash << "\" to have more difficulty than \"" << target << "\"\n");
}
}
BOOST_AUTO_TEST_CASE ( test_ethash_dir_creation ) {
BOOST_AUTO_TEST_CASE ( test_ethash_dir_creation ) {
@ -256,7 +262,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_dir_creation) {
BOOST_AUTO_TEST_CASE ( test_ethash_io_write_files_are_created ) {
BOOST_AUTO_TEST_CASE ( test_ethash_io_write_files_are_created ) {
ethash_blockhash_t seedhash ;
ethash_blockhash_t seedhash ;
static const int blockn = 0 ;
static const int blockn = 0 ;
ethash_get_seedhash ( ( uint8_t * ) & seedhash , blockn ) ;
ethash_get_seedhash ( & seedhash , blockn ) ;
BOOST_REQUIRE_EQUAL (
BOOST_REQUIRE_EQUAL (
ETHASH_IO_MEMO_MISMATCH ,
ETHASH_IO_MEMO_MISMATCH ,
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
@ -273,7 +279,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_write_files_are_created) {
params . cache_size = 1024 ;
params . cache_size = 1024 ;
params . full_size = 1024 * 32 ;
params . full_size = 1024 * 32 ;
cache . mem = our_alloca ( params . cache_size ) ;
cache . mem = our_alloca ( params . cache_size ) ;
ethash_mkcache ( & cache , & params , ( uint8_t * ) & seedhash ) ;
ethash_mkcache ( & cache , & params , & seedhash ) ;
BOOST_REQUIRE (
BOOST_REQUIRE (
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )
@ -290,7 +296,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_write_files_are_created) {
BOOST_AUTO_TEST_CASE ( test_ethash_io_memo_file_match ) {
BOOST_AUTO_TEST_CASE ( test_ethash_io_memo_file_match ) {
ethash_blockhash_t seedhash ;
ethash_blockhash_t seedhash ;
static const int blockn = 0 ;
static const int blockn = 0 ;
ethash_get_seedhash ( ( uint8_t * ) & seedhash , blockn ) ;
ethash_get_seedhash ( & seedhash , blockn ) ;
BOOST_REQUIRE_EQUAL (
BOOST_REQUIRE_EQUAL (
ETHASH_IO_MEMO_MISMATCH ,
ETHASH_IO_MEMO_MISMATCH ,
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
@ -307,7 +313,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_match) {
params . cache_size = 1024 ;
params . cache_size = 1024 ;
params . full_size = 1024 * 32 ;
params . full_size = 1024 * 32 ;
cache . mem = our_alloca ( params . cache_size ) ;
cache . mem = our_alloca ( params . cache_size ) ;
ethash_mkcache ( & cache , & params , ( uint8_t * ) & seedhash ) ;
ethash_mkcache ( & cache , & params , & seedhash ) ;
BOOST_REQUIRE (
BOOST_REQUIRE (
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )
@ -344,7 +350,7 @@ static std::vector<char> readFileIntoVector(char const* filename)
BOOST_AUTO_TEST_CASE ( test_ethash_io_memo_file_contents ) {
BOOST_AUTO_TEST_CASE ( test_ethash_io_memo_file_contents ) {
ethash_blockhash_t seedhash ;
ethash_blockhash_t seedhash ;
static const int blockn = 0 ;
static const int blockn = 0 ;
ethash_get_seedhash ( ( uint8_t * ) & seedhash , blockn ) ;
ethash_get_seedhash ( & seedhash , blockn ) ;
BOOST_REQUIRE_EQUAL (
BOOST_REQUIRE_EQUAL (
ETHASH_IO_MEMO_MISMATCH ,
ETHASH_IO_MEMO_MISMATCH ,
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
ethash_io_prepare ( " ./test_ethash_directory/ " , seedhash )
@ -361,7 +367,7 @@ BOOST_AUTO_TEST_CASE(test_ethash_io_memo_file_contents) {
params . cache_size = 1024 ;
params . cache_size = 1024 ;
params . full_size = 1024 * 32 ;
params . full_size = 1024 * 32 ;
cache . mem = our_alloca ( params . cache_size ) ;
cache . mem = our_alloca ( params . cache_size ) ;
ethash_mkcache ( & cache , & params , ( uint8_t * ) & seedhash ) ;
ethash_mkcache ( & cache , & params , & seedhash ) ;
BOOST_REQUIRE (
BOOST_REQUIRE (
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )
ethash_io_write ( " ./test_ethash_directory/ " , & params , seedhash , & cache , & data , & size )