|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Exercise the listtransactions API |
| 4 | + |
| 5 | +# Add python-bitcoinrpc to module search path: |
| 6 | +import os |
| 7 | +import sys |
| 8 | +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "python-bitcoinrpc")) |
| 9 | + |
| 10 | +import json |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +import tempfile |
| 14 | +import traceback |
| 15 | + |
| 16 | +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException |
| 17 | +from util import * |
| 18 | + |
| 19 | + |
| 20 | +def check_array_result(object_array, to_match, expected): |
| 21 | + """ |
| 22 | + Pass in array of JSON objects, a dictionary with key/value pairs |
| 23 | + to match against, and another dictionary with expected key/value |
| 24 | + pairs. |
| 25 | + """ |
| 26 | + num_matched = 0 |
| 27 | + for item in object_array: |
| 28 | + all_match = True |
| 29 | + for key,value in to_match.items(): |
| 30 | + if item[key] != value: |
| 31 | + all_match = False |
| 32 | + if not all_match: |
| 33 | + continue |
| 34 | + for key,value in expected.items(): |
| 35 | + if item[key] != value: |
| 36 | + raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value))) |
| 37 | + num_matched = num_matched+1 |
| 38 | + if num_matched == 0: |
| 39 | + raise AssertionError("No objects matched %s"%(str(to_match))) |
| 40 | + |
| 41 | +def run_test(nodes): |
| 42 | + # Simple send, 0 to 1: |
| 43 | + txid = nodes[0].sendtoaddress(nodes[1].getnewaddress(), 0.1) |
| 44 | + sync_mempools(nodes) |
| 45 | + check_array_result(nodes[0].listtransactions(), |
| 46 | + {"txid":txid}, |
| 47 | + {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":0}) |
| 48 | + check_array_result(nodes[1].listtransactions(), |
| 49 | + {"txid":txid}, |
| 50 | + {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":0}) |
| 51 | + # mine a block, confirmations should change: |
| 52 | + nodes[0].setgenerate(True, 1) |
| 53 | + sync_blocks(nodes) |
| 54 | + check_array_result(nodes[0].listtransactions(), |
| 55 | + {"txid":txid}, |
| 56 | + {"category":"send","account":"","amount":Decimal("-0.1"),"confirmations":1}) |
| 57 | + check_array_result(nodes[1].listtransactions(), |
| 58 | + {"txid":txid}, |
| 59 | + {"category":"receive","account":"","amount":Decimal("0.1"),"confirmations":1}) |
| 60 | + |
| 61 | + # send-to-self: |
| 62 | + txid = nodes[0].sendtoaddress(nodes[0].getnewaddress(), 0.2) |
| 63 | + check_array_result(nodes[0].listtransactions(), |
| 64 | + {"txid":txid, "category":"send"}, |
| 65 | + {"amount":Decimal("-0.2")}) |
| 66 | + check_array_result(nodes[0].listtransactions(), |
| 67 | + {"txid":txid, "category":"receive"}, |
| 68 | + {"amount":Decimal("0.2")}) |
| 69 | + |
| 70 | + # sendmany from node1: twice to self, twice to node2: |
| 71 | + send_to = { nodes[0].getnewaddress() : 0.11, nodes[1].getnewaddress() : 0.22, |
| 72 | + nodes[0].getaccountaddress("from1") : 0.33, nodes[1].getaccountaddress("toself") : 0.44 } |
| 73 | + txid = nodes[1].sendmany("", send_to) |
| 74 | + sync_mempools(nodes) |
| 75 | + check_array_result(nodes[1].listtransactions(), |
| 76 | + {"category":"send","amount":Decimal("-0.11")}, |
| 77 | + {"txid":txid} ) |
| 78 | + check_array_result(nodes[0].listtransactions(), |
| 79 | + {"category":"receive","amount":Decimal("0.11")}, |
| 80 | + {"txid":txid} ) |
| 81 | + check_array_result(nodes[1].listtransactions(), |
| 82 | + {"category":"send","amount":Decimal("-0.22")}, |
| 83 | + {"txid":txid} ) |
| 84 | + check_array_result(nodes[1].listtransactions(), |
| 85 | + {"category":"receive","amount":Decimal("0.22")}, |
| 86 | + {"txid":txid} ) |
| 87 | + check_array_result(nodes[1].listtransactions(), |
| 88 | + {"category":"send","amount":Decimal("-0.33")}, |
| 89 | + {"txid":txid} ) |
| 90 | + check_array_result(nodes[0].listtransactions(), |
| 91 | + {"category":"receive","amount":Decimal("0.33")}, |
| 92 | + {"txid":txid, "account" : "from1"} ) |
| 93 | + check_array_result(nodes[1].listtransactions(), |
| 94 | + {"category":"send","amount":Decimal("-0.44")}, |
| 95 | + {"txid":txid, "account" : ""} ) |
| 96 | + check_array_result(nodes[1].listtransactions(), |
| 97 | + {"category":"receive","amount":Decimal("0.44")}, |
| 98 | + {"txid":txid, "account" : "toself"} ) |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + import optparse |
| 103 | + |
| 104 | + parser = optparse.OptionParser(usage="%prog [options]") |
| 105 | + parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true", |
| 106 | + help="Leave bitcoinds and test.* datadir on exit or error") |
| 107 | + parser.add_option("--srcdir", dest="srcdir", default="../../src", |
| 108 | + help="Source directory containing bitcoind/bitcoin-cli (default: %default%)") |
| 109 | + parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"), |
| 110 | + help="Root directory for datadirs") |
| 111 | + (options, args) = parser.parse_args() |
| 112 | + |
| 113 | + os.environ['PATH'] = options.srcdir+":"+os.environ['PATH'] |
| 114 | + |
| 115 | + check_json_precision() |
| 116 | + |
| 117 | + success = False |
| 118 | + try: |
| 119 | + print("Initializing test directory "+options.tmpdir) |
| 120 | + if not os.path.isdir(options.tmpdir): |
| 121 | + os.makedirs(options.tmpdir) |
| 122 | + initialize_chain(options.tmpdir) |
| 123 | + |
| 124 | + nodes = start_nodes(2, options.tmpdir) |
| 125 | + connect_nodes(nodes[1], 0) |
| 126 | + sync_blocks(nodes) |
| 127 | + run_test(nodes) |
| 128 | + |
| 129 | + success = True |
| 130 | + |
| 131 | + except AssertionError as e: |
| 132 | + print("Assertion failed: "+e.message) |
| 133 | + except Exception as e: |
| 134 | + print("Unexpected exception caught during testing: "+str(e)) |
| 135 | + stack = traceback.extract_tb(sys.exc_info()[2]) |
| 136 | + print(stack[-1]) |
| 137 | + |
| 138 | + if not options.nocleanup: |
| 139 | + print("Cleaning up") |
| 140 | + stop_nodes() |
| 141 | + shutil.rmtree(options.tmpdir) |
| 142 | + |
| 143 | + if success: |
| 144 | + print("Tests successful") |
| 145 | + sys.exit(0) |
| 146 | + else: |
| 147 | + print("Failed") |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + main() |
0 commit comments