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
199getSingleSection(IniFileSections& secSource, std::string const& strSection, std::string& strValue, beast::Journal j)
200{
201 auto const pmtEntries = getIniFileSection(secSource, strSection);
202
203 if (pmtEntries && pmtEntries->size() == 1)
204 {
205 strValue = (*pmtEntries)[0];
206 return true;
207 }
208
209 if (pmtEntries)
210 {
211 JLOG(j.warn()) << "Section '" << strSection << "': requires 1 line not " << pmtEntries->size() << " lines.";
212 }
213
214 return false;
215}
216
217//------------------------------------------------------------------------------
218//
219// Config
220//
221//------------------------------------------------------------------------------
222
223char const* const Config::configFileName = "xrpld.cfg";
224char const* const Config::configLegacyName = "rippled.cfg";
225char const* const Config::databaseDirName = "db";
226char const* const Config::validatorsFileName = "validators.txt";
227
228[[nodiscard]] static std::string
229getEnvVar(char const* name)
230{
231 std::string value;
232
233 if (auto const v = std::getenv(name); v != nullptr)
234 value = v;
235
236 return value;
237}
238
239Config::Config() : j_(beast::Journal::getNullSink()), ramSize_(detail::getMemorySize() / (1024 * 1024 * 1024))
240{
241}
242
243void
244Config::setupControl(bool bQuiet, bool bSilent, bool bStandalone)
245{
246 XRPL_ASSERT(NODE_SIZE == 0, "xrpl::Config::setupControl : node size not set");
247
248 QUIET = bQuiet || bSilent;
249 SILENT = bSilent;
250 RUN_STANDALONE = bStandalone;
251
252 // We try to autodetect the appropriate node size by checking available
253 // RAM and CPU resources. We default to "tiny" for standalone mode.
254 if (!bStandalone)
255 {
256 // First, check against 'minimum' RAM requirements per node size:
258
259 auto ns = std::find_if(threshold.second.begin(), threshold.second.end(), [this](std::size_t limit) {
260 return (limit == 0) || (ramSize_ < limit);
261 });
262
263 XRPL_ASSERT(ns != threshold.second.end(), "xrpl::Config::setupControl : valid node size");
264
265 if (ns != threshold.second.end())
266 NODE_SIZE = std::distance(threshold.second.begin(), ns);
267
268 // Adjust the size based on the number of hardware threads of
269 // execution available to us:
270 if (auto const hc = std::thread::hardware_concurrency(); hc != 0)
272 }
273
274 XRPL_ASSERT(NODE_SIZE <= 4, "xrpl::Config::setupControl : node size is set");
275}
276
277void
278Config::setup(std::string const& strConf, bool bQuiet, bool bSilent, bool bStandalone)
279{
280 setupControl(bQuiet, bSilent, bStandalone);
281
282 // Determine the config and data directories.
283 // If the config file is found in the current working
284 // directory, use the current working directory as the
285 // config directory and that with "db" as the data
286 // directory.
287 boost::filesystem::path dataDir;
288
289 if (!strConf.empty())
290 {
291 // --conf=<path> : everything is relative that file.
292 CONFIG_FILE = strConf;
293 CONFIG_DIR = boost::filesystem::absolute(CONFIG_FILE);
294 CONFIG_DIR.remove_filename();
295 dataDir = CONFIG_DIR / databaseDirName;
296 }
297 else
298 {
299 do
300 {
301 // Check if either of the config files exist in the current working
302 // directory, in which case the databases will be stored in a
303 // subdirectory.
304 CONFIG_DIR = boost::filesystem::current_path();
305 dataDir = CONFIG_DIR / databaseDirName;
307 if (boost::filesystem::exists(CONFIG_FILE))
308 break;
310 if (boost::filesystem::exists(CONFIG_FILE))
311 break;
312
313 // Check if the home directory is set, and optionally the XDG config
314 // and/or data directories, as the config may be there. See
315 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html.
316 auto const strHome = getEnvVar("HOME");
317 if (!strHome.empty())
318 {
319 auto strXdgConfigHome = getEnvVar("XDG_CONFIG_HOME");
320 auto strXdgDataHome = getEnvVar("XDG_DATA_HOME");
321 if (strXdgConfigHome.empty())
322 {
323 // $XDG_CONFIG_HOME was not set, use default based on $HOME.
324 strXdgConfigHome = strHome + "/.config";
325 }
326 if (strXdgDataHome.empty())
327 {
328 // $XDG_DATA_HOME was not set, use default based on $HOME.
329 strXdgDataHome = strHome + "/.local/share";
330 }
331
332 // Check if either of the config files exist in the XDG config
333 // dir.
334 dataDir = strXdgDataHome + "/" + systemName();
335 CONFIG_DIR = strXdgConfigHome + "/" + systemName();
337 if (boost::filesystem::exists(CONFIG_FILE))
338 break;
340 if (boost::filesystem::exists(CONFIG_FILE))
341 break;
342 }
343
344 // As a last resort, check the system config directory.
345 dataDir = "/var/opt/" + systemName();
346 CONFIG_DIR = "/etc/opt/" + systemName();
348 if (boost::filesystem::exists(CONFIG_FILE))
349 break;
351 } while (false);
352 }
353
354 // Update default values
355 load();
356 {
357 // load() may have set a new value for the dataDir
358 std::string const dbPath(legacy("database_path"));
359 if (!dbPath.empty())
360 dataDir = boost::filesystem::path(dbPath);
361 }
362
363 if (!RUN_STANDALONE)
364 {
365 boost::system::error_code ec;
366 boost::filesystem::create_directories(dataDir, ec);
367
368 if (ec)
369 Throw<std::runtime_error>(boost::str(boost::format("Can not create %s") % dataDir));
370
371 legacy("database_path", boost::filesystem::absolute(dataDir).string());
372 }
373
375
376 if (RUN_STANDALONE)
377 LEDGER_HISTORY = 0;
378
379 std::string ledgerTxDbType;
380 Section ledgerTxTablesSection = section("ledger_tx_tables");
381 get_if_exists(ledgerTxTablesSection, "use_tx_tables", USE_TX_TABLES);
382
384 get_if_exists(nodeDbSection, "fast_load", FAST_LOAD);
385}
386
387// 0 ports are allowed for unit tests, but still not allowed to be present in
388// config file
389static void
390checkZeroPorts(Config const& config)
391{
392 if (!config.exists("server"))
393 return;
394
395 for (auto const& name : config.section("server").values())
396 {
397 if (!config.exists(name))
398 return;
399
400 auto const& section = config[name];
401 auto const optResult = section.get("port");
402 if (optResult)
403 {
404 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
405 if (!port)
406 {
408 ss << "Invalid value '" << *optResult << "' for key 'port' in [" << name << "]";
409 Throw<std::runtime_error>(ss.str());
410 }
411 }
412 }
413}
414
415void
417{
418 // NOTE: this writes to cerr because we want cout to be reserved
419 // for the writing of the json response (so that stdout can be part of a
420 // pipeline, for instance)
421 if (!QUIET)
422 std::cerr << "Loading: " << CONFIG_FILE << "\n";
423
424 boost::system::error_code ec;
425 auto const fileContents = getFileContents(ec, CONFIG_FILE);
426
427 if (ec)
428 {
429 std::cerr << "Failed to read '" << CONFIG_FILE << "'." << ec.value() << ": " << ec.message() << std::endl;
430 return;
431 }
432
433 loadFromString(fileContents);
434 checkZeroPorts(*this);
435}
436
437void
439{
440 IniFileSections secConfig = parseIniFile(fileContents, true);
441
442 build(secConfig);
443
444 if (auto s = getIniFileSection(secConfig, SECTION_IPS))
445 IPS = *s;
446
447 if (auto s = getIniFileSection(secConfig, SECTION_IPS_FIXED))
448 IPS_FIXED = *s;
449
450 // if the user has specified ip:port then replace : with a space.
451 {
452 auto replaceColons = [](std::vector<std::string>& strVec) {
453 static std::regex const e(":([0-9]+)$");
454 for (auto& line : strVec)
455 {
456 // skip anything that might be an ipv6 address
457 if (std::count(line.begin(), line.end(), ':') != 1)
458 continue;
459
460 std::string result = std::regex_replace(line, e, " $1");
461 // sanity check the result of the replace, should be same length
462 // as input
463 if (result.size() == line.size())
464 line = result;
465 }
466 };
467
468 replaceColons(IPS_FIXED);
469 replaceColons(IPS);
470 }
471
472 {
473 std::string dbPath;
474 if (getSingleSection(secConfig, "database_path", dbPath, j_))
475 {
476 boost::filesystem::path p(dbPath);
477 legacy("database_path", boost::filesystem::absolute(p).string());
478 }
479 }
480
481 std::string strTemp;
482
483 if (getSingleSection(secConfig, SECTION_NETWORK_ID, strTemp, j_))
484 {
485 if (strTemp == "main")
486 NETWORK_ID = 0;
487 else if (strTemp == "testnet")
488 NETWORK_ID = 1;
489 else if (strTemp == "devnet")
490 NETWORK_ID = 2;
491 else
492 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
493 }
494
495 if (getSingleSection(secConfig, SECTION_PEER_PRIVATE, strTemp, j_))
496 PEER_PRIVATE = beast::lexicalCastThrow<bool>(strTemp);
497
498 if (getSingleSection(secConfig, SECTION_PEERS_MAX, strTemp, j_))
499 {
500 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
501 }
502 else
503 {
504 std::optional<std::size_t> peers_in_max{};
505 if (getSingleSection(secConfig, SECTION_PEERS_IN_MAX, strTemp, j_))
506 {
507 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
508 if (*peers_in_max > 1000)
509 Throw<std::runtime_error>("Invalid value specified in [" SECTION_PEERS_IN_MAX
510 "] section; the value must be less or equal than 1000");
511 }
512
513 std::optional<std::size_t> peers_out_max{};
514 if (getSingleSection(secConfig, SECTION_PEERS_OUT_MAX, strTemp, j_))
515 {
516 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
517 if (*peers_out_max < 10 || *peers_out_max > 1000)
518 Throw<std::runtime_error>("Invalid value specified in [" SECTION_PEERS_OUT_MAX
519 "] section; the value must be in range 10-1000");
520 }
521
522 // if one section is configured then the other must be configured too
523 if ((peers_in_max && !peers_out_max) || (peers_out_max && !peers_in_max))
524 Throw<std::runtime_error>("Both sections [" SECTION_PEERS_IN_MAX
525 "]"
526 "and [" SECTION_PEERS_OUT_MAX "] must be configured");
527
528 if (peers_in_max && peers_out_max)
529 {
530 PEERS_IN_MAX = *peers_in_max;
531 PEERS_OUT_MAX = *peers_out_max;
532 }
533 }
534
535 if (getSingleSection(secConfig, SECTION_NODE_SIZE, strTemp, j_))
536 {
537 if (boost::iequals(strTemp, "tiny"))
538 NODE_SIZE = 0;
539 else if (boost::iequals(strTemp, "small"))
540 NODE_SIZE = 1;
541 else if (boost::iequals(strTemp, "medium"))
542 NODE_SIZE = 2;
543 else if (boost::iequals(strTemp, "large"))
544 NODE_SIZE = 3;
545 else if (boost::iequals(strTemp, "huge"))
546 NODE_SIZE = 4;
547 else
548 NODE_SIZE = std::min<std::size_t>(4, beast::lexicalCastThrow<std::size_t>(strTemp));
549 }
550
551 if (getSingleSection(secConfig, SECTION_SIGNING_SUPPORT, strTemp, j_))
552 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
553
554 if (getSingleSection(secConfig, SECTION_ELB_SUPPORT, strTemp, j_))
555 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
556
557 getSingleSection(secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_);
558 getSingleSection(secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_);
559
560 if (getSingleSection(secConfig, SECTION_SSL_VERIFY, strTemp, j_))
561 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
562
563 if (getSingleSection(secConfig, SECTION_RELAY_VALIDATIONS, strTemp, j_))
564 {
565 if (boost::iequals(strTemp, "all"))
567 else if (boost::iequals(strTemp, "trusted"))
569 else if (boost::iequals(strTemp, "drop_untrusted"))
571 else
572 Throw<std::runtime_error>("Invalid value specified in [" SECTION_RELAY_VALIDATIONS "] section");
573 }
574
575 if (getSingleSection(secConfig, SECTION_RELAY_PROPOSALS, strTemp, j_))
576 {
577 if (boost::iequals(strTemp, "all"))
579 else if (boost::iequals(strTemp, "trusted"))
581 else if (boost::iequals(strTemp, "drop_untrusted"))
583 else
584 Throw<std::runtime_error>("Invalid value specified in [" SECTION_RELAY_PROPOSALS "] section");
585 }
586
587 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
588 Throw<std::runtime_error>("Cannot have both [" SECTION_VALIDATION_SEED "] and [" SECTION_VALIDATOR_TOKEN
589 "] config sections");
590
591 if (getSingleSection(secConfig, SECTION_NETWORK_QUORUM, strTemp, j_))
592 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
593
594 FEES = setup_FeeVote(section("voting"));
595 /* [fee_default] is documented in the example config files as useful for
596 * things like offline transaction signing. Until that's completely
597 * deprecated, allow it to override the [voting] section. */
598 if (getSingleSection(secConfig, SECTION_FEE_DEFAULT, strTemp, j_))
599 FEES.reference_fee = beast::lexicalCastThrow<std::uint64_t>(strTemp);
600
601 if (getSingleSection(secConfig, SECTION_LEDGER_HISTORY, strTemp, j_))
602 {
603 if (boost::iequals(strTemp, "full"))
605 else if (boost::iequals(strTemp, "none"))
606 LEDGER_HISTORY = 0;
607 else
608 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
609 }
610
611 if (getSingleSection(secConfig, SECTION_FETCH_DEPTH, strTemp, j_))
612 {
613 if (boost::iequals(strTemp, "none"))
614 FETCH_DEPTH = 0;
615 else if (boost::iequals(strTemp, "full"))
616 FETCH_DEPTH = std::numeric_limits<decltype(FETCH_DEPTH)>::max();
617 else
618 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
619
620 if (FETCH_DEPTH < 10)
621 FETCH_DEPTH = 10;
622 }
623
624 // By default, validators don't have pathfinding enabled, unless it is
625 // explicitly requested by the server's admin.
626 if (exists(SECTION_VALIDATION_SEED) || exists(SECTION_VALIDATOR_TOKEN))
627 PATH_SEARCH_MAX = 0;
628
629 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_OLD, strTemp, j_))
630 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
631 if (getSingleSection(secConfig, SECTION_PATH_SEARCH, strTemp, j_))
632 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
633 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_FAST, strTemp, j_))
634 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
635 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_MAX, strTemp, j_))
636 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
637
638 if (getSingleSection(secConfig, SECTION_DEBUG_LOGFILE, strTemp, j_))
639 DEBUG_LOGFILE = strTemp;
640
641 if (getSingleSection(secConfig, SECTION_SWEEP_INTERVAL, strTemp, j_))
642 {
643 SWEEP_INTERVAL = beast::lexicalCastThrow<std::size_t>(strTemp);
644
645 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
646 Throw<std::runtime_error>("Invalid " SECTION_SWEEP_INTERVAL ": must be between 10 and 600 inclusive");
647 }
648
649 if (getSingleSection(secConfig, SECTION_WORKERS, strTemp, j_))
650 {
651 WORKERS = beast::lexicalCastThrow<int>(strTemp);
652
653 if (WORKERS < 1 || WORKERS > 1024)
654 Throw<std::runtime_error>("Invalid " SECTION_WORKERS ": must be between 1 and 1024 inclusive.");
655 }
656
657 if (getSingleSection(secConfig, SECTION_IO_WORKERS, strTemp, j_))
658 {
659 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
660
661 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
662 Throw<std::runtime_error>("Invalid " SECTION_IO_WORKERS ": must be between 1 and 1024 inclusive.");
663 }
664
665 if (getSingleSection(secConfig, SECTION_PREFETCH_WORKERS, strTemp, j_))
666 {
667 PREFETCH_WORKERS = beast::lexicalCastThrow<int>(strTemp);
668
669 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
670 Throw<std::runtime_error>("Invalid " SECTION_PREFETCH_WORKERS ": must be between 1 and 1024 inclusive.");
671 }
672
673 if (getSingleSection(secConfig, SECTION_COMPRESSION, strTemp, j_))
674 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
675
676 if (getSingleSection(secConfig, SECTION_LEDGER_REPLAY, strTemp, j_))
677 LEDGER_REPLAY = beast::lexicalCastThrow<bool>(strTemp);
678
679 if (exists(SECTION_REDUCE_RELAY))
680 {
681 auto sec = section(SECTION_REDUCE_RELAY);
682
684 // vp_enable config option is deprecated by vp_base_squelch_enable //
685 // This option is kept for backwards compatibility. When squelching //
686 // is the default algorithm, it must be replaced with: //
687 // VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = //
688 // sec.value_or("vp_base_squelch_enable", true); //
689 if (sec.exists("vp_base_squelch_enable") && sec.exists("vp_enable"))
690 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
691 " cannot specify both vp_base_squelch_enable and vp_enable "
692 "options. "
693 "vp_enable was deprecated and replaced by "
694 "vp_base_squelch_enable");
695
696 if (sec.exists("vp_base_squelch_enable"))
697 VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = sec.value_or("vp_base_squelch_enable", false);
698 else if (sec.exists("vp_enable"))
699 VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = sec.value_or("vp_enable", false);
700 else
703
705 // Temporary squelching config for the peers selected as a source of //
706 // validator messages. The config must be removed once squelching is //
707 // made the default routing algorithm. //
708 VP_REDUCE_RELAY_SQUELCH_MAX_SELECTED_PEERS = sec.value_or("vp_base_squelch_max_selected_peers", 5);
710 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
711 " vp_base_squelch_max_selected_peers must be "
712 "greater than or equal to 3");
714
715 TX_REDUCE_RELAY_ENABLE = sec.value_or("tx_enable", false);
716 TX_REDUCE_RELAY_METRICS = sec.value_or("tx_metrics", false);
717 TX_REDUCE_RELAY_MIN_PEERS = sec.value_or("tx_min_peers", 20);
718 TX_RELAY_PERCENTAGE = sec.value_or("tx_relay_percentage", 25);
719 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 || TX_REDUCE_RELAY_MIN_PEERS < 10)
720 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
721 ", tx_min_peers must be greater than or equal to 10"
722 ", tx_relay_percentage must be greater than or equal to 10 "
723 "and less than or equal to 100");
724 }
725
726 if (getSingleSection(secConfig, SECTION_MAX_TRANSACTIONS, strTemp, j_))
727 {
728 MAX_TRANSACTIONS = std::clamp(beast::lexicalCastThrow<int>(strTemp), MIN_JOB_QUEUE_TX, MAX_JOB_QUEUE_TX);
729 }
730
731 if (getSingleSection(secConfig, SECTION_SERVER_DOMAIN, strTemp, j_))
732 {
733 if (!isProperlyFormedTomlDomain(strTemp))
734 {
735 Throw<std::runtime_error>("Invalid " SECTION_SERVER_DOMAIN
736 ": the domain name does not appear to meet the requirements.");
737 }
738
739 SERVER_DOMAIN = strTemp;
740 }
741
742 if (exists(SECTION_OVERLAY))
743 {
744 auto const sec = section(SECTION_OVERLAY);
745
746 using namespace std::chrono;
747
748 try
749 {
750 if (auto val = sec.get("max_unknown_time"))
751 MAX_UNKNOWN_TIME = seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
752 }
753 catch (...)
754 {
755 Throw<std::runtime_error>("Invalid value 'max_unknown_time' in " SECTION_OVERLAY
756 ": must be of the form '<number>' representing seconds.");
757 }
758
759 if (MAX_UNKNOWN_TIME < seconds{300} || MAX_UNKNOWN_TIME > seconds{1800})
760 Throw<std::runtime_error>("Invalid value 'max_unknown_time' in " SECTION_OVERLAY
761 ": the time must be between 300 and 1800 seconds, inclusive.");
762
763 try
764 {
765 if (auto val = sec.get("max_diverged_time"))
766 MAX_DIVERGED_TIME = seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
767 }
768 catch (...)
769 {
770 Throw<std::runtime_error>("Invalid value 'max_diverged_time' in " SECTION_OVERLAY
771 ": must be of the form '<number>' representing seconds.");
772 }
773
775 {
776 Throw<std::runtime_error>("Invalid value 'max_diverged_time' in " SECTION_OVERLAY
777 ": the time must be between 60 and 900 seconds, inclusive.");
778 }
779 }
780
781 if (getSingleSection(secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
782 {
783 using namespace std::chrono;
784 boost::regex const re("^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
785 boost::smatch match;
786 if (!boost::regex_match(strTemp, match, re))
787 Throw<std::runtime_error>("Invalid " SECTION_AMENDMENT_MAJORITY_TIME
788 ", must be: [0-9]+ [minutes|hours|days|weeks]");
789
790 std::uint32_t duration = beast::lexicalCastThrow<std::uint32_t>(match[1].str());
791
792 if (boost::iequals(match[2], "minutes"))
794 else if (boost::iequals(match[2], "hours"))
796 else if (boost::iequals(match[2], "days"))
798 else if (boost::iequals(match[2], "weeks"))
800
802 Throw<std::runtime_error>("Invalid " SECTION_AMENDMENT_MAJORITY_TIME
803 ", the minimum amount of time an amendment must hold a "
804 "majority is 15 minutes");
805 }
806
807 if (getSingleSection(secConfig, SECTION_BETA_RPC_API, strTemp, j_))
808 BETA_RPC_API = beast::lexicalCastThrow<bool>(strTemp);
809
810 // Do not load trusted validator configuration for standalone mode
811 if (!RUN_STANDALONE)
812 {
813 // If a file was explicitly specified, then throw if the
814 // path is malformed or if the file does not exist or is
815 // not a file.
816 // If the specified file is not an absolute path, then look
817 // for it in the same directory as the config file.
818 // If no path was specified, then look for validators.txt
819 // in the same directory as the config file, but don't complain
820 // if we can't find it.
821 boost::filesystem::path validatorsFile;
822
823 if (getSingleSection(secConfig, SECTION_VALIDATORS_FILE, strTemp, j_))
824 {
825 validatorsFile = strTemp;
826
827 if (validatorsFile.empty())
828 Throw<std::runtime_error>("Invalid path specified in [" SECTION_VALIDATORS_FILE "]");
829
830 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
831 validatorsFile = CONFIG_DIR / validatorsFile;
832
833 if (!boost::filesystem::exists(validatorsFile))
834 Throw<std::runtime_error>(
835 "The file specified in [" SECTION_VALIDATORS_FILE
836 "] "
837 "does not exist: " +
838 validatorsFile.string());
839
840 else if (
841 !boost::filesystem::is_regular_file(validatorsFile) && !boost::filesystem::is_symlink(validatorsFile))
842 Throw<std::runtime_error>(
843 "Invalid file specified in [" SECTION_VALIDATORS_FILE "]: " + validatorsFile.string());
844 }
845 else if (!CONFIG_DIR.empty())
846 {
847 validatorsFile = CONFIG_DIR / validatorsFileName;
848
849 if (!validatorsFile.empty())
850 {
851 if (!boost::filesystem::exists(validatorsFile))
852 validatorsFile.clear();
853 else if (
854 !boost::filesystem::is_regular_file(validatorsFile) &&
855 !boost::filesystem::is_symlink(validatorsFile))
856 validatorsFile.clear();
857 }
858 }
859
860 if (!validatorsFile.empty() && boost::filesystem::exists(validatorsFile) &&
861 (boost::filesystem::is_regular_file(validatorsFile) || boost::filesystem::is_symlink(validatorsFile)))
862 {
863 boost::system::error_code ec;
864 auto const data = getFileContents(ec, validatorsFile);
865 if (ec)
866 {
867 Throw<std::runtime_error>(
868 "Failed to read '" + validatorsFile.string() + "'." + std::to_string(ec.value()) + ": " +
869 ec.message());
870 }
871
872 auto iniFile = parseIniFile(data, true);
873
874 auto entries = getIniFileSection(iniFile, SECTION_VALIDATORS);
875
876 if (entries)
877 section(SECTION_VALIDATORS).append(*entries);
878
879 auto valKeyEntries = getIniFileSection(iniFile, SECTION_VALIDATOR_KEYS);
880
881 if (valKeyEntries)
882 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
883
884 auto valSiteEntries = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_SITES);
885
886 if (valSiteEntries)
887 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
888
889 auto valListKeys = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_KEYS);
890
891 if (valListKeys)
892 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
893
894 auto valListThreshold = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_THRESHOLD);
895
896 if (valListThreshold)
897 section(SECTION_VALIDATOR_LIST_THRESHOLD).append(*valListThreshold);
898
899 if (!entries && !valKeyEntries && !valListKeys)
900 Throw<std::runtime_error>(
901 "The file specified in [" SECTION_VALIDATORS_FILE
902 "] "
903 "does not contain a [" SECTION_VALIDATORS
904 "], "
905 "[" SECTION_VALIDATOR_KEYS
906 "] or "
907 "[" SECTION_VALIDATOR_LIST_KEYS
908 "]"
909 " section: " +
910 validatorsFile.string());
911 }
912
914 auto const& listThreshold = section(SECTION_VALIDATOR_LIST_THRESHOLD);
915 if (listThreshold.lines().empty())
916 return std::nullopt;
917 else if (listThreshold.values().size() == 1)
918 {
919 auto strTemp = listThreshold.values()[0];
920 auto const listThreshold = beast::lexicalCastThrow<std::size_t>(strTemp);
921 if (listThreshold == 0)
922 return std::nullopt; // NOTE: Explicitly ask for computed
923 else if (listThreshold > section(SECTION_VALIDATOR_LIST_KEYS).values().size())
924 {
925 Throw<std::runtime_error>(
926 "Value in config section "
927 "[" SECTION_VALIDATOR_LIST_THRESHOLD "] exceeds the number of configured list keys");
928 }
929 return listThreshold;
930 }
931 else
932 {
933 Throw<std::runtime_error>(
934 "Config section "
935 "[" SECTION_VALIDATOR_LIST_THRESHOLD "] should contain single value only");
936 }
937 }();
938
939 // Consolidate [validator_keys] and [validators]
940 section(SECTION_VALIDATORS).append(section(SECTION_VALIDATOR_KEYS).lines());
941
942 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
943 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
944 {
945 Throw<std::runtime_error>("[" + std::string(SECTION_VALIDATOR_LIST_KEYS) + "] config section is missing");
946 }
947 }
948
949 {
950 auto const part = section("features");
951 for (auto const& s : part.values())
952 {
953 if (auto const f = getRegisteredFeature(s))
954 features.insert(*f);
955 else
956 Throw<std::runtime_error>("Unknown feature: " + s + " in config file.");
957 }
958 }
959
960 // This doesn't properly belong here, but check to make sure that the
961 // value specified for network_quorum is achievable:
962 {
963 auto pm = PEERS_MAX;
964
965 // FIXME this apparently magic value is actually defined as a constant
966 // elsewhere (see defaultMaxPeers) but we handle this check here.
967 if (pm == 0)
968 pm = 21;
969
970 if (NETWORK_QUORUM > pm)
971 {
972 Throw<std::runtime_error>(
973 "The minimum number of required peers (network_quorum) exceeds "
974 "the maximum number of allowed peers (peers_max)");
975 }
976 }
977}
978
979boost::filesystem::path
981{
982 auto log_file = DEBUG_LOGFILE;
983
984 if (!log_file.empty() && !log_file.is_absolute())
985 {
986 // Unless an absolute path for the log file is specified, the
987 // path is relative to the config file directory.
988 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
989 }
990
991 if (!log_file.empty())
992 {
993 auto log_dir = log_file.parent_path();
994
995 if (!boost::filesystem::is_directory(log_dir))
996 {
997 boost::system::error_code ec;
998 boost::filesystem::create_directories(log_dir, ec);
999
1000 // If we fail, we warn but continue so that the calling code can
1001 // decide how to handle this situation.
1002 if (ec)
1003 {
1004 std::cerr << "Unable to create log file path " << log_dir << ": " << ec.message() << '\n';
1005 }
1006 }
1007 }
1008
1009 return log_file;
1010}
1011
1012int
1014{
1015 auto const index = static_cast<std::underlying_type_t<SizedItem>>(item);
1016 XRPL_ASSERT(index < sizedItems.size(), "xrpl::Config::getValueFor : valid index input");
1017 XRPL_ASSERT(!node || *node <= 4, "xrpl::Config::getValueFor : unset or valid node");
1018 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));
1019}
1020
1022setup_FeeVote(Section const& section)
1023{
1024 FeeSetup setup;
1025 {
1026 std::uint64_t temp;
1027 if (set(temp, "reference_fee", section) && temp <= std::numeric_limits<XRPAmount::value_type>::max())
1028 setup.reference_fee = temp;
1029 }
1030 {
1031 std::uint32_t temp;
1032 if (set(temp, "account_reserve", section))
1033 setup.account_reserve = temp;
1034 if (set(temp, "owner_reserve", section))
1035 setup.owner_reserve = temp;
1036 }
1037 return setup;
1038}
1039
1040} // namespace xrpl
T clamp(T... args)
A generic endpoint for log messages.
Definition Journal.h:41
Stream warn() const
Definition Journal.h:313
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:257
bool ELB_SUPPORT
Definition Config.h:120
bool COMPRESSION
Definition Config.h:201
static char const *const configLegacyName
Definition Config.h:71
boost::filesystem::path DEBUG_LOGFILE
Definition Config.h:86
void load()
Definition Config.cpp:416
std::optional< std::size_t > VALIDATOR_LIST_THRESHOLD
Definition Config.h:280
boost::filesystem::path CONFIG_FILE
Definition Config.h:80
bool TX_REDUCE_RELAY_ENABLE
Definition Config.h:239
static char const *const configFileName
Definition Config.h:70
int MAX_TRANSACTIONS
Definition Config.h:207
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:268
std::chrono::seconds MAX_DIVERGED_TIME
Definition Config.h:265
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:197
std::size_t PEERS_OUT_MAX
Definition Config.h:162
std::string SERVER_DOMAIN
Definition Config.h:259
int RELAY_UNTRUSTED_VALIDATIONS
Definition Config.h:151
bool SILENT
Definition Config.h:93
std::string SSL_VERIFY_DIR
Definition Config.h:198
void setup(std::string const &strConf, bool bQuiet, bool bSilent, bool bStandalone)
Definition Config.cpp:278
bool USE_TX_TABLES
Definition Config.h:105
static constexpr int MAX_JOB_QUEUE_TX
Definition Config.h:208
int PREFETCH_WORKERS
Definition Config.h:217
std::size_t TX_RELAY_PERCENTAGE
Definition Config.h:252
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Definition Config.cpp:438
bool SSL_VERIFY
Definition Config.h:196
boost::filesystem::path getDebugLogFile() const
Returns the full path and filename of the debug log file.
Definition Config.cpp:980
bool QUIET
Definition Config.h:92
bool TX_REDUCE_RELAY_METRICS
Definition Config.h:246
std::chrono::seconds MAX_UNKNOWN_TIME
Definition Config.h:262
static constexpr int MIN_JOB_QUEUE_TX
Definition Config.h:209
bool PEER_PRIVATE
Definition Config.h:155
static char const *const validatorsFileName
Definition Config.h:73
std::uint32_t LEDGER_HISTORY
Definition Config.h:188
std::size_t NODE_SIZE
Definition Config.h:194
bool FAST_LOAD
Definition Config.h:271
std::size_t PEERS_MAX
Definition Config.h:161
std::uint32_t FETCH_DEPTH
Definition Config.h:189
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:224
void setupControl(bool bQuiet, bool bSilent, bool bStandalone)
Definition Config.cpp:244
std::size_t NETWORK_QUORUM
Definition Config.h:146
FeeSetup FEES
Definition Config.h:185
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:1013
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
Definition Config.h:229
int IO_WORKERS
Definition Config.h:216
std::vector< std::string > IPS_FIXED
Definition Config.h:126
std::chrono::seconds AMENDMENT_MAJORITY_TIME
Definition Config.h:212
int RELAY_UNTRUSTED_PROPOSALS
Definition Config.h:152
std::size_t VP_REDUCE_RELAY_SQUELCH_MAX_SELECTED_PEERS
Definition Config.h:235
int WORKERS
Definition Config.h:215
std::size_t TX_REDUCE_RELAY_MIN_PEERS
Definition Config.h:249
boost::filesystem::path CONFIG_DIR
Definition Config.h:83
bool LEDGER_REPLAY
Definition Config.h:204
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:25
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:59
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::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:229
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:1022
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
Definition chrono.h:21
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:17
static std::string const & systemName()
std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 24 > > > days
Definition chrono.h:19
bool get_if_exists(Section const &section, std::string const &name, T &v)
static void checkZeroPorts(Config const &config)
Definition Config.cpp:390
std::optional< uint256 > getRegisteredFeature(std::string const &name)
Definition Feature.cpp:336
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)