1 // Copyright (c) 2011-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.
5 #include <chainparams.h>
7 #include <consensus/consensus.h>
8 #include <consensus/merkle.h>
9 #include <consensus/tx_verify.h>
10 #include <consensus/validation.h>
11 #include <validation.h>
13 #include <policy/policy.h>
15 #include <script/standard.h>
16 #include <txmempool.h>
19 #include <utilstrencodings.h>
21 #include <test/test_bitcoin.h>
25 #include <boost/test/unit_test.hpp>
27 BOOST_FIXTURE_TEST_SUITE(miner_tests
, TestingSetup
)
29 // BOOST_CHECK_EXCEPTION predicates to check the specific validation error
32 HasReason(const std::string
& reason
) : m_reason(reason
) {}
33 bool operator() (const std::runtime_error
& e
) const {
34 return std::string(e
.what()).find(m_reason
) != std::string::npos
;
37 const std::string m_reason
;
40 static CFeeRate blockMinFeeRate
= CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE
);
42 static BlockAssembler
AssemblerForTest(const CChainParams
& params
) {
43 BlockAssembler::Options options
;
45 options
.nBlockMaxWeight
= MAX_BLOCK_WEIGHT
;
46 options
.blockMinFeeRate
= blockMinFeeRate
;
47 return BlockAssembler(params
, options
);
52 unsigned char extranonce
;
55 {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
56 {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
57 {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
58 {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
59 {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
60 {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
61 {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
62 {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
63 {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
64 {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
65 {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
66 {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
67 {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
68 {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
69 {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
70 {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
71 {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
72 {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
73 {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
74 {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
75 {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
76 {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
77 {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
78 {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
79 {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
80 {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
81 {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
82 {2, 0xbbbeb305}, {2, 0xfe1c810a},
85 CBlockIndex
CreateBlockIndex(int nHeight
)
88 index
.nHeight
= nHeight
;
89 index
.pprev
= chainActive
.Tip();
93 bool TestSequenceLocks(const CTransaction
&tx
, int flags
)
96 return CheckSequenceLocks(tx
, flags
);
99 // Test suite for ancestor feerate transaction selection.
100 // Implemented as an additional function, rather than a separate test case,
101 // to allow reusing the blockchain created in CreateNewBlock_validity.
102 void TestPackageSelection(const CChainParams
& chainparams
, CScript scriptPubKey
, std::vector
<CTransactionRef
>& txFirst
)
104 // Test the ancestor feerate transaction selection.
105 TestMemPoolEntryHelper entry
;
107 // Test that a medium fee transaction will be selected after a higher fee
108 // rate package with a low fee rate parent.
109 CMutableTransaction tx
;
111 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
112 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
113 tx
.vin
[0].prevout
.n
= 0;
115 tx
.vout
[0].nValue
= 5000000000LL - 1000;
116 // This tx has a low fee: 1000 satoshis
117 uint256 hashParentTx
= tx
.GetHash(); // save this txid for later use
118 mempool
.addUnchecked(hashParentTx
, entry
.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
120 // This tx has a medium fee: 10000 satoshis
121 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
122 tx
.vout
[0].nValue
= 5000000000LL - 10000;
123 uint256 hashMediumFeeTx
= tx
.GetHash();
124 mempool
.addUnchecked(hashMediumFeeTx
, entry
.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
126 // This tx has a high fee, but depends on the first transaction
127 tx
.vin
[0].prevout
.hash
= hashParentTx
;
128 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 50k satoshi fee
129 uint256 hashHighFeeTx
= tx
.GetHash();
130 mempool
.addUnchecked(hashHighFeeTx
, entry
.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
132 std::unique_ptr
<CBlockTemplate
> pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
133 BOOST_CHECK(pblocktemplate
->block
.vtx
[1]->GetHash() == hashParentTx
);
134 BOOST_CHECK(pblocktemplate
->block
.vtx
[2]->GetHash() == hashHighFeeTx
);
135 BOOST_CHECK(pblocktemplate
->block
.vtx
[3]->GetHash() == hashMediumFeeTx
);
137 // Test that a package below the block min tx fee doesn't get included
138 tx
.vin
[0].prevout
.hash
= hashHighFeeTx
;
139 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 0 fee
140 uint256 hashFreeTx
= tx
.GetHash();
141 mempool
.addUnchecked(hashFreeTx
, entry
.Fee(0).FromTx(tx
));
142 size_t freeTxSize
= ::GetSerializeSize(tx
, SER_NETWORK
, PROTOCOL_VERSION
);
144 // Calculate a fee on child transaction that will put the package just
145 // below the block min tx fee (assuming 1 child tx of the same size).
146 CAmount feeToUse
= blockMinFeeRate
.GetFee(2*freeTxSize
) - 1;
148 tx
.vin
[0].prevout
.hash
= hashFreeTx
;
149 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000 - feeToUse
;
150 uint256 hashLowFeeTx
= tx
.GetHash();
151 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
).FromTx(tx
));
152 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
153 // Verify that the free tx and the low fee tx didn't get selected
154 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
155 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx
);
156 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx
);
159 // Test that packages above the min relay fee do get included, even if one
160 // of the transactions is below the min relay fee
161 // Remove the low fee transaction and replace with a higher fee transaction
162 mempool
.removeRecursive(tx
);
163 tx
.vout
[0].nValue
-= 2; // Now we should be just over the min relay fee
164 hashLowFeeTx
= tx
.GetHash();
165 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
+2).FromTx(tx
));
166 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
167 BOOST_CHECK(pblocktemplate
->block
.vtx
[4]->GetHash() == hashFreeTx
);
168 BOOST_CHECK(pblocktemplate
->block
.vtx
[5]->GetHash() == hashLowFeeTx
);
170 // Test that transaction selection properly updates ancestor fee
171 // calculations as ancestor transactions get included in a block.
172 // Add a 0-fee transaction that has 2 outputs.
173 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
175 tx
.vout
[0].nValue
= 5000000000LL - 100000000;
176 tx
.vout
[1].nValue
= 100000000; // 1BTC output
177 uint256 hashFreeTx2
= tx
.GetHash();
178 mempool
.addUnchecked(hashFreeTx2
, entry
.Fee(0).SpendsCoinbase(true).FromTx(tx
));
180 // This tx can't be mined by itself
181 tx
.vin
[0].prevout
.hash
= hashFreeTx2
;
183 feeToUse
= blockMinFeeRate
.GetFee(freeTxSize
);
184 tx
.vout
[0].nValue
= 5000000000LL - 100000000 - feeToUse
;
185 uint256 hashLowFeeTx2
= tx
.GetHash();
186 mempool
.addUnchecked(hashLowFeeTx2
, entry
.Fee(feeToUse
).SpendsCoinbase(false).FromTx(tx
));
187 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
189 // Verify that this tx isn't selected.
190 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
191 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx2
);
192 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx2
);
195 // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
197 tx
.vin
[0].prevout
.n
= 1;
198 tx
.vout
[0].nValue
= 100000000 - 10000; // 10k satoshi fee
199 mempool
.addUnchecked(tx
.GetHash(), entry
.Fee(10000).FromTx(tx
));
200 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
201 BOOST_CHECK(pblocktemplate
->block
.vtx
[8]->GetHash() == hashLowFeeTx2
);
204 // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
205 BOOST_AUTO_TEST_CASE(CreateNewBlock_validity
)
207 // Note that by default, these tests run with size accounting enabled.
208 const auto chainParams
= CreateChainParams(CBaseChainParams::MAIN
);
209 const CChainParams
& chainparams
= *chainParams
;
210 CScript scriptPubKey
= CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG
;
211 std::unique_ptr
<CBlockTemplate
> pblocktemplate
;
212 CMutableTransaction tx
,tx2
;
215 TestMemPoolEntryHelper entry
;
219 fCheckpointsEnabled
= false;
221 // Simple block creation, nothing special yet:
222 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
224 // We can't make transactions until we have inputs
225 // Therefore, load 100 blocks :)
227 std::vector
<CTransactionRef
> txFirst
;
228 for (unsigned int i
= 0; i
< sizeof(blockinfo
)/sizeof(*blockinfo
); ++i
)
230 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
233 pblock
->nVersion
= 1;
234 pblock
->nTime
= chainActive
.Tip()->GetMedianTimePast()+1;
235 CMutableTransaction
txCoinbase(*pblock
->vtx
[0]);
236 txCoinbase
.nVersion
= 1;
237 txCoinbase
.vin
[0].scriptSig
= CScript();
238 txCoinbase
.vin
[0].scriptSig
.push_back(blockinfo
[i
].extranonce
);
239 txCoinbase
.vin
[0].scriptSig
.push_back(chainActive
.Height());
240 txCoinbase
.vout
.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
241 txCoinbase
.vout
[0].scriptPubKey
= CScript();
242 pblock
->vtx
[0] = MakeTransactionRef(std::move(txCoinbase
));
243 if (txFirst
.size() == 0)
244 baseheight
= chainActive
.Height();
245 if (txFirst
.size() < 4)
246 txFirst
.push_back(pblock
->vtx
[0]);
247 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
248 pblock
->nNonce
= blockinfo
[i
].nonce
;
250 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(*pblock
);
251 BOOST_CHECK(ProcessNewBlock(chainparams
, shared_pblock
, true, nullptr));
252 pblock
->hashPrevBlock
= pblock
->GetHash();
257 // Just to make sure we can still make simple blocks
258 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
260 const CAmount BLOCKSUBSIDY
= 50*COIN
;
261 const CAmount LOWFEE
= CENT
;
262 const CAmount HIGHFEE
= COIN
;
263 const CAmount HIGHERFEE
= 4*COIN
;
265 // block sigops > limit: 1000 CHECKMULTISIG + 1
267 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
268 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_0
<< OP_0
<< OP_NOP
<< OP_CHECKMULTISIG
<< OP_1
;
269 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
270 tx
.vin
[0].prevout
.n
= 0;
272 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
273 for (unsigned int i
= 0; i
< 1001; ++i
)
275 tx
.vout
[0].nValue
-= LOWFEE
;
277 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
278 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
279 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
280 tx
.vin
[0].prevout
.hash
= hash
;
283 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-blk-sigops"));
286 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
287 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
288 for (unsigned int i
= 0; i
< 1001; ++i
)
290 tx
.vout
[0].nValue
-= LOWFEE
;
292 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
293 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
294 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).SigOpsCost(80).FromTx(tx
));
295 tx
.vin
[0].prevout
.hash
= hash
;
297 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
300 // block size > limit
301 tx
.vin
[0].scriptSig
= CScript();
302 // 18 * (520char + DROP) + OP_1 = 9433 bytes
303 std::vector
<unsigned char> vchData(520);
304 for (unsigned int i
= 0; i
< 18; ++i
)
305 tx
.vin
[0].scriptSig
<< vchData
<< OP_DROP
;
306 tx
.vin
[0].scriptSig
<< OP_1
;
307 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
308 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
309 for (unsigned int i
= 0; i
< 128; ++i
)
311 tx
.vout
[0].nValue
-= LOWFEE
;
313 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
314 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
315 tx
.vin
[0].prevout
.hash
= hash
;
317 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
320 // orphan in mempool, template creation fails
322 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).FromTx(tx
));
323 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-txns-inputs-missingorspent"));
326 // child with higher feerate than parent
327 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
328 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
329 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
331 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
332 tx
.vin
[0].prevout
.hash
= hash
;
334 tx
.vin
[1].scriptSig
= CScript() << OP_1
;
335 tx
.vin
[1].prevout
.hash
= txFirst
[0]->GetHash();
336 tx
.vin
[1].prevout
.n
= 0;
337 tx
.vout
[0].nValue
= tx
.vout
[0].nValue
+BLOCKSUBSIDY
-HIGHERFEE
; //First txn output + fresh coinbase - new txn fee
339 mempool
.addUnchecked(hash
, entry
.Fee(HIGHERFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
340 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
343 // coinbase in mempool, template creation fails
345 tx
.vin
[0].prevout
.SetNull();
346 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_1
;
347 tx
.vout
[0].nValue
= 0;
349 // give it a fee so it'll get mined
350 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
351 // Should throw bad-cb-multiple
352 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-cb-multiple"));
355 // double spend txn pair in mempool, template creation fails
356 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
357 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
358 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
359 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
361 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
362 tx
.vout
[0].scriptPubKey
= CScript() << OP_2
;
364 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
365 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-txns-inputs-missingorspent"));
369 int nHeight
= chainActive
.Height();
370 // Create an actual 209999-long block chain (without valid blocks).
371 while (chainActive
.Tip()->nHeight
< 209999) {
372 CBlockIndex
* prev
= chainActive
.Tip();
373 CBlockIndex
* next
= new CBlockIndex();
374 next
->phashBlock
= new uint256(InsecureRand256());
375 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
377 next
->nHeight
= prev
->nHeight
+ 1;
379 chainActive
.SetTip(next
);
381 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
382 // Extend to a 210000-long block chain.
383 while (chainActive
.Tip()->nHeight
< 210000) {
384 CBlockIndex
* prev
= chainActive
.Tip();
385 CBlockIndex
* next
= new CBlockIndex();
386 next
->phashBlock
= new uint256(InsecureRand256());
387 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
389 next
->nHeight
= prev
->nHeight
+ 1;
391 chainActive
.SetTip(next
);
393 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
395 // invalid p2sh txn in mempool, template creation fails
396 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
397 tx
.vin
[0].prevout
.n
= 0;
398 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
399 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-LOWFEE
;
400 script
= CScript() << OP_0
;
401 tx
.vout
[0].scriptPubKey
= GetScriptForDestination(CScriptID(script
));
403 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
404 tx
.vin
[0].prevout
.hash
= hash
;
405 tx
.vin
[0].scriptSig
= CScript() << std::vector
<unsigned char>(script
.begin(), script
.end());
406 tx
.vout
[0].nValue
-= LOWFEE
;
408 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
409 // Should throw block-validation-failed
410 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("block-validation-failed"));
413 // Delete the dummy blocks again.
414 while (chainActive
.Tip()->nHeight
> nHeight
) {
415 CBlockIndex
* del
= chainActive
.Tip();
416 chainActive
.SetTip(del
->pprev
);
417 pcoinsTip
->SetBestBlock(del
->pprev
->GetBlockHash());
418 delete del
->phashBlock
;
422 // non-final txs in mempool
423 SetMockTime(chainActive
.Tip()->GetMedianTimePast()+1);
424 int flags
= LOCKTIME_VERIFY_SEQUENCE
|LOCKTIME_MEDIAN_TIME_PAST
;
426 std::vector
<int> prevheights
;
428 // relative height locked
431 prevheights
.resize(1);
432 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash(); // only 1 transaction
433 tx
.vin
[0].prevout
.n
= 0;
434 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
435 tx
.vin
[0].nSequence
= chainActive
.Tip()->nHeight
+ 1; // txFirst[0] is the 2nd block
436 prevheights
[0] = baseheight
+ 1;
438 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
439 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
442 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
443 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
444 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
445 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 2))); // Sequence locks pass on 2nd block
447 // relative time locked
448 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
449 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
| (((chainActive
.Tip()->GetMedianTimePast()+1-chainActive
[1]->GetMedianTimePast()) >> CTxIn::SEQUENCE_LOCKTIME_GRANULARITY
) + 1); // txFirst[1] is the 3rd block
450 prevheights
[0] = baseheight
+ 2;
452 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
453 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
454 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
456 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
457 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
458 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 1))); // Sequence locks pass 512 seconds later
459 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
460 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
-= 512; //undo tricked MTP
462 // absolute height locked
463 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
464 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_FINAL
- 1;
465 prevheights
[0] = baseheight
+ 3;
466 tx
.nLockTime
= chainActive
.Tip()->nHeight
+ 1;
468 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
469 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
470 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
471 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
473 // absolute time locked
474 tx
.vin
[0].prevout
.hash
= txFirst
[3]->GetHash();
475 tx
.nLockTime
= chainActive
.Tip()->GetMedianTimePast();
476 prevheights
.resize(1);
477 prevheights
[0] = baseheight
+ 4;
479 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
480 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
481 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
482 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
484 // mempool-dependent transactions (not added)
485 tx
.vin
[0].prevout
.hash
= hash
;
486 prevheights
[0] = chainActive
.Tip()->nHeight
+ 1;
488 tx
.vin
[0].nSequence
= 0;
489 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
490 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
491 tx
.vin
[0].nSequence
= 1;
492 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
493 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
;
494 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
495 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
| 1;
496 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
498 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
500 // None of the of the absolute height/time locked tx should have made
501 // it into the template because we still check IsFinalTx in CreateNewBlock,
502 // but relative locked txs will if inconsistently added to mempool.
503 // For now these will still generate a valid template until BIP68 soft fork
504 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 3);
505 // However if we advance height by 1 and time by 512, all of them should be mined
506 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
507 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
508 chainActive
.Tip()->nHeight
++;
509 SetMockTime(chainActive
.Tip()->GetMedianTimePast() + 1);
511 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
512 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 5);
514 chainActive
.Tip()->nHeight
--;
518 TestPackageSelection(chainparams
, scriptPubKey
, txFirst
);
520 fCheckpointsEnabled
= true;
523 BOOST_AUTO_TEST_SUITE_END()