blockmanager_args.cpp raw

   1  // Copyright (c) 2023 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <node/blockmanager_args.h>
   6  
   7  #include <common/args.h>
   8  #include <node/blockstorage.h>
   9  #include <node/database_args.h>
  10  #include <tinyformat.h>
  11  #include <util/result.h>
  12  #include <util/translation.h>
  13  #include <validation.h>
  14  
  15  #include <algorithm>
  16  #include <cstdint>
  17  #include <string_view>
  18  
  19  namespace node {
  20  util::Result<uint64_t> ParsePruneOption(const int64_t nPruneArg, const std::string_view opt_name)
  21  {
  22      // block pruning; get the amount of disk space (in MiB) to allot for block & undo files
  23      if (nPruneArg < 0) {
  24          return util::Error{strprintf(_("%s cannot be configured with a negative value."), opt_name)};
  25      } else if (nPruneArg == 0) { // pruning disabled
  26          return 0;
  27      } else if (nPruneArg == 1) { // manual pruning: -prune=1
  28          return BlockManager::PRUNE_TARGET_MANUAL;
  29      }
  30      const uint64_t nPruneTarget{uint64_t(nPruneArg) * 1024 * 1024};
  31      if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
  32          return util::Error{strprintf(_("%s configured below the minimum of %d MiB.  Please use a higher number."), opt_name, MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024)};
  33      }
  34      return nPruneTarget;
  35  }
  36  
  37  util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Options& opts)
  38  {
  39      if (auto value{args.GetBoolArg("-blocksxor")}) opts.use_xor = *value;
  40  
  41      // block pruning; get the amount of disk space (in MiB) to allot for block & undo files
  42      int64_t nPruneArg{args.GetIntArg("-prune", opts.prune_target)};
  43      if (const auto prune_parsed = ParsePruneOption(nPruneArg, "Prune")) {
  44          opts.prune_target = *prune_parsed;
  45      } else {
  46          return util::Error{util::ErrorString(prune_parsed)};
  47      }
  48  
  49      if (const auto prune_during_init{args.GetIntArg("-pruneduringinit")}) {
  50          if (*prune_during_init == -1) {
  51              opts.prune_target_during_init = -1;
  52          } else if (auto prune_parsed = ParsePruneOption(*prune_during_init, "-pruneduringinit")) {
  53              if (!*prune_parsed) {
  54                  // We don't actually disable pruning, just treat it as manual until sync completes
  55                  *prune_parsed = BlockManager::PRUNE_TARGET_MANUAL;
  56              }
  57              // NOTE: PRUNE_TARGET_MANUAL is >int64 max
  58              opts.prune_target_during_init = std::min(std::numeric_limits<int64_t>::max(), (int64_t)*prune_parsed);
  59          } else {
  60              return util::Error{util::ErrorString(prune_parsed)};
  61          }
  62      }
  63  
  64      if (auto value{args.GetBoolArg("-fastprune")}) opts.fast_prune = *value;
  65  
  66      ReadDatabaseArgs(args, opts.block_tree_db_params.options);
  67  
  68      return {};
  69  }
  70  } // namespace node
  71