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
.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 auto chainParams
= CreateChainParams(CBaseChainParams::MAIN
);
198 const CChainParams
& chainparams
= *chainParams
;
199 CScript scriptPubKey
= CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG
;
200 std::unique_ptr
<CBlockTemplate
> pblocktemplate
;
201 CMutableTransaction tx
,tx2
;
204 TestMemPoolEntryHelper entry
;
209 fCheckpointsEnabled
= false;
211 // Simple block creation, nothing special yet:
212 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
214 // We can't make transactions until we have inputs
215 // Therefore, load 100 blocks :)
217 std::vector
<CTransactionRef
> txFirst
;
218 for (unsigned int i
= 0; i
< sizeof(blockinfo
)/sizeof(*blockinfo
); ++i
)
220 CBlock
*pblock
= &pblocktemplate
->block
; // pointer for convenience
221 pblock
->nVersion
= 1;
222 pblock
->nTime
= chainActive
.Tip()->GetMedianTimePast()+1;
223 CMutableTransaction
txCoinbase(*pblock
->vtx
[0]);
224 txCoinbase
.nVersion
= 1;
225 txCoinbase
.vin
[0].scriptSig
= CScript();
226 txCoinbase
.vin
[0].scriptSig
.push_back(blockinfo
[i
].extranonce
);
227 txCoinbase
.vin
[0].scriptSig
.push_back(chainActive
.Height());
228 txCoinbase
.vout
.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
229 txCoinbase
.vout
[0].scriptPubKey
= CScript();
230 pblock
->vtx
[0] = MakeTransactionRef(std::move(txCoinbase
));
231 if (txFirst
.size() == 0)
232 baseheight
= chainActive
.Height();
233 if (txFirst
.size() < 4)
234 txFirst
.push_back(pblock
->vtx
[0]);
235 pblock
->hashMerkleRoot
= BlockMerkleRoot(*pblock
);
236 pblock
->nNonce
= blockinfo
[i
].nonce
;
237 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(*pblock
);
238 BOOST_CHECK(ProcessNewBlock(chainparams
, shared_pblock
, true, nullptr));
239 pblock
->hashPrevBlock
= pblock
->GetHash();
242 // Just to make sure we can still make simple blocks
243 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
245 const CAmount BLOCKSUBSIDY
= 50*COIN
;
246 const CAmount LOWFEE
= CENT
;
247 const CAmount HIGHFEE
= COIN
;
248 const CAmount HIGHERFEE
= 4*COIN
;
250 // block sigops > limit: 1000 CHECKMULTISIG + 1
252 // NOTE: OP_NOP is used to force 20 SigOps for the CHECKMULTISIG
253 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_0
<< OP_0
<< OP_NOP
<< OP_CHECKMULTISIG
<< OP_1
;
254 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
255 tx
.vin
[0].prevout
.n
= 0;
257 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
258 for (unsigned int i
= 0; i
< 1001; ++i
)
260 tx
.vout
[0].nValue
-= LOWFEE
;
262 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
263 // If we don't set the # of sig ops in the CTxMemPoolEntry, template creation fails
264 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
265 tx
.vin
[0].prevout
.hash
= hash
;
267 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
270 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
271 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
272 for (unsigned int i
= 0; i
< 1001; ++i
)
274 tx
.vout
[0].nValue
-= LOWFEE
;
276 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
277 // If we do set the # of sig ops in the CTxMemPoolEntry, template creation passes
278 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).SigOpsCost(80).FromTx(tx
));
279 tx
.vin
[0].prevout
.hash
= hash
;
281 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
284 // block size > limit
285 tx
.vin
[0].scriptSig
= CScript();
286 // 18 * (520char + DROP) + OP_1 = 9433 bytes
287 std::vector
<unsigned char> vchData(520);
288 for (unsigned int i
= 0; i
< 18; ++i
)
289 tx
.vin
[0].scriptSig
<< vchData
<< OP_DROP
;
290 tx
.vin
[0].scriptSig
<< OP_1
;
291 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
292 tx
.vout
[0].nValue
= BLOCKSUBSIDY
;
293 for (unsigned int i
= 0; i
< 128; ++i
)
295 tx
.vout
[0].nValue
-= LOWFEE
;
297 bool spendsCoinbase
= i
== 0; // only first tx spends coinbase
298 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(spendsCoinbase
).FromTx(tx
));
299 tx
.vin
[0].prevout
.hash
= hash
;
301 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
304 // orphan in mempool, template creation fails
306 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).FromTx(tx
));
307 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
310 // child with higher feerate than parent
311 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
312 tx
.vin
[0].prevout
.hash
= txFirst
[1]->GetHash();
313 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
315 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
316 tx
.vin
[0].prevout
.hash
= hash
;
318 tx
.vin
[1].scriptSig
= CScript() << OP_1
;
319 tx
.vin
[1].prevout
.hash
= txFirst
[0]->GetHash();
320 tx
.vin
[1].prevout
.n
= 0;
321 tx
.vout
[0].nValue
= tx
.vout
[0].nValue
+BLOCKSUBSIDY
-HIGHERFEE
; //First txn output + fresh coinbase - new txn fee
323 mempool
.addUnchecked(hash
, entry
.Fee(HIGHERFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
324 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
327 // coinbase in mempool, template creation fails
329 tx
.vin
[0].prevout
.SetNull();
330 tx
.vin
[0].scriptSig
= CScript() << OP_0
<< OP_1
;
331 tx
.vout
[0].nValue
= 0;
333 // give it a fee so it'll get mined
334 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
335 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
338 // double spend txn pair in mempool, template creation fails
339 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
340 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
341 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-HIGHFEE
;
342 tx
.vout
[0].scriptPubKey
= CScript() << OP_1
;
344 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
345 tx
.vout
[0].scriptPubKey
= CScript() << OP_2
;
347 mempool
.addUnchecked(hash
, entry
.Fee(HIGHFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
348 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
352 int nHeight
= chainActive
.Height();
353 // Create an actual 209999-long block chain (without valid blocks).
354 while (chainActive
.Tip()->nHeight
< 209999) {
355 CBlockIndex
* prev
= chainActive
.Tip();
356 CBlockIndex
* next
= new CBlockIndex();
357 next
->phashBlock
= new uint256(InsecureRand256());
358 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
360 next
->nHeight
= prev
->nHeight
+ 1;
362 chainActive
.SetTip(next
);
364 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
365 // Extend to a 210000-long block chain.
366 while (chainActive
.Tip()->nHeight
< 210000) {
367 CBlockIndex
* prev
= chainActive
.Tip();
368 CBlockIndex
* next
= new CBlockIndex();
369 next
->phashBlock
= new uint256(InsecureRand256());
370 pcoinsTip
->SetBestBlock(next
->GetBlockHash());
372 next
->nHeight
= prev
->nHeight
+ 1;
374 chainActive
.SetTip(next
);
376 BOOST_CHECK(pblocktemplate
= AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
));
378 // invalid p2sh txn in mempool, template creation fails
379 tx
.vin
[0].prevout
.hash
= txFirst
[0]->GetHash();
380 tx
.vin
[0].prevout
.n
= 0;
381 tx
.vin
[0].scriptSig
= CScript() << OP_1
;
382 tx
.vout
[0].nValue
= BLOCKSUBSIDY
-LOWFEE
;
383 script
= CScript() << OP_0
;
384 tx
.vout
[0].scriptPubKey
= GetScriptForDestination(CScriptID(script
));
386 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(true).FromTx(tx
));
387 tx
.vin
[0].prevout
.hash
= hash
;
388 tx
.vin
[0].scriptSig
= CScript() << std::vector
<unsigned char>(script
.begin(), script
.end());
389 tx
.vout
[0].nValue
-= LOWFEE
;
391 mempool
.addUnchecked(hash
, entry
.Fee(LOWFEE
).Time(GetTime()).SpendsCoinbase(false).FromTx(tx
));
392 BOOST_CHECK_THROW(AssemblerForTest(chainparams
).CreateNewBlock(scriptPubKey
), std::runtime_error
);
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()