|
| 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 wallet keypool, and interaction with wallet encryption/locking |
| 7 | + |
| 8 | +# Add python-bitcoinrpc to module search path: |
| 9 | +import os |
| 10 | +import sys |
| 11 | +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "python-bitcoinrpc")) |
| 12 | + |
| 13 | +import json |
| 14 | +import shutil |
| 15 | +import subprocess |
| 16 | +import tempfile |
| 17 | +import traceback |
| 18 | + |
| 19 | +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException |
| 20 | +from util import * |
| 21 | + |
| 22 | + |
| 23 | +def check_array_result(object_array, to_match, expected): |
| 24 | + """ |
| 25 | + Pass in array of JSON objects, a dictionary with key/value pairs |
| 26 | + to match against, and another dictionary with expected key/value |
| 27 | + pairs. |
| 28 | + """ |
| 29 | + num_matched = 0 |
| 30 | + for item in object_array: |
| 31 | + all_match = True |
| 32 | + for key,value in to_match.items(): |
| 33 | + if item[key] != value: |
| 34 | + all_match = False |
| 35 | + if not all_match: |
| 36 | + continue |
| 37 | + for key,value in expected.items(): |
| 38 | + if item[key] != value: |
| 39 | + raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value))) |
| 40 | + num_matched = num_matched+1 |
| 41 | + if num_matched == 0: |
| 42 | + raise AssertionError("No objects matched %s"%(str(to_match))) |
| 43 | + |
| 44 | +def run_test(nodes, tmpdir): |
| 45 | + # Encrypt wallet and wait to terminate |
| 46 | + nodes[0].encryptwallet('test') |
| 47 | + bitcoind_processes[0].wait() |
| 48 | + # Restart node 0 |
| 49 | + nodes[0] = start_node(0, tmpdir) |
| 50 | + # Keep creating keys |
| 51 | + addr = nodes[0].getnewaddress() |
| 52 | + try: |
| 53 | + addr = nodes[0].getnewaddress() |
| 54 | + raise AssertionError('Keypool should be exhausted after one address') |
| 55 | + except JSONRPCException,e: |
| 56 | + assert(e.error['code']==-12) |
| 57 | + |
| 58 | + # put three new keys in the keypool |
| 59 | + nodes[0].walletpassphrase('test', 12000) |
| 60 | + nodes[0].keypoolrefill(3) |
| 61 | + nodes[0].walletlock() |
| 62 | + |
| 63 | + # drain the keys |
| 64 | + addr = set() |
| 65 | + addr.add(nodes[0].getrawchangeaddress()) |
| 66 | + addr.add(nodes[0].getrawchangeaddress()) |
| 67 | + addr.add(nodes[0].getrawchangeaddress()) |
| 68 | + addr.add(nodes[0].getrawchangeaddress()) |
| 69 | + # assert that four unique addresses were returned |
| 70 | + assert(len(addr) == 4) |
| 71 | + # the next one should fail |
| 72 | + try: |
| 73 | + addr = nodes[0].getrawchangeaddress() |
| 74 | + raise AssertionError('Keypool should be exhausted after three addresses') |
| 75 | + except JSONRPCException,e: |
| 76 | + assert(e.error['code']==-12) |
| 77 | + |
| 78 | + |
| 79 | +def main(): |
| 80 | + import optparse |
| 81 | + |
| 82 | + parser = optparse.OptionParser(usage="%prog [options]") |
| 83 | + parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true", |
| 84 | + help="Leave bitcoinds and test.* datadir on exit or error") |
| 85 | + parser.add_option("--srcdir", dest="srcdir", default="../../src", |
| 86 | + help="Source directory containing bitcoind/bitcoin-cli (default: %default%)") |
| 87 | + parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"), |
| 88 | + help="Root directory for datadirs") |
| 89 | + (options, args) = parser.parse_args() |
| 90 | + |
| 91 | + os.environ['PATH'] = options.srcdir+":"+os.environ['PATH'] |
| 92 | + |
| 93 | + check_json_precision() |
| 94 | + |
| 95 | + success = False |
| 96 | + nodes = [] |
| 97 | + try: |
| 98 | + print("Initializing test directory "+options.tmpdir) |
| 99 | + if not os.path.isdir(options.tmpdir): |
| 100 | + os.makedirs(options.tmpdir) |
| 101 | + initialize_chain(options.tmpdir) |
| 102 | + |
| 103 | + nodes = start_nodes(1, options.tmpdir) |
| 104 | + |
| 105 | + run_test(nodes, options.tmpdir) |
| 106 | + |
| 107 | + success = True |
| 108 | + |
| 109 | + except AssertionError as e: |
| 110 | + print("Assertion failed: "+e.message) |
| 111 | + except JSONRPCException as e: |
| 112 | + print("JSONRPC error: "+e.error['message']) |
| 113 | + traceback.print_tb(sys.exc_info()[2]) |
| 114 | + except Exception as e: |
| 115 | + print("Unexpected exception caught during testing: "+str(sys.exc_info()[0])) |
| 116 | + traceback.print_tb(sys.exc_info()[2]) |
| 117 | + |
| 118 | + if not options.nocleanup: |
| 119 | + print("Cleaning up") |
| 120 | + stop_nodes(nodes) |
| 121 | + wait_bitcoinds() |
| 122 | + shutil.rmtree(options.tmpdir) |
| 123 | + |
| 124 | + if success: |
| 125 | + print("Tests successful") |
| 126 | + sys.exit(0) |
| 127 | + else: |
| 128 | + print("Failed") |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + main() |
0 commit comments