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.

getblocktemplate_longpoll.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014 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. from test_framework.test_framework import BitcoinTestFramework
  6. from test_framework.util import *
  7. def check_array_result(object_array, to_match, expected):
  8. """
  9. Pass in array of JSON objects, a dictionary with key/value pairs
  10. to match against, and another dictionary with expected key/value
  11. pairs.
  12. """
  13. num_matched = 0
  14. for item in object_array:
  15. all_match = True
  16. for key,value in to_match.items():
  17. if item[key] != value:
  18. all_match = False
  19. if not all_match:
  20. continue
  21. for key,value in expected.items():
  22. if item[key] != value:
  23. raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
  24. num_matched = num_matched+1
  25. if num_matched == 0:
  26. raise AssertionError("No objects matched %s"%(str(to_match)))
  27. import threading
  28. class LongpollThread(threading.Thread):
  29. def __init__(self, node):
  30. threading.Thread.__init__(self)
  31. # query current longpollid
  32. templat = node.getblocktemplate()
  33. self.longpollid = templat['longpollid']
  34. # create a new connection to the node, we can't use the same
  35. # connection from two threads
  36. self.node = get_rpc_proxy(node.url, 1, timeout=600)
  37. def run(self):
  38. self.node.getblocktemplate({'longpollid':self.longpollid})
  39. class GetBlockTemplateLPTest(BitcoinTestFramework):
  40. '''
  41. Test longpolling with getblocktemplate.
  42. '''
  43. def run_test(self):
  44. print "Warning: this test will take about 70 seconds in the best case. Be patient."
  45. self.nodes[0].generate(10)
  46. templat = self.nodes[0].getblocktemplate()
  47. longpollid = templat['longpollid']
  48. # longpollid should not change between successive invocations if nothing else happens
  49. templat2 = self.nodes[0].getblocktemplate()
  50. assert(templat2['longpollid'] == longpollid)
  51. # Test 1: test that the longpolling wait if we do nothing
  52. thr = LongpollThread(self.nodes[0])
  53. thr.start()
  54. # check that thread still lives
  55. thr.join(5) # wait 5 seconds or until thread exits
  56. assert(thr.is_alive())
  57. # Test 2: test that longpoll will terminate if another node generates a block
  58. self.nodes[1].generate(1) # generate a block on another node
  59. # check that thread will exit now that new transaction entered mempool
  60. thr.join(5) # wait 5 seconds or until thread exits
  61. assert(not thr.is_alive())
  62. # Test 3: test that longpoll will terminate if we generate a block ourselves
  63. thr = LongpollThread(self.nodes[0])
  64. thr.start()
  65. self.nodes[0].generate(1) # generate a block on another node
  66. thr.join(5) # wait 5 seconds or until thread exits
  67. assert(not thr.is_alive())
  68. # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
  69. thr = LongpollThread(self.nodes[0])
  70. thr.start()
  71. # generate a random transaction and submit it
  72. (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"), Decimal("0.0"), Decimal("0.001"), 20)
  73. # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
  74. thr.join(60 + 20)
  75. assert(not thr.is_alive())
  76. if __name__ == '__main__':
  77. GetBlockTemplateLPTest().main()