Skip to content

Commit 866dd66

Browse files
committed
Merge bitcoin#26196: kernel: move RunCommandParseJSON to its own file
43b8777 refactor: move run_command from util to common (Cory Fields) 192325a kernel: move RunCommandParseJSON to its own file (Cory Fields) Pull request description: Because libbitcoinkernel does not include this new object, this has the side-effect of eliminating its unnecessary `boost::process` dependency. This leaves libbitcoinkernel with 3 remaining boost dependencies: - `boost::date_time` for `util/time.cpp`, which I'll separate out next. Exactly like this PR. - `boost::signals2` for which I have a POC re-implementation here: https://github.com/theuni/bitcoin/commits/replace-boost-signals - `boost::multi_index` which I'm not sure about yet. ACKs for top commit: ryanofsky: Code review ACK 43b8777. Could consider squashing the two commits, so the code just moves once instead of twice. fanquake: ACK 43b8777 Tree-SHA512: f2a46cac34aaadfb8a1442316152ad354f6990021b82c78d80cae9fd43cd026209ffd62132eaa99d5d0f8cf34e996b6737d318a9d9a3f1d2ff8d17d697abf26d
2 parents 869342f + 43b8777 commit 866dd66

File tree

7 files changed

+90
-62
lines changed

7 files changed

+90
-62
lines changed

src/Makefile.am

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ BITCOIN_CORE_H = \
133133
clientversion.h \
134134
coins.h \
135135
common/bloom.h \
136+
common/run_command.h \
136137
compat/assumptions.h \
137138
compat/byteswap.h \
138139
compat/compat.h \
@@ -616,14 +617,15 @@ libbitcoin_consensus_a_SOURCES = \
616617
version.h
617618

618619
# common: shared between bitcoind, and bitcoin-qt and non-server tools
619-
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
620+
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
620621
libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
621622
libbitcoin_common_a_SOURCES = \
622623
base58.cpp \
623624
bech32.cpp \
624625
chainparams.cpp \
625626
coins.cpp \
626627
common/bloom.cpp \
628+
common/run_command.cpp \
627629
compressor.cpp \
628630
core_read.cpp \
629631
core_write.cpp \

src/common/run_command.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#if defined(HAVE_CONFIG_H)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <common/run_command.h>
10+
11+
#include <tinyformat.h>
12+
#include <univalue.h>
13+
14+
#ifdef ENABLE_EXTERNAL_SIGNER
15+
#if defined(__GNUC__)
16+
// Boost 1.78 requires the following workaround.
17+
// See: https://github.com/boostorg/process/issues/235
18+
#pragma GCC diagnostic push
19+
#pragma GCC diagnostic ignored "-Wnarrowing"
20+
#endif
21+
#include <boost/process.hpp>
22+
#if defined(__GNUC__)
23+
#pragma GCC diagnostic pop
24+
#endif
25+
#endif // ENABLE_EXTERNAL_SIGNER
26+
27+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
28+
{
29+
#ifdef ENABLE_EXTERNAL_SIGNER
30+
namespace bp = boost::process;
31+
32+
UniValue result_json;
33+
bp::opstream stdin_stream;
34+
bp::ipstream stdout_stream;
35+
bp::ipstream stderr_stream;
36+
37+
if (str_command.empty()) return UniValue::VNULL;
38+
39+
bp::child c(
40+
str_command,
41+
bp::std_out > stdout_stream,
42+
bp::std_err > stderr_stream,
43+
bp::std_in < stdin_stream
44+
);
45+
if (!str_std_in.empty()) {
46+
stdin_stream << str_std_in << std::endl;
47+
}
48+
stdin_stream.pipe().close();
49+
50+
std::string result;
51+
std::string error;
52+
std::getline(stdout_stream, result);
53+
std::getline(stderr_stream, error);
54+
55+
c.wait();
56+
const int n_error = c.exit_code();
57+
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
58+
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
59+
60+
return result_json;
61+
#else
62+
throw std::runtime_error("Compiled without external signing support (required for external signing).");
63+
#endif // ENABLE_EXTERNAL_SIGNER
64+
}

src/common/run_command.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COMMON_RUN_COMMAND_H
6+
#define BITCOIN_COMMON_RUN_COMMAND_H
7+
8+
#include <string>
9+
10+
class UniValue;
11+
12+
/**
13+
* Execute a command which returns JSON, and parse the result.
14+
*
15+
* @param str_command The command to execute, including any arguments
16+
* @param str_std_in string to pass to stdin
17+
* @return parsed JSON
18+
*/
19+
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
20+
21+
#endif // BITCOIN_COMMON_RUN_COMMAND_H

src/external_signer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <chainparams.h>
6+
#include <common/run_command.h>
67
#include <core_io.h>
78
#include <psbt.h>
89
#include <util/strencodings.h>
9-
#include <util/system.h>
1010
#include <external_signer.h>
1111

1212
#include <algorithm>

src/test/system_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
55
#include <test/util/setup_common.h>
6-
#include <util/system.h>
6+
#include <common/run_command.h>
77
#include <univalue.h>
88

99
#ifdef ENABLE_EXTERNAL_SIGNER

src/util/system.cpp

-51
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@
55

66
#include <util/system.h>
77

8-
#ifdef ENABLE_EXTERNAL_SIGNER
9-
#if defined(__GNUC__)
10-
// Boost 1.78 requires the following workaround.
11-
// See: https://github.com/boostorg/process/issues/235
12-
#pragma GCC diagnostic push
13-
#pragma GCC diagnostic ignored "-Wnarrowing"
14-
#endif
15-
#include <boost/process.hpp>
16-
#if defined(__GNUC__)
17-
#pragma GCC diagnostic pop
18-
#endif
19-
#endif // ENABLE_EXTERNAL_SIGNER
20-
218
#include <chainparamsbase.h>
229
#include <fs.h>
2310
#include <sync.h>
@@ -1332,44 +1319,6 @@ void runCommand(const std::string& strCommand)
13321319
}
13331320
#endif
13341321

1335-
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
1336-
{
1337-
#ifdef ENABLE_EXTERNAL_SIGNER
1338-
namespace bp = boost::process;
1339-
1340-
UniValue result_json;
1341-
bp::opstream stdin_stream;
1342-
bp::ipstream stdout_stream;
1343-
bp::ipstream stderr_stream;
1344-
1345-
if (str_command.empty()) return UniValue::VNULL;
1346-
1347-
bp::child c(
1348-
str_command,
1349-
bp::std_out > stdout_stream,
1350-
bp::std_err > stderr_stream,
1351-
bp::std_in < stdin_stream
1352-
);
1353-
if (!str_std_in.empty()) {
1354-
stdin_stream << str_std_in << std::endl;
1355-
}
1356-
stdin_stream.pipe().close();
1357-
1358-
std::string result;
1359-
std::string error;
1360-
std::getline(stdout_stream, result);
1361-
std::getline(stderr_stream, error);
1362-
1363-
c.wait();
1364-
const int n_error = c.exit_code();
1365-
if (n_error) throw std::runtime_error(strprintf("RunCommandParseJSON error: process(%s) returned %d: %s\n", str_command, n_error, error));
1366-
if (!result_json.read(result)) throw std::runtime_error("Unable to parse JSON: " + result);
1367-
1368-
return result_json;
1369-
#else
1370-
throw std::runtime_error("Compiled without external signing support (required for external signing).");
1371-
#endif // ENABLE_EXTERNAL_SIGNER
1372-
}
13731322

13741323
void SetupEnvironment()
13751324
{

src/util/system.h

-8
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,6 @@ std::string ShellEscape(const std::string& arg);
107107
#if HAVE_SYSTEM
108108
void runCommand(const std::string& strCommand);
109109
#endif
110-
/**
111-
* Execute a command which returns JSON, and parse the result.
112-
*
113-
* @param str_command The command to execute, including any arguments
114-
* @param str_std_in string to pass to stdin
115-
* @return parsed JSON
116-
*/
117-
UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in="");
118110

119111
/**
120112
* Most paths passed as configuration arguments are treated as relative to

0 commit comments

Comments
 (0)