Bitcoin Core  27.99.0
P2P Digital Currency
orphanage_tests.cpp
Go to the documentation of this file.
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>
7 #include <pubkey.h>
8 #include <script/sign.h>
10 #include <test/util/random.h>
11 #include <test/util/setup_common.h>
12 #include <txorphanage.h>
13 
14 #include <array>
15 #include <cstdint>
16 
17 #include <boost/test/unit_test.hpp>
18 
19 BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
20 
22 {
23 public:
24  inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
25  {
26  LOCK(m_mutex);
27  return m_orphans.size();
28  }
29 
31  {
32  LOCK(m_mutex);
33  std::map<Wtxid, OrphanTx>::iterator it;
34  it = m_orphans.lower_bound(Wtxid::FromUint256(InsecureRand256()));
35  if (it == m_orphans.end())
36  it = m_orphans.begin();
37  return it->second.tx;
38  }
39 };
40 
42 {
43  std::vector<unsigned char> keydata;
44  keydata = rand_ctx.randbytes(32);
45  key.Set(keydata.data(), keydata.data() + keydata.size(), /*fCompressedIn=*/true);
46  assert(key.IsValid());
47 }
48 
49 // Creates a transaction with 2 outputs. Spends all outpoints. If outpoints is empty, spends a random one.
50 static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, FastRandomContext& det_rand)
51 {
52  CKey key;
53  MakeNewKeyWithFastRandomContext(key, det_rand);
55  // If no outpoints are given, create a random one.
56  if (outpoints.empty()) {
57  tx.vin.emplace_back(Txid::FromUint256(det_rand.rand256()), 0);
58  } else {
59  for (const auto& outpoint : outpoints) {
60  tx.vin.emplace_back(outpoint);
61  }
62  }
63  // Ensure txid != wtxid
64  tx.vin[0].scriptWitness.stack.push_back({1});
65  tx.vout.resize(2);
66  tx.vout[0].nValue = CENT;
67  tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
68  tx.vout[1].nValue = 3 * CENT;
69  tx.vout[1].scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(key.GetPubKey()));
70  return MakeTransactionRef(tx);
71 }
72 
73 // Make another (not necessarily valid) tx with the same txid but different wtxid.
75 {
76  CMutableTransaction tx(*ptx);
77  tx.vin[0].scriptWitness.stack.push_back({5});
78  auto mutated_tx = MakeTransactionRef(tx);
79  assert(ptx->GetHash() == mutated_tx->GetHash());
80  return mutated_tx;
81 }
82 
83 static bool EqualTxns(const std::set<CTransactionRef>& set_txns, const std::vector<CTransactionRef>& vec_txns)
84 {
85  if (vec_txns.size() != set_txns.size()) return false;
86  for (const auto& tx : vec_txns) {
87  if (!set_txns.contains(tx)) return false;
88  }
89  return true;
90 }
91 static bool EqualTxns(const std::set<CTransactionRef>& set_txns,
92  const std::vector<std::pair<CTransactionRef, NodeId>>& vec_txns)
93 {
94  if (vec_txns.size() != set_txns.size()) return false;
95  for (const auto& [tx, nodeid] : vec_txns) {
96  if (!set_txns.contains(tx)) return false;
97  }
98  return true;
99 }
100 
101 BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
102 {
103  // This test had non-deterministic coverage due to
104  // randomly selected seeds.
105  // This seed is chosen so that all branches of the function
106  // ecdsa_signature_parse_der_lax are executed during this test.
107  // Specifically branches that run only when an ECDSA
108  // signature's R and S values have leading zeros.
110 
111  TxOrphanageTest orphanage;
112  CKey key;
114  FillableSigningProvider keystore;
115  BOOST_CHECK(keystore.AddKey(key));
116 
117  // 50 orphan transactions:
118  for (int i = 0; i < 50; i++)
119  {
121  tx.vin.resize(1);
122  tx.vin[0].prevout.n = 0;
123  tx.vin[0].prevout.hash = Txid::FromUint256(InsecureRand256());
124  tx.vin[0].scriptSig << OP_1;
125  tx.vout.resize(1);
126  tx.vout[0].nValue = 1*CENT;
127  tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
128 
129  orphanage.AddTx(MakeTransactionRef(tx), i);
130  }
131 
132  // ... and 50 that depend on other orphans:
133  for (int i = 0; i < 50; i++)
134  {
135  CTransactionRef txPrev = orphanage.RandomOrphan();
136 
138  tx.vin.resize(1);
139  tx.vin[0].prevout.n = 0;
140  tx.vin[0].prevout.hash = txPrev->GetHash();
141  tx.vout.resize(1);
142  tx.vout[0].nValue = 1*CENT;
143  tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
144  SignatureData empty;
145  BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
146 
147  orphanage.AddTx(MakeTransactionRef(tx), i);
148  }
149 
150  // This really-big orphan should be ignored:
151  for (int i = 0; i < 10; i++)
152  {
153  CTransactionRef txPrev = orphanage.RandomOrphan();
154 
156  tx.vout.resize(1);
157  tx.vout[0].nValue = 1*CENT;
158  tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
159  tx.vin.resize(2777);
160  for (unsigned int j = 0; j < tx.vin.size(); j++)
161  {
162  tx.vin[j].prevout.n = j;
163  tx.vin[j].prevout.hash = txPrev->GetHash();
164  }
165  SignatureData empty;
166  BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
167  // Reuse same signature for other inputs
168  // (they don't have to be valid for this test)
169  for (unsigned int j = 1; j < tx.vin.size(); j++)
170  tx.vin[j].scriptSig = tx.vin[0].scriptSig;
171 
172  BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
173  }
174 
175  // Test EraseOrphansFor:
176  for (NodeId i = 0; i < 3; i++)
177  {
178  size_t sizeBefore = orphanage.CountOrphans();
179  orphanage.EraseForPeer(i);
180  BOOST_CHECK(orphanage.CountOrphans() < sizeBefore);
181  }
182 
183  // Test LimitOrphanTxSize() function:
184  FastRandomContext rng{/*fDeterministic=*/true};
185  orphanage.LimitOrphans(40, rng);
186  BOOST_CHECK(orphanage.CountOrphans() <= 40);
187  orphanage.LimitOrphans(10, rng);
188  BOOST_CHECK(orphanage.CountOrphans() <= 10);
189  orphanage.LimitOrphans(0, rng);
190  BOOST_CHECK(orphanage.CountOrphans() == 0);
191 }
192 
193 BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
194 {
195  FastRandomContext det_rand{true};
196  TxOrphanage orphanage;
197  NodeId peer{0};
198 
199  std::vector<COutPoint> empty_outpoints;
200  auto parent = MakeTransactionSpending(empty_outpoints, det_rand);
201 
202  // Create children to go into orphanage.
203  auto child_normal = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
204  auto child_mutated = MakeMutation(child_normal);
205 
206  const auto& normal_wtxid = child_normal->GetWitnessHash();
207  const auto& mutated_wtxid = child_mutated->GetWitnessHash();
208  BOOST_CHECK(normal_wtxid != mutated_wtxid);
209 
210  BOOST_CHECK(orphanage.AddTx(child_normal, peer));
211  // EraseTx fails as transaction by this wtxid doesn't exist.
212  BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 0);
213  BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
214  BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
215 
216  // Must succeed. Both transactions should be present in orphanage.
217  BOOST_CHECK(orphanage.AddTx(child_mutated, peer));
218  BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
219  BOOST_CHECK(orphanage.HaveTx(mutated_wtxid));
220 
221  // Outpoints map should track all entries: check that both are returned as children of the parent.
222  std::set<CTransactionRef> expected_children{child_normal, child_mutated};
223  BOOST_CHECK(EqualTxns(expected_children, orphanage.GetChildrenFromSamePeer(parent, peer)));
224 
225  // Erase by wtxid: mutated first
226  BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 1);
227  BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
228  BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
229 
230  BOOST_CHECK_EQUAL(orphanage.EraseTx(normal_wtxid), 1);
231  BOOST_CHECK(!orphanage.HaveTx(normal_wtxid));
232  BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
233 }
234 
235 
236 BOOST_AUTO_TEST_CASE(get_children)
237 {
238  FastRandomContext det_rand{true};
239  std::vector<COutPoint> empty_outpoints;
240 
241  auto parent1 = MakeTransactionSpending(empty_outpoints, det_rand);
242  auto parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
243 
244  // Make sure these parents have different txids otherwise this test won't make sense.
245  while (parent1->GetHash() == parent2->GetHash()) {
246  parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
247  }
248 
249  // Create children to go into orphanage.
250  auto child_p1n0 = MakeTransactionSpending({{parent1->GetHash(), 0}}, det_rand);
251  auto child_p2n1 = MakeTransactionSpending({{parent2->GetHash(), 1}}, det_rand);
252  // Spends the same tx twice. Should not cause duplicates.
253  auto child_p1n0_p1n1 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent1->GetHash(), 1}}, det_rand);
254  // Spends the same outpoint as previous tx. Should still be returned; don't assume outpoints are unique.
255  auto child_p1n0_p2n0 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent2->GetHash(), 0}}, det_rand);
256 
257  const NodeId node1{1};
258  const NodeId node2{2};
259 
260  // All orphans provided by node1
261  {
262  TxOrphanage orphanage;
263  BOOST_CHECK(orphanage.AddTx(child_p1n0, node1));
264  BOOST_CHECK(orphanage.AddTx(child_p2n1, node1));
265  BOOST_CHECK(orphanage.AddTx(child_p1n0_p1n1, node1));
266  BOOST_CHECK(orphanage.AddTx(child_p1n0_p2n0, node1));
267 
268  std::set<CTransactionRef> expected_parent1_children{child_p1n0, child_p1n0_p2n0, child_p1n0_p1n1};
269  std::set<CTransactionRef> expected_parent2_children{child_p2n1, child_p1n0_p2n0};
270 
271  BOOST_CHECK(EqualTxns(expected_parent1_children, orphanage.GetChildrenFromSamePeer(parent1, node1)));
272  BOOST_CHECK(EqualTxns(expected_parent2_children, orphanage.GetChildrenFromSamePeer(parent2, node1)));
273 
274  BOOST_CHECK(EqualTxns(expected_parent1_children, orphanage.GetChildrenFromDifferentPeer(parent1, node2)));
275  BOOST_CHECK(EqualTxns(expected_parent2_children, orphanage.GetChildrenFromDifferentPeer(parent2, node2)));
276 
277  // The peer must match
278  BOOST_CHECK(orphanage.GetChildrenFromSamePeer(parent1, node2).empty());
279  BOOST_CHECK(orphanage.GetChildrenFromSamePeer(parent2, node2).empty());
280 
281  // There shouldn't be any children of this tx in the orphanage
282  BOOST_CHECK(orphanage.GetChildrenFromSamePeer(child_p1n0_p2n0, node1).empty());
283  BOOST_CHECK(orphanage.GetChildrenFromSamePeer(child_p1n0_p2n0, node2).empty());
284  BOOST_CHECK(orphanage.GetChildrenFromDifferentPeer(child_p1n0_p2n0, node1).empty());
285  BOOST_CHECK(orphanage.GetChildrenFromDifferentPeer(child_p1n0_p2n0, node2).empty());
286  }
287 
288  // Orphans provided by node1 and node2
289  {
290  TxOrphanage orphanage;
291  BOOST_CHECK(orphanage.AddTx(child_p1n0, node1));
292  BOOST_CHECK(orphanage.AddTx(child_p2n1, node1));
293  BOOST_CHECK(orphanage.AddTx(child_p1n0_p1n1, node2));
294  BOOST_CHECK(orphanage.AddTx(child_p1n0_p2n0, node2));
295 
296  // +----------------+---------------+----------------------------------+
297  // | | sender=node1 | sender=node2 |
298  // +----------------+---------------+----------------------------------+
299  // | spends parent1 | child_p1n0 | child_p1n0_p1n1, child_p1n0_p2n0 |
300  // | spends parent2 | child_p2n1 | child_p1n0_p2n0 |
301  // +----------------+---------------+----------------------------------+
302 
303  // Children of parent1 from node1:
304  {
305  std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
306 
307  BOOST_CHECK(EqualTxns(expected_parent1_node1, orphanage.GetChildrenFromSamePeer(parent1, node1)));
308  BOOST_CHECK(EqualTxns(expected_parent1_node1, orphanage.GetChildrenFromDifferentPeer(parent1, node2)));
309  }
310 
311  // Children of parent2 from node1:
312  {
313  std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
314 
315  BOOST_CHECK(EqualTxns(expected_parent2_node1, orphanage.GetChildrenFromSamePeer(parent2, node1)));
316  BOOST_CHECK(EqualTxns(expected_parent2_node1, orphanage.GetChildrenFromDifferentPeer(parent2, node2)));
317  }
318 
319  // Children of parent1 from node2:
320  {
321  std::set<CTransactionRef> expected_parent1_node2{child_p1n0_p1n1, child_p1n0_p2n0};
322 
323  BOOST_CHECK(EqualTxns(expected_parent1_node2, orphanage.GetChildrenFromSamePeer(parent1, node2)));
324  BOOST_CHECK(EqualTxns(expected_parent1_node2, orphanage.GetChildrenFromDifferentPeer(parent1, node1)));
325  }
326 
327  // Children of parent2 from node2:
328  {
329  std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
330 
331  BOOST_CHECK(EqualTxns(expected_parent2_node2, orphanage.GetChildrenFromSamePeer(parent2, node2)));
332  BOOST_CHECK(EqualTxns(expected_parent2_node2, orphanage.GetChildrenFromDifferentPeer(parent2, node1)));
333  }
334  }
335 }
336 
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
An encapsulated private key.
Definition: key.h:33
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:119
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:188
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:99
Fast randomness source.
Definition: random.h:145
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:662
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddKey(const CKey &key)
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:21
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the orphanage to the given maximum.
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new orphan transaction.
Definition: txorphanage.cpp:20
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx but were not received from nodeid.
bool HaveTx(const Wtxid &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an orphan transaction (by wtxid only)
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:97
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx and were received from nodeid.
int EraseTx(const Wtxid &wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by wtxid.
Definition: txorphanage.cpp:55
size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:106
BOOST_AUTO_TEST_SUITE_END()
@ SIGHASH_ALL
Definition: interpreter.h:30
int64_t NodeId
Definition: net.h:97
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static CTransactionRef MakeMutation(const CTransactionRef &ptx)
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
static void MakeNewKeyWithFastRandomContext(CKey &key, FastRandomContext &rand_ctx=g_insecure_rand_ctx)
static CTransactionRef MakeTransactionSpending(const std::vector< COutPoint > &outpoints, FastRandomContext &det_rand)
static bool EqualTxns(const std::set< CTransactionRef > &set_txns, const std::vector< CTransactionRef > &vec_txns)
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_1
Definition: script.h:82
static constexpr CAmount CENT
Definition: setup_common.h:49
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType, SignatureData &sig_data)
Produce a satisfying script (scriptSig or witness).
Definition: sign.cpp:694
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
Testing setup that configures a complete environment.
Definition: setup_common.h:85
#define LOCK(cs)
Definition: sync.h:257
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
Definition: random.cpp:14
static uint256 InsecureRand256()
Definition: random.h:50
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
assert(!tx.IsCoinBase())