util: move spanparsing.h to script/parsing.h

Move miniscript / descriptor script parsing functions out of util library so
they are not a dependency of the kernel.

There are no changes to code or behavior.
This commit is contained in:
Ryan Ofsky 2023-12-06 15:58:47 -05:00
parent 6dd2ad4792
commit 9bcce2608d
9 changed files with 27 additions and 40 deletions

View File

@ -268,6 +268,7 @@ BITCOIN_CORE_H = \
script/descriptor.h \
script/keyorigin.h \
script/miniscript.h \
script/parsing.h \
script/sigcache.h \
script/sign.h \
script/signingprovider.h \
@ -318,7 +319,6 @@ BITCOIN_CORE_H = \
util/serfloat.h \
util/signalinterrupt.h \
util/sock.h \
util/spanparsing.h \
util/strencodings.h \
util/string.h \
util/subprocess.h \
@ -711,6 +711,7 @@ libbitcoin_common_a_SOURCES = \
scheduler.cpp \
script/descriptor.cpp \
script/miniscript.cpp \
script/parsing.cpp \
script/sign.cpp \
script/signingprovider.cpp \
script/solver.cpp \
@ -752,7 +753,6 @@ libbitcoin_util_a_SOURCES = \
util/threadinterrupt.cpp \
util/threadnames.cpp \
util/serfloat.cpp \
util/spanparsing.cpp \
util/strencodings.cpp \
util/string.cpp \
util/time.cpp \

View File

@ -371,6 +371,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/script_format.cpp \
test/fuzz/script_interpreter.cpp \
test/fuzz/script_ops.cpp \
test/fuzz/script_parsing.cpp \
test/fuzz/script_sigcache.cpp \
test/fuzz/script_sign.cpp \
test/fuzz/scriptnum_ops.cpp \
@ -380,7 +381,6 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/signet.cpp \
test/fuzz/socks5.cpp \
test/fuzz/span.cpp \
test/fuzz/spanparsing.cpp \
test/fuzz/string.cpp \
test/fuzz/strprintf.cpp \
test/fuzz/system.cpp \

View File

@ -12,12 +12,12 @@
#include <netaddress.h>
#include <netbase.h>
#include <random.h>
#include <script/parsing.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/fs.h>
#include <util/readwritefile.h>
#include <util/sock.h>
#include <util/spanparsing.h>
#include <util/strencodings.h>
#include <util/threadinterrupt.h>
@ -308,7 +308,7 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
reply.full = sock.RecvUntilTerminator('\n', recv_timeout, *m_interrupt, MAX_MSG_SIZE);
for (const auto& kv : spanparsing::Split(reply.full, ' ')) {
for (const auto& kv : Split(reply.full, ' ')) {
const auto& pos = std::find(kv.begin(), kv.end(), '=');
if (pos != kv.end()) {
reply.keys.emplace(std::string{kv.begin(), pos}, std::string{pos + 1, kv.end()});

View File

@ -8,6 +8,7 @@
#include <key_io.h>
#include <pubkey.h>
#include <script/miniscript.h>
#include <script/parsing.h>
#include <script/script.h>
#include <script/signingprovider.h>
#include <script/solver.h>
@ -17,7 +18,6 @@
#include <span.h>
#include <util/bip32.h>
#include <util/check.h>
#include <util/spanparsing.h>
#include <util/strencodings.h>
#include <util/vector.h>
@ -1350,8 +1350,6 @@ enum class ParseScriptContext {
/** Parse a public key that excludes origin information. */
std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error)
{
using namespace spanparsing;
bool permit_uncompressed = ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH;
auto split = Split(sp, '/');
std::string str(split[0].begin(), split[0].end());
@ -1424,8 +1422,6 @@ std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const S
/** Parse a public key including origin information (if enabled). */
std::unique_ptr<PubkeyProvider> ParsePubkey(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
{
using namespace spanparsing;
auto origin_split = Split(sp, ']');
if (origin_split.size() > 2) {
error = "Multiple ']' characters found for a single pubkey";
@ -1589,7 +1585,7 @@ struct KeyParser {
// NOLINTNEXTLINE(misc-no-recursion)
std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
{
using namespace spanparsing;
using namespace script;
auto expr = Expr(sp);
if (Func("pk", expr)) {
@ -2038,8 +2034,6 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
/** Check a descriptor checksum, and update desc to be the checksum-less part. */
bool CheckChecksum(Span<const char>& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr)
{
using namespace spanparsing;
auto check_split = Split(sp, '#');
if (check_split.size() > 2) {
error = "Multiple '#' symbols";

View File

@ -18,10 +18,10 @@
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <script/parsing.h>
#include <script/script.h>
#include <span.h>
#include <util/check.h>
#include <util/spanparsing.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/vector.h>
@ -1764,7 +1764,7 @@ void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector<Node
template<typename Key, typename Ctx>
inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
{
using namespace spanparsing;
using namespace script;
// Account for the minimum script size for all parsed fragments so far. It "borrows" 1
// script byte from all leaf nodes, counting it instead whenever a space for a recursive

View File

@ -2,7 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <util/spanparsing.h>
#include <script/parsing.h>
#include <span.h>
@ -10,7 +10,7 @@
#include <cstddef>
#include <string>
namespace spanparsing {
namespace script {
bool Const(const std::string& str, Span<const char>& sp)
{
@ -49,4 +49,4 @@ Span<const char> Expr(Span<const char>& sp)
return ret;
}
} // namespace spanparsing
} // namespace script

View File

@ -2,15 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_UTIL_SPANPARSING_H
#define BITCOIN_UTIL_SPANPARSING_H
#ifndef BITCOIN_SCRIPT_PARSING_H
#define BITCOIN_SCRIPT_PARSING_H
#include <span.h>
#include <util/string.h>
#include <string>
namespace spanparsing {
namespace script {
/** Parse a constant.
*
@ -36,13 +35,6 @@ bool Func(const std::string& str, Span<const char>& sp);
*/
Span<const char> Expr(Span<const char>& sp);
/** Split alias for backwards compatibility */
template <typename... Args>
auto Split(Args&&... args)
{
return ::Split(std::forward<Args>(args)...);
}
} // namespace script
} // namespace spanparsing
#endif // BITCOIN_UTIL_SPANPARSING_H
#endif // BITCOIN_SCRIPT_PARSING_H

View File

@ -2,11 +2,12 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/parsing.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <util/spanparsing.h>
#include <util/string.h>
FUZZ_TARGET(spanparsing)
FUZZ_TARGET(script_parsing)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const size_t query_size = fuzzed_data_provider.ConsumeIntegral<size_t>();
@ -15,16 +16,16 @@ FUZZ_TARGET(spanparsing)
const Span<const char> const_span{span_str};
Span<const char> mut_span = const_span;
(void)spanparsing::Const(query, mut_span);
(void)script::Const(query, mut_span);
mut_span = const_span;
(void)spanparsing::Func(query, mut_span);
(void)script::Func(query, mut_span);
mut_span = const_span;
(void)spanparsing::Expr(mut_span);
(void)script::Expr(mut_span);
if (!query.empty()) {
mut_span = const_span;
(void)spanparsing::Split(mut_span, query.front());
(void)Split(mut_span, query.front());
}
}

View File

@ -6,6 +6,7 @@
#include <common/signmessage.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
#include <hash.h> // For Hash()
#include <key.h> // For CKey
#include <script/parsing.h>
#include <sync.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
@ -16,7 +17,6 @@
#include <util/moneystr.h>
#include <util/overflow.h>
#include <util/readwritefile.h>
#include <util/spanparsing.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/time.h>
@ -1292,9 +1292,9 @@ static std::string SpanToStr(const Span<const char>& span)
return std::string(span.begin(), span.end());
}
BOOST_AUTO_TEST_CASE(test_spanparsing)
BOOST_AUTO_TEST_CASE(test_script_parsing)
{
using namespace spanparsing;
using namespace script;
std::string input;
Span<const char> sp;
bool success;