fix merge mining with more than one merge mined chain

reported by sech1
This commit is contained in:
Crypto City 2023-11-12 11:33:20 +00:00 committed by moneromooo-monero
parent ac02af9286
commit 53e632b435
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
8 changed files with 56 additions and 28 deletions

View file

@ -2082,11 +2082,12 @@ namespace cryptonote
}
crypto::hash merkle_root;
size_t merkle_tree_depth = 0;
std::vector<std::pair<crypto::hash, crypto::hash>> aux_pow;
std::vector<crypto::hash> aux_pow_raw;
std::vector<crypto::hash> aux_pow_id_raw;
aux_pow.reserve(req.aux_pow.size());
aux_pow_raw.reserve(req.aux_pow.size());
aux_pow_raw.resize(req.aux_pow.size());
aux_pow_id_raw.resize(req.aux_pow.size());
for (const auto &s: req.aux_pow)
{
aux_pow.push_back({});
@ -2102,7 +2103,6 @@ namespace cryptonote
error_resp.message = "Invalid aux pow hash";
return false;
}
aux_pow_raw.push_back(aux_pow.back().second);
}
size_t path_domain = 1;
@ -2111,10 +2111,13 @@ namespace cryptonote
uint32_t nonce;
const uint32_t max_nonce = 65535;
bool collision = true;
std::vector<uint32_t> slots(aux_pow.size());
for (nonce = 0; nonce <= max_nonce; ++nonce)
{
std::vector<bool> slots(aux_pow.size(), false);
std::vector<bool> slot_seen(aux_pow.size(), false);
collision = false;
for (size_t idx = 0; idx < aux_pow.size(); ++idx)
slots[idx] = 0xffffffff;
for (size_t idx = 0; idx < aux_pow.size(); ++idx)
{
const uint32_t slot = cryptonote::get_aux_slot(aux_pow[idx].first, nonce, aux_pow.size());
@ -2124,12 +2127,13 @@ namespace cryptonote
error_resp.message = "Computed slot is out of range";
return false;
}
if (slots[slot])
if (slot_seen[slot])
{
collision = true;
break;
}
slots[slot] = true;
slot_seen[slot] = true;
slots[idx] = slot;
}
if (!collision)
break;
@ -2141,6 +2145,19 @@ namespace cryptonote
return false;
}
// set the order determined above
for (size_t i = 0; i < aux_pow.size(); ++i)
{
if (slots[i] >= aux_pow.size())
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Slot value out of range";
return false;
}
aux_pow_raw[slots[i]] = aux_pow[i].second;
aux_pow_id_raw[slots[i]] = aux_pow[i].first;
}
crypto::tree_hash((const char(*)[crypto::HASH_SIZE])aux_pow_raw.data(), aux_pow_raw.size(), merkle_root.data);
res.merkle_root = epee::string_tools::pod_to_hex(merkle_root);
res.merkle_tree_depth = cryptonote::encode_mm_depth(aux_pow.size(), nonce);
@ -2167,7 +2184,7 @@ namespace cryptonote
error_resp.message = "Error removing existing merkle root";
return false;
}
if (!add_mm_merkle_root_to_tx_extra(b.miner_tx.extra, merkle_root, merkle_tree_depth))
if (!add_mm_merkle_root_to_tx_extra(b.miner_tx.extra, merkle_root, res.merkle_tree_depth))
{
error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
error_resp.message = "Error adding merkle root";
@ -2181,7 +2198,8 @@ namespace cryptonote
res.blocktemplate_blob = string_tools::buff_to_hex_nodelimer(block_blob);
res.blockhashing_blob = string_tools::buff_to_hex_nodelimer(hashing_blob);
res.aux_pow = req.aux_pow;
for (size_t i = 0; i < aux_pow_raw.size(); ++i)
res.aux_pow.push_back({epee::string_tools::pod_to_hex(aux_pow_id_raw[i]), epee::string_tools::pod_to_hex(aux_pow_raw[i])});
res.status = CORE_RPC_STATUS_OK;
return true;
}