Skip to content

Commit 680eafd

Browse files
committed
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.
1 parent 02e62c6 commit 680eafd

20 files changed

+143
-143
lines changed

src/Makefile.am

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ BITCOIN_CORE_H = \
144144
compat/compat.h \
145145
compat/cpuid.h \
146146
compat/endian.h \
147+
common/messages.h \
147148
common/settings.h \
148149
common/signmessage.h \
149150
common/system.h \
@@ -300,11 +301,9 @@ BITCOIN_CORE_H = \
300301
util/chaintype.h \
301302
util/check.h \
302303
util/epochguard.h \
303-
util/error.h \
304304
util/exception.h \
305305
util/fastrange.h \
306306
util/feefrac.h \
307-
util/fees.h \
308307
util/fs.h \
309308
util/fs_helpers.h \
310309
util/golombrice.h \
@@ -681,6 +680,7 @@ libbitcoin_common_a_SOURCES = \
681680
common/config.cpp \
682681
common/init.cpp \
683682
common/interfaces.cpp \
683+
common/messages.cpp \
684684
common/run_command.cpp \
685685
common/settings.cpp \
686686
common/signmessage.cpp \
@@ -738,10 +738,8 @@ libbitcoin_util_a_SOURCES = \
738738
util/bytevectorhash.cpp \
739739
util/chaintype.cpp \
740740
util/check.cpp \
741-
util/error.cpp \
742741
util/exception.cpp \
743742
util/feefrac.cpp \
744-
util/fees.cpp \
745743
util/fs.cpp \
746744
util/fs_helpers.cpp \
747745
util/hasher.cpp \

src/util/error.cpp renamed to src/common/messages.cpp

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,75 @@
1-
// Copyright (c) 2010-2022 The Bitcoin Core developers
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2022 The Bitcoin Core developers
23
// Distributed under the MIT software license, see the accompanying
34
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
45

5-
#include <util/error.h>
6+
#include <common/messages.h>
67

78
#include <common/types.h>
9+
#include <policy/fees.h>
810
#include <tinyformat.h>
11+
#include <util/strencodings.h>
12+
#include <util/string.h>
913
#include <util/translation.h>
1014

1115
#include <cassert>
16+
#include <map>
1217
#include <string>
18+
#include <utility>
19+
#include <vector>
1320

14-
using common::PSBTError;
21+
namespace common {
22+
std::string StringForFeeReason(FeeReason reason)
23+
{
24+
static const std::map<FeeReason, std::string> fee_reason_strings = {
25+
{FeeReason::NONE, "None"},
26+
{FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
27+
{FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
28+
{FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
29+
{FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
30+
{FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
31+
{FeeReason::PAYTXFEE, "PayTxFee set"},
32+
{FeeReason::FALLBACK, "Fallback fee"},
33+
{FeeReason::REQUIRED, "Minimum Required Fee"},
34+
};
35+
auto reason_string = fee_reason_strings.find(reason);
36+
37+
if (reason_string == fee_reason_strings.end()) return "Unknown";
38+
39+
return reason_string->second;
40+
}
41+
42+
const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
43+
{
44+
static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
45+
{"unset", FeeEstimateMode::UNSET},
46+
{"economical", FeeEstimateMode::ECONOMICAL},
47+
{"conservative", FeeEstimateMode::CONSERVATIVE},
48+
};
49+
return FEE_MODES;
50+
}
51+
52+
std::string FeeModes(const std::string& delimiter)
53+
{
54+
return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
55+
}
56+
57+
std::string InvalidEstimateModeErrorMessage()
58+
{
59+
return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
60+
}
61+
62+
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode)
63+
{
64+
auto searchkey = ToUpper(mode_string);
65+
for (const auto& pair : FeeModeMap()) {
66+
if (ToUpper(pair.first) == searchkey) {
67+
fee_estimate_mode = pair.second;
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
1573

1674
bilingual_str PSBTErrorString(PSBTError err)
1775
{
@@ -74,3 +132,4 @@ bilingual_str AmountErrMsg(const std::string& optname, const std::string& strVal
74132
{
75133
return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
76134
}
135+
} // namespace common

src/common/messages.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2020 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
//! @file common/messages.h is a home for simple string functions returning
7+
//! descriptive messages that are used in RPC and GUI interfaces or log
8+
//! messages, and are called in different parts of the codebase across
9+
//! node/wallet/gui boundaries.
10+
11+
#ifndef BITCOIN_COMMON_MESSAGES_H
12+
#define BITCOIN_COMMON_MESSAGES_H
13+
14+
#include <node/types.h>
15+
#include <string>
16+
17+
struct bilingual_str;
18+
19+
enum class FeeEstimateMode;
20+
enum class FeeReason;
21+
22+
namespace common {
23+
enum class PSBTError;
24+
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
25+
std::string StringForFeeReason(FeeReason reason);
26+
std::string FeeModes(const std::string& delimiter);
27+
std::string InvalidEstimateModeErrorMessage();
28+
bilingual_str PSBTErrorString(PSBTError error);
29+
bilingual_str TransactionErrorString(const TransactionError error);
30+
bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind);
31+
bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& strPort);
32+
bilingual_str AmountHighWarn(const std::string& optname);
33+
bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue);
34+
} // namespace common
35+
36+
#endif // BITCOIN_COMMON_MESSAGES_H

src/init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
#include <zmq/zmqrpc.h>
116116
#endif
117117

118+
using common::AmountErrMsg;
119+
using common::InvalidPortErrMsg;
120+
using common::ResolveErrMsg;
118121
using kernel::DumpMempool;
119122
using kernel::LoadMempool;
120123
using kernel::ValidationCacheSizes;

src/net_permissions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <common/messages.h>
56
#include <common/system.h>
67
#include <net_permissions.h>
78
#include <netbase.h>
8-
#include <util/error.h>
99
#include <util/translation.h>
1010

11+
using common::ResolveErrMsg;
12+
1113
const std::vector<std::string> NET_PERMISSIONS_DOC{
1214
"bloomfilter (allow requesting BIP37 filtered blocks and transactions)",
1315
"noban (do not ban for misbehavior; implies download)",

src/node/mempool_args.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
#include <kernel/mempool_options.h>
99

1010
#include <common/args.h>
11+
#include <common/messages.h>
1112
#include <consensus/amount.h>
1213
#include <kernel/chainparams.h>
1314
#include <logging.h>
1415
#include <policy/feerate.h>
1516
#include <policy/policy.h>
1617
#include <tinyformat.h>
17-
#include <util/error.h>
1818
#include <util/moneystr.h>
1919
#include <util/translation.h>
2020

2121
#include <chrono>
2222
#include <memory>
2323

24+
using common::AmountErrMsg;
2425
using kernel::MemPoolLimits;
2526
using kernel::MemPoolOptions;
2627

src/node/transaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#ifndef BITCOIN_NODE_TRANSACTION_H
66
#define BITCOIN_NODE_TRANSACTION_H
77

8+
#include <common/messages.h>
89
#include <policy/feerate.h>
910
#include <primitives/transaction.h>
10-
#include <util/error.h>
1111

1212
class CBlockIndex;
1313
class CTxMemPool;

src/qt/psbtoperationsdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <qt/psbtoperationsdialog.h>
66

7+
#include <common/messages.h>
78
#include <core_io.h>
89
#include <interfaces/node.h>
910
#include <key_io.h>
@@ -13,14 +14,14 @@
1314
#include <qt/forms/ui_psbtoperationsdialog.h>
1415
#include <qt/guiutil.h>
1516
#include <qt/optionsmodel.h>
16-
#include <util/error.h>
1717
#include <util/fs.h>
1818
#include <util/strencodings.h>
1919

2020
#include <fstream>
2121
#include <iostream>
2222
#include <string>
2323

24+
using common::TransactionErrorString;
2425
using node::AnalyzePSBT;
2526
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
2627
using node::PSBTAnalysis;

src/rpc/fees.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include <common/messages.h>
67
#include <core_io.h>
78
#include <node/context.h>
89
#include <policy/feerate.h>
@@ -14,14 +15,16 @@
1415
#include <rpc/util.h>
1516
#include <txmempool.h>
1617
#include <univalue.h>
17-
#include <util/fees.h>
1818
#include <validationinterface.h>
1919

2020
#include <algorithm>
2121
#include <array>
2222
#include <cmath>
2323
#include <string>
2424

25+
using common::FeeModeFromString;
26+
using common::FeeModes;
27+
using common::InvalidEstimateModeErrorMessage;
2528
using node::NodeContext;
2629

2730
static RPCHelpMan estimatesmartfee()

src/rpc/util.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clientversion.h>
88
#include <core_io.h>
99
#include <common/args.h>
10+
#include <common/messages.h>
1011
#include <common/types.h>
1112
#include <consensus/amount.h>
1213
#include <script/interpreter.h>
@@ -32,6 +33,8 @@
3233
#include <utility>
3334

3435
using common::PSBTError;
36+
using common::PSBTErrorString;
37+
using common::TransactionErrorString;
3538

3639
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
3740
const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};

0 commit comments

Comments
 (0)