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/beast/core/string.hpp>
33 #include <boost/format.hpp>
34 #include <boost/regex.hpp>
35 #include <boost/system/error_code.hpp>
66 []() constexpr->bool {
67 std::underlying_type_t<SizedItem> idx = 0;
69 for (auto const& i : sizedItems)
71 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
79 "Mismatch between sized item enum & array indices");
85 #define SECTION_DEFAULT_NAME ""
95 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
98 boost::algorithm::replace_all(strData,
"\r",
"\n");
100 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
106 secResult[strSection] = IniFileSections::mapped_type();
109 for (
auto& strValue : vLines)
112 boost::algorithm::trim(strValue);
114 if (strValue.empty() || strValue[0] ==
'#')
118 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
121 strSection = strValue.
substr(1, strValue.length() - 2);
122 secResult.
emplace(strSection, IniFileSections::mapped_type{});
127 if (!strValue.empty())
128 secResult[strSection].push_back(strValue);
135 IniFileSections::mapped_type*
138 IniFileSections::iterator it;
139 IniFileSections::mapped_type* smtResult;
140 it = secSource.
find(strSection);
141 if (it == secSource.
end())
144 smtResult = &(it->second);
155 IniFileSections::mapped_type* pmtEntries =
157 bool bSingle = pmtEntries && 1 == pmtEntries->size();
161 strValue = (*pmtEntries)[0];
165 JLOG(j.
warn()) << boost::str(
166 boost::format(
"Section [%s]: requires 1 line not %d lines.") %
167 strSection % pmtEntries->
size());
179 char const*
const Config::configFileName =
"rippled.cfg";
180 char const*
const Config::databaseDirName =
"db";
181 char const*
const Config::validatorsFileName =
"validators.txt";
188 auto const v = getenv(name);
196 constexpr FeeUnit32 Config::TRANSACTION_FEE_BASE;
199 Config::setupControl(
bool bQuiet,
bool bSilent,
bool bStandalone)
201 QUIET = bQuiet || bSilent;
203 RUN_STANDALONE = bStandalone;
213 boost::filesystem::path dataDir;
222 setupControl(bQuiet, bSilent, bStandalone);
224 strDbPath = databaseDirName;
226 if (!strConf.
empty())
227 strConfFile = strConf;
229 strConfFile = configFileName;
231 if (!strConf.
empty())
234 CONFIG_FILE = strConfFile;
235 CONFIG_DIR = boost::filesystem::absolute(CONFIG_FILE);
236 CONFIG_DIR.remove_filename();
237 dataDir = CONFIG_DIR / strDbPath;
241 CONFIG_DIR = boost::filesystem::current_path();
242 CONFIG_FILE = CONFIG_DIR / strConfFile;
243 dataDir = CONFIG_DIR / strDbPath;
251 if (boost::filesystem::exists(CONFIG_FILE)
253 || (strHome.
empty() &&
254 (strXdgConfigHome.
empty() || strXdgDataHome.
empty())))
260 if (strXdgConfigHome.
empty())
263 strXdgConfigHome = strHome +
"/.config";
266 if (strXdgDataHome.
empty())
269 strXdgDataHome = strHome +
"/.local/share";
272 CONFIG_DIR = strXdgConfigHome +
"/" + systemName();
273 CONFIG_FILE = CONFIG_DIR / strConfFile;
274 dataDir = strXdgDataHome +
"/" + systemName();
276 if (!boost::filesystem::exists(CONFIG_FILE))
278 CONFIG_DIR =
"/etc/opt/" + systemName();
279 CONFIG_FILE = CONFIG_DIR / strConfFile;
280 dataDir =
"/var/opt/" + systemName();
291 dataDir = boost::filesystem::path(dbPath);
292 else if (RUN_STANDALONE)
296 if (!dataDir.empty())
298 boost::system::error_code ec;
299 boost::filesystem::create_directories(dataDir, ec);
302 Throw<std::runtime_error>(
303 boost::str(boost::format(
"Can not create %s") % dataDir));
305 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
308 HTTPClient::initializeSSLContext(*
this, j_);
321 std::cerr <<
"Loading: " << CONFIG_FILE <<
"\n";
323 boost::system::error_code ec;
328 std::cerr <<
"Failed to read '" << CONFIG_FILE <<
"'." << ec.value()
333 loadFromString(fileContents);
356 boost::filesystem::path p(dbPath);
357 legacy(
"database_path", boost::filesystem::absolute(p).
string());
364 PEER_PRIVATE = beast::lexicalCastThrow<bool>(strTemp);
368 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
375 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
376 if (*peers_in_max > 1000)
377 Throw<std::runtime_error>(
378 "Invalid value specified in [" SECTION_PEERS_IN_MAX
379 "] section; the value must be less or equal than 1000");
385 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
386 if (*peers_out_max < 10 || *peers_out_max > 1000)
387 Throw<std::runtime_error>(
388 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
389 "] section; the value must be in range 10-1000");
393 if ((peers_in_max && !peers_out_max) ||
394 (peers_out_max && !peers_in_max))
395 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
397 "and [" SECTION_PEERS_OUT_MAX
398 "] must be configured");
400 if (peers_in_max && peers_out_max)
402 PEERS_IN_MAX = *peers_in_max;
403 PEERS_OUT_MAX = *peers_out_max;
409 if (boost::iequals(strTemp,
"tiny"))
411 else if (boost::iequals(strTemp,
"small"))
413 else if (boost::iequals(strTemp,
"medium"))
415 else if (boost::iequals(strTemp,
"large"))
417 else if (boost::iequals(strTemp,
"huge"))
420 NODE_SIZE = std::min<std::size_t>(
421 4, beast::lexicalCastThrow<std::size_t>(strTemp));
425 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
428 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
431 WEBSOCKET_PING_FREQ =
438 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
442 if (boost::iequals(strTemp,
"all"))
443 RELAY_UNTRUSTED_VALIDATIONS =
true;
444 else if (boost::iequals(strTemp,
"trusted"))
445 RELAY_UNTRUSTED_VALIDATIONS =
false;
447 Throw<std::runtime_error>(
448 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
454 if (boost::iequals(strTemp,
"all"))
455 RELAY_UNTRUSTED_PROPOSALS =
true;
456 else if (boost::iequals(strTemp,
"trusted"))
457 RELAY_UNTRUSTED_PROPOSALS =
false;
459 Throw<std::runtime_error>(
460 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
464 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
465 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
466 "] and [" SECTION_VALIDATOR_TOKEN
467 "] config sections");
470 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
473 FEE_ACCOUNT_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
476 FEE_OWNER_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
479 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
483 if (boost::iequals(strTemp,
"full"))
486 else if (boost::iequals(strTemp,
"none"))
489 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
494 if (boost::iequals(strTemp,
"none"))
496 else if (boost::iequals(strTemp,
"full"))
499 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
501 if (FETCH_DEPTH < 10)
506 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
508 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
510 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
512 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
515 DEBUG_LOGFILE = strTemp;
518 WORKERS = beast::lexicalCastThrow<std::size_t>(strTemp);
521 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
523 if (exists(SECTION_REDUCE_RELAY))
525 auto sec = section(SECTION_REDUCE_RELAY);
526 REDUCE_RELAY_ENABLE = sec.value_or(
"enable",
false);
527 REDUCE_RELAY_SQUELCH = sec.value_or(
"squelch",
false);
533 beast::lexicalCastThrow<int>(strTemp),
542 Throw<std::runtime_error>(
543 "Invalid " SECTION_SERVER_DOMAIN
544 ": the domain name does not appear to meet the requirements.");
547 SERVER_DOMAIN = strTemp;
550 if (exists(SECTION_OVERLAY))
552 auto const sec = section(SECTION_OVERLAY);
558 if (
auto val = sec.get<
std::string>(
"max_unknown_time"))
560 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
564 Throw<std::runtime_error>(
565 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
566 ": must be of the form '<number>' representing seconds.");
569 if (MAX_UNKNOWN_TIME <
seconds{300} || MAX_UNKNOWN_TIME >
seconds{1800})
570 Throw<std::runtime_error>(
571 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
572 ": the time must be between 300 and 1800 seconds, inclusive.");
576 if (
auto val = sec.get<
std::string>(
"max_diverged_time"))
578 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
582 Throw<std::runtime_error>(
583 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
584 ": must be of the form '<number>' representing seconds.");
587 if (MAX_DIVERGED_TIME <
seconds{60} || MAX_DIVERGED_TIME >
seconds{900})
589 Throw<std::runtime_error>(
590 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
591 ": the time must be between 60 and 900 seconds, inclusive.");
596 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
599 boost::regex
const re(
600 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
602 if (!boost::regex_match(strTemp, match, re))
603 Throw<std::runtime_error>(
604 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
605 ", must be: [0-9]+ [minutes|hours|days|weeks]");
608 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
610 if (boost::iequals(match[2],
"minutes"))
612 else if (boost::iequals(match[2],
"hours"))
614 else if (boost::iequals(match[2],
"days"))
616 else if (boost::iequals(match[2],
"weeks"))
619 if (AMENDMENT_MAJORITY_TIME <
minutes(15))
620 Throw<std::runtime_error>(
621 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
622 ", the minimum amount of time an amendment must hold a "
623 "majority is 15 minutes");
637 boost::filesystem::path validatorsFile;
641 validatorsFile = strTemp;
643 if (validatorsFile.empty())
644 Throw<std::runtime_error>(
645 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
647 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
648 validatorsFile = CONFIG_DIR / validatorsFile;
650 if (!boost::filesystem::exists(validatorsFile))
651 Throw<std::runtime_error>(
652 "The file specified in [" SECTION_VALIDATORS_FILE
655 validatorsFile.string());
658 !boost::filesystem::is_regular_file(validatorsFile) &&
659 !boost::filesystem::is_symlink(validatorsFile))
660 Throw<std::runtime_error>(
661 "Invalid file specified in [" SECTION_VALIDATORS_FILE
663 validatorsFile.string());
665 else if (!CONFIG_DIR.empty())
667 validatorsFile = CONFIG_DIR / validatorsFileName;
669 if (!validatorsFile.empty())
671 if (!boost::filesystem::exists(validatorsFile))
672 validatorsFile.clear();
674 !boost::filesystem::is_regular_file(validatorsFile) &&
675 !boost::filesystem::is_symlink(validatorsFile))
676 validatorsFile.clear();
680 if (!validatorsFile.empty() &&
681 boost::filesystem::exists(validatorsFile) &&
682 (boost::filesystem::is_regular_file(validatorsFile) ||
683 boost::filesystem::is_symlink(validatorsFile)))
685 boost::system::error_code ec;
689 Throw<std::runtime_error>(
690 "Failed to read '" + validatorsFile.string() +
"'." +
699 section(SECTION_VALIDATORS).append(*entries);
705 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
707 auto valSiteEntries =
711 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
717 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
719 if (!entries && !valKeyEntries && !valListKeys)
720 Throw<std::runtime_error>(
721 "The file specified in [" SECTION_VALIDATORS_FILE
723 "does not contain a [" SECTION_VALIDATORS
725 "[" SECTION_VALIDATOR_KEYS
727 "[" SECTION_VALIDATOR_LIST_KEYS
730 validatorsFile.string());
734 section(SECTION_VALIDATORS)
735 .append(section(SECTION_VALIDATOR_KEYS).lines());
737 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
738 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
740 Throw<std::runtime_error>(
742 "] config section is missing");
747 auto const part = section(
"features");
748 for (
auto const& s : part.values())
753 Throw<std::runtime_error>(
754 "Unknown feature: " + s +
" in config file.");
768 if (NETWORK_QUORUM > pm)
770 Throw<std::runtime_error>(
771 "The minimum number of required peers (network_quorum) exceeds "
772 "the maximum number of allowed peers (peers_max)");
777 boost::filesystem::path
778 Config::getDebugLogFile()
const
780 auto log_file = DEBUG_LOGFILE;
782 if (!log_file.empty() && !log_file.is_absolute())
786 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
789 if (!log_file.empty())
791 auto log_dir = log_file.parent_path();
793 if (!boost::filesystem::is_directory(log_dir))
795 boost::system::error_code ec;
796 boost::filesystem::create_directories(log_dir, ec);
802 std::cerr <<
"Unable to create log file path " << log_dir
803 <<
": " << ec.message() <<
'\n';
812 Config::getValueFor(
SizedItem item, boost::optional<std::size_t> node)
const
816 assert(!node || *node <= 4);
817 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));