Compare commits

...

22 Commits

Author SHA1 Message Date
SChernykh 8aebcdc580 p2pool v2.0 2022-04-30 17:50:27 +02:00
SChernykh e23a2e77a8 Updated dependencies 2022-04-30 12:10:09 +02:00
SChernykh 0f9362e14e More tests for derive_view_tag 2022-04-29 22:25:22 +02:00
SChernykh ab974b9684 Monero network upgrade information 2022-04-29 14:35:06 +02:00
SChernykh 54acfee036 Merge pull request #144 from SChernykh/view_tags
v15 hardfork changes
2022-04-27 19:56:42 +02:00
SChernykh a78ddb50d5 Fixed logger thread timing logic 2022-04-24 16:22:22 +02:00
SChernykh b2f86a5f6b Ban peers with stuck or lagging monerod 2022-04-23 11:29:17 +02:00
SChernykh 2a77695832 Fixed data races in miner 2022-04-09 18:25:12 +02:00
SChernykh 21d8cb917b Fixed data race in logger 2022-04-09 10:41:20 +02:00
SChernykh 2c70bf8616 View tags support
See http://github.com/monero-project/monero/pull/8061
2022-04-09 00:56:42 +02:00
SChernykh 7522111bb7 Fixed data races 2022-04-09 00:38:15 +02:00
SChernykh 7b5b910f3f Undefined behaviour fixes 2022-04-08 22:49:08 +02:00
SChernykh afb52e4235 Check return value (Coverity fix) 2022-04-07 19:33:39 +02:00
SChernykh 93cd9a659a Code cleanup 2022-04-07 19:11:20 +02:00
SChernykh 9e287ab4f0 Logger fixes
- Fixed a race condition when stopping the logger
- Flush log file only once per batch of log lines
2022-04-07 16:55:35 +02:00
SChernykh 094209f311 P2PServer: optimized update_peer_connections() 2022-04-07 16:48:01 +02:00
SChernykh dee66c3f32 Added varint unit tests 2022-04-06 16:34:16 +02:00
SChernykh 32533e3c96 FreeBSD build 2022-04-05 19:36:37 +02:00
SChernykh a073923a82 Updated dependencies 2022-04-05 11:32:39 +02:00
SChernykh 3484b24caa Updated dependencies 2022-04-04 19:56:42 +02:00
SChernykh 1b3c1eb850 P2PServer: increased peer timeout to 15 minutes 2022-03-30 22:47:10 +02:00
SChernykh 7fe032915f Ignore SIGPIPE 2022-03-30 21:53:52 +02:00
42 changed files with 687 additions and 228 deletions
+5 -1
View File
@@ -97,6 +97,8 @@ include_directories(external/src/robin-hood-hashing/src/include)
if (WIN32)
set(LIBS ${LIBS} ws2_32 iphlpapi userenv psapi)
elseif (CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
set(LIBS ${LIBS} pthread)
elseif (NOT APPLE)
set(LIBS ${LIBS} pthread gss dl)
endif()
@@ -140,7 +142,9 @@ if (STATIC_BINARY)
set(STATIC_LIBS randomx)
endif()
if (NOT APPLE)
if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
set(STATIC_LIBS ${STATIC_LIBS} pthread)
elseif (NOT APPLE)
set(STATIC_LIBS ${STATIC_LIBS} pthread dl)
endif()
+29 -7
View File
@@ -6,10 +6,10 @@ Pool status and monitoring pages can be found at https://p2pool.io/, https://p2p
### Build Status
![C/C++ CI](https://github.com/SChernykh/p2pool/actions/workflows/c-cpp.yml/badge.svg)
![CodeQL](https://github.com/SChernykh/p2pool/actions/workflows/codeql-analysis.yml/badge.svg)
![msvc-analysis](https://github.com/SChernykh/p2pool/actions/workflows/msvc-analysis.yml/badge.svg)
![cppcheck](https://github.com/SChernykh/p2pool/actions/workflows/cppcheck.yml/badge.svg)
![C/C++ CI](https://github.com/SChernykh/p2pool/actions/workflows/c-cpp.yml/badge.svg)
![CodeQL](https://github.com/SChernykh/p2pool/actions/workflows/codeql-analysis.yml/badge.svg)
![msvc-analysis](https://github.com/SChernykh/p2pool/actions/workflows/msvc-analysis.yml/badge.svg)
![cppcheck](https://github.com/SChernykh/p2pool/actions/workflows/cppcheck.yml/badge.svg)
<a href="https://scan.coverity.com/projects/schernykh-p2pool">
<img alt="Coverity Scan Build Status"
src="https://scan.coverity.com/projects/23659/badge.svg"/>
@@ -19,6 +19,7 @@ Pool status and monitoring pages can be found at https://p2pool.io/, https://p2p
- [Features](#features)
- [How PPLNS works in P2Pool](#how-pplns-works-in-p2pool)
- [Default P2Pool parameters](#default-p2pool-parameters)
- [Monero version support](#monero-version-support)
- [How to mine on P2Pool](#how-to-mine-on-p2pool)
- [General Considerations](#general-considerations)
- [GNU/Linux](#gnulinux)
@@ -63,12 +64,21 @@ First you need to find a pool share. This share will stay in PPLNS window for 21
* PPLNS window: 2160 blocks (6 hours)
* Minimum payout = Monero block reward/2160, ~0.0003 XMR
## Monero version support
Monero will undergo a network upgrade on July 16th, 2022 (block 2,668,888). In order to continue mining after that date, you must update both Monero and P2Pool software to the latest available versions as soon as they are released.
|Monero protocol version|Required Monero software version|Required P2Pool version
|-|-|-|
|v14 (active until July 16th, 2022)|v0.17.3.0 or newer|v1.0 or newer
|v15, v16 (active after July 16th, 2022)|v0.18.0.0 or newer|v2.0 or newer
## How to mine on P2Pool
### General Considerations
- In order to mine on P2Pool, a synced Monero node using monerod v0.17.3.0 or newer is required. If you do not currently have one configured, you can find instructions to do so [here](https://sethforprivacy.com/guides/run-a-monero-node-advanced/).
- It is highly recommended that you create a separate restricted user account for mining. P2Pool is still relatively new and may still have serious bugs/vulnerabilities.
- It is highly recommended that you create a separate restricted user account for mining. While P2Pool has been battle-tested for a long time now, any software may have unknown bugs/vulnerabilities.
- You have to use a primary wallet address for mining. Subaddresses and integrated addresses are not supported, just like with monerod solo mining.
- Starting from P2Pool v1.7, you can add the `--mini` parameter to your P2Pool command to connect to the **p2pool-mini** sidechain. Note that it will also change the default p2p port from 37889 to 37888.
- Check that ports 18080 (Monero p2p port) and 37889/37888 (P2Pool/P2Pool mini p2p port) are open in your firewall to ensure better connectivity. If you're mining from a computer behind NAT (like a router) you could consider forwarding the ports to your local machine.
@@ -188,7 +198,7 @@ Please see the relevant instructions for your platform:
### Ubuntu 20.04
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on Ubuntu 20.04:
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on Ubuntu 20.04:
```
sudo apt update && sudo apt install git build-essential cmake libuv1-dev libzmq3-dev libsodium-dev libpgm-dev libnorm-dev libgss-dev
git clone --recursive https://github.com/SChernykh/p2pool
@@ -239,7 +249,7 @@ Alternatively, you can select "Clone a repository" within the GUI, then select "
### macOS
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on your Mac:
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on your Mac:
```
brew update && brew install git cmake libuv zmq libpgm
git clone --recursive https://github.com/SChernykh/p2pool
@@ -249,6 +259,18 @@ cmake ..
make -j$(sysctl -n hw.logicalcpu)
```
### FreeBSD
Run the following commands to install the necessary prerequisites, clone this repo, and build P2Pool locally on FreeBSD:
```
pkg install git cmake libuv libzmq4
git clone --recursive https://github.com/SChernykh/p2pool
cd p2pool
mkdir build && cd build
cmake ..
make
```
## Donations
If you'd like to support further development of Monero P2Pool, you're welcome to send any amount of XMR to the following address:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+44 -8
View File
@@ -3831,15 +3831,51 @@ int sc_isnonzero(const unsigned char *s) {
s[27] | s[28] | s[29] | s[30] | s[31]) - 1) >> 8) + 1;
}
int ge_p3_is_point_at_infinity(const ge_p3 *p) {
// X = 0 and Y == Z
int n;
for (n = 0; n < 10; ++n)
int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p) {
// https://eprint.iacr.org/2008/522
// X == T == 0 and Y/Z == 1
// note: convert all pieces to canonical bytes in case rounding is required (i.e. an element is > q)
// note2: even though T = XY/Z is true for valid point representations (implying it isn't necessary to
// test T == 0), the input to this function might NOT be valid, so we must test T == 0
char result_X_bytes[32];
fe_tobytes((unsigned char*)&result_X_bytes, p->X);
// X != 0
for (int i = 0; i < 32; ++i)
{
if (p->X[n] | p->T[n])
return 0;
if (p->Y[n] != p->Z[n])
if (result_X_bytes[i])
return 0;
}
return 1;
char result_T_bytes[32];
fe_tobytes((unsigned char*)&result_T_bytes, p->T);
// T != 0
for (int i = 0; i < 32; ++i)
{
if (result_T_bytes[i])
return 0;
}
char result_Y_bytes[32];
char result_Z_bytes[32];
fe_tobytes((unsigned char*)&result_Y_bytes, p->Y);
fe_tobytes((unsigned char*)&result_Z_bytes, p->Z);
// Y != Z
for (int i = 0; i < 32; ++i)
{
if (result_Y_bytes[i] != result_Z_bytes[i])
return 0;
}
// is Y nonzero? then Y/Z == 1
for (int i = 0; i < 32; ++i)
{
if (result_Y_bytes[i] != 0)
return 1;
}
// Y/Z = 0/0
return 0;
}
+1 -1
View File
@@ -162,4 +162,4 @@ void fe_add(fe h, const fe f, const fe g);
void fe_tobytes(unsigned char *, const fe);
void fe_invert(fe out, const fe z);
int ge_p3_is_point_at_infinity(const ge_p3 *p);
int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p);
+11 -3
View File
@@ -689,6 +689,8 @@ int BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Mine
m_poolBlockTemplate->m_outputs.clear();
m_poolBlockTemplate->m_outputs.reserve(num_outputs);
const uint8_t tx_type = m_poolBlockTemplate->get_tx_type();
uint64_t reward_amounts_weight = 0;
for (size_t i = 0; i < num_outputs; ++i) {
writeVarint(m_rewards[i], [this, &reward_amounts_weight](uint8_t b)
@@ -696,18 +698,24 @@ int BlockTemplate::create_miner_tx(const MinerData& data, const std::vector<Mine
m_minerTx.push_back(b);
++reward_amounts_weight;
});
m_minerTx.push_back(TXOUT_TO_KEY);
m_minerTx.push_back(tx_type);
uint8_t view_tag = 0;
if (dry_run) {
m_minerTx.insert(m_minerTx.end(), HASH_SIZE, 0);
}
else {
hash eph_public_key;
if (!shares[i].m_wallet->get_eph_public_key(m_txkeySec, i, eph_public_key)) {
if (!shares[i].m_wallet->get_eph_public_key(m_txkeySec, i, eph_public_key, view_tag)) {
LOGERR(1, "get_eph_public_key failed at index " << i);
}
m_minerTx.insert(m_minerTx.end(), eph_public_key.h, eph_public_key.h + HASH_SIZE);
m_poolBlockTemplate->m_outputs.emplace_back(m_rewards[i], eph_public_key);
m_poolBlockTemplate->m_outputs.emplace_back(m_rewards[i], eph_public_key, tx_type, view_tag);
}
if (tx_type == TXOUT_TO_TAGGED_KEY) {
m_minerTx.emplace_back(view_tag);
}
}
+3 -1
View File
@@ -81,7 +81,8 @@
namespace p2pool {
constexpr size_t HASH_SIZE = 32;
constexpr uint8_t HARDFORK_SUPPORTED_VERSION = 14;
constexpr uint8_t HARDFORK_VIEW_TAGS_VERSION = 15;
constexpr uint8_t HARDFORK_SUPPORTED_VERSION = 16;
constexpr uint8_t MINER_REWARD_UNLOCK_TIME = 60;
constexpr uint8_t NONCE_SIZE = 4;
constexpr uint8_t EXTRA_NONCE_SIZE = 4;
@@ -89,6 +90,7 @@ constexpr uint8_t EXTRA_NONCE_MAX_SIZE = EXTRA_NONCE_SIZE + 10;
constexpr uint8_t TX_VERSION = 2;
constexpr uint8_t TXIN_GEN = 0xFF;
constexpr uint8_t TXOUT_TO_KEY = 2;
constexpr uint8_t TXOUT_TO_TAGGED_KEY = 3;
constexpr uint8_t TX_EXTRA_TAG_PUBKEY = 1;
constexpr uint8_t TX_EXTRA_NONCE = 2;
constexpr uint8_t TX_EXTRA_MERGE_MINING_TAG = 3;
+1 -3
View File
@@ -115,9 +115,7 @@ static int do_status(p2pool *m_pool, const char * /* args */)
m_pool->p2p_server()->print_status();
}
#ifdef WITH_RANDOMX
if (m_pool->miner()) {
m_pool->miner()->print_status();
}
m_pool->print_miner_status();
#endif
bkg_jobs_tracker.print_status();
return 0;
+39 -18
View File
@@ -109,9 +109,9 @@ bool check_keys(const hash& pub, const hash& sec)
return pub == pub_check;
}
static FORCEINLINE void hash_to_scalar(const uint8_t* data, size_t length, uint8_t(&res)[HASH_SIZE])
static FORCEINLINE void hash_to_scalar(const uint8_t* data, int length, uint8_t (&res)[HASH_SIZE])
{
keccak(data, static_cast<int>(length), res, HASH_SIZE);
keccak(data, length, res, HASH_SIZE);
sc_reduce32(res);
}
@@ -122,18 +122,13 @@ static FORCEINLINE void derivation_to_scalar(const hash& derivation, size_t outp
uint8_t output_index[(sizeof(size_t) * 8 + 6) / 7];
} buf;
uint8_t* begin = buf.derivation;
uint8_t* end = buf.output_index;
memcpy(buf.derivation, derivation.h, sizeof(buf.derivation));
size_t k = output_index;
while (k >= 0x80) {
*(end++) = (static_cast<uint8_t>(k) & 0x7F) | 0x80;
k >>= 7;
}
*(end++) = static_cast<uint8_t>(k);
uint8_t* p = buf.output_index;
writeVarint(output_index, [&p](uint8_t b) { *(p++) = b; });
hash_to_scalar(begin, end - begin, res);
const uint8_t* data = buf.derivation;
hash_to_scalar(data, static_cast<int>(p - data), res);
}
class Cache
@@ -149,17 +144,19 @@ public:
uv_mutex_destroy(&m);
}
bool get_derivation(const hash& key1, const hash& key2, hash& derivation)
bool get_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag)
{
std::array<uint8_t, HASH_SIZE * 2> index;
std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)> index;
memcpy(index.data(), key1.h, HASH_SIZE);
memcpy(index.data() + HASH_SIZE, key2.h, HASH_SIZE);
memcpy(index.data() + HASH_SIZE * 2, &output_index, sizeof(size_t));
{
MutexLock lock(m);
auto it = derivations.find(index);
if (it != derivations.end()) {
derivation = it->second;
derivation = it->second.derivation;
view_tag = it->second.view_tag;
return true;
}
}
@@ -177,9 +174,11 @@ public:
ge_p1p1_to_p2(&point2, &point3);
ge_tobytes(reinterpret_cast<uint8_t*>(&derivation), &point2);
derive_view_tag(derivation, output_index, view_tag);
{
MutexLock lock(m);
derivations.emplace(index, derivation);
derivations.emplace(index, DerivationEntry{ derivation, view_tag } );
}
return true;
@@ -236,16 +235,22 @@ public:
}
private:
struct DerivationEntry
{
hash derivation;
uint8_t view_tag;
};
uv_mutex_t m;
unordered_map<std::array<uint8_t, HASH_SIZE * 2>, hash> derivations;
unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, DerivationEntry> derivations;
unordered_map<std::array<uint8_t, HASH_SIZE * 2 + sizeof(size_t)>, hash> public_keys;
};
static Cache* cache = nullptr;
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation)
bool generate_key_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag)
{
return cache->get_derivation(key1, key2, derivation);
return cache->get_derivation(key1, key2, output_index, derivation, view_tag);
}
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key)
@@ -253,6 +258,22 @@ bool derive_public_key(const hash& derivation, size_t output_index, const hash&
return cache->get_public_key(derivation, output_index, base, derived_key);
}
void derive_view_tag(const hash& derivation, size_t output_index, uint8_t& view_tag)
{
constexpr uint8_t salt[] = "view_tag";
constexpr size_t SALT_SIZE = sizeof(salt) - 1;
uint8_t buf[64];
memcpy(buf, salt, SALT_SIZE);
memcpy(buf + SALT_SIZE, derivation.h, HASH_SIZE);
uint8_t* p = buf + SALT_SIZE + HASH_SIZE;
writeVarint(output_index, [&p](uint8_t b) { *(p++) = b; });
hash view_tag_full;
keccak(buf, static_cast<int>(p - buf), view_tag_full.h, HASH_SIZE);
view_tag = view_tag_full.h[0];
}
void init_crypto_cache()
{
if (!cache) {
+2 -1
View File
@@ -21,8 +21,9 @@ namespace p2pool {
void generate_keys(hash& pub, hash& sec);
bool check_keys(const hash& pub, const hash& sec);
bool generate_key_derivation(const hash& key1, const hash& key2, hash& derivation);
bool generate_key_derivation(const hash& key1, const hash& key2, size_t output_index, hash& derivation, uint8_t& view_tag);
bool derive_public_key(const hash& derivation, size_t output_index, const hash& base, hash& derived_key);
void derive_view_tag(const hash& derivation, size_t output_index, uint8_t& view_tag);
void init_crypto_cache();
void destroy_crypto_cache();
+1 -1
View File
@@ -159,7 +159,7 @@ NOINLINE void keccak(const uint8_t* in, int inlen, uint8_t* md, int mdlen)
for (; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
for (int i = 0; i < rsizw; i++) {
st[i] ^= ((uint64_t*)in)[i];
st[i] ^= read_unaligned(reinterpret_cast<const uint64_t*>(in) + i);
}
keccakf(st);
}
+25 -10
View File
@@ -19,6 +19,7 @@
#include "uv_util.h"
#include <ctime>
#include <fstream>
#include <thread>
static constexpr char log_category_prefix[] = "Log ";
static constexpr char log_file_name[] = "p2pool.log";
@@ -32,7 +33,7 @@ bool CONSOLE_COLORS = true;
#ifndef P2POOL_LOG_DISABLE
static volatile bool stopped = false;
static std::atomic<bool> stopped{ false };
static volatile bool worker_started = false;
#ifdef _WIN32
@@ -47,7 +48,7 @@ public:
enum params : int
{
SLOT_SIZE = 1024,
BUF_SIZE = SLOT_SIZE * 16384,
BUF_SIZE = SLOT_SIZE * 8192,
};
FORCEINLINE Worker()
@@ -72,7 +73,9 @@ public:
abort();
}
do {} while (!worker_started);
while (!worker_started) {
std::this_thread::yield();
}
#ifdef _WIN32
DWORD dwConsoleMode;
@@ -102,11 +105,10 @@ public:
FORCEINLINE void stop()
{
if (stopped) {
if (stopped.exchange(true)) {
return;
}
stopped = true;
LOGINFO(0, "stopped");
uv_thread_join(&m_worker);
uv_cond_destroy(&m_cond);
@@ -131,6 +133,7 @@ public:
memcpy(p + 1, buf + 1, size - 1);
// Ensure memory order in the writer thread
std::atomic_thread_fence(std::memory_order_seq_cst);
// Mark that everything is written into this log slot
@@ -149,18 +152,26 @@ private:
do {
uv_mutex_lock(&m_mutex);
uv_cond_wait(&m_cond, &m_mutex);
if (m_readPos == m_writePos.load()) {
// Nothing to do, wait for the signal
uv_cond_wait(&m_cond, &m_mutex);
}
uv_mutex_unlock(&m_mutex);
for (uint32_t writePos = m_writePos.load(); m_readPos < writePos; writePos = m_writePos.load()) {
for (uint32_t writePos = m_writePos.load(); m_readPos != writePos; writePos = m_writePos.load()) {
// We have at least one log slot pending, possibly more than one
// Process everything in a loop before reading m_writePos again
do {
char* p = m_buf.data() + (m_readPos % BUF_SIZE);
// Wait until everything is written into this log slot
volatile char& severity = *p;
while (!severity) {}
volatile char& severity = p[0];
while (!severity) {
std::this_thread::yield();
}
// Ensure memory order in the reader thread
std::atomic_thread_fence(std::memory_order_seq_cst);
uint32_t size = static_cast<uint8_t>(p[2]);
size = (size << 8) + static_cast<uint8_t>(p[1]);
@@ -208,7 +219,6 @@ private:
}
m_logFile.write(p, size);
m_logFile.flush();
}
}
@@ -218,6 +228,11 @@ private:
m_readPos += SLOT_SIZE;
} while (m_readPos < writePos);
}
// Flush the log file only after all pending log lines have been written
if (m_logFile.is_open()) {
m_logFile.flush();
}
} while (!stopped);
}
+2 -2
View File
@@ -35,7 +35,7 @@ namespace p2pool {
Miner::Miner(p2pool* pool, uint32_t threads)
: m_pool(pool)
, m_threads(threads)
, m_stopped(false)
, m_stopped{ false }
, m_startTimestamp(high_resolution_clock::now())
, m_nonce(0)
, m_nonceTimestamp(m_startTimestamp)
@@ -43,7 +43,7 @@ Miner::Miner(p2pool* pool, uint32_t threads)
, m_totalHashes(0)
, m_sharesFound(0)
, m_job{}
, m_jobIndex(0)
, m_jobIndex{ 0 }
{
on_block(m_pool->block_template());
+2 -2
View File
@@ -49,7 +49,7 @@ private:
};
std::vector<WorkerData*> m_minerThreads;
volatile bool m_stopped;
std::atomic<bool> m_stopped;
std::chrono::high_resolution_clock::time_point m_startTimestamp;
@@ -75,7 +75,7 @@ private:
void set_nonce(uint32_t nonce, uint32_t extra_nonce);
};
Job m_job[2];
volatile uint32_t m_jobIndex;
std::atomic<uint32_t> m_jobIndex;
void run(WorkerData* data);
};
+54 -34
View File
@@ -60,8 +60,10 @@ P2PServer::P2PServer(p2pool* pool)
m_peerId = m_rng();
set_max_outgoing_peers(pool->params().m_maxOutgoingPeers);
set_max_incoming_peers(pool->params().m_maxIncomingPeers);
const Params& params = pool->params();
set_max_outgoing_peers(params.m_maxOutgoingPeers);
set_max_incoming_peers(params.m_maxIncomingPeers);
uv_mutex_init_checked(&m_rngLock);
uv_mutex_init_checked(&m_blockLock);
@@ -98,7 +100,7 @@ P2PServer::P2PServer(p2pool* pool)
}
load_peer_list();
start_listening(pool->params().m_p2pAddresses);
start_listening(params.m_p2pAddresses);
}
P2PServer::~P2PServer()
@@ -199,7 +201,7 @@ void P2PServer::update_peer_connections()
bool has_good_peers = false;
std::vector<raw_ip> connected_clients;
unordered_set<raw_ip> connected_clients;
{
MutexLock lock(m_clientsListLock);
connected_clients.reserve(m_numConnections);
@@ -215,11 +217,11 @@ void P2PServer::update_peer_connections()
}
if (client->m_handshakeComplete && client->m_lastBroadcastTimestamp) {
// - Side chain is at least 5 minutes newer (last_updated >= client->m_lastBroadcastTimestamp + 300)
// - Side chain is at least 15 minutes newer (last_updated >= client->m_lastBroadcastTimestamp + 900)
// - It's been at least 10 seconds since side chain updated (cur_time >= last_updated + 10)
// - It's been at least 10 seconds since the last block request (peer is not syncing)
// - Peer should have sent a broadcast by now
if (last_updated && (cur_time >= std::max(last_updated, client->m_lastBlockrequestTimestamp) + 10) && (last_updated >= client->m_lastBroadcastTimestamp + 300)) {
if (last_updated && (cur_time >= std::max(last_updated, client->m_lastBlockrequestTimestamp) + 10) && (last_updated >= client->m_lastBroadcastTimestamp + 900)) {
const uint64_t dt = last_updated - client->m_lastBroadcastTimestamp;
LOGWARN(5, "peer " << static_cast<char*>(client->m_addrString) << " is not broadcasting blocks (last update " << dt << " seconds ago)");
client->ban(DEFAULT_BAN_TIME);
@@ -230,7 +232,7 @@ void P2PServer::update_peer_connections()
}
if (!disconnected) {
connected_clients.emplace_back(client->m_addr);
connected_clients.insert(client->m_addr);
if (client->m_handshakeComplete && !client->m_handshakeInvalid && (client->m_listenPort >= 0)) {
has_good_peers = true;
}
@@ -245,7 +247,7 @@ void P2PServer::update_peer_connections()
if ((m_timerCounter % 30) == 1) {
// Update last seen time for currently connected peers
for (Peer& p : m_peerList) {
if (std::find_if(connected_clients.begin(), connected_clients.end(), [&p](const raw_ip& addr) { return p.m_addr == addr; }) != connected_clients.end()) {
if (connected_clients.find(p.m_addr) != connected_clients.end()) {
p.m_lastSeen = cur_time;
}
}
@@ -274,15 +276,7 @@ void P2PServer::update_peer_connections()
const uint64_t k = get_random64() % peer_list.size();
const Peer& peer = peer_list[k];
bool already_connected = false;
for (const raw_ip& ip : connected_clients) {
if (ip == peer.m_addr) {
already_connected = true;
break;
}
}
if (!already_connected && connect_to_peer(peer.m_isV6, peer.m_addr, peer.m_port)) {
if ((connected_clients.find(peer.m_addr) == connected_clients.end()) && connect_to_peer(peer.m_isV6, peer.m_addr, peer.m_port)) {
++i;
}
@@ -684,13 +678,15 @@ void P2PServer::remove_peer_from_list(const raw_ip& ip)
void P2PServer::broadcast(const PoolBlock& block)
{
if (block.m_txinGenHeight + 2 < m_pool->miner_data().height) {
LOGWARN(3, "Trying to broadcast a stale block " << block.m_sidechainId << " (mainchain height " << block.m_txinGenHeight << ", current height is " << m_pool->miner_data().height << ')');
MinerData miner_data = m_pool->miner_data();
if (block.m_txinGenHeight + 2 < miner_data.height) {
LOGWARN(3, "Trying to broadcast a stale block " << block.m_sidechainId << " (mainchain height " << block.m_txinGenHeight << ", current height is " << miner_data.height << ')');
return;
}
if (block.m_txinGenHeight > m_pool->miner_data().height + 2) {
LOGWARN(3, "Trying to broadcast a block " << block.m_sidechainId << " ahead on mainchain (mainchain height " << block.m_txinGenHeight << ", current height is " << m_pool->miner_data().height << ')');
if (block.m_txinGenHeight > miner_data.height + 2) {
LOGWARN(3, "Trying to broadcast a block " << block.m_sidechainId << " ahead on mainchain (mainchain height " << block.m_txinGenHeight << ", current height is " << miner_data.height << ')');
return;
}
@@ -804,21 +800,27 @@ void P2PServer::on_broadcast()
LOGINFO(6, "sending BLOCK_BROADCAST (pruned) to " << log::Gray() << static_cast<char*>(client->m_addrString));
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_BROADCAST);
*reinterpret_cast<uint32_t*>(p) = static_cast<uint32_t>(data->pruned_blob.size());
const uint32_t len = static_cast<uint32_t>(data->pruned_blob.size());
memcpy(p, &len, sizeof(uint32_t));
p += sizeof(uint32_t);
memcpy(p, data->pruned_blob.data(), data->pruned_blob.size());
p += data->pruned_blob.size();
if (len) {
memcpy(p, data->pruned_blob.data(), len);
p += len;
}
}
else {
LOGINFO(5, "sending BLOCK_BROADCAST (full) to " << log::Gray() << static_cast<char*>(client->m_addrString));
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_BROADCAST);
*reinterpret_cast<uint32_t*>(p) = static_cast<uint32_t>(data->blob.size());
const uint32_t len = static_cast<uint32_t>(data->blob.size());
memcpy(p, &len, sizeof(uint32_t));
p += sizeof(uint32_t);
memcpy(p, data->blob.data(), data->blob.size());
p += data->blob.size();
if (len) {
memcpy(p, data->blob.data(), len);
p += len;
}
}
return p - p0;
@@ -1028,6 +1030,7 @@ P2PServer::P2PClient::P2PClient()
, m_peerListPendingRequests(0)
, m_pingTime(0)
, m_blockPendingRequests(0)
, m_chainTipBlockRequest(false)
, m_lastAlive(0)
, m_lastBroadcastTimestamp(0)
, m_lastBlockrequestTimestamp(0)
@@ -1057,6 +1060,7 @@ void P2PServer::P2PClient::reset()
m_peerListPendingRequests = 0;
m_pingTime = 0;
m_blockPendingRequests = 0;
m_chainTipBlockRequest = false;
m_lastAlive = 0;
m_lastBroadcastTimestamp = 0;
m_lastBlockrequestTimestamp = 0;
@@ -1223,7 +1227,7 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent BLOCK_RESPONSE");
if (bytes_left >= 1 + sizeof(uint32_t)) {
const uint32_t block_size = *reinterpret_cast<uint32_t*>(buf + 1);
const uint32_t block_size = read_unaligned(reinterpret_cast<uint32_t*>(buf + 1));
if (bytes_left >= 1 + sizeof(uint32_t) + block_size) {
bytes_read = 1 + sizeof(uint32_t) + block_size;
@@ -1241,7 +1245,7 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
LOGINFO(6, "peer " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " sent BLOCK_BROADCAST");
if (bytes_left >= 1 + sizeof(uint32_t)) {
const uint32_t block_size = *reinterpret_cast<uint32_t*>(buf + 1);
const uint32_t block_size = read_unaligned(reinterpret_cast<uint32_t*>(buf + 1));
if (bytes_left >= 1 + sizeof(uint32_t) + block_size) {
bytes_read = 1 + sizeof(uint32_t) + block_size;
if (!on_block_broadcast(buf + 1 + sizeof(uint32_t), block_size)) {
@@ -1640,6 +1644,7 @@ void P2PServer::P2PClient::on_after_handshake(uint8_t* &p)
p += HASH_SIZE;
++m_blockPendingRequests;
m_chainTipBlockRequest = true;
m_lastBroadcastTimestamp = seconds_since_epoch();
}
@@ -1682,11 +1687,14 @@ bool P2PServer::P2PClient::on_block_request(const uint8_t* buf)
LOGINFO(5, "sending BLOCK_RESPONSE");
*(p++) = static_cast<uint8_t>(MessageId::BLOCK_RESPONSE);
*reinterpret_cast<uint32_t*>(p) = static_cast<uint32_t>(blob.size());
const uint32_t len = static_cast<uint32_t>(blob.size());
memcpy(p, &len, sizeof(uint32_t));
p += sizeof(uint32_t);
memcpy(p, blob.data(), blob.size());
p += blob.size();
if (len) {
memcpy(p, blob.data(), len);
p += len;
}
return p - p0;
});
@@ -1709,6 +1717,18 @@ bool P2PServer::P2PClient::on_block_response(const uint8_t* buf, uint32_t size)
return false;
}
if (m_chainTipBlockRequest) {
m_chainTipBlockRequest = false;
const uint64_t peer_height = server->m_block->m_txinGenHeight;
const uint64_t our_height = server->m_pool->miner_data().height;
if (peer_height + 2 < our_height) {
LOGWARN(4, "peer " << static_cast<char*>(m_addrString) << " is mining on top of a stale block (mainchain height " << peer_height << ", expected >= " << our_height << ')');
return false;
}
}
return handle_incoming_block_async(server->m_block);
}
@@ -1731,7 +1751,7 @@ bool P2PServer::P2PClient::on_block_broadcast(const uint8_t* buf, uint32_t size)
m_broadcastedHashes[m_broadcastedHashesIndex.fetch_add(1) % array_size(&P2PClient::m_broadcastedHashes)] = server->m_block->m_sidechainId;
const MinerData& miner_data = server->m_pool->miner_data();
MinerData miner_data = server->m_pool->miner_data();
if (server->m_block->m_prevId != miner_data.prev_id) {
// This peer is mining on top of a different Monero block, investigate it
@@ -1751,7 +1771,7 @@ bool P2PServer::P2PClient::on_block_broadcast(const uint8_t* buf, uint32_t size)
}
}
else {
LOGWARN(5, "peer " << static_cast<char*>(m_addrString) << " broadcasted an unreasonably stale block (mainchain height " << peer_height << ", expected >= " << our_height << ')');
LOGWARN(4, "peer " << static_cast<char*>(m_addrString) << " broadcasted an unreasonably stale block (mainchain height " << peer_height << ", expected >= " << our_height << ')');
return false;
}
}
+1
View File
@@ -117,6 +117,7 @@ public:
int64_t m_pingTime;
int m_blockPendingRequests;
bool m_chainTipBlockRequest;
uint64_t m_lastAlive;
uint64_t m_lastBroadcastTimestamp;
+80 -24
View File
@@ -72,7 +72,8 @@ p2pool::p2pool(int argc, char* argv[])
hash pub, sec, eph_public_key;
generate_keys(pub, sec);
if (!m_params->m_wallet.get_eph_public_key(sec, 0, eph_public_key)) {
uint8_t view_tag;
if (!m_params->m_wallet.get_eph_public_key(sec, 0, eph_public_key, view_tag)) {
LOGERR(1, "Invalid wallet address: get_eph_public_key failed");
panic();
}
@@ -108,7 +109,11 @@ p2pool::p2pool(int argc, char* argv[])
m_stopAsync.data = this;
uv_rwlock_init_checked(&m_mainchainLock);
uv_rwlock_init_checked(&m_minerDataLock);
uv_mutex_init_checked(&m_foundBlocksLock);
#ifdef WITH_RANDOMX
uv_mutex_init_checked(&m_minerLock);
#endif
uv_mutex_init_checked(&m_submitBlockDataLock);
m_api = m_params->m_apiPath.empty() ? nullptr : new p2pool_api(m_params->m_apiPath, m_params->m_localStats);
@@ -149,7 +154,11 @@ p2pool::p2pool(int argc, char* argv[])
p2pool::~p2pool()
{
uv_rwlock_destroy(&m_mainchainLock);
uv_rwlock_destroy(&m_minerDataLock);
uv_mutex_destroy(&m_foundBlocksLock);
#ifdef WITH_RANDOMX
uv_mutex_destroy(&m_minerLock);
#endif
uv_mutex_destroy(&m_submitBlockDataLock);
delete m_api;
@@ -187,6 +196,17 @@ bool p2pool::get_seed(uint64_t height, hash& seed) const
return true;
}
#ifdef WITH_RANDOMX
void p2pool::print_miner_status()
{
MutexLock lock(m_minerLock);
if (m_miner) {
m_miner->print_status();
}
}
#endif
void p2pool::handle_tx(TxMempoolData& tx)
{
if (!tx.weight || !tx.fee) {
@@ -203,7 +223,7 @@ void p2pool::handle_tx(TxMempoolData& tx)
", fee = " << log::Gray() << static_cast<double>(tx.fee) / 1e6 << " um");
#if TEST_MEMPOOL_PICKING_ALGORITHM
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
m_blockTemplate->update(miner_data(), *m_mempool, &m_params->m_wallet);
#endif
m_zmqLastActive = seconds_since_epoch();
@@ -239,7 +259,10 @@ void p2pool::handle_miner_data(MinerData& data)
data.tx_backlog.clear();
data.time_received = std::chrono::high_resolution_clock::now();
m_minerData = data;
{
WriteLock lock(m_minerDataLock);
m_minerData = data;
}
m_updateSeed = true;
update_median_timestamp();
@@ -549,11 +572,13 @@ void p2pool::update_block_template_async()
void p2pool::update_block_template()
{
MinerData data = miner_data();
if (m_updateSeed) {
m_hasher->set_seed_async(m_minerData.seed_hash);
m_hasher->set_seed_async(data.seed_hash);
m_updateSeed = false;
}
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
m_blockTemplate->update(data, *m_mempool, &m_params->m_wallet);
stratum_on_block();
api_update_pool_stats();
}
@@ -596,13 +621,15 @@ void p2pool::download_block_headers(uint64_t current_height)
});
}
const uint64_t start_height = (current_height > BLOCK_HEADERS_REQUIRED) ? (current_height - BLOCK_HEADERS_REQUIRED) : 0;
s.m_pos = 0;
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_block_headers_range\",\"params\":{\"start_height\":" << current_height - BLOCK_HEADERS_REQUIRED << ",\"end_height\":" << current_height - 1 << "}}\0";
s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"get_block_headers_range\",\"params\":{\"start_height\":" << start_height << ",\"end_height\":" << current_height - 1 << "}}\0";
JSONRPCRequest::call(m_params->m_host.c_str(), m_params->m_rpcPort, buf,
[this, current_height](const char* data, size_t size)
[this, start_height, current_height](const char* data, size_t size)
{
if (parse_block_headers_range(data, size) == BLOCK_HEADERS_REQUIRED) {
if (parse_block_headers_range(data, size) == current_height - start_height) {
update_median_timestamp();
if (m_serversStarted.exchange(1) == 0) {
m_ZMQReader = new ZMQReader(m_params->m_host.c_str(), m_params->m_zmqPort, this);
@@ -617,14 +644,14 @@ void p2pool::download_block_headers(uint64_t current_height)
}
}
else {
LOGERR(1, "fatal error: couldn't download block headers for heights " << current_height - BLOCK_HEADERS_REQUIRED << " - " << current_height - 1);
LOGERR(1, "fatal error: couldn't download block headers for heights " << start_height << " - " << current_height - 1);
panic();
}
},
[current_height](const char* data, size_t size)
[start_height, current_height](const char* data, size_t size)
{
if (size > 0) {
LOGERR(1, "fatal error: couldn't download block headers for heights " << current_height - BLOCK_HEADERS_REQUIRED << " - " << current_height - 1 << ", error " << log::const_buf(data, size));
LOGERR(1, "fatal error: couldn't download block headers for heights " << start_height << " - " << current_height - 1 << ", error " << log::const_buf(data, size));
panic();
}
});
@@ -666,6 +693,7 @@ void p2pool::update_median_timestamp()
uint64_t timestamps[TIMESTAMP_WINDOW];
if (!get_timestamps(timestamps))
{
WriteLock lock(m_minerDataLock);
m_minerData.median_timestamp = 0;
return;
}
@@ -673,17 +701,25 @@ void p2pool::update_median_timestamp()
std::sort(timestamps, timestamps + TIMESTAMP_WINDOW);
// Shift it +1 block compared to Monero's code because we don't have the latest block yet when we receive new miner data
m_minerData.median_timestamp = (timestamps[TIMESTAMP_WINDOW / 2] + timestamps[TIMESTAMP_WINDOW / 2 + 1]) / 2;
LOGINFO(4, "median timestamp updated to " << log::Gray() << m_minerData.median_timestamp);
const uint64_t ts = (timestamps[TIMESTAMP_WINDOW / 2] + timestamps[TIMESTAMP_WINDOW / 2 + 1]) / 2;
LOGINFO(4, "median timestamp updated to " << log::Gray() << ts);
WriteLock lock(m_minerDataLock);
m_minerData.median_timestamp = ts;
}
void p2pool::stratum_on_block()
{
#ifdef WITH_RANDOMX
if (m_miner) {
m_miner->on_block(*m_blockTemplate);
{
MutexLock lock(m_minerLock);
if (m_miner) {
m_miner->on_block(*m_blockTemplate);
}
}
#endif
if (m_stratumServer) {
m_stratumServer->on_block(*m_blockTemplate);
}
@@ -1021,10 +1057,16 @@ void p2pool::api_update_network_stats()
return;
}
hash prev_id;
{
ReadLock lock(m_minerDataLock);
prev_id = m_minerData.prev_id;
}
ChainMain mainnet_tip;
{
ReadLock lock(m_mainchainLock);
mainnet_tip = m_mainchainByHash[m_minerData.prev_id];
mainnet_tip = m_mainchainByHash[prev_id];
}
m_api->set(p2pool_api::Category::NETWORK, "stats",
@@ -1086,10 +1128,16 @@ void p2pool::api_update_stats_mod()
return;
}
hash prev_id;
{
ReadLock lock(m_minerDataLock);
prev_id = m_minerData.prev_id;
}
ChainMain mainnet_tip;
{
ReadLock lock(m_mainchainLock);
mainnet_tip = m_mainchainByHash[m_minerData.prev_id];
mainnet_tip = m_mainchainByHash[prev_id];
}
time_t last_block_found_time = 0;
@@ -1100,10 +1148,11 @@ void p2pool::api_update_stats_mod()
{
MutexLock lock(m_foundBlocksLock);
if (!m_foundBlocks.empty()) {
last_block_found_time = m_foundBlocks.back().timestamp;
last_block_found_height = m_foundBlocks.back().height;
last_block_found_hash = m_foundBlocks.back().id;
last_block_total_hashes = m_foundBlocks.back().total_hashes;
const FoundBlock& b = m_foundBlocks.back();
last_block_found_time = b.timestamp;
last_block_found_height = b.height;
last_block_found_hash = b.id;
last_block_total_hashes = b.total_hashes;
}
}
@@ -1233,15 +1282,18 @@ bool p2pool::get_difficulty_at_height(uint64_t height, difficulty_type& diff)
void p2pool::start_mining(uint32_t threads)
{
stop_mining();
MutexLock lock(m_minerLock);
m_miner = new Miner(this, threads);
}
void p2pool::stop_mining()
{
Miner* miner = m_miner;
if (miner) {
MutexLock lock(m_minerLock);
if (m_miner) {
delete m_miner;
m_miner = nullptr;
delete miner;
}
}
#endif
@@ -1288,6 +1340,10 @@ static bool init_uv_threadpool()
static bool init_signals(p2pool* pool)
{
#ifdef SIGPIPE
signal(SIGPIPE, SIG_IGN);
#endif
constexpr int signal_names[] = {
SIGHUP,
SIGINT,
+14 -4
View File
@@ -48,7 +48,12 @@ public:
const Params& params() const { return *m_params; }
BlockTemplate& block_template() { return *m_blockTemplate; }
SideChain& side_chain() { return *m_sideChain; }
const MinerData& miner_data() const { return m_minerData; }
FORCEINLINE MinerData miner_data() const
{
ReadLock lock(m_minerDataLock);
return m_minerData;
}
p2pool_api* api() const { return m_api; }
@@ -59,8 +64,9 @@ public:
StratumServer* stratum_server() const { return m_stratumServer; }
P2PServer* p2p_server() const { return m_p2pServer; }
#ifdef WITH_RANDOMX
Miner* miner() const { return m_miner; }
void print_miner_status();
#endif
virtual void handle_tx(TxMempoolData& tx) override;
@@ -108,7 +114,6 @@ private:
SideChain* m_sideChain;
RandomX_Hasher_Base* m_hasher;
BlockTemplate* m_blockTemplate;
MinerData m_minerData;
bool m_updateSeed;
Mempool* m_mempool;
@@ -116,6 +121,9 @@ private:
std::map<uint64_t, ChainMain> m_mainchainByHeight;
unordered_map<hash, ChainMain> m_mainchainByHash;
mutable uv_rwlock_t m_minerDataLock;
MinerData m_minerData;
enum { TIMESTAMP_WINDOW = 60 };
bool get_timestamps(uint64_t (&timestamps)[TIMESTAMP_WINDOW]) const;
void update_median_timestamp();
@@ -164,7 +172,9 @@ private:
std::atomic<uint32_t> m_serversStarted{ 0 };
StratumServer* m_stratumServer = nullptr;
P2PServer* m_p2pServer = nullptr;
#ifdef WITH_RANDOMX
uv_mutex_t m_minerLock;
Miner* m_miner = nullptr;
#endif
@@ -185,7 +195,7 @@ private:
uv_async_t m_blockTemplateAsync;
uv_async_t m_stopAsync;
uint64_t m_zmqLastActive;
std::atomic<uint64_t> m_zmqLastActive;
uint64_t m_startTime;
ZMQReader* m_ZMQReader = nullptr;
+17 -3
View File
@@ -157,8 +157,12 @@ void PoolBlock::serialize_mainchain_data(uint32_t nonce, uint32_t extra_nonce, c
for (TxOutput& output : m_outputs) {
writeVarint(output.m_reward, m_mainChainData);
m_mainChainData.push_back(TXOUT_TO_KEY);
m_mainChainData.push_back(output.m_txType);
m_mainChainData.insert(m_mainChainData.end(), output.m_ephPublicKey.h, output.m_ephPublicKey.h + HASH_SIZE);
if (output.m_txType == TXOUT_TO_TAGGED_KEY) {
m_mainChainData.push_back(output.m_viewTag);
}
}
m_mainChainOutputsBlobSize = static_cast<int>(m_mainChainData.size()) - m_mainChainOutputsOffset;
@@ -301,9 +305,19 @@ bool PoolBlock::get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const
uint64_t PoolBlock::get_payout(const Wallet& w) const
{
for (size_t i = 0, n = m_outputs.size(); i < n; ++i) {
const TxOutput& out = m_outputs[i];
hash eph_public_key;
if ((w.get_eph_public_key(m_txkeySec, i, eph_public_key)) && (eph_public_key == m_outputs[i].m_ephPublicKey)) {
return m_outputs[i].m_reward;
if (out.m_txType == TXOUT_TO_TAGGED_KEY) {
if (w.get_eph_public_key_with_view_tag(m_txkeySec, i, eph_public_key, out.m_viewTag) && (eph_public_key == out.m_ephPublicKey)) {
return out.m_reward;
}
}
else {
uint8_t view_tag;
if (w.get_eph_public_key(m_txkeySec, i, eph_public_key, view_tag) && (eph_public_key == out.m_ephPublicKey)) {
return out.m_reward;
}
}
}
+8 -2
View File
@@ -81,11 +81,13 @@ struct PoolBlock
struct TxOutput
{
FORCEINLINE TxOutput() : m_reward(0), m_ephPublicKey() {}
FORCEINLINE TxOutput(uint64_t r, const hash& k) : m_reward(r), m_ephPublicKey(k) {}
FORCEINLINE TxOutput() : m_reward(0), m_ephPublicKey(), m_txType(0), m_viewTag(0) {}
FORCEINLINE TxOutput(uint64_t r, const hash& k, uint8_t tx_type, uint8_t view_tag) : m_reward(r), m_ephPublicKey(k), m_txType(tx_type), m_viewTag(view_tag) {}
uint64_t m_reward;
hash m_ephPublicKey;
uint8_t m_txType;
uint8_t m_viewTag;
};
std::vector<TxOutput> m_outputs;
@@ -139,6 +141,10 @@ struct PoolBlock
bool get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash);
uint64_t get_payout(const Wallet& w) const;
// Both tx types are allowed by Monero consensus during v15 because it needs to process pre-fork mempool transactions,
// but P2Pool can switch to using only TXOUT_TO_TAGGED_KEY for miner payouts starting from v15
FORCEINLINE uint8_t get_tx_type() const { return (m_majorVersion < HARDFORK_VIEW_TAGS_VERSION) ? TXOUT_TO_KEY : TXOUT_TO_TAGGED_KEY; }
};
} // namespace p2pool
+11 -25
View File
@@ -46,29 +46,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
#define READ_BYTE(x) do { if (!read_byte(x)) return __LINE__; } while (0)
#define EXPECT_BYTE(value) do { uint8_t tmp; READ_BYTE(tmp); if (tmp != (value)) return __LINE__; } while (0)
auto read_varint = [&data, data_end](auto& b) -> bool
{
uint64_t result = 0;
int k = 0;
while (data < data_end) {
if (k >= static_cast<int>(sizeof(b)) * 8) {
return false;
}
const uint64_t cur_byte = *(data++);
result |= (cur_byte & 0x7F) << k;
k += 7;
if ((cur_byte & 0x80) == 0) {
b = result;
return true;
}
}
return false;
};
#define READ_VARINT(x) do { if (!read_varint(x)) return __LINE__; } while(0)
#define READ_VARINT(x) do { data = readVarint(data, data_end, x); if (!data) return __LINE__; } while(0)
auto read_buf = [&data, data_end](void* buf, size_t size) -> bool
{
@@ -121,7 +99,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
if (num_outputs > 0) {
// Outputs are in the buffer, just read them
// Each output is at least 34 bytes, exit early if there's not enough data left
// 1 byte for reward, 1 byte for TXOUT_TO_KEY, 32 bytes for eph_pub_key
// 1 byte for reward, 1 byte for tx_type, 32 bytes for eph_pub_key
constexpr uint64_t MIN_OUTPUT_SIZE = 34;
if (num_outputs > std::numeric_limits<uint64_t>::max() / MIN_OUTPUT_SIZE) return __LINE__;
@@ -130,15 +108,23 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
m_outputs.clear();
m_outputs.reserve(num_outputs);
const uint8_t expected_tx_type = get_tx_type();
for (uint64_t i = 0; i < num_outputs; ++i) {
TxOutput t;
READ_VARINT(t.m_reward);
total_reward += t.m_reward;
EXPECT_BYTE(TXOUT_TO_KEY);
EXPECT_BYTE(expected_tx_type);
t.m_txType = expected_tx_type;
READ_BUF(t.m_ephPublicKey.h, HASH_SIZE);
if (expected_tx_type == TXOUT_TO_TAGGED_KEY) {
READ_BYTE(t.m_viewTag);
}
m_outputs.emplace_back(std::move(t));
}
+98 -45
View File
@@ -60,7 +60,7 @@ static constexpr uint8_t mini_consensus_id[HASH_SIZE] = { 57,130,201,26,149,174,
SideChain::SideChain(p2pool* pool, NetworkType type, const char* pool_name)
: m_pool(pool)
, m_networkType(type)
, m_chainTip(nullptr)
, m_chainTip{ nullptr }
, m_poolName(pool_name ? pool_name : "default")
, m_targetBlockTime(10)
, m_minDifficulty(MIN_DIFFICULTY, 0)
@@ -177,7 +177,9 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
block.m_txkeySec = txkeySec;
block.m_uncles.clear();
if (!m_chainTip) {
const PoolBlock* tip = m_chainTip;
if (!tip) {
block.m_parent = {};
block.m_sidechainHeight = 0;
block.m_difficulty = m_minDifficulty;
@@ -187,8 +189,8 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
return;
}
block.m_parent = m_chainTip->m_sidechainId;
block.m_sidechainHeight = m_chainTip->m_sidechainHeight + 1;
block.m_parent = tip->m_sidechainId;
block.m_sidechainHeight = tip->m_sidechainHeight + 1;
// Collect uncles from 3 previous block heights
@@ -196,15 +198,15 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
std::vector<hash> mined_blocks;
mined_blocks.reserve(UNCLE_BLOCK_DEPTH * 2 + 1);
PoolBlock* tmp = m_chainTip;
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, m_chainTip->m_sidechainHeight + 1); tmp && (i < n); ++i) {
const PoolBlock* tmp = tip;
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, tip->m_sidechainHeight + 1); tmp && (i < n); ++i) {
mined_blocks.push_back(tmp->m_sidechainId);
mined_blocks.insert(mined_blocks.end(), tmp->m_uncles.begin(), tmp->m_uncles.end());
tmp = get_parent(tmp);
}
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, m_chainTip->m_sidechainHeight + 1); i < n; ++i) {
for (PoolBlock* uncle : m_blocksByHeight[m_chainTip->m_sidechainHeight - i]) {
for (uint64_t i = 0, n = std::min<uint64_t>(UNCLE_BLOCK_DEPTH, tip->m_sidechainHeight + 1); i < n; ++i) {
for (PoolBlock* uncle : m_blocksByHeight[tip->m_sidechainHeight - i]) {
// Only add verified and valid blocks
if (!uncle || !uncle->m_verified || uncle->m_invalid) {
continue;
@@ -218,7 +220,7 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
// Only add it if it's on the same chain
bool same_chain = false;
do {
tmp = m_chainTip;
tmp = tip;
while (tmp->m_sidechainHeight > uncle->m_sidechainHeight) {
tmp = get_parent(tmp);
if (!tmp) {
@@ -262,7 +264,7 @@ void SideChain::fill_sidechain_data(PoolBlock& block, Wallet* w, const hash& txk
}
block.m_difficulty = m_curDifficulty;
block.m_cumulativeDifficulty = m_chainTip->m_cumulativeDifficulty + block.m_difficulty;
block.m_cumulativeDifficulty = tip->m_cumulativeDifficulty + block.m_difficulty;
for (const hash& uncle_id : block.m_uncles) {
auto it = m_blocksById.find(uncle_id);
@@ -367,6 +369,7 @@ bool SideChain::block_seen(const PoolBlock& block)
{
// Check if it's some old block
const PoolBlock* tip = m_chainTip;
if (tip && tip->m_sidechainHeight > block.m_sidechainHeight + m_chainWindowSize * 2 &&
block.m_cumulativeDifficulty < tip->m_cumulativeDifficulty) {
return true;
@@ -406,7 +409,9 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
difficulty_type diff2 = block.m_difficulty;
diff2 += block.m_difficulty;
for (PoolBlock* tmp = m_chainTip; tmp && (tmp->m_sidechainHeight + m_chainWindowSize > m_chainTip->m_sidechainHeight); tmp = get_parent(tmp)) {
const PoolBlock* tip = m_chainTip;
for (const PoolBlock* tmp = tip; tmp && (tmp->m_sidechainHeight + m_chainWindowSize > tip->m_sidechainHeight); tmp = get_parent(tmp)) {
if (diff2 >= tmp->m_difficulty) {
too_low_diff = false;
break;
@@ -449,7 +454,7 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
}
// Check if it has the correct parent and difficulty to go right to monerod for checking
const MinerData& miner_data = m_pool->miner_data();
MinerData miner_data = m_pool->miner_data();
if ((block.m_prevId == miner_data.prev_id) && miner_data.difficulty.check_pow(pow_hash)) {
LOGINFO(0, log::LightGreen() << "add_external_block: block " << block.m_sidechainId << " has enough PoW for Monero network, submitting it");
m_pool->submit_block_async(block.m_mainChainData);
@@ -573,7 +578,7 @@ bool SideChain::get_block_blob(const hash& id, std::vector<uint8_t>& blob)
{
MutexLock lock(m_sidechainLock);
PoolBlock* block = nullptr;
const PoolBlock* block = nullptr;
// Empty hash means we return current sidechain tip
if (id == hash()) {
@@ -608,13 +613,17 @@ bool SideChain::get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::v
PoolBlock* b = it->second;
const size_t n = b->m_outputs.size();
blob.reserve(n * 38 + 64);
blob.reserve(n * 39 + 64);
writeVarint(n, blob);
for (const PoolBlock::TxOutput& output : b->m_outputs) {
writeVarint(output.m_reward, blob);
blob.emplace_back(TXOUT_TO_KEY);
blob.emplace_back(output.m_txType);
blob.insert(blob.end(), output.m_ephPublicKey.h, output.m_ephPublicKey.h + HASH_SIZE);
if (output.m_txType == TXOUT_TO_TAGGED_KEY) {
blob.emplace_back(output.m_viewTag);
}
}
block->m_outputs = b->m_outputs;
@@ -627,25 +636,32 @@ bool SideChain::get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::v
const size_t n = m_tmpShares.size();
blob.reserve(n * 38 + 64);
blob.reserve(n * 39 + 64);
writeVarint(n, blob);
block->m_outputs.clear();
block->m_outputs.reserve(n);
const uint8_t tx_type = block->get_tx_type();
hash eph_public_key;
for (size_t i = 0; i < n; ++i) {
writeVarint(m_tmpRewards[i], blob);
blob.emplace_back(TXOUT_TO_KEY);
blob.emplace_back(tx_type);
if (!m_tmpShares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key)) {
uint8_t view_tag;
if (!m_tmpShares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key, view_tag)) {
LOGWARN(6, "get_eph_public_key failed at index " << i);
}
blob.insert(blob.end(), eph_public_key.h, eph_public_key.h + HASH_SIZE);
block->m_outputs.emplace_back(m_tmpRewards[i], eph_public_key);
if (tx_type == TXOUT_TO_TAGGED_KEY) {
blob.emplace_back(view_tag);
}
block->m_outputs.emplace_back(m_tmpRewards[i], eph_public_key, tx_type, view_tag);
}
return true;
@@ -661,12 +677,14 @@ void SideChain::print_status()
uint64_t rem;
uint64_t pool_hashrate = udiv128(m_curDifficulty.hi, m_curDifficulty.lo, m_targetBlockTime, &rem);
const difficulty_type& network_diff = m_pool->miner_data().difficulty;
difficulty_type network_diff = m_pool->miner_data().difficulty;
uint64_t network_hashrate = udiv128(network_diff.hi, network_diff.lo, 120, &rem);
const PoolBlock* tip = m_chainTip;
uint64_t block_depth = 0;
PoolBlock* cur = m_chainTip;
const uint64_t tip_height = m_chainTip ? m_chainTip->m_sidechainHeight : 0;
const PoolBlock* cur = tip;
const uint64_t tip_height = tip ? tip->m_sidechainHeight : 0;
uint32_t total_blocks_in_window = 0;
uint32_t total_uncles_in_window = 0;
@@ -715,7 +733,7 @@ void SideChain::print_status()
uint64_t your_reward = 0;
uint64_t total_reward = 0;
if (m_chainTip) {
if (tip) {
std::sort(blocks_in_window.begin(), blocks_in_window.end());
for (uint64_t i = 0; (i < m_chainWindowSize) && (i <= tip_height); ++i) {
for (PoolBlock* block : m_blocksByHeight[tip_height - i]) {
@@ -730,14 +748,24 @@ void SideChain::print_status()
}
Wallet w = m_pool->params().m_wallet;
const std::vector<PoolBlock::TxOutput>& outs = m_chainTip->m_outputs;
hash eph_public_key;
for (size_t i = 0, n = outs.size(); i < n; ++i) {
if (w.get_eph_public_key(m_chainTip->m_txkeySec, i, eph_public_key) && (outs[i].m_ephPublicKey == eph_public_key)) {
your_reward = outs[i].m_reward;
for (size_t i = 0, n = tip->m_outputs.size(); i < n; ++i) {
const PoolBlock::TxOutput& out = tip->m_outputs[i];
if (!your_reward) {
if (out.m_txType == TXOUT_TO_TAGGED_KEY) {
if (w.get_eph_public_key_with_view_tag(tip->m_txkeySec, i, eph_public_key, out.m_viewTag) && (out.m_ephPublicKey == eph_public_key)) {
your_reward = out.m_reward;
}
}
else {
uint8_t view_tag;
if (w.get_eph_public_key(tip->m_txkeySec, i, eph_public_key, view_tag) && (out.m_ephPublicKey == eph_public_key)) {
your_reward = out.m_reward;
}
}
}
total_reward += outs[i].m_reward;
total_reward += out.m_reward;
}
}
@@ -785,7 +813,8 @@ void SideChain::print_status()
difficulty_type SideChain::total_hashes() const
{
return m_chainTip ? m_chainTip->m_cumulativeDifficulty : difficulty_type();
const PoolBlock* tip = m_chainTip;
return tip ? tip->m_cumulativeDifficulty : difficulty_type();
}
uint64_t SideChain::miner_count()
@@ -809,7 +838,8 @@ uint64_t SideChain::miner_count()
uint64_t SideChain::last_updated() const
{
return m_chainTip ? m_chainTip->m_localTimestamp : 0;
const PoolBlock* tip = m_chainTip;
return tip ? tip->m_localTimestamp : 0;
}
bool SideChain::is_default() const
@@ -1293,7 +1323,13 @@ void SideChain::verify(PoolBlock* block)
});
std::vector<uint64_t> rewards;
split_reward(total_reward, shares, rewards);
if (!split_reward(total_reward, shares, rewards)) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
", id = " << block->m_sidechainId <<
", mainchain height = " << block->m_txinGenHeight << ": split_reward failed");
block->m_invalid = true;
return;
}
if (rewards.size() != block->m_outputs.size()) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
@@ -1305,17 +1341,20 @@ void SideChain::verify(PoolBlock* block)
}
for (size_t i = 0, n = rewards.size(); i < n; ++i) {
if (rewards[i] != block->m_outputs[i].m_reward) {
const PoolBlock::TxOutput& out = block->m_outputs[i];
if (rewards[i] != out.m_reward) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
", id = " << block->m_sidechainId <<
", mainchain height = " << block->m_txinGenHeight <<
" has invalid reward at index " << i << ": got " << block->m_outputs[i].m_reward << ", expected " << rewards[i]);
" has invalid reward at index " << i << ": got " << out.m_reward << ", expected " << rewards[i]);
block->m_invalid = true;
return;
}
hash eph_public_key;
if (!shares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key)) {
uint8_t view_tag;
if (!shares[i].m_wallet->get_eph_public_key(block->m_txkeySec, i, eph_public_key, view_tag)) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
", id = " << block->m_sidechainId <<
", mainchain height = " << block->m_txinGenHeight <<
@@ -1324,7 +1363,16 @@ void SideChain::verify(PoolBlock* block)
return;
}
if (eph_public_key != block->m_outputs[i].m_ephPublicKey) {
if ((out.m_txType == TXOUT_TO_TAGGED_KEY) && (out.m_viewTag != view_tag)) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
", id = " << block->m_sidechainId <<
", mainchain height = " << block->m_txinGenHeight <<
" has an incorrect view tag at index " << i);
block->m_invalid = true;
return;
}
if (eph_public_key != out.m_ephPublicKey) {
LOGWARN(3, "block at height = " << block->m_sidechainHeight <<
", id = " << block->m_sidechainId <<
", mainchain height = " << block->m_txinGenHeight <<
@@ -1350,8 +1398,10 @@ void SideChain::update_chain_tip(PoolBlock* block)
return;
}
const PoolBlock* tip = m_chainTip;
bool is_alternative;
if (is_longer_chain(m_chainTip, block, is_alternative)) {
if (is_longer_chain(tip, block, is_alternative)) {
difficulty_type diff;
if (get_difficulty(block, m_difficultyData, diff)) {
m_chainTip = block;
@@ -1359,7 +1409,7 @@ void SideChain::update_chain_tip(PoolBlock* block)
LOGINFO(2, "new chain tip: next height = " << log::Gray() << block->m_sidechainHeight + 1 << log::NoColor() <<
", next difficulty = " << log::Gray() << m_curDifficulty << log::NoColor() <<
", main chain height = " << log::Gray() << m_chainTip->m_txinGenHeight);
", main chain height = " << log::Gray() << block->m_txinGenHeight);
block->m_wantBroadcast = true;
if (m_pool) {
@@ -1374,13 +1424,13 @@ void SideChain::update_chain_tip(PoolBlock* block)
prune_old_blocks();
}
}
else if (block->m_sidechainHeight > m_chainTip->m_sidechainHeight) {
else if (block->m_sidechainHeight > tip->m_sidechainHeight) {
LOGINFO(4, "block " << block->m_sidechainId <<
", height = " << block->m_sidechainHeight <<
" is not a longer chain than " << m_chainTip->m_sidechainId <<
", height " << m_chainTip->m_sidechainHeight);
" is not a longer chain than " << tip->m_sidechainId <<
", height " << tip->m_sidechainHeight);
}
else if (block->m_sidechainHeight + UNCLE_BLOCK_DEPTH > m_chainTip->m_sidechainHeight) {
else if (block->m_sidechainHeight + UNCLE_BLOCK_DEPTH > tip->m_sidechainHeight) {
LOGINFO(4, "possible uncle block: id = " << log::Gray() << block->m_sidechainId << log::NoColor() <<
", height = " << log::Gray() << block->m_sidechainHeight);
m_pool->update_block_template_async();
@@ -1513,8 +1563,9 @@ bool SideChain::is_longer_chain(const PoolBlock* block, const PoolBlock* candida
}
// Final check: candidate chain must be built on top of recent mainchain blocks
if (candidate_mainchain_height + 10 < m_pool->miner_data().height) {
LOGWARN(3, "received a longer alternative chain but it's stale: height " << candidate_mainchain_height << ", current height " << m_pool->miner_data().height);
MinerData data = m_pool->miner_data();
if (candidate_mainchain_height + 10 < data.height) {
LOGWARN(3, "received a longer alternative chain but it's stale: height " << candidate_mainchain_height << ", current height " << data.height);
return false;
}
@@ -1598,11 +1649,13 @@ void SideChain::prune_old_blocks()
const uint64_t cur_time = seconds_since_epoch();
const uint64_t prune_delay = m_chainWindowSize * 4 * m_targetBlockTime;
if (m_chainTip->m_sidechainHeight < prune_distance) {
const PoolBlock* tip = m_chainTip;
if (tip->m_sidechainHeight < prune_distance) {
return;
}
const uint64_t h = m_chainTip->m_sidechainHeight - prune_distance;
const uint64_t h = tip->m_sidechainHeight - prune_distance;
uint64_t num_blocks_pruned = 0;
+1 -1
View File
@@ -99,7 +99,7 @@ private:
bool check_config();
mutable uv_mutex_t m_sidechainLock;
PoolBlock* m_chainTip;
std::atomic<PoolBlock*> m_chainTip;
std::map<uint64_t, std::vector<PoolBlock*>> m_blocksByHeight;
unordered_map<hash, PoolBlock*> m_blocksById;
unordered_map<hash, uint64_t> m_seenWallets;
+2 -4
View File
@@ -53,7 +53,7 @@ public:
struct Client
{
Client();
virtual ~Client();
virtual ~Client() {}
virtual void reset();
virtual bool on_connect() = 0;
@@ -90,8 +90,6 @@ public:
uint32_t m_numRead;
std::atomic<uint32_t> m_resetCounter{ 0 };
uv_mutex_t m_sendLock;
};
struct WriteBuf
@@ -152,7 +150,7 @@ protected:
int m_listenPort;
uv_loop_t m_loop;
volatile bool m_loopStopped;
std::atomic<bool> m_loopStopped;
uv_mutex_t m_clientsListLock;
std::vector<Client*> m_preallocatedClients;
+5 -13
View File
@@ -27,7 +27,7 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::TCPServer(allocate_client_callback all
, m_loopThread{}
, m_finished(0)
, m_listenPort(-1)
, m_loopStopped(false)
, m_loopStopped{false}
, m_numConnections(0)
, m_numIncomingConnections(0)
{
@@ -532,8 +532,6 @@ bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, Sen
LOGERR(1, "sending data from another thread, this is not thread safe");
}
MutexLock lock0(client->m_sendLock);
WriteBuf* buf = nullptr;
{
@@ -620,9 +618,11 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_new_connection(uv_stream_t* se
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connection_close(uv_handle_t* handle)
{
Client* client = static_cast<Client*>(handle->data);
MutexLock lock0(client->m_sendLock);
if (!server_event_loop_thread) {
LOGERR(1, "on_connection_close called from another thread, this is not thread safe");
}
Client* client = static_cast<Client*>(handle->data);
TCPServer* owner = client->m_owner;
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " disconnected");
@@ -811,17 +811,9 @@ TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::Client()
{
Client::reset();
uv_mutex_init_checked(&m_sendLock);
m_readBuf[0] = '\0';
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::~Client()
{
uv_mutex_destroy(&m_sendLock);
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::reset()
{
+2 -2
View File
@@ -32,7 +32,7 @@ namespace p2pool {
#define STR2(X) STR(X)
#define STR(X) #X
const char* VERSION = "v1.9 (built"
const char* VERSION = "v2.0 (built"
#if defined(__clang__)
" with clang/" __clang_version__
#elif defined(__GNUC__)
@@ -66,7 +66,7 @@ void make_thread_background()
SetThreadPriorityBoost(GetCurrentThread(), true);
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
#elif !defined(__APPLE__)
#elif !defined(__APPLE__) && !defined(__FreeBSD__)
sched_param param;
param.sched_priority = 0;
if (sched_setscheduler(0, SCHED_IDLE, &param) != 0) {
+34
View File
@@ -109,6 +109,40 @@ FORCEINLINE void writeVarint(T value, std::vector<uint8_t>& out)
writeVarint(value, [&out](uint8_t b) { out.emplace_back(b); });
}
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;
}
template<typename T>
FORCEINLINE T read_unaligned(const T* p)
{
static_assert(std::is_integral<T>::value, "T must be an integer type");
T result;
memcpy(&result, p, sizeof(T));
return result;
}
template<typename T, size_t N> FORCEINLINE constexpr size_t array_size(T(&)[N]) { return N; }
template<typename T, typename U, size_t N> FORCEINLINE constexpr size_t array_size(T(U::*)[N]) { return N; }
+17 -6
View File
@@ -84,10 +84,6 @@ Wallet::Wallet(const char* address) : m_prefix(0), m_checksum(0), m_type(Network
decode(address);
}
Wallet::~Wallet()
{
}
Wallet::Wallet(const Wallet& w)
{
operator=(w);
@@ -199,10 +195,25 @@ bool Wallet::assign(const hash& spend_pub_key, const hash& view_pub_key, Network
return true;
}
bool Wallet::get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key) const
bool Wallet::get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t& view_tag) const
{
hash derivation;
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, derivation)) {
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, output_index, derivation, view_tag)) {
return false;
}
if (!derive_public_key(derivation, output_index, m_spendPublicKey, eph_public_key)) {
return false;
}
return true;
}
bool Wallet::get_eph_public_key_with_view_tag(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t expected_view_tag) const
{
hash derivation;
uint8_t view_tag;
if (!generate_key_derivation(m_viewPublicKey, txkey_sec, output_index, derivation, view_tag) || (view_tag != expected_view_tag)) {
return false;
}
+2 -2
View File
@@ -25,7 +25,6 @@ class Wallet
{
public:
explicit Wallet(const char* address);
~Wallet();
Wallet(const Wallet& w);
Wallet& operator=(const Wallet& w);
@@ -39,7 +38,8 @@ public:
FORCEINLINE const hash& spend_public_key() const { return m_spendPublicKey; }
FORCEINLINE const hash& view_public_key() const { return m_viewPublicKey; }
bool get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key) const;
bool get_eph_public_key(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t& view_tag) const;
bool get_eph_public_key_with_view_tag(const hash& txkey_sec, size_t output_index, hash& eph_public_key, uint8_t expected_view_tag) const;
FORCEINLINE bool operator<(const Wallet& w) const { return m_spendPublicKey < w.m_spendPublicKey; }
FORCEINLINE bool operator==(const Wallet& w) const { return m_spendPublicKey == w.m_spendPublicKey; }
+1
View File
@@ -74,6 +74,7 @@ set(SOURCES
src/keccak_tests.cpp
src/main.cpp
src/pool_block_tests.cpp
src/varint_tests.cpp
src/wallet_tests.cpp
../external/src/cryptonote/crypto-ops-data.c
../external/src/cryptonote/crypto-ops.c
+16 -1
View File
@@ -42,7 +42,8 @@ TEST(crypto, derivation)
if (result) {
f >> expected_derivation;
}
ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, derivation), result);
uint8_t view_tag;
ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 0, derivation, view_tag), result);
if (result) {
ASSERT_EQ(derivation, expected_derivation);
}
@@ -61,6 +62,20 @@ TEST(crypto, derivation)
ASSERT_EQ(derived_key, expected_derived_key);
}
}
else if (name == "derive_view_tag") {
hash derivation;
uint64_t output_index;
std::string result_str;
f >> derivation >> output_index >> result_str;
uint8_t view_tag;
p2pool::derive_view_tag(derivation, output_index, view_tag);
char buf[log::Stream::BUF_SIZE + 1];
log::Stream s(buf);
s << log::hex_buf(&view_tag, 1) << '\0';
ASSERT_EQ(buf, result_str);
}
} while (!f.eof());
}
+70
View File
@@ -542,3 +542,73 @@ derive_public_key 5ea95a51ab11b80c7d09d0c8f8952b70c67e81d0fd421bbed43ab77c1b7b30
derive_public_key 2c312ef971def53361274c37a90bfde86f959d877a636ea641a9c976ee80c7e3 121 b611ebd2bcfefc81cb772e35e3dd0204575cb0da644f68d4f9828a2683861e6c false
derive_public_key 6fa161dd958022caf185faf873dd9adbc5578352cda505e84fff7cc99a8762a7 333934910 c2b56e207862958751d49643f23079009092c32bf82179a1295e3b85a385c1c3 false
derive_public_key d85725562544e1984048391413a6112eb221bd217db3baaa9843bee331000e8e 21880446 f3148c3041e634d829e5df463ba3b1d64df282d620d63485e1f10024e003b939 false
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 0 76
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 1 d6
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 2 87
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 3 1b
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 12 d6
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 13 e9
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 14 12
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 15 26
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 127 2c
derive_view_tag 0fc47054f355ced4d67de73bfa12e4c78ff19089548fffa7d07a674741860f97 128 11
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 0 70
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 1 81
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 2 a0
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 3 ec
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 12 22
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 13 0a
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 14 87
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 15 76
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 127 4b
derive_view_tag a36ba7b4d31349ad278a6df8f77adb76748b59f4929348e67dd92adb9fa174dc 128 39
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 0 93
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 1 67
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 2 9d
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 3 2d
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 12 63
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 13 cf
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 14 ef
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 15 10
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 127 fc
derive_view_tag 7498d5bf0b69e08653f6d420a17f866dd2bd490ab43074f46065cb501fe7e2d8 128 73
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 0 90
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 1 5a
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 2 de
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 3 21
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 12 57
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 13 52
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 14 6f
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 15 eb
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 127 93
derive_view_tag fe7770c4b076e95ddb8026affcfab39d31c7c4a2266e0e25e343bc4badc907d0 128 dc
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 0 c6
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 1 60
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 2 f0
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 3 71
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 12 0e
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 13 42
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 14 b2
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 15 61
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 127 c3
derive_view_tag ea9337d0ddf480abdc4fc56a0cb223702729cb230ae7b9de50243ad25ce90e8d 128 0a
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 0 4c
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 1 9b
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 2 64
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 3 ff
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 12 e3
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 13 24
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 14 ea
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 15 3b
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 127 47
derive_view_tag 25d538315bcb81aff9574189ea65f418aeb0392f5cbbc84cd8a33c7ade31ef0a 128 5a
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 0 74
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 1 77
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 2 a9
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 3 44
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 12 75
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 13 05
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 14 ca
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 15 00
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 127 a6
derive_view_tag 8edfabada2b24ef4d8d915826c9ff0245910e4b835b59c2cf8ed8fc991b2e1e8 128 0d
+85
View File
@@ -0,0 +1,85 @@
/*
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
*
* 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/>.
*/
#include "common.h"
#include "util.h"
#include "gtest/gtest.h"
namespace p2pool {
TEST(varint, read_write)
{
std::vector<uint8_t> v;
v.reserve(16);
uint64_t check;
// 0...2^7 - 1
for (uint64_t value = 0; value < 0x80; ++value) {
v.clear();
writeVarint(value, v);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v[0], value);
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
ASSERT_EQ(check, value);
}
// 2^7...2^14 - 1
for (uint64_t value = 0x80; value < 0x4000; ++value) {
v.clear();
writeVarint(value, v);
ASSERT_EQ(v.size(), 2);
ASSERT_EQ(v[0], (value & 0x7F) | 0x80);
ASSERT_EQ(v[1], value >> 7);
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
ASSERT_EQ(check, value);
}
// 2^14...2^21 - 1
for (uint64_t value = 0x4000; value < 0x200000; ++value) {
v.clear();
writeVarint(value, v);
ASSERT_EQ(v.size(), 3);
ASSERT_EQ(v[0], (value & 0x7F) | 0x80);
ASSERT_EQ(v[1], ((value >> 7) & 0x7F) | 0x80);
ASSERT_EQ(v[2], value >> 14);
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
ASSERT_EQ(check, value);
}
// 2^64 - 1
v.clear();
writeVarint(std::numeric_limits<uint64_t>::max(), v);
ASSERT_EQ(v.size(), 10);
for (int i = 0; i < 9; ++i) {
ASSERT_EQ(v[i], 0xFF);
}
ASSERT_EQ(v[9], 1);
ASSERT_EQ(readVarint(v.data(), v.data() + v.size(), check), v.data() + v.size());
ASSERT_EQ(check, std::numeric_limits<uint64_t>::max());
// Invalid value 1
uint8_t buf[16];
memset(buf, -1, sizeof(buf));
ASSERT_EQ(readVarint(buf, buf + sizeof(buf), check), nullptr);
// Invalid value 2
uint8_t buf2[1] = { 0x80 };
ASSERT_EQ(readVarint(buf2, buf2 + 1, check), nullptr);
}
}