Dash Core  0.12.2.1
P2P Digital Currency
getblocktemplate_longpoll.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 
6 from test_framework.test_framework import BitcoinTestFramework
7 from test_framework.util import *
8 
9 import threading
10 
11 class LongpollThread(threading.Thread):
12  def __init__(self, node):
13  threading.Thread.__init__(self)
14  # query current longpollid
15  templat = node.getblocktemplate()
16  self.longpollid = templat['longpollid']
17  # create a new connection to the node, we can't use the same
18  # connection from two threads
19  self.node = get_rpc_proxy(node.url, 1, timeout=600)
20 
21  def run(self):
22  self.node.getblocktemplate({'longpollid':self.longpollid})
23 
25  '''
26  Test longpolling with getblocktemplate.
27  '''
28 
29  def run_test(self):
30  print "Warning: this test will take about 70 seconds in the best case. Be patient."
31  wait_to_sync(self.nodes[0])
32  self.nodes[0].generate(10)
33  templat = self.nodes[0].getblocktemplate()
34  longpollid = templat['longpollid']
35  # longpollid should not change between successive invocations if nothing else happens
36  templat2 = self.nodes[0].getblocktemplate()
37  assert(templat2['longpollid'] == longpollid)
38 
39  # Test 1: test that the longpolling wait if we do nothing
40  thr = LongpollThread(self.nodes[0])
41  thr.start()
42  # check that thread still lives
43  thr.join(5) # wait 5 seconds or until thread exits
44  assert(thr.is_alive())
45 
46  # Test 2: test that longpoll will terminate if another node generates a block
47  self.nodes[1].generate(1) # generate a block on another node
48  # check that thread will exit now that new transaction entered mempool
49  thr.join(5) # wait 5 seconds or until thread exits
50  assert(not thr.is_alive())
51 
52  # Test 3: test that longpoll will terminate if we generate a block ourselves
53  thr = LongpollThread(self.nodes[0])
54  thr.start()
55  self.nodes[0].generate(1) # generate a block on another node
56  thr.join(5) # wait 5 seconds or until thread exits
57  assert(not thr.is_alive())
58 
59  # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
60  thr = LongpollThread(self.nodes[0])
61  thr.start()
62  # generate a random transaction and submit it
63  (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"), Decimal("0.0"), Decimal("0.001"), 20)
64  # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
65  thr.join(60 + 20)
66  assert(not thr.is_alive())
67 
68 if __name__ == '__main__':
70 
def get_rpc_proxy(url, node_number, timeout=None)
Definition: util.py:58
def random_transaction(nodes, amount, min_fee, fee_increment, fee_variants)
Definition: util.py:442
UniValue getblocktemplate(const UniValue &params, bool fHelp)
Definition: mining.cpp:337
def wait_to_sync(node)
Definition: util.py:87
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122