From 8a27a8cce44787118c1c0d4786d828e6df82ce20 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 22 Nov 2022 15:23:32 +0100 Subject: [PATCH] Added a safety check to block template update --- src/block_template.cpp | 13 +++++++++++++ src/block_template.h | 1 + 2 files changed, 14 insertions(+) diff --git a/src/block_template.cpp b/src/block_template.cpp index 761b906..7249276 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -364,8 +364,14 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_numTransactionHashes = m_mempoolTxsOrder.size(); m_transactionHashes.assign(HASH_SIZE, 0); + m_transactionHashesSet.clear(); + m_transactionHashesSet.reserve(m_mempoolTxsOrder.size()); for (size_t i = 0; i < m_mempoolTxsOrder.size(); ++i) { const TxMempoolData& tx = m_mempoolTxs[m_mempoolTxsOrder[i]]; + if (!m_transactionHashesSet.insert(tx.id).second) { + LOGERR(1, "Added transaction " << tx.id << " twice. Fix the code!"); + continue; + } m_transactionHashes.insert(m_transactionHashes.end(), tx.id.h, tx.id.h + HASH_SIZE); final_fees += tx.fee; final_weight += tx.weight; @@ -445,8 +451,14 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_numTransactionHashes = m_mempoolTxsOrder.size(); m_transactionHashes.assign(HASH_SIZE, 0); + m_transactionHashesSet.clear(); + m_transactionHashesSet.reserve(m_mempoolTxsOrder.size()); for (size_t i = 0; i < m_mempoolTxsOrder.size(); ++i) { const TxMempoolData& tx = m_mempoolTxs[m_mempoolTxsOrder[i]]; + if (!m_transactionHashesSet.insert(tx.id).second) { + LOGERR(1, "Added transaction " << tx.id << " twice. Fix the code!"); + continue; + } m_transactionHashes.insert(m_transactionHashes.end(), tx.id.h, tx.id.h + HASH_SIZE); final_fees += tx.fee; final_weight += tx.weight; @@ -620,6 +632,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_blockHeader.clear(); m_minerTxExtra.clear(); m_transactionHashes.clear(); + m_transactionHashesSet.clear(); m_rewards.clear(); m_mempoolTxs.clear(); m_mempoolTxsOrder.clear(); diff --git a/src/block_template.h b/src/block_template.h index 8204a1f..630a757 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -103,6 +103,7 @@ private: std::vector m_blockHeader; std::vector m_minerTxExtra; std::vector m_transactionHashes; + unordered_set m_transactionHashesSet; std::vector m_rewards; std::vector m_mempoolTxs; std::vector m_mempoolTxsOrder;