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)
137 []() constexpr->bool {
138 std::underlying_type_t<SizedItem> idx = 0;
140 for (auto const& i : sizedItems)
142 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
150 "Mismatch between sized item enum & array indices");
157 #define SECTION_DEFAULT_NAME ""
167 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
170 boost::algorithm::replace_all(strData,
"\r",
"\n");
172 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
178 secResult[strSection] = IniFileSections::mapped_type();
181 for (
auto& strValue : vLines)
184 boost::algorithm::trim(strValue);
186 if (strValue.empty() || strValue[0] ==
'#')
190 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
193 strSection = strValue.
substr(1, strValue.length() - 2);
194 secResult.
emplace(strSection, IniFileSections::mapped_type{});
199 if (!strValue.empty())
200 secResult[strSection].push_back(strValue);
207 IniFileSections::mapped_type*
210 IniFileSections::iterator it;
211 IniFileSections::mapped_type* smtResult;
212 it = secSource.
find(strSection);
213 if (it == secSource.
end())
216 smtResult = &(it->second);
227 IniFileSections::mapped_type* pmtEntries =
229 bool bSingle = pmtEntries && 1 == pmtEntries->size();
233 strValue = (*pmtEntries)[0];
237 JLOG(j.
warn()) << boost::str(
238 boost::format(
"Section [%s]: requires 1 line not %d lines.") %
239 strSection % pmtEntries->
size());
251 char const*
const Config::configFileName =
"rippled.cfg";
252 char const*
const Config::databaseDirName =
"db";
253 char const*
const Config::validatorsFileName =
"validators.txt";
260 if (
auto const v =
std::getenv(name); v !=
nullptr)
267 : j_(
beast::Journal::getNullSink()), ramSize_(detail::getMemorySize())
276 QUIET = bQuiet || bSilent;
285 auto const& threshold =
289 threshold.second.begin(),
290 threshold.second.end(),
292 return (ramSize_ / (1024 * 1024 * 1024)) < limit;
295 if (ns != threshold.second.end())
320 boost::filesystem::path dataDir;
333 if (!strConf.
empty())
334 strConfFile = strConf;
338 if (!strConf.
empty())
348 CONFIG_DIR = boost::filesystem::current_path();
355 auto strXdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
356 auto strXdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
360 || (strHome.empty() &&
361 (strXdgConfigHome.empty() || strXdgDataHome.empty())))
367 if (strXdgConfigHome.empty())
370 strXdgConfigHome = strHome +
"/.config";
373 if (strXdgDataHome.empty())
376 strXdgDataHome = strHome +
"/.local/share";
381 dataDir = strXdgDataHome +
"/" +
systemName();
403 dataDir = boost::filesystem::path(dbPath);
408 if (!dataDir.empty())
410 boost::system::error_code ec;
411 boost::filesystem::create_directories(dataDir, ec);
414 Throw<std::runtime_error>(
415 boost::str(boost::format(
"Can not create %s") % dataDir));
417 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
442 boost::system::error_code ec;
475 boost::filesystem::path p(dbPath);
476 legacy(
"database_path", boost::filesystem::absolute(p).
string());
487 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
494 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
495 if (*peers_in_max > 1000)
496 Throw<std::runtime_error>(
497 "Invalid value specified in [" SECTION_PEERS_IN_MAX
498 "] section; the value must be less or equal than 1000");
504 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
505 if (*peers_out_max < 10 || *peers_out_max > 1000)
506 Throw<std::runtime_error>(
507 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
508 "] section; the value must be in range 10-1000");
512 if ((peers_in_max && !peers_out_max) ||
513 (peers_out_max && !peers_in_max))
514 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
516 "and [" SECTION_PEERS_OUT_MAX
517 "] must be configured");
519 if (peers_in_max && peers_out_max)
528 if (boost::iequals(strTemp,
"tiny"))
530 else if (boost::iequals(strTemp,
"small"))
532 else if (boost::iequals(strTemp,
"medium"))
534 else if (boost::iequals(strTemp,
"large"))
536 else if (boost::iequals(strTemp,
"huge"))
540 4, beast::lexicalCastThrow<std::size_t>(strTemp));
547 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
553 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
557 if (boost::iequals(strTemp,
"all"))
559 else if (boost::iequals(strTemp,
"trusted"))
561 else if (boost::iequals(strTemp,
"drop_untrusted"))
564 Throw<std::runtime_error>(
565 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
571 if (boost::iequals(strTemp,
"all"))
573 else if (boost::iequals(strTemp,
"trusted"))
575 else if (boost::iequals(strTemp,
"drop_untrusted"))
578 Throw<std::runtime_error>(
579 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
583 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
584 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
585 "] and [" SECTION_VALIDATOR_TOKEN
586 "] config sections");
598 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
602 if (boost::iequals(strTemp,
"full"))
605 else if (boost::iequals(strTemp,
"none"))
613 if (boost::iequals(strTemp,
"none"))
615 else if (boost::iequals(strTemp,
"full"))
618 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
626 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
632 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
645 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
646 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
647 ": must be between 10 and 600 inclusive");
652 WORKERS = beast::lexicalCastThrow<int>(strTemp);
654 if (WORKERS < 1 || WORKERS > 1024)
655 Throw<std::runtime_error>(
656 "Invalid " SECTION_WORKERS
657 ": must be between 1 and 1024 inclusive.");
662 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
664 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
665 Throw<std::runtime_error>(
666 "Invalid " SECTION_IO_WORKERS
667 ": must be between 1 and 1024 inclusive.");
674 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
675 Throw<std::runtime_error>(
676 "Invalid " SECTION_PREFETCH_WORKERS
677 ": must be between 1 and 1024 inclusive.");
681 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
686 if (
exists(SECTION_REDUCE_RELAY))
688 auto sec =
section(SECTION_REDUCE_RELAY);
695 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
697 Throw<std::runtime_error>(
698 "Invalid " SECTION_REDUCE_RELAY
699 ", tx_min_peers must be greater or equal to 10"
700 ", tx_relay_percentage must be greater or equal to 10 "
701 "and less or equal to 100");
707 beast::lexicalCastThrow<int>(strTemp),
716 Throw<std::runtime_error>(
717 "Invalid " SECTION_SERVER_DOMAIN
718 ": the domain name does not appear to meet the requirements.");
724 if (
exists(SECTION_OVERLAY))
726 auto const sec =
section(SECTION_OVERLAY);
732 if (
auto val = sec.get(
"max_unknown_time"))
734 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
738 Throw<std::runtime_error>(
739 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
740 ": must be of the form '<number>' representing seconds.");
744 Throw<std::runtime_error>(
745 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
746 ": the time must be between 300 and 1800 seconds, inclusive.");
750 if (
auto val = sec.get(
"max_diverged_time"))
752 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
756 Throw<std::runtime_error>(
757 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
758 ": must be of the form '<number>' representing seconds.");
763 Throw<std::runtime_error>(
764 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
765 ": the time must be between 60 and 900 seconds, inclusive.");
770 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
773 boost::regex
const re(
774 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
776 if (!boost::regex_match(strTemp, match, re))
777 Throw<std::runtime_error>(
778 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
779 ", must be: [0-9]+ [minutes|hours|days|weeks]");
782 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
784 if (boost::iequals(match[2],
"minutes"))
786 else if (boost::iequals(match[2],
"hours"))
788 else if (boost::iequals(match[2],
"days"))
790 else if (boost::iequals(match[2],
"weeks"))
794 Throw<std::runtime_error>(
795 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
796 ", the minimum amount of time an amendment must hold a "
797 "majority is 15 minutes");
814 boost::filesystem::path validatorsFile;
818 validatorsFile = strTemp;
820 if (validatorsFile.empty())
821 Throw<std::runtime_error>(
822 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
824 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
827 if (!boost::filesystem::exists(validatorsFile))
828 Throw<std::runtime_error>(
829 "The file specified in [" SECTION_VALIDATORS_FILE
832 validatorsFile.string());
835 !boost::filesystem::is_regular_file(validatorsFile) &&
836 !boost::filesystem::is_symlink(validatorsFile))
837 Throw<std::runtime_error>(
838 "Invalid file specified in [" SECTION_VALIDATORS_FILE
840 validatorsFile.string());
846 if (!validatorsFile.empty())
848 if (!boost::filesystem::exists(validatorsFile))
849 validatorsFile.clear();
851 !boost::filesystem::is_regular_file(validatorsFile) &&
852 !boost::filesystem::is_symlink(validatorsFile))
853 validatorsFile.clear();
857 if (!validatorsFile.empty() &&
858 boost::filesystem::exists(validatorsFile) &&
859 (boost::filesystem::is_regular_file(validatorsFile) ||
860 boost::filesystem::is_symlink(validatorsFile)))
862 boost::system::error_code ec;
866 Throw<std::runtime_error>(
867 "Failed to read '" + validatorsFile.string() +
"'." +
884 auto valSiteEntries =
888 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
896 if (!entries && !valKeyEntries && !valListKeys)
897 Throw<std::runtime_error>(
898 "The file specified in [" SECTION_VALIDATORS_FILE
900 "does not contain a [" SECTION_VALIDATORS
902 "[" SECTION_VALIDATOR_KEYS
904 "[" SECTION_VALIDATOR_LIST_KEYS
907 validatorsFile.string());
914 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
915 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
917 Throw<std::runtime_error>(
919 "] config section is missing");
924 auto const part =
section(
"features");
925 for (
auto const& s : part.values())
930 Throw<std::runtime_error>(
931 "Unknown feature: " + s +
" in config file.");
947 Throw<std::runtime_error>(
948 "The minimum number of required peers (network_quorum) exceeds "
949 "the maximum number of allowed peers (peers_max)");
954 boost::filesystem::path
959 if (!log_file.empty() && !log_file.is_absolute())
963 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
966 if (!log_file.empty())
968 auto log_dir = log_file.parent_path();
970 if (!boost::filesystem::is_directory(log_dir))
972 boost::system::error_code ec;
973 boost::filesystem::create_directories(log_dir, ec);
979 std::cerr <<
"Unable to create log file path " << log_dir
980 <<
": " << ec.message() <<
'\n';
993 assert(!node || *node <= 4);