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/validation.h"
10 #include "validation.h"
12 #include "policy/policy.h"
14 #include "script/standard.h"
15 #include "txmempool.h"
18 #include "utilstrencodings.h"
20 #include "test/test_bitcoin.h"
24 #include <boost/test/unit_test.hpp>
26 BOOST_FIXTURE_TEST_SUITE(miner_tests
, TestingSetup
)
28 static CFeeRate blockMinFeeRate
= CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE
);
30 static BlockAssembler
AssemblerForTest(const CChainParams
& params
) {
31 BlockAssembler::Options options
;
33 options
.nBlockMaxWeight
= MAX_BLOCK_WEIGHT
;
34 options
.nBlockMaxSize
= MAX_BLOCK_SERIALIZED_SIZE
;
35 options
.blockMinFeeRate
= blockMinFeeRate
;
36 return BlockAssembler(params
, options
);
41 unsigned char extranonce
;
44 {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
45 {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
46 {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
47 {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
48 {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
49 {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
50 {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
51 {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
52 {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
53 {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
54 {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
55 {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
56 {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
57 {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
58 {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
59 {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
60 {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
61 {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
62 {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
63 {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
64 {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
65 {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
66 {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
67 {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
68 {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
69 {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
70 {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
71 {2, 0xbbbeb305}, {2, 0xfe1c810a},
74 CBlockIndex
CreateBlockIndex(int nHeight
)
77 index
.nHeight
= nHeight
;
78 index
.pprev
= chainActive
.Tip();
82 bool TestSequenceLocks(const CTransaction
&tx
, int flags
)
85 return CheckSequenceLocks(tx
, flags
);
88 // Test suite for ancestor feerate transaction selection.
89 // Implemented as an additional function, rather than a separate test case,
90 // to allow reusing the blockchain created in CreateNewBlock_validity.
91 void TestPackageSelection(const CChainParams
& chainparams
, CScript scriptPubKey
, std::vector
<CTransactionRef
>& txFirst
)
93 // Test the ancestor feerate transaction selection.
94 TestMemPoolEntryHelper entry
;
96 // Test that a medium fee transaction will be selected after a higher fee
97 // rate package with a low fee rate parent.
98 CMutableTransaction tx
;
100 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
101 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
102 tx
.vin
[0].prevout
.n
= 0;
104 tx
.vout
[0].nValue
= 5000000000LL - 1000;
105 // This tx has a low fee: 1000 satoshis
106 uint256 hashParentTx
= tx
.GetHash(); // save this txid for later use
107 mempool
.addUnchecked(hashParentTx
, entry
.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
109 // This tx has a medium fee: 10000 satoshis
110 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
111 tx
.vout
[0].nValue
= 5000000000LL - 10000;
112 uint256 hashMediumFeeTx
= tx
.GetHash();
113 mempool
.addUnchecked(hashMediumFeeTx
, entry
.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
115 // This tx has a high fee, but depends on the first transaction
116 tx
.vin
[0].prevout
.hash
= hashParentTx
;
117 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 50k satoshi fee
118 uint256 hashHighFeeTx
= tx
.GetHash();
119 mempool
.addUnchecked(hashHighFeeTx
, entry
.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
121 std::unique_ptr
<CBlockTemplate
> pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
122 BOOST_CHECK(pblocktemplate
->block
.vtx
[1]->GetHash() == hashParentTx
);
123 BOOST_CHECK(pblocktemplate
->block
.vtx
[2]->GetHash() == hashHighFeeTx
);
124 BOOST_CHECK(pblocktemplate
->block
.vtx
[3]->GetHash() == hashMediumFeeTx
);
126 // Test that a package below the block min tx fee doesn't get included
127 tx
.vin
[0].prevout
.hash
= hashHighFeeTx
;
128 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 0 fee
129 uint256 hashFreeTx
= tx
.GetHash();
130 mempool
.addUnchecked(hashFreeTx
, entry
.Fee(0).FromTx(tx
));
131 size_t freeTxSize
= ::GetSerializeSize(tx
, SER_NETWORK
, PROTOCOL_VERSION
);
133 // Calculate a fee on child transaction that will put the package just
134 // below the block min tx fee (assuming 1 child tx of the same size).
135 CAmount feeToUse
= blockMinFeeRate
.GetFee(2*freeTxSize
) - 1;
137 tx
.vin
[0].prevout
.hash
= hashFreeTx
;
138 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000 - feeToUse
;
139 uint256 hashLowFeeTx
= tx
.GetHash();
140 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
).FromTx(tx
));
141 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
142 // Verify that the free tx and the low fee tx didn't get selected
143 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
144 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx
);
145 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx
);
148 // Test that packages above the min relay fee do get included, even if one
149 // of the transactions is below the min relay fee
150 // Remove the low fee transaction and replace with a higher fee transaction
151 mempool
.removeRecursive(tx
);
152 tx
.vout
[0].nValue
-= 2; // Now we should be just over the min relay fee
153 hashLowFeeTx
= tx
.GetHash();
154 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
+2).FromTx(tx
));
155 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
156 BOOST_CHECK(pblocktemplate
->block
.vtx
[4]->GetHash() == hashFreeTx
);
157 BOOST_CHECK(pblocktemplate
->block
.vtx
[5]->GetHash() == hashLowFeeTx
);
159 // Test that transaction selection properly updates ancestor fee
160 // calculations as ancestor transactions get included in a block.
161 // Add a 0-fee transaction that has 2 outputs.
162 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
164 tx
.vout
[0].nValue
= 5000000000LL - 100000000;
165 tx
.vout
[1].nValue
= 100000000; // 1BTC output
166 uint256 hashFreeTx2
= tx
.GetHash();
167 mempool
.addUnchecked(hashFreeTx2
, entry
.Fee(0).SpendsCoinbase(true).FromTx(tx
));
169 // This tx can't be mined by itself
170 tx
.vin
[0].prevout
.hash
= hashFreeTx2
;
172 feeToUse
= blockMinFeeRate
.GetFee(freeTxSize
);
173 tx
.vout
[0].nValue
= 5000000000LL - 100000000 - feeToUse
;
174 uint256 hashLowFeeTx2
= tx
.GetHash();
175 mempool
.addUnchecked(hashLowFeeTx2
, entry
.Fee(feeToUse
).SpendsCoinbase(false).FromTx(tx
));
176 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
178 // Verify that this tx isn't selected.
179 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
180 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx2
);
181 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx2
);
184 // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
186 tx
.vin
[0].prevout
.n
= 1;
187 tx
.vout
[0].nValue
= 100000000 - 10000; // 10k satoshi fee
188 mempool
.addUnchecked(tx
.GetHash(), entry
.Fee(10000).FromTx(tx
));
189 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
190 BOOST_CHECK(pblocktemplate
->block
.vtx
[8]->GetHash() == hashLowFeeTx2
);
193 // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
194 BOOST_AUTO_TEST_CASE(CreateNewBlock_validity
)
196 // Note that by default, these tests run with size accounting enabled.
197 const CChainParams
& chainparams
= Params(CBaseChainParams::MAIN
);
198 CScript scriptPubKey
= CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG
;
199 std::unique_ptr
<CBlockTemplate
> pblocktemplate
;
200 CMutableTransaction tx
,tx2
;
203 TestMemPoolEntryHelper entry
;
208 fCheckpointsEnabled
= false;
210 // Simple block creation, nothing special yet:
211 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
213 // We can't make transactions until we have inputs
214 // Therefore, load 100 blocks :)
216 std::vector
<CTransactionRef
> txFirst
;
217 for (unsigned int i
= 0; i
< sizeof(blockinfo
)/sizeof(*blockinfo
); ++i
)
219 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
220 pblock
->nVersion
= 1;
221 pblock
->nTime
= chainActive
.Tip()->GetMedianTimePast()+1;
222 CMutableTransaction
txCoinbase(*pblock
->vtx
[0]);
223 txCoinbase
.nVersion
= 1;
224 txCoinbase
.vin
[0].scriptSig
= CScript();
225 txCoinbase
.vin
[0].scriptSig
.push_back(blockinfo
[i
].extranonce
);
226 txCoinbase
.vin
[0].scriptSig
.push_back(chainActive
.Height());
227 txCoinbase
.vout
.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
228 txCoinbase
.vout
[0].scriptPubKey
= CScript();
229 pblock
->vtx
[0] = MakeTransactionRef(std::move(txCoinbase
));
230 if (txFirst
.size() == 0)
231 baseheight
= chainActive
.Height();
232 if (txFirst
.size() < 4)
233 txFirst
.push_back(pblock
->vtx
[0]);
234 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
235 pblock
->nNonce
= blockinfo
[i
].nonce
;
236 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(*pblock
);
237 BOOST_CHECK(ProcessNewBlock(chainparams
, shared_pblock
, true, NULL
));
238 pblock
->hashPrevBlock
= pblock
->GetHash();
241 // Just to make sure we can still make simple blocks
242 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
244 const CAmount BLOCKSUBSIDY
= 50*COIN
;
245 const CAmount LOWFEE
= CENT
;
246 const CAmount HIGHFEE
= COIN
;
247 const CAmount HIGHERFEE
= 4*COIN
;
249 // block sigops > limit: 1000 CHECKMULTISIG + 1
251 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
252 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_0
<< OP_0
<< OP_NOP
<< OP_CHECKMULTISIG
<< OP_1
;
253 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
254 tx
.vin
[0].prevout
.n
= 0;
256 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
257 for (unsigned int i
= 0; i
< 1001; ++i
)
259 tx
.vout
[0].nValue
-= LOWFEE
;
261 bool spendsCoinbase
= (i
== 0) ? true : false; // only first tx spends coinbase
262 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
263 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
264 tx
.vin
[0].prevout
.hash
= hash
;
266 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
269 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
270 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
271 for (unsigned int i
= 0; i
< 1001; ++i
)
273 tx
.vout
[0].nValue
-= LOWFEE
;
275 bool spendsCoinbase
= (i
== 0) ? true : false; // only first tx spends coinbase
276 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
277 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).SigOpsCost(80).FromTx(tx
));
278 tx
.vin
[0].prevout
.hash
= hash
;
280 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
283 // block size > limit
284 tx
.vin
[0].scriptSig
= CScript();
285 // 18 * (520char + DROP) + OP_1 = 9433 bytes
286 std::vector
<unsigned char> vchData(520);
287 for (unsigned int i
= 0; i
< 18; ++i
)
288 tx
.vin
[0].scriptSig
<< vchData
<< OP_DROP
;
289 tx
.vin
[0].scriptSig
<< OP_1
;
290 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
291 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
292 for (unsigned int i
= 0; i
< 128; ++i
)
294 tx
.vout
[0].nValue
-= LOWFEE
;
296 bool spendsCoinbase
= (i
== 0) ? true : false; // only first tx spends coinbase
297 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
298 tx
.vin
[0].prevout
.hash
= hash
;
300 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
303 // orphan in mempool, template creation fails
305 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).FromTx(tx
));
306 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
309 // child with higher feerate than parent
310 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
311 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
312 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
314 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
315 tx
.vin
[0].prevout
.hash
= hash
;
317 tx
.vin
[1].scriptSig
= CScript() << OP_1
;
318 tx
.vin
[1].prevout
.hash
= txFirst
[0]->GetHash();
319 tx
.vin
[1].prevout
.n
= 0;
320 tx
.vout
[0].nValue
= tx
.vout
[0].nValue
+BLOCKSUBSIDY
-HIGHERFEE
; //First txn output + fresh coinbase - new txn fee
322 mempool
.addUnchecked(hash
, entry
.Fee(HIGHERFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
323 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
326 // coinbase in mempool, template creation fails
328 tx
.vin
[0].prevout
.SetNull();
329 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_1
;
330 tx
.vout
[0].nValue
= 0;
332 // give it a fee so it'll get mined
333 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
334 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
337 // invalid (pre-p2sh) txn in mempool, template creation fails
338 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
339 tx
.vin
[0].prevout
.n
= 0;
340 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
341 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-LOWFEE
;
342 script
= CScript() << OP_0
;
343 tx
.vout
[0].scriptPubKey
= GetScriptForDestination(CScriptID(script
));
345 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
346 tx
.vin
[0].prevout
.hash
= hash
;
347 tx
.vin
[0].scriptSig
= CScript() << std::vector
<unsigned char>(script
.begin(), script
.end());
348 tx
.vout
[0].nValue
-= LOWFEE
;
350 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
351 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
354 // double spend txn pair in mempool, template creation fails
355 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
356 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
357 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
358 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
360 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
361 tx
.vout
[0].scriptPubKey
= CScript() << OP_2
;
363 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
364 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
368 int nHeight
= chainActive
.Height();
369 // Create an actual 209999-long block chain (without valid blocks).
370 while (chainActive
.Tip()->nHeight
< 209999) {
371 CBlockIndex
* prev
= chainActive
.Tip();
372 CBlockIndex
* next
= new CBlockIndex();
373 next
->phashBlock
= new uint256(GetRandHash());
374 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
376 next
->nHeight
= prev
->nHeight
+ 1;
378 chainActive
.SetTip(next
);
380 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
381 // Extend to a 210000-long block chain.
382 while (chainActive
.Tip()->nHeight
< 210000) {
383 CBlockIndex
* prev
= chainActive
.Tip();
384 CBlockIndex
* next
= new CBlockIndex();
385 next
->phashBlock
= new uint256(GetRandHash());
386 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
388 next
->nHeight
= prev
->nHeight
+ 1;
390 chainActive
.SetTip(next
);
392 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
393 // Delete the dummy blocks again.
394 while (chainActive
.Tip()->nHeight
> nHeight
) {
395 CBlockIndex
* del
= chainActive
.Tip();
396 chainActive
.SetTip(del
->pprev
);
397 pcoinsTip
->SetBestBlock(del
->pprev
->GetBlockHash());
398 delete del
->phashBlock
;
402 // non-final txs in mempool
403 SetMockTime(chainActive
.Tip()->GetMedianTimePast()+1);
404 int flags
= LOCKTIME_VERIFY_SEQUENCE
|LOCKTIME_MEDIAN_TIME_PAST
;
406 std::vector
<int> prevheights
;
408 // relative height locked
411 prevheights
.resize(1);
412 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash(); // only 1 transaction
413 tx
.vin
[0].prevout
.n
= 0;
414 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
415 tx
.vin
[0].nSequence
= chainActive
.Tip()->nHeight
+ 1; // txFirst[0] is the 2nd block
416 prevheights
[0] = baseheight
+ 1;
418 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
419 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
422 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
423 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
424 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
425 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 2))); // Sequence locks pass on 2nd block
427 // relative time locked
428 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
429 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
430 prevheights
[0] = baseheight
+ 2;
432 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
433 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
434 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
436 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
437 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
438 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 1))); // Sequence locks pass 512 seconds later
439 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
440 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
-= 512; //undo tricked MTP
442 // absolute height locked
443 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
444 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_FINAL
- 1;
445 prevheights
[0] = baseheight
+ 3;
446 tx
.nLockTime
= chainActive
.Tip()->nHeight
+ 1;
448 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
449 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
450 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
451 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
453 // absolute time locked
454 tx
.vin
[0].prevout
.hash
= txFirst
[3]->GetHash();
455 tx
.nLockTime
= chainActive
.Tip()->GetMedianTimePast();
456 prevheights
.resize(1);
457 prevheights
[0] = baseheight
+ 4;
459 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
460 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
461 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
462 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
464 // mempool-dependent transactions (not added)
465 tx
.vin
[0].prevout
.hash
= hash
;
466 prevheights
[0] = chainActive
.Tip()->nHeight
+ 1;
468 tx
.vin
[0].nSequence
= 0;
469 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
470 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
471 tx
.vin
[0].nSequence
= 1;
472 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
473 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
;
474 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
475 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
| 1;
476 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
478 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
480 // None of the of the absolute height/time locked tx should have made
481 // it into the template because we still check IsFinalTx in CreateNewBlock,
482 // but relative locked txs will if inconsistently added to mempool.
483 // For now these will still generate a valid template until BIP68 soft fork
484 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 3);
485 // However if we advance height by 1 and time by 512, all of them should be mined
486 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
487 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
488 chainActive
.Tip()->nHeight
++;
489 SetMockTime(chainActive
.Tip()->GetMedianTimePast() + 1);
491 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
492 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 5);
494 chainActive
.Tip()->nHeight
--;
498 TestPackageSelection(chainparams
, scriptPubKey
, txFirst
);
500 fCheckpointsEnabled
= true;
503 BOOST_AUTO_TEST_SUITE_END()