mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-16 18:39:59 +01:00
Merge bitcoin/bitcoin#29015: kernel: Streamline util library
c7376babd1doc: Clarify distinction between util and common libraries in libraries.md (Ryan Ofsky)4f74c59334util: Move util/string.h functions to util namespace (Ryan Ofsky)4d05d3f3b4util: add TransactionError includes and namespace declarations (Ryan Ofsky)680eafdc74util: move fees.h and error.h to common/messages.h (Ryan Ofsky)02e62c6c9acommon: Add PSBTError enum (Ryan Ofsky)0d44c44ae3util: move error.h TransactionError enum to node/types.h (Ryan Ofsky)9bcce2608dutil: move spanparsing.h to script/parsing.h (Ryan Ofsky)6dd2ad4792util: move spanparsing.h Split functions to string.h (Ryan Ofsky)23cc8ddff4util: move HexStr and HexDigit from util to crypto (TheCharlatan)6861f954f8util: move util/message to common/signmessage (Ryan Ofsky)cc5f29fbeabuild: move memory_cleanse from util to crypto (Ryan Ofsky)5b9309420cbuild: move chainparamsbase from util to common (Ryan Ofsky)ffa27af24dtest: Add check-deps.sh script to check for unexpected library dependencies (Ryan Ofsky) Pull request description: Remove `fees.h`, `errors.h`, and `spanparsing.h` from the util library. Specifically: - Move `Split` functions from `util/spanparsing.h` to `util/string.h`, using `util` namespace for clarity. - Move remaining spanparsing functions to `script/parsing.h` since they are used for descriptor and miniscript parsing. - Combine `util/fees.h` and `util/errors.h` into `common/messages.h` so there is a place for simple functions that generate user messages to live, and these functions are not part of the util library. Motivation for this change is that the util library is a dependency of the kernel, and we should remove functionality from util that shouldn't be called by kernel code or kernel applications. These changes should also improve code organization and make functions easier to discover. Some of these same moves are (or were) part of #28690, but did not help with code organization, or made it worse, so it is better to move them and clean them up in the same PR so code only has to change one time. ACKs for top commit: achow101: ACKc7376babd1TheCharlatan: Re-ACKc7376babd1hebasto: re-ACKc7376babd1. Tree-SHA512: 5bcef16c1255463b1b69270548711e7ff78ca0dd34e300b95e3ca1ce52ceb34f83d9ddb2839e83800ba36b200de30396e504bbb04fa02c6d0c24a16d06ae523d
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using util::Split;
|
||||
|
||||
namespace {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
@@ -1350,8 +1352,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 +1424,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 +1587,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 +2036,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";
|
||||
|
||||
@@ -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>
|
||||
@@ -863,8 +863,8 @@ public:
|
||||
if (!key_str) return {};
|
||||
return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
|
||||
}
|
||||
case Fragment::AFTER: return std::move(ret) + "after(" + ::ToString(node.k) + ")";
|
||||
case Fragment::OLDER: return std::move(ret) + "older(" + ::ToString(node.k) + ")";
|
||||
case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
|
||||
case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
|
||||
case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
|
||||
case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
|
||||
case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
|
||||
@@ -883,7 +883,7 @@ public:
|
||||
return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
|
||||
case Fragment::MULTI: {
|
||||
CHECK_NONFATAL(!is_tapscript);
|
||||
auto str = std::move(ret) + "multi(" + ::ToString(node.k);
|
||||
auto str = std::move(ret) + "multi(" + util::ToString(node.k);
|
||||
for (const auto& key : node.keys) {
|
||||
auto key_str = ctx.ToString(key);
|
||||
if (!key_str) return {};
|
||||
@@ -893,7 +893,7 @@ public:
|
||||
}
|
||||
case Fragment::MULTI_A: {
|
||||
CHECK_NONFATAL(is_tapscript);
|
||||
auto str = std::move(ret) + "multi_a(" + ::ToString(node.k);
|
||||
auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
|
||||
for (const auto& key : node.keys) {
|
||||
auto key_str = ctx.ToString(key);
|
||||
if (!key_str) return {};
|
||||
@@ -902,7 +902,7 @@ public:
|
||||
return std::move(str) + ")";
|
||||
}
|
||||
case Fragment::THRESH: {
|
||||
auto str = std::move(ret) + "thresh(" + ::ToString(node.k);
|
||||
auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
|
||||
for (auto& sub : subs) {
|
||||
str += "," + std::move(sub);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
52
src/script/parsing.cpp
Normal file
52
src/script/parsing.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2018-2022 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/parsing.h>
|
||||
|
||||
#include <span.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
namespace script {
|
||||
|
||||
bool Const(const std::string& str, Span<const char>& sp)
|
||||
{
|
||||
if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
|
||||
sp = sp.subspan(str.size());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Func(const std::string& str, Span<const char>& sp)
|
||||
{
|
||||
if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) {
|
||||
sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Span<const char> Expr(Span<const char>& sp)
|
||||
{
|
||||
int level = 0;
|
||||
auto it = sp.begin();
|
||||
while (it != sp.end()) {
|
||||
if (*it == '(' || *it == '{') {
|
||||
++level;
|
||||
} else if (level && (*it == ')' || *it == '}')) {
|
||||
--level;
|
||||
} else if (level == 0 && (*it == ')' || *it == '}' || *it == ',')) {
|
||||
break;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
Span<const char> ret = sp.first(it - sp.begin());
|
||||
sp = sp.subspan(it - sp.begin());
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace script
|
||||
40
src/script/parsing.h
Normal file
40
src/script/parsing.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2018-2022 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_SCRIPT_PARSING_H
|
||||
#define BITCOIN_SCRIPT_PARSING_H
|
||||
|
||||
#include <span.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace script {
|
||||
|
||||
/** Parse a constant.
|
||||
*
|
||||
* If sp's initial part matches str, sp is updated to skip that part, and true is returned.
|
||||
* Otherwise sp is unmodified and false is returned.
|
||||
*/
|
||||
bool Const(const std::string& str, Span<const char>& sp);
|
||||
|
||||
/** Parse a function call.
|
||||
*
|
||||
* If sp's initial part matches str + "(", and sp ends with ")", sp is updated to be the
|
||||
* section between the braces, and true is returned. Otherwise sp is unmodified and false
|
||||
* is returned.
|
||||
*/
|
||||
bool Func(const std::string& str, Span<const char>& sp);
|
||||
|
||||
/** Extract the expression that sp begins with.
|
||||
*
|
||||
* This function will return the initial part of sp, up to (but not including) the first
|
||||
* comma or closing brace, skipping ones that are surrounded by braces. So for example,
|
||||
* for "foo(bar(1),2),3" the initial part "foo(bar(1),2)" will be returned. sp will be
|
||||
* updated to skip the initial part that is returned.
|
||||
*/
|
||||
Span<const char> Expr(Span<const char>& sp);
|
||||
|
||||
} // namespace script
|
||||
|
||||
#endif // BITCOIN_SCRIPT_PARSING_H
|
||||
@@ -6,10 +6,10 @@
|
||||
#include <script/script.h>
|
||||
|
||||
#include <crypto/common.h>
|
||||
#include <crypto/hex_base.h>
|
||||
#include <hash.h>
|
||||
#include <uint256.h>
|
||||
#include <util/hash_type.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user