Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

getblocktemplate_longpoll.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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)
  18. def run(self):
  19. self.node.getblocktemplate({'longpollid':self.longpollid})
  20. class GetBlockTemplateLPTest(BitcoinTestFramework):
  21. def __init__(self):
  22. super().__init__()
  23. self.num_nodes = 4
  24. self.setup_clean_chain = False
  25. def run_test(self):
  26. self.log.info("Warning: this test will take about 70 seconds in the best case. Be patient.")
  27. self.nodes[0].generate(10)
  28. templat = self.nodes[0].getblocktemplate()
  29. longpollid = templat['longpollid']
  30. # longpollid should not change between successive invocations if nothing else happens
  31. templat2 = self.nodes[0].getblocktemplate()
  32. assert(templat2['longpollid'] == longpollid)
  33. # Test 1: test that the longpolling wait if we do nothing
  34. thr = LongpollThread(self.nodes[0])
  35. thr.start()
  36. # check that thread still lives
  37. thr.join(5) # wait 5 seconds or until thread exits
  38. assert(thr.is_alive())
  39. # Test 2: test that longpoll will terminate if another node generates a block
  40. self.nodes[1].generate(1) # generate a block on another node
  41. # check that thread will exit now that new transaction entered mempool
  42. thr.join(5) # wait 5 seconds or until thread exits
  43. assert(not thr.is_alive())
  44. # Test 3: test that longpoll will terminate if we generate a block ourselves
  45. thr = LongpollThread(self.nodes[0])
  46. thr.start()
  47. self.nodes[0].generate(1) # generate a block on another node
  48. thr.join(5) # wait 5 seconds or until thread exits
  49. assert(not thr.is_alive())
  50. # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
  51. thr = LongpollThread(self.nodes[0])
  52. thr.start()
  53. # generate a random transaction and submit it
  54. min_relay_fee = self.nodes[0].getnetworkinfo()["relayfee"]
  55. # min_relay_fee is fee per 1000 bytes, which should be more than enough.
  56. (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"), min_relay_fee, Decimal("0.001"), 20)
  57. # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
  58. thr.join(60 + 20)
  59. assert(not thr.is_alive())
  60. if __name__ == '__main__':
  61. GetBlockTemplateLPTest().main()