18#include <validation.h>
25#include <boost/test/unit_test.hpp>
52constexpr static struct {
55}
BLOCKINFO[]{{8, 582909131}, {0, 971462344}, {2, 1169481553}, {6, 66147495}, {7, 427785981}, {8, 80538907},
56 {8, 207348013}, {2, 1951240923}, {4, 215054351}, {1, 491520534}, {8, 1282281282}, {4, 639565734},
57 {3, 248274685}, {8, 1160085976}, {6, 396349768}, {5, 393780549}, {5, 1096899528}, {4, 965381630},
58 {0, 728758712}, {5, 318638310}, {3, 164591898}, {2, 274234550}, {2, 254411237}, {7, 561761812},
59 {2, 268342573}, {0, 402816691}, {1, 221006382}, {6, 538872455}, {7, 393315655}, {4, 814555937},
60 {7, 504879194}, {6, 467769648}, {3, 925972193}, {2, 200581872}, {3, 168915404}, {8, 430446262},
61 {5, 773507406}, {3, 1195366164}, {0, 433361157}, {3, 297051771}, {0, 558856551}, {2, 501614039},
62 {3, 528488272}, {2, 473587734}, {8, 230125274}, {2, 494084400}, {4, 357314010}, {8, 60361686},
63 {7, 640624687}, {3, 480441695}, {8, 1424447925}, {4, 752745419}, {1, 288532283}, {6, 669170574},
64 {5, 1900907591}, {3, 555326037}, {3, 1121014051}, {0, 545835650}, {8, 189196651}, {5, 252371575},
65 {0, 199163095}, {6, 558895874}, {6, 1656839784}, {6, 815175452}, {6, 718677851}, {5, 544000334},
66 {0, 340113484}, {6, 850744437}, {4, 496721063}, {8, 524715182}, {6, 574361898}, {6, 1642305743},
67 {6, 355110149}, {5, 1647379658}, {8, 1103005356}, {7, 556460625}, {3, 1139533992}, {5, 304736030},
68 {2, 361539446}, {2, 143720360}, {6, 201939025}, {7, 423141476}, {4, 574633709}, {3, 1412254823},
69 {4, 873254135}, {0, 341817335}, {6, 53501687}, {3, 179755410}, {5, 172209688}, {8, 516810279},
70 {4, 1228391489}, {8, 325372589}, {6, 550367589}, {0, 876291812}, {7, 412454120}, {7, 717202854},
71 {2, 222677843}, {6, 251778867}, {7, 842004420}, {7, 194762829}, {4, 96668841}, {1, 925485796},
72 {0, 792342903}, {6, 678455063}, {6, 773251385}, {5, 186617471}, {6, 883189502}, {7, 396077336},
73 {8, 254702874}, {0, 455592851}};
79 index.
pprev = active_chain_tip;
86void MinerTestingSetup::TestPackageSelection(
const CChainParams& chainparams,
const CScript& scriptPubKey,
const std::vector<CTransactionRef>& txFirst)
96 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
97 tx.
vin[0].prevout.n = 0;
99 tx.
vout[0].nValue = 5000000000LL - 1000;
105 tx.
vin[0].prevout.hash = txFirst[1]->GetHash();
106 tx.
vout[0].nValue = 5000000000LL - 10000;
111 tx.
vin[0].prevout.hash = hashParentTx;
112 tx.
vout[0].nValue = 5000000000LL - 1000 - 50000;
116 std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
117 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
118 BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
119 BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
120 BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
123 tx.
vin[0].prevout.hash = hashHighFeeTx;
124 tx.
vout[0].nValue = 5000000000LL - 1000 - 50000;
133 tx.
vin[0].prevout.hash = hashFreeTx;
134 tx.
vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
137 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
139 for (
size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
140 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
141 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx);
148 tx.
vout[0].nValue -= 2;
151 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
152 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
153 BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
154 BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
159 tx.
vin[0].prevout.hash = txFirst[2]->GetHash();
161 tx.
vout[0].nValue = 5000000000LL - 100000000;
162 tx.
vout[1].nValue = 100000000;
167 tx.
vin[0].prevout.hash = hashFreeTx2;
170 tx.
vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
173 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
176 for (
size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
177 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx2);
178 BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashLowFeeTx2);
183 tx.
vin[0].prevout.n = 1;
184 tx.
vout[0].nValue = 100000000 - 10000;
186 pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
187 BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
188 BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
197 CScript scriptPubKey =
CScript() <<
ParseHex(
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") <<
OP_CHECKSIG;
198 std::unique_ptr<CBlockTemplate> pblocktemplate;
209 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
213 static_assert(std::size(
BLOCKINFO) == 110,
"Should have 110 blocks to import");
215 std::vector<CTransactionRef> txFirst;
217 CBlock *pblock = &pblocktemplate->block;
225 txCoinbase.
vout.resize(1);
228 if (txFirst.size() == 0)
230 if (txFirst.size() < 4)
231 txFirst.push_back(pblock->
vtx[0]);
233 pblock->
nNonce = bi.nonce;
235 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
244 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
255 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
256 tx.
vin[0].prevout.n = 0;
258 tx.
vout[0].nValue = BLOCKSUBSIDY;
259 for (
unsigned int i = 0; i < 1001; ++i)
261 tx.
vout[0].nValue -= LOWFEE;
266 tx.
vin[0].prevout.hash = hash;
269 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error,
HasReason(
"bad-blk-sigops"));
272 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
273 tx.
vout[0].nValue = BLOCKSUBSIDY;
274 for (
unsigned int i = 0; i < 1001; ++i)
276 tx.
vout[0].nValue -= LOWFEE;
281 tx.
vin[0].prevout.hash = hash;
283 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
289 std::vector<unsigned char> vchData(520);
290 for (
unsigned int i = 0; i < 18; ++i)
293 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
294 tx.
vout[0].nValue = BLOCKSUBSIDY;
295 for (
unsigned int i = 0; i < 128; ++i)
297 tx.
vout[0].nValue -= LOWFEE;
301 tx.
vin[0].prevout.hash = hash;
303 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
309 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error,
HasReason(
"bad-txns-inputs-missingorspent"));
314 tx.
vin[0].prevout.hash = txFirst[1]->GetHash();
315 tx.
vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
318 tx.
vin[0].prevout.hash = hash;
321 tx.
vin[1].prevout.hash = txFirst[0]->GetHash();
322 tx.
vin[1].prevout.n = 0;
323 tx.
vout[0].nValue = tx.
vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE;
326 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
331 tx.
vin[0].prevout.SetNull();
333 tx.
vout[0].nValue = 0;
338 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error,
HasReason(
"bad-cb-multiple"));
342 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
344 tx.
vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
351 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error,
HasReason(
"bad-txns-inputs-missingorspent"));
367 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
379 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
382 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
383 tx.
vin[0].prevout.n = 0;
385 tx.
vout[0].nValue = BLOCKSUBSIDY-LOWFEE;
390 tx.
vin[0].prevout.hash = hash;
391 tx.
vin[0].scriptSig =
CScript() << std::vector<unsigned char>(script.
begin(), script.
end());
392 tx.
vout[0].nValue -= LOWFEE;
396 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error,
HasReason(
"block-validation-failed"));
412 std::vector<int> prevheights;
417 prevheights.resize(1);
418 tx.
vin[0].prevout.hash = txFirst[0]->GetHash();
419 tx.
vin[0].prevout.n = 0;
422 prevheights[0] = baseheight + 1;
424 tx.
vout[0].nValue = BLOCKSUBSIDY-HIGHFEE;
438 tx.
vin[0].prevout.hash = txFirst[1]->GetHash();
440 prevheights[0] = baseheight + 2;
458 tx.
vin[0].prevout.hash = txFirst[2]->GetHash();
460 prevheights[0] = baseheight + 3;
469 tx.
vin[0].prevout.hash = txFirst[3]->GetHash();
471 prevheights.resize(1);
472 prevheights[0] = baseheight + 4;
480 tx.
vin[0].prevout.hash = hash;
481 prevheights[0] =
m_node.
chainman->ActiveChain().Tip()->nHeight + 1;
483 tx.
vin[0].nSequence = 0;
486 tx.
vin[0].nSequence = 1;
493 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
506 BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
513 TestPackageSelection(chainparams, scriptPubKey, txFirst);
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
#define Assert(val)
Identity function.
Generate a new block, without valid proof-of-work.
static const std::string MAIN
Chain name strings.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
void BuildSkip()
Build the skiplist pointer for this entry.
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
static constexpr int nMedianTimeSpan
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
CCoinsView that brings transactions from a mempool into view.
Fee rate in satoshis per kilobyte: CAmount / kB.
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given size in bytes.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
static const uint32_t SEQUENCE_FINAL
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
static const int SEQUENCE_LOCKTIME_GRANULARITY
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
static constexpr unsigned int LOCKTIME_MEDIAN_TIME_PAST
Use GetMedianTimePast() instead of nTime for end point timestamp.
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
BOOST_AUTO_TEST_SUITE_END()
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
static CFeeRate blockMinFeeRate
static CBlockIndex CreateBlockIndex(int nHeight, CBlockIndex *active_chain_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
constexpr static struct @15 BLOCKINFO[]
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static const unsigned int DEFAULT_BLOCK_MIN_TX_FEE
Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by min...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
size_t GetSerializeSize(const T &t, int nVersion=0)
static constexpr CAmount CENT
static uint256 InsecureRand256()
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::vector< unsigned char > ParseHex(const char *psz)
A mutable version of CTransaction.
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< CTxOut > vout
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< CTxMemPool > mempool
TestMemPoolEntryHelper & Time(int64_t _time)
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & SpendsCoinbase(bool _flag)
TestMemPoolEntryHelper & Fee(CAmount _fee)
Testing setup that configures a complete environment.
void TestPackageSelection(const CChainParams &chainparams, const CScript &scriptPubKey, const std::vector< CTransactionRef > &txFirst) EXCLUSIVE_LOCKS_REQUIRED(bool TestSequenceLocks(const CTransaction &tx, int flags) EXCLUSIVE_LOCKS_REQUIRED(
BlockAssembler AssemblerForTest(const CChainParams ¶ms)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
@ REPLACED
Removed for replacement.
bool CheckSequenceLocks(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx, int flags, LockPoints *lp, bool useExistingLockPoints)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
bool CheckFinalTx(const CBlockIndex *active_chain_tip, const CTransaction &tx, int flags)
Transaction validation functions.
static const int PROTOCOL_VERSION
network protocol versioning
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.