Remove accidental stray semicolon
[bitcoinplatinum.git] / test / functional / p2p-fullblocktest.py
blob1d969fc7c1163f2d6aebbd418ae503bffebcd5d1
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test block processing.
7 This reimplements tests from the bitcoinj/FullBlockTestGenerator used
8 by the pull-tester.
10 We use the testing framework in which we expect a particular answer from
11 each test.
12 """
14 from test_framework.test_framework import ComparisonTestFramework
15 from test_framework.util import *
16 from test_framework.comptool import TestManager, TestInstance, RejectResult
17 from test_framework.blocktools import *
18 import time
19 from test_framework.key import CECKey
20 from test_framework.script import *
21 import struct
23 class PreviousSpendableOutput(object):
24 def __init__(self, tx = CTransaction(), n = -1):
25 self.tx = tx
26 self.n = n # the output we're spending
28 # Use this class for tests that require behavior other than normal "mininode" behavior.
29 # For now, it is used to serialize a bloated varint (b64).
30 class CBrokenBlock(CBlock):
31 def __init__(self, header=None):
32 super(CBrokenBlock, self).__init__(header)
34 def initialize(self, base_block):
35 self.vtx = copy.deepcopy(base_block.vtx)
36 self.hashMerkleRoot = self.calc_merkle_root()
38 def serialize(self):
39 r = b""
40 r += super(CBlock, self).serialize()
41 r += struct.pack("<BQ", 255, len(self.vtx))
42 for tx in self.vtx:
43 r += tx.serialize()
44 return r
46 def normal_serialize(self):
47 r = b""
48 r += super(CBrokenBlock, self).serialize()
49 return r
51 class FullBlockTest(ComparisonTestFramework):
52 # Can either run this test as 1 node with expected answers, or two and compare them.
53 # Change the "outcome" variable from each TestInstance object to only do the comparison.
54 def set_test_params(self):
55 self.num_nodes = 1
56 self.setup_clean_chain = True
57 self.block_heights = {}
58 self.coinbase_key = CECKey()
59 self.coinbase_key.set_secretbytes(b"horsebattery")
60 self.coinbase_pubkey = self.coinbase_key.get_pubkey()
61 self.tip = None
62 self.blocks = {}
64 def add_options(self, parser):
65 super().add_options(parser)
66 parser.add_option("--runbarelyexpensive", dest="runbarelyexpensive", default=True)
68 def run_test(self):
69 self.test = TestManager(self, self.options.tmpdir)
70 self.test.add_all_connections(self.nodes)
71 NetworkThread().start() # Start up network handling in another thread
72 self.test.run()
74 def add_transactions_to_block(self, block, tx_list):
75 [ tx.rehash() for tx in tx_list ]
76 block.vtx.extend(tx_list)
78 # this is a little handier to use than the version in blocktools.py
79 def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])):
80 tx = create_transaction(spend_tx, n, b"", value, script)
81 return tx
83 # sign a transaction, using the key we know about
84 # this signs input 0 in tx, which is assumed to be spending output n in spend_tx
85 def sign_tx(self, tx, spend_tx, n):
86 scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey)
87 if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend
88 tx.vin[0].scriptSig = CScript()
89 return
90 (sighash, err) = SignatureHash(spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL)
91 tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))])
93 def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])):
94 tx = self.create_tx(spend_tx, n, value, script)
95 self.sign_tx(tx, spend_tx, n)
96 tx.rehash()
97 return tx
99 def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE]), solve=True):
100 if self.tip == None:
101 base_block_hash = self.genesis_hash
102 block_time = int(time.time())+1
103 else:
104 base_block_hash = self.tip.sha256
105 block_time = self.tip.nTime + 1
106 # First create the coinbase
107 height = self.block_heights[base_block_hash] + 1
108 coinbase = create_coinbase(height, self.coinbase_pubkey)
109 coinbase.vout[0].nValue += additional_coinbase_value
110 coinbase.rehash()
111 if spend == None:
112 block = create_block(base_block_hash, coinbase, block_time)
113 else:
114 coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 # all but one satoshi to fees
115 coinbase.rehash()
116 block = create_block(base_block_hash, coinbase, block_time)
117 tx = create_transaction(spend.tx, spend.n, b"", 1, script) # spend 1 satoshi
118 self.sign_tx(tx, spend.tx, spend.n)
119 self.add_transactions_to_block(block, [tx])
120 block.hashMerkleRoot = block.calc_merkle_root()
121 if solve:
122 block.solve()
123 self.tip = block
124 self.block_heights[block.sha256] = height
125 assert number not in self.blocks
126 self.blocks[number] = block
127 return block
129 def get_tests(self):
130 self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16)
131 self.block_heights[self.genesis_hash] = 0
132 spendable_outputs = []
134 # save the current tip so it can be spent by a later block
135 def save_spendable_output():
136 spendable_outputs.append(self.tip)
138 # get an output that we previously marked as spendable
139 def get_spendable_output():
140 return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0)
142 # returns a test case that asserts that the current tip was accepted
143 def accepted():
144 return TestInstance([[self.tip, True]])
146 # returns a test case that asserts that the current tip was rejected
147 def rejected(reject = None):
148 if reject is None:
149 return TestInstance([[self.tip, False]])
150 else:
151 return TestInstance([[self.tip, reject]])
153 # move the tip back to a previous block
154 def tip(number):
155 self.tip = self.blocks[number]
157 # adds transactions to the block and updates state
158 def update_block(block_number, new_transactions):
159 block = self.blocks[block_number]
160 self.add_transactions_to_block(block, new_transactions)
161 old_sha256 = block.sha256
162 block.hashMerkleRoot = block.calc_merkle_root()
163 block.solve()
164 # Update the internal state just like in next_block
165 self.tip = block
166 if block.sha256 != old_sha256:
167 self.block_heights[block.sha256] = self.block_heights[old_sha256]
168 del self.block_heights[old_sha256]
169 self.blocks[block_number] = block
170 return block
172 # shorthand for functions
173 block = self.next_block
174 create_tx = self.create_tx
175 create_and_sign_tx = self.create_and_sign_transaction
177 # these must be updated if consensus changes
178 MAX_BLOCK_SIGOPS = 20000
181 # Create a new block
182 block(0)
183 save_spendable_output()
184 yield accepted()
187 # Now we need that block to mature so we can spend the coinbase.
188 test = TestInstance(sync_every_block=False)
189 for i in range(99):
190 block(5000 + i)
191 test.blocks_and_transactions.append([self.tip, True])
192 save_spendable_output()
193 yield test
195 # collect spendable outputs now to avoid cluttering the code later on
196 out = []
197 for i in range(33):
198 out.append(get_spendable_output())
200 # Start by building a couple of blocks on top (which output is spent is
201 # in parentheses):
202 # genesis -> b1 (0) -> b2 (1)
203 block(1, spend=out[0])
204 save_spendable_output()
205 yield accepted()
207 block(2, spend=out[1])
208 yield accepted()
209 save_spendable_output()
211 # so fork like this:
213 # genesis -> b1 (0) -> b2 (1)
214 # \-> b3 (1)
216 # Nothing should happen at this point. We saw b2 first so it takes priority.
217 tip(1)
218 b3 = block(3, spend=out[1])
219 txout_b3 = PreviousSpendableOutput(b3.vtx[1], 0)
220 yield rejected()
223 # Now we add another block to make the alternative chain longer.
225 # genesis -> b1 (0) -> b2 (1)
226 # \-> b3 (1) -> b4 (2)
227 block(4, spend=out[2])
228 yield accepted()
231 # ... and back to the first chain.
232 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
233 # \-> b3 (1) -> b4 (2)
234 tip(2)
235 block(5, spend=out[2])
236 save_spendable_output()
237 yield rejected()
239 block(6, spend=out[3])
240 yield accepted()
242 # Try to create a fork that double-spends
243 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
244 # \-> b7 (2) -> b8 (4)
245 # \-> b3 (1) -> b4 (2)
246 tip(5)
247 block(7, spend=out[2])
248 yield rejected()
250 block(8, spend=out[4])
251 yield rejected()
253 # Try to create a block that has too much fee
254 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
255 # \-> b9 (4)
256 # \-> b3 (1) -> b4 (2)
257 tip(6)
258 block(9, spend=out[4], additional_coinbase_value=1)
259 yield rejected(RejectResult(16, b'bad-cb-amount'))
261 # Create a fork that ends in a block with too much fee (the one that causes the reorg)
262 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
263 # \-> b10 (3) -> b11 (4)
264 # \-> b3 (1) -> b4 (2)
265 tip(5)
266 block(10, spend=out[3])
267 yield rejected()
269 block(11, spend=out[4], additional_coinbase_value=1)
270 yield rejected(RejectResult(16, b'bad-cb-amount'))
273 # Try again, but with a valid fork first
274 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
275 # \-> b12 (3) -> b13 (4) -> b14 (5)
276 # (b12 added last)
277 # \-> b3 (1) -> b4 (2)
278 tip(5)
279 b12 = block(12, spend=out[3])
280 save_spendable_output()
281 b13 = block(13, spend=out[4])
282 # Deliver the block header for b12, and the block b13.
283 # b13 should be accepted but the tip won't advance until b12 is delivered.
284 yield TestInstance([[CBlockHeader(b12), None], [b13, False]])
286 save_spendable_output()
287 # b14 is invalid, but the node won't know that until it tries to connect
288 # Tip still can't advance because b12 is missing
289 block(14, spend=out[5], additional_coinbase_value=1)
290 yield rejected()
292 yield TestInstance([[b12, True, b13.sha256]]) # New tip should be b13.
294 # Add a block with MAX_BLOCK_SIGOPS and one with one more sigop
295 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
296 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b16 (6)
297 # \-> b3 (1) -> b4 (2)
299 # Test that a block with a lot of checksigs is okay
300 lots_of_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS - 1))
301 tip(13)
302 block(15, spend=out[5], script=lots_of_checksigs)
303 yield accepted()
304 save_spendable_output()
307 # Test that a block with too many checksigs is rejected
308 too_many_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
309 block(16, spend=out[6], script=too_many_checksigs)
310 yield rejected(RejectResult(16, b'bad-blk-sigops'))
313 # Attempt to spend a transaction created on a different fork
314 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
315 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b17 (b3.vtx[1])
316 # \-> b3 (1) -> b4 (2)
317 tip(15)
318 block(17, spend=txout_b3)
319 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
321 # Attempt to spend a transaction created on a different fork (on a fork this time)
322 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
323 # \-> b12 (3) -> b13 (4) -> b15 (5)
324 # \-> b18 (b3.vtx[1]) -> b19 (6)
325 # \-> b3 (1) -> b4 (2)
326 tip(13)
327 block(18, spend=txout_b3)
328 yield rejected()
330 block(19, spend=out[6])
331 yield rejected()
333 # Attempt to spend a coinbase at depth too low
334 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
335 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b20 (7)
336 # \-> b3 (1) -> b4 (2)
337 tip(15)
338 block(20, spend=out[7])
339 yield rejected(RejectResult(16, b'bad-txns-premature-spend-of-coinbase'))
341 # Attempt to spend a coinbase at depth too low (on a fork this time)
342 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
343 # \-> b12 (3) -> b13 (4) -> b15 (5)
344 # \-> b21 (6) -> b22 (5)
345 # \-> b3 (1) -> b4 (2)
346 tip(13)
347 block(21, spend=out[6])
348 yield rejected()
350 block(22, spend=out[5])
351 yield rejected()
353 # Create a block on either side of MAX_BLOCK_BASE_SIZE and make sure its accepted/rejected
354 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
355 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6)
356 # \-> b24 (6) -> b25 (7)
357 # \-> b3 (1) -> b4 (2)
358 tip(15)
359 b23 = block(23, spend=out[6])
360 tx = CTransaction()
361 script_length = MAX_BLOCK_BASE_SIZE - len(b23.serialize()) - 69
362 script_output = CScript([b'\x00' * script_length])
363 tx.vout.append(CTxOut(0, script_output))
364 tx.vin.append(CTxIn(COutPoint(b23.vtx[1].sha256, 0)))
365 b23 = update_block(23, [tx])
366 # Make sure the math above worked out to produce a max-sized block
367 assert_equal(len(b23.serialize()), MAX_BLOCK_BASE_SIZE)
368 yield accepted()
369 save_spendable_output()
371 # Make the next block one byte bigger and check that it fails
372 tip(15)
373 b24 = block(24, spend=out[6])
374 script_length = MAX_BLOCK_BASE_SIZE - len(b24.serialize()) - 69
375 script_output = CScript([b'\x00' * (script_length+1)])
376 tx.vout = [CTxOut(0, script_output)]
377 b24 = update_block(24, [tx])
378 assert_equal(len(b24.serialize()), MAX_BLOCK_BASE_SIZE+1)
379 yield rejected(RejectResult(16, b'bad-blk-length'))
381 block(25, spend=out[7])
382 yield rejected()
384 # Create blocks with a coinbase input script size out of range
385 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
386 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7)
387 # \-> ... (6) -> ... (7)
388 # \-> b3 (1) -> b4 (2)
389 tip(15)
390 b26 = block(26, spend=out[6])
391 b26.vtx[0].vin[0].scriptSig = b'\x00'
392 b26.vtx[0].rehash()
393 # update_block causes the merkle root to get updated, even with no new
394 # transactions, and updates the required state.
395 b26 = update_block(26, [])
396 yield rejected(RejectResult(16, b'bad-cb-length'))
398 # Extend the b26 chain to make sure bitcoind isn't accepting b26
399 block(27, spend=out[7])
400 yield rejected(False)
402 # Now try a too-large-coinbase script
403 tip(15)
404 b28 = block(28, spend=out[6])
405 b28.vtx[0].vin[0].scriptSig = b'\x00' * 101
406 b28.vtx[0].rehash()
407 b28 = update_block(28, [])
408 yield rejected(RejectResult(16, b'bad-cb-length'))
410 # Extend the b28 chain to make sure bitcoind isn't accepting b28
411 block(29, spend=out[7])
412 yield rejected(False)
414 # b30 has a max-sized coinbase scriptSig.
415 tip(23)
416 b30 = block(30)
417 b30.vtx[0].vin[0].scriptSig = b'\x00' * 100
418 b30.vtx[0].rehash()
419 b30 = update_block(30, [])
420 yield accepted()
421 save_spendable_output()
423 # b31 - b35 - check sigops of OP_CHECKMULTISIG / OP_CHECKMULTISIGVERIFY / OP_CHECKSIGVERIFY
425 # genesis -> ... -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10)
426 # \-> b36 (11)
427 # \-> b34 (10)
428 # \-> b32 (9)
431 # MULTISIG: each op code counts as 20 sigops. To create the edge case, pack another 19 sigops at the end.
432 lots_of_multisigs = CScript([OP_CHECKMULTISIG] * ((MAX_BLOCK_SIGOPS-1) // 20) + [OP_CHECKSIG] * 19)
433 b31 = block(31, spend=out[8], script=lots_of_multisigs)
434 assert_equal(get_legacy_sigopcount_block(b31), MAX_BLOCK_SIGOPS)
435 yield accepted()
436 save_spendable_output()
438 # this goes over the limit because the coinbase has one sigop
439 too_many_multisigs = CScript([OP_CHECKMULTISIG] * (MAX_BLOCK_SIGOPS // 20))
440 b32 = block(32, spend=out[9], script=too_many_multisigs)
441 assert_equal(get_legacy_sigopcount_block(b32), MAX_BLOCK_SIGOPS + 1)
442 yield rejected(RejectResult(16, b'bad-blk-sigops'))
445 # CHECKMULTISIGVERIFY
446 tip(31)
447 lots_of_multisigs = CScript([OP_CHECKMULTISIGVERIFY] * ((MAX_BLOCK_SIGOPS-1) // 20) + [OP_CHECKSIG] * 19)
448 block(33, spend=out[9], script=lots_of_multisigs)
449 yield accepted()
450 save_spendable_output()
452 too_many_multisigs = CScript([OP_CHECKMULTISIGVERIFY] * (MAX_BLOCK_SIGOPS // 20))
453 block(34, spend=out[10], script=too_many_multisigs)
454 yield rejected(RejectResult(16, b'bad-blk-sigops'))
457 # CHECKSIGVERIFY
458 tip(33)
459 lots_of_checksigs = CScript([OP_CHECKSIGVERIFY] * (MAX_BLOCK_SIGOPS - 1))
460 b35 = block(35, spend=out[10], script=lots_of_checksigs)
461 yield accepted()
462 save_spendable_output()
464 too_many_checksigs = CScript([OP_CHECKSIGVERIFY] * (MAX_BLOCK_SIGOPS))
465 block(36, spend=out[11], script=too_many_checksigs)
466 yield rejected(RejectResult(16, b'bad-blk-sigops'))
469 # Check spending of a transaction in a block which failed to connect
471 # b6 (3)
472 # b12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10)
473 # \-> b37 (11)
474 # \-> b38 (11/37)
477 # save 37's spendable output, but then double-spend out11 to invalidate the block
478 tip(35)
479 b37 = block(37, spend=out[11])
480 txout_b37 = PreviousSpendableOutput(b37.vtx[1], 0)
481 tx = create_and_sign_tx(out[11].tx, out[11].n, 0)
482 b37 = update_block(37, [tx])
483 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
485 # attempt to spend b37's first non-coinbase tx, at which point b37 was still considered valid
486 tip(35)
487 block(38, spend=txout_b37)
488 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
490 # Check P2SH SigOp counting
493 # 13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b41 (12)
494 # \-> b40 (12)
496 # b39 - create some P2SH outputs that will require 6 sigops to spend:
498 # redeem_script = COINBASE_PUBKEY, (OP_2DUP+OP_CHECKSIGVERIFY) * 5, OP_CHECKSIG
499 # p2sh_script = OP_HASH160, ripemd160(sha256(script)), OP_EQUAL
501 tip(35)
502 b39 = block(39)
503 b39_outputs = 0
504 b39_sigops_per_output = 6
506 # Build the redeem script, hash it, use hash to create the p2sh script
507 redeem_script = CScript([self.coinbase_pubkey] + [OP_2DUP, OP_CHECKSIGVERIFY]*5 + [OP_CHECKSIG])
508 redeem_script_hash = hash160(redeem_script)
509 p2sh_script = CScript([OP_HASH160, redeem_script_hash, OP_EQUAL])
511 # Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE
512 # This must be signed because it is spending a coinbase
513 spend = out[11]
514 tx = create_tx(spend.tx, spend.n, 1, p2sh_script)
515 tx.vout.append(CTxOut(spend.tx.vout[spend.n].nValue - 1, CScript([OP_TRUE])))
516 self.sign_tx(tx, spend.tx, spend.n)
517 tx.rehash()
518 b39 = update_block(39, [tx])
519 b39_outputs += 1
521 # Until block is full, add tx's with 1 satoshi to p2sh_script, the rest to OP_TRUE
522 tx_new = None
523 tx_last = tx
524 total_size=len(b39.serialize())
525 while(total_size < MAX_BLOCK_BASE_SIZE):
526 tx_new = create_tx(tx_last, 1, 1, p2sh_script)
527 tx_new.vout.append(CTxOut(tx_last.vout[1].nValue - 1, CScript([OP_TRUE])))
528 tx_new.rehash()
529 total_size += len(tx_new.serialize())
530 if total_size >= MAX_BLOCK_BASE_SIZE:
531 break
532 b39.vtx.append(tx_new) # add tx to block
533 tx_last = tx_new
534 b39_outputs += 1
536 b39 = update_block(39, [])
537 yield accepted()
538 save_spendable_output()
541 # Test sigops in P2SH redeem scripts
543 # b40 creates 3333 tx's spending the 6-sigop P2SH outputs from b39 for a total of 19998 sigops.
544 # The first tx has one sigop and then at the end we add 2 more to put us just over the max.
546 # b41 does the same, less one, so it has the maximum sigops permitted.
548 tip(39)
549 b40 = block(40, spend=out[12])
550 sigops = get_legacy_sigopcount_block(b40)
551 numTxes = (MAX_BLOCK_SIGOPS - sigops) // b39_sigops_per_output
552 assert_equal(numTxes <= b39_outputs, True)
554 lastOutpoint = COutPoint(b40.vtx[1].sha256, 0)
555 new_txs = []
556 for i in range(1, numTxes+1):
557 tx = CTransaction()
558 tx.vout.append(CTxOut(1, CScript([OP_TRUE])))
559 tx.vin.append(CTxIn(lastOutpoint, b''))
560 # second input is corresponding P2SH output from b39
561 tx.vin.append(CTxIn(COutPoint(b39.vtx[i].sha256, 0), b''))
562 # Note: must pass the redeem_script (not p2sh_script) to the signature hash function
563 (sighash, err) = SignatureHash(redeem_script, tx, 1, SIGHASH_ALL)
564 sig = self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))
565 scriptSig = CScript([sig, redeem_script])
567 tx.vin[1].scriptSig = scriptSig
568 tx.rehash()
569 new_txs.append(tx)
570 lastOutpoint = COutPoint(tx.sha256, 0)
572 b40_sigops_to_fill = MAX_BLOCK_SIGOPS - (numTxes * b39_sigops_per_output + sigops) + 1
573 tx = CTransaction()
574 tx.vin.append(CTxIn(lastOutpoint, b''))
575 tx.vout.append(CTxOut(1, CScript([OP_CHECKSIG] * b40_sigops_to_fill)))
576 tx.rehash()
577 new_txs.append(tx)
578 update_block(40, new_txs)
579 yield rejected(RejectResult(16, b'bad-blk-sigops'))
581 # same as b40, but one less sigop
582 tip(39)
583 block(41, spend=None)
584 update_block(41, b40.vtx[1:-1])
585 b41_sigops_to_fill = b40_sigops_to_fill - 1
586 tx = CTransaction()
587 tx.vin.append(CTxIn(lastOutpoint, b''))
588 tx.vout.append(CTxOut(1, CScript([OP_CHECKSIG] * b41_sigops_to_fill)))
589 tx.rehash()
590 update_block(41, [tx])
591 yield accepted()
593 # Fork off of b39 to create a constant base again
595 # b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13)
596 # \-> b41 (12)
598 tip(39)
599 block(42, spend=out[12])
600 yield rejected()
601 save_spendable_output()
603 block(43, spend=out[13])
604 yield accepted()
605 save_spendable_output()
608 # Test a number of really invalid scenarios
610 # -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b44 (14)
611 # \-> ??? (15)
613 # The next few blocks are going to be created "by hand" since they'll do funky things, such as having
614 # the first transaction be non-coinbase, etc. The purpose of b44 is to make sure this works.
615 height = self.block_heights[self.tip.sha256] + 1
616 coinbase = create_coinbase(height, self.coinbase_pubkey)
617 b44 = CBlock()
618 b44.nTime = self.tip.nTime + 1
619 b44.hashPrevBlock = self.tip.sha256
620 b44.nBits = 0x207fffff
621 b44.vtx.append(coinbase)
622 b44.hashMerkleRoot = b44.calc_merkle_root()
623 b44.solve()
624 self.tip = b44
625 self.block_heights[b44.sha256] = height
626 self.blocks[44] = b44
627 yield accepted()
629 # A block with a non-coinbase as the first tx
630 non_coinbase = create_tx(out[15].tx, out[15].n, 1)
631 b45 = CBlock()
632 b45.nTime = self.tip.nTime + 1
633 b45.hashPrevBlock = self.tip.sha256
634 b45.nBits = 0x207fffff
635 b45.vtx.append(non_coinbase)
636 b45.hashMerkleRoot = b45.calc_merkle_root()
637 b45.calc_sha256()
638 b45.solve()
639 self.block_heights[b45.sha256] = self.block_heights[self.tip.sha256]+1
640 self.tip = b45
641 self.blocks[45] = b45
642 yield rejected(RejectResult(16, b'bad-cb-missing'))
644 # A block with no txns
645 tip(44)
646 b46 = CBlock()
647 b46.nTime = b44.nTime+1
648 b46.hashPrevBlock = b44.sha256
649 b46.nBits = 0x207fffff
650 b46.vtx = []
651 b46.hashMerkleRoot = 0
652 b46.solve()
653 self.block_heights[b46.sha256] = self.block_heights[b44.sha256]+1
654 self.tip = b46
655 assert 46 not in self.blocks
656 self.blocks[46] = b46
657 s = ser_uint256(b46.hashMerkleRoot)
658 yield rejected(RejectResult(16, b'bad-blk-length'))
660 # A block with invalid work
661 tip(44)
662 b47 = block(47, solve=False)
663 target = uint256_from_compact(b47.nBits)
664 while b47.sha256 < target: #changed > to <
665 b47.nNonce += 1
666 b47.rehash()
667 yield rejected(RejectResult(16, b'high-hash'))
669 # A block with timestamp > 2 hrs in the future
670 tip(44)
671 b48 = block(48, solve=False)
672 b48.nTime = int(time.time()) + 60 * 60 * 3
673 b48.solve()
674 yield rejected(RejectResult(16, b'time-too-new'))
676 # A block with an invalid merkle hash
677 tip(44)
678 b49 = block(49)
679 b49.hashMerkleRoot += 1
680 b49.solve()
681 yield rejected(RejectResult(16, b'bad-txnmrklroot'))
683 # A block with an incorrect POW limit
684 tip(44)
685 b50 = block(50)
686 b50.nBits = b50.nBits - 1
687 b50.solve()
688 yield rejected(RejectResult(16, b'bad-diffbits'))
690 # A block with two coinbase txns
691 tip(44)
692 b51 = block(51)
693 cb2 = create_coinbase(51, self.coinbase_pubkey)
694 b51 = update_block(51, [cb2])
695 yield rejected(RejectResult(16, b'bad-cb-multiple'))
697 # A block w/ duplicate txns
698 # Note: txns have to be in the right position in the merkle tree to trigger this error
699 tip(44)
700 b52 = block(52, spend=out[15])
701 tx = create_tx(b52.vtx[1], 0, 1)
702 b52 = update_block(52, [tx, tx])
703 yield rejected(RejectResult(16, b'bad-txns-duplicate'))
705 # Test block timestamps
706 # -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15)
707 # \-> b54 (15)
709 tip(43)
710 block(53, spend=out[14])
711 yield rejected() # rejected since b44 is at same height
712 save_spendable_output()
714 # invalid timestamp (b35 is 5 blocks back, so its time is MedianTimePast)
715 b54 = block(54, spend=out[15])
716 b54.nTime = b35.nTime - 1
717 b54.solve()
718 yield rejected(RejectResult(16, b'time-too-old'))
720 # valid timestamp
721 tip(53)
722 b55 = block(55, spend=out[15])
723 b55.nTime = b35.nTime
724 update_block(55, [])
725 yield accepted()
726 save_spendable_output()
729 # Test CVE-2012-2459
731 # -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57p2 (16)
732 # \-> b57 (16)
733 # \-> b56p2 (16)
734 # \-> b56 (16)
736 # Merkle tree malleability (CVE-2012-2459): repeating sequences of transactions in a block without
737 # affecting the merkle root of a block, while still invalidating it.
738 # See: src/consensus/merkle.h
740 # b57 has three txns: coinbase, tx, tx1. The merkle root computation will duplicate tx.
741 # Result: OK
743 # b56 copies b57 but duplicates tx1 and does not recalculate the block hash. So it has a valid merkle
744 # root but duplicate transactions.
745 # Result: Fails
747 # b57p2 has six transactions in its merkle tree:
748 # - coinbase, tx, tx1, tx2, tx3, tx4
749 # Merkle root calculation will duplicate as necessary.
750 # Result: OK.
752 # b56p2 copies b57p2 but adds both tx3 and tx4. The purpose of the test is to make sure the code catches
753 # duplicate txns that are not next to one another with the "bad-txns-duplicate" error (which indicates
754 # that the error was caught early, avoiding a DOS vulnerability.)
756 # b57 - a good block with 2 txs, don't submit until end
757 tip(55)
758 b57 = block(57)
759 tx = create_and_sign_tx(out[16].tx, out[16].n, 1)
760 tx1 = create_tx(tx, 0, 1)
761 b57 = update_block(57, [tx, tx1])
763 # b56 - copy b57, add a duplicate tx
764 tip(55)
765 b56 = copy.deepcopy(b57)
766 self.blocks[56] = b56
767 assert_equal(len(b56.vtx),3)
768 b56 = update_block(56, [tx1])
769 assert_equal(b56.hash, b57.hash)
770 yield rejected(RejectResult(16, b'bad-txns-duplicate'))
772 # b57p2 - a good block with 6 tx'es, don't submit until end
773 tip(55)
774 b57p2 = block("57p2")
775 tx = create_and_sign_tx(out[16].tx, out[16].n, 1)
776 tx1 = create_tx(tx, 0, 1)
777 tx2 = create_tx(tx1, 0, 1)
778 tx3 = create_tx(tx2, 0, 1)
779 tx4 = create_tx(tx3, 0, 1)
780 b57p2 = update_block("57p2", [tx, tx1, tx2, tx3, tx4])
782 # b56p2 - copy b57p2, duplicate two non-consecutive tx's
783 tip(55)
784 b56p2 = copy.deepcopy(b57p2)
785 self.blocks["b56p2"] = b56p2
786 assert_equal(b56p2.hash, b57p2.hash)
787 assert_equal(len(b56p2.vtx),6)
788 b56p2 = update_block("b56p2", [tx3, tx4])
789 yield rejected(RejectResult(16, b'bad-txns-duplicate'))
791 tip("57p2")
792 yield accepted()
794 tip(57)
795 yield rejected() #rejected because 57p2 seen first
796 save_spendable_output()
798 # Test a few invalid tx types
800 # -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17)
801 # \-> ??? (17)
804 # tx with prevout.n out of range
805 tip(57)
806 b58 = block(58, spend=out[17])
807 tx = CTransaction()
808 assert(len(out[17].tx.vout) < 42)
809 tx.vin.append(CTxIn(COutPoint(out[17].tx.sha256, 42), CScript([OP_TRUE]), 0xffffffff))
810 tx.vout.append(CTxOut(0, b""))
811 tx.calc_sha256()
812 b58 = update_block(58, [tx])
813 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
815 # tx with output value > input value out of range
816 tip(57)
817 b59 = block(59)
818 tx = create_and_sign_tx(out[17].tx, out[17].n, 51*COIN)
819 b59 = update_block(59, [tx])
820 yield rejected(RejectResult(16, b'bad-txns-in-belowout'))
822 # reset to good chain
823 tip(57)
824 b60 = block(60, spend=out[17])
825 yield accepted()
826 save_spendable_output()
828 # Test BIP30
830 # -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17)
831 # \-> b61 (18)
833 # Blocks are not allowed to contain a transaction whose id matches that of an earlier,
834 # not-fully-spent transaction in the same chain. To test, make identical coinbases;
835 # the second one should be rejected.
837 tip(60)
838 b61 = block(61, spend=out[18])
839 b61.vtx[0].vin[0].scriptSig = b60.vtx[0].vin[0].scriptSig #equalize the coinbases
840 b61.vtx[0].rehash()
841 b61 = update_block(61, [])
842 assert_equal(b60.vtx[0].serialize(), b61.vtx[0].serialize())
843 yield rejected(RejectResult(16, b'bad-txns-BIP30'))
846 # Test tx.isFinal is properly rejected (not an exhaustive tx.isFinal test, that should be in data-driven transaction tests)
848 # -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17)
849 # \-> b62 (18)
851 tip(60)
852 b62 = block(62)
853 tx = CTransaction()
854 tx.nLockTime = 0xffffffff #this locktime is non-final
855 assert(out[18].n < len(out[18].tx.vout))
856 tx.vin.append(CTxIn(COutPoint(out[18].tx.sha256, out[18].n))) # don't set nSequence
857 tx.vout.append(CTxOut(0, CScript([OP_TRUE])))
858 assert(tx.vin[0].nSequence < 0xffffffff)
859 tx.calc_sha256()
860 b62 = update_block(62, [tx])
861 yield rejected(RejectResult(16, b'bad-txns-nonfinal'))
864 # Test a non-final coinbase is also rejected
866 # -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17)
867 # \-> b63 (-)
869 tip(60)
870 b63 = block(63)
871 b63.vtx[0].nLockTime = 0xffffffff
872 b63.vtx[0].vin[0].nSequence = 0xDEADBEEF
873 b63.vtx[0].rehash()
874 b63 = update_block(63, [])
875 yield rejected(RejectResult(16, b'bad-txns-nonfinal'))
878 # This checks that a block with a bloated VARINT between the block_header and the array of tx such that
879 # the block is > MAX_BLOCK_BASE_SIZE with the bloated varint, but <= MAX_BLOCK_BASE_SIZE without the bloated varint,
880 # does not cause a subsequent, identical block with canonical encoding to be rejected. The test does not
881 # care whether the bloated block is accepted or rejected; it only cares that the second block is accepted.
883 # What matters is that the receiving node should not reject the bloated block, and then reject the canonical
884 # block on the basis that it's the same as an already-rejected block (which would be a consensus failure.)
886 # -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18)
888 # b64a (18)
889 # b64a is a bloated block (non-canonical varint)
890 # b64 is a good block (same as b64 but w/ canonical varint)
892 tip(60)
893 regular_block = block("64a", spend=out[18])
895 # make it a "broken_block," with non-canonical serialization
896 b64a = CBrokenBlock(regular_block)
897 b64a.initialize(regular_block)
898 self.blocks["64a"] = b64a
899 self.tip = b64a
900 tx = CTransaction()
902 # use canonical serialization to calculate size
903 script_length = MAX_BLOCK_BASE_SIZE - len(b64a.normal_serialize()) - 69
904 script_output = CScript([b'\x00' * script_length])
905 tx.vout.append(CTxOut(0, script_output))
906 tx.vin.append(CTxIn(COutPoint(b64a.vtx[1].sha256, 0)))
907 b64a = update_block("64a", [tx])
908 assert_equal(len(b64a.serialize()), MAX_BLOCK_BASE_SIZE + 8)
909 yield TestInstance([[self.tip, None]])
911 # comptool workaround: to make sure b64 is delivered, manually erase b64a from blockstore
912 self.test.block_store.erase(b64a.sha256)
914 tip(60)
915 b64 = CBlock(b64a)
916 b64.vtx = copy.deepcopy(b64a.vtx)
917 assert_equal(b64.hash, b64a.hash)
918 assert_equal(len(b64.serialize()), MAX_BLOCK_BASE_SIZE)
919 self.blocks[64] = b64
920 update_block(64, [])
921 yield accepted()
922 save_spendable_output()
924 # Spend an output created in the block itself
926 # -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19)
928 tip(64)
929 block(65)
930 tx1 = create_and_sign_tx(out[19].tx, out[19].n, out[19].tx.vout[0].nValue)
931 tx2 = create_and_sign_tx(tx1, 0, 0)
932 update_block(65, [tx1, tx2])
933 yield accepted()
934 save_spendable_output()
936 # Attempt to spend an output created later in the same block
938 # -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19)
939 # \-> b66 (20)
940 tip(65)
941 block(66)
942 tx1 = create_and_sign_tx(out[20].tx, out[20].n, out[20].tx.vout[0].nValue)
943 tx2 = create_and_sign_tx(tx1, 0, 1)
944 update_block(66, [tx2, tx1])
945 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
947 # Attempt to double-spend a transaction created in a block
949 # -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19)
950 # \-> b67 (20)
953 tip(65)
954 block(67)
955 tx1 = create_and_sign_tx(out[20].tx, out[20].n, out[20].tx.vout[0].nValue)
956 tx2 = create_and_sign_tx(tx1, 0, 1)
957 tx3 = create_and_sign_tx(tx1, 0, 2)
958 update_block(67, [tx1, tx2, tx3])
959 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
961 # More tests of block subsidy
963 # -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19) -> b69 (20)
964 # \-> b68 (20)
966 # b68 - coinbase with an extra 10 satoshis,
967 # creates a tx that has 9 satoshis from out[20] go to fees
968 # this fails because the coinbase is trying to claim 1 satoshi too much in fees
970 # b69 - coinbase with extra 10 satoshis, and a tx that gives a 10 satoshi fee
971 # this succeeds
973 tip(65)
974 block(68, additional_coinbase_value=10)
975 tx = create_and_sign_tx(out[20].tx, out[20].n, out[20].tx.vout[0].nValue-9)
976 update_block(68, [tx])
977 yield rejected(RejectResult(16, b'bad-cb-amount'))
979 tip(65)
980 b69 = block(69, additional_coinbase_value=10)
981 tx = create_and_sign_tx(out[20].tx, out[20].n, out[20].tx.vout[0].nValue-10)
982 update_block(69, [tx])
983 yield accepted()
984 save_spendable_output()
986 # Test spending the outpoint of a non-existent transaction
988 # -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19) -> b69 (20)
989 # \-> b70 (21)
991 tip(69)
992 block(70, spend=out[21])
993 bogus_tx = CTransaction()
994 bogus_tx.sha256 = uint256_from_str(b"23c70ed7c0506e9178fc1a987f40a33946d4ad4c962b5ae3a52546da53af0c5c")
995 tx = CTransaction()
996 tx.vin.append(CTxIn(COutPoint(bogus_tx.sha256, 0), b"", 0xffffffff))
997 tx.vout.append(CTxOut(1, b""))
998 update_block(70, [tx])
999 yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
1002 # Test accepting an invalid block which has the same hash as a valid one (via merkle tree tricks)
1004 # -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19) -> b69 (20) -> b72 (21)
1005 # \-> b71 (21)
1007 # b72 is a good block.
1008 # b71 is a copy of 72, but re-adds one of its transactions. However, it has the same hash as b71.
1010 tip(69)
1011 b72 = block(72)
1012 tx1 = create_and_sign_tx(out[21].tx, out[21].n, 2)
1013 tx2 = create_and_sign_tx(tx1, 0, 1)
1014 b72 = update_block(72, [tx1, tx2]) # now tip is 72
1015 b71 = copy.deepcopy(b72)
1016 b71.vtx.append(tx2) # add duplicate tx2
1017 self.block_heights[b71.sha256] = self.block_heights[b69.sha256] + 1 # b71 builds off b69
1018 self.blocks[71] = b71
1020 assert_equal(len(b71.vtx), 4)
1021 assert_equal(len(b72.vtx), 3)
1022 assert_equal(b72.sha256, b71.sha256)
1024 tip(71)
1025 yield rejected(RejectResult(16, b'bad-txns-duplicate'))
1026 tip(72)
1027 yield accepted()
1028 save_spendable_output()
1031 # Test some invalid scripts and MAX_BLOCK_SIGOPS
1033 # -> b55 (15) -> b57 (16) -> b60 (17) -> b64 (18) -> b65 (19) -> b69 (20) -> b72 (21)
1034 # \-> b** (22)
1037 # b73 - tx with excessive sigops that are placed after an excessively large script element.
1038 # The purpose of the test is to make sure those sigops are counted.
1040 # script is a bytearray of size 20,526
1042 # bytearray[0-19,998] : OP_CHECKSIG
1043 # bytearray[19,999] : OP_PUSHDATA4
1044 # bytearray[20,000-20,003]: 521 (max_script_element_size+1, in little-endian format)
1045 # bytearray[20,004-20,525]: unread data (script_element)
1046 # bytearray[20,526] : OP_CHECKSIG (this puts us over the limit)
1048 tip(72)
1049 b73 = block(73)
1050 size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 1 + 5 + 1
1051 a = bytearray([OP_CHECKSIG] * size)
1052 a[MAX_BLOCK_SIGOPS - 1] = int("4e",16) # OP_PUSHDATA4
1054 element_size = MAX_SCRIPT_ELEMENT_SIZE + 1
1055 a[MAX_BLOCK_SIGOPS] = element_size % 256
1056 a[MAX_BLOCK_SIGOPS+1] = element_size // 256
1057 a[MAX_BLOCK_SIGOPS+2] = 0
1058 a[MAX_BLOCK_SIGOPS+3] = 0
1060 tx = create_and_sign_tx(out[22].tx, 0, 1, CScript(a))
1061 b73 = update_block(73, [tx])
1062 assert_equal(get_legacy_sigopcount_block(b73), MAX_BLOCK_SIGOPS+1)
1063 yield rejected(RejectResult(16, b'bad-blk-sigops'))
1065 # b74/75 - if we push an invalid script element, all prevous sigops are counted,
1066 # but sigops after the element are not counted.
1068 # The invalid script element is that the push_data indicates that
1069 # there will be a large amount of data (0xffffff bytes), but we only
1070 # provide a much smaller number. These bytes are CHECKSIGS so they would
1071 # cause b75 to fail for excessive sigops, if those bytes were counted.
1073 # b74 fails because we put MAX_BLOCK_SIGOPS+1 before the element
1074 # b75 succeeds because we put MAX_BLOCK_SIGOPS before the element
1077 tip(72)
1078 b74 = block(74)
1079 size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 42 # total = 20,561
1080 a = bytearray([OP_CHECKSIG] * size)
1081 a[MAX_BLOCK_SIGOPS] = 0x4e
1082 a[MAX_BLOCK_SIGOPS+1] = 0xfe
1083 a[MAX_BLOCK_SIGOPS+2] = 0xff
1084 a[MAX_BLOCK_SIGOPS+3] = 0xff
1085 a[MAX_BLOCK_SIGOPS+4] = 0xff
1086 tx = create_and_sign_tx(out[22].tx, 0, 1, CScript(a))
1087 b74 = update_block(74, [tx])
1088 yield rejected(RejectResult(16, b'bad-blk-sigops'))
1090 tip(72)
1091 b75 = block(75)
1092 size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 42
1093 a = bytearray([OP_CHECKSIG] * size)
1094 a[MAX_BLOCK_SIGOPS-1] = 0x4e
1095 a[MAX_BLOCK_SIGOPS] = 0xff
1096 a[MAX_BLOCK_SIGOPS+1] = 0xff
1097 a[MAX_BLOCK_SIGOPS+2] = 0xff
1098 a[MAX_BLOCK_SIGOPS+3] = 0xff
1099 tx = create_and_sign_tx(out[22].tx, 0, 1, CScript(a))
1100 b75 = update_block(75, [tx])
1101 yield accepted()
1102 save_spendable_output()
1104 # Check that if we push an element filled with CHECKSIGs, they are not counted
1105 tip(75)
1106 b76 = block(76)
1107 size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 1 + 5
1108 a = bytearray([OP_CHECKSIG] * size)
1109 a[MAX_BLOCK_SIGOPS-1] = 0x4e # PUSHDATA4, but leave the following bytes as just checksigs
1110 tx = create_and_sign_tx(out[23].tx, 0, 1, CScript(a))
1111 b76 = update_block(76, [tx])
1112 yield accepted()
1113 save_spendable_output()
1115 # Test transaction resurrection
1117 # -> b77 (24) -> b78 (25) -> b79 (26)
1118 # \-> b80 (25) -> b81 (26) -> b82 (27)
1120 # b78 creates a tx, which is spent in b79. After b82, both should be in mempool
1122 # The tx'es must be unsigned and pass the node's mempool policy. It is unsigned for the
1123 # rather obscure reason that the Python signature code does not distinguish between
1124 # Low-S and High-S values (whereas the bitcoin code has custom code which does so);
1125 # as a result of which, the odds are 50% that the python code will use the right
1126 # value and the transaction will be accepted into the mempool. Until we modify the
1127 # test framework to support low-S signing, we are out of luck.
1129 # To get around this issue, we construct transactions which are not signed and which
1130 # spend to OP_TRUE. If the standard-ness rules change, this test would need to be
1131 # updated. (Perhaps to spend to a P2SH OP_TRUE script)
1133 tip(76)
1134 block(77)
1135 tx77 = create_and_sign_tx(out[24].tx, out[24].n, 10*COIN)
1136 update_block(77, [tx77])
1137 yield accepted()
1138 save_spendable_output()
1140 block(78)
1141 tx78 = create_tx(tx77, 0, 9*COIN)
1142 update_block(78, [tx78])
1143 yield accepted()
1145 block(79)
1146 tx79 = create_tx(tx78, 0, 8*COIN)
1147 update_block(79, [tx79])
1148 yield accepted()
1150 # mempool should be empty
1151 assert_equal(len(self.nodes[0].getrawmempool()), 0)
1153 tip(77)
1154 block(80, spend=out[25])
1155 yield rejected()
1156 save_spendable_output()
1158 block(81, spend=out[26])
1159 yield rejected() # other chain is same length
1160 save_spendable_output()
1162 block(82, spend=out[27])
1163 yield accepted() # now this chain is longer, triggers re-org
1164 save_spendable_output()
1166 # now check that tx78 and tx79 have been put back into the peer's mempool
1167 mempool = self.nodes[0].getrawmempool()
1168 assert_equal(len(mempool), 2)
1169 assert(tx78.hash in mempool)
1170 assert(tx79.hash in mempool)
1173 # Test invalid opcodes in dead execution paths.
1175 # -> b81 (26) -> b82 (27) -> b83 (28)
1177 block(83)
1178 op_codes = [OP_IF, OP_INVALIDOPCODE, OP_ELSE, OP_TRUE, OP_ENDIF]
1179 script = CScript(op_codes)
1180 tx1 = create_and_sign_tx(out[28].tx, out[28].n, out[28].tx.vout[0].nValue, script)
1182 tx2 = create_and_sign_tx(tx1, 0, 0, CScript([OP_TRUE]))
1183 tx2.vin[0].scriptSig = CScript([OP_FALSE])
1184 tx2.rehash()
1186 update_block(83, [tx1, tx2])
1187 yield accepted()
1188 save_spendable_output()
1191 # Reorg on/off blocks that have OP_RETURN in them (and try to spend them)
1193 # -> b81 (26) -> b82 (27) -> b83 (28) -> b84 (29) -> b87 (30) -> b88 (31)
1194 # \-> b85 (29) -> b86 (30) \-> b89a (32)
1197 block(84)
1198 tx1 = create_tx(out[29].tx, out[29].n, 0, CScript([OP_RETURN]))
1199 tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
1200 tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
1201 tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
1202 tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
1203 tx1.calc_sha256()
1204 self.sign_tx(tx1, out[29].tx, out[29].n)
1205 tx1.rehash()
1206 tx2 = create_tx(tx1, 1, 0, CScript([OP_RETURN]))
1207 tx2.vout.append(CTxOut(0, CScript([OP_RETURN])))
1208 tx3 = create_tx(tx1, 2, 0, CScript([OP_RETURN]))
1209 tx3.vout.append(CTxOut(0, CScript([OP_TRUE])))
1210 tx4 = create_tx(tx1, 3, 0, CScript([OP_TRUE]))
1211 tx4.vout.append(CTxOut(0, CScript([OP_RETURN])))
1212 tx5 = create_tx(tx1, 4, 0, CScript([OP_RETURN]))
1214 update_block(84, [tx1,tx2,tx3,tx4,tx5])
1215 yield accepted()
1216 save_spendable_output()
1218 tip(83)
1219 block(85, spend=out[29])
1220 yield rejected()
1222 block(86, spend=out[30])
1223 yield accepted()
1225 tip(84)
1226 block(87, spend=out[30])
1227 yield rejected()
1228 save_spendable_output()
1230 block(88, spend=out[31])
1231 yield accepted()
1232 save_spendable_output()
1234 # trying to spend the OP_RETURN output is rejected
1235 block("89a", spend=out[32])
1236 tx = create_tx(tx1, 0, 0, CScript([OP_TRUE]))
1237 update_block("89a", [tx])
1238 yield rejected()
1241 # Test re-org of a week's worth of blocks (1088 blocks)
1242 # This test takes a minute or two and can be accomplished in memory
1244 if self.options.runbarelyexpensive:
1245 tip(88)
1246 LARGE_REORG_SIZE = 1088
1247 test1 = TestInstance(sync_every_block=False)
1248 spend=out[32]
1249 for i in range(89, LARGE_REORG_SIZE + 89):
1250 b = block(i, spend)
1251 tx = CTransaction()
1252 script_length = MAX_BLOCK_BASE_SIZE - len(b.serialize()) - 69
1253 script_output = CScript([b'\x00' * script_length])
1254 tx.vout.append(CTxOut(0, script_output))
1255 tx.vin.append(CTxIn(COutPoint(b.vtx[1].sha256, 0)))
1256 b = update_block(i, [tx])
1257 assert_equal(len(b.serialize()), MAX_BLOCK_BASE_SIZE)
1258 test1.blocks_and_transactions.append([self.tip, True])
1259 save_spendable_output()
1260 spend = get_spendable_output()
1262 yield test1
1263 chain1_tip = i
1265 # now create alt chain of same length
1266 tip(88)
1267 test2 = TestInstance(sync_every_block=False)
1268 for i in range(89, LARGE_REORG_SIZE + 89):
1269 block("alt"+str(i))
1270 test2.blocks_and_transactions.append([self.tip, False])
1271 yield test2
1273 # extend alt chain to trigger re-org
1274 block("alt" + str(chain1_tip + 1))
1275 yield accepted()
1277 # ... and re-org back to the first chain
1278 tip(chain1_tip)
1279 block(chain1_tip + 1)
1280 yield rejected()
1281 block(chain1_tip + 2)
1282 yield accepted()
1284 chain1_tip += 2
1288 if __name__ == '__main__':
1289 FullBlockTest().main()