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