1#include <xrpld/core/Config.h>
3#include <xrpl/basics/FileUtilities.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/StringUtilities.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/beast/core/LexicalCast.h>
9#include <xrpl/beast/utility/Journal.h>
10#include <xrpl/beast/utility/instrumentation.h>
11#include <xrpl/config/BasicConfig.h>
12#include <xrpl/config/Constants.h>
13#include <xrpl/net/HTTPClient.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Fees.h>
16#include <xrpl/protocol/SystemParameters.h>
17#include <xrpl/rdb/DBInit.h>
18#include <xrpl/rdb/DatabaseCon.h>
20#include <boost/algorithm/string/classification.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/algorithm/string/split.hpp>
24#include <boost/algorithm/string/trim.hpp>
25#include <boost/multiprecision/detail/endian.hpp>
26#include <boost/predef.h>
27#include <boost/regex.hpp>
28#include <boost/regex/v5/regex.hpp>
29#include <boost/regex/v5/regex_match.hpp>
55#include <sysinfoapi.h>
59[[nodiscard]] std::uint64_t
62 if (MEMORYSTATUSEX msx{
sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
63 return static_cast<std::uint64_t
>(msx.ullTotalPhys);
73#include <sys/sysinfo.h>
77[[nodiscard]] std::uint64_t
80 if (
struct sysinfo si{}; sysinfo(&si) == 0)
81 return static_cast<std::uint64_t
>(si.totalram) * si.mem_unit;
91#include <sys/sysctl.h>
95[[nodiscard]] std::uint64_t
98 int mib[] = {CTL_HW, HW_MEMSIZE};
100 size_t size =
sizeof(ram);
102 if (sysctl(mib, 2, &ram, &size,
nullptr, 0) == 0)
103 return static_cast<std::uint64_t
>(ram);
116inline constexpr std::array<std::pair<SizedItem, std::array<int, 5>>, 13>
143 []
constexpr ->
bool {
156 "Mismatch between sized item enum & array indices");
162#define SECTION_DEFAULT_NAME ""
172 boost::algorithm::replace_all(strData,
"\r\n",
"\n");
175 boost::algorithm::replace_all(strData,
"\r",
"\n");
177 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of(
"\n"));
183 secResult[strSection] = IniFileSections::mapped_type();
186 for (
auto& strValue : vLines)
191 if (strValue.empty() || strValue[0] ==
'#')
195 else if (strValue[0] ==
'[' && strValue[strValue.length() - 1] ==
']')
198 strSection = strValue.
substr(1, strValue.length() - 2);
199 secResult.
emplace(strSection, IniFileSections::mapped_type{});
204 if (!strValue.empty())
205 secResult[strSection].push_back(strValue);
212IniFileSections::mapped_type*
215 if (
auto const it = secSource.
find(strSection); it != secSource.
end())
230 if ((pmtEntries !=
nullptr) && pmtEntries->size() == 1)
232 strValue = (*pmtEntries)[0];
239 if ((pmtEntries !=
nullptr) && pmtEntries->size() > 1)
242 std::format(
"[{}] requires exactly one value, not {}", strSection, pmtEntries->
size()));
245 if (pmtEntries !=
nullptr)
247 JLOG(j.
warn()) <<
"Section '" << strSection <<
"': requires 1 line not "
248 << pmtEntries->
size() <<
" lines.";
265 if (
auto const v =
std::getenv(name); v !=
nullptr)
290[[nodiscard]] std::vector<Candidate>
293 std::vector<Candidate> candidates;
302 if (std::filesystem::path
const home =
getEnvVar(
"HOME"); !home.
empty())
304 std::filesystem::path xdgConfigHome =
getEnvVar(
"XDG_CONFIG_HOME");
305 if (xdgConfigHome.
empty())
308 xdgConfigHome = home /
".config";
310 std::filesystem::path xdgDataHome =
getEnvVar(
"XDG_DATA_HOME");
311 if (xdgDataHome.
empty())
314 xdgDataHome = home /
".local" /
"share";
322 std::filesystem::path(
"/var/lib") /
systemName());
337 XRPL_ASSERT(
nodeSize == 0,
"xrpl::Config::setupControl : node size not set");
339 quiet_ = bQuiet || bSilent;
348 auto const& threshold =
352 return (limit == 0) || (ramSize_ < limit);
355 XRPL_ASSERT(ns != threshold.second.end(),
"xrpl::Config::setupControl : valid node size");
357 if (ns != threshold.second.end())
366 XRPL_ASSERT(
nodeSize <= 4,
"xrpl::Config::setupControl : node size is set");
381 if (!strConf.
empty())
391 auto const candidates = configCandidates();
395 for (
auto const& candidate : candidates)
398 dataDir = candidate.dataDir;
431 if (!dataDir.
empty())
467 auto const& section = config[name];
475 ss <<
"Invalid value '" << *optResult <<
"' for key 'port' in [" << name <<
"]";
522 for (
auto& line : strVec)
525 if (
std::count(line.begin(), line.end(),
':') != 1)
531 if (result.
size() == line.size())
553 if (strTemp ==
"main")
557 else if (strTemp ==
"testnet")
561 else if (strTemp ==
"devnet")
584 if (*peersInMaxOpt > 1000)
587 "Invalid value specified in [{}] section; the value must be less or equal than "
597 if (*peersOutMaxOpt < 10 || *peersOutMaxOpt > 1000)
600 "Invalid value specified in [{}] section; the value must be in range 10-1000",
606 if ((peersInMaxOpt && !peersOutMaxOpt) || (peersOutMaxOpt && !peersInMaxOpt))
609 "Both sections [{}] and [{}] must be configured",
614 if (peersInMaxOpt && peersOutMaxOpt)
623 if (boost::iequals(strTemp,
"tiny"))
627 else if (boost::iequals(strTemp,
"small"))
631 else if (boost::iequals(strTemp,
"medium"))
635 else if (boost::iequals(strTemp,
"large"))
639 else if (boost::iequals(strTemp,
"huge"))
663 if (boost::iequals(strTemp,
"all"))
667 else if (boost::iequals(strTemp,
"trusted"))
671 else if (boost::iequals(strTemp,
"drop_untrusted"))
684 if (boost::iequals(strTemp,
"all"))
688 else if (boost::iequals(strTemp,
"trusted"))
692 else if (boost::iequals(strTemp,
"drop_untrusted"))
706 "Cannot have both [{}] and [{}] config sections",
726 if (boost::iequals(strTemp,
"full"))
730 else if (boost::iequals(strTemp,
"none"))
742 if (boost::iequals(strTemp,
"none"))
746 else if (boost::iequals(strTemp,
"full"))
840 "Invalid {} cannot specify both vp_base_squelch_enable and vp_enable options. "
841 "vp_enable was deprecated and replaced by vp_base_squelch_enable",
871 "Invalid {} vp_base_squelch_max_selected_peers must be greater than or equal to 3",
885 "Invalid {}, tx_min_peers must be greater than or equal to 10, tx_relay_percentage "
886 "must be greater than or equal to 10 and less than or equal to 100",
902 "Invalid {}: the domain name does not appear to meet the requirements.",
923 "Invalid value 'max_unknown_time' in {}: must be of the form '<number>' "
924 "representing seconds.",
931 "Invalid value 'max_unknown_time' in {}: the time must be between 300 and 1800 "
932 "seconds, inclusive.",
944 "Invalid value 'max_diverged_time' in {}: must be of the form '<number>' "
945 "representing seconds.",
952 "Invalid value 'max_diverged_time' in {}: the time must be between 60 and 900 "
953 "seconds, inclusive.",
965 if (
auto val = sec.get(key))
971 "Invalid value '{}' in {}: must be of the form '<number>' representing a count "
980 "Invalid value '{}' in {}: the count must be between {} and {}, inclusive.",
997 boost::regex
const re(R
"(^\s*(\d+)\s*(minutes|hours|days|weeks)\s*(\s+.*)?$)");
999 if (!boost::regex_match(strTemp, match, re))
1002 "Invalid {}, must be: [0-9]+ [minutes|hours|days|weeks]",
1008 if (boost::iequals(match[2],
"minutes"))
1012 else if (boost::iequals(match[2],
"hours"))
1016 else if (boost::iequals(match[2],
"days"))
1020 else if (boost::iequals(match[2],
"weeks"))
1028 "Invalid {}, the minimum amount of time an amendment must hold a majority is 15 "
1052 validatorsFile = strTemp;
1054 if (validatorsFile.
empty())
1061 validatorsFile =
configDir / validatorsFile;
1066 "The file specified in [{}] does not exist: {}",
1068 validatorsFile.
string()));
1074 "Invalid file specified in [{}]: {}",
1076 validatorsFile.
string()));
1083 if (!validatorsFile.
empty())
1089 validatorsFile.
clear();
1103 "Failed to read '{}': {} (error {})",
1113 if (entries !=
nullptr)
1118 if (valKeyEntries !=
nullptr)
1123 if (valSiteEntries !=
nullptr)
1128 if (valListKeys !=
nullptr)
1133 if (valListThreshold !=
nullptr)
1136 if ((entries ==
nullptr) && (valKeyEntries ==
nullptr) && (valListKeys ==
nullptr))
1139 "The file specified in [{}] does not contain a [{}], [{}] or [{}] section: {}",
1144 validatorsFile.
string()));
1150 if (listThreshold.lines().empty())
1152 return std::nullopt;
1154 if (listThreshold.values().size() == 1)
1156 auto const strTemp = listThreshold.values()[0];
1158 if (listThreshold == 0)
1160 return std::nullopt;
1165 "Value in config section [{}] exceeds the number of configured list keys",
1168 return listThreshold;
1172 "Config section [{}] should contain single value only",
1189 for (
auto const& s : part.values())
1215 "The minimum number of required peers (network_quorum) exceeds "
1216 "the maximum number of allowed peers (peers_max)");
1226 if (!logFile.empty() && !logFile.is_absolute())
1233 if (!logFile.empty())
1235 auto const logDir = logFile.parent_path();
1246 std::cerr <<
"Unable to create log file path " << logDir <<
": " << ec.
message()
1259 XRPL_ASSERT(index <
kSizedItems.size(),
"xrpl::Config::getValueFor : valid index input");
1260 XRPL_ASSERT(!node || *node <= 4,
"xrpl::Config::getValueFor : unset or valid node");
1320 bool showRiskWarning =
false;
1322 if (
set(safetyLevel,
"safety_level", sqlite))
1324 if (boost::iequals(safetyLevel,
"low"))
1327 journalMode =
"memory";
1328 synchronous =
"off";
1329 tempStore =
"memory";
1330 showRiskWarning =
true;
1332 else if (!boost::iequals(safetyLevel,
"high"))
1335 std::format(
"Invalid safety_level value: {}", safetyLevel));
1342 if (
set(journalMode,
"journal_mode", sqlite) && !safetyLevel.
empty())
1345 "Configuration file may not define both "
1346 "\"safety_level\" and \"journal_mode\"");
1348 bool const higherRisk =
1349 boost::iequals(journalMode,
"memory") || boost::iequals(journalMode,
"off");
1350 showRiskWarning = showRiskWarning || higherRisk;
1351 if (higherRisk || boost::iequals(journalMode,
"delete") ||
1352 boost::iequals(journalMode,
"truncate") || boost::iequals(journalMode,
"persist") ||
1353 boost::iequals(journalMode,
"wal"))
1360 std::format(
"Invalid journal_mode value: {}", journalMode));
1366 if (
set(synchronous,
"synchronous", sqlite) && !safetyLevel.
empty())
1369 "Configuration file may not define both "
1370 "\"safety_level\" and \"synchronous\"");
1372 bool const higherRisk = boost::iequals(synchronous,
"off");
1373 showRiskWarning = showRiskWarning || higherRisk;
1374 if (higherRisk || boost::iequals(synchronous,
"normal") ||
1375 boost::iequals(synchronous,
"full") || boost::iequals(synchronous,
"extra"))
1382 std::format(
"Invalid synchronous value: {}", synchronous));
1388 if (
set(tempStore,
"temp_store", sqlite) && !safetyLevel.
empty())
1391 "Configuration file may not define both "
1392 "\"safety_level\" and \"temp_store\"");
1394 bool const higherRisk = boost::iequals(tempStore,
"memory");
1395 showRiskWarning = showRiskWarning || higherRisk;
1396 if (higherRisk || boost::iequals(tempStore,
"default") ||
1397 boost::iequals(tempStore,
"file"))
1409 JLOG(j->warn()) <<
"reducing the data integrity guarantees from the "
1410 "default [sqlite] behavior is not recommended for "
1411 "nodes storing large amounts of history, because of the "
1412 "difficulty inherent in rebuilding corrupted data.";
1415 result->size() == 3,
"xrpl::setup_DatabaseCon::globalPragma : result size is 3");
1425 setPragma(setup.
lgrPragma[0],
"journal_size_limit", 1582080);
1429 int64_t journalSizeLimit = 1582080;
1435 if (pageSize < 512 || pageSize > 65536)
1438 if ((pageSize & (pageSize - 1)) != 0)
1442 setPragma(setup.
txPragma[0],
"page_size", pageSize);
1443 setPragma(setup.
txPragma[1],
"journal_size_limit", journalSizeLimit);
1444 setPragma(setup.
txPragma[2],
"max_page_count", 4294967294);
1445 setPragma(setup.
txPragma[3],
"mmap_size", 17179869184);
A generic endpoint for log messages.
void build(IniFileSections const &ifs)
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
Section & section(std::string const &name)
Returns the section with the given name.
static constexpr int kMinJobQueueTx
std::filesystem::path configFile_
std::unordered_set< UInt256, beast::Uhash<> > features
static constexpr std::string_view kConfigLegacyName
std::optional< std::size_t > maxUntrustedCount
std::chrono::seconds maxDivergedTime
static constexpr int kMaxJobQueueTx
std::optional< int > sweepInterval
std::size_t txReduceRelayMinPeers
static constexpr std::size_t kMinManifestCount
std::optional< std::size_t > maxTrustedCount
static constexpr std::string_view kDatabaseDirName
std::size_t vpReduceRelaySquelchMaxSelectedPeers
////////////////// !
static constexpr std::string_view kConfigFileName
std::filesystem::path getDebugLogFile() const
Returns the full path and filename of the debug log file.
static constexpr std::size_t kMaxManifestCount
void setup(std::string const &strConf, bool bQuiet, bool bSilent, bool bStandalone)
Set up the configuration and determine the config and data directories.
std::chrono::seconds maxUnknownTime
std::uint64_t const ramSize_
int relayUntrustedProposals
int relayUntrustedValidations
std::vector< std::string > ipsFixed
bool vpReduceRelayBaseSquelchEnable
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
std::string sslVerifyFile
std::optional< std::size_t > maxSubscriptionsPerConnection
std::size_t txRelayPercentage
bool txReduceRelayMetrics
std::optional< std::size_t > validatorListThreshold
std::filesystem::path debugLogfile_
bool runStandalone_
Operate in stand-alone mode.
static constexpr std::string_view kValidatorsFileName
bool txReduceRelayEnable
////////////// END OF TEMPORARY CODE BLOCK /////////////////////
std::vector< std::string > ips
bool signingEnabled_
Determines if the server will sign a tx, given an account's secret seed.
void setupControl(bool bQuiet, bool bSilent, bool bStandalone)
std::uint32_t ledgerHistory
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.
std::size_t networkQuorum
std::filesystem::path configDir
std::chrono::seconds amendmentMajorityTime
static void initializeSSLContext(std::string const &sslVerifyDir, std::string const &sslVerifyFile, bool sslVerify, beast::Journal j)
Holds a collection of configuration values.
std::vector< std::string > const & values() const
Returns all the values in the section.
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
T create_directories(T... args)
T current_path(T... args)
T emplace_back(T... args)
T hardware_concurrency(T... args)
T is_directory(T... args)
T is_regular_file(T... args)
constexpr Out lexicalCastThrow(In in)
Convert from one type to another, throw on error.
constexpr Out lexicalCast(In in, Out defaultValue=Out())
Convert from one type to another.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
constexpr std::uint32_t kSqliteTuningCutoff
constexpr std::uint32_t kMinGasPrice
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > Days
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
static std::string getEnvVar(char const *name)
std::optional< UInt256 > getRegisteredFeature(std::string const &name)
FeeSetup setupFeeVote(Section const §ion)
bool getSingleSection(IniFileSections &secSource, std::string const &strSection, std::string &strValue, beast::Journal j)
IniFileSections::mapped_type * getIniFileSection(IniFileSections &secSource, std::string const &strSection)
constexpr std::array< std::pair< SizedItem, std::array< int, 5 > >, 13 > kSizedItems
IniFileSections parseIniFile(std::string const &strInput, bool const bTrim)
bool getIfExists(Section const §ion, std::string const &name, T &v)
std::string commonDbPragmaJournal(std::string_view journalMode)
std::string trimWhitespace(std::string str)
Remove leading and trailing ASCII whitespace.
std::string commonDbPragmaTemp(std::string_view tempStore)
static std::string const & systemName()
std::string getFileContents(std::error_code &ec, std::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
static void checkZeroPorts(Config const &config)
DatabaseCon::Setup setupDatabaseCon(Config const &c, std::optional< beast::Journal > j=std::nullopt)
constexpr std::uint32_t kMaxGasLimit
Hard protocol bounds on the Feature Extension fee settings.
std::chrono::duration< int, std::ratio_multiply< Days::period, std::ratio< 7 > > > Weeks
constexpr std::uint32_t kMaxBytecodeSizeLimit
std::unordered_map< std::string, std::vector< std::string > > IniFileSections
std::string commonDbPragmaSync(std::string_view synchronous)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
T regex_replace(T... args)
std::array< std::string, 4 > txPragma
static std::unique_ptr< std::vector< std::string > const > globalPragma
std::filesystem::path dataDir
std::array< std::string, 1 > lgrPragma
Fee schedule for startup / standalone, and to vote for.
XRPAmount accountReserve
The account reserve requirement in drops.
XRPAmount ownerReserve
The per-owned item reserve requirement in drops.
XRPAmount referenceFee
The cost of a reference transaction in drops.
std::uint32_t gasPrice
The price of 1 WASM gas, in micro-drops.
std::uint32_t bytecodeSizeLimit
The bytecode size limit for Feature Extensions.
std::uint32_t gasLimit
The gas limit for Feature Extensions.
static constexpr auto kPageSize
static constexpr auto kTxRelayPercentage
static constexpr auto kGasLimit
static constexpr auto kFastLoad
static constexpr auto kTxMetrics
static constexpr auto kAccountReserve
static constexpr auto kBytecodeSizeLimit
static constexpr auto kUseTxTables
static constexpr auto kReferenceFee
static constexpr auto kVpBaseSquelchEnable
static constexpr auto kPort
static constexpr auto kGasPrice
static constexpr auto kMaxUnknownTime
static constexpr auto kJournalSizeLimit
static constexpr auto kTxMinPeers
static constexpr auto kVpEnable
static constexpr auto kMaxTrustedCount
static constexpr auto kMaxUntrustedCount
static constexpr auto kVpBaseSquelchMaxSelectedPeers
static constexpr auto kMaxDivergedTime
static constexpr auto kTxEnable
static constexpr auto kOwnerReserve
static constexpr auto kAmendmentMajorityTime
static constexpr auto kMaxSubscriptionsPerConnection
static constexpr auto kPeersInMax
static constexpr auto kMaxTransactions
static constexpr auto kRelayProposals
static constexpr auto kSweepInterval
static constexpr auto kNodeSize
static constexpr auto kServerDomain
static constexpr auto kIps
static constexpr auto kOverlay
static constexpr auto kReduceRelay
static constexpr auto kValidatorListThreshold
static constexpr auto kValidators
static constexpr auto kSslVerifyFile
static constexpr auto kValidatorListKeys
static constexpr auto kSslVerifyDir
static constexpr auto kNetworkQuorum
static constexpr auto kValidationSeed
static constexpr auto kSigningSupport
static constexpr auto kCompression
static constexpr auto kPeersMax
static constexpr auto kPathSearchOld
static constexpr auto kServer
static constexpr auto kLedgerHistory
static constexpr auto kElbSupport
static constexpr auto kValidatorKeys
static constexpr auto kPeerPrivate
static constexpr auto kSslVerify
static constexpr auto kPeersOutMax
static constexpr auto kFeeDefault
static constexpr auto kValidatorsFile
static constexpr auto kPathSearchMax
static constexpr auto kRelayValidations
static constexpr auto kNodeDatabase
static constexpr auto kIpsFixed
static constexpr auto kDatabasePath
static constexpr auto kPathSearch
static constexpr auto kLedgerReplay
static constexpr auto kPrefetchWorkers
static constexpr auto kDebugLogfile
static constexpr auto kSqlite
static constexpr auto kValidatorToken
static constexpr auto kPathSearchFast
static constexpr auto kVoting
static constexpr auto kLedgerTxTables
static constexpr auto kBetaRpcApi
static constexpr auto kIoWorkers
static constexpr auto kFetchDepth
static constexpr auto kValidatorListSites
static constexpr auto kWorkers
static constexpr auto kNetworkId
static constexpr auto kFeatures