diff --git a/src/log.h b/src/log.h index 0f55d61..c414216 100644 --- a/src/log.h +++ b/src/log.h @@ -288,7 +288,7 @@ template<> struct log::Stream::Entry struct hex_buf { - FORCEINLINE hex_buf(const uint8_t* data, size_t size) : m_data(data), m_size(size) {} + FORCEINLINE hex_buf(const void* data, size_t size) : m_data(reinterpret_cast(data)), m_size(size) {} template explicit FORCEINLINE hex_buf(const T* data) : m_data(reinterpret_cast(data)), m_size(sizeof(T)) {} diff --git a/src/miner.cpp b/src/miner.cpp index 5afc9ee..5487351 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -224,7 +224,16 @@ void Miner::run(WorkerData* data) job[index].set_nonce(static_cast(full_nonce), static_cast(full_nonce >> 32)); hash h; - randomx_calculate_hash_next(vm, job[index].m_blob, job[index].m_blobSize, &h); + try { + randomx_calculate_hash_next(vm, job[index].m_blob, job[index].m_blobSize, &h); + } + catch (const std::exception& e) { + LOGERR(0, "Failed to calculate RandomX hash: exception \"" << e.what() << "\". Is your CPU/RAM unstable?" << + "\nFailed RandomX hash input: " << log::hex_buf(j.m_blob, j.m_blobSize)); + + // Make the result hash all FF's to fail difficulty checks + memset(h.h, -1, HASH_SIZE); + } if (j.m_diff.check_pow(h)) { LOGINFO(0, log::Green() << "worker thread " << data->m_index << '/' << data->m_count << " found a mainchain block at height " << j.m_height << ", submitting it"); diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 5419e85..84a591f 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -1691,8 +1691,7 @@ int p2pool::run() m_ZMQReader = nullptr; } catch (const std::exception& e) { - const char* s = e.what(); - LOGERR(1, "exception " << s); + LOGERR(1, "exception " << e.what()); PANIC_STOP(); } diff --git a/src/pool_block_parser.inl b/src/pool_block_parser.inl index 6deb041..259ee02 100644 --- a/src/pool_block_parser.inl +++ b/src/pool_block_parser.inl @@ -393,8 +393,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si } } catch (std::exception& e) { - const char* msg = e.what(); - LOGERR(0, "Exception in PoolBlock::deserialize(): " << (msg ? msg : "unknown exception")); + LOGERR(0, "Exception in PoolBlock::deserialize(): " << e.what()); return __LINE__; } diff --git a/src/pow_hash.cpp b/src/pow_hash.cpp index 39922d9..c3ec08e 100644 --- a/src/pow_hash.cpp +++ b/src/pow_hash.cpp @@ -322,6 +322,22 @@ void RandomX_Hasher::sync_wait() ReadLock lock2(m_cacheLock); } +static bool randomx_calculate_hash_safe(randomx_vm* machine, const void* input, size_t inputSize, void* output) +{ + // Try to calculate the hash again if something went wrong the first time (for example, because of an unstable CPU) + for (size_t i = 0; i < 2; ++i) { + try { + randomx_calculate_hash(machine, input, inputSize, output); + return true; + } + catch (const std::exception& e) { + LOGERR(0, "Failed to calculate RandomX hash: exception \"" << e.what() << "\". Is your CPU/RAM unstable?" << + "\nFailed RandomX hash input: " << log::hex_buf(input, inputSize)); + } + } + return false; +} + bool RandomX_Hasher::calculate(const void* data, size_t size, uint64_t /*height*/, const hash& seed, hash& result, bool force_light_mode) { // First try to use the dataset if it's ready @@ -335,8 +351,7 @@ bool RandomX_Hasher::calculate(const void* data, size_t size, uint64_t /*height* MutexLock lock(m_vm[FULL_DATASET_VM].mutex); if (m_vm[FULL_DATASET_VM].vm && (seed == m_seed[m_index])) { - randomx_calculate_hash(m_vm[FULL_DATASET_VM].vm, data, size, &result); - return true; + return randomx_calculate_hash_safe(m_vm[FULL_DATASET_VM].vm, data, size, &result); } } @@ -350,8 +365,7 @@ bool RandomX_Hasher::calculate(const void* data, size_t size, uint64_t /*height* { MutexLock lock2(m_vm[m_index].mutex); if (m_vm[m_index].vm && (seed == m_seed[m_index])) { - randomx_calculate_hash(m_vm[m_index].vm, data, size, &result); - return true; + return randomx_calculate_hash_safe(m_vm[m_index].vm, data, size, &result); } } @@ -360,8 +374,7 @@ bool RandomX_Hasher::calculate(const void* data, size_t size, uint64_t /*height* MutexLock lock2(m_vm[prev_index].mutex); if (m_vm[prev_index].vm && (seed == m_seed[prev_index])) { - randomx_calculate_hash(m_vm[prev_index].vm, data, size, &result); - return true; + return randomx_calculate_hash_safe(m_vm[prev_index].vm, data, size, &result); } return false;