More fixes and refactoring

This commit is contained in:
SChernykh
2026-04-24 17:51:08 +02:00
parent e1acf7b786
commit fa53bdea55
8 changed files with 51 additions and 22 deletions
+3 -2
View File
@@ -1560,7 +1560,8 @@ void BlockTemplate::init_merge_mining_merkle_proof()
}
root_hash root;
if (!merkle_hash_with_proof(hashes, aux_slot, m_poolBlockTemplate->m_merkleProof, m_poolBlockTemplate->m_merkleProofPath, root)) {
uint32_t merkle_proof_path;
if (!merkle_hash_with_proof(hashes, aux_slot, m_poolBlockTemplate->m_merkleProof, merkle_proof_path, root)) {
LOGERR(1, "init_merge_mining_merkle_proof: merkle_hash_with_proof failed. Fix the code!");
return;
}
@@ -1577,7 +1578,7 @@ void BlockTemplate::init_merge_mining_merkle_proof()
return;
}
if ((proof != m_poolBlockTemplate->m_merkleProof) || (path != m_poolBlockTemplate->m_merkleProofPath)) {
if ((proof != m_poolBlockTemplate->m_merkleProof) || (path != merkle_proof_path)) {
LOGERR(1, "init_merge_mining_merkle_proof: merkle_hash_with_proof and get_merkle_proof returned different results. Fix the code!");
}
}
+4 -4
View File
@@ -125,14 +125,14 @@ void MergeMiningClientShared::on_external_block(const PoolBlock& block)
{
const std::vector<uint8_t>& v = i.second;
const uint8_t* p = v.data();
const uint8_t* e = v.data() + v.size();
if (p + HASH_SIZE > e) {
if (v.size() < HASH_SIZE) {
LOGWARN(3, "on_external_block: sanity check failed - invalid merge mining extra data " << '1');
return;
}
const uint8_t* p = v.data();
const uint8_t* e = v.data() + v.size();
memcpy(data.h, p, HASH_SIZE);
p += HASH_SIZE;
+7
View File
@@ -41,6 +41,13 @@ MergeMiningClientJSON_RPC::MergeMiningClientJSON_RPC(p2pool* pool, const std::st
const size_t k = host.find_last_of(':');
if (k != std::string::npos) {
m_host = host.substr(0, k);
// Handle IPv6 addresses
if ((m_host.length() > 2) && (m_host.find_first_of(':') != std::string::npos) && (m_host.front() == '[') && (m_host.back() == ']')) {
m_host.erase(m_host.begin());
m_host.pop_back();
}
m_port = static_cast<int32_t>(std::stol(host.substr(k + 1), nullptr, 10));
}
+12 -3
View File
@@ -94,15 +94,15 @@ MergeMiningClientTari::MergeMiningClientTari(p2pool* pool, std::string host, con
uv_mutex_init_checked(&m_workerLock);
uv_cond_init_checked(&m_workerCond);
uv_mutex_init_checked(&m_pushBlockLock);
uv_cond_init_checked(&m_pushBlockCond);
int err = uv_thread_create(&m_worker, run_wrapper, this);
if (err) {
LOGERR(1, "failed to start worker thread, error " << uv_err_name(err));
throw std::exception();
}
uv_mutex_init_checked(&m_pushBlockLock);
uv_cond_init_checked(&m_pushBlockCond);
std::ifstream f("tari_nodes.txt");
if (f.is_open()) {
@@ -851,6 +851,8 @@ void MergeMiningClientTari::push_blocks_to(const std::string& node_address)
LOGINFO(4, node_address << " is at block height " << tip_info.metadata().best_block_height());
}
std::string prev_header_hash;
while (!m_pushBlockStop) {
Block block;
{
@@ -865,6 +867,13 @@ void MergeMiningClientTari::push_blocks_to(const std::string& node_address)
}
const std::string& h = block.header().hash();
// Don't push the same block twice
if (h == prev_header_hash) {
continue;
}
prev_header_hash = h;
LOGINFO(4, "Pushing block " << log::hex_buf(h.data(), h.size()) << " to " << node_address);
grpc::ClientContext ctx;
+1 -3
View File
@@ -51,7 +51,6 @@ PoolBlock::PoolBlock()
, m_difficulty{}
, m_cumulativeDifficulty{}
, m_merkleProof{}
, m_merkleProofPath(0)
, m_mergeMiningExtra{}
, m_sidechainExtraBuf{}
, m_sidechainId{}
@@ -108,7 +107,6 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b)
m_difficulty = b.m_difficulty;
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;
@@ -200,7 +198,7 @@ std::vector<uint8_t> PoolBlock::serialize_mainchain_data(size_t* header_size, si
}
*(p++) = TX_EXTRA_NONCE;
*(p++) = static_cast<uint8_t>(extra_nonce_size);
writeVarint(extra_nonce_size, [&p](uint8_t value) { *(p++) = value; });
if (!extra_nonce) {
extra_nonce = &m_extraNonce;
+3 -1
View File
@@ -62,6 +62,9 @@ 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;
// Taken from Monero's cryptonote_config.h
static constexpr uint64_t CRYPTONOTE_MAX_BLOCK_NUMBER = 500000000ULL;
// 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;
@@ -140,7 +143,6 @@ struct PoolBlock
// Merkle proof for merge mining
std::vector<hash> m_merkleProof;
uint32_t m_merkleProofPath;
// Merge mining extra data
//
+15 -8
View File
@@ -79,11 +79,13 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
uint64_t unlock_height;
READ_VARINT(unlock_height);
if (unlock_height > CRYPTONOTE_MAX_BLOCK_NUMBER) return __LINE__;
EXPECT_BYTE(1);
EXPECT_BYTE(TXIN_GEN);
READ_VARINT(m_txinGenHeight);
if (m_txinGenHeight > CRYPTONOTE_MAX_BLOCK_NUMBER) return __LINE__;
if (m_majorVersion != sidechain.network_major_version(m_txinGenHeight)) return __LINE__;
if (unlock_height != m_txinGenHeight + MINER_REWARD_UNLOCK_TIME) return __LINE__;
@@ -99,8 +101,8 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
if (num_outputs > 0) {
// Outputs are in the buffer, just read them
// Each output is at least 34 bytes, exit early if there's not enough data left
// 1 byte for reward, 1 byte for tx_type, 32 bytes for eph_pub_key
constexpr uint64_t MIN_OUTPUT_SIZE = 34;
// 1 byte for reward, 1 byte for tx_type, 32 bytes for eph_pub_key, 1 byte for view_tag
constexpr uint64_t MIN_OUTPUT_SIZE = 35;
if (num_outputs > std::numeric_limits<uint64_t>::max() / MIN_OUTPUT_SIZE) return __LINE__;
if (static_cast<uint64_t>(data_end - data) < num_outputs * MIN_OUTPUT_SIZE) return __LINE__;
@@ -123,6 +125,8 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
}
t.m_reward = reward;
if (total_reward + reward < total_reward) return __LINE__;
total_reward += reward;
EXPECT_BYTE(TXOUT_TO_TAGGED_KEY);
@@ -269,6 +273,12 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
const int transactions_blob_size = static_cast<int>(num_transactions) * HASH_SIZE;
const int transactions_blob_size_diff = transactions_blob_size - transactions_actual_blob_size;
const int data_size = static_cast<int>((data_end - data_begin) + outputs_blob_size_diff + transactions_blob_size_diff);
if (data_size > static_cast<int>(MAX_BLOCK_SIZE)) {
return __LINE__;
}
#if POOL_BLOCK_DEBUG
m_mainChainDataDebug.reserve((data - data_begin) + outputs_blob_size_diff + transactions_blob_size_diff);
m_mainChainDataDebug.assign(data_begin, data_begin + outputs_offset);
@@ -413,9 +423,11 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
if (static_cast<uint64_t>(data_end - data) < n) return __LINE__;
std::vector<uint8_t> t;
t.resize(n);
if (n > 0) {
t.resize(n);
READ_BUF(t.data(), n);
}
m_mergeMiningExtra.emplace(chain_id, std::move(t));
}
@@ -449,11 +461,6 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
hash check;
const std::vector<uint8_t>& consensus_id = sidechain.consensus_id();
const int data_size = static_cast<int>((data_end - data_begin) + outputs_blob_size_diff + transactions_blob_size_diff);
if (data_size > static_cast<int>(MAX_BLOCK_SIZE)) {
return __LINE__;
}
keccak_custom(
[nonce_offset, extra_nonce_offset, mm_root_hash_offset, data_begin, data_size, &consensus_id, &outputs_blob, outputs_blob_size_diff, outputs_offset, outputs_blob_size, transactions_blob, transactions_blob_size_diff, transactions_offset, transactions_blob_size](int offset) -> uint8_t
+5
View File
@@ -38,6 +38,11 @@ TEST(hash, constructor)
for (uint8_t i = 0; i < HASH_SIZE; ++i) {
ASSERT_EQ(h2.h[i], i);
}
hash h3{ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" };
for (uint8_t i = 0; i < HASH_SIZE; ++i) {
ASSERT_EQ(h3.h[i], i);
}
}
TEST(hash, compare)