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
10 We use the testing framework in which we expect a particular answer from
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 *
19 from test_framework
.key
import CECKey
20 from test_framework
.script
import *
23 class PreviousSpendableOutput(object):
24 def __init__(self
, tx
= CTransaction(), n
= -1):
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()
40 r
+= super(CBlock
, self
).serialize()
41 r
+= struct
.pack("<BQ", 255, len(self
.vtx
))
46 def normal_serialize(self
):
48 r
+= super(CBrokenBlock
, self
).serialize()
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
):
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()
64 def add_options(self
, parser
):
65 super().add_options(parser
)
66 parser
.add_option("--runbarelyexpensive", dest
="runbarelyexpensive", default
=True)
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
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
)
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()
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
)
99 def next_block(self
, number
, spend
=None, additional_coinbase_value
=0, script
=CScript([OP_TRUE
]), solve
=True):
101 base_block_hash
= self
.genesis_hash
102 block_time
= int(time
.time())+1
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
112 block
= create_block(base_block_hash
, coinbase
, block_time
)
114 coinbase
.vout
[0].nValue
+= spend
.tx
.vout
[spend
.n
].nValue
- 1 # all but one satoshi to fees
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()
124 self
.block_heights
[block
.sha256
] = height
125 assert number
not in self
.blocks
126 self
.blocks
[number
] = block
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
144 return TestInstance([[self
.tip
, True]])
146 # returns a test case that asserts that the current tip was rejected
147 def rejected(reject
= None):
149 return TestInstance([[self
.tip
, False]])
151 return TestInstance([[self
.tip
, reject
]])
153 # move the tip back to a previous block
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()
164 # Update the internal state just like in next_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
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
183 save_spendable_output()
187 # Now we need that block to mature so we can spend the coinbase.
188 test
= TestInstance(sync_every_block
=False)
191 test
.blocks_and_transactions
.append([self
.tip
, True])
192 save_spendable_output()
195 # collect spendable outputs now to avoid cluttering the code later on
198 out
.append(get_spendable_output())
200 # Start by building a couple of blocks on top (which output is spent is
202 # genesis -> b1 (0) -> b2 (1)
203 block(1, spend
=out
[0])
204 save_spendable_output()
207 block(2, spend
=out
[1])
209 save_spendable_output()
213 # genesis -> b1 (0) -> b2 (1)
216 # Nothing should happen at this point. We saw b2 first so it takes priority.
218 b3
= block(3, spend
=out
[1])
219 txout_b3
= PreviousSpendableOutput(b3
.vtx
[1], 0)
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])
231 # ... and back to the first chain.
232 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
233 # \-> b3 (1) -> b4 (2)
235 block(5, spend
=out
[2])
236 save_spendable_output()
239 block(6, spend
=out
[3])
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)
247 block(7, spend
=out
[2])
250 block(8, spend
=out
[4])
253 # Try to create a block that has too much fee
254 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
256 # \-> b3 (1) -> b4 (2)
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)
266 block(10, spend
=out
[3])
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)
277 # \-> b3 (1) -> b4 (2)
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)
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))
302 block(15, spend
=out
[5], script
=lots_of_checksigs
)
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)
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)
327 block(18, spend
=txout_b3
)
330 block(19, spend
=out
[6])
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)
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)
347 block(21, spend
=out
[6])
350 block(22, spend
=out
[5])
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)
359 b23
= block(23, spend
=out
[6])
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
)
369 save_spendable_output()
371 # Make the next block one byte bigger and check that it fails
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])
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)
390 b26
= block(26, spend
=out
[6])
391 b26
.vtx
[0].vin
[0].scriptSig
= b
'\x00'
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 b27
= block(27, spend
=out
[7])
400 yield rejected(False)
402 # Now try a too-large-coinbase script
404 b28
= block(28, spend
=out
[6])
405 b28
.vtx
[0].vin
[0].scriptSig
= b
'\x00' * 101
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 b29
= block(29, spend
=out
[7])
412 yield rejected(False)
414 # b30 has a max-sized coinbase scriptSig.
417 b30
.vtx
[0].vin
[0].scriptSig
= b
'\x00' * 100
419 b30
= update_block(30, [])
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)
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
)
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
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
)
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'))
459 lots_of_checksigs
= CScript([OP_CHECKSIGVERIFY
] * (MAX_BLOCK_SIGOPS
- 1))
460 b35
= block(35, spend
=out
[10], script
=lots_of_checksigs
)
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
472 # b12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10)
477 # save 37's spendable output, but then double-spend out11 to invalidate the block
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
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)
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
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
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
)
518 b39
= update_block(39, [tx
])
521 # Until block is full, add tx's with 1 satoshi to p2sh_script, the rest to OP_TRUE
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
])))
529 total_size
+= len(tx_new
.serialize())
530 if total_size
>= MAX_BLOCK_BASE_SIZE
:
532 b39
.vtx
.append(tx_new
) # add tx to block
536 b39
= update_block(39, [])
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.
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)
556 for i
in range(1, numTxes
+1):
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
570 lastOutpoint
= COutPoint(tx
.sha256
, 0)
572 b40_sigops_to_fill
= MAX_BLOCK_SIGOPS
- (numTxes
* b39_sigops_per_output
+ sigops
) + 1
574 tx
.vin
.append(CTxIn(lastOutpoint
, b
''))
575 tx
.vout
.append(CTxOut(1, CScript([OP_CHECKSIG
] * b40_sigops_to_fill
)))
578 update_block(40, new_txs
)
579 yield rejected(RejectResult(16, b
'bad-blk-sigops'))
581 # same as b40, but one less sigop
583 b41
= block(41, spend
=None)
584 update_block(41, b40
.vtx
[1:-1])
585 b41_sigops_to_fill
= b40_sigops_to_fill
- 1
587 tx
.vin
.append(CTxIn(lastOutpoint
, b
''))
588 tx
.vout
.append(CTxOut(1, CScript([OP_CHECKSIG
] * b41_sigops_to_fill
)))
590 update_block(41, [tx
])
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)
599 block(42, spend
=out
[12])
601 save_spendable_output()
603 block(43, spend
=out
[13])
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)
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
)
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()
625 self
.block_heights
[b44
.sha256
] = height
626 self
.blocks
[44] = b44
629 # A block with a non-coinbase as the first tx
630 non_coinbase
= create_tx(out
[15].tx
, out
[15].n
, 1)
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()
639 self
.block_heights
[b45
.sha256
] = self
.block_heights
[self
.tip
.sha256
]+1
641 self
.blocks
[45] = b45
642 yield rejected(RejectResult(16, b
'bad-cb-missing'))
644 # A block with no txns
647 b46
.nTime
= b44
.nTime
+1
648 b46
.hashPrevBlock
= b44
.sha256
649 b46
.nBits
= 0x207fffff
651 b46
.hashMerkleRoot
= 0
653 self
.block_heights
[b46
.sha256
] = self
.block_heights
[b44
.sha256
]+1
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
662 b47
= block(47, solve
=False)
663 target
= uint256_from_compact(b47
.nBits
)
664 while b47
.sha256
< target
: #changed > to <
667 yield rejected(RejectResult(16, b
'high-hash'))
669 # A block with timestamp > 2 hrs in the future
671 b48
= block(48, solve
=False)
672 b48
.nTime
= int(time
.time()) + 60 * 60 * 3
674 yield rejected(RejectResult(16, b
'time-too-new'))
676 # A block with an invalid merkle hash
679 b49
.hashMerkleRoot
+= 1
681 yield rejected(RejectResult(16, b
'bad-txnmrklroot'))
683 # A block with an incorrect POW limit
686 b50
.nBits
= b50
.nBits
- 1
688 yield rejected(RejectResult(16, b
'bad-diffbits'))
690 # A block with two coinbase txns
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
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)
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
718 yield rejected(RejectResult(16, b
'time-too-old'))
722 b55
= block(55, spend
=out
[15])
723 b55
.nTime
= b35
.nTime
726 save_spendable_output()
731 # -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57p2 (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.
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.
747 # b57p2 has six transactions in its merkle tree:
748 # - coinbase, tx, tx1, tx2, tx3, tx4
749 # Merkle root calculation will duplicate as necessary.
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
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
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
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
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'))
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)
804 # tx with prevout.n out of range
806 b58
= block(58, spend
=out
[17])
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
""))
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
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
824 b60
= block(60, spend
=out
[17])
826 save_spendable_output()
830 # -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57 (16) -> b60 (17)
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.
838 b61
= block(61, spend
=out
[18])
839 b61
.vtx
[0].vin
[0].scriptSig
= b60
.vtx
[0].vin
[0].scriptSig
#equalize the coinbases
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)
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)
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)
871 b63
.vtx
[0].nLockTime
= 0xffffffff
872 b63
.vtx
[0].vin
[0].nSequence
= 0xDEADBEEF
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)
889 # b64a is a bloated block (non-canonical varint)
890 # b64 is a good block (same as b64 but w/ canonical varint)
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
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
)
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
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)
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
])
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)
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)
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)
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
974 b68
= 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'))
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
])
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)
992 block(70, spend
=out
[21])
993 bogus_tx
= CTransaction()
994 bogus_tx
.sha256
= uint256_from_str(b
"23c70ed7c0506e9178fc1a987f40a33946d4ad4c962b5ae3a52546da53af0c5c")
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)
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.
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
)
1025 yield rejected(RejectResult(16, b
'bad-txns-duplicate'))
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)
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)
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
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'))
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
])
1102 save_spendable_output()
1104 # Check that if we push an element filled with CHECKSIGs, they are not counted
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
])
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)
1135 tx77
= create_and_sign_tx(out
[24].tx
, out
[24].n
, 10*COIN
)
1136 update_block(77, [tx77
])
1138 save_spendable_output()
1141 tx78
= create_tx(tx77
, 0, 9*COIN
)
1142 update_block(78, [tx78
])
1146 tx79
= create_tx(tx78
, 0, 8*COIN
)
1147 update_block(79, [tx79
])
1150 # mempool should be empty
1151 assert_equal(len(self
.nodes
[0].getrawmempool()), 0)
1154 block(80, spend
=out
[25])
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)
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
])
1186 update_block(83, [tx1
, tx2
])
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)
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
])))
1204 self
.sign_tx(tx1
, out
[29].tx
, out
[29].n
)
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
])
1216 save_spendable_output()
1219 block(85, spend
=out
[29])
1222 block(86, spend
=out
[30])
1226 block(87, spend
=out
[30])
1228 save_spendable_output()
1230 block(88, spend
=out
[31])
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
])
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
:
1246 LARGE_REORG_SIZE
= 1088
1247 test1
= TestInstance(sync_every_block
=False)
1249 for i
in range(89, LARGE_REORG_SIZE
+ 89):
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()
1265 # now create alt chain of same length
1267 test2
= TestInstance(sync_every_block
=False)
1268 for i
in range(89, LARGE_REORG_SIZE
+ 89):
1270 test2
.blocks_and_transactions
.append([self
.tip
, False])
1273 # extend alt chain to trigger re-org
1274 block("alt" + str(chain1_tip
+ 1))
1277 # ... and re-org back to the first chain
1279 block(chain1_tip
+ 1)
1281 block(chain1_tip
+ 2)
1288 if __name__
== '__main__':
1289 FullBlockTest().main()