diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index d3c7dba48..2e39534fa 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -1605,6 +1605,31 @@ namespace cryptonote return get_tx_tree_hash(txs_ids); } //--------------------------------------------------------------- + crypto::hash get_block_longhash(const blobdata_ref block_hashing_blob, + const uint64_t height, + const uint8_t major_version, + const crypto::hash &seed_hash) + { + crypto::hash res; + + if (height == 202612) // block 202612 bug workaround + { + static const std::string longhash_202612 = "84f64766475d51837ac9efbef1926486e58563c95a19fef4aec3254f03000000"; + epee::string_tools::hex_to_pod(longhash_202612, res); + } + else if (major_version >= RX_BLOCK_VERSION) // RandomX + { + crypto::rx_slow_hash(seed_hash.data, block_hashing_blob.data(), block_hashing_blob.size(), res.data); + } + else // CryptoNight + { + const int pow_variant = major_version >= 7 ? major_version - 6 : 0; + crypto::cn_slow_hash(block_hashing_blob.data(), block_hashing_blob.size(), res, pow_variant, height); + } + + return res; + } + //--------------------------------------------------------------- bool is_valid_decomposed_amount(uint64_t amount) { if (0 == amount) diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h index 9c29790e9..64383ca94 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.h +++ b/src/cryptonote_basic/cryptonote_format_utils.h @@ -261,6 +261,10 @@ namespace cryptonote void get_tx_tree_hash(const std::vector& tx_hashes, crypto::hash& h); crypto::hash get_tx_tree_hash(const std::vector& tx_hashes); crypto::hash get_tx_tree_hash(const block& b); + crypto::hash get_block_longhash(const blobdata_ref block_hashing_blob, + const uint64_t height, + const uint8_t major_version, + const crypto::hash &seed_hash); bool is_valid_decomposed_amount(uint64_t amount); void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t & block_hashes_cached); diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp index f8daddbc6..e9a66ac50 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.cpp +++ b/src/cryptonote_core/cryptonote_tx_utils.cpp @@ -678,29 +678,13 @@ namespace cryptonote bool get_block_longhash(const Blockchain *pbc, const blobdata& bd, crypto::hash& res, const uint64_t height, const int major_version, const crypto::hash *seed_hash, const int miners) { - // block 202612 bug workaround - if (height == 202612) + crypto::hash seed_hash_ = crypto::null_hash; + if (pbc != NULL && major_version >= RX_BLOCK_VERSION) { - static const std::string longhash_202612 = "84f64766475d51837ac9efbef1926486e58563c95a19fef4aec3254f03000000"; - epee::string_tools::hex_to_pod(longhash_202612, res); - return true; - } - if (major_version >= RX_BLOCK_VERSION) - { - crypto::hash hash; - if (pbc != NULL) - { - const uint64_t seed_height = rx_seedheight(height); - hash = seed_hash ? *seed_hash : pbc->get_pending_block_id_by_height(seed_height); - } else - { - memset(&hash, 0, sizeof(hash)); // only happens when generating genesis block - } - rx_slow_hash(hash.data, bd.data(), bd.size(), res.data); - } else { - const int pow_variant = major_version >= 7 ? major_version - 6 : 0; - crypto::cn_slow_hash(bd.data(), bd.size(), res, pow_variant, height); + const uint64_t seed_height = rx_seedheight(height); + seed_hash_ = seed_hash ? *seed_hash : pbc->get_pending_block_id_by_height(seed_height); } + res = get_block_longhash(bd, height, major_version, seed_hash_); return true; }