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>
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>
71 if (
struct sysinfo si; sysinfo(&si) == 0)
83#include <sys/sysctl.h>
92 int mib[] = {CTL_HW, HW_MEMSIZE};
94 size_t size =
sizeof(ram);
96 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
136 []() constexpr->bool {
149 "Mismatch between sized item enum & array indices");
156#define SECTION_DEFAULT_NAME ""
166 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
169 boost::algorithm::replace_all(strData,
"\r",
"\n");
171 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
177 secResult[strSection] = IniFileSections::mapped_type();
180 for (
auto& strValue : vLines)
183 boost::algorithm::trim(strValue);
185 if (strValue.empty() || strValue[0] ==
'#')
189 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
192 strSection = strValue.
substr(1, strValue.length() - 2);
193 secResult.
emplace(strSection, IniFileSections::mapped_type{});
198 if (!strValue.empty())
199 secResult[strSection].push_back(strValue);
206IniFileSections::mapped_type*
209 if (
auto it = secSource.
find(strSection); it != secSource.
end())
210 return &(it->second);
224 if (pmtEntries && pmtEntries->size() == 1)
226 strValue = (*pmtEntries)[0];
232 JLOG(j.
warn()) <<
"Section '" << strSection <<
"': requires 1 line not "
233 << pmtEntries->
size() <<
" lines.";
245char const*
const Config::configFileName =
"rippled.cfg";
246char const*
const Config::databaseDirName =
"db";
247char const*
const Config::validatorsFileName =
"validators.txt";
254 if (
auto const v =
std::getenv(name); v !=
nullptr)
261 : j_(
beast::Journal::getNullSink())
262 , ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
270 NODE_SIZE == 0,
"ripple::Config::setupControl : node size not set");
272 QUIET = bQuiet || bSilent;
281 auto const& threshold =
285 threshold.second.begin(),
286 threshold.second.end(),
288 return (limit == 0) || (ramSize_ < limit);
292 ns != threshold.second.end(),
293 "ripple::Config::setupControl : valid node size");
295 if (ns != threshold.second.end())
305 NODE_SIZE <= 4,
"ripple::Config::setupControl : node size is set");
315 boost::filesystem::path dataDir;
328 if (!strConf.
empty())
329 strConfFile = strConf;
333 if (!strConf.
empty())
343 CONFIG_DIR = boost::filesystem::current_path();
350 auto strXdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
351 auto strXdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
355 || (strHome.empty() &&
356 (strXdgConfigHome.empty() || strXdgDataHome.empty())))
362 if (strXdgConfigHome.empty())
365 strXdgConfigHome = strHome +
"/.config";
368 if (strXdgDataHome.empty())
371 strXdgDataHome = strHome +
"/.local/share";
376 dataDir = strXdgDataHome +
"/" +
systemName();
393 dataDir = boost::filesystem::path(dbPath);
398 if (!dataDir.empty())
400 boost::system::error_code ec;
401 boost::filesystem::create_directories(dataDir, ec);
404 Throw<std::runtime_error>(
405 boost::str(boost::format(
"Can not create %s") % dataDir));
407 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
428 if (!config.
exists(
"server"))
436 auto const& section = config[name];
437 auto const optResult = section.get(
"port");
440 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
444 ss <<
"Invalid value '" << *optResult <<
"' for key 'port' in ["
446 Throw<std::runtime_error>(ss.
str());
461 boost::system::error_code ec;
492 for (
auto& line : strVec)
495 if (
std::count(line.begin(), line.end(),
':') != 1)
501 if (result.
size() == line.size())
514 boost::filesystem::path p(dbPath);
515 legacy(
"database_path", boost::filesystem::absolute(p).
string());
523 if (strTemp ==
"main")
525 else if (strTemp ==
"testnet")
527 else if (strTemp ==
"devnet")
530 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
538 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
545 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
546 if (*peers_in_max > 1000)
547 Throw<std::runtime_error>(
548 "Invalid value specified in [" SECTION_PEERS_IN_MAX
549 "] section; the value must be less or equal than 1000");
555 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
556 if (*peers_out_max < 10 || *peers_out_max > 1000)
557 Throw<std::runtime_error>(
558 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
559 "] section; the value must be in range 10-1000");
563 if ((peers_in_max && !peers_out_max) ||
564 (peers_out_max && !peers_in_max))
565 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
567 "and [" SECTION_PEERS_OUT_MAX
568 "] must be configured");
570 if (peers_in_max && peers_out_max)
579 if (boost::iequals(strTemp,
"tiny"))
581 else if (boost::iequals(strTemp,
"small"))
583 else if (boost::iequals(strTemp,
"medium"))
585 else if (boost::iequals(strTemp,
"large"))
587 else if (boost::iequals(strTemp,
"huge"))
591 4, beast::lexicalCastThrow<std::size_t>(strTemp));
598 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
604 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
608 if (boost::iequals(strTemp,
"all"))
610 else if (boost::iequals(strTemp,
"trusted"))
612 else if (boost::iequals(strTemp,
"drop_untrusted"))
615 Throw<std::runtime_error>(
616 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
622 if (boost::iequals(strTemp,
"all"))
624 else if (boost::iequals(strTemp,
"trusted"))
626 else if (boost::iequals(strTemp,
"drop_untrusted"))
629 Throw<std::runtime_error>(
630 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
634 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
635 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
636 "] and [" SECTION_VALIDATOR_TOKEN
637 "] config sections");
651 if (boost::iequals(strTemp,
"full"))
654 else if (boost::iequals(strTemp,
"none"))
662 if (boost::iequals(strTemp,
"none"))
664 else if (boost::iequals(strTemp,
"full"))
667 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
675 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
681 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
694 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
695 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
696 ": must be between 10 and 600 inclusive");
701 WORKERS = beast::lexicalCastThrow<int>(strTemp);
703 if (WORKERS < 1 || WORKERS > 1024)
704 Throw<std::runtime_error>(
705 "Invalid " SECTION_WORKERS
706 ": must be between 1 and 1024 inclusive.");
711 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
713 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
714 Throw<std::runtime_error>(
715 "Invalid " SECTION_IO_WORKERS
716 ": must be between 1 and 1024 inclusive.");
723 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
724 Throw<std::runtime_error>(
725 "Invalid " SECTION_PREFETCH_WORKERS
726 ": must be between 1 and 1024 inclusive.");
730 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
735 if (
exists(SECTION_REDUCE_RELAY))
737 auto sec =
section(SECTION_REDUCE_RELAY);
744 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
746 Throw<std::runtime_error>(
747 "Invalid " SECTION_REDUCE_RELAY
748 ", tx_min_peers must be greater or equal to 10"
749 ", tx_relay_percentage must be greater or equal to 10 "
750 "and less or equal to 100");
756 beast::lexicalCastThrow<int>(strTemp),
765 Throw<std::runtime_error>(
766 "Invalid " SECTION_SERVER_DOMAIN
767 ": the domain name does not appear to meet the requirements.");
773 if (
exists(SECTION_OVERLAY))
775 auto const sec =
section(SECTION_OVERLAY);
781 if (
auto val = sec.get(
"max_unknown_time"))
783 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
787 Throw<std::runtime_error>(
788 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
789 ": must be of the form '<number>' representing seconds.");
793 Throw<std::runtime_error>(
794 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
795 ": the time must be between 300 and 1800 seconds, inclusive.");
799 if (
auto val = sec.get(
"max_diverged_time"))
801 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
805 Throw<std::runtime_error>(
806 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
807 ": must be of the form '<number>' representing seconds.");
812 Throw<std::runtime_error>(
813 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
814 ": the time must be between 60 and 900 seconds, inclusive.");
819 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
822 boost::regex
const re(
823 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
825 if (!boost::regex_match(strTemp,
match, re))
826 Throw<std::runtime_error>(
827 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
828 ", must be: [0-9]+ [minutes|hours|days|weeks]");
831 beast::lexicalCastThrow<std::uint32_t>(
match[1].str());
833 if (boost::iequals(
match[2],
"minutes"))
835 else if (boost::iequals(
match[2],
"hours"))
837 else if (boost::iequals(
match[2],
"days"))
839 else if (boost::iequals(
match[2],
"weeks"))
843 Throw<std::runtime_error>(
844 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
845 ", the minimum amount of time an amendment must hold a "
846 "majority is 15 minutes");
863 boost::filesystem::path validatorsFile;
867 validatorsFile = strTemp;
869 if (validatorsFile.empty())
870 Throw<std::runtime_error>(
871 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
873 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
876 if (!boost::filesystem::exists(validatorsFile))
877 Throw<std::runtime_error>(
878 "The file specified in [" SECTION_VALIDATORS_FILE
881 validatorsFile.string());
884 !boost::filesystem::is_regular_file(validatorsFile) &&
885 !boost::filesystem::is_symlink(validatorsFile))
886 Throw<std::runtime_error>(
887 "Invalid file specified in [" SECTION_VALIDATORS_FILE
889 validatorsFile.string());
895 if (!validatorsFile.empty())
897 if (!boost::filesystem::exists(validatorsFile))
898 validatorsFile.clear();
900 !boost::filesystem::is_regular_file(validatorsFile) &&
901 !boost::filesystem::is_symlink(validatorsFile))
902 validatorsFile.clear();
906 if (!validatorsFile.empty() &&
907 boost::filesystem::exists(validatorsFile) &&
908 (boost::filesystem::is_regular_file(validatorsFile) ||
909 boost::filesystem::is_symlink(validatorsFile)))
911 boost::system::error_code ec;
915 Throw<std::runtime_error>(
916 "Failed to read '" + validatorsFile.string() +
"'." +
933 auto valSiteEntries =
937 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
945 auto valListThreshold =
948 if (valListThreshold)
949 section(SECTION_VALIDATOR_LIST_THRESHOLD)
950 .
append(*valListThreshold);
952 if (!entries && !valKeyEntries && !valListKeys)
953 Throw<std::runtime_error>(
954 "The file specified in [" SECTION_VALIDATORS_FILE
956 "does not contain a [" SECTION_VALIDATORS
958 "[" SECTION_VALIDATOR_KEYS
960 "[" SECTION_VALIDATOR_LIST_KEYS
963 validatorsFile.string());
967 auto const& listThreshold =
968 section(SECTION_VALIDATOR_LIST_THRESHOLD);
969 if (listThreshold.lines().empty())
971 else if (listThreshold.values().size() == 1)
973 auto strTemp = listThreshold.values()[0];
974 auto const listThreshold =
975 beast::lexicalCastThrow<std::size_t>(strTemp);
976 if (listThreshold == 0)
980 section(SECTION_VALIDATOR_LIST_KEYS).values().size())
982 Throw<std::runtime_error>(
983 "Value in config section "
984 "[" SECTION_VALIDATOR_LIST_THRESHOLD
985 "] exceeds the number of configured list keys");
987 return listThreshold;
991 Throw<std::runtime_error>(
993 "[" SECTION_VALIDATOR_LIST_THRESHOLD
994 "] should contain single value only");
1002 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
1003 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
1005 Throw<std::runtime_error>(
1007 "] config section is missing");
1012 auto const part =
section(
"features");
1013 for (
auto const& s : part.values())
1018 Throw<std::runtime_error>(
1019 "Unknown feature: " + s +
" in config file.");
1035 Throw<std::runtime_error>(
1036 "The minimum number of required peers (network_quorum) exceeds "
1037 "the maximum number of allowed peers (peers_max)");
1042boost::filesystem::path
1047 if (!log_file.empty() && !log_file.is_absolute())
1051 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
1054 if (!log_file.empty())
1056 auto log_dir = log_file.parent_path();
1058 if (!boost::filesystem::is_directory(log_dir))
1060 boost::system::error_code ec;
1061 boost::filesystem::create_directories(log_dir, ec);
1067 std::cerr <<
"Unable to create log file path " << log_dir
1068 <<
": " << ec.message() <<
'\n';
1082 "ripple::Config::getValueFor : valid index input");
1084 !node || *node <= 4,
1085 "ripple::Config::getValueFor : unset or valid node");
1095 if (
set(temp,
"reference_fee", section) &&
1101 if (
set(temp,
"account_reserve", section))
1103 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.