p2pool/src/pow_hash.h

131 lines
3.4 KiB
C
Raw Normal View History

2021-08-22 04:20:59 -06:00
/*
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
2023-01-04 05:07:55 -07:00
* Copyright (c) 2021-2023 SChernykh <https://github.com/SChernykh>
2021-08-22 04:20:59 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "uv_util.h"
struct randomx_cache;
struct randomx_dataset;
class randomx_vm;
namespace p2pool {
class p2pool;
2021-11-20 03:51:22 -07:00
class RandomX_Hasher_Base
{
public:
virtual ~RandomX_Hasher_Base() {}
virtual void set_seed_async(const hash&) {}
virtual void set_old_seed(const hash&) {}
2022-01-22 15:09:29 -07:00
virtual randomx_cache* cache() const { return nullptr; }
virtual randomx_dataset* dataset() const { return nullptr; }
virtual uint32_t seed_counter() const { return 0; }
virtual void sync_wait() {}
2023-03-28 02:14:47 -06:00
virtual bool calculate(const void* data, size_t size, uint64_t height, const hash& seed, hash& result, bool force_light_mode) = 0;
2021-11-20 03:51:22 -07:00
};
#ifdef WITH_RANDOMX
2021-11-20 03:51:22 -07:00
class RandomX_Hasher : public RandomX_Hasher_Base
2021-08-22 04:20:59 -06:00
{
public:
explicit RandomX_Hasher(p2pool* pool);
~RandomX_Hasher();
2021-11-20 03:51:22 -07:00
void set_seed_async(const hash& seed) override;
2021-09-06 07:49:39 -06:00
void set_seed(const hash& seed);
2021-11-20 03:51:22 -07:00
void set_old_seed(const hash& seed) override;
2021-08-22 04:20:59 -06:00
2022-01-22 15:09:29 -07:00
randomx_cache* cache() const override { return m_cache[m_index]; }
randomx_dataset* dataset() const override { return m_dataset; }
uint32_t seed_counter() const override { return m_seedCounter.load(); }
void sync_wait() override;
2023-03-28 02:14:47 -06:00
bool calculate(const void* data, size_t size, uint64_t height, const hash& seed, hash& result, bool force_light_mode) override;
2021-08-22 04:20:59 -06:00
private:
struct ThreadSafeVM
{
2021-10-27 07:16:25 -06:00
uv_mutex_t mutex{};
randomx_vm* vm = nullptr;
2021-08-22 04:20:59 -06:00
};
p2pool* m_pool;
std::atomic<int> m_stopped{ 0 };
uv_rwlock_t m_cacheLock;
randomx_cache* m_cache[2];
uv_rwlock_t m_datasetLock;
randomx_dataset* m_dataset;
// 0: light VM for the current seed
// 1: light VM for the previous seed
// 2: full dataset VM for the current seed
enum { FULL_DATASET_VM = 2 };
2021-10-27 07:16:25 -06:00
ThreadSafeVM m_vm[3]{};
2021-08-22 04:20:59 -06:00
hash m_seed[2];
uint32_t m_index;
2022-01-22 15:09:29 -07:00
std::atomic<uint32_t> m_seedCounter;
std::atomic<uint32_t> m_oldSeedCounter;
2021-08-22 04:20:59 -06:00
};
#endif
2021-08-22 04:20:59 -06:00
2021-11-20 03:51:22 -07:00
class RandomX_Hasher_RPC : public RandomX_Hasher_Base
{
public:
explicit RandomX_Hasher_RPC(p2pool* pool);
~RandomX_Hasher_RPC();
2023-03-28 02:14:47 -06:00
bool calculate(const void* data_ptr, size_t size, uint64_t height, const hash& seed, hash& h, bool force_light_mode) override;
2021-11-20 03:51:22 -07:00
private:
static void loop(void* data);
p2pool* m_pool;
uv_mutex_t m_requestMutex;
uv_loop_t m_loop;
uv_thread_t m_loopThread;
uv_mutex_t m_condMutex;
uv_cond_t m_cond;
uv_async_t m_shutdownAsync;
uv_async_t m_kickTheLoopAsync;
static void on_shutdown(uv_async_t* async)
{
RandomX_Hasher_RPC* server = reinterpret_cast<RandomX_Hasher_RPC*>(async->data);
uv_close(reinterpret_cast<uv_handle_t*>(&server->m_shutdownAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&server->m_kickTheLoopAsync), nullptr);
2022-06-04 05:16:05 -06:00
delete GetLoopUserData(&server->m_loop, false);
2021-11-20 03:51:22 -07:00
}
};
2021-08-22 04:20:59 -06:00
} // namespace p2pool