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>
68 []() constexpr->bool {
69 std::underlying_type_t<SizedItem> idx = 0;
71 for (auto const& i : sizedItems)
73 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
81 "Mismatch between sized item enum & array indices");
87 #define SECTION_DEFAULT_NAME ""
97 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
100 boost::algorithm::replace_all(strData,
"\r",
"\n");
102 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
108 secResult[strSection] = IniFileSections::mapped_type();
111 for (
auto& strValue : vLines)
114 boost::algorithm::trim(strValue);
116 if (strValue.empty() || strValue[0] ==
'#')
120 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
123 strSection = strValue.
substr(1, strValue.length() - 2);
124 secResult.
emplace(strSection, IniFileSections::mapped_type{});
129 if (!strValue.empty())
130 secResult[strSection].push_back(strValue);
137 IniFileSections::mapped_type*
140 IniFileSections::iterator it;
141 IniFileSections::mapped_type* smtResult;
142 it = secSource.
find(strSection);
143 if (it == secSource.
end())
146 smtResult = &(it->second);
157 IniFileSections::mapped_type* pmtEntries =
159 bool bSingle = pmtEntries && 1 == pmtEntries->size();
163 strValue = (*pmtEntries)[0];
167 JLOG(j.
warn()) << boost::str(
168 boost::format(
"Section [%s]: requires 1 line not %d lines.") %
169 strSection % pmtEntries->
size());
181 char const*
const Config::configFileName =
"rippled.cfg";
182 char const*
const Config::databaseDirName =
"db";
183 char const*
const Config::validatorsFileName =
"validators.txt";
190 auto const v = getenv(name);
198 constexpr FeeUnit32 Config::TRANSACTION_FEE_BASE;
201 Config::setupControl(
bool bQuiet,
bool bSilent,
bool bStandalone)
203 QUIET = bQuiet || bSilent;
205 RUN_STANDALONE = bStandalone;
215 boost::filesystem::path dataDir;
224 setupControl(bQuiet, bSilent, bStandalone);
226 strDbPath = databaseDirName;
228 if (!strConf.
empty())
229 strConfFile = strConf;
231 strConfFile = configFileName;
233 if (!strConf.
empty())
236 CONFIG_FILE = strConfFile;
237 CONFIG_DIR = boost::filesystem::absolute(CONFIG_FILE);
238 CONFIG_DIR.remove_filename();
239 dataDir = CONFIG_DIR / strDbPath;
243 CONFIG_DIR = boost::filesystem::current_path();
244 CONFIG_FILE = CONFIG_DIR / strConfFile;
245 dataDir = CONFIG_DIR / strDbPath;
253 if (boost::filesystem::exists(CONFIG_FILE)
255 || (strHome.
empty() &&
256 (strXdgConfigHome.
empty() || strXdgDataHome.
empty())))
262 if (strXdgConfigHome.
empty())
265 strXdgConfigHome = strHome +
"/.config";
268 if (strXdgDataHome.
empty())
271 strXdgDataHome = strHome +
"/.local/share";
274 CONFIG_DIR = strXdgConfigHome +
"/" + systemName();
275 CONFIG_FILE = CONFIG_DIR / strConfFile;
276 dataDir = strXdgDataHome +
"/" + systemName();
278 if (!boost::filesystem::exists(CONFIG_FILE))
280 CONFIG_DIR =
"/etc/opt/" + systemName();
281 CONFIG_FILE = CONFIG_DIR / strConfFile;
282 dataDir =
"/var/opt/" + systemName();
293 dataDir = boost::filesystem::path(dbPath);
294 else if (RUN_STANDALONE)
298 if (!dataDir.empty())
300 boost::system::error_code ec;
301 boost::filesystem::create_directories(dataDir, ec);
304 Throw<std::runtime_error>(
305 boost::str(boost::format(
"Can not create %s") % dataDir));
307 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
310 HTTPClient::initializeSSLContext(*
this, j_);
323 std::cerr <<
"Loading: " << CONFIG_FILE <<
"\n";
325 boost::system::error_code ec;
330 std::cerr <<
"Failed to read '" << CONFIG_FILE <<
"'." << ec.value()
335 loadFromString(fileContents);
358 boost::filesystem::path p(dbPath);
359 legacy(
"database_path", boost::filesystem::absolute(p).
string());
366 PEER_PRIVATE = beast::lexicalCastThrow<bool>(strTemp);
369 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
373 if (boost::iequals(strTemp,
"tiny"))
375 else if (boost::iequals(strTemp,
"small"))
377 else if (boost::iequals(strTemp,
"medium"))
379 else if (boost::iequals(strTemp,
"large"))
381 else if (boost::iequals(strTemp,
"huge"))
384 NODE_SIZE = std::min<std::size_t>(
385 4, beast::lexicalCastThrow<std::size_t>(strTemp));
389 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
392 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
395 WEBSOCKET_PING_FREQ =
402 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
406 if (boost::iequals(strTemp,
"all"))
407 RELAY_UNTRUSTED_VALIDATIONS =
true;
408 else if (boost::iequals(strTemp,
"trusted"))
409 RELAY_UNTRUSTED_VALIDATIONS =
false;
411 Throw<std::runtime_error>(
412 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
418 if (boost::iequals(strTemp,
"all"))
419 RELAY_UNTRUSTED_PROPOSALS =
true;
420 else if (boost::iequals(strTemp,
"trusted"))
421 RELAY_UNTRUSTED_PROPOSALS =
false;
423 Throw<std::runtime_error>(
424 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
428 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
429 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
431 "and [" SECTION_VALIDATOR_TOKEN
432 "] config sections");
435 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
438 FEE_ACCOUNT_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
441 FEE_OWNER_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
444 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
448 if (boost::iequals(strTemp,
"full"))
451 else if (boost::iequals(strTemp,
"none"))
454 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
459 if (boost::iequals(strTemp,
"none"))
461 else if (boost::iequals(strTemp,
"full"))
464 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
466 if (FETCH_DEPTH < 10)
471 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
473 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
475 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
477 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
480 DEBUG_LOGFILE = strTemp;
483 WORKERS = beast::lexicalCastThrow<std::size_t>(strTemp);
486 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
488 if (exists(SECTION_REDUCE_RELAY))
490 auto sec = section(SECTION_REDUCE_RELAY);
491 REDUCE_RELAY_ENABLE = sec.value_or(
"enable",
false);
492 REDUCE_RELAY_SQUELCH = sec.value_or(
"squelch",
false);
498 beast::lexicalCastThrow<int>(strTemp),
507 Throw<std::runtime_error>(
508 "Invalid " SECTION_SERVER_DOMAIN
509 ": the domain name does not appear to meet the requirements.");
512 SERVER_DOMAIN = strTemp;
516 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
519 boost::regex
const re(
520 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
522 if (!boost::regex_match(strTemp, match, re))
523 Throw<std::runtime_error>(
524 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
525 ", must be: [0-9]+ [minutes|hours|days|weeks]");
528 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
530 if (boost::iequals(match[2],
"minutes"))
532 else if (boost::iequals(match[2],
"hours"))
534 else if (boost::iequals(match[2],
"days"))
536 else if (boost::iequals(match[2],
"weeks"))
539 if (AMENDMENT_MAJORITY_TIME <
minutes(15))
540 Throw<std::runtime_error>(
541 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
542 ", the minimum amount of time an amendment must hold a "
543 "majority is 15 minutes");
557 boost::filesystem::path validatorsFile;
561 validatorsFile = strTemp;
563 if (validatorsFile.empty())
564 Throw<std::runtime_error>(
565 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
567 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
568 validatorsFile = CONFIG_DIR / validatorsFile;
570 if (!boost::filesystem::exists(validatorsFile))
571 Throw<std::runtime_error>(
572 "The file specified in [" SECTION_VALIDATORS_FILE
575 validatorsFile.string());
578 !boost::filesystem::is_regular_file(validatorsFile) &&
579 !boost::filesystem::is_symlink(validatorsFile))
580 Throw<std::runtime_error>(
581 "Invalid file specified in [" SECTION_VALIDATORS_FILE
583 validatorsFile.string());
585 else if (!CONFIG_DIR.empty())
587 validatorsFile = CONFIG_DIR / validatorsFileName;
589 if (!validatorsFile.empty())
591 if (!boost::filesystem::exists(validatorsFile))
592 validatorsFile.clear();
594 !boost::filesystem::is_regular_file(validatorsFile) &&
595 !boost::filesystem::is_symlink(validatorsFile))
596 validatorsFile.clear();
600 if (!validatorsFile.empty() &&
601 boost::filesystem::exists(validatorsFile) &&
602 (boost::filesystem::is_regular_file(validatorsFile) ||
603 boost::filesystem::is_symlink(validatorsFile)))
605 boost::system::error_code ec;
609 Throw<std::runtime_error>(
610 "Failed to read '" + validatorsFile.string() +
"'." +
619 section(SECTION_VALIDATORS).append(*entries);
625 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
627 auto valSiteEntries =
631 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
637 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
639 if (!entries && !valKeyEntries && !valListKeys)
640 Throw<std::runtime_error>(
641 "The file specified in [" SECTION_VALIDATORS_FILE
643 "does not contain a [" SECTION_VALIDATORS
645 "[" SECTION_VALIDATOR_KEYS
647 "[" SECTION_VALIDATOR_LIST_KEYS
650 validatorsFile.string());
654 section(SECTION_VALIDATORS)
655 .append(section(SECTION_VALIDATOR_KEYS).lines());
657 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
658 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
660 Throw<std::runtime_error>(
662 "] config section is missing");
667 auto const part = section(
"features");
668 for (
auto const& s : part.values())
673 Throw<std::runtime_error>(
674 "Unknown feature: " + s +
" in config file.");
688 if (NETWORK_QUORUM > pm)
690 Throw<std::runtime_error>(
691 "The minimum number of required peers (network_quorum) exceeds "
692 "the maximum number of allowed peers (peers_max)");
697 boost::filesystem::path
698 Config::getDebugLogFile()
const
700 auto log_file = DEBUG_LOGFILE;
702 if (!log_file.empty() && !log_file.is_absolute())
706 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
709 if (!log_file.empty())
711 auto log_dir = log_file.parent_path();
713 if (!boost::filesystem::is_directory(log_dir))
715 boost::system::error_code ec;
716 boost::filesystem::create_directories(log_dir, ec);
722 std::cerr <<
"Unable to create log file path " << log_dir
723 <<
": " << ec.message() <<
'\n';
732 Config::getValueFor(
SizedItem item, boost::optional<std::size_t> node)
const
736 assert(!node || *node <= 4);
737 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));