Skip to content

Commit

Permalink
util: move fees.h and error.h to common/messages.h
Browse files Browse the repository at this point in the history
Move enum and message formatting functions to a common/messages where they
should be more discoverable, and also not 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.
  • Loading branch information
ryanofsky committed Dec 7, 2023
1 parent 78e9278 commit 778c021
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 141 deletions.
6 changes: 2 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ BITCOIN_CORE_H = \
compat/compat.h \
compat/cpuid.h \
compat/endian.h \
common/messages.h \
common/settings.h \
common/system.h \
compressor.h \
Expand Down Expand Up @@ -300,10 +301,8 @@ BITCOIN_CORE_H = \
util/chaintype.h \
util/check.h \
util/epochguard.h \
util/error.h \
util/exception.h \
util/fastrange.h \
util/fees.h \
util/fs.h \
util/fs_helpers.h \
util/getuniquepath.h \
Expand Down Expand Up @@ -677,6 +676,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/system.cpp \
Expand Down Expand Up @@ -738,9 +738,7 @@ libbitcoin_util_a_SOURCES = \
util/bytevectorhash.cpp \
util/chaintype.cpp \
util/check.cpp \
util/error.cpp \
util/exception.cpp \
util/fees.cpp \
util/fs.cpp \
util/fs_helpers.cpp \
util/getuniquepath.cpp \
Expand Down
65 changes: 63 additions & 2 deletions src/util/error.cpp → src/common/messages.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
// 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 <node/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 node::TransactionError;

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 util::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 TransactionErrorString(const TransactionError err)
{
switch (err) {
Expand Down Expand Up @@ -68,3 +128,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 changes: 36 additions & 0 deletions src/common/messages.h
Original file line number Diff line number Diff line change
@@ -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 <string>

struct bilingual_str;

enum class FeeEstimateMode;
enum class FeeReason;
namespace node {
enum class TransactionError;
} // namespace node

namespace common {
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 TransactionErrorString(const node::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
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
#include <zmq/zmqrpc.h>
#endif

using common::AmountErrMsg;
using common::InvalidPortErrMsg;
using common::ResolveErrMsg;

using kernel::DumpMempool;
using kernel::LoadMempool;
using kernel::ValidationCacheSizes;
Expand Down
4 changes: 2 additions & 2 deletions src/net_permissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// 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>

const std::vector<std::string> NET_PERMISSIONS_DOC{
Expand Down Expand Up @@ -90,7 +90,7 @@ bool NetWhitebindPermissions::TryParse(const std::string& str, NetWhitebindPermi
const std::string strBind = str.substr(offset);
const std::optional<CService> addrBind{Lookup(strBind, 0, false)};
if (!addrBind.has_value()) {
error = ResolveErrMsg("whitebind", strBind);
error = common::ResolveErrMsg("whitebind", strBind);
return false;
}
if (addrBind.value().GetPort() == 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/node/mempool_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/node/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/qt/psbtoperationsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/rpc/fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <policy/feerate.h>
#include <policy/fees.h>
Expand All @@ -13,7 +14,6 @@
#include <rpc/util.h>
#include <txmempool.h>
#include <univalue.h>
#include <util/fees.h>
#include <validationinterface.h>

#include <algorithm>
Expand All @@ -25,6 +25,9 @@ namespace node {
struct NodeContext;
}

using common::FeeModeFromString;
using common::FeeModes;
using common::InvalidEstimateModeErrorMessage;
using node::NodeContext;

static RPCHelpMan estimatesmartfee()
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_s
if (err_string.length() > 0) {
return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
} else {
return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr).original);
return JSONRPCError(RPCErrorFromTransactionError(terr), common::TransactionErrorString(terr).original);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// 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>
Expand All @@ -25,5 +25,5 @@ FUZZ_TARGET(fees)
assert(MoneyRange(rounded_fee));
}
const FeeReason fee_reason = fuzzed_data_provider.PickValueInArray({FeeReason::NONE, FeeReason::HALF_ESTIMATE, FeeReason::FULL_ESTIMATE, FeeReason::DOUBLE_ESTIMATE, FeeReason::CONSERVATIVE, FeeReason::MEMPOOL_MIN, FeeReason::PAYTXFEE, FeeReason::FALLBACK, FeeReason::REQUIRED});
(void)StringForFeeReason(fee_reason);
(void)common::StringForFeeReason(fee_reason);
}
3 changes: 2 additions & 1 deletion src/test/fuzz/kitchen_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
// 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 <node/types.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>
#include <cstdint>
#include <optional>
#include <vector>

using common::TransactionErrorString;
using node::TransactionError;

namespace {
Expand Down
7 changes: 5 additions & 2 deletions src/test/fuzz/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -35,6 +34,10 @@
#include <string>
#include <vector>

using common::AmountErrMsg;
using common::AmountHighWarn;
using common::FeeModeFromString;
using common::ResolveErrMsg;
using util::ContainsNoNUL;
using util::Join;
using util::RemovePrefix;
Expand Down
35 changes: 0 additions & 35 deletions src/util/error.h

This file was deleted.

0 comments on commit 778c021

Please sign in to comment.