Skip to content

Commit faec689

Browse files
author
MarcoFalke
committed
txmempool: Make entry time type-safe (std::chrono)
1 parent faaa1f0 commit faec689

File tree

6 files changed

+16
-15
lines changed

6 files changed

+16
-15
lines changed

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ class CNode
761761
// Used for BIP35 mempool sending
762762
bool fSendMempool GUARDED_BY(cs_tx_inventory){false};
763763
// Last time a "MEMPOOL" request was serviced.
764-
std::atomic<int64_t> timeLastMempoolReq{0};
764+
std::atomic<std::chrono::seconds> m_last_mempool_req{std::chrono::seconds{0}};
765765
int64_t nNextInvSend{0};
766766

767767
CCriticalSection cs_feeFilter;

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,11 +1541,11 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm
15411541
if (mi != mapRelay.end()) {
15421542
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
15431543
push = true;
1544-
} else if (pfrom->m_tx_relay->timeLastMempoolReq) {
1544+
} else if (pfrom->m_tx_relay->m_last_mempool_req.load().count()) {
15451545
auto txinfo = mempool.info(inv.hash);
15461546
// To protect privacy, do not answer getdata using the mempool when
15471547
// that TX couldn't have been INVed in reply to a MEMPOOL request.
1548-
if (txinfo.tx && txinfo.nTime <= pfrom->m_tx_relay->timeLastMempoolReq) {
1548+
if (txinfo.tx && txinfo.m_time <= pfrom->m_tx_relay->m_last_mempool_req.load()) {
15491549
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
15501550
push = true;
15511551
}
@@ -3873,7 +3873,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
38733873
vInv.clear();
38743874
}
38753875
}
3876-
pto->m_tx_relay->timeLastMempoolReq = GetTime();
3876+
pto->m_tx_relay->m_last_mempool_req = GetTime<std::chrono::seconds>();
38773877
}
38783878

38793879
// Determine transactions to relay

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
418418
info.pushKV("weight", (int)e.GetTxWeight());
419419
info.pushKV("fee", ValueFromAmount(e.GetFee()));
420420
info.pushKV("modifiedfee", ValueFromAmount(e.GetModifiedFee()));
421-
info.pushKV("time", e.GetTime());
421+
info.pushKV("time", count_seconds(e.GetTime()));
422422
info.pushKV("height", (int)e.GetHeight());
423423
info.pushKV("descendantcount", e.GetCountWithDescendants());
424424
info.pushKV("descendantsize", e.GetSizeWithDescendants());

src/txmempool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@ void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPool
917917
}
918918
}
919919

920-
int CTxMemPool::Expire(int64_t time) {
920+
int CTxMemPool::Expire(std::chrono::seconds time)
921+
{
921922
AssertLockHeld(cs);
922923
indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin();
923924
setEntries toremove;

src/txmempool.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class CTxMemPoolEntry
102102
const CAmount& GetFee() const { return nFee; }
103103
size_t GetTxSize() const;
104104
size_t GetTxWeight() const { return nTxWeight; }
105-
int64_t GetTime() const { return nTime; }
105+
std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
106106
unsigned int GetHeight() const { return entryHeight; }
107107
int64_t GetSigOpCost() const { return sigOpCost; }
108108
int64_t GetModifiedFee() const { return nFee + feeDelta; }
@@ -332,7 +332,7 @@ struct TxMempoolInfo
332332
CTransactionRef tx;
333333

334334
/** Time the transaction entered the mempool. */
335-
int64_t nTime;
335+
std::chrono::seconds m_time;
336336

337337
/** Feerate of the transaction. */
338338
CFeeRate feeRate;
@@ -657,7 +657,7 @@ class CTxMemPool
657657
void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
658658

659659
/** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
660-
int Expire(int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
660+
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
661661

662662
/**
663663
* Calculate the ancestor and descendant count for the given transaction.

src/validation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,10 @@ bool CheckSequenceLocks(const CTxMemPool& pool, const CTransaction& tx, int flag
314314
// Returns the script flags which should be checked for a given block
315315
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
316316

317-
static void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age)
317+
static void LimitMempoolSize(CTxMemPool& pool, size_t limit, std::chrono::seconds age)
318318
EXCLUSIVE_LOCKS_REQUIRED(pool.cs, ::cs_main)
319319
{
320-
int expired = pool.Expire(GetTime() - age);
320+
int expired = pool.Expire(GetTime<std::chrono::seconds>() - age);
321321
if (expired != 0) {
322322
LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
323323
}
@@ -389,7 +389,7 @@ static void UpdateMempoolForReorg(DisconnectedBlockTransactions& disconnectpool,
389389
// We also need to remove any now-immature transactions
390390
mempool.removeForReorg(&::ChainstateActive().CoinsTip(), ::ChainActive().Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
391391
// Re-limit mempool size, in case we added any transactions
392-
LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
392+
LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, std::chrono::hours{gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
393393
}
394394

395395
// Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
@@ -1011,7 +1011,7 @@ bool MemPoolAccept::Finalize(ATMPArgs& args, Workspace& ws)
10111011

10121012
// trim mempool and check if tx was trimmed
10131013
if (!bypass_limits) {
1014-
LimitMempoolSize(m_pool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
1014+
LimitMempoolSize(m_pool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, std::chrono::hours{gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
10151015
if (!m_pool.exists(hash))
10161016
return state.Invalid(ValidationInvalidReason::TX_MEMPOOL_POLICY, false, REJECT_INSUFFICIENTFEE, "mempool full");
10171017
}
@@ -4992,8 +4992,8 @@ bool DumpMempool(const CTxMemPool& pool)
49924992
file << (uint64_t)vinfo.size();
49934993
for (const auto& i : vinfo) {
49944994
file << *(i.tx);
4995-
file << (int64_t)i.nTime;
4996-
file << (int64_t)i.nFeeDelta;
4995+
file << int64_t{count_seconds(i.m_time)};
4996+
file << int64_t{i.nFeeDelta};
49974997
mapDeltas.erase(i.tx->GetHash());
49984998
}
49994999

0 commit comments

Comments
 (0)