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");
621 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
625 if (boost::iequals(strTemp,
"full"))
628 else if (boost::iequals(strTemp,
"none"))
636 if (boost::iequals(strTemp,
"none"))
638 else if (boost::iequals(strTemp,
"full"))
641 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
649 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
655 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
668 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
669 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
670 ": must be between 10 and 600 inclusive");
675 WORKERS = beast::lexicalCastThrow<int>(strTemp);
677 if (WORKERS < 1 || WORKERS > 1024)
678 Throw<std::runtime_error>(
679 "Invalid " SECTION_WORKERS
680 ": must be between 1 and 1024 inclusive.");
685 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
687 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
688 Throw<std::runtime_error>(
689 "Invalid " SECTION_IO_WORKERS
690 ": must be between 1 and 1024 inclusive.");
697 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
698 Throw<std::runtime_error>(
699 "Invalid " SECTION_PREFETCH_WORKERS
700 ": must be between 1 and 1024 inclusive.");
704 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
709 if (
exists(SECTION_REDUCE_RELAY))
711 auto sec =
section(SECTION_REDUCE_RELAY);
718 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
720 Throw<std::runtime_error>(
721 "Invalid " SECTION_REDUCE_RELAY
722 ", tx_min_peers must be greater or equal to 10"
723 ", tx_relay_percentage must be greater or equal to 10 "
724 "and less or equal to 100");
730 beast::lexicalCastThrow<int>(strTemp),
739 Throw<std::runtime_error>(
740 "Invalid " SECTION_SERVER_DOMAIN
741 ": the domain name does not appear to meet the requirements.");
747 if (
exists(SECTION_OVERLAY))
749 auto const sec =
section(SECTION_OVERLAY);
755 if (
auto val = sec.get(
"max_unknown_time"))
757 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
761 Throw<std::runtime_error>(
762 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
763 ": must be of the form '<number>' representing seconds.");
767 Throw<std::runtime_error>(
768 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
769 ": the time must be between 300 and 1800 seconds, inclusive.");
773 if (
auto val = sec.get(
"max_diverged_time"))
775 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
779 Throw<std::runtime_error>(
780 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
781 ": must be of the form '<number>' representing seconds.");
786 Throw<std::runtime_error>(
787 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
788 ": the time must be between 60 and 900 seconds, inclusive.");
793 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
796 boost::regex
const re(
797 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
799 if (!boost::regex_match(strTemp, match, re))
800 Throw<std::runtime_error>(
801 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
802 ", must be: [0-9]+ [minutes|hours|days|weeks]");
805 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
807 if (boost::iequals(match[2],
"minutes"))
809 else if (boost::iequals(match[2],
"hours"))
811 else if (boost::iequals(match[2],
"days"))
813 else if (boost::iequals(match[2],
"weeks"))
817 Throw<std::runtime_error>(
818 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
819 ", the minimum amount of time an amendment must hold a "
820 "majority is 15 minutes");
837 boost::filesystem::path validatorsFile;
841 validatorsFile = strTemp;
843 if (validatorsFile.empty())
844 Throw<std::runtime_error>(
845 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
847 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
850 if (!boost::filesystem::exists(validatorsFile))
851 Throw<std::runtime_error>(
852 "The file specified in [" SECTION_VALIDATORS_FILE
855 validatorsFile.string());
858 !boost::filesystem::is_regular_file(validatorsFile) &&
859 !boost::filesystem::is_symlink(validatorsFile))
860 Throw<std::runtime_error>(
861 "Invalid file specified in [" SECTION_VALIDATORS_FILE
863 validatorsFile.string());
869 if (!validatorsFile.empty())
871 if (!boost::filesystem::exists(validatorsFile))
872 validatorsFile.clear();
874 !boost::filesystem::is_regular_file(validatorsFile) &&
875 !boost::filesystem::is_symlink(validatorsFile))
876 validatorsFile.clear();
880 if (!validatorsFile.empty() &&
881 boost::filesystem::exists(validatorsFile) &&
882 (boost::filesystem::is_regular_file(validatorsFile) ||
883 boost::filesystem::is_symlink(validatorsFile)))
885 boost::system::error_code ec;
889 Throw<std::runtime_error>(
890 "Failed to read '" + validatorsFile.string() +
"'." +
907 auto valSiteEntries =
911 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
919 if (!entries && !valKeyEntries && !valListKeys)
920 Throw<std::runtime_error>(
921 "The file specified in [" SECTION_VALIDATORS_FILE
923 "does not contain a [" SECTION_VALIDATORS
925 "[" SECTION_VALIDATOR_KEYS
927 "[" SECTION_VALIDATOR_LIST_KEYS
930 validatorsFile.string());
937 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
938 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
940 Throw<std::runtime_error>(
942 "] config section is missing");
947 auto const part =
section(
"features");
948 for (
auto const& s : part.values())
953 Throw<std::runtime_error>(
954 "Unknown feature: " + s +
" in config file.");
970 Throw<std::runtime_error>(
971 "The minimum number of required peers (network_quorum) exceeds "
972 "the maximum number of allowed peers (peers_max)");
977 boost::filesystem::path
982 if (!log_file.empty() && !log_file.is_absolute())
986 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
989 if (!log_file.empty())
991 auto log_dir = log_file.parent_path();
993 if (!boost::filesystem::is_directory(log_dir))
995 boost::system::error_code ec;
996 boost::filesystem::create_directories(log_dir, ec);
1002 std::cerr <<
"Unable to create log file path " << log_dir
1003 <<
": " << ec.message() <<
'\n';
1016 assert(!node || *node <= 4);