20#include <xrpld/core/Config.h>
21#include <xrpld/core/ConfigSections.h>
22#include <xrpld/net/HTTPClient.h>
23#include <xrpl/basics/FileUtilities.h>
24#include <xrpl/basics/Log.h>
25#include <xrpl/basics/StringUtilities.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/beast/core/LexicalCast.h>
28#include <xrpl/json/json_reader.h>
29#include <xrpl/protocol/Feature.h>
30#include <xrpl/protocol/SystemParameters.h>
32#include <boost/algorithm/string.hpp>
33#include <boost/format.hpp>
34#include <boost/predef.h>
35#include <boost/regex.hpp>
45#include <sysinfoapi.h>
53 if (MEMORYSTATUSEX msx{
sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
64#include <sys/sysinfo.h>
72 if (
struct sysinfo si; sysinfo(&si) == 0)
84#include <sys/sysctl.h>
93 int mib[] = {CTL_HW, HW_MEMSIZE};
95 size_t size =
sizeof(ram);
97 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
137 []() constexpr->bool {
150 "Mismatch between sized item enum & array indices");
157#define SECTION_DEFAULT_NAME ""
167 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
170 boost::algorithm::replace_all(strData,
"\r",
"\n");
172 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
178 secResult[strSection] = IniFileSections::mapped_type();
181 for (
auto& strValue : vLines)
184 boost::algorithm::trim(strValue);
186 if (strValue.empty() || strValue[0] ==
'#')
190 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
193 strSection = strValue.
substr(1, strValue.length() - 2);
194 secResult.
emplace(strSection, IniFileSections::mapped_type{});
199 if (!strValue.empty())
200 secResult[strSection].push_back(strValue);
207IniFileSections::mapped_type*
210 if (
auto it = secSource.
find(strSection); it != secSource.
end())
211 return &(it->second);
225 if (pmtEntries && pmtEntries->size() == 1)
227 strValue = (*pmtEntries)[0];
233 JLOG(j.
warn()) <<
"Section '" << strSection <<
"': requires 1 line not "
234 << pmtEntries->
size() <<
" lines.";
246char const*
const Config::configFileName =
"rippled.cfg";
247char const*
const Config::databaseDirName =
"db";
248char const*
const Config::validatorsFileName =
"validators.txt";
255 if (
auto const v =
std::getenv(name); v !=
nullptr)
262 : j_(
beast::Journal::getNullSink())
263 , ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
271 NODE_SIZE == 0,
"ripple::Config::setupControl : node size not set");
273 QUIET = bQuiet || bSilent;
282 auto const& threshold =
286 threshold.second.begin(),
287 threshold.second.end(),
289 return (limit == 0) || (ramSize_ < limit);
293 ns != threshold.second.end(),
294 "ripple::Config::setupControl : valid node size");
296 if (ns != threshold.second.end())
306 NODE_SIZE <= 4,
"ripple::Config::setupControl : node size is set");
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();
394 dataDir = boost::filesystem::path(dbPath);
399 if (!dataDir.empty())
401 boost::system::error_code ec;
402 boost::filesystem::create_directories(dataDir, ec);
405 Throw<std::runtime_error>(
406 boost::str(boost::format(
"Can not create %s") % dataDir));
408 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
429 if (!config.
exists(
"server"))
437 auto const& section = config[name];
438 auto const optResult = section.get(
"port");
441 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
445 ss <<
"Invalid value '" << *optResult <<
"' for key 'port' in ["
447 Throw<std::runtime_error>(ss.
str());
462 boost::system::error_code ec;
493 for (
auto& line : strVec)
496 if (
std::count(line.begin(), line.end(),
':') != 1)
502 if (result.
size() == line.size())
515 boost::filesystem::path p(dbPath);
516 legacy(
"database_path", boost::filesystem::absolute(p).
string());
524 if (strTemp ==
"main")
526 else if (strTemp ==
"testnet")
528 else if (strTemp ==
"devnet")
531 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
539 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
546 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
547 if (*peers_in_max > 1000)
548 Throw<std::runtime_error>(
549 "Invalid value specified in [" SECTION_PEERS_IN_MAX
550 "] section; the value must be less or equal than 1000");
556 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
557 if (*peers_out_max < 10 || *peers_out_max > 1000)
558 Throw<std::runtime_error>(
559 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
560 "] section; the value must be in range 10-1000");
564 if ((peers_in_max && !peers_out_max) ||
565 (peers_out_max && !peers_in_max))
566 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
568 "and [" SECTION_PEERS_OUT_MAX
569 "] must be configured");
571 if (peers_in_max && peers_out_max)
580 if (boost::iequals(strTemp,
"tiny"))
582 else if (boost::iequals(strTemp,
"small"))
584 else if (boost::iequals(strTemp,
"medium"))
586 else if (boost::iequals(strTemp,
"large"))
588 else if (boost::iequals(strTemp,
"huge"))
592 4, beast::lexicalCastThrow<std::size_t>(strTemp));
599 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
605 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
609 if (boost::iequals(strTemp,
"all"))
611 else if (boost::iequals(strTemp,
"trusted"))
613 else if (boost::iequals(strTemp,
"drop_untrusted"))
616 Throw<std::runtime_error>(
617 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
623 if (boost::iequals(strTemp,
"all"))
625 else if (boost::iequals(strTemp,
"trusted"))
627 else if (boost::iequals(strTemp,
"drop_untrusted"))
630 Throw<std::runtime_error>(
631 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
635 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
636 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
637 "] and [" SECTION_VALIDATOR_TOKEN
638 "] config sections");
652 if (boost::iequals(strTemp,
"full"))
655 else if (boost::iequals(strTemp,
"none"))
663 if (boost::iequals(strTemp,
"none"))
665 else if (boost::iequals(strTemp,
"full"))
668 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
676 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
682 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
695 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
696 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
697 ": must be between 10 and 600 inclusive");
702 WORKERS = beast::lexicalCastThrow<int>(strTemp);
704 if (WORKERS < 1 || WORKERS > 1024)
705 Throw<std::runtime_error>(
706 "Invalid " SECTION_WORKERS
707 ": must be between 1 and 1024 inclusive.");
712 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
714 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
715 Throw<std::runtime_error>(
716 "Invalid " SECTION_IO_WORKERS
717 ": must be between 1 and 1024 inclusive.");
724 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
725 Throw<std::runtime_error>(
726 "Invalid " SECTION_PREFETCH_WORKERS
727 ": must be between 1 and 1024 inclusive.");
731 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
736 if (
exists(SECTION_REDUCE_RELAY))
738 auto sec =
section(SECTION_REDUCE_RELAY);
745 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
747 Throw<std::runtime_error>(
748 "Invalid " SECTION_REDUCE_RELAY
749 ", tx_min_peers must be greater or equal to 10"
750 ", tx_relay_percentage must be greater or equal to 10 "
751 "and less or equal to 100");
757 beast::lexicalCastThrow<int>(strTemp),
766 Throw<std::runtime_error>(
767 "Invalid " SECTION_SERVER_DOMAIN
768 ": the domain name does not appear to meet the requirements.");
774 if (
exists(SECTION_OVERLAY))
776 auto const sec =
section(SECTION_OVERLAY);
782 if (
auto val = sec.get(
"max_unknown_time"))
784 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
788 Throw<std::runtime_error>(
789 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
790 ": must be of the form '<number>' representing seconds.");
794 Throw<std::runtime_error>(
795 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
796 ": the time must be between 300 and 1800 seconds, inclusive.");
800 if (
auto val = sec.get(
"max_diverged_time"))
802 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
806 Throw<std::runtime_error>(
807 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
808 ": must be of the form '<number>' representing seconds.");
813 Throw<std::runtime_error>(
814 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
815 ": the time must be between 60 and 900 seconds, inclusive.");
820 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
823 boost::regex
const re(
824 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
826 if (!boost::regex_match(strTemp,
match, re))
827 Throw<std::runtime_error>(
828 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
829 ", must be: [0-9]+ [minutes|hours|days|weeks]");
832 beast::lexicalCastThrow<std::uint32_t>(
match[1].str());
834 if (boost::iequals(
match[2],
"minutes"))
836 else if (boost::iequals(
match[2],
"hours"))
838 else if (boost::iequals(
match[2],
"days"))
840 else if (boost::iequals(
match[2],
"weeks"))
844 Throw<std::runtime_error>(
845 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
846 ", the minimum amount of time an amendment must hold a "
847 "majority is 15 minutes");
864 boost::filesystem::path validatorsFile;
868 validatorsFile = strTemp;
870 if (validatorsFile.empty())
871 Throw<std::runtime_error>(
872 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
874 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
877 if (!boost::filesystem::exists(validatorsFile))
878 Throw<std::runtime_error>(
879 "The file specified in [" SECTION_VALIDATORS_FILE
882 validatorsFile.string());
885 !boost::filesystem::is_regular_file(validatorsFile) &&
886 !boost::filesystem::is_symlink(validatorsFile))
887 Throw<std::runtime_error>(
888 "Invalid file specified in [" SECTION_VALIDATORS_FILE
890 validatorsFile.string());
896 if (!validatorsFile.empty())
898 if (!boost::filesystem::exists(validatorsFile))
899 validatorsFile.clear();
901 !boost::filesystem::is_regular_file(validatorsFile) &&
902 !boost::filesystem::is_symlink(validatorsFile))
903 validatorsFile.clear();
907 if (!validatorsFile.empty() &&
908 boost::filesystem::exists(validatorsFile) &&
909 (boost::filesystem::is_regular_file(validatorsFile) ||
910 boost::filesystem::is_symlink(validatorsFile)))
912 boost::system::error_code ec;
916 Throw<std::runtime_error>(
917 "Failed to read '" + validatorsFile.string() +
"'." +
934 auto valSiteEntries =
938 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
946 auto valListThreshold =
949 if (valListThreshold)
950 section(SECTION_VALIDATOR_LIST_THRESHOLD)
951 .
append(*valListThreshold);
953 if (!entries && !valKeyEntries && !valListKeys)
954 Throw<std::runtime_error>(
955 "The file specified in [" SECTION_VALIDATORS_FILE
957 "does not contain a [" SECTION_VALIDATORS
959 "[" SECTION_VALIDATOR_KEYS
961 "[" SECTION_VALIDATOR_LIST_KEYS
964 validatorsFile.string());
968 auto const& listThreshold =
969 section(SECTION_VALIDATOR_LIST_THRESHOLD);
970 if (listThreshold.lines().empty())
972 else if (listThreshold.values().size() == 1)
974 auto strTemp = listThreshold.values()[0];
975 auto const listThreshold =
976 beast::lexicalCastThrow<std::size_t>(strTemp);
977 if (listThreshold == 0)
981 section(SECTION_VALIDATOR_LIST_KEYS).values().size())
983 Throw<std::runtime_error>(
984 "Value in config section "
985 "[" SECTION_VALIDATOR_LIST_THRESHOLD
986 "] exceeds the number of configured list keys");
988 return listThreshold;
992 Throw<std::runtime_error>(
994 "[" SECTION_VALIDATOR_LIST_THRESHOLD
995 "] should contain single value only");
1003 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
1004 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
1006 Throw<std::runtime_error>(
1008 "] config section is missing");
1013 auto const part =
section(
"features");
1014 for (
auto const& s : part.values())
1019 Throw<std::runtime_error>(
1020 "Unknown feature: " + s +
" in config file.");
1036 Throw<std::runtime_error>(
1037 "The minimum number of required peers (network_quorum) exceeds "
1038 "the maximum number of allowed peers (peers_max)");
1043boost::filesystem::path
1048 if (!log_file.empty() && !log_file.is_absolute())
1052 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
1055 if (!log_file.empty())
1057 auto log_dir = log_file.parent_path();
1059 if (!boost::filesystem::is_directory(log_dir))
1061 boost::system::error_code ec;
1062 boost::filesystem::create_directories(log_dir, ec);
1068 std::cerr <<
"Unable to create log file path " << log_dir
1069 <<
": " << ec.message() <<
'\n';
1083 "ripple::Config::getValueFor : valid index input");
1085 !node || *node <= 4,
1086 "ripple::Config::getValueFor : unset or valid node");
1096 if (
set(temp,
"reference_fee", section) &&
1102 if (
set(temp,
"account_reserve", section))
1104 if (
set(temp,
"owner_reserve", section))
A generic endpoint for log messages.
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Section & section(std::string const &name)
Returns the section with the given name.
void build(IniFileSections const &ifs)
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
static char const *const databaseDirName
std::optional< int > SWEEP_INTERVAL
std::uint32_t LEDGER_HISTORY
std::uint32_t FETCH_DEPTH
std::size_t NETWORK_QUORUM
std::vector< std::string > IPS_FIXED
boost::filesystem::path CONFIG_DIR
void setup(std::string const &strConf, bool bQuiet, bool bSilent, bool bStandalone)
static char const *const configFileName
std::vector< std::string > IPS
bool VP_REDUCE_RELAY_ENABLE
std::size_t TX_REDUCE_RELAY_MIN_PEERS
std::optional< std::size_t > VALIDATOR_LIST_THRESHOLD
int RELAY_UNTRUSTED_PROPOSALS
bool TX_REDUCE_RELAY_ENABLE
boost::filesystem::path getDebugLogFile() const
Returns the full path and filename of the debug log file.
bool TX_REDUCE_RELAY_METRICS
static constexpr int MIN_JOB_QUEUE_TX
bool RUN_STANDALONE
Operate in stand-alone mode.
std::size_t TX_RELAY_PERCENTAGE
std::string SERVER_DOMAIN
static constexpr int MAX_JOB_QUEUE_TX
std::chrono::seconds MAX_DIVERGED_TIME
boost::filesystem::path CONFIG_FILE
int getValueFor(SizedItem item, std::optional< std::size_t > node=std::nullopt) const
Retrieve the default value for the item at the specified node size.
bool signingEnabled_
Determines if the server will sign a tx, given an account's secret seed.
boost::filesystem::path DEBUG_LOGFILE
std::string SSL_VERIFY_FILE
void setupControl(bool bQuiet, bool bSilent, bool bStandalone)
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
std::unordered_set< uint256, beast::uhash<> > features
std::chrono::seconds AMENDMENT_MAJORITY_TIME
bool VP_REDUCE_RELAY_SQUELCH
static char const *const validatorsFileName
std::chrono::seconds MAX_UNKNOWN_TIME
std::size_t PEERS_OUT_MAX
int RELAY_UNTRUSTED_VALIDATIONS
std::string SSL_VERIFY_DIR
static void initializeSSLContext(Config const &config, beast::Journal j)
Holds a collection of configuration values.
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
std::vector< std::string > const & values() const
Returns all the values in the section.
T hardware_concurrency(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool getSingleSection(IniFileSections &secSource, std::string const &strSection, std::string &strValue, beast::Journal j)
IniFileSections::mapped_type * getIniFileSection(IniFileSections &secSource, std::string const &strSection)
static std::string const & systemName()
static void checkZeroPorts(Config const &config)
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > days
bool get_if_exists(Section const §ion, std::string const &name, T &v)
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
static std::string getEnvVar(char const *name)
std::optional< uint256 > getRegisteredFeature(std::string const &name)
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
IniFileSections parseIniFile(std::string const &strInput, const bool bTrim)
FeeSetup setup_FeeVote(Section const §ion)
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
std::unordered_map< std::string, std::vector< std::string > > IniFileSections
constexpr std::array< std::pair< SizedItem, std::array< int, 5 > >, 13 > sizedItems
T regex_replace(T... args)
static std::string nodeDatabase()
Fee schedule for startup / standalone, and to vote for.
XRPAmount reference_fee
The cost of a reference transaction in drops.
XRPAmount owner_reserve
The per-owned item reserve requirement in drops.
XRPAmount account_reserve
The account reserve requirement in drops.