Skip to content

Commit f53466d

Browse files
committed
merge bitcoin#24968: Move TxOrphange tests to orphange_tests.cpp
1 parent 5da523c commit f53466d

File tree

3 files changed

+138
-118
lines changed

3 files changed

+138
-118
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ BITCOIN_TESTS =\
139139
test/net_peer_eviction_tests.cpp \
140140
test/net_tests.cpp \
141141
test/netbase_tests.cpp \
142+
test/orphanage_tests.cpp \
142143
test/pmt_tests.cpp \
143144
test/policyestimator_tests.cpp \
144145
test/pool_tests.cpp \

src/test/denialofservice_tests.cpp

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <test/util/net.h>
1616
#include <test/util/setup_common.h>
1717
#include <timedata.h>
18-
#include <txorphanage.h>
1918
#include <util/string.h>
2019
#include <util/system.h>
2120
#include <util/time.h>
@@ -456,121 +455,4 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
456455
peerLogic->FinalizeNode(dummyNode);
457456
}
458457

459-
class TxOrphanageTest : public TxOrphanage
460-
{
461-
public:
462-
inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
463-
{
464-
return m_orphans.size();
465-
}
466-
467-
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
468-
{
469-
std::map<uint256, OrphanTx>::iterator it;
470-
it = m_orphans.lower_bound(InsecureRand256());
471-
if (it == m_orphans.end())
472-
it = m_orphans.begin();
473-
return it->second.tx;
474-
}
475-
};
476-
477-
static void MakeNewKeyWithFastRandomContext(CKey& key)
478-
{
479-
std::vector<unsigned char> keydata;
480-
keydata = g_insecure_rand_ctx.randbytes(32);
481-
key.Set(keydata.data(), keydata.data() + keydata.size(), /*fCompressedIn*/ true);
482-
assert(key.IsValid());
483-
}
484-
485-
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
486-
{
487-
// This test had non-deterministic coverage due to
488-
// randomly selected seeds.
489-
// This seed is chosen so that all branches of the function
490-
// ecdsa_signature_parse_der_lax are executed during this test.
491-
// Specifically branches that run only when an ECDSA
492-
// signature's R and S values have leading zeros.
493-
g_insecure_rand_ctx = FastRandomContext{uint256{33}};
494-
495-
TxOrphanageTest orphanage;
496-
CKey key;
497-
MakeNewKeyWithFastRandomContext(key);
498-
FillableSigningProvider keystore;
499-
BOOST_CHECK(keystore.AddKey(key));
500-
501-
LOCK(g_cs_orphans);
502-
503-
// 50 orphan transactions:
504-
for (int i = 0; i < 50; i++)
505-
{
506-
CMutableTransaction tx;
507-
tx.vin.resize(1);
508-
tx.vin[0].prevout.n = 0;
509-
tx.vin[0].prevout.hash = InsecureRand256();
510-
tx.vin[0].scriptSig << OP_1;
511-
tx.vout.resize(1);
512-
tx.vout[0].nValue = 1*CENT;
513-
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
514-
515-
orphanage.AddTx(MakeTransactionRef(tx), i);
516-
}
517-
518-
// ... and 50 that depend on other orphans:
519-
for (int i = 0; i < 50; i++)
520-
{
521-
CTransactionRef txPrev = orphanage.RandomOrphan();
522-
523-
CMutableTransaction tx;
524-
tx.vin.resize(1);
525-
tx.vin[0].prevout.n = 0;
526-
tx.vin[0].prevout.hash = txPrev->GetHash();
527-
tx.vout.resize(1);
528-
tx.vout[0].nValue = 1*CENT;
529-
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
530-
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL));
531-
532-
orphanage.AddTx(MakeTransactionRef(tx), i);
533-
}
534-
535-
// This really-big orphan should be ignored:
536-
for (int i = 0; i < 10; i++)
537-
{
538-
CTransactionRef txPrev = orphanage.RandomOrphan();
539-
540-
CMutableTransaction tx;
541-
tx.vout.resize(1);
542-
tx.vout[0].nValue = 1*CENT;
543-
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
544-
tx.vin.resize(2777);
545-
for (unsigned int j = 0; j < tx.vin.size(); j++)
546-
{
547-
tx.vin[j].prevout.n = j;
548-
tx.vin[j].prevout.hash = txPrev->GetHash();
549-
}
550-
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL));
551-
// Re-use same signature for other inputs
552-
// (they don't have to be valid for this test)
553-
for (unsigned int j = 1; j < tx.vin.size(); j++)
554-
tx.vin[j].scriptSig = tx.vin[0].scriptSig;
555-
556-
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
557-
}
558-
559-
// Test EraseOrphansFor:
560-
for (NodeId i = 0; i < 3; i++)
561-
{
562-
size_t sizeBefore = orphanage.CountOrphans();
563-
orphanage.EraseForPeer(i);
564-
BOOST_CHECK(orphanage.CountOrphans() < sizeBefore);
565-
}
566-
567-
// Test LimitOrphanTxSize() function:
568-
orphanage.LimitOrphans(40);
569-
BOOST_CHECK(orphanage.CountOrphans() <= 40);
570-
orphanage.LimitOrphans(10);
571-
BOOST_CHECK(orphanage.CountOrphans() <= 10);
572-
orphanage.LimitOrphans(0);
573-
BOOST_CHECK(orphanage.CountOrphans() == 0);
574-
}
575-
576458
BOOST_AUTO_TEST_SUITE_END()

src/test/orphanage_tests.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) 2011-2022 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 <arith_uint256.h>
6+
#include <pubkey.h>
7+
#include <script/sign.h>
8+
#include <script/signingprovider.h>
9+
#include <script/standard.h>
10+
#include <test/util/setup_common.h>
11+
#include <txorphanage.h>
12+
13+
#include <array>
14+
#include <cstdint>
15+
16+
#include <boost/test/unit_test.hpp>
17+
18+
BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
19+
20+
class TxOrphanageTest : public TxOrphanage
21+
{
22+
public:
23+
inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
24+
{
25+
return m_orphans.size();
26+
}
27+
28+
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
29+
{
30+
std::map<uint256, OrphanTx>::iterator it;
31+
it = m_orphans.lower_bound(InsecureRand256());
32+
if (it == m_orphans.end())
33+
it = m_orphans.begin();
34+
return it->second.tx;
35+
}
36+
};
37+
38+
static void MakeNewKeyWithFastRandomContext(CKey& key)
39+
{
40+
std::vector<unsigned char> keydata;
41+
keydata = g_insecure_rand_ctx.randbytes(32);
42+
key.Set(keydata.data(), keydata.data() + keydata.size(), /*fCompressedIn=*/true);
43+
assert(key.IsValid());
44+
}
45+
46+
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
47+
{
48+
// This test had non-deterministic coverage due to
49+
// randomly selected seeds.
50+
// This seed is chosen so that all branches of the function
51+
// ecdsa_signature_parse_der_lax are executed during this test.
52+
// Specifically branches that run only when an ECDSA
53+
// signature's R and S values have leading zeros.
54+
g_insecure_rand_ctx = FastRandomContext{uint256{33}};
55+
56+
TxOrphanageTest orphanage;
57+
CKey key;
58+
MakeNewKeyWithFastRandomContext(key);
59+
FillableSigningProvider keystore;
60+
BOOST_CHECK(keystore.AddKey(key));
61+
62+
LOCK(g_cs_orphans);
63+
64+
// 50 orphan transactions:
65+
for (int i = 0; i < 50; i++)
66+
{
67+
CMutableTransaction tx;
68+
tx.vin.resize(1);
69+
tx.vin[0].prevout.n = 0;
70+
tx.vin[0].prevout.hash = InsecureRand256();
71+
tx.vin[0].scriptSig << OP_1;
72+
tx.vout.resize(1);
73+
tx.vout[0].nValue = 1*CENT;
74+
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
75+
76+
orphanage.AddTx(MakeTransactionRef(tx), i);
77+
}
78+
79+
// ... and 50 that depend on other orphans:
80+
for (int i = 0; i < 50; i++)
81+
{
82+
CTransactionRef txPrev = orphanage.RandomOrphan();
83+
84+
CMutableTransaction tx;
85+
tx.vin.resize(1);
86+
tx.vin[0].prevout.n = 0;
87+
tx.vin[0].prevout.hash = txPrev->GetHash();
88+
tx.vout.resize(1);
89+
tx.vout[0].nValue = 1*CENT;
90+
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
91+
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL));
92+
93+
orphanage.AddTx(MakeTransactionRef(tx), i);
94+
}
95+
96+
// This really-big orphan should be ignored:
97+
for (int i = 0; i < 10; i++)
98+
{
99+
CTransactionRef txPrev = orphanage.RandomOrphan();
100+
101+
CMutableTransaction tx;
102+
tx.vout.resize(1);
103+
tx.vout[0].nValue = 1*CENT;
104+
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
105+
tx.vin.resize(2777);
106+
for (unsigned int j = 0; j < tx.vin.size(); j++)
107+
{
108+
tx.vin[j].prevout.n = j;
109+
tx.vin[j].prevout.hash = txPrev->GetHash();
110+
}
111+
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL));
112+
// Re-use same signature for other inputs
113+
// (they don't have to be valid for this test)
114+
for (unsigned int j = 1; j < tx.vin.size(); j++)
115+
tx.vin[j].scriptSig = tx.vin[0].scriptSig;
116+
117+
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
118+
}
119+
120+
// Test EraseOrphansFor:
121+
for (NodeId i = 0; i < 3; i++)
122+
{
123+
size_t sizeBefore = orphanage.CountOrphans();
124+
orphanage.EraseForPeer(i);
125+
BOOST_CHECK(orphanage.CountOrphans() < sizeBefore);
126+
}
127+
128+
// Test LimitOrphanTxSize() function:
129+
orphanage.LimitOrphans(40);
130+
BOOST_CHECK(orphanage.CountOrphans() <= 40);
131+
orphanage.LimitOrphans(10);
132+
BOOST_CHECK(orphanage.CountOrphans() <= 10);
133+
orphanage.LimitOrphans(0);
134+
BOOST_CHECK(orphanage.CountOrphans() == 0);
135+
}
136+
137+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)