Skip to content

Commit 03e06e5

Browse files
committed
kernel: Remove UniValue from kernel library
Besides the build system changes, this is a move-only change for moving the few UniValue-related functions out of kernel files. UniValue is not required by any of the kernel components and a JSON library should not need to be part of a consensus library.
1 parent 4a1aae6 commit 03e06e5

File tree

10 files changed

+73
-39
lines changed

10 files changed

+73
-39
lines changed

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ BITCOIN_CORE_H = \
145145
compat/endian.h \
146146
common/settings.h \
147147
common/system.h \
148+
common/univalue_helpers.h \
148149
compressor.h \
149150
consensus/consensus.h \
150151
consensus/tx_check.h \
@@ -667,6 +668,7 @@ libbitcoin_common_a_SOURCES = \
667668
common/run_command.cpp \
668669
common/settings.cpp \
669670
common/system.cpp \
671+
common/univalue_helpers.cpp \
670672
compressor.cpp \
671673
core_read.cpp \
672674
core_write.cpp \
@@ -896,8 +898,8 @@ if BUILD_BITCOIN_KERNEL_LIB
896898
lib_LTLIBRARIES += $(LIBBITCOINKERNEL)
897899

898900
libbitcoinkernel_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) $(PTHREAD_FLAGS)
899-
libbitcoinkernel_la_LIBADD = $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1)
900-
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS) -I$(srcdir)/$(UNIVALUE_INCLUDE_DIR_INT)
901+
libbitcoinkernel_la_LIBADD = $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1)
902+
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)
901903

902904
# libbitcoinkernel requires default symbol visibility, explicitly specify that
903905
# here so that things still work even when user configures with

src/bitcoin-tx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <coins.h>
1212
#include <common/args.h>
1313
#include <common/system.h>
14+
#include <common/univalue_helpers.h>
1415
#include <compat/compat.h>
1516
#include <consensus/amount.h>
1617
#include <consensus/consensus.h>

src/common/univalue_helpers.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2023 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+
#include <script/interpreter.h>
6+
#include <univalue.h>
7+
#include <util/strencodings.h>
8+
9+
#include <map>
10+
#include <stdexcept>
11+
#include <string>
12+
#include <utility>
13+
#include <vector>
14+
15+
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
16+
{
17+
std::string strHex;
18+
if (v.isStr())
19+
strHex = v.getValStr();
20+
if (!IsHex(strHex))
21+
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
22+
return ParseHex(strHex);
23+
}
24+
25+
int ParseSighashString(const UniValue& sighash)
26+
{
27+
int hash_type = SIGHASH_DEFAULT;
28+
if (!sighash.isNull()) {
29+
static std::map<std::string, int> map_sighash_values = {
30+
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
31+
{std::string("ALL"), int(SIGHASH_ALL)},
32+
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
33+
{std::string("NONE"), int(SIGHASH_NONE)},
34+
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
35+
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
36+
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
37+
};
38+
const std::string& strHashType = sighash.get_str();
39+
const auto& it = map_sighash_values.find(strHashType);
40+
if (it != map_sighash_values.end()) {
41+
hash_type = it->second;
42+
} else {
43+
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
44+
}
45+
}
46+
return hash_type;
47+
}

src/common/univalue_helpers.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2023 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_UNIVALUE_HELPERS_H
6+
#define BITCOIN_COMMON_UNIVALUE_HELPERS_H
7+
8+
#include <string>
9+
#include <vector>
10+
11+
class UniValue;
12+
13+
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
14+
15+
int ParseSighashString(const UniValue& sighash);
16+
17+
#endif // BITCOIN_COMMON_UNIVALUE_HELPERS_H

src/core_io.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
4545
* @see ParseHashV for an RPC-oriented version of this
4646
*/
4747
bool ParseHashStr(const std::string& strHex, uint256& result);
48-
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
49-
int ParseSighashString(const UniValue& sighash);
5048

5149
// core_write.cpp
5250
UniValue ValueFromAmount(const CAmount amount);

src/core_read.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <script/sign.h>
1111
#include <serialize.h>
1212
#include <streams.h>
13-
#include <univalue.h>
1413
#include <util/strencodings.h>
1514
#include <version.h>
1615

@@ -241,37 +240,3 @@ bool ParseHashStr(const std::string& strHex, uint256& result)
241240
result.SetHex(strHex);
242241
return true;
243242
}
244-
245-
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
246-
{
247-
std::string strHex;
248-
if (v.isStr())
249-
strHex = v.getValStr();
250-
if (!IsHex(strHex))
251-
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
252-
return ParseHex(strHex);
253-
}
254-
255-
int ParseSighashString(const UniValue& sighash)
256-
{
257-
int hash_type = SIGHASH_DEFAULT;
258-
if (!sighash.isNull()) {
259-
static std::map<std::string, int> map_sighash_values = {
260-
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
261-
{std::string("ALL"), int(SIGHASH_ALL)},
262-
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
263-
{std::string("NONE"), int(SIGHASH_NONE)},
264-
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
265-
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
266-
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
267-
};
268-
const std::string& strHashType = sighash.get_str();
269-
const auto& it = map_sighash_values.find(strHashType);
270-
if (it != map_sighash_values.end()) {
271-
hash_type = it->second;
272-
} else {
273-
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
274-
}
275-
}
276-
return hash_type;
277-
}

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <base58.h>
77
#include <chain.h>
88
#include <coins.h>
9+
#include <common/univalue_helpers.h>
910
#include <consensus/amount.h>
1011
#include <consensus/validation.h>
1112
#include <core_io.h>

src/rpc/rawtransaction_util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <rpc/rawtransaction_util.h>
77

88
#include <coins.h>
9+
#include <common/univalue_helpers.h>
910
#include <consensus/amount.h>
1011
#include <core_io.h>
1112
#include <key_io.h>

src/test/fuzz/parse_univalue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <chainparams.h>
6+
#include <common/univalue_helpers.h>
67
#include <core_io.h>
78
#include <rpc/client.h>
89
#include <rpc/util.h>

src/wallet/rpc/spend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <common/univalue_helpers.h>
56
#include <consensus/validation.h>
67
#include <core_io.h>
78
#include <key_io.h>

0 commit comments

Comments
 (0)