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>
44 #include <sysinfoapi.h>
52 if (MEMORYSTATUSEX msx{
sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
63 #include <sys/sysinfo.h>
73 if (sysinfo(&si) == 0)
85 #include <sys/sysctl.h>
86 #include <sys/types.h>
94 int mib[] = {CTL_HW, HW_MEMSIZE};
96 size_t size =
sizeof(ram);
98 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
138 []() constexpr->bool {
139 std::underlying_type_t<SizedItem> idx = 0;
141 for (auto const& i : sizedItems)
143 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
151 "Mismatch between sized item enum & array indices");
158 #define SECTION_DEFAULT_NAME ""
168 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
171 boost::algorithm::replace_all(strData,
"\r",
"\n");
173 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
179 secResult[strSection] = IniFileSections::mapped_type();
182 for (
auto& strValue : vLines)
185 boost::algorithm::trim(strValue);
187 if (strValue.empty() || strValue[0] ==
'#')
191 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
194 strSection = strValue.
substr(1, strValue.length() - 2);
195 secResult.
emplace(strSection, IniFileSections::mapped_type{});
200 if (!strValue.empty())
201 secResult[strSection].push_back(strValue);
208 IniFileSections::mapped_type*
211 IniFileSections::iterator it;
212 IniFileSections::mapped_type* smtResult;
213 it = secSource.
find(strSection);
214 if (it == secSource.
end())
217 smtResult = &(it->second);
228 IniFileSections::mapped_type* pmtEntries =
230 bool bSingle = pmtEntries && 1 == pmtEntries->size();
234 strValue = (*pmtEntries)[0];
238 JLOG(j.
warn()) << boost::str(
239 boost::format(
"Section [%s]: requires 1 line not %d lines.") %
240 strSection % pmtEntries->
size());
252 char const*
const Config::configFileName =
"rippled.cfg";
253 char const*
const Config::databaseDirName =
"db";
254 char const*
const Config::validatorsFileName =
"validators.txt";
261 if (
auto const v =
std::getenv(name); v !=
nullptr)
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 for (
auto& line : strVec)
479 if (
std::count(line.begin(), line.end(),
':') != 1)
485 if (result.
size() == line.size())
498 boost::filesystem::path p(dbPath);
499 legacy(
"database_path", boost::filesystem::absolute(p).
string());
510 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
517 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
518 if (*peers_in_max > 1000)
519 Throw<std::runtime_error>(
520 "Invalid value specified in [" SECTION_PEERS_IN_MAX
521 "] section; the value must be less or equal than 1000");
527 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
528 if (*peers_out_max < 10 || *peers_out_max > 1000)
529 Throw<std::runtime_error>(
530 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
531 "] section; the value must be in range 10-1000");
535 if ((peers_in_max && !peers_out_max) ||
536 (peers_out_max && !peers_in_max))
537 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
539 "and [" SECTION_PEERS_OUT_MAX
540 "] must be configured");
542 if (peers_in_max && peers_out_max)
551 if (boost::iequals(strTemp,
"tiny"))
553 else if (boost::iequals(strTemp,
"small"))
555 else if (boost::iequals(strTemp,
"medium"))
557 else if (boost::iequals(strTemp,
"large"))
559 else if (boost::iequals(strTemp,
"huge"))
563 4, beast::lexicalCastThrow<std::size_t>(strTemp));
570 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
576 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
580 if (boost::iequals(strTemp,
"all"))
582 else if (boost::iequals(strTemp,
"trusted"))
584 else if (boost::iequals(strTemp,
"drop_untrusted"))
587 Throw<std::runtime_error>(
588 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
594 if (boost::iequals(strTemp,
"all"))
596 else if (boost::iequals(strTemp,
"trusted"))
598 else if (boost::iequals(strTemp,
"drop_untrusted"))
601 Throw<std::runtime_error>(
602 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
606 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
607 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
608 "] and [" SECTION_VALIDATOR_TOKEN
609 "] config sections");
623 if (boost::iequals(strTemp,
"full"))
626 else if (boost::iequals(strTemp,
"none"))
634 if (boost::iequals(strTemp,
"none"))
636 else if (boost::iequals(strTemp,
"full"))
639 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
647 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
653 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
666 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
667 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
668 ": must be between 10 and 600 inclusive");
673 WORKERS = beast::lexicalCastThrow<int>(strTemp);
675 if (WORKERS < 1 || WORKERS > 1024)
676 Throw<std::runtime_error>(
677 "Invalid " SECTION_WORKERS
678 ": must be between 1 and 1024 inclusive.");
683 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
685 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
686 Throw<std::runtime_error>(
687 "Invalid " SECTION_IO_WORKERS
688 ": must be between 1 and 1024 inclusive.");
695 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
696 Throw<std::runtime_error>(
697 "Invalid " SECTION_PREFETCH_WORKERS
698 ": must be between 1 and 1024 inclusive.");
702 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
707 if (
exists(SECTION_REDUCE_RELAY))
709 auto sec =
section(SECTION_REDUCE_RELAY);
716 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
718 Throw<std::runtime_error>(
719 "Invalid " SECTION_REDUCE_RELAY
720 ", tx_min_peers must be greater or equal to 10"
721 ", tx_relay_percentage must be greater or equal to 10 "
722 "and less or equal to 100");
728 beast::lexicalCastThrow<int>(strTemp),
737 Throw<std::runtime_error>(
738 "Invalid " SECTION_SERVER_DOMAIN
739 ": the domain name does not appear to meet the requirements.");
745 if (
exists(SECTION_OVERLAY))
747 auto const sec =
section(SECTION_OVERLAY);
753 if (
auto val = sec.get(
"max_unknown_time"))
755 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
759 Throw<std::runtime_error>(
760 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
761 ": must be of the form '<number>' representing seconds.");
765 Throw<std::runtime_error>(
766 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
767 ": the time must be between 300 and 1800 seconds, inclusive.");
771 if (
auto val = sec.get(
"max_diverged_time"))
773 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
777 Throw<std::runtime_error>(
778 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
779 ": must be of the form '<number>' representing seconds.");
784 Throw<std::runtime_error>(
785 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
786 ": the time must be between 60 and 900 seconds, inclusive.");
791 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
794 boost::regex
const re(
795 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
797 if (!boost::regex_match(strTemp, match, re))
798 Throw<std::runtime_error>(
799 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
800 ", must be: [0-9]+ [minutes|hours|days|weeks]");
803 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
805 if (boost::iequals(match[2],
"minutes"))
807 else if (boost::iequals(match[2],
"hours"))
809 else if (boost::iequals(match[2],
"days"))
811 else if (boost::iequals(match[2],
"weeks"))
815 Throw<std::runtime_error>(
816 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
817 ", the minimum amount of time an amendment must hold a "
818 "majority is 15 minutes");
835 boost::filesystem::path validatorsFile;
839 validatorsFile = strTemp;
841 if (validatorsFile.empty())
842 Throw<std::runtime_error>(
843 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
845 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
848 if (!boost::filesystem::exists(validatorsFile))
849 Throw<std::runtime_error>(
850 "The file specified in [" SECTION_VALIDATORS_FILE
853 validatorsFile.string());
856 !boost::filesystem::is_regular_file(validatorsFile) &&
857 !boost::filesystem::is_symlink(validatorsFile))
858 Throw<std::runtime_error>(
859 "Invalid file specified in [" SECTION_VALIDATORS_FILE
861 validatorsFile.string());
867 if (!validatorsFile.empty())
869 if (!boost::filesystem::exists(validatorsFile))
870 validatorsFile.clear();
872 !boost::filesystem::is_regular_file(validatorsFile) &&
873 !boost::filesystem::is_symlink(validatorsFile))
874 validatorsFile.clear();
878 if (!validatorsFile.empty() &&
879 boost::filesystem::exists(validatorsFile) &&
880 (boost::filesystem::is_regular_file(validatorsFile) ||
881 boost::filesystem::is_symlink(validatorsFile)))
883 boost::system::error_code ec;
887 Throw<std::runtime_error>(
888 "Failed to read '" + validatorsFile.string() +
"'." +
905 auto valSiteEntries =
909 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
917 if (!entries && !valKeyEntries && !valListKeys)
918 Throw<std::runtime_error>(
919 "The file specified in [" SECTION_VALIDATORS_FILE
921 "does not contain a [" SECTION_VALIDATORS
923 "[" SECTION_VALIDATOR_KEYS
925 "[" SECTION_VALIDATOR_LIST_KEYS
928 validatorsFile.string());
935 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
936 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
938 Throw<std::runtime_error>(
940 "] config section is missing");
945 auto const part =
section(
"features");
946 for (
auto const& s : part.values())
951 Throw<std::runtime_error>(
952 "Unknown feature: " + s +
" in config file.");
968 Throw<std::runtime_error>(
969 "The minimum number of required peers (network_quorum) exceeds "
970 "the maximum number of allowed peers (peers_max)");
975 boost::filesystem::path
980 if (!log_file.empty() && !log_file.is_absolute())
984 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
987 if (!log_file.empty())
989 auto log_dir = log_file.parent_path();
991 if (!boost::filesystem::is_directory(log_dir))
993 boost::system::error_code ec;
994 boost::filesystem::create_directories(log_dir, ec);
1000 std::cerr <<
"Unable to create log file path " << log_dir
1001 <<
": " << ec.message() <<
'\n';
1014 assert(!node || *node <= 4);
1026 setup.reference_fee = temp;
1031 setup.account_reserve = temp;
1033 setup.owner_reserve = temp;