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.

invalidtxrequest.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 node responses to invalid transactions.
  6. In this test we connect to one node over p2p, and test tx requests.
  7. """
  8. from test_framework.test_framework import ComparisonTestFramework
  9. from test_framework.comptool import TestManager, TestInstance, RejectResult
  10. from test_framework.blocktools import *
  11. import time
  12. # Use the ComparisonTestFramework with 1 node: only use --testbinary.
  13. class InvalidTxRequestTest(ComparisonTestFramework):
  14. ''' Can either run this test as 1 node with expected answers, or two and compare them.
  15. Change the "outcome" variable from each TestInstance object to only do the comparison. '''
  16. def __init__(self):
  17. super().__init__()
  18. self.num_nodes = 1
  19. def run_test(self):
  20. test = TestManager(self, self.options.tmpdir)
  21. test.add_all_connections(self.nodes)
  22. self.tip = None
  23. self.block_time = None
  24. NetworkThread().start() # Start up network handling in another thread
  25. test.run()
  26. def get_tests(self):
  27. if self.tip is None:
  28. self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
  29. self.block_time = int(time.time())+1
  30. '''
  31. Create a new block with an anyone-can-spend coinbase
  32. '''
  33. height = 1
  34. block = create_block(self.tip, create_coinbase(height), self.block_time)
  35. self.block_time += 1
  36. block.solve()
  37. # Save the coinbase for later
  38. self.block1 = block
  39. self.tip = block.sha256
  40. height += 1
  41. yield TestInstance([[block, True]])
  42. '''
  43. Now we need that block to mature so we can spend the coinbase.
  44. '''
  45. test = TestInstance(sync_every_block=False)
  46. for i in range(100):
  47. block = create_block(self.tip, create_coinbase(height), self.block_time)
  48. block.solve()
  49. self.tip = block.sha256
  50. self.block_time += 1
  51. test.blocks_and_transactions.append([block, True])
  52. height += 1
  53. yield test
  54. # b'\x64' is OP_NOTIF
  55. # Transaction will be rejected with code 16 (REJECT_INVALID)
  56. tx1 = create_transaction(self.block1.vtx[0], 0, b'\x64', 50 * COIN - 12000)
  57. yield TestInstance([[tx1, RejectResult(16, b'mandatory-script-verify-flag-failed')]])
  58. # TODO: test further transactions...
  59. if __name__ == '__main__':
  60. InvalidTxRequestTest().main()