From 5b3c1d749cf7244c18cb488ff8e1836015fe8439 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Sun, 16 Jun 2024 21:21:27 +0200 Subject: [PATCH] Added merge mining extra For future merge mining needs --- src/pool_block.cpp | 11 +++++++++++ src/pool_block.h | 9 +++++++++ src/pool_block_parser.inl | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/pool_block.cpp b/src/pool_block.cpp index c24dcc0..20c7990 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -50,6 +50,7 @@ PoolBlock::PoolBlock() , m_cumulativeDifficulty{} , m_merkleProof{} , m_merkleProofPath(0) + , m_mergeMiningExtra{} , m_sidechainExtraBuf{} , m_sidechainId{} , m_depth(0) @@ -105,6 +106,7 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b) m_cumulativeDifficulty = b.m_cumulativeDifficulty; m_merkleProof = b.m_merkleProof; m_merkleProofPath = b.m_merkleProofPath; + m_mergeMiningExtra = b.m_mergeMiningExtra; memcpy(m_sidechainExtraBuf, b.m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); m_sidechainId = b.m_sidechainId; m_depth = b.m_depth; @@ -273,6 +275,15 @@ std::vector PoolBlock::serialize_sidechain_data() const const hash& h = m_merkleProof[i]; data.insert(data.end(), h.h, h.h + HASH_SIZE); } + + writeVarint(m_mergeMiningExtra.size(), data); + + for (const auto& mm_extra_data : m_mergeMiningExtra) { + data.insert(data.end(), mm_extra_data.first.h, mm_extra_data.first.h + HASH_SIZE); + + writeVarint(mm_extra_data.second.size(), data); + data.insert(data.end(), mm_extra_data.second.begin(), mm_extra_data.second.end()); + } } const uint8_t* p = reinterpret_cast(m_sidechainExtraBuf); diff --git a/src/pool_block.h b/src/pool_block.h index c8c047a..d5b67a9 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -59,6 +59,10 @@ static constexpr difficulty_type MAX_CUMULATIVE_DIFFICULTY{ 13019633956666736640 // 1000 years at 1 block/second. It should be enough for any normal use. static constexpr uint64_t MAX_SIDECHAIN_HEIGHT = 31556952000ULL; +// Limited by the format of the Merkle tree parameters in tx_extra +static constexpr uint64_t MERGE_MINING_MAX_CHAINS = 256; +static constexpr uint64_t LOG2_MERGE_MINING_MAX_CHAINS = 8; + // TBD static constexpr uint64_t MERGE_MINING_FORK_TIME = std::numeric_limits::max(); @@ -141,6 +145,11 @@ struct PoolBlock std::vector m_merkleProof; uint32_t m_merkleProofPath; + // Merge mining extra data + // Format: vector of (chain ID, chain data) pairs + // Chain data format is arbitrary and depends on the merge mined chain's requirements + std::vector>> m_mergeMiningExtra; + // Arbitrary extra data uint32_t m_sidechainExtraBuf[4]; diff --git a/src/pool_block_parser.inl b/src/pool_block_parser.inl index d5a4a12..ddc7a7d 100644 --- a/src/pool_block_parser.inl +++ b/src/pool_block_parser.inl @@ -361,12 +361,13 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si } m_merkleProof.clear(); + m_mergeMiningExtra.clear(); if (merge_mining_enabled()) { uint8_t merkle_proof_size; READ_BYTE(merkle_proof_size); - if (merkle_proof_size > 8) { + if (merkle_proof_size > LOG2_MERGE_MINING_MAX_CHAINS) { return __LINE__; } @@ -377,6 +378,38 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si READ_BUF(h.h, HASH_SIZE); m_merkleProof.emplace_back(h); } + + uint64_t mm_extra_data_count; + READ_VARINT(mm_extra_data_count); + + if (mm_extra_data_count) { + // Sanity check + if (mm_extra_data_count > MERGE_MINING_MAX_CHAINS) return __LINE__; + if (static_cast(data_end - data) < mm_extra_data_count * (HASH_SIZE + 1)) return __LINE__; + + m_mergeMiningExtra.reserve(mm_extra_data_count); + + for (uint64_t i = 0; i < mm_extra_data_count; ++i) { + hash chain_id; + READ_BUF(chain_id.h, HASH_SIZE); + + // IDs must be ordered to avoid duplicates + if (i && !(m_mergeMiningExtra[i - 1].first < chain_id)) return __LINE__; + + uint64_t n; + READ_VARINT(n); + + // Sanity check + if (static_cast(data_end - data) < n) return __LINE__; + + std::vector t; + t.resize(n); + + READ_BUF(t.data(), n); + + m_mergeMiningExtra.emplace_back(chain_id, std::move(t)); + } + } } READ_BUF(m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf));