Skip to content

Commit b6ebbae

Browse files
committed
Merge branch 'bytespersigopstrict-28+knots' into datacarriercost-28+knots
2 parents 1248d0d + cbecc46 commit b6ebbae

Some content is hidden

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

58 files changed

+1293
-261
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: 19 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);
@@ -641,13 +642,18 @@ void SetupServerArgs(ArgsManager& argsman)
641642
argsman.AddArg("-dustrelayfee=<amt>", strprintf("Fee rate (in %s/kvB) used to define dust, the value of an output such that it will cost more than its value in fees at this fee rate to spend it. (default: %s)", CURRENCY_UNIT, FormatMoney(DUST_RELAY_TX_FEE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::NODE_RELAY);
642643
argsman.AddArg("-acceptstalefeeestimates", strprintf("Read fee estimates even if they are stale (%sdefault: %u) fee estimates are considered stale if they are %s hours old", "regtest only; ", DEFAULT_ACCEPT_STALE_FEE_ESTIMATES, Ticks<std::chrono::hours>(MAX_FILE_AGE)), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
643644
argsman.AddArg("-bytespersigop", strprintf("Equivalent bytes per sigop in transactions for relay and mining (default: %u)", DEFAULT_BYTES_PER_SIGOP), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
645+
argsman.AddArg("-bytespersigopstrict", strprintf("Minimum bytes per sigop in transactions we relay and mine (default: %u)", DEFAULT_BYTES_PER_SIGOP_STRICT), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
644646
argsman.AddArg("-datacarrier", strprintf("Relay and mine data carrier transactions (default: %u)", DEFAULT_ACCEPT_DATACARRIER), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
645647
argsman.AddArg("-datacarriersize",
646648
strprintf("Relay and mine transactions whose data-carrying raw scriptPubKey "
647649
"is of this size or less (default: %u)",
648650
MAX_OP_RETURN_RELAY),
649651
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);
652+
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);
653+
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);
654+
argsman.AddArg("-mempooltruc", strprintf("Behaviour for transactions requesting TRUC limits: \"reject\" the transactions entirely, \"accept\" them just like any other, or \"enforce\" to impose their requested restrictions (default: %s)", "enforce"), ArgsManager::ALLOW_ANY, OptionsCategory::NODE_RELAY);
655+
argsman.AddArg("-permitbarepubkey", strprintf("Relay legacy pubkey outputs (default: %u)", DEFAULT_PERMIT_BAREPUBKEY), ArgsManager::ALLOW_ANY,
656+
OptionsCategory::NODE_RELAY);
651657
argsman.AddArg("-permitbaremultisig", strprintf("Relay transactions creating non-P2SH multisig outputs (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY,
652658
OptionsCategory::NODE_RELAY);
653659
argsman.AddArg("-minrelaytxfee=<amt>", strprintf("Fees (in %s/kvB) smaller than this are considered zero fee for relaying, mining and transaction creation (default: %s)",
@@ -1032,6 +1038,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10321038
}
10331039

10341040
nBytesPerSigOp = args.GetIntArg("-bytespersigop", nBytesPerSigOp);
1041+
nBytesPerSigOpStrict = args.GetIntArg("-bytespersigopstrict", nBytesPerSigOpStrict);
10351042

10361043
if (!g_wallet_init_interface.ParameterInteraction()) return false;
10371044

@@ -1540,6 +1547,17 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15401547
bool do_reindex{args.GetBoolArg("-reindex", false)};
15411548
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
15421549

1550+
if (gArgs.IsArgSet("-lowmem")) {
1551+
g_low_memory_threshold = gArgs.GetIntArg("-lowmem", 0 /* not used */) * 1024 * 1024;
1552+
}
1553+
if (g_low_memory_threshold > 0) {
1554+
LogPrintf("* Flushing caches if available system memory drops below %s MiB\n", g_low_memory_threshold / 1024 / 1024);
1555+
}
1556+
1557+
if (mempool_opts.rbf_policy == RBFPolicy::Always) {
1558+
nLocalServices = ServiceFlags(nLocalServices | NODE_REPLACE_BY_FEE);
1559+
}
1560+
15431561
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
15441562
bilingual_str mempool_error;
15451563
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515

1616
class ValidationSignals;
1717

18+
enum class RBFPolicy { Never, OptIn, Always };
19+
enum class TRUCPolicy { Reject, Accept, Enforce };
20+
1821
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
1922
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
2023
/** Default for -maxmempool when blocksonly is set */
2124
static constexpr unsigned int DEFAULT_BLOCKSONLY_MAX_MEMPOOL_SIZE_MB{5};
2225
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
2326
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};
27+
/** Default for -mempoolreplacement; must update docs in init.cpp manually */
28+
static constexpr RBFPolicy DEFAULT_MEMPOOL_RBF_POLICY{RBFPolicy::Always};
29+
/** Default for -mempooltruc; must update docs in init.cpp manually */
30+
static constexpr TRUCPolicy DEFAULT_MEMPOOL_TRUC_POLICY{TRUCPolicy::Enforce};
2631
/** Whether to fall back to legacy V1 serialization when writing mempool.dat */
2732
static constexpr bool DEFAULT_PERSIST_V1_DAT{false};
2833
/** Default for -acceptnonstdtxn */
@@ -53,9 +58,11 @@ struct MemPoolOptions {
5358
* If nullopt, any size is nonstandard.
5459
*/
5560
std::optional<unsigned> max_datacarrier_bytes{DEFAULT_ACCEPT_DATACARRIER ? std::optional{MAX_OP_RETURN_RELAY} : std::nullopt};
61+
bool permit_bare_pubkey{DEFAULT_PERMIT_BAREPUBKEY};
5662
bool permit_bare_multisig{DEFAULT_PERMIT_BAREMULTISIG};
5763
bool require_standard{true};
58-
bool full_rbf{DEFAULT_MEMPOOL_FULL_RBF};
64+
RBFPolicy rbf_policy{DEFAULT_MEMPOOL_RBF_POLICY};
65+
TRUCPolicy truc_policy{DEFAULT_MEMPOOL_TRUC_POLICY};
5966
bool persist_v1_dat{DEFAULT_PERSIST_V1_DAT};
6067
MemPoolLimits limits{};
6168

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: 84 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>
@@ -79,6 +81,8 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& argsman, const CChainP
7981
}
8082
}
8183

84+
mempool_opts.permit_bare_pubkey = argsman.GetBoolArg("-permitbarepubkey", DEFAULT_PERMIT_BAREPUBKEY);
85+
8286
mempool_opts.permit_bare_multisig = argsman.GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
8387

8488
if (argsman.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER)) {
@@ -92,9 +96,86 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& argsman, const CChainP
9296
return util::Error{strprintf(Untranslated("acceptnonstdtxn is not currently supported for %s chain"), chainparams.GetChainTypeString())};
9397
}
9498

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");
99+
if (argsman.IsArgSet("-mempoolreplacement") || argsman.IsArgSet("-mempoolfullrbf")) {
100+
// Generally, mempoolreplacement overrides mempoolfullrbf, but the latter is used to infer intent in some cases
101+
std::optional<bool> optin_flag;
102+
bool fee_flag{false};
103+
if (argsman.GetBoolArg("-mempoolreplacement", false)) {
104+
fee_flag = true;
105+
} else {
106+
for (auto& opt : util::SplitString(argsman.GetArg("-mempoolreplacement", ""), ",+")) {
107+
if (opt == "optin") {
108+
optin_flag = true;
109+
} else if (opt == "-optin") {
110+
optin_flag = false;
111+
} else if (opt == "fee") {
112+
fee_flag = true;
113+
}
114+
}
115+
}
116+
if (optin_flag.value_or(false)) {
117+
// "optin" is explicitly specified
118+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
119+
} else if (argsman.GetBoolArg("-mempoolfullrbf", false)) {
120+
const bool mempoolreplacement_false{argsman.IsArgSet("-mempoolreplacement") && !(fee_flag || optin_flag.has_value())};
121+
if (mempoolreplacement_false) {
122+
// This is a contradiction, but override rather than error
123+
InitWarning(_("False mempoolreplacement option contradicts true mempoolfullrbf; disallowing all RBF"));
124+
mempool_opts.rbf_policy = RBFPolicy::Never;
125+
} else {
126+
mempool_opts.rbf_policy = RBFPolicy::Always;
127+
}
128+
} else if (!optin_flag.value_or(true)) {
129+
// "-optin" is explicitly specified
130+
mempool_opts.rbf_policy = fee_flag ? RBFPolicy::Always : RBFPolicy::Never;
131+
} else if (fee_flag) {
132+
// Just "fee" by itself
133+
if (!argsman.GetBoolArg("-mempoolfullrbf", true)) {
134+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
135+
} else {
136+
// Fallback to default, unless it's been changed to Never
137+
if (mempool_opts.rbf_policy == RBFPolicy::Never) {
138+
mempool_opts.rbf_policy = RBFPolicy::Always;
139+
}
140+
}
141+
} else if (!argsman.IsArgSet("-mempoolreplacement")) {
142+
// mempoolfullrbf is always explicitly false here
143+
// Fallback to default, as long as it isn't Always
144+
if (mempool_opts.rbf_policy == RBFPolicy::Always) {
145+
mempool_opts.rbf_policy = RBFPolicy::OptIn;
146+
}
147+
} else {
148+
// mempoolreplacement is explicitly false here
149+
mempool_opts.rbf_policy = RBFPolicy::Never;
150+
}
151+
}
152+
153+
if (argsman.IsArgSet("-mempooltruc")) {
154+
std::optional<bool> accept_flag, enforce_flag;
155+
if (argsman.GetBoolArg("-mempooltruc", false)) {
156+
enforce_flag = true;
157+
}
158+
for (auto& opt : util::SplitString(argsman.GetArg("-mempooltruc", ""), ",+")) {
159+
if (opt == "optin" || opt == "enforce") {
160+
enforce_flag = true;
161+
} else if (opt == "-optin" || opt == "-enforce") {
162+
enforce_flag = false;
163+
} else if (opt == "accept") {
164+
accept_flag = true;
165+
} else if (opt == "reject" || opt == "0") {
166+
accept_flag = false;
167+
}
168+
}
169+
170+
if (accept_flag && !*accept_flag) { // reject
171+
mempool_opts.truc_policy = TRUCPolicy::Reject;
172+
} else if (enforce_flag && *enforce_flag) { // enforce
173+
mempool_opts.truc_policy = TRUCPolicy::Enforce;
174+
} else if ((!accept_flag) && !enforce_flag) {
175+
// nothing specified, leave at default
176+
} else { // accept or -enforce
177+
mempool_opts.truc_policy = TRUCPolicy::Accept;
178+
}
98179
}
99180

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

0 commit comments

Comments
 (0)