Skip to content

Commit b8f17fb

Browse files
committed
[tests] Move TxOrphange tests to orphange_tests.cpp
1 parent b1c5991 commit b8f17fb

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
@@ -110,6 +110,7 @@ BITCOIN_TESTS =\
110110
test/net_peer_eviction_tests.cpp \
111111
test/net_tests.cpp \
112112
test/netbase_tests.cpp \
113+
test/orphanage_tests.cpp \
113114
test/pmt_tests.cpp \
114115
test/policy_fee_tests.cpp \
115116
test/policyestimator_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 <serialize.h>
1616
#include <test/util/net.h>
1717
#include <test/util/setup_common.h>
18-
#include <txorphanage.h>
1918
#include <util/string.h>
2019
#include <util/system.h>
2120
#include <util/time.h>
@@ -429,121 +428,4 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
429428
peerLogic->FinalizeNode(dummyNode);
430429
}
431430

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