From 9266f28d2253a8a782e5d9fca2c50f58373af20d Mon Sep 17 00:00:00 2001 From: Matt Hess Date: Wed, 17 Dec 2025 22:53:56 +0000 Subject: [PATCH] fix one critical bug and mostly code quality/cleanup items for portability, minor cleanup for compile on mac, fixed null check, Remove redundant TX_VERSION check --- src/block_template.cpp | 18 +++--------------- src/block_template.h | 1 - src/pool_block.cpp | 1 + src/pool_block_parser.inl | 4 ++-- src/side_chain.cpp | 9 ++------- 5 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index 99dd59f..b5f177c 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -51,6 +51,7 @@ BlockTemplate::BlockTemplate(SideChain* sidechain, RandomX_Hasher_Base* hasher) , m_numTransactionHashes(0) , m_prevId{} , m_height(0) + , m_majorVersion(0) , m_difficulty{} , m_auxDifficulty{} , m_seedHash{} @@ -59,6 +60,7 @@ BlockTemplate::BlockTemplate(SideChain* sidechain, RandomX_Hasher_Base* hasher) , m_poolBlockTemplate(new PoolBlock()) , m_finalReward(0) , m_minerTxKeccakState{} + , m_minerTxPrefixSize(0) , m_minerTxKeccakStateInputLength(0) , m_sidechainHashKeccakState{} , m_sidechainHashInputLength(0) @@ -733,7 +735,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const std::vector v; v.reserve(HASH_SIZE + 16); - v.assign(c.data.h, c.data.h + HASH_SIZE * 2); + v.assign(c.data.h, c.data.h + HASH_SIZE); writeVarint(c.difficulty.lo, v); writeVarint(c.difficulty.hi, v); @@ -1845,20 +1847,6 @@ bool BlockTemplate::submit_sidechain_block(uint32_t template_id, uint32_t nonce, return false; } -hash BlockTemplate::calc_tx_merkle_root(uint32_t extra_nonce) const -{ - const hash miner_hash = calc_miner_tx_hash(extra_nonce); - const uint8_t* protocol_hash_ptr = m_transactionHashes.data() + HASH_SIZE; - - uint8_t combined[HASH_SIZE * 2]; - memcpy(combined, miner_hash.h, HASH_SIZE); - memcpy(combined + HASH_SIZE, protocol_hash_ptr, HASH_SIZE); - - hash result; - keccak(combined, HASH_SIZE * 2, result.h); - return result; -} - void BlockTemplate::init_merge_mining_merkle_proof() { const uint32_t n_aux_chains = static_cast(m_poolBlockTemplate->m_auxChains.size() + 1); diff --git a/src/block_template.h b/src/block_template.h index 65e797b..769f112 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -78,7 +78,6 @@ private: int create_miner_tx(const MinerData& data, const std::vector& shares, uint64_t max_reward_amounts_weight, bool dry_run, uint64_t full_block_reward); hash calc_sidechain_hash(uint32_t sidechain_extra_nonce) const; hash calc_miner_tx_hash(uint32_t extra_nonce) const; - hash calc_tx_merkle_root(uint32_t extra_nonce) const; void calc_merkle_tree_main_branch(); hash m_protocolTxHash; diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 6bf9f3f..b6e8df7 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -40,6 +40,7 @@ PoolBlock::PoolBlock() , m_prevId{} , m_nonce(0) , m_txinGenHeight(0) + , m_amountBurnt(0) , m_txkeyPub{} , m_extraNonceSize(0) , m_extraNonce(0) diff --git a/src/pool_block_parser.inl b/src/pool_block_parser.inl index 2a518be..762fd44 100644 --- a/src/pool_block_parser.inl +++ b/src/pool_block_parser.inl @@ -75,10 +75,10 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si const int nonce_offset = static_cast(data - data_begin); READ_BUF(&m_nonce, NONCE_SIZE); - // Accept both old (4) and new (60) TX_VERSION during transition + // Verify TX_VERSION (currently 4) uint8_t tx_version; READ_BYTE(tx_version); - if (tx_version != 4 && tx_version != TX_VERSION) return __LINE__; + if (tx_version != TX_VERSION) return __LINE__; uint64_t unlock_height; READ_VARINT(unlock_height); diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 3e12e43..cfa124e 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -958,7 +958,8 @@ bool SideChain::get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::v const PoolBlock::TxOutput& output = block->m_outputAmounts[i]; writeVarint(output.m_reward, blob); blob.emplace_back(TXOUT_TO_CARROT_V1); - blob.insert(blob.end(), block->m_ephPublicKeys[i].h, block->m_ephPublicKeys[i].h + HASH_SIZE); + const hash eph_key = block->m_ephPublicKeys[i]; + blob.insert(blob.end(), eph_key.h, eph_key.h + HASH_SIZE); // Carrot v1 format blob.push_back(4); blob.push_back('S'); @@ -3458,12 +3459,6 @@ bool SideChain::validate_loaded_checkpoints() PoolBlock* block = const_cast(found); - if (!block) { - // Block not yet synced but reachable - can't validate yet - LOGWARN(3, "Checkpoint at height " << cp.height << " not yet synced, deferring validation - mining blocked"); - return false; // NOT safe to mine yet - } - if (block->m_sidechainId != cp.id) { LOGWARN(0, "CHECKPOINT MISMATCH at height " << cp.height << ": cached=" << cp.id <<