From 6a99c22cbca869558dcb2c5d5dd92cef67ce5665 Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Wed, 21 Jan 2026 00:44:49 -0600 Subject: [PATCH] rpc: return error gracefully on cryptonight v1 check --- .../cryptonote_format_utils.cpp | 4 ++- src/cryptonote_config.h | 1 + src/rpc/core_rpc_server.cpp | 28 +++++++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index 0fa5161ef..f560fe77a 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -1622,7 +1622,9 @@ namespace cryptonote } else // CryptoNight { - const int pow_variant = major_version >= 7 ? major_version - 6 : 0; + static_assert(HF_VERSION_CRYPTONIGHT_VARIANT_1 >= 1); + const int pow_variant = major_version >= HF_VERSION_CRYPTONIGHT_VARIANT_1 + ? major_version - (HF_VERSION_CRYPTONIGHT_VARIANT_1 - 1) : 0; crypto::cn_slow_hash(block_hashing_blob.data(), block_hashing_blob.size(), res, pow_variant, height); } diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index 19b6f9ab8..31d3fd30f 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -174,6 +174,7 @@ #define HF_VERSION_DYNAMIC_FEE 4 #define HF_VERSION_MIN_MIXIN_4 6 #define HF_VERSION_MIN_MIXIN_6 7 +#define HF_VERSION_CRYPTONIGHT_VARIANT_1 7 #define HF_VERSION_MIN_MIXIN_10 8 #define HF_VERSION_MIN_MIXIN_15 15 #define HF_VERSION_ENFORCE_RCT 6 diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index c1b5199f5..5b680b523 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -2081,6 +2081,20 @@ namespace cryptonote error_resp.message = "Wrong block blob"; return false; } + if (req.major_version == HF_VERSION_CRYPTONIGHT_VARIANT_1 && req.major_version < RX_BLOCK_VERSION) + { + try + { + crypto::cn_variant1_check(blockblob.size(), /*variant=*/1); + } + catch(const std::exception& e) + { + error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE; + error_resp.message = "Block blob size is too small for CryptoNight v1, rejecting block"; + return false; + } + } + if(!m_core.check_incoming_block_size(blockblob)) { error_resp.code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE; @@ -2101,8 +2115,18 @@ namespace cryptonote buf.copy(reinterpret_cast(&seed_hash), sizeof(crypto::hash)); } - cryptonote::get_block_longhash(&(m_core.get_blockchain_storage()), blockblob, pow_hash, req.height, - req.major_version, req.seed_hash.size() ? &seed_hash : NULL, 0); + try + { + cryptonote::get_block_longhash(&(m_core.get_blockchain_storage()), blockblob, pow_hash, req.height, + req.major_version, req.seed_hash.size() ? &seed_hash : NULL, 0); + } + catch (const std::exception &e) + { + MERROR("Caught unexpected error while hashing in " << __PRETTY_FUNCTION__ << ": " << e.what()); + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = e.what(); + return false; + } res = string_tools::pod_to_hex(pow_hash); return true; }