Merge bitcoin/bitcoin#30170: refactor: Use type-safe time in txorphanage

fa6d4891c7 refactor: Use type-safe time in txorphanage (MarcoFalke)

Pull request description:

  This allows to remove manual conversions like multiplication by `60`, and uses a type-safe type instead of a raw `int64_t`.

ACKs for top commit:
  epiccurious:
    utACK fa6d4891c7.
  dergoegge:
    Code review ACK fa6d4891c7
  brunoerg:
    utACK fa6d4891c7

Tree-SHA512: c187d0e579b1131afcef8c901f5662c18ab867fa2a99fbb13b67bb1e10b2047128194bfef8329cde0d51e1c35d6227ae292b823968f37ea9422975e46e01846a
This commit is contained in:
merge-script
2024-05-29 09:22:24 +01:00
2 changed files with 12 additions and 10 deletions

View File

@@ -8,13 +8,14 @@
#include <logging.h> #include <logging.h>
#include <policy/policy.h> #include <policy/policy.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <util/time.h>
#include <cassert> #include <cassert>
/** Expiration time for orphan transactions in seconds */ /** Expiration time for orphan transactions */
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60; static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
/** Minimum time between orphan transactions expire time checks in seconds */ /** Minimum time between orphan transactions expire time checks */
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60; static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer) bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
@@ -40,7 +41,7 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
return false; return false;
} }
auto ret = m_orphans.emplace(wtxid, OrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size()}); auto ret = m_orphans.emplace(wtxid, OrphanTx{tx, peer, Now<NodeSeconds>() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size()});
assert(ret.second); assert(ret.second);
m_orphan_list.push_back(ret.first); m_orphan_list.push_back(ret.first);
for (const CTxIn& txin : tx->vin) { for (const CTxIn& txin : tx->vin) {
@@ -87,7 +88,7 @@ int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
// Time spent in orphanage = difference between current and entry time. // Time spent in orphanage = difference between current and entry time.
// Entry time is equal to ORPHAN_TX_EXPIRE_TIME earlier than entry's expiry. // Entry time is equal to ORPHAN_TX_EXPIRE_TIME earlier than entry's expiry.
LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s) after %ds\n", txid.ToString(), wtxid.ToString(), LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s) after %ds\n", txid.ToString(), wtxid.ToString(),
GetTime() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire); Ticks<std::chrono::seconds>(NodeClock::now() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire));
m_orphan_list.pop_back(); m_orphan_list.pop_back();
m_orphans.erase(it); m_orphans.erase(it);
@@ -118,12 +119,12 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
LOCK(m_mutex); LOCK(m_mutex);
unsigned int nEvicted = 0; unsigned int nEvicted = 0;
static int64_t nNextSweep; static NodeSeconds nNextSweep;
int64_t nNow = GetTime(); auto nNow{Now<NodeSeconds>()};
if (nNextSweep <= nNow) { if (nNextSweep <= nNow) {
// Sweep out expired orphan pool entries: // Sweep out expired orphan pool entries:
int nErased = 0; int nErased = 0;
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL; auto nMinExpTime{nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL};
std::map<Wtxid, OrphanTx>::iterator iter = m_orphans.begin(); std::map<Wtxid, OrphanTx>::iterator iter = m_orphans.begin();
while (iter != m_orphans.end()) while (iter != m_orphans.end())
{ {

View File

@@ -9,6 +9,7 @@
#include <primitives/block.h> #include <primitives/block.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <sync.h> #include <sync.h>
#include <util/time.h>
#include <map> #include <map>
#include <set> #include <set>
@@ -73,7 +74,7 @@ protected:
struct OrphanTx { struct OrphanTx {
CTransactionRef tx; CTransactionRef tx;
NodeId fromPeer; NodeId fromPeer;
int64_t nTimeExpire; NodeSeconds nTimeExpire;
size_t list_pos; size_t list_pos;
}; };