Skip to content

Commit b45a6e8

Browse files
committed
Add test for getblocktemplate longpolling
1 parent ff6a7af commit b45a6e8

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

qa/rpc-tests/getblocktemplate.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2014 The Bitcoin Core developers
3+
# Distributed under the MIT/X11 software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
# Exercise the listtransactions API
7+
8+
from test_framework import BitcoinTestFramework
9+
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
10+
from util import *
11+
12+
13+
def check_array_result(object_array, to_match, expected):
14+
"""
15+
Pass in array of JSON objects, a dictionary with key/value pairs
16+
to match against, and another dictionary with expected key/value
17+
pairs.
18+
"""
19+
num_matched = 0
20+
for item in object_array:
21+
all_match = True
22+
for key,value in to_match.items():
23+
if item[key] != value:
24+
all_match = False
25+
if not all_match:
26+
continue
27+
for key,value in expected.items():
28+
if item[key] != value:
29+
raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
30+
num_matched = num_matched+1
31+
if num_matched == 0:
32+
raise AssertionError("No objects matched %s"%(str(to_match)))
33+
34+
import threading
35+
36+
class LongpollThread(threading.Thread):
37+
def __init__(self, node):
38+
threading.Thread.__init__(self)
39+
# query current longpollid
40+
templat = node.getblocktemplate()
41+
self.longpollid = templat['longpollid']
42+
# create a new connection to the node, we can't use the same
43+
# connection from two threads
44+
self.node = AuthServiceProxy(node.url, timeout=600)
45+
46+
def run(self):
47+
self.node.getblocktemplate({'longpollid':self.longpollid})
48+
49+
class GetBlockTemplateTest(BitcoinTestFramework):
50+
'''
51+
Test longpolling with getblocktemplate.
52+
'''
53+
54+
def run_test(self, nodes):
55+
print "Warning: this test will take about 70 seconds in the best case. Be patient."
56+
nodes[0].setgenerate(True, 10)
57+
templat = nodes[0].getblocktemplate()
58+
longpollid = templat['longpollid']
59+
# longpollid should not change between successive invocations if nothing else happens
60+
templat2 = nodes[0].getblocktemplate()
61+
assert(templat2['longpollid'] == longpollid)
62+
63+
# Test 1: test that the longpolling wait if we do nothing
64+
thr = LongpollThread(nodes[0])
65+
thr.start()
66+
# check that thread still lives
67+
thr.join(5) # wait 5 seconds or until thread exits
68+
assert(thr.is_alive())
69+
70+
# Test 2: test that longpoll will terminate if another node generates a block
71+
nodes[1].setgenerate(True, 1) # generate a block on another node
72+
# check that thread will exit now that new transaction entered mempool
73+
thr.join(5) # wait 5 seconds or until thread exits
74+
assert(not thr.is_alive())
75+
76+
# Test 3: test that longpoll will terminate if we generate a block ourselves
77+
thr = LongpollThread(nodes[0])
78+
thr.start()
79+
nodes[0].setgenerate(True, 1) # generate a block on another node
80+
thr.join(5) # wait 5 seconds or until thread exits
81+
assert(not thr.is_alive())
82+
83+
# Test 4: test that introducing a new transaction into the mempool will terminate the longpoll
84+
thr = LongpollThread(nodes[0])
85+
thr.start()
86+
# generate a random transaction and submit it
87+
(txid, txhex, fee) = random_transaction(nodes, Decimal("1.1"), Decimal("0.0"), Decimal("0.001"), 20)
88+
# after one minute, every 10 seconds the mempool is probed, so in 80 seconds it should have returned
89+
thr.join(60 + 20)
90+
assert(not thr.is_alive())
91+
92+
if __name__ == '__main__':
93+
GetBlockTemplateTest().main()
94+

qa/rpc-tests/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def start_node(i, dir, extra_args=None, rpchost=None):
156156
["-rpcwait", "getblockcount"], stdout=devnull)
157157
devnull.close()
158158
url = "http://rt:rt@%s:%d" % (rpchost or '127.0.0.1', rpc_port(i))
159-
return AuthServiceProxy(url)
159+
proxy = AuthServiceProxy(url)
160+
proxy.url = url # store URL on proxy for info
161+
return proxy
160162

161163
def start_nodes(num_nodes, dir, extra_args=None, rpchost=None):
162164
"""

0 commit comments

Comments
 (0)