diff --git a/src/Makefile.am b/src/Makefile.am index 36b91027127..15734762c0d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/util/error.cpp b/src/common/messages.cpp similarity index 57% rename from src/util/error.cpp rename to src/common/messages.cpp index 9d506ee25cf..5d68eb3d7fc 100644 --- a/src/util/error.cpp +++ b/src/common/messages.cpp @@ -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 +#include #include +#include #include +#include +#include #include #include +#include #include +#include +#include -using common::PSBTError; +namespace common { +std::string StringForFeeReason(FeeReason reason) +{ + static const std::map 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>& FeeModeMap() +{ + static const std::vector> 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& 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=: '%s'"), optname, strValue); } +} // namespace common diff --git a/src/common/messages.h b/src/common/messages.h new file mode 100644 index 00000000000..bdd853791d3 --- /dev/null +++ b/src/common/messages.h @@ -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 +#include + +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 diff --git a/src/init.cpp b/src/init.cpp index 51ade4de939..080c79f3be9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -115,6 +115,9 @@ #include #endif +using common::AmountErrMsg; +using common::InvalidPortErrMsg; +using common::ResolveErrMsg; using kernel::DumpMempool; using kernel::LoadMempool; using kernel::ValidationCacheSizes; diff --git a/src/net_permissions.cpp b/src/net_permissions.cpp index b01b2f643d3..8f0042c141f 100644 --- a/src/net_permissions.cpp +++ b/src/net_permissions.cpp @@ -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 #include #include #include -#include #include +using common::ResolveErrMsg; + const std::vector NET_PERMISSIONS_DOC{ "bloomfilter (allow requesting BIP37 filtered blocks and transactions)", "noban (do not ban for misbehavior; implies download)", diff --git a/src/node/mempool_args.cpp b/src/node/mempool_args.cpp index ac26600919f..f329affb1d8 100644 --- a/src/node/mempool_args.cpp +++ b/src/node/mempool_args.cpp @@ -8,19 +8,20 @@ #include #include +#include #include #include #include #include #include #include -#include #include #include #include #include +using common::AmountErrMsg; using kernel::MemPoolLimits; using kernel::MemPoolOptions; diff --git a/src/node/transaction.h b/src/node/transaction.h index 6782536aceb..5f524f4e28e 100644 --- a/src/node/transaction.h +++ b/src/node/transaction.h @@ -5,9 +5,9 @@ #ifndef BITCOIN_NODE_TRANSACTION_H #define BITCOIN_NODE_TRANSACTION_H +#include #include #include -#include class CBlockIndex; class CTxMemPool; diff --git a/src/qt/psbtoperationsdialog.cpp b/src/qt/psbtoperationsdialog.cpp index 7bbebec46c7..23bb4cc9f0f 100644 --- a/src/qt/psbtoperationsdialog.cpp +++ b/src/qt/psbtoperationsdialog.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -13,7 +14,6 @@ #include #include #include -#include #include #include @@ -21,6 +21,7 @@ #include #include +using common::TransactionErrorString; using node::AnalyzePSBT; using node::DEFAULT_MAX_RAW_TX_FEE_RATE; using node::PSBTAnalysis; diff --git a/src/rpc/fees.cpp b/src/rpc/fees.cpp index b933d8c6474..d74d77299f0 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 #include #include #include @@ -14,7 +15,6 @@ #include #include #include -#include #include #include @@ -22,6 +22,9 @@ #include #include +using common::FeeModeFromString; +using common::FeeModes; +using common::InvalidEstimateModeErrorMessage; using node::NodeContext; static RPCHelpMan estimatesmartfee() diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 9ae6dd98e97..f1cff640fa3 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include