BlockTemplate: wait for 10 seconds before including new transactions

This commit is contained in:
SChernykh 2021-09-14 12:54:42 +02:00
parent 94adce4045
commit 8aa9e54dc4
5 changed files with 45 additions and 13 deletions

View File

@ -194,9 +194,22 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_difficulty = data.difficulty; m_difficulty = data.difficulty;
m_seedHash = data.seed_hash; 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); 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 // Safeguard for busy mempool moments
@ -211,6 +224,8 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_mempoolTxs.resize(1000); 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); const uint64_t base_reward = get_base_reward(data.already_generated_coins);
uint64_t total_tx_fees = 0; 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; const uint64_t max_reward = base_reward + total_tx_fees;
LOGINFO(3, "base reward = " << log::Gray() << log::XMRAmount(base_reward) << log::NoColor() << 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() << " transactions, fees = " << log::Gray() << log::XMRAmount(total_tx_fees) << log::NoColor() <<
", weight = " << log::Gray() << total_tx_weight); ", 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; m_poolBlockTemplate->m_minorVersion = HARDFORK_SUPPORTED_VERSION;
// Timestamp // Timestamp
m_timestamp = time(nullptr); m_timestamp = cur_time;
if (m_timestamp <= data.median_timestamp) { if (m_timestamp <= data.median_timestamp) {
LOGWARN(2, "timestamp adjusted from " << m_timestamp << " to " << data.median_timestamp + 1 << ". Fix your system time!"); LOGWARN(2, "timestamp adjusted from " << m_timestamp << " to " << data.median_timestamp + 1 << ". Fix your system time!");
m_timestamp = data.median_timestamp + 1; m_timestamp = data.median_timestamp + 1;

View File

@ -226,12 +226,13 @@ difficulty_type operator+(const difficulty_type& a, const difficulty_type& b);
struct TxMempoolData 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; hash id;
uint64_t blob_size; uint64_t blob_size;
uint64_t weight; uint64_t weight;
uint64_t fee; uint64_t fee;
time_t time_received;
}; };
struct MinerData struct MinerData

View File

@ -37,20 +37,33 @@ void Mempool::add(const TxMempoolData& tx)
{ {
WriteLock lock(m_lock); WriteLock lock(m_lock);
for (const TxMempoolData& old_tx : m_transactions) { if (!m_transactions.emplace(tx.id, tx).second) {
if (old_tx.id == tx.id) {
LOGWARN(1, "duplicate transaction with id = " << tx.id << ", skipped"); LOGWARN(1, "duplicate transaction with id = " << tx.id << ", skipped");
return;
} }
}
m_transactions.push_back(tx);
} }
void Mempool::swap(std::vector<TxMempoolData>& transactions) void Mempool::swap(std::vector<TxMempoolData>& transactions)
{ {
const time_t cur_time = time(nullptr);
WriteLock lock(m_lock); 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 } // namespace p2pool

View File

@ -18,6 +18,7 @@
#pragma once #pragma once
#include "uv_util.h" #include "uv_util.h"
#include <unordered_map>
namespace p2pool { namespace p2pool {
@ -34,7 +35,7 @@ public:
public: public:
mutable uv_rwlock_t m_lock; mutable uv_rwlock_t m_lock;
std::vector<TxMempoolData> m_transactions; std::unordered_map<hash, TxMempoolData> m_transactions;
}; };
} // namespace p2pool } // namespace p2pool

View File

@ -152,6 +152,8 @@ void ZMQReader::parse(char* data, size_t size)
return; return;
} }
m_tx.time_received = time(nullptr);
for (SizeType i = 0, n = doc.Size(); i < n; ++i) { for (SizeType i = 0, n = doc.Size(); i < n; ++i) {
const auto& v = doc[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)) { if (PARSE(v, m_tx, id) && PARSE(v, m_tx, blob_size) && PARSE(v, m_tx, weight) && PARSE(v, m_tx, fee)) {