From 8aa9e54dc442cf12ade70b1268b5ccfcc374d48a Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 14 Sep 2021 12:54:42 +0200 Subject: [PATCH] BlockTemplate: wait for 10 seconds before including new transactions --- src/block_template.cpp | 21 ++++++++++++++++++--- src/common.h | 3 ++- src/mempool.cpp | 29 +++++++++++++++++++++-------- src/mempool.h | 3 ++- src/zmq_reader.cpp | 2 ++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index bfc7428..0115ee7 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -194,9 +194,22 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_difficulty = data.difficulty; m_seedHash = data.seed_hash; + const time_t cur_time = time(nullptr); + + // Only choose transactions that were received 10 or more seconds ago + size_t total_mempool_transactions; { + m_mempoolTxs.clear(); + ReadLock mempool_lock(mempool.m_lock); - m_mempoolTxs = mempool.m_transactions; + + total_mempool_transactions = mempool.m_transactions.size(); + + for (auto& it : mempool.m_transactions) { + if (cur_time >= it.second.time_received + 10) { + m_mempoolTxs.emplace_back(it.second); + } + } } // Safeguard for busy mempool moments @@ -211,6 +224,8 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_mempoolTxs.resize(1000); } + LOGINFO(4, "mempool has " << total_mempool_transactions << " transactions, taking " << m_mempoolTxs.size() << " transactions from it"); + const uint64_t base_reward = get_base_reward(data.already_generated_coins); uint64_t total_tx_fees = 0; @@ -223,7 +238,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet const uint64_t max_reward = base_reward + total_tx_fees; LOGINFO(3, "base reward = " << log::Gray() << log::XMRAmount(base_reward) << log::NoColor() << - ", mempool: " << log::Gray() << m_mempoolTxs.size() << log::NoColor() << + ", " << log::Gray() << m_mempoolTxs.size() << log::NoColor() << " transactions, fees = " << log::Gray() << log::XMRAmount(total_tx_fees) << log::NoColor() << ", weight = " << log::Gray() << total_tx_weight); @@ -237,7 +252,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_poolBlockTemplate->m_minorVersion = HARDFORK_SUPPORTED_VERSION; // Timestamp - m_timestamp = time(nullptr); + m_timestamp = cur_time; if (m_timestamp <= data.median_timestamp) { LOGWARN(2, "timestamp adjusted from " << m_timestamp << " to " << data.median_timestamp + 1 << ". Fix your system time!"); m_timestamp = data.median_timestamp + 1; diff --git a/src/common.h b/src/common.h index 5077550..20234ca 100644 --- a/src/common.h +++ b/src/common.h @@ -226,12 +226,13 @@ difficulty_type operator+(const difficulty_type& a, const difficulty_type& b); struct TxMempoolData { - FORCEINLINE TxMempoolData() : id(), blob_size(0), weight(0), fee(0) {} + FORCEINLINE TxMempoolData() : id(), blob_size(0), weight(0), fee(0), time_received(0) {} hash id; uint64_t blob_size; uint64_t weight; uint64_t fee; + time_t time_received; }; struct MinerData diff --git a/src/mempool.cpp b/src/mempool.cpp index 5ba4f3f..5cad682 100644 --- a/src/mempool.cpp +++ b/src/mempool.cpp @@ -37,20 +37,33 @@ void Mempool::add(const TxMempoolData& tx) { WriteLock lock(m_lock); - for (const TxMempoolData& old_tx : m_transactions) { - if (old_tx.id == tx.id) { - LOGWARN(1, "duplicate transaction with id = " << tx.id << ", skipped"); - return; - } + if (!m_transactions.emplace(tx.id, tx).second) { + LOGWARN(1, "duplicate transaction with id = " << tx.id << ", skipped"); } - - m_transactions.push_back(tx); } void Mempool::swap(std::vector& transactions) { + const time_t cur_time = time(nullptr); + WriteLock lock(m_lock); - m_transactions.swap(transactions); + + // Initialize time_received for all transactions + for (TxMempoolData& data : transactions) { + auto it = m_transactions.find(data.id); + if (it != m_transactions.end()) { + data.time_received = it->second.time_received; + } + else { + data.time_received = cur_time; + } + } + + m_transactions.clear(); + + for (TxMempoolData& data : transactions) { + m_transactions.emplace(data.id, data); + } } } // namespace p2pool diff --git a/src/mempool.h b/src/mempool.h index 8a4c035..c751f7a 100644 --- a/src/mempool.h +++ b/src/mempool.h @@ -18,6 +18,7 @@ #pragma once #include "uv_util.h" +#include namespace p2pool { @@ -34,7 +35,7 @@ public: public: mutable uv_rwlock_t m_lock; - std::vector m_transactions; + std::unordered_map m_transactions; }; } // namespace p2pool diff --git a/src/zmq_reader.cpp b/src/zmq_reader.cpp index 4d586f6..6753be2 100644 --- a/src/zmq_reader.cpp +++ b/src/zmq_reader.cpp @@ -152,6 +152,8 @@ void ZMQReader::parse(char* data, size_t size) return; } + m_tx.time_received = time(nullptr); + for (SizeType i = 0, n = doc.Size(); i < n; ++i) { const auto& v = doc[i]; if (PARSE(v, m_tx, id) && PARSE(v, m_tx, blob_size) && PARSE(v, m_tx, weight) && PARSE(v, m_tx, fee)) {