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>
51 {{128000, 256000, 512000, 768000, 2048000}}},
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);
367 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
371 if (boost::iequals(strTemp,
"tiny"))
373 else if (boost::iequals(strTemp,
"small"))
375 else if (boost::iequals(strTemp,
"medium"))
377 else if (boost::iequals(strTemp,
"large"))
379 else if (boost::iequals(strTemp,
"huge"))
382 NODE_SIZE = std::min<std::size_t>(
383 4, beast::lexicalCastThrow<std::size_t>(strTemp));
387 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
390 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
393 WEBSOCKET_PING_FREQ =
400 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
404 if (boost::iequals(strTemp,
"all"))
405 RELAY_UNTRUSTED_VALIDATIONS =
true;
406 else if (boost::iequals(strTemp,
"trusted"))
407 RELAY_UNTRUSTED_VALIDATIONS =
false;
409 Throw<std::runtime_error>(
410 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
416 if (boost::iequals(strTemp,
"all"))
417 RELAY_UNTRUSTED_PROPOSALS =
true;
418 else if (boost::iequals(strTemp,
"trusted"))
419 RELAY_UNTRUSTED_PROPOSALS =
false;
421 Throw<std::runtime_error>(
422 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
426 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
427 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
429 "and [" SECTION_VALIDATOR_TOKEN
430 "] config sections");
433 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
436 FEE_ACCOUNT_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
439 FEE_OWNER_RESERVE = beast::lexicalCastThrow<std::uint64_t>(strTemp);
442 FEE_DEFAULT = beast::lexicalCastThrow<std::uint64_t>(strTemp);
446 if (boost::iequals(strTemp,
"full"))
449 else if (boost::iequals(strTemp,
"none"))
452 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
457 if (boost::iequals(strTemp,
"none"))
459 else if (boost::iequals(strTemp,
"full"))
462 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
464 if (FETCH_DEPTH < 10)
469 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
471 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
473 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
475 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
478 DEBUG_LOGFILE = strTemp;
481 WORKERS = beast::lexicalCastThrow<std::size_t>(strTemp);
484 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
486 if (exists(SECTION_REDUCE_RELAY))
488 auto sec = section(SECTION_REDUCE_RELAY);
489 REDUCE_RELAY_ENABLE = sec.value_or(
"enable",
false);
490 REDUCE_RELAY_SQUELCH = sec.value_or(
"squelch",
false);
496 beast::lexicalCastThrow<int>(strTemp),
505 Throw<std::runtime_error>(
506 "Invalid " SECTION_SERVER_DOMAIN
507 ": the domain name does not appear to meet the requirements.");
510 SERVER_DOMAIN = strTemp;
514 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
517 boost::regex
const re(
518 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
520 if (!boost::regex_match(strTemp, match, re))
521 Throw<std::runtime_error>(
522 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
523 ", must be: [0-9]+ [minutes|hours|days|weeks]");
526 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
528 if (boost::iequals(match[2],
"minutes"))
530 else if (boost::iequals(match[2],
"hours"))
532 else if (boost::iequals(match[2],
"days"))
534 else if (boost::iequals(match[2],
"weeks"))
537 if (AMENDMENT_MAJORITY_TIME <
minutes(15))
538 Throw<std::runtime_error>(
539 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
540 ", the minimum amount of time an amendment must hold a "
541 "majority is 15 minutes");
555 boost::filesystem::path validatorsFile;
559 validatorsFile = strTemp;
561 if (validatorsFile.empty())
562 Throw<std::runtime_error>(
563 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
565 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
566 validatorsFile = CONFIG_DIR / validatorsFile;
568 if (!boost::filesystem::exists(validatorsFile))
569 Throw<std::runtime_error>(
570 "The file specified in [" SECTION_VALIDATORS_FILE
573 validatorsFile.string());
576 !boost::filesystem::is_regular_file(validatorsFile) &&
577 !boost::filesystem::is_symlink(validatorsFile))
578 Throw<std::runtime_error>(
579 "Invalid file specified in [" SECTION_VALIDATORS_FILE
581 validatorsFile.string());
583 else if (!CONFIG_DIR.empty())
585 validatorsFile = CONFIG_DIR / validatorsFileName;
587 if (!validatorsFile.empty())
589 if (!boost::filesystem::exists(validatorsFile))
590 validatorsFile.clear();
592 !boost::filesystem::is_regular_file(validatorsFile) &&
593 !boost::filesystem::is_symlink(validatorsFile))
594 validatorsFile.clear();
598 if (!validatorsFile.empty() &&
599 boost::filesystem::exists(validatorsFile) &&
600 (boost::filesystem::is_regular_file(validatorsFile) ||
601 boost::filesystem::is_symlink(validatorsFile)))
603 boost::system::error_code ec;
607 Throw<std::runtime_error>(
608 "Failed to read '" + validatorsFile.string() +
"'." +
617 section(SECTION_VALIDATORS).append(*entries);
623 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
625 auto valSiteEntries =
629 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
635 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
637 if (!entries && !valKeyEntries && !valListKeys)
638 Throw<std::runtime_error>(
639 "The file specified in [" SECTION_VALIDATORS_FILE
641 "does not contain a [" SECTION_VALIDATORS
643 "[" SECTION_VALIDATOR_KEYS
645 "[" SECTION_VALIDATOR_LIST_KEYS
648 validatorsFile.string());
652 section(SECTION_VALIDATORS)
653 .append(section(SECTION_VALIDATOR_KEYS).lines());
655 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
656 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
658 Throw<std::runtime_error>(
660 "] config section is missing");
665 auto const part = section(
"features");
666 for (
auto const& s : part.values())
671 Throw<std::runtime_error>(
672 "Unknown feature: " + s +
" in config file.");
686 if (NETWORK_QUORUM > pm)
688 Throw<std::runtime_error>(
689 "The minimum number of required peers (network_quorum) exceeds "
690 "the maximum number of allowed peers (peers_max)");
695 boost::filesystem::path
696 Config::getDebugLogFile()
const
698 auto log_file = DEBUG_LOGFILE;
700 if (!log_file.empty() && !log_file.is_absolute())
704 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
707 if (!log_file.empty())
709 auto log_dir = log_file.parent_path();
711 if (!boost::filesystem::is_directory(log_dir))
713 boost::system::error_code ec;
714 boost::filesystem::create_directories(log_dir, ec);
720 std::cerr <<
"Unable to create log file path " << log_dir
721 <<
": " << ec.message() <<
'\n';
730 Config::getValueFor(
SizedItem item, boost::optional<std::size_t> node)
const
734 assert(!node || *node <= 4);
735 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));