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 5b3c1d7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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
9 changes: 9 additions & 0 deletions src/pool_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>::max();

Expand Down Expand Up @@ -141,6 +145,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
35 changes: 34 additions & 1 deletion src/pool_block_parser.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
}

Expand All @@ -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<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);

// 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<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 5b3c1d7

Please sign in to comment.