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 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.
  • Loading branch information
ryanofsky committed May 16, 2024
1 parent 02e62c6 commit 680eafd
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 143 deletions.
6 changes: 2 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
65 changes: 62 additions & 3 deletions src/util/error.cpp → src/common/messages.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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 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 <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
3 changes: 3 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/net_permissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
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
3 changes: 2 additions & 1 deletion src/qt/psbtoperationsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <qt/psbtoperationsdialog.h>

#include <common/messages.h>
#include <core_io.h>
#include <interfaces/node.h>
#include <key_io.h>
Expand All @@ -13,14 +14,14 @@
#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>

#include <fstream>
#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 <node/context.h>
#include <policy/feerate.h>
Expand All @@ -14,14 +15,16 @@
#include <rpc/util.h>
#include <txmempool.h>
#include <univalue.h>
#include <util/fees.h>
#include <validationinterface.h>

#include <algorithm>
#include <array>
#include <cmath>
#include <string>

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

static RPCHelpMan estimatesmartfee()
Expand Down
3 changes: 3 additions & 0 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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"};
Expand Down
4 changes: 3 additions & 1 deletion src/test/fuzz/fees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/test/fuzz/kitchen_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +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 <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;

namespace {
constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
TransactionError::MISSING_INPUTS,
Expand Down
8 changes: 6 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 @@ -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());
Expand Down
38 changes: 0 additions & 38 deletions src/util/error.h

This file was deleted.

Loading

0 comments on commit 680eafd

Please sign in to comment.