|
| 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 | + |
0 commit comments