2 # Copyright (c) 2014-2017 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."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
12 class LongpollThread(threading
.Thread
):
13 def __init__(self
, node
):
14 threading
.Thread
.__init
__(self
)
15 # query current longpollid
16 templat
= node
.getblocktemplate()
17 self
.longpollid
= templat
['longpollid']
18 # create a new connection to the node, we can't use the same
19 # connection from two threads
20 self
.node
= get_rpc_proxy(node
.url
, 1, timeout
=600, coveragedir
=node
.coverage_dir
)
23 self
.node
.getblocktemplate({'longpollid':self
.longpollid
})
25 class GetBlockTemplateLPTest(BitcoinTestFramework
):
26 def set_test_params(self
):
30 self
.log
.info("Warning: this test will take about 70 seconds in the best case. Be patient.")
31 self
.nodes
[0].generate(10)
32 templat
= self
.nodes
[0].getblocktemplate()
33 longpollid
= templat
['longpollid']
34 # longpollid should not change between successive invocations if nothing else happens
35 templat2
= self
.nodes
[0].getblocktemplate()
36 assert(templat2
['longpollid'] == longpollid
)
38 # Test 1: test that the longpolling wait if we do nothing
39 thr
= LongpollThread(self
.nodes
[0])
41 # check that thread still lives
42 thr
.join(5) # wait 5 seconds or until thread exits
43 assert(thr
.is_alive())
45 # Test 2: test that longpoll will terminate if another node generates a block
46 self
.nodes
[1].generate(1) # generate a block on another node
47 # check that thread will exit now that new transaction entered mempool
48 thr
.join(5) # wait 5 seconds or until thread exits
49 assert(not thr
.is_alive())
51 # Test 3: test that longpoll will terminate if we generate a block ourselves
52 thr
= LongpollThread(self
.nodes
[0])
54 self
.nodes
[0].generate(1) # generate a block on another node
55 thr
.join(5) # wait 5 seconds or until thread exits
56 assert(not thr
.is_alive())
58 # Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
59 thr
= LongpollThread(self
.nodes
[0])
61 # generate a random transaction and submit it
62 min_relay_fee
= self
.nodes
[0].getnetworkinfo()["relayfee"]
63 # min_relay_fee is fee per 1000 bytes, which should be more than enough.
64 (txid
, txhex
, fee
) = random_transaction(self
.nodes
, Decimal("1.1"), min_relay_fee
, Decimal("0.001"), 20)
65 # after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
67 assert(not thr
.is_alive())
69 if __name__
== '__main__':
70 GetBlockTemplateLPTest().main()