What changed, and why it matters
This Monero update fixes a bug in how the transaction pool (mempool) handles transactions whose recorded arrival time is in the future. Before the fix, a transaction with a future receive_time could be treated as extremely old and wrongly removed from the pool, or cause misleading statistics. The patch clamps such times to the current time so they are not penalized. A new test confirms future-timestamped transactions stay in the pool while genuinely expired ones are still cleaned up.
Treat as a routine correctness/stability fix. Reviewers should verify that all call sites computing transaction age from receive_time use the same clamping, and consider whether receive_time should be validated on insertion to prevent future timestamps from being stored at all.
Security signals we found
Integer underflow/negative age avoided in mempool pruning logic
Future receive_time no longer causes premature eviction
Pool statistics clamped to current time to prevent inconsistent histograms
Regression test added for future receive_time behavior
Evidence from the diff
The commit modifies tx_pool.cpp to handle txpool_tx_meta_t.receive_time values that can exceed the current wall-clock time. It introduces a local ‘now’ snapshot and replaces underflow-prone subtraction with a guarded expression (now >= meta.receive_time ? now - meta.receive_time : 0). It also caps receive_time used for stats/histograms at ‘now’ to avoid negative/future ages. A regression test (txpool_future_receive_time) manipulates pool metadata to set receive_time to now+3600 and now-LIVETIME-1, verifying that future-timed transactions survive cleanup and that stats remain consistent.
Changed components
src/cryptonote_core/tx_pool.cpptests/core_tests/tx_pool.cpptests/core_tests/tx_pool.htests/core_tests/chaingen.htests/core_tests/chaingen_main.cppInspect captured patch +134 / −7
### src/cryptonote_core/tx_pool.cpp
@@ -756,9 +756,10 @@ namespace cryptonote
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
CRITICAL_REGION_LOCAL1(m_blockchain);
+ const uint64_t now = time(nullptr);
std::list<std::pair<crypto::hash, uint64_t>> remove;
- m_blockchain.for_all_txpool_txes([this, &remove](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*) {
- uint64_t tx_age = time(nullptr) - meta.receive_time;
+ m_blockchain.for_all_txpool_txes([this, &remove, now](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*) {
+ const uint64_t tx_age = now >= meta.receive_time ? now - meta.receive_time : 0;
if((tx_age > CRYPTONOTE_MEMPOOL_TX_LIVETIME && !meta.kept_by_block) ||
(tx_age > CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME && meta.kept_by_block) )
@@ -1185,13 +1186,14 @@ namespace cryptonote
if (!meta.relayed)
stats.num_not_relayed++;
stats.fee_total += meta.fee;
- if (!stats.oldest || meta.receive_time < stats.oldest)
- stats.oldest = meta.receive_time;
- if (meta.receive_time < now - 600)
+ const uint64_t receive_time = std::min(meta.receive_time, now);
+ if (!stats.oldest || receive_time < stats.oldest)
+ stats.oldest = receive_time;
+ if (receive_time < now - 600)
stats.num_10m++;
if (meta.last_failed_height)
stats.num_failing++;
- uint64_t age = now - meta.receive_time + (now == meta.receive_time);
+ const uint64_t age = now - receive_time + (now == receive_time);
agebytes[age].txs++;
agebytes[age].bytes += meta.weight;
if (meta.double_spend_seen)
### tests/core_tests/chaingen.h
@@ -789,7 +789,7 @@ inline bool do_replay_events_get_core(std::vector<test_event_entry>& events, cry
test_options_ = &test_options_tmp;
}
- if (!c.init(vm, test_options_))
+ if (!c.init(vm, test_options_, nullptr, false))
{
MERROR("Failed to init core");
return false;
### tests/core_tests/chaingen_main.cpp
@@ -162,6 +162,7 @@ int main(int argc, char* argv[])
// Mempool
GENERATE_AND_PLAY(txpool_spend_key_public);
GENERATE_AND_PLAY(txpool_spend_key_all);
+ GENERATE_AND_PLAY(txpool_future_receive_time);
GENERATE_AND_PLAY(txpool_double_spend_norelay);
GENERATE_AND_PLAY(txpool_double_spend_local);
GENERATE_AND_PLAY(txpool_double_spend_keyimage);
### tests/core_tests/tx_pool.cpp
@@ -30,7 +30,9 @@
#include <boost/chrono/chrono.hpp>
#include <boost/thread/thread_only.hpp>
+#include <ctime>
#include <limits>
+#include "blockchain_db/locked_txn.h"
#include "string_tools.h"
#define INIT_MEMPOOL_TEST() \
@@ -94,6 +96,114 @@ bool txpool_spend_key_all::generate(std::vector<test_event_entry>& events)
return true;
}
+txpool_future_receive_time::txpool_future_receive_time()
+ : test_chain_unit_base()
+ , m_future_txids{crypto::null_hash, crypto::null_hash}
+ , m_expired_txid(crypto::null_hash)
+{
+ REGISTER_CALLBACK_METHOD(txpool_future_receive_time, set_future_receive_time);
+ REGISTER_CALLBACK_METHOD(txpool_future_receive_time, check_txpool);
+}
+
+bool txpool_future_receive_time::generate(std::vector<test_event_entry>& events)
+{
+ INIT_MEMPOOL_TEST();
+ GENERATE_ACCOUNT(alice_account);
+ GENERATE_ACCOUNT(carol_account);
+
+ MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0r);
+ MAKE_TX(events, tx_1, miner_account, alice_account, send_amount, blk_0r);
+ MAKE_TX(events, tx_2, miner_account, carol_account, send_amount, blk_0r);
+ DO_CALLBACK(events, "set_future_receive_time");
+ DO_CALLBACK(events, "check_txpool");
+
+ return true;
+}
+
+bool txpool_future_receive_time::set_future_receive_time(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
+{
+ cryptonote::Blockchain& blockchain = c.get_blockchain_storage();
+ m_future_txids[0] = cryptonote::get_transaction_hash(boost::get<cryptonote::transaction>(events.at(ev_index - 3)));
+ m_future_txids[1] = cryptonote::get_transaction_hash(boost::get<cryptonote::transaction>(events.at(ev_index - 2)));
+ m_expired_txid = cryptonote::get_transaction_hash(boost::get<cryptonote::transaction>(events.at(ev_index - 1)));
+
+ const uint64_t now = std::time(nullptr);
+ const auto set_receive_time = [&blockchain](const crypto::hash& txid, uint64_t receive_time)
+ {
+ cryptonote::txpool_tx_meta_t meta{};
+ if (!blockchain.get_txpool_tx_meta(txid, meta))
+ return false;
+ meta.receive_time = receive_time;
+ blockchain.update_txpool_tx(txid, meta);
+ return true;
+ };
+
+ cryptonote::LockedTXN lock(blockchain.get_db());
+ if (!set_receive_time(m_future_txids[0], now + 3600) ||
+ !set_receive_time(m_future_txids[1], now + 3600) ||
+ !set_receive_time(m_expired_txid, now - CRYPTONOTE_MEMPOOL_TX_LIVETIME - 1))
+ {
+ MERROR("Failed to update transaction pool metadata");
+ return false;
+ }
+ lock.commit();
+ return true;
+}
+
+bool txpool_future_receive_time::check_txpool(cryptonote::core& c, size_t /*ev_index*/, const std::vector<test_event_entry>& /*events*/)
+{
+ bool valid = true;
+ cryptonote::txpool_stats stats{};
+ if (!c.get_pool_transaction_stats(stats) || stats.txs_total != 3)
+ {
+ MERROR("Failed to get transaction pool statistics");
+ valid = false;
+ }
+
+ uint32_t histogram_txs = 0;
+ for (const cryptonote::txpool_histo& entry : stats.histo)
+ histogram_txs += entry.txs;
+ if (histogram_txs != stats.txs_total)
+ {
+ MERROR("Transaction pool histogram contains " << histogram_txs << " transactions, expected " << stats.txs_total);
+ valid = false;
+ }
+
+ c.on_idle();
+ for (const crypto::hash& txid : m_future_txids)
+ {
+ if (!c.pool_has_tx(txid))
+ {
+ MERROR("Transaction with a future receive time was removed from the pool");
+ valid = false;
+ }
+ }
+ if (c.pool_has_tx(m_expired_txid))
+ {
+ MERROR("Expired transaction was not removed from the pool");
+ valid = false;
+ }
+
+ stats = {};
+ if (!c.get_pool_transaction_stats(stats) || stats.txs_total != 2)
+ {
+ MERROR("Failed to get transaction pool statistics after cleanup");
+ valid = false;
+ }
+
+ histogram_txs = 0;
+ for (const cryptonote::txpool_histo& entry : stats.histo)
+ histogram_txs += entry.txs;
+ const uint64_t now = std::time(nullptr);
+ if (histogram_txs != stats.txs_total || stats.oldest > now)
+ {
+ MERROR("Invalid transaction pool statistics after cleanup");
+ valid = false;
+ }
+
+ return valid;
+}
+
txpool_double_spend_base::txpool_double_spend_base()
: txpool_base()
, m_broadcasted_hashes()
### tests/core_tests/tx_pool.h
@@ -28,6 +28,7 @@
#pragma once
+#include <array>
#include <unordered_map>
#include <unordered_set>
@@ -69,6 +70,19 @@ struct txpool_spend_key_all : txpool_base
bool generate(std::vector<test_event_entry>& events);
};
+class txpool_future_receive_time : public test_chain_unit_base
+{
+ std::array<crypto::hash, 2> m_future_txids;
+ crypto::hash m_expired_txid;
+
+public:
+ txpool_future_receive_time();
+
+ bool generate(std::vector<test_event_entry>& events);
+ bool set_future_receive_time(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
+ bool check_txpool(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
+};
+
class txpool_double_spend_base : public txpool_base
{
std::unordered_set<crypto::hash> m_broadcasted_hashes;Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.