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 static CFeeRate blockMinFeeRate
= CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE
);
31 static BlockAssembler
AssemblerForTest(const CChainParams
& params
) {
32 BlockAssembler::Options options
;
34 options
.nBlockMaxWeight
= MAX_BLOCK_WEIGHT
;
35 options
.nBlockMaxSize
= MAX_BLOCK_SERIALIZED_SIZE
;
36 options
.blockMinFeeRate
= blockMinFeeRate
;
37 return BlockAssembler(params
, options
);
42 unsigned char extranonce
;
45 {4, 0xa4a3e223}, {2, 0x15c32f9e}, {1, 0x0375b547}, {1, 0x7004a8a5},
46 {2, 0xce440296}, {2, 0x52cfe198}, {1, 0x77a72cd0}, {2, 0xbb5d6f84},
47 {2, 0x83f30c2c}, {1, 0x48a73d5b}, {1, 0xef7dcd01}, {2, 0x6809c6c4},
48 {2, 0x0883ab3c}, {1, 0x087bbbe2}, {2, 0x2104a814}, {2, 0xdffb6daa},
49 {1, 0xee8a0a08}, {2, 0xba4237c1}, {1, 0xa70349dc}, {1, 0x344722bb},
50 {3, 0xd6294733}, {2, 0xec9f5c94}, {2, 0xca2fbc28}, {1, 0x6ba4f406},
51 {2, 0x015d4532}, {1, 0x6e119b7c}, {2, 0x43e8f314}, {2, 0x27962f38},
52 {2, 0xb571b51b}, {2, 0xb36bee23}, {2, 0xd17924a8}, {2, 0x6bc212d9},
53 {1, 0x630d4948}, {2, 0x9a4c4ebb}, {2, 0x554be537}, {1, 0xd63ddfc7},
54 {2, 0xa10acc11}, {1, 0x759a8363}, {2, 0xfb73090d}, {1, 0xe82c6a34},
55 {1, 0xe33e92d7}, {3, 0x658ef5cb}, {2, 0xba32ff22}, {5, 0x0227a10c},
56 {1, 0xa9a70155}, {5, 0xd096d809}, {1, 0x37176174}, {1, 0x830b8d0f},
57 {1, 0xc6e3910e}, {2, 0x823f3ca8}, {1, 0x99850849}, {1, 0x7521fb81},
58 {1, 0xaacaabab}, {1, 0xd645a2eb}, {5, 0x7aea1781}, {5, 0x9d6e4b78},
59 {1, 0x4ce90fd8}, {1, 0xabdc832d}, {6, 0x4a34f32a}, {2, 0xf2524c1c},
60 {2, 0x1bbeb08a}, {1, 0xad47f480}, {1, 0x9f026aeb}, {1, 0x15a95049},
61 {2, 0xd1cb95b2}, {2, 0xf84bbda5}, {1, 0x0fa62cd1}, {1, 0xe05f9169},
62 {1, 0x78d194a9}, {5, 0x3e38147b}, {5, 0x737ba0d4}, {1, 0x63378e10},
63 {1, 0x6d5f91cf}, {2, 0x88612eb8}, {2, 0xe9639484}, {1, 0xb7fabc9d},
64 {2, 0x19b01592}, {1, 0x5a90dd31}, {2, 0x5bd7e028}, {2, 0x94d00323},
65 {1, 0xa9b9c01a}, {1, 0x3a40de61}, {1, 0x56e7eec7}, {5, 0x859f7ef6},
66 {1, 0xfd8e5630}, {1, 0x2b0c9f7f}, {1, 0xba700e26}, {1, 0x7170a408},
67 {1, 0x70de86a8}, {1, 0x74d64cd5}, {1, 0x49e738a1}, {2, 0x6910b602},
68 {0, 0x643c565f}, {1, 0x54264b3f}, {2, 0x97ea6396}, {2, 0x55174459},
69 {2, 0x03e8779a}, {1, 0x98f34d8f}, {1, 0xc07b2b07}, {1, 0xdfe29668},
70 {1, 0x3141c7c1}, {1, 0xb3b595f4}, {1, 0x735abf08}, {5, 0x623bfbce},
71 {2, 0xd351e722}, {1, 0xf4ca48c9}, {1, 0x5b19c670}, {1, 0xa164bf0e},
72 {2, 0xbbbeb305}, {2, 0xfe1c810a},
75 CBlockIndex
CreateBlockIndex(int nHeight
)
78 index
.nHeight
= nHeight
;
79 index
.pprev
= chainActive
.Tip();
83 bool TestSequenceLocks(const CTransaction
&tx
, int flags
)
86 return CheckSequenceLocks(tx
, flags
);
89 // Test suite for ancestor feerate transaction selection.
90 // Implemented as an additional function, rather than a separate test case,
91 // to allow reusing the blockchain created in CreateNewBlock_validity.
92 void TestPackageSelection(const CChainParams
& chainparams
, CScript scriptPubKey
, std::vector
<CTransactionRef
>& txFirst
)
94 // Test the ancestor feerate transaction selection.
95 TestMemPoolEntryHelper entry
;
97 // Test that a medium fee transaction will be selected after a higher fee
98 // rate package with a low fee rate parent.
99 CMutableTransaction tx
;
101 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
102 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
103 tx
.vin
[0].prevout
.n
= 0;
105 tx
.vout
[0].nValue
= 5000000000LL - 1000;
106 // This tx has a low fee: 1000 satoshis
107 uint256 hashParentTx
= tx
.GetHash(); // save this txid for later use
108 mempool
.addUnchecked(hashParentTx
, entry
.Fee(1000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
110 // This tx has a medium fee: 10000 satoshis
111 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
112 tx
.vout
[0].nValue
= 5000000000LL - 10000;
113 uint256 hashMediumFeeTx
= tx
.GetHash();
114 mempool
.addUnchecked(hashMediumFeeTx
, entry
.Fee(10000).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
116 // This tx has a high fee, but depends on the first transaction
117 tx
.vin
[0].prevout
.hash
= hashParentTx
;
118 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 50k satoshi fee
119 uint256 hashHighFeeTx
= tx
.GetHash();
120 mempool
.addUnchecked(hashHighFeeTx
, entry
.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
122 std::unique_ptr
<CBlockTemplate
> pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
123 BOOST_CHECK(pblocktemplate
->block
.vtx
[1]->GetHash() == hashParentTx
);
124 BOOST_CHECK(pblocktemplate
->block
.vtx
[2]->GetHash() == hashHighFeeTx
);
125 BOOST_CHECK(pblocktemplate
->block
.vtx
[3]->GetHash() == hashMediumFeeTx
);
127 // Test that a package below the block min tx fee doesn't get included
128 tx
.vin
[0].prevout
.hash
= hashHighFeeTx
;
129 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000; // 0 fee
130 uint256 hashFreeTx
= tx
.GetHash();
131 mempool
.addUnchecked(hashFreeTx
, entry
.Fee(0).FromTx(tx
));
132 size_t freeTxSize
= ::GetSerializeSize(tx
, SER_NETWORK
, PROTOCOL_VERSION
);
134 // Calculate a fee on child transaction that will put the package just
135 // below the block min tx fee (assuming 1 child tx of the same size).
136 CAmount feeToUse
= blockMinFeeRate
.GetFee(2*freeTxSize
) - 1;
138 tx
.vin
[0].prevout
.hash
= hashFreeTx
;
139 tx
.vout
[0].nValue
= 5000000000LL - 1000 - 50000 - feeToUse
;
140 uint256 hashLowFeeTx
= tx
.GetHash();
141 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
).FromTx(tx
));
142 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
143 // Verify that the free tx and the low fee tx didn't get selected
144 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
145 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx
);
146 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx
);
149 // Test that packages above the min relay fee do get included, even if one
150 // of the transactions is below the min relay fee
151 // Remove the low fee transaction and replace with a higher fee transaction
152 mempool
.removeRecursive(tx
);
153 tx
.vout
[0].nValue
-= 2; // Now we should be just over the min relay fee
154 hashLowFeeTx
= tx
.GetHash();
155 mempool
.addUnchecked(hashLowFeeTx
, entry
.Fee(feeToUse
+2).FromTx(tx
));
156 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
157 BOOST_CHECK(pblocktemplate
->block
.vtx
[4]->GetHash() == hashFreeTx
);
158 BOOST_CHECK(pblocktemplate
->block
.vtx
[5]->GetHash() == hashLowFeeTx
);
160 // Test that transaction selection properly updates ancestor fee
161 // calculations as ancestor transactions get included in a block.
162 // Add a 0-fee transaction that has 2 outputs.
163 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
165 tx
.vout
[0].nValue
= 5000000000LL - 100000000;
166 tx
.vout
[1].nValue
= 100000000; // 1BTC output
167 uint256 hashFreeTx2
= tx
.GetHash();
168 mempool
.addUnchecked(hashFreeTx2
, entry
.Fee(0).SpendsCoinbase(true).FromTx(tx
));
170 // This tx can't be mined by itself
171 tx
.vin
[0].prevout
.hash
= hashFreeTx2
;
173 feeToUse
= blockMinFeeRate
.GetFee(freeTxSize
);
174 tx
.vout
[0].nValue
= 5000000000LL - 100000000 - feeToUse
;
175 uint256 hashLowFeeTx2
= tx
.GetHash();
176 mempool
.addUnchecked(hashLowFeeTx2
, entry
.Fee(feeToUse
).SpendsCoinbase(false).FromTx(tx
));
177 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
179 // Verify that this tx isn't selected.
180 for (size_t i
=0; i
<pblocktemplate
->block
.vtx
.size(); ++i
) {
181 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashFreeTx2
);
182 BOOST_CHECK(pblocktemplate
->block
.vtx
[i
]->GetHash() != hashLowFeeTx2
);
185 // This tx will be mineable, and should cause hashLowFeeTx2 to be selected
187 tx
.vin
[0].prevout
.n
= 1;
188 tx
.vout
[0].nValue
= 100000000 - 10000; // 10k satoshi fee
189 mempool
.addUnchecked(tx
.GetHash(), entry
.Fee(10000).FromTx(tx
));
190 pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
);
191 BOOST_CHECK(pblocktemplate
->block
.vtx
[8]->GetHash() == hashLowFeeTx2
);
194 // NOTE: These tests rely on CreateNewBlock doing its own self-validation!
195 BOOST_AUTO_TEST_CASE(CreateNewBlock_validity
)
197 // Note that by default, these tests run with size accounting enabled.
198 const auto chainParams
= CreateChainParams(CBaseChainParams::MAIN
);
199 const CChainParams
& chainparams
= *chainParams
;
200 CScript scriptPubKey
= CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG
;
201 std::unique_ptr
<CBlockTemplate
> pblocktemplate
;
202 CMutableTransaction tx
,tx2
;
205 TestMemPoolEntryHelper entry
;
210 fCheckpointsEnabled
= false;
212 // Simple block creation, nothing special yet:
213 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
215 // We can't make transactions until we have inputs
216 // Therefore, load 100 blocks :)
218 std::vector
<CTransactionRef
> txFirst
;
219 for (unsigned int i
= 0; i
< sizeof(blockinfo
)/sizeof(*blockinfo
); ++i
)
221 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
222 pblock
->nVersion
= 1;
223 pblock
->nTime
= chainActive
.Tip()->GetMedianTimePast()+1;
224 CMutableTransaction
txCoinbase(*pblock
->vtx
[0]);
225 txCoinbase
.nVersion
= 1;
226 txCoinbase
.vin
[0].scriptSig
= CScript();
227 txCoinbase
.vin
[0].scriptSig
.push_back(blockinfo
[i
].extranonce
);
228 txCoinbase
.vin
[0].scriptSig
.push_back(chainActive
.Height());
229 txCoinbase
.vout
.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
230 txCoinbase
.vout
[0].scriptPubKey
= CScript();
231 pblock
->vtx
[0] = MakeTransactionRef(std::move(txCoinbase
));
232 if (txFirst
.size() == 0)
233 baseheight
= chainActive
.Height();
234 if (txFirst
.size() < 4)
235 txFirst
.push_back(pblock
->vtx
[0]);
236 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
237 pblock
->nNonce
= blockinfo
[i
].nonce
;
238 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(*pblock
);
239 BOOST_CHECK(ProcessNewBlock(chainparams
, shared_pblock
, true, NULL
));
240 pblock
->hashPrevBlock
= pblock
->GetHash();
243 // Just to make sure we can still make simple blocks
244 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
246 const CAmount BLOCKSUBSIDY
= 50*COIN
;
247 const CAmount LOWFEE
= CENT
;
248 const CAmount HIGHFEE
= COIN
;
249 const CAmount HIGHERFEE
= 4*COIN
;
251 // block sigops > limit: 1000 CHECKMULTISIG + 1
253 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
254 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_0
<< OP_0
<< OP_NOP
<< OP_CHECKMULTISIG
<< OP_1
;
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
;
263 bool spendsCoinbase
= (i
== 0) ? true : false; // only first tx spends coinbase
264 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
265 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
266 tx
.vin
[0].prevout
.hash
= hash
;
268 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
271 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
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) ? true : false; // only first tx spends coinbase
278 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
279 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).SigOpsCost(80).FromTx(tx
));
280 tx
.vin
[0].prevout
.hash
= hash
;
282 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
285 // block size > limit
286 tx
.vin
[0].scriptSig
= CScript();
287 // 18 * (520char + DROP) + OP_1 = 9433 bytes
288 std::vector
<unsigned char> vchData(520);
289 for (unsigned int i
= 0; i
< 18; ++i
)
290 tx
.vin
[0].scriptSig
<< vchData
<< OP_DROP
;
291 tx
.vin
[0].scriptSig
<< OP_1
;
292 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
293 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
294 for (unsigned int i
= 0; i
< 128; ++i
)
296 tx
.vout
[0].nValue
-= LOWFEE
;
298 bool spendsCoinbase
= (i
== 0) ? true : false; // only first tx spends coinbase
299 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
300 tx
.vin
[0].prevout
.hash
= hash
;
302 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
305 // orphan in mempool, template creation fails
307 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).FromTx(tx
));
308 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
311 // child with higher feerate than parent
312 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
313 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
314 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
316 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
317 tx
.vin
[0].prevout
.hash
= hash
;
319 tx
.vin
[1].scriptSig
= CScript() << OP_1
;
320 tx
.vin
[1].prevout
.hash
= txFirst
[0]->GetHash();
321 tx
.vin
[1].prevout
.n
= 0;
322 tx
.vout
[0].nValue
= tx
.vout
[0].nValue
+BLOCKSUBSIDY
-HIGHERFEE
; //First txn output + fresh coinbase - new txn fee
324 mempool
.addUnchecked(hash
, entry
.Fee(HIGHERFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
325 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
328 // coinbase in mempool, template creation fails
330 tx
.vin
[0].prevout
.SetNull();
331 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_1
;
332 tx
.vout
[0].nValue
= 0;
334 // give it a fee so it'll get mined
335 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
336 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
339 // invalid (pre-p2sh) txn in mempool, template creation fails
340 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
341 tx
.vin
[0].prevout
.n
= 0;
342 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
343 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-LOWFEE
;
344 script
= CScript() << OP_0
;
345 tx
.vout
[0].scriptPubKey
= GetScriptForDestination(CScriptID(script
));
347 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
348 tx
.vin
[0].prevout
.hash
= hash
;
349 tx
.vin
[0].scriptSig
= CScript() << std::vector
<unsigned char>(script
.begin(), script
.end());
350 tx
.vout
[0].nValue
-= LOWFEE
;
352 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
353 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
356 // double spend txn pair in mempool, template creation fails
357 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
358 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
359 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
360 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
362 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
363 tx
.vout
[0].scriptPubKey
= CScript() << OP_2
;
365 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
366 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
370 int nHeight
= chainActive
.Height();
371 // Create an actual 209999-long block chain (without valid blocks).
372 while (chainActive
.Tip()->nHeight
< 209999) {
373 CBlockIndex
* prev
= chainActive
.Tip();
374 CBlockIndex
* next
= new CBlockIndex();
375 next
->phashBlock
= new uint256(InsecureRand256());
376 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
378 next
->nHeight
= prev
->nHeight
+ 1;
380 chainActive
.SetTip(next
);
382 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
383 // Extend to a 210000-long block chain.
384 while (chainActive
.Tip()->nHeight
< 210000) {
385 CBlockIndex
* prev
= chainActive
.Tip();
386 CBlockIndex
* next
= new CBlockIndex();
387 next
->phashBlock
= new uint256(InsecureRand256());
388 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
390 next
->nHeight
= prev
->nHeight
+ 1;
392 chainActive
.SetTip(next
);
394 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
395 // Delete the dummy blocks again.
396 while (chainActive
.Tip()->nHeight
> nHeight
) {
397 CBlockIndex
* del
= chainActive
.Tip();
398 chainActive
.SetTip(del
->pprev
);
399 pcoinsTip
->SetBestBlock(del
->pprev
->GetBlockHash());
400 delete del
->phashBlock
;
404 // non-final txs in mempool
405 SetMockTime(chainActive
.Tip()->GetMedianTimePast()+1);
406 int flags
= LOCKTIME_VERIFY_SEQUENCE
|LOCKTIME_MEDIAN_TIME_PAST
;
408 std::vector
<int> prevheights
;
410 // relative height locked
413 prevheights
.resize(1);
414 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash(); // only 1 transaction
415 tx
.vin
[0].prevout
.n
= 0;
416 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
417 tx
.vin
[0].nSequence
= chainActive
.Tip()->nHeight
+ 1; // txFirst[0] is the 2nd block
418 prevheights
[0] = baseheight
+ 1;
420 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
421 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
424 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
425 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
426 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
427 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 2))); // Sequence locks pass on 2nd block
429 // relative time locked
430 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
431 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
432 prevheights
[0] = baseheight
+ 2;
434 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
435 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
436 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
438 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
439 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
440 BOOST_CHECK(SequenceLocks(tx
, flags
, &prevheights
, CreateBlockIndex(chainActive
.Tip()->nHeight
+ 1))); // Sequence locks pass 512 seconds later
441 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
442 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
-= 512; //undo tricked MTP
444 // absolute height locked
445 tx
.vin
[0].prevout
.hash
= txFirst
[2]->GetHash();
446 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_FINAL
- 1;
447 prevheights
[0] = baseheight
+ 3;
448 tx
.nLockTime
= chainActive
.Tip()->nHeight
+ 1;
450 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
451 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
452 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
453 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
455 // absolute time locked
456 tx
.vin
[0].prevout
.hash
= txFirst
[3]->GetHash();
457 tx
.nLockTime
= chainActive
.Tip()->GetMedianTimePast();
458 prevheights
.resize(1);
459 prevheights
[0] = baseheight
+ 4;
461 mempool
.addUnchecked(hash
, entry
.Time(GetTime()).FromTx(tx
));
462 BOOST_CHECK(!CheckFinalTx(tx
, flags
)); // Locktime fails
463 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
464 BOOST_CHECK(IsFinalTx(tx
, chainActive
.Tip()->nHeight
+ 2, chainActive
.Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
466 // mempool-dependent transactions (not added)
467 tx
.vin
[0].prevout
.hash
= hash
;
468 prevheights
[0] = chainActive
.Tip()->nHeight
+ 1;
470 tx
.vin
[0].nSequence
= 0;
471 BOOST_CHECK(CheckFinalTx(tx
, flags
)); // Locktime passes
472 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
473 tx
.vin
[0].nSequence
= 1;
474 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
475 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
;
476 BOOST_CHECK(TestSequenceLocks(tx
, flags
)); // Sequence locks pass
477 tx
.vin
[0].nSequence
= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
| 1;
478 BOOST_CHECK(!TestSequenceLocks(tx
, flags
)); // Sequence locks fail
480 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
482 // None of the of the absolute height/time locked tx should have made
483 // it into the template because we still check IsFinalTx in CreateNewBlock,
484 // but relative locked txs will if inconsistently added to mempool.
485 // For now these will still generate a valid template until BIP68 soft fork
486 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 3);
487 // However if we advance height by 1 and time by 512, all of them should be mined
488 for (int i
= 0; i
< CBlockIndex::nMedianTimeSpan
; i
++)
489 chainActive
.Tip()->GetAncestor(chainActive
.Tip()->nHeight
- i
)->nTime
+= 512; //Trick the MedianTimePast
490 chainActive
.Tip()->nHeight
++;
491 SetMockTime(chainActive
.Tip()->GetMedianTimePast() + 1);
493 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
494 BOOST_CHECK_EQUAL(pblocktemplate
->block
.vtx
.size(), 5);
496 chainActive
.Tip()->nHeight
--;
500 TestPackageSelection(chainparams
, scriptPubKey
, txFirst
);
502 fCheckpointsEnabled
= true;
505 BOOST_AUTO_TEST_SUITE_END()