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());
440 boost::system::error_code ec;
473 boost::filesystem::path p(dbPath);
474 legacy(
"database_path", boost::filesystem::absolute(p).
string());
485 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
492 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
493 if (*peers_in_max > 1000)
494 Throw<std::runtime_error>(
495 "Invalid value specified in [" SECTION_PEERS_IN_MAX
496 "] section; the value must be less or equal than 1000");
502 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
503 if (*peers_out_max < 10 || *peers_out_max > 1000)
504 Throw<std::runtime_error>(
505 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
506 "] section; the value must be in range 10-1000");
510 if ((peers_in_max && !peers_out_max) ||
511 (peers_out_max && !peers_in_max))
512 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
514 "and [" SECTION_PEERS_OUT_MAX
515 "] must be configured");
517 if (peers_in_max && peers_out_max)
526 if (boost::iequals(strTemp,
"tiny"))
528 else if (boost::iequals(strTemp,
"small"))
530 else if (boost::iequals(strTemp,
"medium"))
532 else if (boost::iequals(strTemp,
"large"))
534 else if (boost::iequals(strTemp,
"huge"))
538 4, beast::lexicalCastThrow<std::size_t>(strTemp));
545 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
551 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
555 if (boost::iequals(strTemp,
"all"))
557 else if (boost::iequals(strTemp,
"trusted"))
560 Throw<std::runtime_error>(
561 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
567 if (boost::iequals(strTemp,
"all"))
569 else if (boost::iequals(strTemp,
"trusted"))
572 Throw<std::runtime_error>(
573 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
577 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
578 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
579 "] and [" SECTION_VALIDATOR_TOKEN
580 "] config sections");
592 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
596 if (boost::iequals(strTemp,
"full"))
599 else if (boost::iequals(strTemp,
"none"))
607 if (boost::iequals(strTemp,
"none"))
609 else if (boost::iequals(strTemp,
"full"))
612 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
621 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
631 WORKERS = beast::lexicalCastThrow<std::size_t>(strTemp);
634 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
639 if (
exists(SECTION_REDUCE_RELAY))
641 auto sec =
section(SECTION_REDUCE_RELAY);
649 beast::lexicalCastThrow<int>(strTemp),
658 Throw<std::runtime_error>(
659 "Invalid " SECTION_SERVER_DOMAIN
660 ": the domain name does not appear to meet the requirements.");
666 if (
exists(SECTION_OVERLAY))
668 auto const sec =
section(SECTION_OVERLAY);
674 if (
auto val = sec.get<
std::string>(
"max_unknown_time"))
676 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
680 Throw<std::runtime_error>(
681 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
682 ": must be of the form '<number>' representing seconds.");
686 Throw<std::runtime_error>(
687 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
688 ": the time must be between 300 and 1800 seconds, inclusive.");
692 if (
auto val = sec.get<
std::string>(
"max_diverged_time"))
694 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
698 Throw<std::runtime_error>(
699 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
700 ": must be of the form '<number>' representing seconds.");
705 Throw<std::runtime_error>(
706 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
707 ": the time must be between 60 and 900 seconds, inclusive.");
712 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
715 boost::regex
const re(
716 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
718 if (!boost::regex_match(strTemp, match, re))
719 Throw<std::runtime_error>(
720 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
721 ", must be: [0-9]+ [minutes|hours|days|weeks]");
724 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
726 if (boost::iequals(match[2],
"minutes"))
728 else if (boost::iequals(match[2],
"hours"))
730 else if (boost::iequals(match[2],
"days"))
732 else if (boost::iequals(match[2],
"weeks"))
736 Throw<std::runtime_error>(
737 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
738 ", the minimum amount of time an amendment must hold a "
739 "majority is 15 minutes");
756 boost::filesystem::path validatorsFile;
760 validatorsFile = strTemp;
762 if (validatorsFile.empty())
763 Throw<std::runtime_error>(
764 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
766 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
769 if (!boost::filesystem::exists(validatorsFile))
770 Throw<std::runtime_error>(
771 "The file specified in [" SECTION_VALIDATORS_FILE
774 validatorsFile.string());
777 !boost::filesystem::is_regular_file(validatorsFile) &&
778 !boost::filesystem::is_symlink(validatorsFile))
779 Throw<std::runtime_error>(
780 "Invalid file specified in [" SECTION_VALIDATORS_FILE
782 validatorsFile.string());
788 if (!validatorsFile.empty())
790 if (!boost::filesystem::exists(validatorsFile))
791 validatorsFile.clear();
793 !boost::filesystem::is_regular_file(validatorsFile) &&
794 !boost::filesystem::is_symlink(validatorsFile))
795 validatorsFile.clear();
799 if (!validatorsFile.empty() &&
800 boost::filesystem::exists(validatorsFile) &&
801 (boost::filesystem::is_regular_file(validatorsFile) ||
802 boost::filesystem::is_symlink(validatorsFile)))
804 boost::system::error_code ec;
808 Throw<std::runtime_error>(
809 "Failed to read '" + validatorsFile.string() +
"'." +
826 auto valSiteEntries =
830 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
838 if (!entries && !valKeyEntries && !valListKeys)
839 Throw<std::runtime_error>(
840 "The file specified in [" SECTION_VALIDATORS_FILE
842 "does not contain a [" SECTION_VALIDATORS
844 "[" SECTION_VALIDATOR_KEYS
846 "[" SECTION_VALIDATOR_LIST_KEYS
849 validatorsFile.string());
856 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
857 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
859 Throw<std::runtime_error>(
861 "] config section is missing");
866 auto const part =
section(
"features");
867 for (
auto const& s : part.values())
872 Throw<std::runtime_error>(
873 "Unknown feature: " + s +
" in config file.");
889 Throw<std::runtime_error>(
890 "The minimum number of required peers (network_quorum) exceeds "
891 "the maximum number of allowed peers (peers_max)");
896 boost::filesystem::path
901 if (!log_file.empty() && !log_file.is_absolute())
905 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
908 if (!log_file.empty())
910 auto log_dir = log_file.parent_path();
912 if (!boost::filesystem::is_directory(log_dir))
914 boost::system::error_code ec;
915 boost::filesystem::create_directories(log_dir, ec);
921 std::cerr <<
"Unable to create log file path " << log_dir
922 <<
": " << ec.message() <<
'\n';
935 assert(!node || *node <= 4);