Skip to content

Commit 4664717

Browse files
committed
Merge branch 'rbf_opts-28+knots' into bloom_default-28+knots
2 parents 1248d0d + d5b8327 commit 4664717

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1097
-234
lines changed

configure.ac

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,21 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
927927
[ AC_MSG_RESULT([no])]
928928
)
929929

930+
AC_MSG_CHECKING(for compatible sysinfo call)
931+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/sysinfo.h>]],
932+
[[
933+
struct sysinfo info;
934+
int rv = sysinfo(&info);
935+
unsigned long test = info.freeram + info.bufferram + info.mem_unit;
936+
]])],
937+
[
938+
AC_MSG_RESULT(yes);
939+
AC_DEFINE(HAVE_LINUX_SYSINFO, 1, [Define this symbol if you have a Linux-compatible sysinfo call])
940+
],[
941+
AC_MSG_RESULT(no)
942+
]
943+
)
944+
930945
dnl Check for posix_fallocate
931946
AC_MSG_CHECKING([for posix_fallocate])
932947
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[

src/bitcoin-cli.cpp

Lines changed: 62 additions & 21 deletions
Large diffs are not rendered by default.

src/common/system.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <malloc.h>
2323
#endif
2424

25+
#ifdef HAVE_LINUX_SYSINFO
26+
#include <sys/sysinfo.h>
27+
#endif
28+
2529
#include <cstdlib>
2630
#include <locale>
2731
#include <stdexcept>
@@ -110,3 +114,39 @@ int64_t GetStartupTime()
110114
{
111115
return nStartupTime;
112116
}
117+
118+
size_t g_low_memory_threshold = 10 * 1024 * 1024 /* 10 MB */;
119+
120+
bool SystemNeedsMemoryReleased()
121+
{
122+
if (g_low_memory_threshold <= 0) {
123+
// Intentionally bypass other metrics when disabled entirely
124+
return false;
125+
}
126+
#ifdef WIN32
127+
MEMORYSTATUSEX mem_status;
128+
mem_status.dwLength = sizeof(mem_status);
129+
if (GlobalMemoryStatusEx(&mem_status)) {
130+
if (mem_status.dwMemoryLoad >= 99 ||
131+
mem_status.ullAvailPhys < g_low_memory_threshold ||
132+
mem_status.ullAvailVirtual < g_low_memory_threshold) {
133+
LogPrintf("%s: YES: %s%% memory load; %s available physical memory; %s available virtual memory\n", __func__, int(mem_status.dwMemoryLoad), size_t(mem_status.ullAvailPhys), size_t(mem_status.ullAvailVirtual));
134+
return true;
135+
}
136+
}
137+
#endif
138+
#ifdef HAVE_LINUX_SYSINFO
139+
struct sysinfo sys_info;
140+
if (!sysinfo(&sys_info)) {
141+
// Explicitly 64-bit in case of 32-bit userspace on 64-bit kernel
142+
const uint64_t free_ram = uint64_t(sys_info.freeram) * sys_info.mem_unit;
143+
const uint64_t buffer_ram = uint64_t(sys_info.bufferram) * sys_info.mem_unit;
144+
if (free_ram + buffer_ram < g_low_memory_threshold) {
145+
LogPrintf("%s: YES: %s free RAM + %s buffer RAM\n", __func__, free_ram, buffer_ram);
146+
return true;
147+
}
148+
}
149+
#endif
150+
// NOTE: sysconf(_SC_AVPHYS_PAGES) doesn't account for caches on at least Linux, so not safe to use here
151+
return false;
152+
}

src/common/system.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ std::string ShellEscape(const std::string& arg);
2323
void runCommand(const std::string& strCommand);
2424
#endif
2525

26+
extern size_t g_low_memory_threshold;
27+
28+
bool SystemNeedsMemoryReleased();
29+
2630
/**
2731
* Return the number of cores available on the current system.
2832
* @note This does count virtual cores, such as those provided by HyperThreading.

src/init.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ void SetupServerArgs(ArgsManager& argsman)
492492
argsman.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
493493
argsman.AddArg("-allowignoredconf", strprintf("For backwards compatibility, treat an unused %s file in the datadir as a warning, not an error.", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
494494
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
495+
argsman.AddArg("-lowmem=<n>", strprintf("If system available memory falls below <n> MiB, flush caches (0 to disable, default: %s)", g_low_memory_threshold / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
495496
argsman.AddArg("-maxmempool=<n>", strprintf("Keep the transaction memory pool below <n> megabytes (default: %u)", DEFAULT_MAX_MEMPOOL_SIZE_MB), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
496497
argsman.AddArg("-maxorphantx=<n>", strprintf("Keep at most <n> unconnectable transactions in memory (default: %u)", DEFAULT_MAX_ORPHAN_TRANSACTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
497498
argsman.AddArg("-mempoolexpiry=<n>", strprintf("Do not keep transactions in the mempool longer than <n> hours (default: %u)", DEFAULT_MEMPOOL_EXPIRY_HOURS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -647,7 +648,8 @@ void SetupServerArgs(ArgsManager& argsman)
647648
"is of this size or less (default: %u)",
648649
MAX_OP_RETURN_RELAY),
649650
ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
650-
argsman.AddArg("-mempoolfullrbf", strprintf("(DEPRECATED) Accept transaction replace-by-fee without requiring replaceability signaling (default: %u)", DEFAULT_MEMPOOL_FULL_RBF), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
651+
argsman.AddArg("-mempoolfullrbf", strprintf("Accept transaction replace-by-fee without requiring replaceability signaling (default: %u)", (DEFAULT_MEMPOOL_RBF_POLICY == RBFPolicy::Always)), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
652+
argsman.AddArg("-mempoolreplacement", strprintf("Set to 0 to disable RBF entirely, \"fee,optin\" to honour RBF opt-out signal, or \"fee,-optin\" to always RBF aka full RBF (default: %s)", "fee,-optin"), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
651653
argsman.AddArg("-permitbaremultisig", strprintf("Relay transactions creating non-P2SH multisig outputs (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY,
652654
OptionsCategory::NODE_RELAY);
653655
argsman.AddArg("-minrelaytxfee=<amt>", strprintf("Fees (in %s/kvB) smaller than this are considered zero fee for relaying, mining and transaction creation (default: %s)",
@@ -1540,6 +1542,17 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15401542
bool do_reindex{args.GetBoolArg("-reindex", false)};
15411543
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
15421544

1545+
if (gArgs.IsArgSet("-lowmem")) {
1546+
g_low_memory_threshold = gArgs.GetIntArg("-lowmem", 0 /* not used */) * 1024 * 1024;
1547+
}
1548+
if (g_low_memory_threshold > 0) {
1549+
LogPrintf("* Flushing caches if available system memory drops below %s MiB\n", g_low_memory_threshold / 1024 / 1024);
1550+
}
1551+
1552+
if (mempool_opts.rbf_policy == RBFPolicy::Always) {
1553+
nLocalServices = ServiceFlags(nLocalServices | NODE_REPLACE_BY_FEE);
1554+
}
1555+
15431556
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
15441557
bilingual_str mempool_error;
15451558
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);

src/interfaces/node.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdint.h>
2121
#include <string>
2222
#include <tuple>
23+
#include <variant>
2324
#include <vector>
2425

2526
class BanMan;
@@ -214,7 +215,7 @@ class Node
214215
virtual std::optional<Coin> getUnspentOutput(const COutPoint& output) = 0;
215216

216217
//! Broadcast transaction.
217-
virtual node::TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) = 0;
218+
virtual node::TransactionError broadcastTransaction(CTransactionRef tx, const std::variant<CAmount, CFeeRate>& max_tx_fee, std::string& err_string) = 0;
218219

219220
//! Get wallet loader.
220221
virtual WalletLoader& walletLoader() = 0;

src/kernel/mempool_entry.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ struct NewMempoolTransactionInfo {
226226
* This boolean indicates whether the transaction was added
227227
* without enforcing mempool fee limits.
228228
*/
229-
const bool m_mempool_limit_bypassed;
229+
const ignore_rejects_type m_ignore_rejects;
230230
/* This boolean indicates whether the transaction is part of a package. */
231231
const bool m_submitted_in_package;
232232
/*
@@ -239,11 +239,11 @@ struct NewMempoolTransactionInfo {
239239

240240
explicit NewMempoolTransactionInfo(const CTransactionRef& tx, const CAmount& fee,
241241
const int64_t vsize, const unsigned int height,
242-
const bool mempool_limit_bypassed, const bool submitted_in_package,
242+
const ignore_rejects_type& ignore_rejects, const bool submitted_in_package,
243243
const bool chainstate_is_current,
244244
const bool has_no_mempool_parents)
245245
: info{tx, fee, vsize, height},
246-
m_mempool_limit_bypassed{mempool_limit_bypassed},
246+
m_ignore_rejects{ignore_rejects},
247247
m_submitted_in_package{submitted_in_package},
248248
m_chainstate_is_current{chainstate_is_current},
249249
m_has_no_mempool_parents{has_no_mempool_parents} {}

src/kernel/mempool_options.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515

1616
class ValidationSignals;
1717

18+
enum class RBFPolicy { Never, OptIn, Always };
19+
1820
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
1921
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
2022
/** Default for -maxmempool when blocksonly is set */
2123
static constexpr unsigned int DEFAULT_BLOCKSONLY_MAX_MEMPOOL_SIZE_MB{5};
2224
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
2325
static constexpr unsigned int DEFAULT_MEMPOOL_EXPIRY_HOURS{336};
24-
/** Default for -mempoolfullrbf, if the transaction replaceability signaling is ignored */
25-
static constexpr bool DEFAULT_MEMPOOL_FULL_RBF{true};
26+
/** Default for -mempoolreplacement; must update docs in init.cpp manually */
27+
static constexpr RBFPolicy DEFAULT_MEMPOOL_RBF_POLICY{RBFPolicy::Always};
2628
/** Whether to fall back to legacy V1 serialization when writing mempool.dat */
2729
static constexpr bool DEFAULT_PERSIST_V1_DAT{false};
2830
/** Default for -acceptnonstdtxn */
@@ -55,7 +57,7 @@ struct MemPoolOptions {
5557
std::optional<unsigned> max_datacarrier_bytes{DEFAULT_ACCEPT_DATACARRIER ? std::optional{MAX_OP_RETURN_RELAY} : std::nullopt};
5658
bool permit_bare_multisig{DEFAULT_PERMIT_BAREMULTISIG};
5759
bool require_standard{true};
58-
bool full_rbf{DEFAULT_MEMPOOL_FULL_RBF};
60+
RBFPolicy rbf_policy{DEFAULT_MEMPOOL_RBF_POLICY};
5961
bool persist_v1_dat{DEFAULT_PERSIST_V1_DAT};
6062
MemPoolLimits limits{};
6163

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class NodeImpl : public Node
355355
if (chainman().ActiveChainstate().CoinsTip().GetCoin(output, coin)) return coin;
356356
return {};
357357
}
358-
TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) override
358+
TransactionError broadcastTransaction(CTransactionRef tx, const std::variant<CAmount, CFeeRate>& max_tx_fee, std::string& err_string) override
359359
{
360360
return BroadcastTransaction(*m_context, std::move(tx), err_string, max_tx_fee, /*relay=*/ true, /*wait_callback=*/ false);
361361
}

src/node/mempool_args.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
#include <consensus/amount.h>
1313
#include <kernel/chainparams.h>
1414
#include <logging.h>
15+
#include <node/interface_ui.h>
1516
#include <policy/feerate.h>
1617
#include <policy/policy.h>
1718
#include <tinyformat.h>
1819
#include <util/moneystr.h>
20+
#include <util/string.h>
1921
#include <util/translation.h>
2022

2123
#include <chrono>
@@ -92,9 +94,58 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& argsman, const CChainP
9294
return util::Error{strprintf(Untranslated("acceptnonstdtxn is not currently supported for %s chain"), chainparams.GetChainTypeString())};
9395
}
9496

95-
mempool_opts.full_rbf = argsman.GetBoolArg("-mempoolfullrbf", mempool_opts.full_rbf);
96-
if (!mempool_opts.full_rbf) {
97-
LogInfo("Warning: mempoolfullrbf=0 set but deprecated and will be removed in a future release\n");
97+
if (argsman.IsArgSet("-mempoolreplacement") || argsman.IsArgSet("-mempoolfullrbf")) {
98+
// Generally, mempoolreplacement overrides mempoolfullrbf, but the latter is used to infer intent in some cases
99+
std::optional<bool> optin_flag;
100+
bool fee_flag{false};
101+
if (argsman.GetBoolArg("-mempoolreplacement", false)) {
102+
fee_flag = true;
103+
} else {
104+
for (auto& opt : util::SplitString(argsman.GetArg("-mempoolreplacement", ""), ",+")) {
105+
if (opt == "optin") {
106+
optin_flag = true;
107+
} else if (opt == "-optin") {
108+
optin_flag = false;
109+
} else if (opt == "fee") {
110+
fee_flag = true;
111+
}
112+
}
113+
}
114+
if (optin_flag.value_or(false)) {
115+
// "optin" is explicitly specified
116+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
117+
} else if (argsman.GetBoolArg("-mempoolfullrbf", false)) {
118+
const bool mempoolreplacement_false{argsman.IsArgSet("-mempoolreplacement") && !(fee_flag || optin_flag.has_value())};
119+
if (mempoolreplacement_false) {
120+
// This is a contradiction, but override rather than error
121+
InitWarning(_("False mempoolreplacement option contradicts true mempoolfullrbf; disallowing all RBF"));
122+
mempool_opts.rbf_policy = RBFPolicy::Never;
123+
} else {
124+
mempool_opts.rbf_policy = RBFPolicy::Always;
125+
}
126+
} else if (!optin_flag.value_or(true)) {
127+
// "-optin" is explicitly specified
128+
mempool_opts.rbf_policy = fee_flag ? RBFPolicy::Always : RBFPolicy::Never;
129+
} else if (fee_flag) {
130+
// Just "fee" by itself
131+
if (!argsman.GetBoolArg("-mempoolfullrbf", true)) {
132+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
133+
} else {
134+
// Fallback to default, unless it's been changed to Never
135+
if (mempool_opts.rbf_policy == RBFPolicy::Never) {
136+
mempool_opts.rbf_policy = RBFPolicy::Always;
137+
}
138+
}
139+
} else if (!argsman.IsArgSet("-mempoolreplacement")) {
140+
// mempoolfullrbf is always explicitly false here
141+
// Fallback to default, as long as it isn't Always
142+
if (mempool_opts.rbf_policy == RBFPolicy::Always) {
143+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
144+
}
145+
} else {
146+
// mempoolreplacement is explicitly false here
147+
mempool_opts.rbf_policy = RBFPolicy::Never;
148+
}
98149
}
99150

100151
mempool_opts.persist_v1_dat = argsman.GetBoolArg("-persistmempoolv1", mempool_opts.persist_v1_dat);

0 commit comments

Comments
 (0)