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>
71 if (
struct sysinfo si; sysinfo(&si) == 0)
83 #include <sys/sysctl.h>
84 #include <sys/types.h>
92 int mib[] = {CTL_HW, HW_MEMSIZE};
94 size_t size =
sizeof(ram);
96 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)
266 : j_(
beast::Journal::getNullSink())
267 , ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
276 QUIET = bQuiet || bSilent;
285 auto const& threshold =
289 threshold.second.begin(),
290 threshold.second.end(),
292 return (limit == 0) || (ramSize_ < limit);
295 assert(ns != threshold.second.end());
297 if (ns != threshold.second.end())
316 boost::filesystem::path dataDir;
329 if (!strConf.
empty())
330 strConfFile = strConf;
334 if (!strConf.
empty())
344 CONFIG_DIR = boost::filesystem::current_path();
351 auto strXdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
352 auto strXdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
356 || (strHome.empty() &&
357 (strXdgConfigHome.empty() || strXdgDataHome.empty())))
363 if (strXdgConfigHome.empty())
366 strXdgConfigHome = strHome +
"/.config";
369 if (strXdgDataHome.empty())
372 strXdgDataHome = strHome +
"/.local/share";
377 dataDir = strXdgDataHome +
"/" +
systemName();
399 dataDir = boost::filesystem::path(dbPath);
404 if (!dataDir.empty())
406 boost::system::error_code ec;
407 boost::filesystem::create_directories(dataDir, ec);
410 Throw<std::runtime_error>(
411 boost::str(boost::format(
"Can not create %s") % dataDir));
413 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
438 boost::system::error_code ec;
471 for (
auto& line : strVec)
474 if (
std::count(line.begin(), line.end(),
':') != 1)
480 if (result.
size() == line.size())
493 boost::filesystem::path p(dbPath);
494 legacy(
"database_path", boost::filesystem::absolute(p).
string());
502 if (strTemp ==
"main")
504 else if (strTemp ==
"testnet")
506 else if (strTemp ==
"devnet")
509 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
517 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
524 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
525 if (*peers_in_max > 1000)
526 Throw<std::runtime_error>(
527 "Invalid value specified in [" SECTION_PEERS_IN_MAX
528 "] section; the value must be less or equal than 1000");
534 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
535 if (*peers_out_max < 10 || *peers_out_max > 1000)
536 Throw<std::runtime_error>(
537 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
538 "] section; the value must be in range 10-1000");
542 if ((peers_in_max && !peers_out_max) ||
543 (peers_out_max && !peers_in_max))
544 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
546 "and [" SECTION_PEERS_OUT_MAX
547 "] must be configured");
549 if (peers_in_max && peers_out_max)
558 if (boost::iequals(strTemp,
"tiny"))
560 else if (boost::iequals(strTemp,
"small"))
562 else if (boost::iequals(strTemp,
"medium"))
564 else if (boost::iequals(strTemp,
"large"))
566 else if (boost::iequals(strTemp,
"huge"))
570 4, beast::lexicalCastThrow<std::size_t>(strTemp));
577 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
583 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
587 if (boost::iequals(strTemp,
"all"))
589 else if (boost::iequals(strTemp,
"trusted"))
591 else if (boost::iequals(strTemp,
"drop_untrusted"))
594 Throw<std::runtime_error>(
595 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
601 if (boost::iequals(strTemp,
"all"))
603 else if (boost::iequals(strTemp,
"trusted"))
605 else if (boost::iequals(strTemp,
"drop_untrusted"))
608 Throw<std::runtime_error>(
609 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
613 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
614 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
615 "] and [" SECTION_VALIDATOR_TOKEN
616 "] config sections");
630 if (boost::iequals(strTemp,
"full"))
633 else if (boost::iequals(strTemp,
"none"))
641 if (boost::iequals(strTemp,
"none"))
643 else if (boost::iequals(strTemp,
"full"))
646 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
654 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
660 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
673 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
674 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
675 ": must be between 10 and 600 inclusive");
680 WORKERS = beast::lexicalCastThrow<int>(strTemp);
682 if (WORKERS < 1 || WORKERS > 1024)
683 Throw<std::runtime_error>(
684 "Invalid " SECTION_WORKERS
685 ": must be between 1 and 1024 inclusive.");
690 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
692 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
693 Throw<std::runtime_error>(
694 "Invalid " SECTION_IO_WORKERS
695 ": must be between 1 and 1024 inclusive.");
702 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
703 Throw<std::runtime_error>(
704 "Invalid " SECTION_PREFETCH_WORKERS
705 ": must be between 1 and 1024 inclusive.");
709 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
714 if (
exists(SECTION_REDUCE_RELAY))
716 auto sec =
section(SECTION_REDUCE_RELAY);
723 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
725 Throw<std::runtime_error>(
726 "Invalid " SECTION_REDUCE_RELAY
727 ", tx_min_peers must be greater or equal to 10"
728 ", tx_relay_percentage must be greater or equal to 10 "
729 "and less or equal to 100");
735 beast::lexicalCastThrow<int>(strTemp),
744 Throw<std::runtime_error>(
745 "Invalid " SECTION_SERVER_DOMAIN
746 ": the domain name does not appear to meet the requirements.");
752 if (
exists(SECTION_OVERLAY))
754 auto const sec =
section(SECTION_OVERLAY);
760 if (
auto val = sec.get(
"max_unknown_time"))
762 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
766 Throw<std::runtime_error>(
767 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
768 ": must be of the form '<number>' representing seconds.");
772 Throw<std::runtime_error>(
773 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
774 ": the time must be between 300 and 1800 seconds, inclusive.");
778 if (
auto val = sec.get(
"max_diverged_time"))
780 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
784 Throw<std::runtime_error>(
785 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
786 ": must be of the form '<number>' representing seconds.");
791 Throw<std::runtime_error>(
792 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
793 ": the time must be between 60 and 900 seconds, inclusive.");
798 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
801 boost::regex
const re(
802 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
804 if (!boost::regex_match(strTemp, match, re))
805 Throw<std::runtime_error>(
806 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
807 ", must be: [0-9]+ [minutes|hours|days|weeks]");
810 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
812 if (boost::iequals(match[2],
"minutes"))
814 else if (boost::iequals(match[2],
"hours"))
816 else if (boost::iequals(match[2],
"days"))
818 else if (boost::iequals(match[2],
"weeks"))
822 Throw<std::runtime_error>(
823 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
824 ", the minimum amount of time an amendment must hold a "
825 "majority is 15 minutes");
842 boost::filesystem::path validatorsFile;
846 validatorsFile = strTemp;
848 if (validatorsFile.empty())
849 Throw<std::runtime_error>(
850 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
852 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
855 if (!boost::filesystem::exists(validatorsFile))
856 Throw<std::runtime_error>(
857 "The file specified in [" SECTION_VALIDATORS_FILE
860 validatorsFile.string());
863 !boost::filesystem::is_regular_file(validatorsFile) &&
864 !boost::filesystem::is_symlink(validatorsFile))
865 Throw<std::runtime_error>(
866 "Invalid file specified in [" SECTION_VALIDATORS_FILE
868 validatorsFile.string());
874 if (!validatorsFile.empty())
876 if (!boost::filesystem::exists(validatorsFile))
877 validatorsFile.clear();
879 !boost::filesystem::is_regular_file(validatorsFile) &&
880 !boost::filesystem::is_symlink(validatorsFile))
881 validatorsFile.clear();
885 if (!validatorsFile.empty() &&
886 boost::filesystem::exists(validatorsFile) &&
887 (boost::filesystem::is_regular_file(validatorsFile) ||
888 boost::filesystem::is_symlink(validatorsFile)))
890 boost::system::error_code ec;
894 Throw<std::runtime_error>(
895 "Failed to read '" + validatorsFile.string() +
"'." +
912 auto valSiteEntries =
916 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
924 if (!entries && !valKeyEntries && !valListKeys)
925 Throw<std::runtime_error>(
926 "The file specified in [" SECTION_VALIDATORS_FILE
928 "does not contain a [" SECTION_VALIDATORS
930 "[" SECTION_VALIDATOR_KEYS
932 "[" SECTION_VALIDATOR_LIST_KEYS
935 validatorsFile.string());
942 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
943 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
945 Throw<std::runtime_error>(
947 "] config section is missing");
952 auto const part =
section(
"features");
953 for (
auto const& s : part.values())
958 Throw<std::runtime_error>(
959 "Unknown feature: " + s +
" in config file.");
975 Throw<std::runtime_error>(
976 "The minimum number of required peers (network_quorum) exceeds "
977 "the maximum number of allowed peers (peers_max)");
982 boost::filesystem::path
987 if (!log_file.empty() && !log_file.is_absolute())
991 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
994 if (!log_file.empty())
996 auto log_dir = log_file.parent_path();
998 if (!boost::filesystem::is_directory(log_dir))
1000 boost::system::error_code ec;
1001 boost::filesystem::create_directories(log_dir, ec);
1007 std::cerr <<
"Unable to create log file path " << log_dir
1008 <<
": " << ec.message() <<
'\n';
1021 assert(!node || *node <= 4);
1033 setup.reference_fee = temp;
1038 setup.account_reserve = temp;
1040 setup.owner_reserve = temp;