17 #include <boost/test/unit_test.hpp>
31 coinbaseTx.
vin.resize(1);
32 coinbaseTx.
vout.resize(1);
34 coinbaseTx.
vout[0].nValue = 1 *
CENT;
35 coinbaseTx.
vout[0].scriptPubKey = scriptPubKey;
57 std::vector<COutPoint> outpoints;
58 for (
size_t i{0}; i < num_outpoints; ++i) {
64 static inline std::vector<CPubKey>
random_keys(
size_t num_keys) {
65 std::vector<CPubKey> keys;
66 keys.reserve(num_keys);
67 for (
size_t i{0}; i < num_keys; ++i) {
80 mtx.
vin.resize(inputs.size());
82 for (
size_t i{0}; i < inputs.size(); ++i) {
83 mtx.
vin[i].prevout = inputs[i];
85 for (
auto i{0}; i < 25; ++i) {
87 mtx.
vout[i].nValue = 10000;
98 std::set<Txid> empty_conflicts_set;
115 const auto expected_error_str{
strprintf(
"non-v3 tx %s (wtxid=%s) cannot spend from v3 tx %s (wtxid=%s)",
116 tx_v2_from_v3->GetHash().ToString(), tx_v2_from_v3->GetWitnessHash().ToString(),
117 mempool_tx_v3->GetHash().ToString(), mempool_tx_v3->GetWitnessHash().ToString())};
122 Package package_v3_v2{mempool_tx_v3, tx_v2_from_v3};
130 auto tx_v2_from_v2_and_v3 =
make_tx({
COutPoint{mempool_tx_v3->GetHash(), 0},
COutPoint{mempool_tx_v2->GetHash(), 0}}, 2);
132 const auto expected_error_str_2{
strprintf(
"non-v3 tx %s (wtxid=%s) cannot spend from v3 tx %s (wtxid=%s)",
133 tx_v2_from_v2_and_v3->GetHash().ToString(), tx_v2_from_v2_and_v3->GetWitnessHash().ToString(),
134 mempool_tx_v3->GetHash().ToString(), mempool_tx_v3->GetWitnessHash().ToString())};
139 Package package_v3_v2_v2{mempool_tx_v3, mempool_tx_v2, tx_v2_from_v2_and_v3};
150 const auto expected_error_str{
strprintf(
"v3 tx %s (wtxid=%s) cannot spend from non-v3 tx %s (wtxid=%s)",
151 tx_v3_from_v2->GetHash().ToString(), tx_v3_from_v2->GetWitnessHash().ToString(),
152 mempool_tx_v2->GetHash().ToString(), mempool_tx_v2->GetWitnessHash().ToString())};
157 Package package_v2_v3{mempool_tx_v2, tx_v3_from_v2};
165 auto tx_v3_from_v2_and_v3 =
make_tx({
COutPoint{mempool_tx_v3->GetHash(), 0},
COutPoint{mempool_tx_v2->GetHash(), 0}}, 3);
167 const auto expected_error_str_2{
strprintf(
"v3 tx %s (wtxid=%s) cannot spend from non-v3 tx %s (wtxid=%s)",
168 tx_v3_from_v2_and_v3->GetHash().ToString(), tx_v3_from_v2_and_v3->GetWitnessHash().ToString(),
169 mempool_tx_v2->GetHash().ToString(), mempool_tx_v2->GetWitnessHash().ToString())};
175 const auto expected_error_str_3{
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
176 tx_v3_from_v2_and_v3->GetHash().ToString(), tx_v3_from_v2_and_v3->GetWitnessHash().ToString())};
177 Package package_v3_v2_v3{mempool_tx_v3, mempool_tx_v2, tx_v3_from_v2_and_v3};
190 Package package_v3_v3{mempool_tx_v3, tx_v3_from_v3};
201 Package package_v2_v2{mempool_tx_v2, tx_v2_from_v2};
209 std::vector<COutPoint> mempool_outpoints;
210 mempool_outpoints.emplace_back(mempool_tx_v3->GetHash(), 0);
211 package_multi_parents.emplace_back(mempool_tx_v3);
212 for (
size_t i{0}; i < 2; ++i) {
215 mempool_outpoints.emplace_back(mempool_tx->GetHash(), 0);
216 package_multi_parents.emplace_back(mempool_tx);
218 auto tx_v3_multi_parent =
make_tx(mempool_outpoints, 3);
219 package_multi_parents.emplace_back(tx_v3_multi_parent);
222 const auto expected_error_str{
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
223 tx_v3_multi_parent->GetHash().ToString(), tx_v3_multi_parent->GetWitnessHash().ToString())};
237 for (
size_t i{0}; i < 2; ++i) {
238 auto mempool_tx =
make_tx({last_outpoint}, 3);
240 last_outpoint =
COutPoint{mempool_tx->GetHash(), 0};
241 package_multi_gen.emplace_back(mempool_tx);
242 if (i == 1) middle_tx = mempool_tx;
244 auto tx_v3_multi_gen =
make_tx({last_outpoint}, 3);
245 package_multi_gen.emplace_back(tx_v3_multi_gen);
247 const auto expected_error_str{
strprintf(
"tx %s (wtxid=%s) would have too many ancestors",
248 tx_v3_multi_gen->GetHash().ToString(), tx_v3_multi_gen->GetWitnessHash().ToString())};
260 many_inputs.emplace_back(mempool_tx_v3->GetHash(), 0);
262 auto tx_v3_child_big =
make_tx(many_inputs, 3);
265 const auto expected_error_str{
strprintf(
"v3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
266 tx_v3_child_big->GetHash().ToString(), tx_v3_child_big->GetWitnessHash().ToString(), vsize,
V3_CHILD_MAX_VSIZE)};
271 Package package_child_big{mempool_tx_v3, tx_v3_child_big};
279 multisig_outpoints.emplace_back(mempool_tx_v3->GetHash(), 0);
282 script_multisig <<
OP_1;
283 for (
const auto& key : keys) {
290 for (
const auto& outpoint : multisig_outpoints) {
291 mtx_many_sigops.
vin.emplace_back(outpoint);
292 mtx_many_sigops.
vin.back().scriptWitness.stack.emplace_back(script_multisig.
begin(), script_multisig.
end());
294 mtx_many_sigops.
vout.resize(1);
296 mtx_many_sigops.
vout.back().nValue = 10000;
301 const int64_t total_sigops{
static_cast<int64_t
>(tx_many_sigops->vin.size()) *
static_cast<int64_t
>(script_multisig.
GetSigOpCount(
false))};
307 const auto expected_error_str{
strprintf(
"v3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
308 tx_many_sigops->GetHash().ToString(), tx_many_sigops->GetWitnessHash().ToString(),
310 auto result{
SingleV3Checks(tx_many_sigops, *ancestors, empty_conflicts_set,
315 Package package_child_sigops{mempool_tx_v3, tx_many_sigops};
321 auto tx_mempool_v3_child =
make_tx({
COutPoint{mempool_tx_v3->GetHash(), 0}}, 3);
328 Package package_v3_1p1c{mempool_tx_v3, tx_mempool_v3_child};
338 const auto expected_error_str{
strprintf(
"tx %s (wtxid=%s) would exceed descendant count limit",
339 mempool_tx_v3->GetHash().ToString(), mempool_tx_v3->GetWitnessHash().ToString())};
349 Package package_v3_1p2c{mempool_tx_v3, tx_mempool_v3_child, tx_v3_child2};
356 auto entry_mempool_parent = pool.
GetIter(mempool_tx_v3->GetHash().ToUint256()).value();
369 auto tx_mempool_sibling =
make_tx({
COutPoint{tx_mempool_grandparent->GetHash(), 0}}, 3);
370 auto tx_mempool_nibling =
make_tx({
COutPoint{tx_mempool_sibling->GetHash(), 0}}, 3);
371 auto tx_to_submit =
make_tx({
COutPoint{tx_mempool_grandparent->GetHash(), 1}}, 3);
378 const auto expected_error_str{
strprintf(
"tx %s (wtxid=%s) would exceed descendant count limit",
379 tx_mempool_grandparent->GetHash().ToString(), tx_mempool_grandparent->GetWitnessHash().ToString())};
#define Assert(val)
Identity function.
An encapsulated private key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
CPubKey GetPubKey() const
Compute the public key from a private key.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
The basic transaction that is broadcasted on the network and contained in blocks.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
util::Result< setEntries > CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
std::set< txiter, CompareIteratorByHash > setEntries
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
std::string GetRejectReason() const
static transaction_identifier FromUint256(const uint256 &id)
static int32_t GetTransactionWeight(const CTransaction &tx)
@ TX_CONSENSUS
invalid by consensus rules
static const int WITNESS_SCALE_FACTOR
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP
Default for -bytespersigop.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
std::vector< unsigned char > ToByteVector(const T &in)
static const int MAX_PUBKEYS_PER_MULTISIG
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
const TxValidationState m_state
Contains information about why the transaction failed.
@ INVALID
Fully validated, valid.
Identical to TestingSetup, but chain set to regtest.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Options struct containing limit options for a CTxMemPool.
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< ChainstateManager > chainman
static std::vector< COutPoint > random_outpoints(size_t num_outpoints)
static CTransactionRef make_tx(const std::vector< COutPoint > &inputs, int32_t version)
static std::vector< CPubKey > random_keys(size_t num_keys)
BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
Ensure that the mempool won't accept coinbase transactions.
std::optional< std::string > PackageV3Checks(const CTransactionRef &ptx, int64_t vsize, const Package &package, const CTxMemPool::setEntries &mempool_ancestors)
Must be called for every transaction that is submitted within a package, even if not v3.
std::optional< std::pair< std::string, CTransactionRef > > SingleV3Checks(const CTransactionRef &ptx, const CTxMemPool::setEntries &mempool_ancestors, const std::set< Txid > &direct_conflicts, int64_t vsize)
Must be called for every transaction, even if not v3.
static constexpr int64_t V3_CHILD_MAX_VSIZE
Maximum sigop-adjusted virtual size of a tx which spends from an unconfirmed v3 transaction.