Skip to content

Commit 901ba3e

Browse files
author
MarcoFalke
committed
Merge #11714: [tests] Test that mempool rejects coinbase transactions
65e91f5 [tests] Test that mempool rejects coinbase transactions (James O'Beirne) Pull request description: ![selection_063](https://user-images.githubusercontent.com/73197/32978622-b0fa9d70-cbfa-11e7-9a72-1997409e5ba8.png) Neither the unit nor functional tests appear to cover rejecting a transaction from acceptance to the mempool on the basis of it being a coinbase. Seems like a decent thing to have a test for. Tree-SHA512: 53af53c975cad5d7a21c443d71a1c0ced5c70a7799b75bb44d9b7dd6ab2afbcdcaab14571540efeb848f3a1daee5e1dd856530d8f2b50582595219a1c17555ff
2 parents 7a43fbb + 65e91f5 commit 901ba3e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ BITCOIN_TESTS =\
8080
test/timedata_tests.cpp \
8181
test/torcontrol_tests.cpp \
8282
test/transaction_tests.cpp \
83+
test/txvalidation_tests.cpp \
8384
test/txvalidationcache_tests.cpp \
8485
test/versionbits_tests.cpp \
8586
test/uint256_tests.cpp \

src/test/txvalidation_tests.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)