Skip to content

Commit

Permalink
Added command line for merge mining
Browse files Browse the repository at this point in the history
Start a merge mining client for each of the aux chains
  • Loading branch information
SChernykh committed Nov 10, 2023
1 parent abd9c80 commit e6b8292
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 25 deletions.
7 changes: 7 additions & 0 deletions docs/COMMAND_LINE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
--no-upnp Disable UPnP port forwarding
--no-igd An alias for --no-upnp
--upnp-stratum Port forward Stratum port (it's not forwarded by default)
--merge-mine IP:port and wallet address for another blockchain to merge mine with
--version Print p2pool's version and build details
```

Expand All @@ -50,3 +51,9 @@ In this example, you have local Monero host running on ports 18081/18083 (RPC/zm
```
p2pool.exe --host 127.0.0.1 --host xmr1.rs.me --rpc-port 18089 --zmq-port 18084 --host xmr2.rs.me --host xmr3.rs.me --wallet YOUR_WALLET_ADDRESS
```

### Merge mining

```
p2pool.exe --wallet YOUR_WALLET_ADDRESS --merge-mine IP:port YOUR_WALLET_ADDRESS_ON_ANOTHER_BLOCKCHAIN
```
2 changes: 2 additions & 0 deletions docs/MERGE_MINING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Example response 2: `{"jsonrpc":"2.0","id":"0","result":{}}`

### merge_mining_submit_solution

Example request: `{"jsonrpc":"2.0","id":"0","method":"merge_mining_submit_solution","params":{"aux_blob":"4c6f72656d20697073756d","aux_hash":"f6952d6eef555ddd87aca66e56b91530222d6e318414816f3ba7cf5bf694bf0f","blob":"...","merkle_proof":["hash1","hash2","hash3"]}}`

Request: a JSON containing these fields:
Field|Description
-|-
Expand Down
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ struct TxMempoolData

struct AuxChainData
{
FORCEINLINE AuxChainData(const hash& _id, const hash& _data, const difficulty_type& _difficulty) : unique_id(_id), data(_data), difficulty(_difficulty) {}

hash unique_id;
hash data;
difficulty_type difficulty;
Expand Down
12 changes: 0 additions & 12 deletions src/keccak.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ FORCEINLINE void keccak(const uint8_t* in, int inlen, uint8_t (&md)[N])
template<typename T>
FORCEINLINE void keccak_custom(T&& in, int inlen, uint8_t* md, int mdlen)
{
// TODO: remove after testing
#if 0
if (inlen > 100) {
printf("\nkeccak_custom %d", inlen);
for (int i = 0; i < inlen; ++i) {
if ((i & 31) == 0) printf("\n");
printf("%02X ", static_cast<uint8_t>(in(i)));
}
printf("\n");
}
#endif

uint64_t st[25] = {};

const int rsiz = sizeof(st) == mdlen ? KeccakParams::HASH_DATA_AREA : 200 - 2 * mdlen;
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void p2pool_usage()
"--no-igd An alias for --no-upnp\n"
"--upnp-stratum Port forward Stratum port (it's not forwarded by default)\n"
#endif
"--merge-mine IP:port and wallet address for another blockchain to merge mine with"
"--version Print p2pool's version and build details\n"
"--help Show this help message\n\n"
"Example command line:\n\n"
Expand Down
12 changes: 6 additions & 6 deletions src/merge_mining_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ LOG_CATEGORY(MergeMiningClient)

namespace p2pool {

MergeMiningClient::MergeMiningClient(p2pool* pool, const std::string& host, const std::string& address)
MergeMiningClient::MergeMiningClient(p2pool* pool, const std::string& host, const std::string& wallet)
: m_host(host)
, m_port(80)
, m_auxAddress(address)
, m_auxWallet(wallet)
, m_ping(0.0)
, m_pool(pool)
, m_loop{}
Expand All @@ -42,7 +42,7 @@ MergeMiningClient::MergeMiningClient(p2pool* pool, const std::string& host, cons
const size_t k = host.find_last_of(':');
if (k != std::string::npos) {
m_host = host.substr(0, k);
m_port = strtoul(host.substr(k + 1).c_str(), nullptr, 10);
m_port = std::stoul(host.substr(k + 1), nullptr, 10);
}

if (m_host.empty() || (m_port == 0) || (m_port >= 65536)) {
Expand Down Expand Up @@ -95,7 +95,7 @@ MergeMiningClient::~MergeMiningClient()
void MergeMiningClient::on_timer()
{
MinerData data = m_pool->miner_data();
merge_mining_get_job(data.height, data.prev_id, m_auxAddress, m_auxHash);
merge_mining_get_job(data.height, data.prev_id, m_auxWallet, m_auxHash);
}

void MergeMiningClient::merge_mining_get_chain_id()
Expand Down Expand Up @@ -157,7 +157,7 @@ bool MergeMiningClient::parse_merge_mining_get_chain_id(const char* data, size_t
return true;
}

void MergeMiningClient::merge_mining_get_job(uint64_t height, const hash& prev_id, const std::string& address, const hash& aux_hash)
void MergeMiningClient::merge_mining_get_job(uint64_t height, const hash& prev_id, const std::string& wallet, const hash& aux_hash)
{
if (m_getJobRunning) {
return;
Expand All @@ -169,7 +169,7 @@ void MergeMiningClient::merge_mining_get_job(uint64_t height, const hash& prev_i
log::Stream s(buf);

s << "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"merge_mining_get_job\",\"params\":{"
<< "\"address\":\"" << address << '"'
<< "\"address\":\"" << wallet << '"'
<< ",\"aux_hash\":\"" << aux_hash << '"'
<< ",\"height\":" << height
<< ",\"prev_id\":\"" << prev_id << '"'
Expand Down
10 changes: 7 additions & 3 deletions src/merge_mining_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class p2pool;
class MergeMiningClient
{
public:
MergeMiningClient(p2pool* pool, const std::string& host, const std::string& address);
MergeMiningClient(p2pool* pool, const std::string& host, const std::string& wallet);
~MergeMiningClient();

void merge_mining_submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof);

FORCEINLINE const hash& aux_id() const { return m_chainID; }
FORCEINLINE const hash& aux_data() const { return m_auxHash; }
FORCEINLINE const difficulty_type& aux_diff() const { return m_auxDiff; }

private:
static void loop(void* data);

Expand All @@ -40,15 +44,15 @@ class MergeMiningClient
void merge_mining_get_chain_id();
bool parse_merge_mining_get_chain_id(const char* data, size_t size);

void merge_mining_get_job(uint64_t height, const hash& prev_id, const std::string& address, const hash& aux_hash);
void merge_mining_get_job(uint64_t height, const hash& prev_id, const std::string& wallet, const hash& aux_hash);
bool parse_merge_mining_get_job(const char* data, size_t size);

bool parse_merge_mining_submit_solution(const char* data, size_t size);

std::string m_host;
uint32_t m_port;

std::string m_auxAddress;
std::string m_auxWallet;
std::vector<uint8_t> m_auxBlob;
hash m_auxHash;
difficulty_type m_auxDiff;
Expand Down
34 changes: 31 additions & 3 deletions src/p2pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "pool_block.h"
#include "keccak.h"
#include "merkle.h"
#include "merge_mining_client.h"
#include <thread>
#include <fstream>
#include <numeric>
Expand Down Expand Up @@ -186,6 +187,11 @@ p2pool::~p2pool()
}
#endif

for (const MergeMiningClient* c : m_mergeMiningClients) {
delete c;
}
m_mergeMiningClients.clear();

uv_rwlock_destroy(&m_mainchainLock);
uv_rwlock_destroy(&m_minerDataLock);
uv_rwlock_destroy(&m_ZMQReaderLock);
Expand Down Expand Up @@ -334,6 +340,25 @@ void p2pool::handle_miner_data(MinerData& data)
cleanup_mainchain_data(data.height);
}

data.aux_chains.clear();

if (!m_mergeMiningClients.empty()) {
data.aux_chains.reserve(m_mergeMiningClients.size());

std::vector<hash> tmp;
tmp.reserve(m_mergeMiningClients.size());

for (const MergeMiningClient* c : m_mergeMiningClients) {
data.aux_chains.emplace_back(c->aux_id(), c->aux_data(), c->aux_diff());
tmp.emplace_back(c->aux_data());
}

if (!find_aux_nonce(tmp, data.aux_nonce)) {
LOGERR(1, "Failed to find the aux nonce for merge mining. Merge mining will be off this round.");
data.aux_chains.clear();
}
}

// TODO: remove after testing
#if 0
{
Expand Down Expand Up @@ -840,6 +865,10 @@ void p2pool::download_block_headers(uint64_t current_height)
}
}

for (const auto& h : m_params->m_mergeMiningHosts) {
m_mergeMiningClients.push_back(new MergeMiningClient(this, h.m_host, h.m_wallet));
}

m_startupFinished = true;
}
}
Expand Down Expand Up @@ -1136,11 +1165,10 @@ void p2pool::get_miner_data(bool retry)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
m_getMinerDataPending = false;
get_miner_data();
return;
}
}
else {
m_getMinerDataPending = false;
}
m_getMinerDataPending = false;
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/p2pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Miner;
class ConsoleCommands;
class p2pool_api;
class ZMQReader;
class MergeMiningClient;
struct PoolBlock;

class p2pool : public MinerCallbackHandler, public nocopy_nomove
Expand Down Expand Up @@ -226,6 +227,8 @@ class p2pool : public MinerCallbackHandler, public nocopy_nomove
mutable uv_rwlock_t m_ZMQReaderLock;
ZMQReader* m_ZMQReader = nullptr;

std::vector<MergeMiningClient*> m_mergeMiningClients;

hash m_getMinerDataHash;
bool m_getMinerDataPending = false;

Expand Down
6 changes: 6 additions & 0 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ Params::Params(int argc, char* const argv[])
}
#endif

if ((strcmp(argv[i], "--merge-mine") == 0) && (i + 2 < argc)) {
m_mergeMiningHosts.emplace_back(argv[i + 1], argv[i + 2]);
i += 2;
ok = true;
}

if (!ok) {
fprintf(stderr, "Unknown command line parameter %s\n\n", argv[i]);
p2pool_usage();
Expand Down
10 changes: 10 additions & 0 deletions src/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ struct Params

std::vector<Host> m_hosts;

struct MergeMiningHost
{
MergeMiningHost(const char* host, const char* wallet) : m_host(host), m_wallet(wallet) {}

std::string m_host;
std::string m_wallet;
};

std::vector<MergeMiningHost> m_mergeMiningHosts;

bool m_lightMode = false;
Wallet m_wallet{ nullptr };
std::string m_stratumAddresses;
Expand Down
2 changes: 1 addition & 1 deletion src/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void TCPServer::parse_address_list_internal(const std::string& address_list, Cal
}
}

const int port = strtol(address.substr(k2 + 1).c_str(), nullptr, 10);
const uint32_t port = std::stoul(address.substr(k2 + 1), nullptr, 10);
if ((port > 0) && (port < 65536)) {
callback(is_v6, address, ip, port);
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(SOURCES
../src/log.cpp
../src/memory_leak_debug.cpp
../src/mempool.cpp
../src/merge_mining_client.cpp
../src/merkle.cpp
../src/miner.cpp
../src/p2p_server.cpp
Expand Down

0 comments on commit e6b8292

Please sign in to comment.