Bitcoin Core  22.99.0
P2P Digital Currency
txvalidation_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 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 <consensus/validation.h>
6 #include <key_io.h>
7 #include <policy/packages.h>
8 #include <policy/policy.h>
10 #include <script/script.h>
11 #include <script/standard.h>
12 #include <test/util/setup_common.h>
13 #include <validation.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 
18 BOOST_AUTO_TEST_SUITE(txvalidation_tests)
19 
20 
23 BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
24 {
25  CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
26  CMutableTransaction coinbaseTx;
27 
28  coinbaseTx.nVersion = 1;
29  coinbaseTx.vin.resize(1);
30  coinbaseTx.vout.resize(1);
31  coinbaseTx.vin[0].scriptSig = CScript() << OP_11 << OP_EQUAL;
32  coinbaseTx.vout[0].nValue = 1 * CENT;
33  coinbaseTx.vout[0].scriptPubKey = scriptPubKey;
34 
35  BOOST_CHECK(CTransaction(coinbaseTx).IsCoinBase());
36 
37  LOCK(cs_main);
38 
39  unsigned int initialPoolSize = m_node.mempool->size();
40  const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(coinbaseTx));
41 
43 
44  // Check that the transaction hasn't been added to mempool.
45  BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
46 
47  // Check that the validation state reflects the unsuccessful attempt.
48  BOOST_CHECK(result.m_state.IsInvalid());
49  BOOST_CHECK_EQUAL(result.m_state.GetRejectReason(), "coinbase");
51 }
policy.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:366
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
MempoolAcceptResult::m_state
const TxValidationState m_state
Contains information about why the transaction failed.
Definition: validation.h:172
key_io.h
setup_common.h
transaction.h
MempoolAcceptResult::m_result_type
const ResultType m_result_type
Result type.
Definition: validation.h:169
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:368
m_node
node::NodeContext m_node
Definition: bitcoin-gui.cpp:36
validation.h
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:407
node::NodeContext::mempool
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:45
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:279
packages.h
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
OP_EQUAL
@ OP_EQUAL
Definition: script.h:139
MempoolAcceptResult::ResultType::INVALID
@ INVALID
Fully validated, valid.
CENT
static constexpr CAmount CENT
Definition: setup_common.h:78
standard.h
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:124
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
ValidationState::GetResult
Result GetResult() const
Definition: validation.h:123
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:367
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
Ensure that the mempool won't accept coinbase transactions.
Definition: txvalidation_tests.cpp:23
TxValidationResult::TX_CONSENSUS
@ TX_CONSENSUS
invalid by consensus rules
LOCK
#define LOCK(cs)
Definition: sync.h:226
MempoolAcceptResult
Validation result for a single transaction mempool acceptance.
Definition: validation.h:160
node::NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:48
ValidationState::GetRejectReason
std::string GetRejectReason() const
Definition: validation.h:124
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:364
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
OP_11
@ OP_11
Definition: script.h:87
ValidationState::IsInvalid
bool IsInvalid() const
Definition: validation.h:121
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18