Submit merge mined blocks asynchronously

This commit is contained in:
SChernykh 2024-05-20 15:02:59 +02:00
parent b0a63e2d34
commit 710f6c2eb7
4 changed files with 107 additions and 50 deletions

View file

@ -245,7 +245,7 @@ void Miner::run(WorkerData* data)
for (const AuxChainData& aux_data : j.m_auxChains) {
if (aux_data.difficulty.check_pow(h)) {
LOGINFO(0, log::Green() << "AUX BLOCK FOUND: chain_id " << aux_data.unique_id << ", diff " << j.m_auxDiff << ", worker thread " << data->m_index << '/' << data->m_count);
m_pool->submit_aux_block(aux_data.unique_id, j.m_templateId, j.m_nonce, j.m_extraNonce);
m_pool->submit_aux_block_async(aux_data.unique_id, j.m_templateId, j.m_nonce, j.m_extraNonce);
}
}
}

View file

@ -114,6 +114,13 @@ p2pool::p2pool(int argc, char* argv[])
}
m_submitBlockAsync.data = this;
err = uv_async_init(uv_default_loop_checked(), &m_submitAuxBlockAsync, on_submit_aux_block);
if (err) {
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
throw std::exception();
}
m_submitAuxBlockAsync.data = this;
err = uv_async_init(uv_default_loop_checked(), &m_blockTemplateAsync, on_update_block_template);
if (err) {
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
@ -145,6 +152,7 @@ p2pool::p2pool(int argc, char* argv[])
uv_mutex_init_checked(&m_minerLock);
#endif
uv_mutex_init_checked(&m_submitBlockDataLock);
uv_mutex_init_checked(&m_submitAuxBlockDataLock);
m_api = p->m_apiPath.empty() ? nullptr : new p2pool_api(p->m_apiPath, p->m_localStats);
@ -215,6 +223,7 @@ p2pool::~p2pool()
uv_mutex_destroy(&m_minerLock);
#endif
uv_mutex_destroy(&m_submitBlockDataLock);
uv_mutex_destroy(&m_submitAuxBlockDataLock);
delete m_api;
delete m_sideChain;
@ -627,69 +636,102 @@ void p2pool::submit_block_async(std::vector<uint8_t>&& blob)
}
}
void p2pool::submit_aux_block(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const
void p2pool::submit_aux_block_async(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce)
{
LOGINFO(3, "submit_aux_block: template id = " << template_id << ", chain_id = " << chain_id << ", nonce = " << nonce << ", extra_nonce = " << extra_nonce);
{
MutexLock lock(m_submitAuxBlockDataLock);
m_submitAuxBlockData.emplace_back(SubmitAuxBlockData{ chain_id, template_id, nonce, extra_nonce });
}
size_t nonce_offset = 0;
size_t extra_nonce_offset = 0;
size_t merkle_root_offset = 0;
root_hash merge_mining_root;
const BlockTemplate* block_tpl = nullptr;
std::vector<uint8_t> blob = m_blockTemplate->get_block_template_blob(template_id, extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, &block_tpl);
uint8_t hashing_blob[128] = {};
uint64_t height = 0;
difficulty_type diff, aux_diff, sidechain_diff;
hash seed_hash;
m_blockTemplate->get_hashing_blob(template_id, extra_nonce, hashing_blob, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset);
if (blob.empty()) {
LOGWARN(3, "submit_aux_block: block template blob not found");
// If p2pool is stopped, m_submitAuxBlockAsync is most likely already closed
if (m_stopped) {
LOGWARN(0, "p2pool is shutting down, but a block was found. Trying to submit it anyway!");
submit_aux_block();
return;
}
uint8_t* p = blob.data();
memcpy(p + nonce_offset, &nonce, NONCE_SIZE);
memcpy(p + extra_nonce_offset, &extra_nonce, EXTRA_NONCE_SIZE);
memcpy(p + merkle_root_offset, merge_mining_root.h, HASH_SIZE);
const int err = uv_async_send(&m_submitAuxBlockAsync);
if (err) {
LOGERR(1, "uv_async_send failed, error " << uv_err_name(err));
}
}
ReadLock lock(m_mergeMiningClientsLock);
void p2pool::submit_aux_block() const
{
std::vector<SubmitAuxBlockData> submit_data;
{
MutexLock lock(m_submitAuxBlockDataLock);
m_submitAuxBlockData.swap(submit_data);
}
IMergeMiningClient::ChainParameters params;
for (size_t i = 0; i < submit_data.size(); ++i) {
const hash chain_id = submit_data[i].chain_id;
const uint32_t template_id = submit_data[i].template_id;
const uint32_t nonce = submit_data[i].nonce;
const uint32_t extra_nonce = submit_data[i].extra_nonce;
for (IMergeMiningClient* c : m_mergeMiningClients) {
if (!c->get_params(params)) {
continue;
LOGINFO(3, "submit_aux_block: template id = " << template_id << ", chain_id = " << chain_id << ", nonce = " << nonce << ", extra_nonce = " << extra_nonce);
size_t nonce_offset = 0;
size_t extra_nonce_offset = 0;
size_t merkle_root_offset = 0;
root_hash merge_mining_root;
const BlockTemplate* block_tpl = nullptr;
std::vector<uint8_t> blob = m_blockTemplate->get_block_template_blob(template_id, extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, &block_tpl);
uint8_t hashing_blob[128] = {};
uint64_t height = 0;
difficulty_type diff, aux_diff, sidechain_diff;
hash seed_hash;
m_blockTemplate->get_hashing_blob(template_id, extra_nonce, hashing_blob, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset);
if (blob.empty()) {
LOGWARN(3, "submit_aux_block: block template blob not found");
return;
}
if (chain_id == params.aux_id) {
std::vector<hash> proof;
uint32_t path;
uint8_t* p = blob.data();
memcpy(p + nonce_offset, &nonce, NONCE_SIZE);
memcpy(p + extra_nonce_offset, &extra_nonce, EXTRA_NONCE_SIZE);
memcpy(p + merkle_root_offset, merge_mining_root.h, HASH_SIZE);
if (m_blockTemplate->get_aux_proof(template_id, extra_nonce, params.aux_hash, proof, path)) {
if (pool_block_debug()) {
const MinerData data = miner_data();
const uint32_t n_aux_chains = static_cast<uint32_t>(data.aux_chains.size() + 1);
const uint32_t index = get_aux_slot(params.aux_id, data.aux_nonce, n_aux_chains);
ReadLock lock(m_mergeMiningClientsLock);
if (!verify_merkle_proof(params.aux_hash, proof, index, n_aux_chains, merge_mining_root)) {
LOGERR(0, "submit_aux_block: verify_merkle_proof (1) failed for chain_id " << chain_id);
}
if (!verify_merkle_proof(params.aux_hash, proof, path, merge_mining_root)) {
LOGERR(0, "submit_aux_block: verify_merkle_proof (2) failed for chain_id " << chain_id);
IMergeMiningClient::ChainParameters params;
for (IMergeMiningClient* c : m_mergeMiningClients) {
if (!c->get_params(params)) {
continue;
}
if (chain_id == params.aux_id) {
std::vector<hash> proof;
uint32_t path;
if (m_blockTemplate->get_aux_proof(template_id, extra_nonce, params.aux_hash, proof, path)) {
if (pool_block_debug()) {
const MinerData data = miner_data();
const uint32_t n_aux_chains = static_cast<uint32_t>(data.aux_chains.size() + 1);
const uint32_t index = get_aux_slot(params.aux_id, data.aux_nonce, n_aux_chains);
if (!verify_merkle_proof(params.aux_hash, proof, index, n_aux_chains, merge_mining_root)) {
LOGERR(0, "submit_aux_block: verify_merkle_proof (1) failed for chain_id " << chain_id);
}
if (!verify_merkle_proof(params.aux_hash, proof, path, merge_mining_root)) {
LOGERR(0, "submit_aux_block: verify_merkle_proof (2) failed for chain_id " << chain_id);
}
}
c->submit_solution(block_tpl, hashing_blob, nonce_offset, seed_hash, blob, proof, path);
}
else {
LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id);
}
c->submit_solution(block_tpl, hashing_blob, nonce_offset, seed_hash, blob, proof, path);
return;
}
else {
LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id);
}
return;
}
}
}
@ -707,6 +749,7 @@ void p2pool::on_stop(uv_async_t* async)
}
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_submitBlockAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_submitAuxBlockAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_blockTemplateAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_stopAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&pool->m_reconnectToHostAsync), nullptr);

View file

@ -89,7 +89,7 @@ public:
void submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
void submit_block_async(std::vector<uint8_t>&& blob);
void submit_aux_block(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const;
void submit_aux_block_async(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
bool submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
@ -123,11 +123,13 @@ private:
const Params::Host& switch_host();
static void on_submit_block(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->submit_block(); }
static void on_submit_aux_block(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->submit_aux_block(); }
static void on_update_block_template(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->update_block_template(); }
static void on_stop(uv_async_t*);
static void on_reconnect_to_host(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->reconnect_to_host(); }
void submit_block() const;
void submit_aux_block() const;
std::atomic<bool> m_stopped;
@ -219,7 +221,19 @@ private:
mutable uv_mutex_t m_submitBlockDataLock;
SubmitBlockData m_submitBlockData;
struct SubmitAuxBlockData
{
hash chain_id;
uint32_t template_id = 0;
uint32_t nonce = 0;
uint32_t extra_nonce = 0;
};
mutable uv_mutex_t m_submitAuxBlockDataLock;
mutable std::vector<SubmitAuxBlockData> m_submitAuxBlockData;
uv_async_t m_submitBlockAsync;
uv_async_t m_submitAuxBlockAsync;
uv_async_t m_blockTemplateAsync;
uv_async_t m_stopAsync;

View file

@ -407,7 +407,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo
if (aux_data.difficulty.check_pow(resultHash)) {
const char* s = client->m_customUser;
LOGINFO(0, log::Green() << "client " << static_cast<char*>(client->m_addrString) << (*s ? " user " : "") << s << " found an aux block for chain_id " << aux_data.unique_id << ", diff " << aux_data.difficulty << ", submitting it");
m_pool->submit_aux_block(aux_data.unique_id, template_id, nonce, extra_nonce);
m_pool->submit_aux_block_async(aux_data.unique_id, template_id, nonce, extra_nonce);
}
}
}