20 #include <ripple/basics/FileUtilities.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/basics/StringUtilities.h>
23 #include <ripple/basics/contract.h>
24 #include <ripple/beast/core/LexicalCast.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/core/ConfigSections.h>
27 #include <ripple/json/json_reader.h>
28 #include <ripple/net/HTTPClient.h>
29 #include <ripple/protocol/Feature.h>
30 #include <ripple/protocol/SystemParameters.h>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/format.hpp>
33 #include <boost/predef.h>
34 #include <boost/regex.hpp>
35 #include <boost/system/error_code.hpp>
43 #include <sysinfoapi.h>
51 if (MEMORYSTATUSEX msx{
sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
62 #include <sys/sysinfo.h>
72 if (sysinfo(&si) == 0)
84 #include <sys/sysctl.h>
85 #include <sys/types.h>
93 int mib[] = {CTL_HW, HW_MEMSIZE};
95 size_t size =
sizeof(ram);
97 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
136 []() constexpr->bool {
137 std::underlying_type_t<SizedItem> idx = 0;
139 for (auto const& i : sizedItems)
141 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
149 "Mismatch between sized item enum & array indices");
156 #define SECTION_DEFAULT_NAME ""
166 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
169 boost::algorithm::replace_all(strData,
"\r",
"\n");
171 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
177 secResult[strSection] = IniFileSections::mapped_type();
180 for (
auto& strValue : vLines)
183 boost::algorithm::trim(strValue);
185 if (strValue.empty() || strValue[0] ==
'#')
189 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
192 strSection = strValue.
substr(1, strValue.length() - 2);
193 secResult.
emplace(strSection, IniFileSections::mapped_type{});
198 if (!strValue.empty())
199 secResult[strSection].push_back(strValue);
206 IniFileSections::mapped_type*
209 IniFileSections::iterator it;
210 IniFileSections::mapped_type* smtResult;
211 it = secSource.
find(strSection);
212 if (it == secSource.
end())
215 smtResult = &(it->second);
226 IniFileSections::mapped_type* pmtEntries =
228 bool bSingle = pmtEntries && 1 == pmtEntries->size();
232 strValue = (*pmtEntries)[0];
236 JLOG(j.
warn()) << boost::str(
237 boost::format(
"Section [%s]: requires 1 line not %d lines.") %
238 strSection % pmtEntries->
size());
250 char const*
const Config::configFileName =
"rippled.cfg";
251 char const*
const Config::databaseDirName =
"db";
252 char const*
const Config::validatorsFileName =
"validators.txt";
259 if (
auto const v =
std::getenv(name); v !=
nullptr)
265 constexpr FeeUnit32 Config::TRANSACTION_FEE_BASE;
268 : j_(
beast::Journal::getNullSink()), ramSize_(detail::getMemorySize())
277 QUIET = bQuiet || bSilent;
286 auto const& threshold =
290 threshold.second.begin(),
291 threshold.second.end(),
293 return (ramSize_ / (1024 * 1024 * 1024)) < limit;
296 if (ns != threshold.second.end())
321 boost::filesystem::path dataDir;
334 if (!strConf.
empty())
335 strConfFile = strConf;
339 if (!strConf.
empty())
349 CONFIG_DIR = boost::filesystem::current_path();
356 auto strXdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
357 auto strXdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
361 || (strHome.empty() &&
362 (strXdgConfigHome.empty() || strXdgDataHome.empty())))
368 if (strXdgConfigHome.empty())
371 strXdgConfigHome = strHome +
"/.config";
374 if (strXdgDataHome.empty())
377 strXdgDataHome = strHome +
"/.local/share";
382 dataDir = strXdgDataHome +
"/" +
systemName();
404 dataDir = boost::filesystem::path(dbPath);
409 if (!dataDir.empty())
411 boost::system::error_code ec;
412 boost::filesystem::create_directories(dataDir, ec);
415 Throw<std::runtime_error>(
416 boost::str(boost::format(
"Can not create %s") % dataDir));
418 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
443 boost::system::error_code ec;
476 boost::filesystem::path p(dbPath);
477 legacy(
"database_path", boost::filesystem::absolute(p).
string());
488 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
495 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
496 if (*peers_in_max > 1000)
497 Throw<std::runtime_error>(
498 "Invalid value specified in [" SECTION_PEERS_IN_MAX
499 "] section; the value must be less or equal than 1000");
505 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
506 if (*peers_out_max < 10 || *peers_out_max > 1000)
507 Throw<std::runtime_error>(
508 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
509 "] section; the value must be in range 10-1000");
513 if ((peers_in_max && !peers_out_max) ||
514 (peers_out_max && !peers_in_max))
515 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
517 "and [" SECTION_PEERS_OUT_MAX
518 "] must be configured");
520 if (peers_in_max && peers_out_max)
529 if (boost::iequals(strTemp,
"tiny"))
531 else if (boost::iequals(strTemp,
"small"))
533 else if (boost::iequals(strTemp,
"medium"))
535 else if (boost::iequals(strTemp,
"large"))
537 else if (boost::iequals(strTemp,
"huge"))
541 4, beast::lexicalCastThrow<std::size_t>(strTemp));
548 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
554 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
558 if (boost::iequals(strTemp,
"all"))
560 else if (boost::iequals(strTemp,
"trusted"))
562 else if (boost::iequals(strTemp,
"drop_untrusted"))
565 Throw<std::runtime_error>(
566 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
572 if (boost::iequals(strTemp,
"all"))
574 else if (boost::iequals(strTemp,
"trusted"))
576 else if (boost::iequals(strTemp,
"drop_untrusted"))
579 Throw<std::runtime_error>(
580 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
584 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
585 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
586 "] and [" SECTION_VALIDATOR_TOKEN
587 "] config sections");
599 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
603 if (boost::iequals(strTemp,
"full"))
606 else if (boost::iequals(strTemp,
"none"))
614 if (boost::iequals(strTemp,
"none"))
616 else if (boost::iequals(strTemp,
"full"))
619 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
627 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
633 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
646 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
647 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
648 ": must be between 10 and 600 inclusive");
653 WORKERS = beast::lexicalCastThrow<int>(strTemp);
655 if (WORKERS < 1 || WORKERS > 1024)
656 Throw<std::runtime_error>(
657 "Invalid " SECTION_WORKERS
658 ": must be between 1 and 1024 inclusive.");
663 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
665 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
666 Throw<std::runtime_error>(
667 "Invalid " SECTION_IO_WORKERS
668 ": must be between 1 and 1024 inclusive.");
675 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
676 Throw<std::runtime_error>(
677 "Invalid " SECTION_PREFETCH_WORKERS
678 ": must be between 1 and 1024 inclusive.");
682 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
687 if (
exists(SECTION_REDUCE_RELAY))
689 auto sec =
section(SECTION_REDUCE_RELAY);
696 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
698 Throw<std::runtime_error>(
699 "Invalid " SECTION_REDUCE_RELAY
700 ", tx_min_peers must be greater or equal to 10"
701 ", tx_relay_percentage must be greater or equal to 10 "
702 "and less or equal to 100");
708 beast::lexicalCastThrow<int>(strTemp),
717 Throw<std::runtime_error>(
718 "Invalid " SECTION_SERVER_DOMAIN
719 ": the domain name does not appear to meet the requirements.");
725 if (
exists(SECTION_OVERLAY))
727 auto const sec =
section(SECTION_OVERLAY);
733 if (
auto val = sec.get(
"max_unknown_time"))
735 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
739 Throw<std::runtime_error>(
740 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
741 ": must be of the form '<number>' representing seconds.");
745 Throw<std::runtime_error>(
746 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
747 ": the time must be between 300 and 1800 seconds, inclusive.");
751 if (
auto val = sec.get(
"max_diverged_time"))
753 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
757 Throw<std::runtime_error>(
758 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
759 ": must be of the form '<number>' representing seconds.");
764 Throw<std::runtime_error>(
765 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
766 ": the time must be between 60 and 900 seconds, inclusive.");
771 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
774 boost::regex
const re(
775 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
777 if (!boost::regex_match(strTemp, match, re))
778 Throw<std::runtime_error>(
779 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
780 ", must be: [0-9]+ [minutes|hours|days|weeks]");
783 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
785 if (boost::iequals(match[2],
"minutes"))
787 else if (boost::iequals(match[2],
"hours"))
789 else if (boost::iequals(match[2],
"days"))
791 else if (boost::iequals(match[2],
"weeks"))
795 Throw<std::runtime_error>(
796 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
797 ", the minimum amount of time an amendment must hold a "
798 "majority is 15 minutes");
815 boost::filesystem::path validatorsFile;
819 validatorsFile = strTemp;
821 if (validatorsFile.empty())
822 Throw<std::runtime_error>(
823 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
825 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
828 if (!boost::filesystem::exists(validatorsFile))
829 Throw<std::runtime_error>(
830 "The file specified in [" SECTION_VALIDATORS_FILE
833 validatorsFile.string());
836 !boost::filesystem::is_regular_file(validatorsFile) &&
837 !boost::filesystem::is_symlink(validatorsFile))
838 Throw<std::runtime_error>(
839 "Invalid file specified in [" SECTION_VALIDATORS_FILE
841 validatorsFile.string());
847 if (!validatorsFile.empty())
849 if (!boost::filesystem::exists(validatorsFile))
850 validatorsFile.clear();
852 !boost::filesystem::is_regular_file(validatorsFile) &&
853 !boost::filesystem::is_symlink(validatorsFile))
854 validatorsFile.clear();
858 if (!validatorsFile.empty() &&
859 boost::filesystem::exists(validatorsFile) &&
860 (boost::filesystem::is_regular_file(validatorsFile) ||
861 boost::filesystem::is_symlink(validatorsFile)))
863 boost::system::error_code ec;
867 Throw<std::runtime_error>(
868 "Failed to read '" + validatorsFile.string() +
"'." +
885 auto valSiteEntries =
889 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
897 if (!entries && !valKeyEntries && !valListKeys)
898 Throw<std::runtime_error>(
899 "The file specified in [" SECTION_VALIDATORS_FILE
901 "does not contain a [" SECTION_VALIDATORS
903 "[" SECTION_VALIDATOR_KEYS
905 "[" SECTION_VALIDATOR_LIST_KEYS
908 validatorsFile.string());
915 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
916 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
918 Throw<std::runtime_error>(
920 "] config section is missing");
925 auto const part =
section(
"features");
926 for (
auto const& s : part.values())
931 Throw<std::runtime_error>(
932 "Unknown feature: " + s +
" in config file.");
948 Throw<std::runtime_error>(
949 "The minimum number of required peers (network_quorum) exceeds "
950 "the maximum number of allowed peers (peers_max)");
955 boost::filesystem::path
960 if (!log_file.empty() && !log_file.is_absolute())
964 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
967 if (!log_file.empty())
969 auto log_dir = log_file.parent_path();
971 if (!boost::filesystem::is_directory(log_dir))
973 boost::system::error_code ec;
974 boost::filesystem::create_directories(log_dir, ec);
980 std::cerr <<
"Unable to create log file path " << log_dir
981 <<
": " << ec.message() <<
'\n';
994 assert(!node || *node <= 4);