Added a safety check to block template update

This commit is contained in:
SChernykh 2022-11-22 15:23:32 +01:00
parent 7eec4d742c
commit 8a27a8cce4
2 changed files with 14 additions and 0 deletions

View file

@ -364,8 +364,14 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_numTransactionHashes = m_mempoolTxsOrder.size(); m_numTransactionHashes = m_mempoolTxsOrder.size();
m_transactionHashes.assign(HASH_SIZE, 0); 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) { for (size_t i = 0; i < m_mempoolTxsOrder.size(); ++i) {
const TxMempoolData& tx = m_mempoolTxs[m_mempoolTxsOrder[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); m_transactionHashes.insert(m_transactionHashes.end(), tx.id.h, tx.id.h + HASH_SIZE);
final_fees += tx.fee; final_fees += tx.fee;
final_weight += tx.weight; final_weight += tx.weight;
@ -445,8 +451,14 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_numTransactionHashes = m_mempoolTxsOrder.size(); m_numTransactionHashes = m_mempoolTxsOrder.size();
m_transactionHashes.assign(HASH_SIZE, 0); 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) { for (size_t i = 0; i < m_mempoolTxsOrder.size(); ++i) {
const TxMempoolData& tx = m_mempoolTxs[m_mempoolTxsOrder[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); m_transactionHashes.insert(m_transactionHashes.end(), tx.id.h, tx.id.h + HASH_SIZE);
final_fees += tx.fee; final_fees += tx.fee;
final_weight += tx.weight; final_weight += tx.weight;
@ -620,6 +632,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_blockHeader.clear(); m_blockHeader.clear();
m_minerTxExtra.clear(); m_minerTxExtra.clear();
m_transactionHashes.clear(); m_transactionHashes.clear();
m_transactionHashesSet.clear();
m_rewards.clear(); m_rewards.clear();
m_mempoolTxs.clear(); m_mempoolTxs.clear();
m_mempoolTxsOrder.clear(); m_mempoolTxsOrder.clear();

View file

@ -103,6 +103,7 @@ private:
std::vector<uint8_t> m_blockHeader; std::vector<uint8_t> m_blockHeader;
std::vector<uint8_t> m_minerTxExtra; std::vector<uint8_t> m_minerTxExtra;
std::vector<uint8_t> m_transactionHashes; std::vector<uint8_t> m_transactionHashes;
unordered_set<hash> m_transactionHashesSet;
std::vector<uint64_t> m_rewards; std::vector<uint64_t> m_rewards;
std::vector<TxMempoolData> m_mempoolTxs; std::vector<TxMempoolData> m_mempoolTxs;
std::vector<int> m_mempoolTxsOrder; std::vector<int> m_mempoolTxsOrder;