Skip to content

Commit 9d5313d

Browse files
committed
move-only: Add txorphanage module
This module captures orphan tracking code for tx relay. Can be reviewed with --color-moved=dimmed-zebra
1 parent 56f06a9 commit 9d5313d

File tree

9 files changed

+170
-134
lines changed

9 files changed

+170
-134
lines changed

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ BITCOIN_CORE_H = \
224224
timedata.h \
225225
torcontrol.h \
226226
txdb.h \
227-
txrequest.h \
228227
txmempool.h \
228+
txorphanage.h \
229+
txrequest.h \
229230
undo.h \
230231
util/asmap.h \
231232
util/bip32.h \
@@ -347,8 +348,9 @@ libbitcoin_server_a_SOURCES = \
347348
timedata.cpp \
348349
torcontrol.cpp \
349350
txdb.cpp \
350-
txrequest.cpp \
351351
txmempool.cpp \
352+
txorphanage.cpp \
353+
txrequest.cpp \
352354
validation.cpp \
353355
validationinterface.cpp \
354356
versionbits.cpp \

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <torcontrol.h>
5353
#include <txdb.h>
5454
#include <txmempool.h>
55+
#include <txorphanage.h>
5556
#include <util/asmap.h>
5657
#include <util/check.h>
5758
#include <util/moneystr.h>

src/net_processing.cpp

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <streams.h>
2727
#include <tinyformat.h>
2828
#include <txmempool.h>
29+
#include <txorphanage.h>
2930
#include <txrequest.h>
3031
#include <util/check.h> // For NDEBUG compile time check
3132
#include <util/strencodings.h>
@@ -35,10 +36,6 @@
3536
#include <memory>
3637
#include <typeinfo>
3738

38-
/** Expiration time for orphan transactions in seconds */
39-
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
40-
/** Minimum time between orphan transactions expire time checks in seconds */
41-
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
4239
/** How long to cache transactions in mapRelay for normal relay */
4340
static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME = std::chrono::minutes{15};
4441
/** How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
@@ -148,25 +145,6 @@ static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
148145
/** the maximum percentage of addresses from our addrman to return in response to a getaddr message. */
149146
static constexpr size_t MAX_PCT_ADDR_TO_SEND = 23;
150147

151-
struct COrphanTx {
152-
// When modifying, adapt the copy of this definition in tests/DoS_tests.
153-
CTransactionRef tx;
154-
NodeId fromPeer;
155-
int64_t nTimeExpire;
156-
size_t list_pos;
157-
};
158-
159-
/** Guards orphan transactions and extra txs for compact blocks */
160-
RecursiveMutex g_cs_orphans;
161-
/** Map from txid to orphan transaction record. Limited by
162-
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
163-
std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
164-
/** Index from wtxid into the mapOrphanTransactions to lookup orphan
165-
* transactions using their witness ids. */
166-
std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans);
167-
168-
void EraseOrphansFor(NodeId peer);
169-
170148
// Internal stuff
171149
namespace {
172150
/** Blocks that are in flight, and that are in the queue to be downloaded. */
@@ -487,21 +465,6 @@ namespace {
487465
/** Number of preferable block download peers. */
488466
int nPreferredDownload GUARDED_BY(cs_main) = 0;
489467

490-
struct IteratorComparator
491-
{
492-
template<typename I>
493-
bool operator()(const I& a, const I& b) const
494-
{
495-
return &(*a) < &(*b);
496-
}
497-
};
498-
499-
/** Index from the parents' COutPoint into the mapOrphanTransactions. Used
500-
* to remove orphan transactions from the mapOrphanTransactions */
501-
std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans);
502-
/** Orphan transactions in vector for quick random eviction */
503-
std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans);
504-
505468
/** Orphan/conflicted/etc transactions that are kept for compact block reconstruction.
506469
* The last -blockreconstructionextratxn/DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN of
507470
* these are kept in a ring buffer */
@@ -1169,90 +1132,6 @@ bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRE
11691132
return true;
11701133
}
11711134

1172-
int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
1173-
{
1174-
std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
1175-
if (it == mapOrphanTransactions.end())
1176-
return 0;
1177-
for (const CTxIn& txin : it->second.tx->vin)
1178-
{
1179-
auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
1180-
if (itPrev == mapOrphanTransactionsByPrev.end())
1181-
continue;
1182-
itPrev->second.erase(it);
1183-
if (itPrev->second.empty())
1184-
mapOrphanTransactionsByPrev.erase(itPrev);
1185-
}
1186-
1187-
size_t old_pos = it->second.list_pos;
1188-
assert(g_orphan_list[old_pos] == it);
1189-
if (old_pos + 1 != g_orphan_list.size()) {
1190-
// Unless we're deleting the last entry in g_orphan_list, move the last
1191-
// entry to the position we're deleting.
1192-
auto it_last = g_orphan_list.back();
1193-
g_orphan_list[old_pos] = it_last;
1194-
it_last->second.list_pos = old_pos;
1195-
}
1196-
g_orphan_list.pop_back();
1197-
g_orphans_by_wtxid.erase(it->second.tx->GetWitnessHash());
1198-
1199-
mapOrphanTransactions.erase(it);
1200-
return 1;
1201-
}
1202-
1203-
void EraseOrphansFor(NodeId peer)
1204-
{
1205-
LOCK(g_cs_orphans);
1206-
int nErased = 0;
1207-
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
1208-
while (iter != mapOrphanTransactions.end())
1209-
{
1210-
std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
1211-
if (maybeErase->second.fromPeer == peer)
1212-
{
1213-
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
1214-
}
1215-
}
1216-
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
1217-
}
1218-
1219-
1220-
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
1221-
{
1222-
LOCK(g_cs_orphans);
1223-
1224-
unsigned int nEvicted = 0;
1225-
static int64_t nNextSweep;
1226-
int64_t nNow = GetTime();
1227-
if (nNextSweep <= nNow) {
1228-
// Sweep out expired orphan pool entries:
1229-
int nErased = 0;
1230-
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
1231-
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
1232-
while (iter != mapOrphanTransactions.end())
1233-
{
1234-
std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
1235-
if (maybeErase->second.nTimeExpire <= nNow) {
1236-
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
1237-
} else {
1238-
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
1239-
}
1240-
}
1241-
// Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
1242-
nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
1243-
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
1244-
}
1245-
FastRandomContext rng;
1246-
while (mapOrphanTransactions.size() > nMaxOrphans)
1247-
{
1248-
// Evict a random orphan:
1249-
size_t randompos = rng.randrange(g_orphan_list.size());
1250-
EraseOrphanTx(g_orphan_list[randompos]->first);
1251-
++nEvicted;
1252-
}
1253-
return nEvicted;
1254-
}
1255-
12561135
void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const std::string& message)
12571136
{
12581137
assert(howmuch > 0);

src/net_processing.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class CTxMemPool;
1515
class ChainstateManager;
1616

1717
extern RecursiveMutex cs_main;
18-
extern RecursiveMutex g_cs_orphans;
1918

2019
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
2120
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <script/signingprovider.h>
1515
#include <script/standard.h>
1616
#include <serialize.h>
17+
#include <txorphanage.h>
1718
#include <util/memory.h>
1819
#include <util/string.h>
1920
#include <util/system.h>
@@ -45,15 +46,6 @@ struct CConnmanTest : public CConnman {
4546

4647
// Tests these internal-to-net_processing.cpp methods:
4748
extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer);
48-
extern void EraseOrphansFor(NodeId peer);
49-
extern unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans);
50-
51-
struct COrphanTx {
52-
CTransactionRef tx;
53-
NodeId fromPeer;
54-
int64_t nTimeExpire;
55-
};
56-
extern std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
5749

5850
static CService ip(uint32_t i)
5951
{

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <test/util/net.h>
1919
#include <test/util/setup_common.h>
2020
#include <test/util/validation.h>
21+
#include <txorphanage.h>
2122
#include <util/memory.h>
2223
#include <validationinterface.h>
2324
#include <version.h>

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <test/util/net.h>
1414
#include <test/util/setup_common.h>
1515
#include <test/util/validation.h>
16+
#include <txorphanage.h>
1617
#include <util/memory.h>
1718
#include <validation.h>
1819
#include <validationinterface.h>

src/txorphanage.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <txorphanage.h>
6+
7+
#include <consensus/validation.h>
8+
#include <logging.h>
9+
#include <policy/policy.h>
10+
11+
#include <cassert>
12+
13+
/** Minimum time between orphan transactions expire time checks in seconds */
14+
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
15+
16+
RecursiveMutex g_cs_orphans;
17+
18+
std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
19+
std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans);
20+
21+
std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans);
22+
23+
std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans);
24+
25+
int EraseOrphanTx(uint256 hash)
26+
{
27+
std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
28+
if (it == mapOrphanTransactions.end())
29+
return 0;
30+
for (const CTxIn& txin : it->second.tx->vin)
31+
{
32+
auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
33+
if (itPrev == mapOrphanTransactionsByPrev.end())
34+
continue;
35+
itPrev->second.erase(it);
36+
if (itPrev->second.empty())
37+
mapOrphanTransactionsByPrev.erase(itPrev);
38+
}
39+
40+
size_t old_pos = it->second.list_pos;
41+
assert(g_orphan_list[old_pos] == it);
42+
if (old_pos + 1 != g_orphan_list.size()) {
43+
// Unless we're deleting the last entry in g_orphan_list, move the last
44+
// entry to the position we're deleting.
45+
auto it_last = g_orphan_list.back();
46+
g_orphan_list[old_pos] = it_last;
47+
it_last->second.list_pos = old_pos;
48+
}
49+
g_orphan_list.pop_back();
50+
g_orphans_by_wtxid.erase(it->second.tx->GetWitnessHash());
51+
52+
mapOrphanTransactions.erase(it);
53+
return 1;
54+
}
55+
56+
void EraseOrphansFor(NodeId peer)
57+
{
58+
LOCK(g_cs_orphans);
59+
int nErased = 0;
60+
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
61+
while (iter != mapOrphanTransactions.end())
62+
{
63+
std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
64+
if (maybeErase->second.fromPeer == peer)
65+
{
66+
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
67+
}
68+
}
69+
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
70+
}
71+
72+
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
73+
{
74+
LOCK(g_cs_orphans);
75+
76+
unsigned int nEvicted = 0;
77+
static int64_t nNextSweep;
78+
int64_t nNow = GetTime();
79+
if (nNextSweep <= nNow) {
80+
// Sweep out expired orphan pool entries:
81+
int nErased = 0;
82+
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
83+
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
84+
while (iter != mapOrphanTransactions.end())
85+
{
86+
std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
87+
if (maybeErase->second.nTimeExpire <= nNow) {
88+
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
89+
} else {
90+
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
91+
}
92+
}
93+
// Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
94+
nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
95+
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
96+
}
97+
FastRandomContext rng;
98+
while (mapOrphanTransactions.size() > nMaxOrphans)
99+
{
100+
// Evict a random orphan:
101+
size_t randompos = rng.randrange(g_orphan_list.size());
102+
EraseOrphanTx(g_orphan_list[randompos]->first);
103+
++nEvicted;
104+
}
105+
return nEvicted;
106+
}
107+

src/txorphanage.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TXORPHANAGE_H
6+
#define BITCOIN_TXORPHANAGE_H
7+
8+
#include <net.h>
9+
#include <primitives/transaction.h>
10+
#include <sync.h>
11+
12+
/** Expiration time for orphan transactions in seconds */
13+
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
14+
15+
/** Guards orphan transactions and extra txs for compact blocks */
16+
extern RecursiveMutex g_cs_orphans;
17+
18+
struct COrphanTx {
19+
// When modifying, adapt the copy of this definition in tests/DoS_tests.
20+
CTransactionRef tx;
21+
NodeId fromPeer;
22+
int64_t nTimeExpire;
23+
size_t list_pos;
24+
};
25+
26+
int EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
27+
void EraseOrphansFor(NodeId peer);
28+
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans);
29+
30+
/** Map from txid to orphan transaction record. Limited by
31+
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
32+
extern std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
33+
34+
/** Index from wtxid into the mapOrphanTransactions to lookup orphan
35+
* transactions using their witness ids. */
36+
extern std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans);
37+
38+
struct IteratorComparator
39+
{
40+
template<typename I>
41+
bool operator()(const I& a, const I& b) const
42+
{
43+
return &(*a) < &(*b);
44+
}
45+
};
46+
47+
/** Index from the parents' COutPoint into the mapOrphanTransactions. Used
48+
* to remove orphan transactions from the mapOrphanTransactions */
49+
extern std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans);
50+
51+
/** Orphan transactions in vector for quick random eviction */
52+
extern std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans);
53+
54+
#endif // BITCOIN_TXORPHANAGE_H

0 commit comments

Comments
 (0)