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 *
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)
22 self
.node
.getblocktemplate({'longpollid':self
.longpollid
})
24 class GetBlockTemplateLPTest(BitcoinTestFramework
):
26 Test longpolling with getblocktemplate.
32 self
.setup_clean_chain
= False
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])
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])
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])
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
70 assert(not thr
.is_alive())
72 if __name__
== '__main__':
73 GetBlockTemplateLPTest().main()