|
| 1 | +// Copyright (c) 2017 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 <validation.h> |
| 6 | +#include <txmempool.h> |
| 7 | +#include <amount.h> |
| 8 | +#include <consensus/validation.h> |
| 9 | +#include <primitives/transaction.h> |
| 10 | +#include <script/script.h> |
| 11 | +#include <test/test_bitcoin.h> |
| 12 | + |
| 13 | +#include <boost/test/unit_test.hpp> |
| 14 | + |
| 15 | + |
| 16 | +BOOST_AUTO_TEST_SUITE(txvalidation_tests) |
| 17 | + |
| 18 | +/** |
| 19 | + * Ensure that the mempool won't accept coinbase transactions. |
| 20 | + */ |
| 21 | +BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup) |
| 22 | +{ |
| 23 | + CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG; |
| 24 | + CMutableTransaction coinbaseTx; |
| 25 | + |
| 26 | + coinbaseTx.nVersion = 1; |
| 27 | + coinbaseTx.vin.resize(1); |
| 28 | + coinbaseTx.vout.resize(1); |
| 29 | + coinbaseTx.vin[0].scriptSig = CScript() << OP_11 << OP_EQUAL; |
| 30 | + coinbaseTx.vout[0].nValue = 1 * CENT; |
| 31 | + coinbaseTx.vout[0].scriptPubKey = scriptPubKey; |
| 32 | + |
| 33 | + assert(CTransaction(coinbaseTx).IsCoinBase()); |
| 34 | + |
| 35 | + CValidationState state; |
| 36 | + |
| 37 | + LOCK(cs_main); |
| 38 | + |
| 39 | + unsigned int initialPoolSize = mempool.size(); |
| 40 | + |
| 41 | + BOOST_CHECK_EQUAL( |
| 42 | + false, |
| 43 | + AcceptToMemoryPool(mempool, state, MakeTransactionRef(coinbaseTx), |
| 44 | + nullptr /* pfMissingInputs */, |
| 45 | + nullptr /* plTxnReplaced */, |
| 46 | + true /* bypass_limits */, |
| 47 | + 0 /* nAbsurdFee */)); |
| 48 | + |
| 49 | + // Check that the transaction hasn't been added to mempool. |
| 50 | + BOOST_CHECK_EQUAL(mempool.size(), initialPoolSize); |
| 51 | + |
| 52 | + // Check that the validation state reflects the unsuccesful attempt. |
| 53 | + BOOST_CHECK(state.IsInvalid()); |
| 54 | + BOOST_CHECK_EQUAL(state.GetRejectReason(), "coinbase"); |
| 55 | + |
| 56 | + int nDoS; |
| 57 | + BOOST_CHECK_EQUAL(state.IsInvalid(nDoS), true); |
| 58 | + BOOST_CHECK_EQUAL(nDoS, 100); |
| 59 | +} |
| 60 | + |
| 61 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments