Skip to content

Commit 5895602

Browse files
committed
Merge bitcoin/bitcoin#30170: refactor: Use type-safe time in txorphanage
fa6d489 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 fa6d489. dergoegge: Code review ACK fa6d489 brunoerg: utACK fa6d489 Tree-SHA512: c187d0e579b1131afcef8c901f5662c18ab867fa2a99fbb13b67bb1e10b2047128194bfef8329cde0d51e1c35d6227ae292b823968f37ea9422975e46e01846a
2 parents 327f08b + fa6d489 commit 5895602

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) {
@@ -87,7 +88,7 @@ int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
8788
// Time spent in orphanage = difference between current and entry time.
8889
// Entry time is equal to ORPHAN_TX_EXPIRE_TIME earlier than entry's expiry.
8990
LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s) after %ds\n", txid.ToString(), wtxid.ToString(),
90-
GetTime() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire);
91+
Ticks<std::chrono::seconds>(NodeClock::now() + ORPHAN_TX_EXPIRE_TIME - it->second.nTimeExpire));
9192
m_orphan_list.pop_back();
9293

9394
m_orphans.erase(it);
@@ -118,12 +119,12 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
118119
LOCK(m_mutex);
119120

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

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)