Skip to content

Commit 96c756f

Browse files
MarcoFalkejanus
authored andcommitted
refactor: Use type-safe time in txorphanage
1 parent 442291c commit 96c756f

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/txorphanage.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
#include <logging.h>
99
#include <policy/policy.h>
1010
#include <primitives/transaction.h>
11+
#include <util/time.h>
1112

1213
#include <cassert>
1314

14-
/** Expiration time for orphan transactions in seconds */
15-
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
16-
/** Minimum time between orphan transactions expire time checks in seconds */
17-
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
15+
/** Expiration time for orphan transactions */
16+
static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
17+
/** Minimum time between orphan transactions expire time checks */
18+
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
1819

1920

2021
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
@@ -40,7 +41,7 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
4041
return false;
4142
}
4243

43-
auto ret = m_orphans.emplace(wtxid, OrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size()});
44+
auto ret = m_orphans.emplace(wtxid, OrphanTx{tx, peer, Now<NodeSeconds>() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size()});
4445
assert(ret.second);
4546
m_orphan_list.push_back(ret.first);
4647
for (const CTxIn& txin : tx->vin) {
@@ -90,7 +91,7 @@ int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
9091
// Time spent in orphanage = difference between current and entry time.
9192
// Entry time is equal to ORPHAN_TX_EXPIRE_TIME earlier than entry's expiry.
9293
LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s) after %ds\n", txid.ToString(), wtxid.ToString(),
93-
GetTime() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire);
94+
Ticks<std::chrono::seconds>(NodeClock::now() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire));
9495
m_orphan_list.pop_back();
9596
m_wtxid_to_orphan_it.erase(wtxid);
9697

@@ -122,12 +123,12 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
122123
LOCK(m_mutex);
123124

124125
unsigned int nEvicted = 0;
125-
static int64_t nNextSweep;
126-
int64_t nNow = GetTime();
126+
static NodeSeconds nNextSweep;
127+
auto nNow{Now<NodeSeconds>()};
127128
if (nNextSweep <= nNow) {
128129
// Sweep out expired orphan pool entries:
129130
int nErased = 0;
130-
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
131+
auto nMinExpTime{nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL};
131132
std::map<Wtxid, OrphanTx>::iterator iter = m_orphans.begin();
132133
while (iter != m_orphans.end())
133134
{

src/txorphanage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <primitives/block.h>
1010
#include <primitives/transaction.h>
1111
#include <sync.h>
12+
#include <util/time.h>
1213

1314
#include <map>
1415
#include <set>
@@ -73,7 +74,7 @@ class TxOrphanage {
7374
struct OrphanTx {
7475
CTransactionRef tx;
7576
NodeId fromPeer;
76-
int64_t nTimeExpire;
77+
NodeSeconds nTimeExpire;
7778
size_t list_pos;
7879
};
7980

0 commit comments

Comments
 (0)