[CoinControl] Allow non-wallet owned change addresses
[bitcoinplatinum.git] / qa / rpc-tests / getblocktemplate_longpoll.py
blob3cddf4046aaf6eed75634c4ef2161a0a607ebc09
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.
6 from test_framework.test_framework import BitcoinTestFramework
7 from test_framework.util import *
9 import threading
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)
21 def run(self):
22 self.node.getblocktemplate({'longpollid':self.longpollid})
24 class GetBlockTemplateLPTest(BitcoinTestFramework):
25 '''
26 Test longpolling with getblocktemplate.
27 '''
29 def __init__(self):
30 super().__init__()
31 self.num_nodes = 4
32 self.setup_clean_chain = False
34 def run_test(self):
35 print("Warning: this test will take about 70 seconds in the best case. Be patient.")
36 self.nodes[0].generate(10)
37 templat = self.nodes[0].getblocktemplate()
38 longpollid = templat['longpollid']
39 # longpollid should not change between successive invocations if nothing else happens
40 templat2 = self.nodes[0].getblocktemplate()
41 assert(templat2['longpollid'] == longpollid)
43 # Test 1: test that the longpolling wait if we do nothing
44 thr = LongpollThread(self.nodes[0])
45 thr.start()
46 # check that thread still lives
47 thr.join(5) # wait 5 seconds or until thread exits
48 assert(thr.is_alive())
50 # Test 2: test that longpoll will terminate if another node generates a block
51 self.nodes[1].generate(1) # generate a block on another node
52 # check that thread will exit now that new transaction entered mempool
53 thr.join(5) # wait 5 seconds or until thread exits
54 assert(not thr.is_alive())
56 # Test 3: test that longpoll will terminate if we generate a block ourselves
57 thr = LongpollThread(self.nodes[0])
58 thr.start()
59 self.nodes[0].generate(1) # generate a block on another node
60 thr.join(5) # wait 5 seconds or until thread exits
61 assert(not thr.is_alive())
63 # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
64 thr = LongpollThread(self.nodes[0])
65 thr.start()
66 # generate a random transaction and submit it
67 (txid, txhex, fee) = random_transaction(self.nodes, Decimal("1.1"), Decimal("0.0"), Decimal("0.001"), 20)
68 # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
69 thr.join(60 + 20)
70 assert(not thr.is_alive())
72 if __name__ == '__main__':
73 GetBlockTemplateLPTest().main()