|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Class for bitcoind node under test"""
|
6 | 6 |
|
| 7 | +import decimal |
7 | 8 | import errno
|
8 | 9 | import http.client
|
| 10 | +import json |
9 | 11 | import logging
|
10 | 12 | import os
|
11 | 13 | import subprocess
|
@@ -49,6 +51,8 @@ def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mo
|
49 | 51 | self.extra_args = extra_args
|
50 | 52 | self.args = [self.binary, "-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i]
|
51 | 53 |
|
| 54 | + self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) |
| 55 | + |
52 | 56 | self.running = False
|
53 | 57 | self.process = None
|
54 | 58 | self.rpc_connected = False
|
@@ -136,3 +140,28 @@ def node_encrypt_wallet(self, passphrase):
|
136 | 140 | time.sleep(0.1)
|
137 | 141 | self.rpc = None
|
138 | 142 | self.rpc_connected = False
|
| 143 | + |
| 144 | +class TestNodeCLI(): |
| 145 | + """Interface to bitcoin-cli for an individual node""" |
| 146 | + |
| 147 | + def __init__(self, binary, datadir): |
| 148 | + self.binary = binary |
| 149 | + self.datadir = datadir |
| 150 | + |
| 151 | + def __getattr__(self, command): |
| 152 | + def dispatcher(*args, **kwargs): |
| 153 | + return self.send_cli(command, *args, **kwargs) |
| 154 | + return dispatcher |
| 155 | + |
| 156 | + def send_cli(self, command, *args, **kwargs): |
| 157 | + """Run bitcoin-cli command. Deserializes returned string as python object.""" |
| 158 | + |
| 159 | + pos_args = [str(arg) for arg in args] |
| 160 | + named_args = [str(key) + "=" + str(value) for (key, value) in kwargs.items()] |
| 161 | + assert not (pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call" |
| 162 | + p_args = [self.binary, "-datadir=" + self.datadir] |
| 163 | + if named_args: |
| 164 | + p_args += ["-named"] |
| 165 | + p_args += [command] + pos_args + named_args |
| 166 | + cli_output = subprocess.check_output(p_args, universal_newlines=True) |
| 167 | + return json.loads(cli_output, parse_float=decimal.Decimal) |
0 commit comments