Run bitcoin-cli command. Deserializes returned string as python object.
(self, clicommand=None, *args, **kwargs)
| 912 | return results |
| 913 | |
| 914 | def send_cli(self, clicommand=None, *args, **kwargs): |
| 915 | """Run bitcoin-cli command. Deserializes returned string as python object.""" |
| 916 | pos_args = [arg_to_cli(arg) for arg in args] |
| 917 | named_args = [key + "=" + arg_to_cli(value) for (key, value) in kwargs.items() if value is not None] |
| 918 | p_args = self.binaries.rpc_argv() + self.options |
| 919 | if named_args: |
| 920 | p_args += ["-named"] |
| 921 | base_arg_pos = len(p_args) |
| 922 | if clicommand is not None: |
| 923 | p_args += [clicommand] |
| 924 | p_args += pos_args + named_args |
| 925 | |
| 926 | # TEST_CLI_MAX_ARG_SIZE is set low enough that checking the string |
| 927 | # length is enough and encoding to bytes is not needed before |
| 928 | # calculating the sum. |
| 929 | sum_arg_size = sum(len(arg) for arg in p_args) |
| 930 | stdin_data = self.input |
| 931 | if sum_arg_size >= TEST_CLI_MAX_ARG_SIZE: |
| 932 | self.log.debug(f"Cli: Command size {sum_arg_size} too large, using stdin") |
| 933 | rpc_args = "\n".join([arg for arg in p_args[base_arg_pos:]]) |
| 934 | if stdin_data is not None: |
| 935 | stdin_data += "\n" + rpc_args |
| 936 | else: |
| 937 | stdin_data = rpc_args |
| 938 | p_args = p_args[:base_arg_pos] + ['-stdin'] |
| 939 | |
| 940 | self.log.debug("Running bitcoin-cli {}".format(p_args[2:])) |
| 941 | process = subprocess.Popen(p_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 942 | cli_stdout, cli_stderr = process.communicate(input=stdin_data) |
| 943 | returncode = process.poll() |
| 944 | if returncode: |
| 945 | match = re.match(r'error code: ([-0-9]+)\nerror message:\n(.*)', cli_stderr) |
| 946 | if match: |
| 947 | code, message = match.groups() |
| 948 | raise JSONRPCException(dict(code=int(code), message=message)) |
| 949 | # Ignore cli_stdout, raise with cli_stderr |
| 950 | raise subprocess.CalledProcessError(returncode, p_args, output=cli_stderr) |
| 951 | try: |
| 952 | if not cli_stdout.strip(): |
| 953 | return None |
| 954 | return json.loads(cli_stdout, parse_float=decimal.Decimal) |
| 955 | except (json.JSONDecodeError, decimal.InvalidOperation): |
| 956 | return cli_stdout.rstrip("\n") |
no test coverage detected