Skip to content

Commit

Permalink
Tari: serialize pow_data WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed May 9, 2024
1 parent 258c93e commit fd953a0
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/block_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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);

if (template_id != m_templateId) {
const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)];
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;
Expand All @@ -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);

merkle_root_offset = m_extraNonceOffsetInTemplate + m_poolBlockTemplate->m_extraNonceSize + 2 + m_poolBlockTemplate->m_merkleTreeDataSize;

*pThis = this;

return m_blockTemplateBlob;
}

Expand Down
2 changes: 1 addition & 1 deletion src/block_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BlockTemplate
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;

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 difficulty_type difficulty() const { return m_difficulty; }
Expand Down
3 changes: 2 additions & 1 deletion src/merge_mining_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace p2pool {

class p2pool;
class BlockTemplate;

class IMergeMiningClient
{
Expand All @@ -37,7 +38,7 @@ class IMergeMiningClient
virtual ~IMergeMiningClient() {}

[[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
2 changes: 1 addition & 1 deletion src/merge_mining_client_json_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ bool MergeMiningClientJSON_RPC::parse_merge_mining_get_job(const char* data, siz
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);

Expand Down
2 changes: 1 addition & 1 deletion src/merge_mining_client_json_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MergeMiningClientJSON_RPC : public IMergeMiningClient
~MergeMiningClientJSON_RPC() 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:
static void loop(void* data);
Expand Down
31 changes: 27 additions & 4 deletions src/merge_mining_client_tari.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "merge_mining_client_tari.h"
#include "p2pool.h"
#include "params.h"
#include "block_template.h"

LOG_CATEGORY(MergeMiningClientTari)

Expand Down Expand Up @@ -124,7 +125,7 @@ bool MergeMiningClientTari::get_params(ChainParameters& out_params) const
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;
{
Expand All @@ -135,9 +136,31 @@ void MergeMiningClientTari::submit_solution(const std::vector<uint8_t>& /*blob*/
ProofOfWork* pow = block.mutable_header()->mutable_pow();
pow->set_pow_algo(PowAlgo_PowAlgos_POW_ALGOS_RANDOMX);

// TODO fill in the data
std::string data;
pow->set_pow_data(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);
}

grpc::ClientContext ctx;
SubmitBlockResponse response;
Expand Down
2 changes: 1 addition & 1 deletion src/merge_mining_client_tari.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MergeMiningClientTari : public IMergeMiningClient, public nocopy_nomove
~MergeMiningClientTari() 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://";

Expand Down
16 changes: 13 additions & 3 deletions src/p2pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 merkle_root_offset = 0;
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()) {
LOGWARN(3, "submit_aux_block: block template blob not found");
Expand Down Expand Up @@ -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 {
LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id);
Expand Down Expand Up @@ -721,10 +729,12 @@ void p2pool::submit_block() const
size_t extra_nonce_offset = 0;
size_t merkle_root_offset = 0;
hash merge_mining_root;
const BlockTemplate* block_template = nullptr;

bool is_external = false;

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
<< ", template id = " << submit_data.template_id
Expand Down
4 changes: 2 additions & 2 deletions src/pool_block_parser.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

READ_BYTE(m_majorVersion);
READ_VARINT(m_majorVersion);
if (m_majorVersion > HARDFORK_SUPPORTED_VERSION) return __LINE__;

READ_BYTE(m_minorVersion);
READ_VARINT(m_minorVersion);
if (m_minorVersion < m_majorVersion) return __LINE__;

READ_VARINT(m_timestamp);
Expand Down
10 changes: 9 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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++);
result |= (cur_byte & 0x7F) << k;

if (shiftleft128(cur_byte & 0x7F, 0, k) != 0) {
return nullptr;
}

k += 7;

if ((cur_byte & 0x80) == 0) {
b = result;
if (result > std::numeric_limits<T>::max()) {
return nullptr;
}
b = static_cast<T>(result);
return data;
}
}
Expand Down

0 comments on commit fd953a0

Please sign in to comment.