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());
507 if (strTemp ==
"main")
509 else if (strTemp ==
"testnet")
511 else if (strTemp ==
"devnet")
514 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
522 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
529 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
530 if (*peers_in_max > 1000)
531 Throw<std::runtime_error>(
532 "Invalid value specified in [" SECTION_PEERS_IN_MAX
533 "] section; the value must be less or equal than 1000");
539 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
540 if (*peers_out_max < 10 || *peers_out_max > 1000)
541 Throw<std::runtime_error>(
542 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
543 "] section; the value must be in range 10-1000");
547 if ((peers_in_max && !peers_out_max) ||
548 (peers_out_max && !peers_in_max))
549 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
551 "and [" SECTION_PEERS_OUT_MAX
552 "] must be configured");
554 if (peers_in_max && peers_out_max)
563 if (boost::iequals(strTemp,
"tiny"))
565 else if (boost::iequals(strTemp,
"small"))
567 else if (boost::iequals(strTemp,
"medium"))
569 else if (boost::iequals(strTemp,
"large"))
571 else if (boost::iequals(strTemp,
"huge"))
575 4, beast::lexicalCastThrow<std::size_t>(strTemp));
582 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
588 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
592 if (boost::iequals(strTemp,
"all"))
594 else if (boost::iequals(strTemp,
"trusted"))
596 else if (boost::iequals(strTemp,
"drop_untrusted"))
599 Throw<std::runtime_error>(
600 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
606 if (boost::iequals(strTemp,
"all"))
608 else if (boost::iequals(strTemp,
"trusted"))
610 else if (boost::iequals(strTemp,
"drop_untrusted"))
613 Throw<std::runtime_error>(
614 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
618 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
619 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
620 "] and [" SECTION_VALIDATOR_TOKEN
621 "] config sections");
635 if (boost::iequals(strTemp,
"full"))
638 else if (boost::iequals(strTemp,
"none"))
646 if (boost::iequals(strTemp,
"none"))
648 else if (boost::iequals(strTemp,
"full"))
651 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
659 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
665 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
678 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
679 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
680 ": must be between 10 and 600 inclusive");
685 WORKERS = beast::lexicalCastThrow<int>(strTemp);
687 if (WORKERS < 1 || WORKERS > 1024)
688 Throw<std::runtime_error>(
689 "Invalid " SECTION_WORKERS
690 ": must be between 1 and 1024 inclusive.");
695 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
697 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
698 Throw<std::runtime_error>(
699 "Invalid " SECTION_IO_WORKERS
700 ": must be between 1 and 1024 inclusive.");
707 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
708 Throw<std::runtime_error>(
709 "Invalid " SECTION_PREFETCH_WORKERS
710 ": must be between 1 and 1024 inclusive.");
714 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
719 if (
exists(SECTION_REDUCE_RELAY))
721 auto sec =
section(SECTION_REDUCE_RELAY);
728 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
730 Throw<std::runtime_error>(
731 "Invalid " SECTION_REDUCE_RELAY
732 ", tx_min_peers must be greater or equal to 10"
733 ", tx_relay_percentage must be greater or equal to 10 "
734 "and less or equal to 100");
740 beast::lexicalCastThrow<int>(strTemp),
749 Throw<std::runtime_error>(
750 "Invalid " SECTION_SERVER_DOMAIN
751 ": the domain name does not appear to meet the requirements.");
757 if (
exists(SECTION_OVERLAY))
759 auto const sec =
section(SECTION_OVERLAY);
765 if (
auto val = sec.get(
"max_unknown_time"))
767 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
771 Throw<std::runtime_error>(
772 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
773 ": must be of the form '<number>' representing seconds.");
777 Throw<std::runtime_error>(
778 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
779 ": the time must be between 300 and 1800 seconds, inclusive.");
783 if (
auto val = sec.get(
"max_diverged_time"))
785 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
789 Throw<std::runtime_error>(
790 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
791 ": must be of the form '<number>' representing seconds.");
796 Throw<std::runtime_error>(
797 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
798 ": the time must be between 60 and 900 seconds, inclusive.");
803 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
806 boost::regex
const re(
807 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
809 if (!boost::regex_match(strTemp, match, re))
810 Throw<std::runtime_error>(
811 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
812 ", must be: [0-9]+ [minutes|hours|days|weeks]");
815 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
817 if (boost::iequals(match[2],
"minutes"))
819 else if (boost::iequals(match[2],
"hours"))
821 else if (boost::iequals(match[2],
"days"))
823 else if (boost::iequals(match[2],
"weeks"))
827 Throw<std::runtime_error>(
828 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
829 ", the minimum amount of time an amendment must hold a "
830 "majority is 15 minutes");
847 boost::filesystem::path validatorsFile;
851 validatorsFile = strTemp;
853 if (validatorsFile.empty())
854 Throw<std::runtime_error>(
855 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
857 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
860 if (!boost::filesystem::exists(validatorsFile))
861 Throw<std::runtime_error>(
862 "The file specified in [" SECTION_VALIDATORS_FILE
865 validatorsFile.string());
868 !boost::filesystem::is_regular_file(validatorsFile) &&
869 !boost::filesystem::is_symlink(validatorsFile))
870 Throw<std::runtime_error>(
871 "Invalid file specified in [" SECTION_VALIDATORS_FILE
873 validatorsFile.string());
879 if (!validatorsFile.empty())
881 if (!boost::filesystem::exists(validatorsFile))
882 validatorsFile.clear();
884 !boost::filesystem::is_regular_file(validatorsFile) &&
885 !boost::filesystem::is_symlink(validatorsFile))
886 validatorsFile.clear();
890 if (!validatorsFile.empty() &&
891 boost::filesystem::exists(validatorsFile) &&
892 (boost::filesystem::is_regular_file(validatorsFile) ||
893 boost::filesystem::is_symlink(validatorsFile)))
895 boost::system::error_code ec;
899 Throw<std::runtime_error>(
900 "Failed to read '" + validatorsFile.string() +
"'." +
917 auto valSiteEntries =
921 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
929 if (!entries && !valKeyEntries && !valListKeys)
930 Throw<std::runtime_error>(
931 "The file specified in [" SECTION_VALIDATORS_FILE
933 "does not contain a [" SECTION_VALIDATORS
935 "[" SECTION_VALIDATOR_KEYS
937 "[" SECTION_VALIDATOR_LIST_KEYS
940 validatorsFile.string());
947 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
948 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
950 Throw<std::runtime_error>(
952 "] config section is missing");
957 auto const part =
section(
"features");
958 for (
auto const& s : part.values())
963 Throw<std::runtime_error>(
964 "Unknown feature: " + s +
" in config file.");
980 Throw<std::runtime_error>(
981 "The minimum number of required peers (network_quorum) exceeds "
982 "the maximum number of allowed peers (peers_max)");
987 boost::filesystem::path
992 if (!log_file.empty() && !log_file.is_absolute())
996 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
999 if (!log_file.empty())
1001 auto log_dir = log_file.parent_path();
1003 if (!boost::filesystem::is_directory(log_dir))
1005 boost::system::error_code ec;
1006 boost::filesystem::create_directories(log_dir, ec);
1012 std::cerr <<
"Unable to create log file path " << log_dir
1013 <<
": " << ec.message() <<
'\n';
1026 assert(!node || *node <= 4);
1038 setup.reference_fee = temp;
1043 setup.account_reserve = temp;
1045 setup.owner_reserve = temp;