1 // Copyright (c) 2011-2016 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
;
220 fCheckpointsEnabled
= false;
222 // Simple block creation, nothing special yet:
223 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
225 // We can't make transactions until we have inputs
226 // Therefore, load 100 blocks :)
228 std::vector
<CTransactionRef
> txFirst
;
229 for (unsigned int i
= 0; i
< sizeof(blockinfo
)/sizeof(*blockinfo
); ++i
)
231 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
232 pblock
->nVersion
= 1;
233 pblock
->nTime
= chainActive
.Tip()->GetMedianTimePast()+1;
234 CMutableTransaction
txCoinbase(*pblock
->vtx
[0]);
235 txCoinbase
.nVersion
= 1;
236 txCoinbase
.vin
[0].scriptSig
= CScript();
237 txCoinbase
.vin
[0].scriptSig
.push_back(blockinfo
[i
].extranonce
);
238 txCoinbase
.vin
[0].scriptSig
.push_back(chainActive
.Height());
239 txCoinbase
.vout
.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
240 txCoinbase
.vout
[0].scriptPubKey
= CScript();
241 pblock
->vtx
[0] = MakeTransactionRef(std::move(txCoinbase
));
242 if (txFirst
.size() == 0)
243 baseheight
= chainActive
.Height();
244 if (txFirst
.size() < 4)
245 txFirst
.push_back(pblock
->vtx
[0]);
246 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
247 pblock
->nNonce
= blockinfo
[i
].nonce
;
248 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(*pblock
);
249 BOOST_CHECK(ProcessNewBlock(chainparams
, shared_pblock
, true, nullptr));
250 pblock
->hashPrevBlock
= pblock
->GetHash();
253 // Just to make sure we can still make simple blocks
254 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
256 const CAmount BLOCKSUBSIDY
= 50*COIN
;
257 const CAmount LOWFEE
= CENT
;
258 const CAmount HIGHFEE
= COIN
;
259 const CAmount HIGHERFEE
= 4*COIN
;
261 // block sigops > limit: 1000 CHECKMULTISIG + 1
263 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
264 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_0
<< OP_0
<< OP_NOP
<< OP_CHECKMULTISIG
<< OP_1
;
265 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
266 tx
.vin
[0].prevout
.n
= 0;
268 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
269 for (unsigned int i
= 0; i
< 1001; ++i
)
271 tx
.vout
[0].nValue
-= LOWFEE
;
273 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
274 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
275 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
276 tx
.vin
[0].prevout
.hash
= hash
;
279 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-blk-sigops"));
282 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
283 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
284 for (unsigned int i
= 0; i
< 1001; ++i
)
286 tx
.vout
[0].nValue
-= LOWFEE
;
288 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
289 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
290 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).SigOpsCost(80).FromTx(tx
));
291 tx
.vin
[0].prevout
.hash
= hash
;
293 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
296 // block size > limit
297 tx
.vin
[0].scriptSig
= CScript();
298 // 18 * (520char + DROP) + OP_1 = 9433 bytes
299 std::vector
<unsigned char> vchData(520);
300 for (unsigned int i
= 0; i
< 18; ++i
)
301 tx
.vin
[0].scriptSig
<< vchData
<< OP_DROP
;
302 tx
.vin
[0].scriptSig
<< OP_1
;
303 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
304 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
305 for (unsigned int i
= 0; i
< 128; ++i
)
307 tx
.vout
[0].nValue
-= LOWFEE
;
309 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
310 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
311 tx
.vin
[0].prevout
.hash
= hash
;
313 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
316 // orphan in mempool, template creation fails
318 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).FromTx(tx
));
319 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-txns-inputs-missingorspent"));
322 // child with higher feerate than parent
323 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
324 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
325 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
327 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
328 tx
.vin
[0].prevout
.hash
= hash
;
330 tx
.vin
[1].scriptSig
= CScript() << OP_1
;
331 tx
.vin
[1].prevout
.hash
= txFirst
[0]->GetHash();
332 tx
.vin
[1].prevout
.n
= 0;
333 tx
.vout
[0].nValue
= tx
.vout
[0].nValue
+BLOCKSUBSIDY
-HIGHERFEE
; //First txn output + fresh coinbase - new txn fee
335 mempool
.addUnchecked(hash
, entry
.Fee(HIGHERFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
336 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
339 // coinbase in mempool, template creation fails
341 tx
.vin
[0].prevout
.SetNull();
342 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_1
;
343 tx
.vout
[0].nValue
= 0;
345 // give it a fee so it'll get mined
346 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
347 // Should throw bad-cb-multiple
348 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-cb-multiple"));
351 // double spend txn pair in mempool, template creation fails
352 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
353 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
354 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
355 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
357 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
358 tx
.vout
[0].scriptPubKey
= CScript() << OP_2
;
360 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
361 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("bad-txns-inputs-missingorspent"));
365 int nHeight
= chainActive
.Height();
366 // Create an actual 209999-long block chain (without valid blocks).
367 while (chainActive
.Tip()->nHeight
< 209999) {
368 CBlockIndex
* prev
= chainActive
.Tip();
369 CBlockIndex
* next
= new CBlockIndex();
370 next
->phashBlock
= new uint256(InsecureRand256());
371 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
373 next
->nHeight
= prev
->nHeight
+ 1;
375 chainActive
.SetTip(next
);
377 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
378 // Extend to a 210000-long block chain.
379 while (chainActive
.Tip()->nHeight
< 210000) {
380 CBlockIndex
* prev
= chainActive
.Tip();
381 CBlockIndex
* next
= new CBlockIndex();
382 next
->phashBlock
= new uint256(InsecureRand256());
383 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
385 next
->nHeight
= prev
->nHeight
+ 1;
387 chainActive
.SetTip(next
);
389 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
391 // invalid p2sh txn in mempool, template creation fails
392 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
393 tx
.vin
[0].prevout
.n
= 0;
394 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
395 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-LOWFEE
;
396 script
= CScript() << OP_0
;
397 tx
.vout
[0].scriptPubKey
= GetScriptForDestination(CScriptID(script
));
399 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
400 tx
.vin
[0].prevout
.hash
= hash
;
401 tx
.vin
[0].scriptSig
= CScript() << std::vector
<unsigned char>(script
.begin(), script
.end());
402 tx
.vout
[0].nValue
-= LOWFEE
;
404 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
405 // Should throw block-validation-failed
406 BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
, HasReason("block-validation-failed"));
409 // Delete the dummy blocks again.
410 while (chainActive
.Tip()->nHeight
> nHeight
) {
411 CBlockIndex
* del
= chainActive
.Tip();
412 chainActive
.SetTip(del
->pprev
);
413 pcoinsTip
->SetBestBlock(del
->pprev
->GetBlockHash());
414 delete del
->phashBlock
;
418 // non-final txs in mempool
419 SetMockTime(chainActive
.Tip()->GetMedianTimePast()+1);
420 int flags
= LOCKTIME_VERIFY_SEQUENCE
|LOCKTIME_MEDIAN_TIME_PAST
;
422 std::vector
<int> prevheights
;
424 // relative height locked
427 prevheights
.resize(1);
428 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash(); // only 1 transaction
429 tx
.vin
[0].prevout
.n
= 0;
430 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
431 tx
.vin
[0].nSequence
= chainActive
.Tip()->nHeight
+ 1; // txFirst[0] is the 2nd block
432 prevheights
[0] = baseheight
+ 1;
434 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
435 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
438 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
439 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
440 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
441 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 2))); // Sequence locks pass on 2nd block
443 // relative time locked
444 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
445 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
446 prevheights
[0] = baseheight
+ 2;
448 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
449 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
450 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
452 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
453 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
454 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 1))); // Sequence locks pass 512 seconds later
455 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
456 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
-= 512; //undo tricked MTP
458 // absolute height locked
459 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
460 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_FINAL
- 1;
461 prevheights
[0] = baseheight
+ 3;
462 tx
.nLockTime
= chainActive
.Tip()->nHeight
+ 1;
464 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
465 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
466 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
467 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
469 // absolute time locked
470 tx
.vin
[0].prevout
.hash
= txFirst
[3]->GetHash();
471 tx
.nLockTime
= chainActive
.Tip()->GetMedianTimePast();
472 prevheights
.resize(1);
473 prevheights
[0] = baseheight
+ 4;
475 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
476 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
477 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
478 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
480 // mempool-dependent transactions (not added)
481 tx
.vin
[0].prevout
.hash
= hash
;
482 prevheights
[0] = chainActive
.Tip()->nHeight
+ 1;
484 tx
.vin
[0].nSequence
= 0;
485 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
486 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
487 tx
.vin
[0].nSequence
= 1;
488 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
489 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
;
490 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
491 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
| 1;
492 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
494 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
496 // None of the of the absolute height/time locked tx should have made
497 // it into the template because we still check IsFinalTx in CreateNewBlock,
498 // but relative locked txs will if inconsistently added to mempool.
499 // For now these will still generate a valid template until BIP68 soft fork
500 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 3);
501 // However if we advance height by 1 and time by 512, all of them should be mined
502 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
503 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
504 chainActive
.Tip()->nHeight
++;
505 SetMockTime(chainActive
.Tip()->GetMedianTimePast() + 1);
507 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
508 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 5);
510 chainActive
.Tip()->nHeight
--;
514 TestPackageSelection(chainparams
, scriptPubKey
, txFirst
);
516 fCheckpointsEnabled
= true;
519 BOOST_AUTO_TEST_SUITE_END()