Skip to content

Commit

Permalink
Merge bitcoin#15163: Correct units for "-dbcache" and "-prune"
Browse files Browse the repository at this point in the history
6f6514a Correct units for "-dbcache" and "-prune" (Hennadii Stepanov)

Pull request description:

  Actually, all `dbcache`-related values in the code are measured in MiB (not in megabytes, MB) or in bytes (e.g., `nTotalCache`).

  See: https://github.com/bitcoin/bitcoin/blob/master/src/txdb.h

  https://github.com/bitcoin/bitcoin/blob/ba8c8b22272ad40fe2de465d7e745532bab48d3b/src/init.cpp#L1405-L1424

  Also, "-prune" is fixed:
  1. The GUI values in GB are translated to the node values in MiB correctly.
  2. The maximum of the "prune" `QSpinBox` is not limited by default value of 99 (GB).

  Fix: bitcoin#15106

Tree-SHA512: 151ec43b31b1074db8b345fedb1dcc10bde225899a5296bfc183f57e1553d13ac27db8db100226646769ad03c9fcab29d88763065a471757c6c41ac51108459d
  • Loading branch information
laanwj authored and Munkybooty committed Aug 24, 2021
1 parent fcd4c15 commit 4f13c4a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/init.cpp
Expand Up @@ -468,7 +468,7 @@ void SetupServerArgs()
gArgs.AddArg("-conf=<file>", strprintf("Specify configuration file. Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME), false, OptionsCategory::OPTIONS);
gArgs.AddArg("-datadir=<dir>", "Specify data directory", false, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), true, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbcache=<n>", strprintf("Set database cache size in megabytes (%d to %d, default: %d)", nMinDbCache, nMaxDbCache, nDefaultDbCache), false, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbcache=<n>", strprintf("Set database cache size in MiB (%d to %d, default: %d)", nMinDbCache, nMaxDbCache, nDefaultDbCache), false, OptionsCategory::OPTIONS);
gArgs.AddArg("-debuglogfile=<file>", strprintf("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (-nodebuglogfile to disable; default: %s)", DEFAULT_DEBUGLOGFILE), false, OptionsCategory::OPTIONS);
gArgs.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", false, OptionsCategory::OPTIONS);
gArgs.AddArg("-loadblock=<file>", "Imports blocks from external blk000??.dat file on startup", false, OptionsCategory::OPTIONS);
Expand Down Expand Up @@ -1388,7 +1388,7 @@ bool AppInitParameterInteraction()
return InitError(strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024));
}
}
LogPrintf("Prune configured to target %uMiB on disk for block and undo files.\n", nPruneTarget / 1024 / 1024);
LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", nPruneTarget / 1024 / 1024);
fPruneMode = true;
}

Expand Down Expand Up @@ -2038,16 +2038,16 @@ bool AppInitMain()
int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
int64_t nEvoDbCache = 1024 * 1024 * 16; // TODO
LogPrintf("Cache configuration:\n");
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
LogPrintf("* Using %.1fMiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
}
for (BlockFilterType filter_type : g_enabled_filter_types) {
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
}
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));

bool fLoaded = false;

Expand Down
2 changes: 1 addition & 1 deletion src/qt/forms/optionsdialog.ui
Expand Up @@ -230,7 +230,7 @@
<item>
<widget class="QLabel" name="databaseCacheUnitLabel">
<property name="text">
<string>MB</string>
<string>MiB</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
Expand Down
3 changes: 3 additions & 0 deletions src/qt/guiconstants.h
Expand Up @@ -48,4 +48,7 @@ static const int MAX_URI_LENGTH = 255;
#define QAPP_APP_NAME_DEVNET "Dash-Qt-%s"
#define QAPP_APP_NAME_REGTEST "Dash-Qt-regtest"

/* One gigabyte (GB) in bytes */
static constexpr uint64_t GB_BYTES{1000000000};

#endif // BITCOIN_QT_GUICONSTANTS_H
2 changes: 1 addition & 1 deletion src/qt/intro.cpp
Expand Up @@ -11,6 +11,7 @@
#include <qt/intro.h>
#include <qt/forms/ui_intro.h>

#include <qt/guiconstants.h>
#include <qt/guiutil.h>

#include <interfaces/node.h>
Expand All @@ -22,7 +23,6 @@

#include <cmath>

static const uint64_t GB_BYTES = 1000000000LL;
/* Total required space (in GB) depending on user choice (prune, not prune) */
static uint64_t requiredSpace;

Expand Down
9 changes: 5 additions & 4 deletions src/qt/optionsdialog.cpp
Expand Up @@ -11,6 +11,7 @@

#include <qt/appearancewidget.h>
#include <qt/bitcoinunits.h>
#include <qt/guiconstants.h>
#include <qt/guiutil.h>
#include <qt/optionsmodel.h>

Expand Down Expand Up @@ -56,10 +57,6 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
/* Main elements init */
ui->databaseCache->setMinimum(nMinDbCache);
ui->databaseCache->setMaximum(nMaxDbCache);
static const uint64_t GiB = 1024 * 1024 * 1024;
static const uint64_t nMinDiskSpace = MIN_DISK_SPACE_FOR_BLOCK_FILES / GiB +
(MIN_DISK_SPACE_FOR_BLOCK_FILES % GiB) ? 1 : 0;
ui->pruneSize->setMinimum(nMinDiskSpace);
ui->threadsScriptVerif->setMinimum(-GetNumCores());
ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
ui->pruneWarning->setVisible(false);
Expand Down Expand Up @@ -226,6 +223,10 @@ void OptionsDialog::setModel(OptionsModel *_model)
appearance->setModel(_model);

updateDefaultProxyNets();

// Prune values are in GB to be consistent with intro.cpp
static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
ui->pruneSize->setRange(nMinDiskSpace, _model->node().getAssumedBlockchainSize());
}

/* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
Expand Down
7 changes: 4 additions & 3 deletions src/qt/optionsmodel.cpp
Expand Up @@ -10,6 +10,7 @@
#include <qt/optionsmodel.h>

#include <qt/bitcoinunits.h>
#include <qt/guiconstants.h>
#include <qt/guiutil.h>

#include <interfaces/node.h>
Expand Down Expand Up @@ -179,9 +180,9 @@ void OptionsModel::Init(bool resetSettings)
settings.setValue("bPrune", false);
if (!settings.contains("nPruneSize"))
settings.setValue("nPruneSize", 2);
// Convert prune size to MB:
const uint64_t nPruneSizeMB = settings.value("nPruneSize").toInt() * 1000;
if (!m_node.softSetArg("-prune", settings.value("bPrune").toBool() ? std::to_string(nPruneSizeMB) : "0")) {
// Convert prune size from GB to MiB:
const uint64_t nPruneSizeMiB = (settings.value("nPruneSize").toInt() * GB_BYTES) >> 20;
if (!m_node.softSetArg("-prune", settings.value("bPrune").toBool() ? std::to_string(nPruneSizeMiB) : "0")) {
addOverriddenOption("-prune");
}

Expand Down
14 changes: 7 additions & 7 deletions src/validation.h
Expand Up @@ -102,14 +102,14 @@ static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
static const signed int DEFAULT_CHECKBLOCKS = 6;
static const unsigned int DEFAULT_CHECKLEVEL = 3;

// Require that user allocate at least 945MB for block & undo files (blk???.dat and rev???.dat)
// At 2MB per block, 288 blocks = 576MB.
// Add 15% for Undo data = 662MB
// Add 20% for Orphan block rate = 794MB
// We want the low water mark after pruning to be at least 794 MB and since we prune in
// Require that user allocate at least 945 MiB for block & undo files (blk???.dat and rev???.dat)
// At 2B MiB per block, 288 blocks = 576 MiB.
// Add 15% for Undo data = 662 MiB
// Add 20% for Orphan block rate = 794 MiB
// We want the low water mark after pruning to be at least 794 MiB and since we prune in
// full block file chunks, we need the high water mark which triggers the prune to be
// one 128MB block file + added 15% undo data = 147MB greater for a total of 941MB
// Setting the target to > than 945MB will make it likely we can respect the target.
// one 128 MiB block file + added 15% undo data = 147 MiB greater for a total of 941 MiB
// Setting the target to > than 945 MiB will make it likely we can respect the target.
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 945 * 1024 * 1024;

struct BlockHasher
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_pruning.py
Expand Up @@ -315,7 +315,7 @@ def has_block(index):
if has_block(3):
raise AssertionError("blk00003.dat is still there, should be pruned by now")

# stop node, start back up with auto-prune at 550MB, make sure still runs
# stop node, start back up with auto-prune at 550 MiB, make sure still runs
self.stop_node(node_number)
self.start_node(node_number, extra_args=["-dip3params=2000:2000", "-dip8params=2000", "-disablegovernance", "-txindex=0", "-prune=550"])

Expand Down

0 comments on commit 4f13c4a

Please sign in to comment.