util: move fees.h and error.h to common/messages.h

Move enum and message formatting functions to a common/messages header where
they should be more discoverable, and also out of the util library, so they
will not be a dependency of the kernel

The are no changes in behavior and no changes to the moved code.
This commit is contained in:
Ryan Ofsky 2023-12-06 16:43:49 -05:00
parent 02e62c6c9a
commit 680eafdc74
20 changed files with 143 additions and 143 deletions

View file

@ -144,6 +144,7 @@ BITCOIN_CORE_H = \
compat/compat.h \
compat/cpuid.h \
compat/endian.h \
common/messages.h \
common/settings.h \
common/signmessage.h \
common/system.h \
@ -300,11 +301,9 @@ BITCOIN_CORE_H = \
util/chaintype.h \
util/check.h \
util/epochguard.h \
util/error.h \
util/exception.h \
util/fastrange.h \
util/feefrac.h \
util/fees.h \
util/fs.h \
util/fs_helpers.h \
util/golombrice.h \
@ -681,6 +680,7 @@ libbitcoin_common_a_SOURCES = \
common/config.cpp \
common/init.cpp \
common/interfaces.cpp \
common/messages.cpp \
common/run_command.cpp \
common/settings.cpp \
common/signmessage.cpp \
@ -738,10 +738,8 @@ libbitcoin_util_a_SOURCES = \
util/bytevectorhash.cpp \
util/chaintype.cpp \
util/check.cpp \
util/error.cpp \
util/exception.cpp \
util/feefrac.cpp \
util/fees.cpp \
util/fs.cpp \
util/fs_helpers.cpp \
util/hasher.cpp \

View file

@ -1,17 +1,75 @@
// Copyright (c) 2010-2022 The Bitcoin Core developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-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 <util/error.h>
#include <common/messages.h>
#include <common/types.h>
#include <policy/fees.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
#include <cassert>
#include <map>
#include <string>
#include <utility>
#include <vector>
using common::PSBTError;
namespace common {
std::string StringForFeeReason(FeeReason reason)
{
static const std::map<FeeReason, std::string> fee_reason_strings = {
{FeeReason::NONE, "None"},
{FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
{FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
{FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
{FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
{FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
{FeeReason::PAYTXFEE, "PayTxFee set"},
{FeeReason::FALLBACK, "Fallback fee"},
{FeeReason::REQUIRED, "Minimum Required Fee"},
};
auto reason_string = fee_reason_strings.find(reason);
if (reason_string == fee_reason_strings.end()) return "Unknown";
return reason_string->second;
}
const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
{
static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
{"unset", FeeEstimateMode::UNSET},
{"economical", FeeEstimateMode::ECONOMICAL},
{"conservative", FeeEstimateMode::CONSERVATIVE},
};
return FEE_MODES;
}
std::string FeeModes(const std::string& delimiter)
{
return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
}
std::string InvalidEstimateModeErrorMessage()
{
return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
}
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode)
{
auto searchkey = ToUpper(mode_string);
for (const auto& pair : FeeModeMap()) {
if (ToUpper(pair.first) == searchkey) {
fee_estimate_mode = pair.second;
return true;
}
}
return false;
}
bilingual_str PSBTErrorString(PSBTError err)
{
@ -74,3 +132,4 @@ bilingual_str AmountErrMsg(const std::string& optname, const std::string& strVal
{
return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
}
} // namespace common

36
src/common/messages.h Normal file
View file

@ -0,0 +1,36 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//! @file common/messages.h is a home for simple string functions returning
//! descriptive messages that are used in RPC and GUI interfaces or log
//! messages, and are called in different parts of the codebase across
//! node/wallet/gui boundaries.
#ifndef BITCOIN_COMMON_MESSAGES_H
#define BITCOIN_COMMON_MESSAGES_H
#include <node/types.h>
#include <string>
struct bilingual_str;
enum class FeeEstimateMode;
enum class FeeReason;
namespace common {
enum class PSBTError;
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
std::string StringForFeeReason(FeeReason reason);
std::string FeeModes(const std::string& delimiter);
std::string InvalidEstimateModeErrorMessage();
bilingual_str PSBTErrorString(PSBTError error);
bilingual_str TransactionErrorString(const TransactionError error);
bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind);
bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& strPort);
bilingual_str AmountHighWarn(const std::string& optname);
bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue);
} // namespace common
#endif // BITCOIN_COMMON_MESSAGES_H

View file

@ -115,6 +115,9 @@
#include <zmq/zmqrpc.h>
#endif
using common::AmountErrMsg;
using common::InvalidPortErrMsg;
using common::ResolveErrMsg;
using kernel::DumpMempool;
using kernel::LoadMempool;
using kernel::ValidationCacheSizes;

View file

@ -2,12 +2,14 @@
// 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 <common/system.h>
#include <net_permissions.h>
#include <netbase.h>
#include <util/error.h>
#include <util/translation.h>
using common::ResolveErrMsg;
const std::vector<std::string> NET_PERMISSIONS_DOC{
"bloomfilter (allow requesting BIP37 filtered blocks and transactions)",
"noban (do not ban for misbehavior; implies download)",

View file

@ -8,19 +8,20 @@
#include <kernel/mempool_options.h>
#include <common/args.h>
#include <common/messages.h>
#include <consensus/amount.h>
#include <kernel/chainparams.h>
#include <logging.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <tinyformat.h>
#include <util/error.h>
#include <util/moneystr.h>
#include <util/translation.h>
#include <chrono>
#include <memory>
using common::AmountErrMsg;
using kernel::MemPoolLimits;
using kernel::MemPoolOptions;

View file

@ -5,9 +5,9 @@
#ifndef BITCOIN_NODE_TRANSACTION_H
#define BITCOIN_NODE_TRANSACTION_H
#include <common/messages.h>
#include <policy/feerate.h>
#include <primitives/transaction.h>
#include <util/error.h>
class CBlockIndex;
class CTxMemPool;

View file

@ -4,6 +4,7 @@
#include <qt/psbtoperationsdialog.h>
#include <common/messages.h>
#include <core_io.h>
#include <interfaces/node.h>
#include <key_io.h>
@ -13,7 +14,6 @@
#include <qt/forms/ui_psbtoperationsdialog.h>
#include <qt/guiutil.h>
#include <qt/optionsmodel.h>
#include <util/error.h>
#include <util/fs.h>
#include <util/strencodings.h>
@ -21,6 +21,7 @@
#include <iostream>
#include <string>
using common::TransactionErrorString;
using node::AnalyzePSBT;
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
using node::PSBTAnalysis;

View file

@ -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()

View file

@ -7,6 +7,7 @@
#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>
@ -32,6 +33,8 @@
#include <utility>
using common::PSBTError;
using common::PSBTErrorString;
using common::TransactionErrorString;
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};

View file

@ -2,17 +2,19 @@
// 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 <consensus/amount.h>
#include <policy/fees.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/fees.h>
#include <cstdint>
#include <string>
#include <vector>
using common::StringForFeeReason;
FUZZ_TARGET(fees)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());

View file

@ -2,13 +2,13 @@
// 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 <merkleblock.h>
#include <policy/fees.h>
#include <rpc/util.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/error.h>
#include <util/translation.h>
#include <array>
@ -16,6 +16,8 @@
#include <optional>
#include <vector>
using common::TransactionErrorString;
namespace {
constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
TransactionError::MISSING_INPUTS,

View file

@ -5,6 +5,7 @@
#include <blockfilter.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/messages.h>
#include <common/settings.h>
#include <common/system.h>
#include <common/url.h>
@ -21,8 +22,6 @@
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/error.h>
#include <util/fees.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
@ -37,6 +36,11 @@
enum class FeeEstimateMode;
using common::AmountErrMsg;
using common::AmountHighWarn;
using common::FeeModeFromString;
using common::ResolveErrMsg;
FUZZ_TARGET(string)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());

View file

@ -1,38 +0,0 @@
// Copyright (c) 2010-2021 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_UTIL_ERROR_H
#define BITCOIN_UTIL_ERROR_H
/**
* util/error.h is a common place for definitions of simple error types and
* string functions. Types and functions defined here should not require any
* outside dependencies.
*
* Error types defined here can be used in different parts of the
* codebase, to avoid the need to write boilerplate code catching and
* translating errors passed across wallet/node/rpc/gui code boundaries.
*/
#include <node/types.h>
#include <string>
struct bilingual_str;
namespace common {
enum class PSBTError;
} // namespace common
bilingual_str PSBTErrorString(common::PSBTError err);
bilingual_str TransactionErrorString(const TransactionError error);
bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind);
bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& strPort);
bilingual_str AmountHighWarn(const std::string& optname);
bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue);
#endif // BITCOIN_UTIL_ERROR_H

View file

@ -1,67 +0,0 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2020 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 <util/fees.h>
#include <policy/fees.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <map>
#include <string>
#include <vector>
#include <utility>
std::string StringForFeeReason(FeeReason reason)
{
static const std::map<FeeReason, std::string> fee_reason_strings = {
{FeeReason::NONE, "None"},
{FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
{FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
{FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
{FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
{FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
{FeeReason::PAYTXFEE, "PayTxFee set"},
{FeeReason::FALLBACK, "Fallback fee"},
{FeeReason::REQUIRED, "Minimum Required Fee"},
};
auto reason_string = fee_reason_strings.find(reason);
if (reason_string == fee_reason_strings.end()) return "Unknown";
return reason_string->second;
}
const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
{
static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
{"unset", FeeEstimateMode::UNSET},
{"economical", FeeEstimateMode::ECONOMICAL},
{"conservative", FeeEstimateMode::CONSERVATIVE},
};
return FEE_MODES;
}
std::string FeeModes(const std::string& delimiter)
{
return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
}
std::string InvalidEstimateModeErrorMessage()
{
return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
}
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode)
{
auto searchkey = ToUpper(mode_string);
for (const auto& pair : FeeModeMap()) {
if (ToUpper(pair.first) == searchkey) {
fee_estimate_mode = pair.second;
return true;
}
}
return false;
}

View file

@ -1,18 +0,0 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2020 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_UTIL_FEES_H
#define BITCOIN_UTIL_FEES_H
#include <string>
enum class FeeEstimateMode;
enum class FeeReason;
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
std::string StringForFeeReason(FeeReason reason);
std::string FeeModes(const std::string& delimiter);
std::string InvalidEstimateModeErrorMessage();
#endif // BITCOIN_UTIL_FEES_H

View file

@ -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/messages.h>
#include <consensus/validation.h>
#include <core_io.h>
#include <key_io.h>
@ -9,7 +10,6 @@
#include <rpc/rawtransaction_util.h>
#include <rpc/util.h>
#include <script/script.h>
#include <util/fees.h>
#include <util/rbf.h>
#include <util/translation.h>
#include <util/vector.h>
@ -22,6 +22,11 @@
#include <univalue.h>
using common::FeeModeFromString;
using common::FeeModes;
using common::InvalidEstimateModeErrorMessage;
using common::StringForFeeReason;
using common::TransactionErrorString;
namespace wallet {
std::vector<CRecipient> CreateRecipients(const std::vector<std::pair<CTxDestination, CAmount>>& outputs, const std::set<int>& subtract_fee_outputs)

View file

@ -6,6 +6,7 @@
#define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
#include <addresstype.h>
#include <common/messages.h>
#include <common/signmessage.h>
#include <common/types.h>
#include <logging.h>
@ -13,7 +14,6 @@
#include <script/descriptor.h>
#include <script/script.h>
#include <script/signingprovider.h>
#include <util/error.h>
#include <util/result.h>
#include <util/time.h>
#include <wallet/crypter.h>

View file

@ -4,6 +4,7 @@
#include <algorithm>
#include <common/args.h>
#include <common/messages.h>
#include <common/system.h>
#include <consensus/amount.h>
#include <consensus/validation.h>
@ -15,7 +16,6 @@
#include <script/signingprovider.h>
#include <script/solver.h>
#include <util/check.h>
#include <util/fees.h>
#include <util/moneystr.h>
#include <util/rbf.h>
#include <util/trace.h>
@ -29,6 +29,8 @@
#include <cmath>
using common::StringForFeeReason;
using common::TransactionErrorString;
using interfaces::FoundBlock;
namespace wallet {

View file

@ -11,6 +11,7 @@
#include <chain.h>
#include <coins.h>
#include <common/args.h>
#include <common/messages.h>
#include <common/settings.h>
#include <common/signmessage.h>
#include <common/system.h>
@ -50,7 +51,6 @@
#include <uint256.h>
#include <univalue.h>
#include <util/check.h>
#include <util/error.h>
#include <util/fs.h>
#include <util/fs_helpers.h>
#include <util/moneystr.h>
@ -81,6 +81,8 @@
struct KeyOriginInfo;
using common::AmountErrMsg;
using common::AmountHighWarn;
using common::PSBTError;
using interfaces::FoundBlock;