Skip to content

Commit 9bcce26

Browse files
committedMay 16, 2024
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.
1 parent 6dd2ad4 commit 9bcce26

9 files changed

+27
-40
lines changed
 

‎src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ BITCOIN_CORE_H = \
268268
script/descriptor.h \
269269
script/keyorigin.h \
270270
script/miniscript.h \
271+
script/parsing.h \
271272
script/sigcache.h \
272273
script/sign.h \
273274
script/signingprovider.h \
@@ -318,7 +319,6 @@ BITCOIN_CORE_H = \
318319
util/serfloat.h \
319320
util/signalinterrupt.h \
320321
util/sock.h \
321-
util/spanparsing.h \
322322
util/strencodings.h \
323323
util/string.h \
324324
util/subprocess.h \
@@ -711,6 +711,7 @@ libbitcoin_common_a_SOURCES = \
711711
scheduler.cpp \
712712
script/descriptor.cpp \
713713
script/miniscript.cpp \
714+
script/parsing.cpp \
714715
script/sign.cpp \
715716
script/signingprovider.cpp \
716717
script/solver.cpp \
@@ -752,7 +753,6 @@ libbitcoin_util_a_SOURCES = \
752753
util/threadinterrupt.cpp \
753754
util/threadnames.cpp \
754755
util/serfloat.cpp \
755-
util/spanparsing.cpp \
756756
util/strencodings.cpp \
757757
util/string.cpp \
758758
util/time.cpp \

‎src/Makefile.test.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ test_fuzz_fuzz_SOURCES = \
371371
test/fuzz/script_format.cpp \
372372
test/fuzz/script_interpreter.cpp \
373373
test/fuzz/script_ops.cpp \
374+
test/fuzz/script_parsing.cpp \
374375
test/fuzz/script_sigcache.cpp \
375376
test/fuzz/script_sign.cpp \
376377
test/fuzz/scriptnum_ops.cpp \
@@ -380,7 +381,6 @@ test_fuzz_fuzz_SOURCES = \
380381
test/fuzz/signet.cpp \
381382
test/fuzz/socks5.cpp \
382383
test/fuzz/span.cpp \
383-
test/fuzz/spanparsing.cpp \
384384
test/fuzz/string.cpp \
385385
test/fuzz/strprintf.cpp \
386386
test/fuzz/system.cpp \

‎src/i2p.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include <netaddress.h>
1313
#include <netbase.h>
1414
#include <random.h>
15+
#include <script/parsing.h>
1516
#include <sync.h>
1617
#include <tinyformat.h>
1718
#include <util/fs.h>
1819
#include <util/readwritefile.h>
1920
#include <util/sock.h>
20-
#include <util/spanparsing.h>
2121
#include <util/strencodings.h>
2222
#include <util/threadinterrupt.h>
2323

@@ -308,7 +308,7 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
308308

309309
reply.full = sock.RecvUntilTerminator('\n', recv_timeout, *m_interrupt, MAX_MSG_SIZE);
310310

311-
for (const auto& kv : spanparsing::Split(reply.full, ' ')) {
311+
for (const auto& kv : Split(reply.full, ' ')) {
312312
const auto& pos = std::find(kv.begin(), kv.end(), '=');
313313
if (pos != kv.end()) {
314314
reply.keys.emplace(std::string{kv.begin(), pos}, std::string{pos + 1, kv.end()});

‎src/script/descriptor.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <key_io.h>
99
#include <pubkey.h>
1010
#include <script/miniscript.h>
11+
#include <script/parsing.h>
1112
#include <script/script.h>
1213
#include <script/signingprovider.h>
1314
#include <script/solver.h>
@@ -17,7 +18,6 @@
1718
#include <span.h>
1819
#include <util/bip32.h>
1920
#include <util/check.h>
20-
#include <util/spanparsing.h>
2121
#include <util/strencodings.h>
2222
#include <util/vector.h>
2323

@@ -1350,8 +1350,6 @@ enum class ParseScriptContext {
13501350
/** Parse a public key that excludes origin information. */
13511351
std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error)
13521352
{
1353-
using namespace spanparsing;
1354-
13551353
bool permit_uncompressed = ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH;
13561354
auto split = Split(sp, '/');
13571355
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
14241422
/** Parse a public key including origin information (if enabled). */
14251423
std::unique_ptr<PubkeyProvider> ParsePubkey(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
14261424
{
1427-
using namespace spanparsing;
1428-
14291425
auto origin_split = Split(sp, ']');
14301426
if (origin_split.size() > 2) {
14311427
error = "Multiple ']' characters found for a single pubkey";
@@ -1589,7 +1585,7 @@ struct KeyParser {
15891585
// NOLINTNEXTLINE(misc-no-recursion)
15901586
std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
15911587
{
1592-
using namespace spanparsing;
1588+
using namespace script;
15931589

15941590
auto expr = Expr(sp);
15951591
if (Func("pk", expr)) {
@@ -2038,8 +2034,6 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
20382034
/** Check a descriptor checksum, and update desc to be the checksum-less part. */
20392035
bool CheckChecksum(Span<const char>& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr)
20402036
{
2041-
using namespace spanparsing;
2042-
20432037
auto check_split = Split(sp, '#');
20442038
if (check_split.size() > 2) {
20452039
error = "Multiple '#' symbols";

‎src/script/miniscript.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
#include <policy/policy.h>
2020
#include <primitives/transaction.h>
21+
#include <script/parsing.h>
2122
#include <script/script.h>
2223
#include <span.h>
2324
#include <util/check.h>
24-
#include <util/spanparsing.h>
2525
#include <util/strencodings.h>
2626
#include <util/string.h>
2727
#include <util/vector.h>
@@ -1764,7 +1764,7 @@ void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector<Node
17641764
template<typename Key, typename Ctx>
17651765
inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
17661766
{
1767-
using namespace spanparsing;
1767+
using namespace script;
17681768

17691769
// Account for the minimum script size for all parsed fragments so far. It "borrows" 1
17701770
// script byte from all leaf nodes, counting it instead whenever a space for a recursive

‎src/util/spanparsing.cpp ‎src/script/parsing.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
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 <util/spanparsing.h>
5+
#include <script/parsing.h>
66

77
#include <span.h>
88

99
#include <algorithm>
1010
#include <cstddef>
1111
#include <string>
1212

13-
namespace spanparsing {
13+
namespace script {
1414

1515
bool Const(const std::string& str, Span<const char>& sp)
1616
{
@@ -49,4 +49,4 @@ Span<const char> Expr(Span<const char>& sp)
4949
return ret;
5050
}
5151

52-
} // namespace spanparsing
52+
} // namespace script

‎src/util/spanparsing.h ‎src/script/parsing.h

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_UTIL_SPANPARSING_H
6-
#define BITCOIN_UTIL_SPANPARSING_H
5+
#ifndef BITCOIN_SCRIPT_PARSING_H
6+
#define BITCOIN_SCRIPT_PARSING_H
77

88
#include <span.h>
9-
#include <util/string.h>
109

1110
#include <string>
1211

13-
namespace spanparsing {
12+
namespace script {
1413

1514
/** Parse a constant.
1615
*
@@ -36,13 +35,6 @@ bool Func(const std::string& str, Span<const char>& sp);
3635
*/
3736
Span<const char> Expr(Span<const char>& sp);
3837

39-
/** Split alias for backwards compatibility */
40-
template <typename... Args>
41-
auto Split(Args&&... args)
42-
{
43-
return ::Split(std::forward<Args>(args)...);
44-
}
38+
} // namespace script
4539

46-
} // namespace spanparsing
47-
48-
#endif // BITCOIN_UTIL_SPANPARSING_H
40+
#endif // BITCOIN_SCRIPT_PARSING_H

‎src/test/fuzz/spanparsing.cpp ‎src/test/fuzz/script_parsing.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
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 <script/parsing.h>
56
#include <test/fuzz/FuzzedDataProvider.h>
67
#include <test/fuzz/fuzz.h>
7-
#include <util/spanparsing.h>
8+
#include <util/string.h>
89

9-
FUZZ_TARGET(spanparsing)
10+
FUZZ_TARGET(script_parsing)
1011
{
1112
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
1213
const size_t query_size = fuzzed_data_provider.ConsumeIntegral<size_t>();
@@ -15,16 +16,16 @@ FUZZ_TARGET(spanparsing)
1516
const Span<const char> const_span{span_str};
1617

1718
Span<const char> mut_span = const_span;
18-
(void)spanparsing::Const(query, mut_span);
19+
(void)script::Const(query, mut_span);
1920

2021
mut_span = const_span;
21-
(void)spanparsing::Func(query, mut_span);
22+
(void)script::Func(query, mut_span);
2223

2324
mut_span = const_span;
24-
(void)spanparsing::Expr(mut_span);
25+
(void)script::Expr(mut_span);
2526

2627
if (!query.empty()) {
2728
mut_span = const_span;
28-
(void)spanparsing::Split(mut_span, query.front());
29+
(void)Split(mut_span, query.front());
2930
}
3031
}

‎src/test/util_tests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <common/signmessage.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
77
#include <hash.h> // For Hash()
88
#include <key.h> // For CKey
9+
#include <script/parsing.h>
910
#include <sync.h>
1011
#include <test/util/random.h>
1112
#include <test/util/setup_common.h>
@@ -16,7 +17,6 @@
1617
#include <util/moneystr.h>
1718
#include <util/overflow.h>
1819
#include <util/readwritefile.h>
19-
#include <util/spanparsing.h>
2020
#include <util/strencodings.h>
2121
#include <util/string.h>
2222
#include <util/time.h>
@@ -1292,9 +1292,9 @@ static std::string SpanToStr(const Span<const char>& span)
12921292
return std::string(span.begin(), span.end());
12931293
}
12941294

1295-
BOOST_AUTO_TEST_CASE(test_spanparsing)
1295+
BOOST_AUTO_TEST_CASE(test_script_parsing)
12961296
{
1297-
using namespace spanparsing;
1297+
using namespace script;
12981298
std::string input;
12991299
Span<const char> sp;
13001300
bool success;

0 commit comments

Comments
 (0)
Please sign in to comment.