20#include <xrpld/core/Config.h>
21#include <xrpld/core/ConfigSections.h>
22#include <xrpld/net/HTTPClient.h>
24#include <xrpl/basics/FileUtilities.h>
25#include <xrpl/basics/Log.h>
26#include <xrpl/basics/StringUtilities.h>
27#include <xrpl/basics/contract.h>
28#include <xrpl/beast/core/LexicalCast.h>
29#include <xrpl/json/json_reader.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/SystemParameters.h>
33#include <boost/algorithm/string.hpp>
34#include <boost/format.hpp>
35#include <boost/predef.h>
36#include <boost/regex.hpp>
46#include <sysinfoapi.h>
54 if (MEMORYSTATUSEX msx{
sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
65#include <sys/sysinfo.h>
73 if (
struct sysinfo si; sysinfo(&si) == 0)
85#include <sys/sysctl.h>
94 int mib[] = {CTL_HW, HW_MEMSIZE};
96 size_t size =
sizeof(ram);
98 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
138 []() constexpr->bool {
151 "Mismatch between sized item enum & array indices");
158#define SECTION_DEFAULT_NAME ""
168 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
171 boost::algorithm::replace_all(strData,
"\r",
"\n");
173 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
179 secResult[strSection] = IniFileSections::mapped_type();
182 for (
auto& strValue : vLines)
185 boost::algorithm::trim(strValue);
187 if (strValue.empty() || strValue[0] ==
'#')
191 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
194 strSection = strValue.
substr(1, strValue.length() - 2);
195 secResult.
emplace(strSection, IniFileSections::mapped_type{});
200 if (!strValue.empty())
201 secResult[strSection].push_back(strValue);
208IniFileSections::mapped_type*
211 if (
auto it = secSource.
find(strSection); it != secSource.
end())
212 return &(it->second);
226 if (pmtEntries && pmtEntries->size() == 1)
228 strValue = (*pmtEntries)[0];
234 JLOG(j.
warn()) <<
"Section '" << strSection <<
"': requires 1 line not "
235 << pmtEntries->
size() <<
" lines.";
247char const*
const Config::configFileName =
"rippled.cfg";
248char const*
const Config::databaseDirName =
"db";
249char const*
const Config::validatorsFileName =
"validators.txt";
256 if (
auto const v =
std::getenv(name); v !=
nullptr)
263 : j_(
beast::Journal::getNullSink())
264 , ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
272 NODE_SIZE == 0,
"ripple::Config::setupControl : node size not set");
274 QUIET = bQuiet || bSilent;
283 auto const& threshold =
287 threshold.second.begin(),
288 threshold.second.end(),
290 return (limit == 0) || (ramSize_ < limit);
294 ns != threshold.second.end(),
295 "ripple::Config::setupControl : valid node size");
297 if (ns != threshold.second.end())
307 NODE_SIZE <= 4,
"ripple::Config::setupControl : node size is set");
317 boost::filesystem::path dataDir;
330 if (!strConf.
empty())
331 strConfFile = strConf;
335 if (!strConf.
empty())
345 CONFIG_DIR = boost::filesystem::current_path();
352 auto strXdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
353 auto strXdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
357 || (strHome.empty() &&
358 (strXdgConfigHome.empty() || strXdgDataHome.empty())))
364 if (strXdgConfigHome.empty())
367 strXdgConfigHome = strHome +
"/.config";
370 if (strXdgDataHome.empty())
373 strXdgDataHome = strHome +
"/.local/share";
378 dataDir = strXdgDataHome +
"/" +
systemName();
395 dataDir = boost::filesystem::path(dbPath);
400 if (!dataDir.empty())
402 boost::system::error_code ec;
403 boost::filesystem::create_directories(dataDir, ec);
406 Throw<std::runtime_error>(
407 boost::str(boost::format(
"Can not create %s") % dataDir));
409 legacy(
"database_path", boost::filesystem::absolute(dataDir).
string());
430 if (!config.
exists(
"server"))
438 auto const& section = config[name];
439 auto const optResult = section.get(
"port");
442 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
446 ss <<
"Invalid value '" << *optResult <<
"' for key 'port' in ["
448 Throw<std::runtime_error>(ss.
str());
463 boost::system::error_code ec;
494 for (
auto& line : strVec)
497 if (
std::count(line.begin(), line.end(),
':') != 1)
503 if (result.
size() == line.size())
516 boost::filesystem::path p(dbPath);
517 legacy(
"database_path", boost::filesystem::absolute(p).
string());
525 if (strTemp ==
"main")
527 else if (strTemp ==
"testnet")
529 else if (strTemp ==
"devnet")
532 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
540 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
547 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
548 if (*peers_in_max > 1000)
549 Throw<std::runtime_error>(
550 "Invalid value specified in [" SECTION_PEERS_IN_MAX
551 "] section; the value must be less or equal than 1000");
557 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
558 if (*peers_out_max < 10 || *peers_out_max > 1000)
559 Throw<std::runtime_error>(
560 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
561 "] section; the value must be in range 10-1000");
565 if ((peers_in_max && !peers_out_max) ||
566 (peers_out_max && !peers_in_max))
567 Throw<std::runtime_error>(
"Both sections [" SECTION_PEERS_IN_MAX
569 "and [" SECTION_PEERS_OUT_MAX
570 "] must be configured");
572 if (peers_in_max && peers_out_max)
581 if (boost::iequals(strTemp,
"tiny"))
583 else if (boost::iequals(strTemp,
"small"))
585 else if (boost::iequals(strTemp,
"medium"))
587 else if (boost::iequals(strTemp,
"large"))
589 else if (boost::iequals(strTemp,
"huge"))
593 4, beast::lexicalCastThrow<std::size_t>(strTemp));
600 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
606 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
610 if (boost::iequals(strTemp,
"all"))
612 else if (boost::iequals(strTemp,
"trusted"))
614 else if (boost::iequals(strTemp,
"drop_untrusted"))
617 Throw<std::runtime_error>(
618 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
624 if (boost::iequals(strTemp,
"all"))
626 else if (boost::iequals(strTemp,
"trusted"))
628 else if (boost::iequals(strTemp,
"drop_untrusted"))
631 Throw<std::runtime_error>(
632 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
636 if (
exists(SECTION_VALIDATION_SEED) &&
exists(SECTION_VALIDATOR_TOKEN))
637 Throw<std::runtime_error>(
"Cannot have both [" SECTION_VALIDATION_SEED
638 "] and [" SECTION_VALIDATOR_TOKEN
639 "] config sections");
653 if (boost::iequals(strTemp,
"full"))
656 else if (boost::iequals(strTemp,
"none"))
664 if (boost::iequals(strTemp,
"none"))
666 else if (boost::iequals(strTemp,
"full"))
669 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
677 if (
exists(SECTION_VALIDATION_SEED) ||
exists(SECTION_VALIDATOR_TOKEN))
683 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
696 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
697 Throw<std::runtime_error>(
"Invalid " SECTION_SWEEP_INTERVAL
698 ": must be between 10 and 600 inclusive");
703 WORKERS = beast::lexicalCastThrow<int>(strTemp);
705 if (WORKERS < 1 || WORKERS > 1024)
706 Throw<std::runtime_error>(
707 "Invalid " SECTION_WORKERS
708 ": must be between 1 and 1024 inclusive.");
713 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
715 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
716 Throw<std::runtime_error>(
717 "Invalid " SECTION_IO_WORKERS
718 ": must be between 1 and 1024 inclusive.");
725 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
726 Throw<std::runtime_error>(
727 "Invalid " SECTION_PREFETCH_WORKERS
728 ": must be between 1 and 1024 inclusive.");
732 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
737 if (
exists(SECTION_REDUCE_RELAY))
739 auto sec =
section(SECTION_REDUCE_RELAY);
747 if (sec.exists(
"vp_base_squelch_enable") && sec.exists(
"vp_enable"))
748 Throw<std::runtime_error>(
749 "Invalid " SECTION_REDUCE_RELAY
750 " cannot specify both vp_base_squelch_enable and vp_enable "
752 "vp_enable was deprecated and replaced by "
753 "vp_base_squelch_enable");
755 if (sec.exists(
"vp_base_squelch_enable"))
757 sec.value_or(
"vp_base_squelch_enable",
false);
758 else if (sec.exists(
"vp_enable"))
760 sec.value_or(
"vp_enable",
false);
770 sec.value_or(
"vp_base_squelch_max_selected_peers", 5);
772 Throw<std::runtime_error>(
773 "Invalid " SECTION_REDUCE_RELAY
774 " vp_base_squelch_max_selected_peers must be "
775 "greater than or equal to 3");
782 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
784 Throw<std::runtime_error>(
785 "Invalid " SECTION_REDUCE_RELAY
786 ", tx_min_peers must be greater than or equal to 10"
787 ", tx_relay_percentage must be greater than or equal to 10 "
788 "and less than or equal to 100");
794 beast::lexicalCastThrow<int>(strTemp),
803 Throw<std::runtime_error>(
804 "Invalid " SECTION_SERVER_DOMAIN
805 ": the domain name does not appear to meet the requirements.");
811 if (
exists(SECTION_OVERLAY))
813 auto const sec =
section(SECTION_OVERLAY);
819 if (
auto val = sec.get(
"max_unknown_time"))
821 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
825 Throw<std::runtime_error>(
826 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
827 ": must be of the form '<number>' representing seconds.");
831 Throw<std::runtime_error>(
832 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
833 ": the time must be between 300 and 1800 seconds, inclusive.");
837 if (
auto val = sec.get(
"max_diverged_time"))
839 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
843 Throw<std::runtime_error>(
844 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
845 ": must be of the form '<number>' representing seconds.");
850 Throw<std::runtime_error>(
851 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
852 ": the time must be between 60 and 900 seconds, inclusive.");
857 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp,
j_))
860 boost::regex
const re(
861 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
863 if (!boost::regex_match(strTemp,
match, re))
864 Throw<std::runtime_error>(
865 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
866 ", must be: [0-9]+ [minutes|hours|days|weeks]");
869 beast::lexicalCastThrow<std::uint32_t>(
match[1].str());
871 if (boost::iequals(
match[2],
"minutes"))
873 else if (boost::iequals(
match[2],
"hours"))
875 else if (boost::iequals(
match[2],
"days"))
877 else if (boost::iequals(
match[2],
"weeks"))
881 Throw<std::runtime_error>(
882 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
883 ", the minimum amount of time an amendment must hold a "
884 "majority is 15 minutes");
901 boost::filesystem::path validatorsFile;
905 validatorsFile = strTemp;
907 if (validatorsFile.empty())
908 Throw<std::runtime_error>(
909 "Invalid path specified in [" SECTION_VALIDATORS_FILE
"]");
911 if (!validatorsFile.is_absolute() && !
CONFIG_DIR.empty())
914 if (!boost::filesystem::exists(validatorsFile))
915 Throw<std::runtime_error>(
916 "The file specified in [" SECTION_VALIDATORS_FILE
919 validatorsFile.string());
922 !boost::filesystem::is_regular_file(validatorsFile) &&
923 !boost::filesystem::is_symlink(validatorsFile))
924 Throw<std::runtime_error>(
925 "Invalid file specified in [" SECTION_VALIDATORS_FILE
927 validatorsFile.string());
933 if (!validatorsFile.empty())
935 if (!boost::filesystem::exists(validatorsFile))
936 validatorsFile.clear();
938 !boost::filesystem::is_regular_file(validatorsFile) &&
939 !boost::filesystem::is_symlink(validatorsFile))
940 validatorsFile.clear();
944 if (!validatorsFile.empty() &&
945 boost::filesystem::exists(validatorsFile) &&
946 (boost::filesystem::is_regular_file(validatorsFile) ||
947 boost::filesystem::is_symlink(validatorsFile)))
949 boost::system::error_code ec;
953 Throw<std::runtime_error>(
954 "Failed to read '" + validatorsFile.string() +
"'." +
971 auto valSiteEntries =
975 section(SECTION_VALIDATOR_LIST_SITES).
append(*valSiteEntries);
983 auto valListThreshold =
986 if (valListThreshold)
987 section(SECTION_VALIDATOR_LIST_THRESHOLD)
988 .
append(*valListThreshold);
990 if (!entries && !valKeyEntries && !valListKeys)
991 Throw<std::runtime_error>(
992 "The file specified in [" SECTION_VALIDATORS_FILE
994 "does not contain a [" SECTION_VALIDATORS
996 "[" SECTION_VALIDATOR_KEYS
998 "[" SECTION_VALIDATOR_LIST_KEYS
1001 validatorsFile.string());
1005 auto const& listThreshold =
1006 section(SECTION_VALIDATOR_LIST_THRESHOLD);
1007 if (listThreshold.lines().empty())
1008 return std::nullopt;
1009 else if (listThreshold.values().size() == 1)
1011 auto strTemp = listThreshold.values()[0];
1012 auto const listThreshold =
1013 beast::lexicalCastThrow<std::size_t>(strTemp);
1014 if (listThreshold == 0)
1015 return std::nullopt;
1018 section(SECTION_VALIDATOR_LIST_KEYS).values().size())
1020 Throw<std::runtime_error>(
1021 "Value in config section "
1022 "[" SECTION_VALIDATOR_LIST_THRESHOLD
1023 "] exceeds the number of configured list keys");
1025 return listThreshold;
1029 Throw<std::runtime_error>(
1031 "[" SECTION_VALIDATOR_LIST_THRESHOLD
1032 "] should contain single value only");
1040 if (!
section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
1041 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
1043 Throw<std::runtime_error>(
1045 "] config section is missing");
1050 auto const part =
section(
"features");
1051 for (
auto const& s : part.values())
1056 Throw<std::runtime_error>(
1057 "Unknown feature: " + s +
" in config file.");
1073 Throw<std::runtime_error>(
1074 "The minimum number of required peers (network_quorum) exceeds "
1075 "the maximum number of allowed peers (peers_max)");
1080boost::filesystem::path
1085 if (!log_file.empty() && !log_file.is_absolute())
1089 log_file = boost::filesystem::absolute(log_file,
CONFIG_DIR);
1092 if (!log_file.empty())
1094 auto log_dir = log_file.parent_path();
1096 if (!boost::filesystem::is_directory(log_dir))
1098 boost::system::error_code ec;
1099 boost::filesystem::create_directories(log_dir, ec);
1105 std::cerr <<
"Unable to create log file path " << log_dir
1106 <<
": " << ec.message() <<
'\n';
1120 "ripple::Config::getValueFor : valid index input");
1122 !node || *node <= 4,
1123 "ripple::Config::getValueFor : unset or valid node");
1133 if (
set(temp,
"reference_fee", section) &&
1139 if (
set(temp,
"account_reserve", section))
1141 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
std::size_t TX_REDUCE_RELAY_MIN_PEERS
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
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
static char const *const validatorsFileName
std::chrono::seconds MAX_UNKNOWN_TIME
std::size_t VP_REDUCE_RELAY_SQUELCH_MAX_SELECTED_PEERS
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)
IniFileSections parseIniFile(std::string const &strInput, bool const bTrim)
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)
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.