Skip to content

Commit

Permalink
Added merge mining extra
Browse files Browse the repository at this point in the history
For future merge mining needs
  • Loading branch information
SChernykh committed Jun 16, 2024
1 parent 180766f commit 93e1640
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/pool_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ PoolBlock::PoolBlock()
, m_cumulativeDifficulty{}
, m_merkleProof{}
, m_merkleProofPath(0)
, m_mergeMiningExtra{}
, m_sidechainExtraBuf{}
, m_sidechainId{}
, m_depth(0)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -273,6 +275,15 @@ std::vector<uint8_t> 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<const uint8_t*>(m_sidechainExtraBuf);
Expand Down
5 changes: 5 additions & 0 deletions src/pool_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ struct PoolBlock
std::vector<hash> 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<std::pair<hash, std::vector<uint8_t>>> m_mergeMiningExtra;

// Arbitrary extra data
uint32_t m_sidechainExtraBuf[4];

Expand Down
30 changes: 30 additions & 0 deletions src/pool_block_parser.inl
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ 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;
Expand All @@ -377,6 +378,35 @@ 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 > std::numeric_limits<uint64_t>::max() / (HASH_SIZE + 1)) return __LINE__;
if (static_cast<uint64_t>(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);

uint64_t n;
READ_VARINT(n);

// Sanity check
if (static_cast<uint64_t>(data_end - data) < n) return __LINE__;

std::vector<uint8_t> 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));
Expand Down

0 comments on commit 93e1640

Please sign in to comment.