2021-08-22 04:20:59 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2022-03-30 06:42:26 -06:00
|
|
|
* Copyright (c) 2021-2022 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
|
|
|
|
|
2021-10-22 10:18:38 -06:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
2021-10-29 06:24:05 -06:00
|
|
|
#pragma warning(disable : 4623 5026 5027)
|
2021-10-22 10:18:38 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ROBIN_HOOD_MALLOC(size) p2pool::malloc_hook(size)
|
|
|
|
#define ROBIN_HOOD_CALLOC(count, size) p2pool::calloc_hook((count), (size))
|
|
|
|
#define ROBIN_HOOD_FREE(ptr) p2pool::free_hook(ptr)
|
|
|
|
|
|
|
|
#include "robin_hood.h"
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
namespace p2pool {
|
|
|
|
|
2021-10-02 09:06:48 -06:00
|
|
|
extern const char* VERSION;
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
template<typename T> struct not_implemented { enum { value = 0 }; };
|
|
|
|
|
|
|
|
struct nocopy_nomove
|
|
|
|
{
|
|
|
|
nocopy_nomove() = default;
|
|
|
|
nocopy_nomove(const nocopy_nomove&) = delete;
|
|
|
|
nocopy_nomove(nocopy_nomove&&) = delete;
|
|
|
|
nocopy_nomove& operator=(const nocopy_nomove&) = delete;
|
|
|
|
nocopy_nomove& operator=(nocopy_nomove&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ScopeGuard
|
|
|
|
{
|
|
|
|
explicit FORCEINLINE ScopeGuard(T&& handler) : m_handler(std::move(handler)) {}
|
|
|
|
FORCEINLINE ~ScopeGuard() { m_handler(); }
|
|
|
|
|
|
|
|
T m_handler;
|
|
|
|
|
|
|
|
// Disable copying/moving of ScopeGuard objects
|
|
|
|
|
|
|
|
// We can't declare copy constructor as explicitly deleted because of copy elision semantics
|
|
|
|
// Just leave it without definition and it'll fail when linking if someone tries to copy a ScopeGuard object
|
|
|
|
ScopeGuard(const ScopeGuard&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ScopeGuard& operator=(const ScopeGuard&) = delete;
|
|
|
|
ScopeGuard& operator=(ScopeGuard&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> FORCEINLINE ScopeGuard<T> on_scope_leave(T&& handler) { return ScopeGuard<T>(std::move(handler)); }
|
|
|
|
|
2022-08-23 06:13:09 -06:00
|
|
|
#define ON_SCOPE_LEAVE(...) auto CONCAT(scope_guard_, __LINE__) = on_scope_leave(__VA_ARGS__);
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
struct MinerCallbackHandler
|
|
|
|
{
|
|
|
|
virtual ~MinerCallbackHandler() = 0;
|
|
|
|
|
|
|
|
virtual void handle_tx(TxMempoolData& tx) = 0;
|
|
|
|
virtual void handle_miner_data(MinerData& data) = 0;
|
|
|
|
virtual void handle_chain_main(ChainMain& data, const char* extra) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static FORCEINLINE bool from_hex(char c, T& out_value) {
|
|
|
|
if ('0' <= c && c <= '9') { out_value = static_cast<T>(c - '0'); return true; }
|
|
|
|
if ('a' <= c && c <= 'f') { out_value = static_cast<T>((c - 'a') + 10); return true; }
|
|
|
|
if ('A' <= c && c <= 'F') { out_value = static_cast<T>((c - 'A') + 10); return true; }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, bool is_signed> struct abs_helper {};
|
|
|
|
template<typename T> struct abs_helper<T, false> { static FORCEINLINE T value(T x) { return x; } };
|
|
|
|
template<typename T> struct abs_helper<T, true> { static FORCEINLINE T value(T x) { return (x >= 0) ? x : -x; } };
|
|
|
|
|
|
|
|
template<typename T> FORCEINLINE T abs(T x) { return abs_helper<T, std::is_signed<T>::value>::value(x); }
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
FORCEINLINE void writeVarint(T value, U&& callback)
|
|
|
|
{
|
|
|
|
while (value >= 0x80) {
|
|
|
|
callback(static_cast<uint8_t>((value & 0x7F) | 0x80));
|
|
|
|
value >>= 7;
|
|
|
|
}
|
|
|
|
callback(static_cast<uint8_t>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
FORCEINLINE void writeVarint(T value, std::vector<uint8_t>& out)
|
|
|
|
{
|
|
|
|
writeVarint(value, [&out](uint8_t b) { out.emplace_back(b); });
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:29:12 -06:00
|
|
|
template<typename T>
|
|
|
|
const uint8_t* readVarint(const uint8_t* data, const uint8_t* data_end, T& b)
|
|
|
|
{
|
|
|
|
uint64_t result = 0;
|
|
|
|
int k = 0;
|
|
|
|
|
|
|
|
while (data < data_end) {
|
|
|
|
if (k >= static_cast<int>(sizeof(T)) * 8) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t cur_byte = *(data++);
|
|
|
|
result |= (cur_byte & 0x7F) << k;
|
|
|
|
k += 7;
|
|
|
|
|
|
|
|
if ((cur_byte & 0x80) == 0) {
|
|
|
|
b = result;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:34:37 -06:00
|
|
|
template<typename T>
|
|
|
|
FORCEINLINE T read_unaligned(const T* p)
|
|
|
|
{
|
2022-06-27 08:20:45 -06:00
|
|
|
static_assert(std::is_trivially_copyable<T>::value, "T must be a trivially copyable type");
|
2022-04-08 14:34:37 -06:00
|
|
|
|
|
|
|
T result;
|
|
|
|
memcpy(&result, p, sizeof(T));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
template<typename T, size_t N> FORCEINLINE constexpr size_t array_size(T(&)[N]) { return N; }
|
2021-10-14 08:46:49 -06:00
|
|
|
template<typename T, typename U, size_t N> FORCEINLINE constexpr size_t array_size(T(U::*)[N]) { return N; }
|
2021-08-22 04:20:59 -06:00
|
|
|
|
|
|
|
[[noreturn]] void panic();
|
|
|
|
|
|
|
|
void make_thread_background();
|
|
|
|
|
2021-09-02 04:29:50 -06:00
|
|
|
class BackgroundJobTracker : public nocopy_nomove
|
2021-08-24 03:42:41 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BackgroundJobTracker();
|
|
|
|
~BackgroundJobTracker();
|
|
|
|
|
|
|
|
void start(const char* name);
|
|
|
|
void stop(const char* name);
|
|
|
|
void wait();
|
2021-08-25 03:31:35 -06:00
|
|
|
void print_status();
|
2021-08-24 03:42:41 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
|
|
Impl* m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern BackgroundJobTracker bkg_jobs_tracker;
|
2021-10-31 07:19:49 -06:00
|
|
|
|
|
|
|
void set_main_thread();
|
|
|
|
bool is_main_thread();
|
2021-08-22 04:20:59 -06:00
|
|
|
|
2022-09-04 10:10:14 -06:00
|
|
|
extern bool disable_resolve_host;
|
2021-09-08 12:25:39 -06:00
|
|
|
bool resolve_host(std::string& host, bool& is_v6);
|
|
|
|
|
2021-10-22 10:18:38 -06:00
|
|
|
template <typename Key, typename T>
|
|
|
|
using unordered_map = robin_hood::detail::Table<false, 80, Key, T, robin_hood::hash<Key>, std::equal_to<Key>>;
|
|
|
|
|
|
|
|
template <typename Key>
|
|
|
|
using unordered_set = robin_hood::detail::Table<false, 80, Key, void, robin_hood::hash<Key>, std::equal_to<Key>>;
|
|
|
|
|
2022-03-17 09:14:29 -06:00
|
|
|
// Fills the whole initial MT19937-64 state with non-deterministic random numbers
|
|
|
|
struct RandomDeviceSeed
|
|
|
|
{
|
|
|
|
using result_type = std::random_device::result_type;
|
|
|
|
static_assert(sizeof(result_type) >= 4, "result_type must have at least 32 bits");
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void generate(T begin, T end)
|
|
|
|
{
|
|
|
|
std::random_device rd;
|
|
|
|
for (T i = begin; i != end; ++i) {
|
|
|
|
*i = rd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RandomDeviceSeed instance;
|
|
|
|
};
|
|
|
|
|
2022-03-23 04:30:38 -06:00
|
|
|
FORCEINLINE uint64_t seconds_since_epoch()
|
|
|
|
{
|
|
|
|
using namespace std::chrono;
|
|
|
|
return duration_cast<seconds>(steady_clock::now().time_since_epoch()).count();
|
|
|
|
}
|
|
|
|
|
2022-05-09 03:25:52 -06:00
|
|
|
uint64_t bsr_reference(uint64_t x);
|
|
|
|
|
|
|
|
#ifdef HAVE_BUILTIN_CLZLL
|
|
|
|
#define bsr(x) (63 - __builtin_clzll(x))
|
|
|
|
#elif defined HAVE_BITSCANREVERSE64
|
|
|
|
#pragma intrinsic(_BitScanReverse64)
|
|
|
|
FORCEINLINE uint64_t bsr(uint64_t x)
|
|
|
|
{
|
|
|
|
unsigned long index;
|
|
|
|
_BitScanReverse64(&index, x);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define bsr bsr_reference
|
|
|
|
#endif
|
|
|
|
|
2022-08-31 08:37:33 -06:00
|
|
|
bool str_to_ip(bool is_v6, const char* ip, raw_ip& result);
|
|
|
|
bool is_localhost(const std::string& host);
|
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
} // namespace p2pool
|
|
|
|
|
2022-06-01 06:25:58 -06:00
|
|
|
void memory_tracking_start();
|
|
|
|
void memory_tracking_stop();
|
|
|
|
void p2pool_usage();
|
|
|
|
|
2021-10-22 10:18:38 -06:00
|
|
|
namespace robin_hood {
|
2021-09-09 03:44:40 -06:00
|
|
|
|
2021-08-22 04:20:59 -06:00
|
|
|
template<>
|
2021-09-09 03:44:40 -06:00
|
|
|
struct hash<p2pool::hash>
|
2021-08-22 04:20:59 -06:00
|
|
|
{
|
2021-09-27 05:28:23 -06:00
|
|
|
FORCEINLINE size_t operator()(const p2pool::hash& value) const noexcept
|
2021-08-22 04:20:59 -06:00
|
|
|
{
|
2021-10-22 10:18:38 -06:00
|
|
|
return hash_bytes(value.h, p2pool::HASH_SIZE);
|
2021-08-22 04:20:59 -06:00
|
|
|
}
|
|
|
|
};
|
2021-09-09 03:44:40 -06:00
|
|
|
|
2021-09-14 03:06:49 -06:00
|
|
|
template<size_t N>
|
|
|
|
struct hash<std::array<uint8_t, N>>
|
|
|
|
{
|
2021-09-27 05:28:23 -06:00
|
|
|
FORCEINLINE size_t operator()(const std::array<uint8_t, N>& value) const noexcept
|
2021-09-14 03:06:49 -06:00
|
|
|
{
|
2021-10-22 10:18:38 -06:00
|
|
|
return hash_bytes(value.data(), N);
|
2021-09-14 03:06:49 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-29 06:24:05 -06:00
|
|
|
template<>
|
|
|
|
struct hash<p2pool::raw_ip>
|
|
|
|
{
|
|
|
|
FORCEINLINE size_t operator()(const p2pool::raw_ip& value) const noexcept
|
|
|
|
{
|
|
|
|
return hash_bytes(value.data, sizeof(value.data));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct hash<std::pair<uint64_t, uint64_t>>
|
|
|
|
{
|
|
|
|
FORCEINLINE size_t operator()(const std::pair<uint64_t, uint64_t>& value) const noexcept
|
|
|
|
{
|
|
|
|
static_assert(sizeof(value) == sizeof(uint64_t) * 2, "Invalid std::pair<uint64_t, uint64_t> size");
|
|
|
|
return hash_bytes(&value, sizeof(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-22 10:18:38 -06:00
|
|
|
} // namespace robin_hood
|