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.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-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 longpolling with getblocktemplate."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. import threading
  9. class LongpollThread(threading.Thread):
  10. def __init__(self, node):
  11. threading.Thread.__init__(self)
  12. # query current longpollid
  13. templat = node.getblocktemplate()
  14. self.longpollid = templat['longpollid']
  15. # create a new connection to the node, we can't use the same
  16. # connection from two threads
  17. self.node = get_rpc_proxy(node.url, 1, timeout=600, coveragedir=node.coverage_dir)
  18. def run(self):
  19. self.node.getblocktemplate({'longpollid':self.longpollid})
  20. class GetBlockTemplateLPTest(BitcoinTestFramework):
  21. def set_test_params(self):
  22. self.num_nodes = 2
  23. def run_test(self):
  24. self.log.info("Warning: this test will take about 70 seconds in the best case. Be patient.")
  25. self.nodes[0].generate(10)
  26. templat = self.nodes[0].getblocktemplate()
  27. longpollid = templat['longpollid']
  28. # longpollid should not change between successive invocations if nothing else happens
  29. templat2 = self.nodes[0].getblocktemplate()
  30. assert(templat2['longpollid'] == longpollid)
  31. # Test 1: test that the longpolling wait if we do nothing
  32. thr = LongpollThread(self.nodes[0])
  33. thr.start()
  34. # check that thread still lives
  35. thr.join(5) # wait 5 seconds or until thread exits
  36. assert(thr.is_alive())
  37. # Test 2: test that longpoll will terminate if another node generates a block
  38. self.nodes[1].generate(1) # generate a block on another node
  39. # check that thread will exit now that new transaction entered mempool
  40. thr.join(5) # wait 5 seconds or until thread exits
  41. assert(not thr.is_alive())
  42. # Test 3: test that longpoll will terminate if we generate a block ourselves
  43. thr = LongpollThread(self.nodes[0])
  44. thr.start()
  45. self.nodes[0].generate(1) # generate a block on another node
  46. thr.join(5) # wait 5 seconds or until thread exits
  47. assert(not thr.is_alive())
  48. # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
  49. thr = LongpollThread(self.nodes[0])
  50. thr.start()
  51. # generate a random transaction and submit it
  52. min_relay_fee = self.nodes[0].getnetworkinfo()["relayfee"]
  53. # min_relay_fee is fee per 1000 bytes, which should be more than enough.
  54. (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"), min_relay_fee, Decimal("0.001"), 20)
  55. # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
  56. thr.join(60 + 20)
  57. assert(not thr.is_alive())
  58. if __name__ == '__main__':
  59. GetBlockTemplateLPTest().main()