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

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