You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p2p-fullblocktest.py 51KB

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