aboutsummaryrefslogtreecommitdiff
path: root/src/rpc
diff options
context:
space:
mode:
authorAva Chow <github@achow101.com>2024-06-12 17:12:54 -0400
committerAva Chow <github@achow101.com>2024-06-12 17:12:54 -0400
commit011a895a82df8971b7fed4fcd090afb638c4c0d7 (patch)
treef303bf5acf0fe27a3253173e6cb5a9c8e613760b /src/rpc
parenta7bc9b76e73f04dfe4d6ba42033fe38659090e8b (diff)
parentc7376babd19d0c858fef93ebd58338abd530c1f4 (diff)
downloadbitcoin-011a895a82df8971b7fed4fcd090afb638c4c0d7.tar.xz
Merge bitcoin/bitcoin#29015: kernel: Streamline util library
c7376babd19d0c858fef93ebd58338abd530c1f4 doc: Clarify distinction between util and common libraries in libraries.md (Ryan Ofsky) 4f74c59334d496f28e1a5c0d84c412f9020b366f util: Move util/string.h functions to util namespace (Ryan Ofsky) 4d05d3f3b42a41525aa6ec44b90f543dfab53ecf util: add TransactionError includes and namespace declarations (Ryan Ofsky) 680eafdc74021c1e0893c3a62404e607fd4724f5 util: move fees.h and error.h to common/messages.h (Ryan Ofsky) 02e62c6c9af4beabaeea58fb1ea3ad0dc5094678 common: Add PSBTError enum (Ryan Ofsky) 0d44c44ae33434f366229c612d6edeedf7658963 util: move error.h TransactionError enum to node/types.h (Ryan Ofsky) 9bcce2608dd2515dc35a0f0866abc9d43903c795 util: move spanparsing.h to script/parsing.h (Ryan Ofsky) 6dd2ad47922694d2ab84bad4dac9dd442c5df617 util: move spanparsing.h Split functions to string.h (Ryan Ofsky) 23cc8ddff472d259605d7790ba98a1900e77efab util: move HexStr and HexDigit from util to crypto (TheCharlatan) 6861f954f8ff42c87ad638037adae86a5bd89600 util: move util/message to common/signmessage (Ryan Ofsky) cc5f29fbea15d33e4d1aa95591253c6b86953fe7 build: move memory_cleanse from util to crypto (Ryan Ofsky) 5b9309420cc9721a0d5745b6ad3166a4bdbd1508 build: move chainparamsbase from util to common (Ryan Ofsky) ffa27af24da81a97d6c4912ae0e10bc5b6f17f69 test: 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: ACK c7376babd19d0c858fef93ebd58338abd530c1f4 TheCharlatan: Re-ACK c7376babd19d0c858fef93ebd58338abd530c1f4 hebasto: re-ACK c7376babd19d0c858fef93ebd58338abd530c1f4. Tree-SHA512: 5bcef16c1255463b1b69270548711e7ff78ca0dd34e300b95e3ca1ce52ceb34f83d9ddb2839e83800ba36b200de30396e504bbb04fa02c6d0c24a16d06ae523d
Diffstat (limited to 'src/rpc')
-rw-r--r--src/rpc/blockchain.cpp3
-rw-r--r--src/rpc/fees.cpp5
-rw-r--r--src/rpc/mempool.cpp3
-rw-r--r--src/rpc/mining.cpp1
-rw-r--r--src/rpc/net.cpp2
-rw-r--r--src/rpc/rawtransaction.cpp6
-rw-r--r--src/rpc/server.cpp2
-rw-r--r--src/rpc/signmessage.cpp2
-rw-r--r--src/rpc/util.cpp37
-rw-r--r--src/rpc/util.h12
10 files changed, 57 insertions, 16 deletions
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 3e3e91927c..46b0ae161f 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -61,6 +61,9 @@ using kernel::CoinStatsHashType;
using node::BlockManager;
using node::NodeContext;
using node::SnapshotMetadata;
+using util::Join;
+using util::MakeUnorderedList;
+using util::ToString;
struct CUpdatedBlock
{
diff --git a/src/rpc/fees.cpp b/src/rpc/fees.cpp
index a7cec96746..aefe78162b 100644
--- a/src/rpc/fees.cpp
+++ b/src/rpc/fees.cpp
@@ -3,6 +3,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <common/messages.h>
#include <core_io.h>
#include <node/context.h>
#include <policy/feerate.h>
@@ -14,7 +15,6 @@
#include <rpc/util.h>
#include <txmempool.h>
#include <univalue.h>
-#include <util/fees.h>
#include <validationinterface.h>
#include <algorithm>
@@ -22,6 +22,9 @@
#include <cmath>
#include <string>
+using common::FeeModeFromString;
+using common::FeeModes;
+using common::InvalidEstimateModeErrorMessage;
using node::NodeContext;
static RPCHelpMan estimatesmartfee()
diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp
index 4a0e96fd75..fd11f6cfeb 100644
--- a/src/rpc/mempool.cpp
+++ b/src/rpc/mempool.cpp
@@ -11,6 +11,7 @@
#include <core_io.h>
#include <kernel/mempool_entry.h>
#include <node/mempool_persist_args.h>
+#include <node/types.h>
#include <policy/rbf.h>
#include <policy/settings.h>
#include <primitives/transaction.h>
@@ -32,6 +33,8 @@ using node::DEFAULT_MAX_BURN_AMOUNT;
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
using node::MempoolPath;
using node::NodeContext;
+using node::TransactionError;
+using util::ToString;
static RPCHelpMan sendrawtransaction()
{
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 63daa3da3a..6412fb35ec 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -47,6 +47,7 @@ using node::CBlockTemplate;
using node::NodeContext;
using node::RegenerateCommitments;
using node::UpdateTime;
+using util::ToString;
/**
* Return average network hashes per second based on the last 'lookup' blocks,
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index 034dbdc914..1cc55f891a 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -35,6 +35,8 @@
#include <univalue.h>
using node::NodeContext;
+using util::Join;
+using util::TrimString;
const std::vector<std::string> CONNECTION_TYPE_DOC{
"outbound-full-relay (default automatic connections)",
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index f45c921c3c..75b538061d 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -16,6 +16,7 @@
#include <node/context.h>
#include <node/psbt.h>
#include <node/transaction.h>
+#include <node/types.h>
#include <policy/packages.h>
#include <policy/policy.h>
#include <policy/rbf.h>
@@ -1489,9 +1490,8 @@ static RPCHelpMan combinepsbt()
}
PartiallySignedTransaction merged_psbt;
- const TransactionError error = CombinePSBTs(merged_psbt, psbtxs);
- if (error != TransactionError::OK) {
- throw JSONRPCTransactionError(error);
+ if (!CombinePSBTs(merged_psbt, psbtxs)) {
+ throw JSONRPCError(RPC_INVALID_PARAMETER, "PSBTs not compatible (different transactions)");
}
DataStream ssTx{};
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 838068bc19..19063fa5be 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -27,6 +27,8 @@
#include <mutex>
#include <unordered_map>
+using util::SplitString;
+
static GlobalMutex g_rpc_warmup_mutex;
static std::atomic<bool> g_rpc_running{false};
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
diff --git a/src/rpc/signmessage.cpp b/src/rpc/signmessage.cpp
index 9f3c24efcf..83462738c5 100644
--- a/src/rpc/signmessage.cpp
+++ b/src/rpc/signmessage.cpp
@@ -3,6 +3,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <common/signmessage.h>
#include <key.h>
#include <key_io.h>
#include <rpc/protocol.h>
@@ -10,7 +11,6 @@
#include <rpc/server.h>
#include <rpc/util.h>
#include <univalue.h>
-#include <util/message.h>
#include <string>
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 9123bddff4..bb1aef63f4 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -7,9 +7,12 @@
#include <clientversion.h>
#include <core_io.h>
#include <common/args.h>
+#include <common/messages.h>
+#include <common/types.h>
#include <consensus/amount.h>
#include <script/interpreter.h>
#include <key_io.h>
+#include <node/types.h>
#include <outputtype.h>
#include <rpc/util.h>
#include <script/descriptor.h>
@@ -30,6 +33,14 @@
#include <tuple>
#include <utility>
+using common::PSBTError;
+using common::PSBTErrorString;
+using common::TransactionErrorString;
+using node::TransactionError;
+using util::Join;
+using util::SplitString;
+using util::TrimString;
+
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
@@ -364,6 +375,18 @@ unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
return unsigned_target;
}
+RPCErrorCode RPCErrorFromPSBTError(PSBTError err)
+{
+ switch (err) {
+ case PSBTError::UNSUPPORTED:
+ return RPC_INVALID_PARAMETER;
+ case PSBTError::SIGHASH_MISMATCH:
+ return RPC_DESERIALIZATION_ERROR;
+ default: break;
+ }
+ return RPC_TRANSACTION_ERROR;
+}
+
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
{
switch (terr) {
@@ -371,18 +394,16 @@ RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
return RPC_TRANSACTION_REJECTED;
case TransactionError::ALREADY_IN_CHAIN:
return RPC_TRANSACTION_ALREADY_IN_CHAIN;
- case TransactionError::P2P_DISABLED:
- return RPC_CLIENT_P2P_DISABLED;
- case TransactionError::INVALID_PSBT:
- case TransactionError::PSBT_MISMATCH:
- return RPC_INVALID_PARAMETER;
- case TransactionError::SIGHASH_MISMATCH:
- return RPC_DESERIALIZATION_ERROR;
default: break;
}
return RPC_TRANSACTION_ERROR;
}
+UniValue JSONRPCPSBTError(PSBTError err)
+{
+ return JSONRPCError(RPCErrorFromPSBTError(err), PSBTErrorString(err).original);
+}
+
UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
{
if (err_string.length() > 0) {
@@ -778,7 +799,7 @@ std::string RPCHelpMan::ToString() const
if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
// Push named argument name and description
- sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
+ sections.m_sections.emplace_back(util::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
// Recursively push nested args
diff --git a/src/rpc/util.h b/src/rpc/util.h
index 51ecaff13c..ca6ca6007b 100644
--- a/src/rpc/util.h
+++ b/src/rpc/util.h
@@ -34,9 +34,14 @@
class JSONRPCRequest;
enum ServiceFlags : uint64_t;
enum class OutputType;
-enum class TransactionError;
struct FlatSigningProvider;
struct bilingual_str;
+namespace common {
+enum class PSBTError;
+} // namespace common
+namespace node {
+enum class TransactionError;
+} // namespace node
static constexpr bool DEFAULT_RPC_DOC_CHECK{
#ifdef RPC_DOC_CHECK
@@ -127,8 +132,9 @@ int ParseSighashString(const UniValue& sighash);
//! Parse a confirm target option and raise an RPC error if it is invalid.
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target);
-RPCErrorCode RPCErrorFromTransactionError(TransactionError terr);
-UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = "");
+RPCErrorCode RPCErrorFromTransactionError(node::TransactionError terr);
+UniValue JSONRPCPSBTError(common::PSBTError err);
+UniValue JSONRPCTransactionError(node::TransactionError terr, const std::string& err_string = "");
//! Parse a JSON range specified as int64, or [int64, int64]
std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value);