Skip to content

Commit f66011e

Browse files
committed
Merge bitcoin/bitcoin#30784: test: add check that too large txs aren't put into orphanage
66d13c8 test: add check that large txs aren't put into orphanage (Sebastian Falbesoner) ed7d224 test: add `BulkTransaction` helper to unit test transaction utils (Sebastian Falbesoner) Pull request description: This PR adds test coverage for the following check in `TxOrphanage::AddTx`, where large orphan txs are ignored in order to avoid memory exhaustion attacks: https://github.com/bitcoin/bitcoin/blob/5abb9b1af49be9024f95fa2f777285c531785d85/src/txorphanage.cpp#L22-L34 Note that this code-path isn't reachable under normal circumstances, as txs larger than `MAX_STANDARD_TX_WEIGHT` are already rejected earlier in the course of doing the mempool standardness checks (see `MemPoolAccept::PreChecks` -> `IsStandardTx` -> `reason = "tx-size";`), so this is only relevant if tx standardness rules are disabled via `-acceptnonstdtxns=1`. The ignore path is checked ~~by asserting the debug log, which is ugly, but as far as I know there is currently no way to access the orphanage entries from the outside~~ via unit test that checks the return value of `AddTx`. As an alternative to adding test coverage, one might consider removing this check altogether (or replacing it with an `Assume`), as it's redundant as explained above. ACKs for top commit: maflcko: review ACK 66d13c8 glozow: ACK 66d13c8 tdb3: re-ACK 66d13c8 Tree-SHA512: 88e8254ab5fca70c387a5992649ea6a704a65162999be972cc86bd74fc26c5f0f1e13e04856708d07ad5524cb77c0918e19663db92b3593e842469dfe04af6a1
2 parents ab317ad + 66d13c8 commit f66011e

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/test/orphanage_tests.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <arith_uint256.h>
6+
#include <consensus/validation.h>
7+
#include <policy/policy.h>
68
#include <primitives/transaction.h>
79
#include <pubkey.h>
810
#include <script/sign.h>
911
#include <script/signingprovider.h>
1012
#include <test/util/random.h>
1113
#include <test/util/setup_common.h>
14+
#include <test/util/transaction_utils.h>
1215
#include <txorphanage.h>
1316

1417
#include <array>
@@ -370,4 +373,21 @@ BOOST_AUTO_TEST_CASE(get_children)
370373
}
371374
}
372375

376+
BOOST_AUTO_TEST_CASE(too_large_orphan_tx)
377+
{
378+
TxOrphanage orphanage;
379+
CMutableTransaction tx;
380+
tx.vin.resize(1);
381+
382+
// check that txs larger than MAX_STANDARD_TX_WEIGHT are not added to the orphanage
383+
BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT + 4);
384+
BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT + 4);
385+
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), 0));
386+
387+
tx.vout.clear();
388+
BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT);
389+
BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT);
390+
BOOST_CHECK(orphanage.AddTx(MakeTransactionRef(tx), 0));
391+
}
392+
373393
BOOST_AUTO_TEST_SUITE_END()

src/test/util/transaction_utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <coins.h>
6+
#include <consensus/validation.h>
67
#include <script/signingprovider.h>
78
#include <test/util/transaction_utils.h>
89

@@ -69,3 +70,23 @@ std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keyst
6970

7071
return dummyTransactions;
7172
}
73+
74+
void BulkTransaction(CMutableTransaction& tx, int32_t target_weight)
75+
{
76+
tx.vout.emplace_back(0, CScript() << OP_RETURN);
77+
auto unpadded_weight{GetTransactionWeight(CTransaction(tx))};
78+
assert(target_weight >= unpadded_weight);
79+
80+
// determine number of needed padding bytes by converting weight difference to vbytes
81+
auto dummy_vbytes = (target_weight - unpadded_weight + (WITNESS_SCALE_FACTOR - 1)) / WITNESS_SCALE_FACTOR;
82+
// compensate for the increase of the compact-size encoded script length
83+
// (note that the length encoding of the unpadded output script needs one byte)
84+
dummy_vbytes -= GetSizeOfCompactSize(dummy_vbytes) - 1;
85+
86+
// pad transaction by repeatedly appending a dummy opcode to the output script
87+
tx.vout[0].scriptPubKey.insert(tx.vout[0].scriptPubKey.end(), dummy_vbytes, OP_1);
88+
89+
// actual weight should be at most 3 higher than target weight
90+
assert(GetTransactionWeight(CTransaction(tx)) >= target_weight);
91+
assert(GetTransactionWeight(CTransaction(tx)) <= target_weight + 3);
92+
}

src/test/util/transaction_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CSc
2626
// the second nValues[2] and nValues[3] outputs paid to a TxoutType::PUBKEYHASH.
2727
std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keystoreRet, CCoinsViewCache& coinsRet, const std::array<CAmount,4>& nValues);
2828

29+
// bulk transaction to reach a certain target weight,
30+
// by appending a single output with padded output script
31+
void BulkTransaction(CMutableTransaction& tx, int32_t target_weight);
32+
2933
#endif // BITCOIN_TEST_UTIL_TRANSACTION_UTILS_H

0 commit comments

Comments
 (0)