1 #!/usr/bin/env python3
2 # Copyright (c) 2014-present 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 7 import random
8 import threading
9 10 from test_framework.blocktools import NORMAL_GBT_REQUEST_PARAMS
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import assert_equal
13 from test_framework.wallet import MiniWallet
14 15 16 class LongpollThread(threading.Thread):
17 def __init__(self, node):
18 threading.Thread.__init__(self)
19 # query current longpollid
20 template = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
21 self.longpollid = template['longpollid']
22 # create a new connection to the node for this thread
23 self.node = node.create_new_rpc_connection(client_timeout=600)
24 25 def run(self):
26 self.node.getblocktemplate({'longpollid': self.longpollid, **NORMAL_GBT_REQUEST_PARAMS})
27 28 class GetBlockTemplateLPTest(BitcoinTestFramework):
29 def set_test_params(self):
30 self.num_nodes = 2
31 32 def run_test(self):
33 self.log.info("Warning: this test will take about 70 seconds in the best case. Be patient.")
34 self.log.info("Test that longpollid doesn't change between successive getblocktemplate() invocations if nothing else happens")
35 self.generate(self.nodes[0], 10)
36 template = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
37 longpollid = template['longpollid']
38 template2 = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
39 assert_equal(template2['longpollid'], longpollid)
40 41 self.log.info("Test that longpoll waits if we do nothing")
42 thr = LongpollThread(self.nodes[0])
43 with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):
44 thr.start()
45 # check that thread still lives
46 thr.join(5) # wait 5 seconds or until thread exits
47 assert thr.is_alive()
48 49 self.miniwallet = MiniWallet(self.nodes[0])
50 self.log.info("Test that longpoll will terminate if another node generates a block")
51 self.generate(self.nodes[1], 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()
55 56 self.log.info("Test that longpoll will terminate if we generate a block ourselves")
57 thr = LongpollThread(self.nodes[0])
58 with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):
59 thr.start()
60 self.generate(self.nodes[0], 1) # generate a block on own node
61 thr.join(5) # wait 5 seconds or until thread exits
62 assert not thr.is_alive()
63 64 self.log.info("Test that introducing a new transaction into the mempool will terminate the longpoll")
65 thr = LongpollThread(self.nodes[0])
66 with self.nodes[0].assert_debug_log(["ThreadRPCServer method=getblocktemplate"], timeout=3):
67 thr.start()
68 # generate a transaction and submit it
69 self.miniwallet.send_self_transfer(from_node=random.choice(self.nodes))
70 # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
71 thr.join(60 + 20)
72 assert not thr.is_alive()
73 74 if __name__ == '__main__':
75 GetBlockTemplateLPTest(__file__).main()
76