Skip to content

Commit 1041616

Browse files
committed
txorphanage: Extract OrphanageAddTx
Extract code from AddOrphanTx into OrphanageAddTx.
1 parent f294da7 commit 1041616

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

src/net_processing.cpp

+1-26
Original file line numberDiff line numberDiff line change
@@ -1098,37 +1098,12 @@ static void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_L
10981098

10991099
bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
11001100
{
1101-
const uint256& hash = tx->GetHash();
1102-
if (mapOrphanTransactions.count(hash))
1101+
if (!OrphanageAddTx(tx, peer)) {
11031102
return false;
1104-
1105-
// Ignore big transactions, to avoid a
1106-
// send-big-orphans memory exhaustion attack. If a peer has a legitimate
1107-
// large transaction with a missing parent then we assume
1108-
// it will rebroadcast it later, after the parent transaction(s)
1109-
// have been mined or received.
1110-
// 100 orphans, each of which is at most 100,000 bytes big is
1111-
// at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
1112-
unsigned int sz = GetTransactionWeight(*tx);
1113-
if (sz > MAX_STANDARD_TX_WEIGHT)
1114-
{
1115-
LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
1116-
return false;
1117-
}
1118-
1119-
auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, g_orphan_list.size()});
1120-
assert(ret.second);
1121-
g_orphan_list.push_back(ret.first);
1122-
// Allow for lookups in the orphan pool by wtxid, as well as txid
1123-
g_orphans_by_wtxid.emplace(tx->GetWitnessHash(), ret.first);
1124-
for (const CTxIn& txin : tx->vin) {
1125-
mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
11261103
}
11271104

11281105
AddToCompactExtraTransactions(tx);
11291106

1130-
LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
1131-
mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
11321107
return true;
11331108
}
11341109

src/txorphanage.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <cassert>
1212

13+
/** Expiration time for orphan transactions in seconds */
14+
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
1315
/** Minimum time between orphan transactions expire time checks in seconds */
1416
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
1517

@@ -22,6 +24,42 @@ std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUA
2224

2325
std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans);
2426

27+
bool OrphanageAddTx(const CTransactionRef& tx, NodeId peer)
28+
{
29+
AssertLockHeld(g_cs_orphans);
30+
31+
const uint256& hash = tx->GetHash();
32+
if (mapOrphanTransactions.count(hash))
33+
return false;
34+
35+
// Ignore big transactions, to avoid a
36+
// send-big-orphans memory exhaustion attack. If a peer has a legitimate
37+
// large transaction with a missing parent then we assume
38+
// it will rebroadcast it later, after the parent transaction(s)
39+
// have been mined or received.
40+
// 100 orphans, each of which is at most 100,000 bytes big is
41+
// at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
42+
unsigned int sz = GetTransactionWeight(*tx);
43+
if (sz > MAX_STANDARD_TX_WEIGHT)
44+
{
45+
LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
46+
return false;
47+
}
48+
49+
auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, g_orphan_list.size()});
50+
assert(ret.second);
51+
g_orphan_list.push_back(ret.first);
52+
// Allow for lookups in the orphan pool by wtxid, as well as txid
53+
g_orphans_by_wtxid.emplace(tx->GetWitnessHash(), ret.first);
54+
for (const CTxIn& txin : tx->vin) {
55+
mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
56+
}
57+
58+
LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
59+
mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
60+
return true;
61+
}
62+
2563
int EraseOrphanTx(const uint256& txid)
2664
{
2765
std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(txid);

src/txorphanage.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#include <primitives/transaction.h>
1010
#include <sync.h>
1111

12-
/** Expiration time for orphan transactions in seconds */
13-
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
14-
1512
/** Guards orphan transactions and extra txs for compact blocks */
1613
extern RecursiveMutex g_cs_orphans;
1714

@@ -29,6 +26,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRE
2926
void AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
3027
bool HaveOrphanTx(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
3128
std::pair<CTransactionRef, NodeId> GetOrphanTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
29+
bool OrphanageAddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
3230

3331
/** Map from txid to orphan transaction record. Limited by
3432
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */

0 commit comments

Comments
 (0)