Skip to content

Commit

Permalink
kernel: Remove UniValue from kernel library
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheCharlatan committed Jul 20, 2023
1 parent 4a1aae6 commit 03e06e5
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 39 deletions.
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ BITCOIN_CORE_H = \
compat/endian.h \
common/settings.h \
common/system.h \
common/univalue_helpers.h \
compressor.h \
consensus/consensus.h \
consensus/tx_check.h \
Expand Down Expand Up @@ -667,6 +668,7 @@ libbitcoin_common_a_SOURCES = \
common/run_command.cpp \
common/settings.cpp \
common/system.cpp \
common/univalue_helpers.cpp \
compressor.cpp \
core_read.cpp \
core_write.cpp \
Expand Down Expand Up @@ -896,8 +898,8 @@ if BUILD_BITCOIN_KERNEL_LIB
lib_LTLIBRARIES += $(LIBBITCOINKERNEL)

libbitcoinkernel_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) $(PTHREAD_FLAGS)
libbitcoinkernel_la_LIBADD = $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1)
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)
libbitcoinkernel_la_LIBADD = $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1)
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)

# libbitcoinkernel requires default symbol visibility, explicitly specify that
# here so that things still work even when user configures with
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <coins.h>
#include <common/args.h>
#include <common/system.h>
#include <common/univalue_helpers.h>
#include <compat/compat.h>
#include <consensus/amount.h>
#include <consensus/consensus.h>
Expand Down
47 changes: 47 additions & 0 deletions src/common/univalue_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <script/interpreter.h>
#include <univalue.h>
#include <util/strencodings.h>

#include <map>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
{
std::string strHex;
if (v.isStr())
strHex = v.getValStr();
if (!IsHex(strHex))
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
return ParseHex(strHex);
}

int ParseSighashString(const UniValue& sighash)
{
int hash_type = SIGHASH_DEFAULT;
if (!sighash.isNull()) {
static std::map<std::string, int> map_sighash_values = {
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
{std::string("ALL"), int(SIGHASH_ALL)},
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
{std::string("NONE"), int(SIGHASH_NONE)},
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
};
const std::string& strHashType = sighash.get_str();
const auto& it = map_sighash_values.find(strHashType);
if (it != map_sighash_values.end()) {
hash_type = it->second;
} else {
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
}
}
return hash_type;
}
17 changes: 17 additions & 0 deletions src/common/univalue_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_COMMON_UNIVALUE_HELPERS_H
#define BITCOIN_COMMON_UNIVALUE_HELPERS_H

#include <string>
#include <vector>

class UniValue;

std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);

int ParseSighashString(const UniValue& sighash);

#endif // BITCOIN_COMMON_UNIVALUE_HELPERS_H
2 changes: 0 additions & 2 deletions src/core_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header);
* @see ParseHashV for an RPC-oriented version of this
*/
bool ParseHashStr(const std::string& strHex, uint256& result);
std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName);
int ParseSighashString(const UniValue& sighash);

// core_write.cpp
UniValue ValueFromAmount(const CAmount amount);
Expand Down
35 changes: 0 additions & 35 deletions src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <script/sign.h>
#include <serialize.h>
#include <streams.h>
#include <univalue.h>
#include <util/strencodings.h>
#include <version.h>

Expand Down Expand Up @@ -241,37 +240,3 @@ bool ParseHashStr(const std::string& strHex, uint256& result)
result.SetHex(strHex);
return true;
}

std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
{
std::string strHex;
if (v.isStr())
strHex = v.getValStr();
if (!IsHex(strHex))
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
return ParseHex(strHex);
}

int ParseSighashString(const UniValue& sighash)
{
int hash_type = SIGHASH_DEFAULT;
if (!sighash.isNull()) {
static std::map<std::string, int> map_sighash_values = {
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
{std::string("ALL"), int(SIGHASH_ALL)},
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
{std::string("NONE"), int(SIGHASH_NONE)},
{std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
{std::string("SINGLE"), int(SIGHASH_SINGLE)},
{std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
};
const std::string& strHashType = sighash.get_str();
const auto& it = map_sighash_values.find(strHashType);
if (it != map_sighash_values.end()) {
hash_type = it->second;
} else {
throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
}
}
return hash_type;
}
1 change: 1 addition & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <base58.h>
#include <chain.h>
#include <coins.h>
#include <common/univalue_helpers.h>
#include <consensus/amount.h>
#include <consensus/validation.h>
#include <core_io.h>
Expand Down
1 change: 1 addition & 0 deletions src/rpc/rawtransaction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <rpc/rawtransaction_util.h>

#include <coins.h>
#include <common/univalue_helpers.h>
#include <consensus/amount.h>
#include <core_io.h>
#include <key_io.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/parse_univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <chainparams.h>
#include <common/univalue_helpers.h>
#include <core_io.h>
#include <rpc/client.h>
#include <rpc/util.h>
Expand Down
1 change: 1 addition & 0 deletions src/wallet/rpc/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <common/univalue_helpers.h>
#include <consensus/validation.h>
#include <core_io.h>
#include <key_io.h>
Expand Down

0 comments on commit 03e06e5

Please sign in to comment.