rippled
Loading...
Searching...
No Matches
Config.cpp
1#include <xrpld/core/Config.h>
2#include <xrpld/core/ConfigSections.h>
3
4#include <xrpl/basics/FileUtilities.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/StringUtilities.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/beast/core/LexicalCast.h>
9#include <xrpl/json/json_reader.h>
10#include <xrpl/net/HTTPClient.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/SystemParameters.h>
13
14#include <boost/algorithm/string.hpp>
15#include <boost/format.hpp>
16#include <boost/predef.h>
17#include <boost/regex.hpp>
18
19#include <algorithm>
20#include <cstdlib>
21#include <iostream>
22#include <iterator>
23#include <regex>
24#include <thread>
25
26#if BOOST_OS_WINDOWS
27#include <sysinfoapi.h>
28
29namespace xrpl {
30namespace detail {
31
32[[nodiscard]] std::uint64_t
33getMemorySize()
34{
35 if (MEMORYSTATUSEX msx{sizeof(MEMORYSTATUSEX)}; GlobalMemoryStatusEx(&msx))
36 return static_cast<std::uint64_t>(msx.ullTotalPhys);
37
38 return 0;
39}
40
41} // namespace detail
42} // namespace xrpl
43#endif
44
45#if BOOST_OS_LINUX
46#include <sys/sysinfo.h>
47
48namespace xrpl {
49namespace detail {
50
51[[nodiscard]] std::uint64_t
52getMemorySize()
53{
54 if (struct sysinfo si; sysinfo(&si) == 0)
55 return static_cast<std::uint64_t>(si.totalram) * si.mem_unit;
56
57 return 0;
58}
59
60} // namespace detail
61} // namespace xrpl
62
63#endif
64
65#if BOOST_OS_MACOS
66#include <sys/sysctl.h>
67#include <sys/types.h>
68
69namespace xrpl {
70namespace detail {
71
72[[nodiscard]] std::uint64_t
73getMemorySize()
74{
75 int mib[] = {CTL_HW, HW_MEMSIZE};
76 std::int64_t ram = 0;
77 size_t size = sizeof(ram);
78
79 if (sysctl(mib, 2, &ram, &size, NULL, 0) == 0)
80 return static_cast<std::uint64_t>(ram);
81
82 return 0;
83}
84
85} // namespace detail
86} // namespace xrpl
87#endif
88
89namespace xrpl {
90
91// clang-format off
92// The configurable node sizes are "tiny", "small", "medium", "large", "huge"
95{{
96 // FIXME: We should document each of these items, explaining exactly
97 // what they control and whether there exists an explicit
98 // config option that can be used to override the default.
99
100 // tiny small medium large huge
101 {SizedItem::sweepInterval, {{ 10, 30, 60, 90, 120 }}},
102 {SizedItem::treeCacheSize, {{ 262144, 524288, 2097152, 4194304, 8388608 }}},
103 {SizedItem::treeCacheAge, {{ 30, 60, 90, 120, 900 }}},
104 {SizedItem::ledgerSize, {{ 32, 32, 64, 256, 384 }}},
105 {SizedItem::ledgerAge, {{ 30, 60, 180, 300, 600 }}},
106 {SizedItem::ledgerFetch, {{ 2, 3, 4, 5, 8 }}},
107 {SizedItem::hashNodeDBCache, {{ 4, 12, 24, 64, 128 }}},
108 {SizedItem::txnDBCache, {{ 4, 12, 24, 64, 128 }}},
109 {SizedItem::lgrDBCache, {{ 4, 8, 16, 32, 128 }}},
110 {SizedItem::openFinalLimit, {{ 8, 16, 32, 64, 128 }}},
111 {SizedItem::burstSize, {{ 4, 8, 16, 32, 48 }}},
112 {SizedItem::ramSizeGB, {{ 6, 8, 12, 24, 0 }}},
113 {SizedItem::accountIdCacheSize, {{ 20047, 50053, 77081, 150061, 300007 }}}
114}};
115
116// Ensure that the order of entries in the table corresponds to the
117// order of entries in the enum:
118static_assert(
119 []() constexpr->bool {
121
122 for (auto const& i : sizedItems)
123 {
124 if (static_cast<std::underlying_type_t<SizedItem>>(i.first) != idx)
125 return false;
126
127 ++idx;
128 }
129
130 return true;
131 }(),
132 "Mismatch between sized item enum & array indices");
133// clang-format on
134
135//
136// TODO: Check permissions on config file before using it.
137//
138
139#define SECTION_DEFAULT_NAME ""
140
142parseIniFile(std::string const& strInput, bool const bTrim)
143{
144 std::string strData(strInput);
146 IniFileSections secResult;
147
148 // Convert DOS format to unix.
149 boost::algorithm::replace_all(strData, "\r\n", "\n");
150
151 // Convert MacOS format to unix.
152 boost::algorithm::replace_all(strData, "\r", "\n");
153
154 boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of("\n"));
155
156 // Set the default Section name.
157 std::string strSection = SECTION_DEFAULT_NAME;
158
159 // Initialize the default Section.
160 secResult[strSection] = IniFileSections::mapped_type();
161
162 // Parse each line.
163 for (auto& strValue : vLines)
164 {
165 if (bTrim)
166 boost::algorithm::trim(strValue);
167
168 if (strValue.empty() || strValue[0] == '#')
169 {
170 // Blank line or comment, do nothing.
171 }
172 else if (strValue[0] == '[' && strValue[strValue.length() - 1] == ']')
173 {
174 // New Section.
175 strSection = strValue.substr(1, strValue.length() - 2);
176 secResult.emplace(strSection, IniFileSections::mapped_type{});
177 }
178 else
179 {
180 // Another line for Section.
181 if (!strValue.empty())
182 secResult[strSection].push_back(strValue);
183 }
184 }
185
186 return secResult;
187}
188
189IniFileSections::mapped_type*
190getIniFileSection(IniFileSections& secSource, std::string const& strSection)
191{
192 if (auto it = secSource.find(strSection); it != secSource.end())
193 return &(it->second);
194
195 return nullptr;
196}
197
198bool
200 IniFileSections& secSource,
201 std::string const& strSection,
202 std::string& strValue,
204{
205 auto const pmtEntries = getIniFileSection(secSource, strSection);
206
207 if (pmtEntries && pmtEntries->size() == 1)
208 {
209 strValue = (*pmtEntries)[0];
210 return true;
211 }
212
213 if (pmtEntries)
214 {
215 JLOG(j.warn()) << "Section '" << strSection << "': requires 1 line not "
216 << pmtEntries->size() << " lines.";
217 }
218
219 return false;
220}
221
222//------------------------------------------------------------------------------
223//
224// Config
225//
226//------------------------------------------------------------------------------
227
228char const* const Config::configFileName = "xrpld.cfg";
229char const* const Config::configLegacyName = "rippled.cfg";
230char const* const Config::databaseDirName = "db";
231char const* const Config::validatorsFileName = "validators.txt";
232
233[[nodiscard]] static std::string
234getEnvVar(char const* name)
235{
236 std::string value;
237
238 if (auto const v = std::getenv(name); v != nullptr)
239 value = v;
240
241 return value;
242}
243
244Config::Config()
245 : j_(beast::Journal::getNullSink())
246 , ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
247{
248}
249
250void
251Config::setupControl(bool bQuiet, bool bSilent, bool bStandalone)
252{
253 XRPL_ASSERT(
254 NODE_SIZE == 0, "xrpl::Config::setupControl : node size not set");
255
256 QUIET = bQuiet || bSilent;
257 SILENT = bSilent;
258 RUN_STANDALONE = bStandalone;
259
260 // We try to autodetect the appropriate node size by checking available
261 // RAM and CPU resources. We default to "tiny" for standalone mode.
262 if (!bStandalone)
263 {
264 // First, check against 'minimum' RAM requirements per node size:
265 auto const& threshold =
267
268 auto ns = std::find_if(
269 threshold.second.begin(),
270 threshold.second.end(),
271 [this](std::size_t limit) {
272 return (limit == 0) || (ramSize_ < limit);
273 });
274
275 XRPL_ASSERT(
276 ns != threshold.second.end(),
277 "xrpl::Config::setupControl : valid node size");
278
279 if (ns != threshold.second.end())
280 NODE_SIZE = std::distance(threshold.second.begin(), ns);
281
282 // Adjust the size based on the number of hardware threads of
283 // execution available to us:
284 if (auto const hc = std::thread::hardware_concurrency(); hc != 0)
286 }
287
288 XRPL_ASSERT(
289 NODE_SIZE <= 4, "xrpl::Config::setupControl : node size is set");
290}
291
292void
294 std::string const& strConf,
295 bool bQuiet,
296 bool bSilent,
297 bool bStandalone)
298{
299 setupControl(bQuiet, bSilent, bStandalone);
300
301 // Determine the config and data directories.
302 // If the config file is found in the current working
303 // directory, use the current working directory as the
304 // config directory and that with "db" as the data
305 // directory.
306 boost::filesystem::path dataDir;
307
308 if (!strConf.empty())
309 {
310 // --conf=<path> : everything is relative that file.
311 CONFIG_FILE = strConf;
312 CONFIG_DIR = boost::filesystem::absolute(CONFIG_FILE);
313 CONFIG_DIR.remove_filename();
314 dataDir = CONFIG_DIR / databaseDirName;
315 }
316 else
317 {
318 do
319 {
320 // Check if either of the config files exist in the current working
321 // directory, in which case the databases will be stored in a
322 // subdirectory.
323 CONFIG_DIR = boost::filesystem::current_path();
324 dataDir = CONFIG_DIR / databaseDirName;
326 if (boost::filesystem::exists(CONFIG_FILE))
327 break;
329 if (boost::filesystem::exists(CONFIG_FILE))
330 break;
331
332 // Check if the home directory is set, and optionally the XDG config
333 // and/or data directories, as the config may be there. See
334 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html.
335 auto const strHome = getEnvVar("HOME");
336 if (!strHome.empty())
337 {
338 auto strXdgConfigHome = getEnvVar("XDG_CONFIG_HOME");
339 auto strXdgDataHome = getEnvVar("XDG_DATA_HOME");
340 if (strXdgConfigHome.empty())
341 {
342 // $XDG_CONFIG_HOME was not set, use default based on $HOME.
343 strXdgConfigHome = strHome + "/.config";
344 }
345 if (strXdgDataHome.empty())
346 {
347 // $XDG_DATA_HOME was not set, use default based on $HOME.
348 strXdgDataHome = strHome + "/.local/share";
349 }
350
351 // Check if either of the config files exist in the XDG config
352 // dir.
353 dataDir = strXdgDataHome + "/" + systemName();
354 CONFIG_DIR = strXdgConfigHome + "/" + systemName();
356 if (boost::filesystem::exists(CONFIG_FILE))
357 break;
359 if (boost::filesystem::exists(CONFIG_FILE))
360 break;
361 }
362
363 // As a last resort, check the system config directory.
364 dataDir = "/var/opt/" + systemName();
365 CONFIG_DIR = "/etc/opt/" + systemName();
367 if (boost::filesystem::exists(CONFIG_FILE))
368 break;
370 } while (false);
371 }
372
373 // Update default values
374 load();
375 {
376 // load() may have set a new value for the dataDir
377 std::string const dbPath(legacy("database_path"));
378 if (!dbPath.empty())
379 dataDir = boost::filesystem::path(dbPath);
380 }
381
382 if (!RUN_STANDALONE)
383 {
384 boost::system::error_code ec;
385 boost::filesystem::create_directories(dataDir, ec);
386
387 if (ec)
388 Throw<std::runtime_error>(
389 boost::str(boost::format("Can not create %s") % dataDir));
390
391 legacy("database_path", boost::filesystem::absolute(dataDir).string());
392 }
393
395 this->SSL_VERIFY_DIR, this->SSL_VERIFY_FILE, this->SSL_VERIFY, j_);
396
397 if (RUN_STANDALONE)
398 LEDGER_HISTORY = 0;
399
400 std::string ledgerTxDbType;
401 Section ledgerTxTablesSection = section("ledger_tx_tables");
402 get_if_exists(ledgerTxTablesSection, "use_tx_tables", USE_TX_TABLES);
403
405 get_if_exists(nodeDbSection, "fast_load", FAST_LOAD);
406}
407
408// 0 ports are allowed for unit tests, but still not allowed to be present in
409// config file
410static void
411checkZeroPorts(Config const& config)
412{
413 if (!config.exists("server"))
414 return;
415
416 for (auto const& name : config.section("server").values())
417 {
418 if (!config.exists(name))
419 return;
420
421 auto const& section = config[name];
422 auto const optResult = section.get("port");
423 if (optResult)
424 {
425 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
426 if (!port)
427 {
429 ss << "Invalid value '" << *optResult << "' for key 'port' in ["
430 << name << "]";
431 Throw<std::runtime_error>(ss.str());
432 }
433 }
434 }
435}
436
437void
439{
440 // NOTE: this writes to cerr because we want cout to be reserved
441 // for the writing of the json response (so that stdout can be part of a
442 // pipeline, for instance)
443 if (!QUIET)
444 std::cerr << "Loading: " << CONFIG_FILE << "\n";
445
446 boost::system::error_code ec;
447 auto const fileContents = getFileContents(ec, CONFIG_FILE);
448
449 if (ec)
450 {
451 std::cerr << "Failed to read '" << CONFIG_FILE << "'." << ec.value()
452 << ": " << ec.message() << std::endl;
453 return;
454 }
455
456 loadFromString(fileContents);
457 checkZeroPorts(*this);
458}
459
460void
462{
463 IniFileSections secConfig = parseIniFile(fileContents, true);
464
465 build(secConfig);
466
467 if (auto s = getIniFileSection(secConfig, SECTION_IPS))
468 IPS = *s;
469
470 if (auto s = getIniFileSection(secConfig, SECTION_IPS_FIXED))
471 IPS_FIXED = *s;
472
473 // if the user has specified ip:port then replace : with a space.
474 {
475 auto replaceColons = [](std::vector<std::string>& strVec) {
476 static std::regex const e(":([0-9]+)$");
477 for (auto& line : strVec)
478 {
479 // skip anything that might be an ipv6 address
480 if (std::count(line.begin(), line.end(), ':') != 1)
481 continue;
482
483 std::string result = std::regex_replace(line, e, " $1");
484 // sanity check the result of the replace, should be same length
485 // as input
486 if (result.size() == line.size())
487 line = result;
488 }
489 };
490
491 replaceColons(IPS_FIXED);
492 replaceColons(IPS);
493 }
494
495 {
496 std::string dbPath;
497 if (getSingleSection(secConfig, "database_path", dbPath, j_))
498 {
499 boost::filesystem::path p(dbPath);
500 legacy("database_path", boost::filesystem::absolute(p).string());
501 }
502 }
503
504 std::string strTemp;
505
506 if (getSingleSection(secConfig, SECTION_NETWORK_ID, strTemp, j_))
507 {
508 if (strTemp == "main")
509 NETWORK_ID = 0;
510 else if (strTemp == "testnet")
511 NETWORK_ID = 1;
512 else if (strTemp == "devnet")
513 NETWORK_ID = 2;
514 else
515 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
516 }
517
518 if (getSingleSection(secConfig, SECTION_PEER_PRIVATE, strTemp, j_))
519 PEER_PRIVATE = beast::lexicalCastThrow<bool>(strTemp);
520
521 if (getSingleSection(secConfig, SECTION_PEERS_MAX, strTemp, j_))
522 {
523 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
524 }
525 else
526 {
527 std::optional<std::size_t> peers_in_max{};
528 if (getSingleSection(secConfig, SECTION_PEERS_IN_MAX, strTemp, j_))
529 {
530 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
531 if (*peers_in_max > 1000)
532 Throw<std::runtime_error>(
533 "Invalid value specified in [" SECTION_PEERS_IN_MAX
534 "] section; the value must be less or equal than 1000");
535 }
536
537 std::optional<std::size_t> peers_out_max{};
538 if (getSingleSection(secConfig, SECTION_PEERS_OUT_MAX, strTemp, j_))
539 {
540 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
541 if (*peers_out_max < 10 || *peers_out_max > 1000)
542 Throw<std::runtime_error>(
543 "Invalid value specified in [" SECTION_PEERS_OUT_MAX
544 "] section; the value must be in range 10-1000");
545 }
546
547 // if one section is configured then the other must be configured too
548 if ((peers_in_max && !peers_out_max) ||
549 (peers_out_max && !peers_in_max))
550 Throw<std::runtime_error>("Both sections [" SECTION_PEERS_IN_MAX
551 "]"
552 "and [" SECTION_PEERS_OUT_MAX
553 "] must be configured");
554
555 if (peers_in_max && peers_out_max)
556 {
557 PEERS_IN_MAX = *peers_in_max;
558 PEERS_OUT_MAX = *peers_out_max;
559 }
560 }
561
562 if (getSingleSection(secConfig, SECTION_NODE_SIZE, strTemp, j_))
563 {
564 if (boost::iequals(strTemp, "tiny"))
565 NODE_SIZE = 0;
566 else if (boost::iequals(strTemp, "small"))
567 NODE_SIZE = 1;
568 else if (boost::iequals(strTemp, "medium"))
569 NODE_SIZE = 2;
570 else if (boost::iequals(strTemp, "large"))
571 NODE_SIZE = 3;
572 else if (boost::iequals(strTemp, "huge"))
573 NODE_SIZE = 4;
574 else
576 4, beast::lexicalCastThrow<std::size_t>(strTemp));
577 }
578
579 if (getSingleSection(secConfig, SECTION_SIGNING_SUPPORT, strTemp, j_))
580 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
581
582 if (getSingleSection(secConfig, SECTION_ELB_SUPPORT, strTemp, j_))
583 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
584
585 getSingleSection(secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_);
586 getSingleSection(secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_);
587
588 if (getSingleSection(secConfig, SECTION_SSL_VERIFY, strTemp, j_))
589 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
590
591 if (getSingleSection(secConfig, SECTION_RELAY_VALIDATIONS, strTemp, j_))
592 {
593 if (boost::iequals(strTemp, "all"))
595 else if (boost::iequals(strTemp, "trusted"))
597 else if (boost::iequals(strTemp, "drop_untrusted"))
599 else
600 Throw<std::runtime_error>(
601 "Invalid value specified in [" SECTION_RELAY_VALIDATIONS
602 "] section");
603 }
604
605 if (getSingleSection(secConfig, SECTION_RELAY_PROPOSALS, strTemp, j_))
606 {
607 if (boost::iequals(strTemp, "all"))
609 else if (boost::iequals(strTemp, "trusted"))
611 else if (boost::iequals(strTemp, "drop_untrusted"))
613 else
614 Throw<std::runtime_error>(
615 "Invalid value specified in [" SECTION_RELAY_PROPOSALS
616 "] section");
617 }
618
619 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
620 Throw<std::runtime_error>("Cannot have both [" SECTION_VALIDATION_SEED
621 "] and [" SECTION_VALIDATOR_TOKEN
622 "] config sections");
623
624 if (getSingleSection(secConfig, SECTION_NETWORK_QUORUM, strTemp, j_))
625 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
626
627 FEES = setup_FeeVote(section("voting"));
628 /* [fee_default] is documented in the example config files as useful for
629 * things like offline transaction signing. Until that's completely
630 * deprecated, allow it to override the [voting] section. */
631 if (getSingleSection(secConfig, SECTION_FEE_DEFAULT, strTemp, j_))
632 FEES.reference_fee = beast::lexicalCastThrow<std::uint64_t>(strTemp);
633
634 if (getSingleSection(secConfig, SECTION_LEDGER_HISTORY, strTemp, j_))
635 {
636 if (boost::iequals(strTemp, "full"))
638 std::numeric_limits<decltype(LEDGER_HISTORY)>::max();
639 else if (boost::iequals(strTemp, "none"))
640 LEDGER_HISTORY = 0;
641 else
642 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
643 }
644
645 if (getSingleSection(secConfig, SECTION_FETCH_DEPTH, strTemp, j_))
646 {
647 if (boost::iequals(strTemp, "none"))
648 FETCH_DEPTH = 0;
649 else if (boost::iequals(strTemp, "full"))
650 FETCH_DEPTH = std::numeric_limits<decltype(FETCH_DEPTH)>::max();
651 else
652 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
653
654 if (FETCH_DEPTH < 10)
655 FETCH_DEPTH = 10;
656 }
657
658 // By default, validators don't have pathfinding enabled, unless it is
659 // explicitly requested by the server's admin.
660 if (exists(SECTION_VALIDATION_SEED) || exists(SECTION_VALIDATOR_TOKEN))
661 PATH_SEARCH_MAX = 0;
662
663 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_OLD, strTemp, j_))
664 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
665 if (getSingleSection(secConfig, SECTION_PATH_SEARCH, strTemp, j_))
666 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
667 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_FAST, strTemp, j_))
668 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
669 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_MAX, strTemp, j_))
670 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
671
672 if (getSingleSection(secConfig, SECTION_DEBUG_LOGFILE, strTemp, j_))
673 DEBUG_LOGFILE = strTemp;
674
675 if (getSingleSection(secConfig, SECTION_SWEEP_INTERVAL, strTemp, j_))
676 {
677 SWEEP_INTERVAL = beast::lexicalCastThrow<std::size_t>(strTemp);
678
679 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
680 Throw<std::runtime_error>("Invalid " SECTION_SWEEP_INTERVAL
681 ": must be between 10 and 600 inclusive");
682 }
683
684 if (getSingleSection(secConfig, SECTION_WORKERS, strTemp, j_))
685 {
686 WORKERS = beast::lexicalCastThrow<int>(strTemp);
687
688 if (WORKERS < 1 || WORKERS > 1024)
689 Throw<std::runtime_error>(
690 "Invalid " SECTION_WORKERS
691 ": must be between 1 and 1024 inclusive.");
692 }
693
694 if (getSingleSection(secConfig, SECTION_IO_WORKERS, strTemp, j_))
695 {
696 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
697
698 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
699 Throw<std::runtime_error>(
700 "Invalid " SECTION_IO_WORKERS
701 ": must be between 1 and 1024 inclusive.");
702 }
703
704 if (getSingleSection(secConfig, SECTION_PREFETCH_WORKERS, strTemp, j_))
705 {
706 PREFETCH_WORKERS = beast::lexicalCastThrow<int>(strTemp);
707
708 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
709 Throw<std::runtime_error>(
710 "Invalid " SECTION_PREFETCH_WORKERS
711 ": must be between 1 and 1024 inclusive.");
712 }
713
714 if (getSingleSection(secConfig, SECTION_COMPRESSION, strTemp, j_))
715 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
716
717 if (getSingleSection(secConfig, SECTION_LEDGER_REPLAY, strTemp, j_))
718 LEDGER_REPLAY = beast::lexicalCastThrow<bool>(strTemp);
719
720 if (exists(SECTION_REDUCE_RELAY))
721 {
722 auto sec = section(SECTION_REDUCE_RELAY);
723
725 // vp_enable config option is deprecated by vp_base_squelch_enable //
726 // This option is kept for backwards compatibility. When squelching //
727 // is the default algorithm, it must be replaced with: //
728 // VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = //
729 // sec.value_or("vp_base_squelch_enable", true); //
730 if (sec.exists("vp_base_squelch_enable") && sec.exists("vp_enable"))
731 Throw<std::runtime_error>(
732 "Invalid " SECTION_REDUCE_RELAY
733 " cannot specify both vp_base_squelch_enable and vp_enable "
734 "options. "
735 "vp_enable was deprecated and replaced by "
736 "vp_base_squelch_enable");
737
738 if (sec.exists("vp_base_squelch_enable"))
740 sec.value_or("vp_base_squelch_enable", false);
741 else if (sec.exists("vp_enable"))
743 sec.value_or("vp_enable", false);
744 else
747
749 // Temporary squelching config for the peers selected as a source of //
750 // validator messages. The config must be removed once squelching is //
751 // made the default routing algorithm. //
753 sec.value_or("vp_base_squelch_max_selected_peers", 5);
755 Throw<std::runtime_error>(
756 "Invalid " SECTION_REDUCE_RELAY
757 " vp_base_squelch_max_selected_peers must be "
758 "greater than or equal to 3");
760
761 TX_REDUCE_RELAY_ENABLE = sec.value_or("tx_enable", false);
762 TX_REDUCE_RELAY_METRICS = sec.value_or("tx_metrics", false);
763 TX_REDUCE_RELAY_MIN_PEERS = sec.value_or("tx_min_peers", 20);
764 TX_RELAY_PERCENTAGE = sec.value_or("tx_relay_percentage", 25);
765 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 ||
767 Throw<std::runtime_error>(
768 "Invalid " SECTION_REDUCE_RELAY
769 ", tx_min_peers must be greater than or equal to 10"
770 ", tx_relay_percentage must be greater than or equal to 10 "
771 "and less than or equal to 100");
772 }
773
774 if (getSingleSection(secConfig, SECTION_MAX_TRANSACTIONS, strTemp, j_))
775 {
777 beast::lexicalCastThrow<int>(strTemp),
780 }
781
782 if (getSingleSection(secConfig, SECTION_SERVER_DOMAIN, strTemp, j_))
783 {
784 if (!isProperlyFormedTomlDomain(strTemp))
785 {
786 Throw<std::runtime_error>(
787 "Invalid " SECTION_SERVER_DOMAIN
788 ": the domain name does not appear to meet the requirements.");
789 }
790
791 SERVER_DOMAIN = strTemp;
792 }
793
794 if (exists(SECTION_OVERLAY))
795 {
796 auto const sec = section(SECTION_OVERLAY);
797
798 using namespace std::chrono;
799
800 try
801 {
802 if (auto val = sec.get("max_unknown_time"))
804 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
805 }
806 catch (...)
807 {
808 Throw<std::runtime_error>(
809 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
810 ": must be of the form '<number>' representing seconds.");
811 }
812
813 if (MAX_UNKNOWN_TIME < seconds{300} || MAX_UNKNOWN_TIME > seconds{1800})
814 Throw<std::runtime_error>(
815 "Invalid value 'max_unknown_time' in " SECTION_OVERLAY
816 ": the time must be between 300 and 1800 seconds, inclusive.");
817
818 try
819 {
820 if (auto val = sec.get("max_diverged_time"))
822 seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
823 }
824 catch (...)
825 {
826 Throw<std::runtime_error>(
827 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
828 ": must be of the form '<number>' representing seconds.");
829 }
830
832 {
833 Throw<std::runtime_error>(
834 "Invalid value 'max_diverged_time' in " SECTION_OVERLAY
835 ": the time must be between 60 and 900 seconds, inclusive.");
836 }
837 }
838
840 secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
841 {
842 using namespace std::chrono;
843 boost::regex const re(
844 "^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
845 boost::smatch match;
846 if (!boost::regex_match(strTemp, match, re))
847 Throw<std::runtime_error>(
848 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
849 ", must be: [0-9]+ [minutes|hours|days|weeks]");
850
852 beast::lexicalCastThrow<std::uint32_t>(match[1].str());
853
854 if (boost::iequals(match[2], "minutes"))
856 else if (boost::iequals(match[2], "hours"))
858 else if (boost::iequals(match[2], "days"))
860 else if (boost::iequals(match[2], "weeks"))
862
864 Throw<std::runtime_error>(
865 "Invalid " SECTION_AMENDMENT_MAJORITY_TIME
866 ", the minimum amount of time an amendment must hold a "
867 "majority is 15 minutes");
868 }
869
870 if (getSingleSection(secConfig, SECTION_BETA_RPC_API, strTemp, j_))
871 BETA_RPC_API = beast::lexicalCastThrow<bool>(strTemp);
872
873 // Do not load trusted validator configuration for standalone mode
874 if (!RUN_STANDALONE)
875 {
876 // If a file was explicitly specified, then throw if the
877 // path is malformed or if the file does not exist or is
878 // not a file.
879 // If the specified file is not an absolute path, then look
880 // for it in the same directory as the config file.
881 // If no path was specified, then look for validators.txt
882 // in the same directory as the config file, but don't complain
883 // if we can't find it.
884 boost::filesystem::path validatorsFile;
885
886 if (getSingleSection(secConfig, SECTION_VALIDATORS_FILE, strTemp, j_))
887 {
888 validatorsFile = strTemp;
889
890 if (validatorsFile.empty())
891 Throw<std::runtime_error>(
892 "Invalid path specified in [" SECTION_VALIDATORS_FILE "]");
893
894 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
895 validatorsFile = CONFIG_DIR / validatorsFile;
896
897 if (!boost::filesystem::exists(validatorsFile))
898 Throw<std::runtime_error>(
899 "The file specified in [" SECTION_VALIDATORS_FILE
900 "] "
901 "does not exist: " +
902 validatorsFile.string());
903
904 else if (
905 !boost::filesystem::is_regular_file(validatorsFile) &&
906 !boost::filesystem::is_symlink(validatorsFile))
907 Throw<std::runtime_error>(
908 "Invalid file specified in [" SECTION_VALIDATORS_FILE
909 "]: " +
910 validatorsFile.string());
911 }
912 else if (!CONFIG_DIR.empty())
913 {
914 validatorsFile = CONFIG_DIR / validatorsFileName;
915
916 if (!validatorsFile.empty())
917 {
918 if (!boost::filesystem::exists(validatorsFile))
919 validatorsFile.clear();
920 else if (
921 !boost::filesystem::is_regular_file(validatorsFile) &&
922 !boost::filesystem::is_symlink(validatorsFile))
923 validatorsFile.clear();
924 }
925 }
926
927 if (!validatorsFile.empty() &&
928 boost::filesystem::exists(validatorsFile) &&
929 (boost::filesystem::is_regular_file(validatorsFile) ||
930 boost::filesystem::is_symlink(validatorsFile)))
931 {
932 boost::system::error_code ec;
933 auto const data = getFileContents(ec, validatorsFile);
934 if (ec)
935 {
936 Throw<std::runtime_error>(
937 "Failed to read '" + validatorsFile.string() + "'." +
938 std::to_string(ec.value()) + ": " + ec.message());
939 }
940
941 auto iniFile = parseIniFile(data, true);
942
943 auto entries = getIniFileSection(iniFile, SECTION_VALIDATORS);
944
945 if (entries)
946 section(SECTION_VALIDATORS).append(*entries);
947
948 auto valKeyEntries =
949 getIniFileSection(iniFile, SECTION_VALIDATOR_KEYS);
950
951 if (valKeyEntries)
952 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
953
954 auto valSiteEntries =
955 getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_SITES);
956
957 if (valSiteEntries)
958 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
959
960 auto valListKeys =
961 getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_KEYS);
962
963 if (valListKeys)
964 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
965
966 auto valListThreshold =
967 getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_THRESHOLD);
968
969 if (valListThreshold)
970 section(SECTION_VALIDATOR_LIST_THRESHOLD)
971 .append(*valListThreshold);
972
973 if (!entries && !valKeyEntries && !valListKeys)
974 Throw<std::runtime_error>(
975 "The file specified in [" SECTION_VALIDATORS_FILE
976 "] "
977 "does not contain a [" SECTION_VALIDATORS
978 "], "
979 "[" SECTION_VALIDATOR_KEYS
980 "] or "
981 "[" SECTION_VALIDATOR_LIST_KEYS
982 "]"
983 " section: " +
984 validatorsFile.string());
985 }
986
988 auto const& listThreshold =
989 section(SECTION_VALIDATOR_LIST_THRESHOLD);
990 if (listThreshold.lines().empty())
991 return std::nullopt;
992 else if (listThreshold.values().size() == 1)
993 {
994 auto strTemp = listThreshold.values()[0];
995 auto const listThreshold =
996 beast::lexicalCastThrow<std::size_t>(strTemp);
997 if (listThreshold == 0)
998 return std::nullopt; // NOTE: Explicitly ask for computed
999 else if (
1000 listThreshold >
1001 section(SECTION_VALIDATOR_LIST_KEYS).values().size())
1002 {
1003 Throw<std::runtime_error>(
1004 "Value in config section "
1005 "[" SECTION_VALIDATOR_LIST_THRESHOLD
1006 "] exceeds the number of configured list keys");
1007 }
1008 return listThreshold;
1009 }
1010 else
1011 {
1012 Throw<std::runtime_error>(
1013 "Config section "
1014 "[" SECTION_VALIDATOR_LIST_THRESHOLD
1015 "] should contain single value only");
1016 }
1017 }();
1018
1019 // Consolidate [validator_keys] and [validators]
1020 section(SECTION_VALIDATORS)
1021 .append(section(SECTION_VALIDATOR_KEYS).lines());
1022
1023 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
1024 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
1025 {
1026 Throw<std::runtime_error>(
1027 "[" + std::string(SECTION_VALIDATOR_LIST_KEYS) +
1028 "] config section is missing");
1029 }
1030 }
1031
1032 {
1033 auto const part = section("features");
1034 for (auto const& s : part.values())
1035 {
1036 if (auto const f = getRegisteredFeature(s))
1037 features.insert(*f);
1038 else
1039 Throw<std::runtime_error>(
1040 "Unknown feature: " + s + " in config file.");
1041 }
1042 }
1043
1044 // This doesn't properly belong here, but check to make sure that the
1045 // value specified for network_quorum is achievable:
1046 {
1047 auto pm = PEERS_MAX;
1048
1049 // FIXME this apparently magic value is actually defined as a constant
1050 // elsewhere (see defaultMaxPeers) but we handle this check here.
1051 if (pm == 0)
1052 pm = 21;
1053
1054 if (NETWORK_QUORUM > pm)
1055 {
1056 Throw<std::runtime_error>(
1057 "The minimum number of required peers (network_quorum) exceeds "
1058 "the maximum number of allowed peers (peers_max)");
1059 }
1060 }
1061}
1062
1063boost::filesystem::path
1065{
1066 auto log_file = DEBUG_LOGFILE;
1067
1068 if (!log_file.empty() && !log_file.is_absolute())
1069 {
1070 // Unless an absolute path for the log file is specified, the
1071 // path is relative to the config file directory.
1072 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
1073 }
1074
1075 if (!log_file.empty())
1076 {
1077 auto log_dir = log_file.parent_path();
1078
1079 if (!boost::filesystem::is_directory(log_dir))
1080 {
1081 boost::system::error_code ec;
1082 boost::filesystem::create_directories(log_dir, ec);
1083
1084 // If we fail, we warn but continue so that the calling code can
1085 // decide how to handle this situation.
1086 if (ec)
1087 {
1088 std::cerr << "Unable to create log file path " << log_dir
1089 << ": " << ec.message() << '\n';
1090 }
1091 }
1092 }
1093
1094 return log_file;
1095}
1096
1097int
1099{
1100 auto const index = static_cast<std::underlying_type_t<SizedItem>>(item);
1101 XRPL_ASSERT(
1102 index < sizedItems.size(),
1103 "xrpl::Config::getValueFor : valid index input");
1104 XRPL_ASSERT(
1105 !node || *node <= 4, "xrpl::Config::getValueFor : unset or valid node");
1106 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));
1107}
1108
1110setup_FeeVote(Section const& section)
1111{
1112 FeeSetup setup;
1113 {
1114 std::uint64_t temp;
1115 if (set(temp, "reference_fee", section) &&
1117 setup.reference_fee = temp;
1118 }
1119 {
1120 std::uint32_t temp;
1121 if (set(temp, "account_reserve", section))
1122 setup.account_reserve = temp;
1123 if (set(temp, "owner_reserve", section))
1124 setup.owner_reserve = temp;
1125 }
1126 return setup;
1127}
1128
1129} // namespace xrpl
T clamp(T... args)
A generic endpoint for log messages.
Definition Journal.h:41
Stream warn() const
Definition Journal.h:321
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 &section, 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.
uint32_t NETWORK_ID
Definition Config.h:138
std::unordered_set< uint256, beast::uhash<> > features
Definition Config.h:258
bool ELB_SUPPORT
Definition Config.h:120
bool COMPRESSION
Definition Config.h:202
static char const *const configLegacyName
Definition Config.h:71
boost::filesystem::path DEBUG_LOGFILE
Definition Config.h:86
void load()
Definition Config.cpp:438
std::optional< std::size_t > VALIDATOR_LIST_THRESHOLD
Definition Config.h:282
boost::filesystem::path CONFIG_FILE
Definition Config.h:80
bool TX_REDUCE_RELAY_ENABLE
Definition Config.h:240
static char const *const configFileName
Definition Config.h:70
int MAX_TRANSACTIONS
Definition Config.h:208
std::size_t PEERS_IN_MAX
Definition Config.h:163
int PATH_SEARCH_MAX
Definition Config.h:180
int PATH_SEARCH_OLD
Definition Config.h:177
bool BETA_RPC_API
Definition Config.h:269
std::chrono::seconds MAX_DIVERGED_TIME
Definition Config.h:266
beast::Journal const j_
Definition Config.h:90
std::vector< std::string > IPS
Definition Config.h:123
bool RUN_STANDALONE
Operate in stand-alone mode.
Definition Config.h:103
int PATH_SEARCH_FAST
Definition Config.h:179
std::string SSL_VERIFY_FILE
Definition Config.h:198
std::size_t PEERS_OUT_MAX
Definition Config.h:162
std::string SERVER_DOMAIN
Definition Config.h:260
int RELAY_UNTRUSTED_VALIDATIONS
Definition Config.h:151
bool SILENT
Definition Config.h:93
std::string SSL_VERIFY_DIR
Definition Config.h:199
void setup(std::string const &strConf, bool bQuiet, bool bSilent, bool bStandalone)
Definition Config.cpp:293
bool USE_TX_TABLES
Definition Config.h:105
static constexpr int MAX_JOB_QUEUE_TX
Definition Config.h:209
int PREFETCH_WORKERS
Definition Config.h:218
std::size_t TX_RELAY_PERCENTAGE
Definition Config.h:253
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Definition Config.cpp:461
bool SSL_VERIFY
Definition Config.h:197
boost::filesystem::path getDebugLogFile() const
Returns the full path and filename of the debug log file.
Definition Config.cpp:1064
bool QUIET
Definition Config.h:92
bool TX_REDUCE_RELAY_METRICS
Definition Config.h:247
std::chrono::seconds MAX_UNKNOWN_TIME
Definition Config.h:263
static constexpr int MIN_JOB_QUEUE_TX
Definition Config.h:210
bool PEER_PRIVATE
Definition Config.h:155
static char const *const validatorsFileName
Definition Config.h:73
std::uint32_t LEDGER_HISTORY
Definition Config.h:189
std::size_t NODE_SIZE
Definition Config.h:195
bool FAST_LOAD
Definition Config.h:272
std::size_t PEERS_MAX
Definition Config.h:161
std::uint32_t FETCH_DEPTH
Definition Config.h:190
bool signingEnabled_
Determines if the server will sign a tx, given an account's secret seed.
Definition Config.h:113
int PATH_SEARCH
Definition Config.h:178
std::optional< int > SWEEP_INTERVAL
Definition Config.h:225
void setupControl(bool bQuiet, bool bSilent, bool bStandalone)
Definition Config.cpp:251
std::size_t NETWORK_QUORUM
Definition Config.h:146
FeeSetup FEES
Definition Config.h:186
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.
Definition Config.cpp:1098
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
Definition Config.h:230
int IO_WORKERS
Definition Config.h:217
std::vector< std::string > IPS_FIXED
Definition Config.h:126
std::chrono::seconds AMENDMENT_MAJORITY_TIME
Definition Config.h:213
int RELAY_UNTRUSTED_PROPOSALS
Definition Config.h:152
std::size_t VP_REDUCE_RELAY_SQUELCH_MAX_SELECTED_PEERS
Definition Config.h:236
int WORKERS
Definition Config.h:216
std::size_t TX_REDUCE_RELAY_MIN_PEERS
Definition Config.h:250
boost::filesystem::path CONFIG_DIR
Definition Config.h:83
bool LEDGER_REPLAY
Definition Config.h:205
static char const *const databaseDirName
Definition Config.h:72
static void initializeSSLContext(std::string const &sslVerifyDir, std::string const &sslVerifyFile, bool sslVerify, beast::Journal j)
Holds a collection of configuration values.
Definition BasicConfig.h:26
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:60
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
T count(T... args)
T distance(T... args)
T emplace(T... args)
T empty(T... args)
T end(T... args)
T endl(T... args)
T find(T... args)
T getenv(T... args)
T hardware_concurrency(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
Definition chrono.h:24
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
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)
Definition Config.cpp:234
bool getSingleSection(IniFileSections &secSource, std::string const &strSection, std::string &strValue, beast::Journal j)
Definition Config.cpp:199
IniFileSections::mapped_type * getIniFileSection(IniFileSections &secSource, std::string const &strSection)
Definition Config.cpp:190
IniFileSections parseIniFile(std::string const &strInput, bool const bTrim)
Definition Config.cpp:142
FeeSetup setup_FeeVote(Section const &section)
Definition Config.cpp:1110
SizedItem
Definition Config.h:25
constexpr std::array< std::pair< SizedItem, std::array< int, 5 > >, 13 > sizedItems
Definition Config.cpp:95
std::unordered_map< std::string, std::vector< std::string > > IniFileSections
Definition BasicConfig.h:18
static std::string const & systemName()
bool get_if_exists(Section const &section, std::string const &name, T &v)
static void checkZeroPorts(Config const &config)
Definition Config.cpp:411
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > days
Definition chrono.h:21
std::optional< uint256 > getRegisteredFeature(std::string const &name)
Definition Feature.cpp:363
T regex_replace(T... args)
T size(T... args)
T str(T... args)
static std::string nodeDatabase()
Fee schedule for startup / standalone, and to vote for.
Definition Config.h:47
XRPAmount reference_fee
The cost of a reference transaction in drops.
Definition Config.h:49
XRPAmount account_reserve
The account reserve requirement in drops.
Definition Config.h:52
XRPAmount owner_reserve
The per-owned item reserve requirement in drops.
Definition Config.h:55
T substr(T... args)
T to_string(T... args)
T value_or(T... args)