Skip to content

Commit ae8e6df

Browse files
committed
[policy] limit package sizes
Maximum number of transactions allowed in a package is 25, equal to the default mempool descendant limit: if a package has more transactions than this, either it would fail default mempool descendant limit or the transactions don't all have a dependency relationship (but then they shouldn't be in a package together). Same rationale for 101KvB virtual size package limit. Note that these policies are only used in test accepts so far.
1 parent c9e1a26 commit ae8e6df

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/policy/packages.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
#include <vector>
1212

13+
/** Default maximum number of transactions in a package. */
14+
static constexpr uint32_t MAX_PACKAGE_COUNT{25};
15+
/** Default maximum total virtual size of transactions in a package in KvB. */
16+
static constexpr uint32_t MAX_PACKAGE_SIZE{101};
17+
1318
/** A "reason" why a package was invalid. It may be that one or more of the included
1419
* transactions is invalid or the package itself violates our rules.
1520
* We don't distinguish between consensus and policy violations right now.

src/test/txvalidation_tests.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <consensus/validation.h>
66
#include <key_io.h>
7+
#include <policy/packages.h>
8+
#include <policy/policy.h>
79
#include <primitives/transaction.h>
810
#include <script/script.h>
911
#include <script/standard.h>
@@ -49,6 +51,25 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
4951
BOOST_CHECK(result.m_state.GetResult() == TxValidationResult::TX_CONSENSUS);
5052
}
5153

54+
// Create placeholder transactions that have no meaning.
55+
inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
56+
{
57+
CMutableTransaction mtx = CMutableTransaction();
58+
mtx.vin.resize(num_inputs);
59+
mtx.vout.resize(num_outputs);
60+
auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256());
61+
for (size_t i{0}; i < num_inputs; ++i) {
62+
mtx.vin[i].prevout.hash = InsecureRand256();
63+
mtx.vin[i].prevout.n = 0;
64+
mtx.vin[i].scriptSig = random_script;
65+
}
66+
for (size_t o{0}; o < num_outputs; ++o) {
67+
mtx.vout[o].nValue = 1 * CENT;
68+
mtx.vout[o].scriptPubKey = random_script;
69+
}
70+
return MakeTransactionRef(mtx);
71+
}
72+
5273
BOOST_FIXTURE_TEST_CASE(package_tests, TestChain100Setup)
5374
{
5475
LOCK(cs_main);
@@ -84,6 +105,43 @@ BOOST_FIXTURE_TEST_CASE(package_tests, TestChain100Setup)
84105
BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
85106
"Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
86107

108+
// Packages can't have more than 25 transactions.
109+
Package package_too_many;
110+
package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
111+
for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
112+
package_too_many.emplace_back(create_placeholder_tx(1, 1));
113+
}
114+
auto result_too_many = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_too_many, /* test_accept */ true);
115+
BOOST_CHECK(result_too_many.m_state.IsInvalid());
116+
BOOST_CHECK_EQUAL(result_too_many.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
117+
BOOST_CHECK_EQUAL(result_too_many.m_state.GetRejectReason(), "package-too-many-transactions");
118+
119+
// Packages can't have a total size of more than 101KvB.
120+
CTransactionRef large_ptx = create_placeholder_tx(150, 150);
121+
Package package_too_large;
122+
auto size_large = GetVirtualTransactionSize(*large_ptx);
123+
size_t total_size{0};
124+
while (total_size <= MAX_PACKAGE_SIZE * 1000) {
125+
package_too_large.push_back(large_ptx);
126+
total_size += size_large;
127+
}
128+
BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
129+
auto result_too_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_too_large, /* test_accept */ true);
130+
BOOST_CHECK(result_too_large.m_state.IsInvalid());
131+
BOOST_CHECK_EQUAL(result_too_large.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
132+
BOOST_CHECK_EQUAL(result_too_large.m_state.GetRejectReason(), "package-too-large");
133+
134+
// A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
135+
CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
136+
BOOST_CHECK(GetVirtualTransactionSize(*giant_ptx) > MAX_PACKAGE_SIZE * 1000);
137+
auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {giant_ptx}, /* test_accept */ true);
138+
BOOST_CHECK(result_single_large.m_state.IsInvalid());
139+
BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
140+
BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
141+
auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
142+
BOOST_CHECK(it_giant_tx != result_single_large.m_tx_results.end());
143+
BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
144+
87145
// Check that mempool size hasn't changed.
88146
BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
89147
}

src/validation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,20 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::
10791079
PackageValidationState package_state;
10801080
const unsigned int package_count = txns.size();
10811081

1082+
// These context-free package limits can be checked before taking the mempool lock.
1083+
if (package_count > MAX_PACKAGE_COUNT) {
1084+
package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-many-transactions");
1085+
return PackageMempoolAcceptResult(package_state, {});
1086+
}
1087+
1088+
const int64_t total_size = std::accumulate(txns.cbegin(), txns.cend(), 0,
1089+
[](int64_t sum, const auto& tx) { return sum + GetVirtualTransactionSize(*tx); });
1090+
// If the package only contains 1 tx, it's better to report the policy violation on individual tx size.
1091+
if (package_count > 1 && total_size > MAX_PACKAGE_SIZE * 1000) {
1092+
package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-large");
1093+
return PackageMempoolAcceptResult(package_state, {});
1094+
}
1095+
10821096
std::vector<Workspace> workspaces{};
10831097
workspaces.reserve(package_count);
10841098
std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces), [](const auto& tx) {

0 commit comments

Comments
 (0)