Skip to content

Commit 62607d7

Browse files
committed
Convert COrphanTx to keep a CTransactionRef
1 parent c44e4c4 commit 62607d7

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

src/net_processing.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct IteratorComparator
5151

5252
struct COrphanTx {
5353
// When modifying, adapt the copy of this definition in tests/DoS_tests.
54-
CTransaction tx;
54+
CTransactionRef tx;
5555
NodeId fromPeer;
5656
int64_t nTimeExpire;
5757
};
@@ -586,9 +586,9 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals)
586586
// mapOrphanTransactions
587587
//
588588

589-
bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
589+
bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
590590
{
591-
uint256 hash = tx.GetHash();
591+
const uint256& hash = tx->GetHash();
592592
if (mapOrphanTransactions.count(hash))
593593
return false;
594594

@@ -599,7 +599,7 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(c
599599
// have been mined or received.
600600
// 100 orphans, each of which is at most 99,999 bytes big is
601601
// at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
602-
unsigned int sz = GetTransactionWeight(tx);
602+
unsigned int sz = GetTransactionWeight(*tx);
603603
if (sz >= MAX_STANDARD_TX_WEIGHT)
604604
{
605605
LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
@@ -608,7 +608,7 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(c
608608

609609
auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
610610
assert(ret.second);
611-
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
611+
BOOST_FOREACH(const CTxIn& txin, tx->vin) {
612612
mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
613613
}
614614

@@ -622,7 +622,7 @@ int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
622622
map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
623623
if (it == mapOrphanTransactions.end())
624624
return 0;
625-
BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
625+
BOOST_FOREACH(const CTxIn& txin, it->second.tx->vin)
626626
{
627627
auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
628628
if (itPrev == mapOrphanTransactionsByPrev.end())
@@ -644,7 +644,7 @@ void EraseOrphansFor(NodeId peer)
644644
map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
645645
if (maybeErase->second.fromPeer == peer)
646646
{
647-
nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
647+
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
648648
}
649649
}
650650
if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
@@ -665,7 +665,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRE
665665
{
666666
map<uint256, COrphanTx>::iterator maybeErase = iter++;
667667
if (maybeErase->second.nTimeExpire <= nNow) {
668-
nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
668+
nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
669669
} else {
670670
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
671671
}
@@ -736,7 +736,7 @@ void PeerLogicValidation::SyncTransaction(const CTransaction& tx, const CBlockIn
736736
auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
737737
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
738738
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
739-
const CTransaction& orphanTx = (*mi)->second.tx;
739+
const CTransaction& orphanTx = *(*mi)->second.tx;
740740
const uint256& orphanHash = orphanTx.GetHash();
741741
vOrphanErase.push_back(orphanHash);
742742
}
@@ -1636,7 +1636,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
16361636
mi != itByPrev->second.end();
16371637
++mi)
16381638
{
1639-
const CTransaction& orphanTx = (*mi)->second.tx;
1639+
const CTransactionRef& porphanTx = (*mi)->second.tx;
1640+
const CTransaction& orphanTx = *porphanTx;
16401641
const uint256& orphanHash = orphanTx.GetHash();
16411642
NodeId fromPeer = (*mi)->second.fromPeer;
16421643
bool fMissingInputs2 = false;
@@ -1648,7 +1649,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
16481649

16491650
if (setMisbehaving.count(fromPeer))
16501651
continue;
1651-
if (AcceptToMemoryPool(mempool, stateDummy, MakeTransactionRef(orphanTx), true, &fMissingInputs2)) {
1652+
if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, true, &fMissingInputs2)) {
16521653
LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
16531654
RelayTransaction(orphanTx, connman);
16541655
for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
@@ -1701,7 +1702,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
17011702
pfrom->AddInventoryKnown(_inv);
17021703
if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
17031704
}
1704-
AddOrphanTx(tx, pfrom->GetId());
1705+
AddOrphanTx(ptx, pfrom->GetId());
17051706

17061707
// DoS prevention: do not allow mapOrphanTransactions to grow unbounded
17071708
unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));

src/test/DoS_tests.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include <boost/test/unit_test.hpp>
2424

2525
// Tests this internal-to-main.cpp method:
26-
extern bool AddOrphanTx(const CTransaction& tx, NodeId peer);
26+
extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer);
2727
extern void EraseOrphansFor(NodeId peer);
2828
extern unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans);
2929
struct COrphanTx {
30-
CTransaction tx;
30+
CTransactionRef tx;
3131
NodeId fromPeer;
3232
int64_t nTimeExpire;
3333
};
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
115115
BOOST_CHECK(!connman->IsBanned(addr));
116116
}
117117

118-
CTransaction RandomOrphan()
118+
CTransactionRef RandomOrphan()
119119
{
120120
std::map<uint256, COrphanTx>::iterator it;
121121
it = mapOrphanTransactions.lower_bound(GetRandHash());
@@ -143,30 +143,30 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
143143
tx.vout[0].nValue = 1*CENT;
144144
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
145145

146-
AddOrphanTx(tx, i);
146+
AddOrphanTx(MakeTransactionRef(tx), i);
147147
}
148148

149149
// ... and 50 that depend on other orphans:
150150
for (int i = 0; i < 50; i++)
151151
{
152-
CTransaction txPrev = RandomOrphan();
152+
CTransactionRef txPrev = RandomOrphan();
153153

154154
CMutableTransaction tx;
155155
tx.vin.resize(1);
156156
tx.vin[0].prevout.n = 0;
157-
tx.vin[0].prevout.hash = txPrev.GetHash();
157+
tx.vin[0].prevout.hash = txPrev->GetHash();
158158
tx.vout.resize(1);
159159
tx.vout[0].nValue = 1*CENT;
160160
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
161-
SignSignature(keystore, txPrev, tx, 0, SIGHASH_ALL);
161+
SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL);
162162

163-
AddOrphanTx(tx, i);
163+
AddOrphanTx(MakeTransactionRef(tx), i);
164164
}
165165

166166
// This really-big orphan should be ignored:
167167
for (int i = 0; i < 10; i++)
168168
{
169-
CTransaction txPrev = RandomOrphan();
169+
CTransactionRef txPrev = RandomOrphan();
170170

171171
CMutableTransaction tx;
172172
tx.vout.resize(1);
@@ -176,15 +176,15 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
176176
for (unsigned int j = 0; j < tx.vin.size(); j++)
177177
{
178178
tx.vin[j].prevout.n = j;
179-
tx.vin[j].prevout.hash = txPrev.GetHash();
179+
tx.vin[j].prevout.hash = txPrev->GetHash();
180180
}
181-
SignSignature(keystore, txPrev, tx, 0, SIGHASH_ALL);
181+
SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL);
182182
// Re-use same signature for other inputs
183183
// (they don't have to be valid for this test)
184184
for (unsigned int j = 1; j < tx.vin.size(); j++)
185185
tx.vin[j].scriptSig = tx.vin[0].scriptSig;
186186

187-
BOOST_CHECK(!AddOrphanTx(tx, i));
187+
BOOST_CHECK(!AddOrphanTx(MakeTransactionRef(tx), i));
188188
}
189189

190190
// Test EraseOrphansFor:

0 commit comments

Comments
 (0)