Tari: serialize pow_data WIP

This commit is contained in:
SChernykh 2024-05-09 20:35:41 +02:00
parent 258c93e661
commit fd953a0b26
10 changed files with 62 additions and 17 deletions

View file

@ -1349,14 +1349,14 @@ bool BlockTemplate::get_aux_proof(const uint32_t template_id, uint32_t extra_non
return get_merkle_proof(tree, h, proof); return get_merkle_proof(tree, h, proof);
} }
std::vector<uint8_t> BlockTemplate::get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& merkle_root_offset, hash& merge_mining_root) const std::vector<uint8_t> BlockTemplate::get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& merkle_root_offset, hash& merge_mining_root, const BlockTemplate** pThis) const
{ {
ReadLock lock(m_lock); ReadLock lock(m_lock);
if (template_id != m_templateId) { if (template_id != m_templateId) {
const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)]; const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
if (old && (template_id == old->m_templateId)) { if (old && (template_id == old->m_templateId)) {
return old->get_block_template_blob(template_id, sidechain_extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root); return old->get_block_template_blob(template_id, sidechain_extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, pThis);
} }
nonce_offset = 0; nonce_offset = 0;
@ -1375,6 +1375,9 @@ std::vector<uint8_t> BlockTemplate::get_block_template_blob(uint32_t template_id
merge_mining_root = get_root_from_proof(sidechain_id, m_poolBlockTemplate->m_merkleProof, aux_slot, n_aux_chains); merge_mining_root = get_root_from_proof(sidechain_id, m_poolBlockTemplate->m_merkleProof, aux_slot, n_aux_chains);
merkle_root_offset = m_extraNonceOffsetInTemplate + m_poolBlockTemplate->m_extraNonceSize + 2 + m_poolBlockTemplate->m_merkleTreeDataSize; merkle_root_offset = m_extraNonceOffsetInTemplate + m_poolBlockTemplate->m_extraNonceSize + 2 + m_poolBlockTemplate->m_merkleTreeDataSize;
*pThis = this;
return m_blockTemplateBlob; return m_blockTemplateBlob;
} }

View file

@ -51,7 +51,7 @@ public:
std::vector<AuxChainData> get_aux_chains(const uint32_t template_id) const; std::vector<AuxChainData> get_aux_chains(const uint32_t template_id) const;
bool get_aux_proof(const uint32_t template_id, uint32_t extra_nonce, const hash& h, std::vector<hash>& proof) const; bool get_aux_proof(const uint32_t template_id, uint32_t extra_nonce, const hash& h, std::vector<hash>& proof) const;
std::vector<uint8_t> get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& merkle_root_offset, hash& merge_mining_root) const; std::vector<uint8_t> get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& merkle_root_offset, hash& merge_mining_root, const BlockTemplate** pThis) const;
FORCEINLINE uint64_t height() const { return m_height; } FORCEINLINE uint64_t height() const { return m_height; }
FORCEINLINE difficulty_type difficulty() const { return m_difficulty; } FORCEINLINE difficulty_type difficulty() const { return m_difficulty; }

View file

@ -20,6 +20,7 @@
namespace p2pool { namespace p2pool {
class p2pool; class p2pool;
class BlockTemplate;
class IMergeMiningClient class IMergeMiningClient
{ {
@ -37,7 +38,7 @@ public:
virtual ~IMergeMiningClient() {} virtual ~IMergeMiningClient() {}
[[nodiscard]] virtual bool get_params(ChainParameters& out_params) const = 0; [[nodiscard]] virtual bool get_params(ChainParameters& out_params) const = 0;
virtual void submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) = 0; virtual void submit_solution(const uint8_t (&hashing_blob)[128], size_t nonce_offset, const hash& seed_hash, const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) = 0;
}; };
} // namespace p2pool } // namespace p2pool

View file

@ -283,7 +283,7 @@ bool MergeMiningClientJSON_RPC::parse_merge_mining_get_job(const char* data, siz
return true; return true;
} }
void MergeMiningClientJSON_RPC::submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) void MergeMiningClientJSON_RPC::submit_solution(const uint8_t (&/*hashing_blob*/)[128], size_t /*nonce_offset*/, const hash& /*seed_hash*/, const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof)
{ {
ReadLock lock(m_lock); ReadLock lock(m_lock);

View file

@ -30,7 +30,7 @@ public:
~MergeMiningClientJSON_RPC() override; ~MergeMiningClientJSON_RPC() override;
bool get_params(ChainParameters& out_params) const override; bool get_params(ChainParameters& out_params) const override;
void submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) override; void submit_solution(const uint8_t (&hashing_blob)[128], size_t nonce_offset, const hash& seed_hash, const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) override;
private: private:
static void loop(void* data); static void loop(void* data);

View file

@ -20,6 +20,7 @@
#include "merge_mining_client_tari.h" #include "merge_mining_client_tari.h"
#include "p2pool.h" #include "p2pool.h"
#include "params.h" #include "params.h"
#include "block_template.h"
LOG_CATEGORY(MergeMiningClientTari) LOG_CATEGORY(MergeMiningClientTari)
@ -124,7 +125,7 @@ bool MergeMiningClientTari::get_params(ChainParameters& out_params) const
return true; return true;
} }
void MergeMiningClientTari::submit_solution(const std::vector<uint8_t>& /*blob*/, const std::vector<hash>& /*merkle_proof*/) void MergeMiningClientTari::submit_solution(const uint8_t (&hashing_blob)[128], size_t nonce_offset, const hash& seed_hash, const std::vector<uint8_t>& /*blob*/, const std::vector<hash>& /*merkle_proof*/)
{ {
Block block; Block block;
{ {
@ -135,9 +136,31 @@ void MergeMiningClientTari::submit_solution(const std::vector<uint8_t>& /*blob*/
ProofOfWork* pow = block.mutable_header()->mutable_pow(); ProofOfWork* pow = block.mutable_header()->mutable_pow();
pow->set_pow_algo(PowAlgo_PowAlgos_POW_ALGOS_RANDOMX); pow->set_pow_algo(PowAlgo_PowAlgos_POW_ALGOS_RANDOMX);
// TODO fill in the data {
std::string data; std::string data;
// Monero header + nonce
data.append(reinterpret_cast<const char*>(hashing_blob), nonce_offset + sizeof(uint32_t));
// Monero seed
data.append(1, HASH_SIZE);
data.append(reinterpret_cast<const char*>(seed_hash.h), HASH_SIZE);
uint64_t transaction_count;
if (!readVarint(hashing_blob + nonce_offset + sizeof(uint32_t) + HASH_SIZE, hashing_blob + 128, transaction_count)) {
return;
}
// Total number of transactions in this block (including the miner tx)
data.append(reinterpret_cast<const char*>(&transaction_count), 2);
// Tx Merkle tree root
data.append(reinterpret_cast<const char*>(hashing_blob + nonce_offset + sizeof(uint32_t)), HASH_SIZE);
// TODO: serialize coinbase_merkle_proof, coinbase_tx_extra, coinbase_tx_hasher, aux_chain_merkle_proof
pow->set_pow_data(data); pow->set_pow_data(data);
}
grpc::ClientContext ctx; grpc::ClientContext ctx;
SubmitBlockResponse response; SubmitBlockResponse response;

View file

@ -31,7 +31,7 @@ public:
~MergeMiningClientTari() override; ~MergeMiningClientTari() override;
bool get_params(ChainParameters& out_params) const override; bool get_params(ChainParameters& out_params) const override;
void submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) override; void submit_solution(const uint8_t (&hashing_blob)[128], size_t nonce_offset, const hash& seed_hash, const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) override;
static constexpr char TARI_PREFIX[] = "tari://"; static constexpr char TARI_PREFIX[] = "tari://";

View file

@ -635,8 +635,16 @@ void p2pool::submit_aux_block(const hash& chain_id, uint32_t template_id, uint32
size_t extra_nonce_offset = 0; size_t extra_nonce_offset = 0;
size_t merkle_root_offset = 0; size_t merkle_root_offset = 0;
root_hash merge_mining_root; root_hash merge_mining_root;
const BlockTemplate* block_template = 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); 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_template);
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()) { if (blob.empty()) {
LOGWARN(3, "submit_aux_block: block template blob not found"); LOGWARN(3, "submit_aux_block: block template blob not found");
@ -671,7 +679,7 @@ void p2pool::submit_aux_block(const hash& chain_id, uint32_t template_id, uint32
} }
} }
c->submit_solution(blob, proof); c->submit_solution(hashing_blob, nonce_offset, seed_hash, blob, proof);
} }
else { else {
LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id); LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id);
@ -721,10 +729,12 @@ void p2pool::submit_block() const
size_t extra_nonce_offset = 0; size_t extra_nonce_offset = 0;
size_t merkle_root_offset = 0; size_t merkle_root_offset = 0;
hash merge_mining_root; hash merge_mining_root;
const BlockTemplate* block_template = nullptr;
bool is_external = false; bool is_external = false;
if (submit_data.blob.empty()) { if (submit_data.blob.empty()) {
submit_data.blob = m_blockTemplate->get_block_template_blob(submit_data.template_id, submit_data.extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root); submit_data.blob = m_blockTemplate->get_block_template_blob(submit_data.template_id, submit_data.extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, &block_template);
LOGINFO(0, log::LightGreen() << "submit_block: height = " << height LOGINFO(0, log::LightGreen() << "submit_block: height = " << height
<< ", template id = " << submit_data.template_id << ", template id = " << submit_data.template_id

View file

@ -61,10 +61,10 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
#define READ_BUF(buf, size) do { if (!read_buf((buf), (size))) return __LINE__; } while(0) #define READ_BUF(buf, size) do { if (!read_buf((buf), (size))) return __LINE__; } while(0)
READ_BYTE(m_majorVersion); READ_VARINT(m_majorVersion);
if (m_majorVersion > HARDFORK_SUPPORTED_VERSION) return __LINE__; if (m_majorVersion > HARDFORK_SUPPORTED_VERSION) return __LINE__;
READ_BYTE(m_minorVersion); READ_VARINT(m_minorVersion);
if (m_minorVersion < m_majorVersion) return __LINE__; if (m_minorVersion < m_majorVersion) return __LINE__;
READ_VARINT(m_timestamp); READ_VARINT(m_timestamp);

View file

@ -167,10 +167,18 @@ const uint8_t* readVarint(const uint8_t* data, const uint8_t* data_end, T& b)
const uint64_t cur_byte = *(data++); const uint64_t cur_byte = *(data++);
result |= (cur_byte & 0x7F) << k; result |= (cur_byte & 0x7F) << k;
if (shiftleft128(cur_byte & 0x7F, 0, k) != 0) {
return nullptr;
}
k += 7; k += 7;
if ((cur_byte & 0x80) == 0) { if ((cur_byte & 0x80) == 0) {
b = result; if (result > std::numeric_limits<T>::max()) {
return nullptr;
}
b = static_cast<T>(result);
return data; return data;
} }
} }