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 else if (RUN_STANDALONE)
362 dataDir.clear();
363 }
364
365 if (!dataDir.empty())
366 {
367 boost::system::error_code ec;
368 boost::filesystem::create_directories(dataDir, ec);
369
370 if (ec)
371 Throw<std::runtime_error>(boost::str(boost::format("Can not create %s") % dataDir));
372
373 legacy("database_path", boost::filesystem::absolute(dataDir).string());
374 }
375
377
378 if (RUN_STANDALONE)
379 LEDGER_HISTORY = 0;
380
381 std::string ledgerTxDbType;
382 Section ledgerTxTablesSection = section("ledger_tx_tables");
383 get_if_exists(ledgerTxTablesSection, "use_tx_tables", USE_TX_TABLES);
384
386 get_if_exists(nodeDbSection, "fast_load", FAST_LOAD);
387}
388
389// 0 ports are allowed for unit tests, but still not allowed to be present in
390// config file
391static void
392checkZeroPorts(Config const& config)
393{
394 if (!config.exists("server"))
395 return;
396
397 for (auto const& name : config.section("server").values())
398 {
399 if (!config.exists(name))
400 return;
401
402 auto const& section = config[name];
403 auto const optResult = section.get("port");
404 if (optResult)
405 {
406 auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
407 if (!port)
408 {
410 ss << "Invalid value '" << *optResult << "' for key 'port' in [" << name << "]";
411 Throw<std::runtime_error>(ss.str());
412 }
413 }
414 }
415}
416
417void
419{
420 // NOTE: this writes to cerr because we want cout to be reserved
421 // for the writing of the json response (so that stdout can be part of a
422 // pipeline, for instance)
423 if (!QUIET)
424 std::cerr << "Loading: " << CONFIG_FILE << "\n";
425
426 boost::system::error_code ec;
427 auto const fileContents = getFileContents(ec, CONFIG_FILE);
428
429 if (ec)
430 {
431 std::cerr << "Failed to read '" << CONFIG_FILE << "'." << ec.value() << ": " << ec.message() << std::endl;
432 return;
433 }
434
435 loadFromString(fileContents);
436 checkZeroPorts(*this);
437}
438
439void
441{
442 IniFileSections secConfig = parseIniFile(fileContents, true);
443
444 build(secConfig);
445
446 if (auto s = getIniFileSection(secConfig, SECTION_IPS))
447 IPS = *s;
448
449 if (auto s = getIniFileSection(secConfig, SECTION_IPS_FIXED))
450 IPS_FIXED = *s;
451
452 // if the user has specified ip:port then replace : with a space.
453 {
454 auto replaceColons = [](std::vector<std::string>& strVec) {
455 static std::regex const e(":([0-9]+)$");
456 for (auto& line : strVec)
457 {
458 // skip anything that might be an ipv6 address
459 if (std::count(line.begin(), line.end(), ':') != 1)
460 continue;
461
462 std::string result = std::regex_replace(line, e, " $1");
463 // sanity check the result of the replace, should be same length
464 // as input
465 if (result.size() == line.size())
466 line = result;
467 }
468 };
469
470 replaceColons(IPS_FIXED);
471 replaceColons(IPS);
472 }
473
474 {
475 std::string dbPath;
476 if (getSingleSection(secConfig, "database_path", dbPath, j_))
477 {
478 boost::filesystem::path p(dbPath);
479 legacy("database_path", boost::filesystem::absolute(p).string());
480 }
481 }
482
483 std::string strTemp;
484
485 if (getSingleSection(secConfig, SECTION_NETWORK_ID, strTemp, j_))
486 {
487 if (strTemp == "main")
488 NETWORK_ID = 0;
489 else if (strTemp == "testnet")
490 NETWORK_ID = 1;
491 else if (strTemp == "devnet")
492 NETWORK_ID = 2;
493 else
494 NETWORK_ID = beast::lexicalCastThrow<uint32_t>(strTemp);
495 }
496
497 if (getSingleSection(secConfig, SECTION_PEER_PRIVATE, strTemp, j_))
498 PEER_PRIVATE = beast::lexicalCastThrow<bool>(strTemp);
499
500 if (getSingleSection(secConfig, SECTION_PEERS_MAX, strTemp, j_))
501 {
502 PEERS_MAX = beast::lexicalCastThrow<std::size_t>(strTemp);
503 }
504 else
505 {
506 std::optional<std::size_t> peers_in_max{};
507 if (getSingleSection(secConfig, SECTION_PEERS_IN_MAX, strTemp, j_))
508 {
509 peers_in_max = beast::lexicalCastThrow<std::size_t>(strTemp);
510 if (*peers_in_max > 1000)
511 Throw<std::runtime_error>("Invalid value specified in [" SECTION_PEERS_IN_MAX
512 "] section; the value must be less or equal than 1000");
513 }
514
515 std::optional<std::size_t> peers_out_max{};
516 if (getSingleSection(secConfig, SECTION_PEERS_OUT_MAX, strTemp, j_))
517 {
518 peers_out_max = beast::lexicalCastThrow<std::size_t>(strTemp);
519 if (*peers_out_max < 10 || *peers_out_max > 1000)
520 Throw<std::runtime_error>("Invalid value specified in [" SECTION_PEERS_OUT_MAX
521 "] section; the value must be in range 10-1000");
522 }
523
524 // if one section is configured then the other must be configured too
525 if ((peers_in_max && !peers_out_max) || (peers_out_max && !peers_in_max))
526 Throw<std::runtime_error>("Both sections [" SECTION_PEERS_IN_MAX
527 "]"
528 "and [" SECTION_PEERS_OUT_MAX "] must be configured");
529
530 if (peers_in_max && peers_out_max)
531 {
532 PEERS_IN_MAX = *peers_in_max;
533 PEERS_OUT_MAX = *peers_out_max;
534 }
535 }
536
537 if (getSingleSection(secConfig, SECTION_NODE_SIZE, strTemp, j_))
538 {
539 if (boost::iequals(strTemp, "tiny"))
540 NODE_SIZE = 0;
541 else if (boost::iequals(strTemp, "small"))
542 NODE_SIZE = 1;
543 else if (boost::iequals(strTemp, "medium"))
544 NODE_SIZE = 2;
545 else if (boost::iequals(strTemp, "large"))
546 NODE_SIZE = 3;
547 else if (boost::iequals(strTemp, "huge"))
548 NODE_SIZE = 4;
549 else
550 NODE_SIZE = std::min<std::size_t>(4, beast::lexicalCastThrow<std::size_t>(strTemp));
551 }
552
553 if (getSingleSection(secConfig, SECTION_SIGNING_SUPPORT, strTemp, j_))
554 signingEnabled_ = beast::lexicalCastThrow<bool>(strTemp);
555
556 if (getSingleSection(secConfig, SECTION_ELB_SUPPORT, strTemp, j_))
557 ELB_SUPPORT = beast::lexicalCastThrow<bool>(strTemp);
558
559 getSingleSection(secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_);
560 getSingleSection(secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_);
561
562 if (getSingleSection(secConfig, SECTION_SSL_VERIFY, strTemp, j_))
563 SSL_VERIFY = beast::lexicalCastThrow<bool>(strTemp);
564
565 if (getSingleSection(secConfig, SECTION_RELAY_VALIDATIONS, strTemp, j_))
566 {
567 if (boost::iequals(strTemp, "all"))
569 else if (boost::iequals(strTemp, "trusted"))
571 else if (boost::iequals(strTemp, "drop_untrusted"))
573 else
574 Throw<std::runtime_error>("Invalid value specified in [" SECTION_RELAY_VALIDATIONS "] section");
575 }
576
577 if (getSingleSection(secConfig, SECTION_RELAY_PROPOSALS, strTemp, j_))
578 {
579 if (boost::iequals(strTemp, "all"))
581 else if (boost::iequals(strTemp, "trusted"))
583 else if (boost::iequals(strTemp, "drop_untrusted"))
585 else
586 Throw<std::runtime_error>("Invalid value specified in [" SECTION_RELAY_PROPOSALS "] section");
587 }
588
589 if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN))
590 Throw<std::runtime_error>("Cannot have both [" SECTION_VALIDATION_SEED "] and [" SECTION_VALIDATOR_TOKEN
591 "] config sections");
592
593 if (getSingleSection(secConfig, SECTION_NETWORK_QUORUM, strTemp, j_))
594 NETWORK_QUORUM = beast::lexicalCastThrow<std::size_t>(strTemp);
595
596 FEES = setup_FeeVote(section("voting"));
597 /* [fee_default] is documented in the example config files as useful for
598 * things like offline transaction signing. Until that's completely
599 * deprecated, allow it to override the [voting] section. */
600 if (getSingleSection(secConfig, SECTION_FEE_DEFAULT, strTemp, j_))
601 FEES.reference_fee = beast::lexicalCastThrow<std::uint64_t>(strTemp);
602
603 if (getSingleSection(secConfig, SECTION_LEDGER_HISTORY, strTemp, j_))
604 {
605 if (boost::iequals(strTemp, "full"))
607 else if (boost::iequals(strTemp, "none"))
608 LEDGER_HISTORY = 0;
609 else
610 LEDGER_HISTORY = beast::lexicalCastThrow<std::uint32_t>(strTemp);
611 }
612
613 if (getSingleSection(secConfig, SECTION_FETCH_DEPTH, strTemp, j_))
614 {
615 if (boost::iequals(strTemp, "none"))
616 FETCH_DEPTH = 0;
617 else if (boost::iequals(strTemp, "full"))
618 FETCH_DEPTH = std::numeric_limits<decltype(FETCH_DEPTH)>::max();
619 else
620 FETCH_DEPTH = beast::lexicalCastThrow<std::uint32_t>(strTemp);
621
622 if (FETCH_DEPTH < 10)
623 FETCH_DEPTH = 10;
624 }
625
626 // By default, validators don't have pathfinding enabled, unless it is
627 // explicitly requested by the server's admin.
628 if (exists(SECTION_VALIDATION_SEED) || exists(SECTION_VALIDATOR_TOKEN))
629 PATH_SEARCH_MAX = 0;
630
631 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_OLD, strTemp, j_))
632 PATH_SEARCH_OLD = beast::lexicalCastThrow<int>(strTemp);
633 if (getSingleSection(secConfig, SECTION_PATH_SEARCH, strTemp, j_))
634 PATH_SEARCH = beast::lexicalCastThrow<int>(strTemp);
635 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_FAST, strTemp, j_))
636 PATH_SEARCH_FAST = beast::lexicalCastThrow<int>(strTemp);
637 if (getSingleSection(secConfig, SECTION_PATH_SEARCH_MAX, strTemp, j_))
638 PATH_SEARCH_MAX = beast::lexicalCastThrow<int>(strTemp);
639
640 if (getSingleSection(secConfig, SECTION_DEBUG_LOGFILE, strTemp, j_))
641 DEBUG_LOGFILE = strTemp;
642
643 if (getSingleSection(secConfig, SECTION_SWEEP_INTERVAL, strTemp, j_))
644 {
645 SWEEP_INTERVAL = beast::lexicalCastThrow<std::size_t>(strTemp);
646
647 if (SWEEP_INTERVAL < 10 || SWEEP_INTERVAL > 600)
648 Throw<std::runtime_error>("Invalid " SECTION_SWEEP_INTERVAL ": must be between 10 and 600 inclusive");
649 }
650
651 if (getSingleSection(secConfig, SECTION_WORKERS, strTemp, j_))
652 {
653 WORKERS = beast::lexicalCastThrow<int>(strTemp);
654
655 if (WORKERS < 1 || WORKERS > 1024)
656 Throw<std::runtime_error>("Invalid " SECTION_WORKERS ": must be between 1 and 1024 inclusive.");
657 }
658
659 if (getSingleSection(secConfig, SECTION_IO_WORKERS, strTemp, j_))
660 {
661 IO_WORKERS = beast::lexicalCastThrow<int>(strTemp);
662
663 if (IO_WORKERS < 1 || IO_WORKERS > 1024)
664 Throw<std::runtime_error>("Invalid " SECTION_IO_WORKERS ": must be between 1 and 1024 inclusive.");
665 }
666
667 if (getSingleSection(secConfig, SECTION_PREFETCH_WORKERS, strTemp, j_))
668 {
669 PREFETCH_WORKERS = beast::lexicalCastThrow<int>(strTemp);
670
671 if (PREFETCH_WORKERS < 1 || PREFETCH_WORKERS > 1024)
672 Throw<std::runtime_error>("Invalid " SECTION_PREFETCH_WORKERS ": must be between 1 and 1024 inclusive.");
673 }
674
675 if (getSingleSection(secConfig, SECTION_COMPRESSION, strTemp, j_))
676 COMPRESSION = beast::lexicalCastThrow<bool>(strTemp);
677
678 if (getSingleSection(secConfig, SECTION_LEDGER_REPLAY, strTemp, j_))
679 LEDGER_REPLAY = beast::lexicalCastThrow<bool>(strTemp);
680
681 if (exists(SECTION_REDUCE_RELAY))
682 {
683 auto sec = section(SECTION_REDUCE_RELAY);
684
686 // vp_enable config option is deprecated by vp_base_squelch_enable //
687 // This option is kept for backwards compatibility. When squelching //
688 // is the default algorithm, it must be replaced with: //
689 // VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = //
690 // sec.value_or("vp_base_squelch_enable", true); //
691 if (sec.exists("vp_base_squelch_enable") && sec.exists("vp_enable"))
692 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
693 " cannot specify both vp_base_squelch_enable and vp_enable "
694 "options. "
695 "vp_enable was deprecated and replaced by "
696 "vp_base_squelch_enable");
697
698 if (sec.exists("vp_base_squelch_enable"))
699 VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = sec.value_or("vp_base_squelch_enable", false);
700 else if (sec.exists("vp_enable"))
701 VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE = sec.value_or("vp_enable", false);
702 else
705
707 // Temporary squelching config for the peers selected as a source of //
708 // validator messages. The config must be removed once squelching is //
709 // made the default routing algorithm. //
710 VP_REDUCE_RELAY_SQUELCH_MAX_SELECTED_PEERS = sec.value_or("vp_base_squelch_max_selected_peers", 5);
712 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
713 " vp_base_squelch_max_selected_peers must be "
714 "greater than or equal to 3");
716
717 TX_REDUCE_RELAY_ENABLE = sec.value_or("tx_enable", false);
718 TX_REDUCE_RELAY_METRICS = sec.value_or("tx_metrics", false);
719 TX_REDUCE_RELAY_MIN_PEERS = sec.value_or("tx_min_peers", 20);
720 TX_RELAY_PERCENTAGE = sec.value_or("tx_relay_percentage", 25);
721 if (TX_RELAY_PERCENTAGE < 10 || TX_RELAY_PERCENTAGE > 100 || TX_REDUCE_RELAY_MIN_PEERS < 10)
722 Throw<std::runtime_error>("Invalid " SECTION_REDUCE_RELAY
723 ", tx_min_peers must be greater than or equal to 10"
724 ", tx_relay_percentage must be greater than or equal to 10 "
725 "and less than or equal to 100");
726 }
727
728 if (getSingleSection(secConfig, SECTION_MAX_TRANSACTIONS, strTemp, j_))
729 {
730 MAX_TRANSACTIONS = std::clamp(beast::lexicalCastThrow<int>(strTemp), MIN_JOB_QUEUE_TX, MAX_JOB_QUEUE_TX);
731 }
732
733 if (getSingleSection(secConfig, SECTION_SERVER_DOMAIN, strTemp, j_))
734 {
735 if (!isProperlyFormedTomlDomain(strTemp))
736 {
737 Throw<std::runtime_error>("Invalid " SECTION_SERVER_DOMAIN
738 ": the domain name does not appear to meet the requirements.");
739 }
740
741 SERVER_DOMAIN = strTemp;
742 }
743
744 if (exists(SECTION_OVERLAY))
745 {
746 auto const sec = section(SECTION_OVERLAY);
747
748 using namespace std::chrono;
749
750 try
751 {
752 if (auto val = sec.get("max_unknown_time"))
753 MAX_UNKNOWN_TIME = seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
754 }
755 catch (...)
756 {
757 Throw<std::runtime_error>("Invalid value 'max_unknown_time' in " SECTION_OVERLAY
758 ": must be of the form '<number>' representing seconds.");
759 }
760
761 if (MAX_UNKNOWN_TIME < seconds{300} || MAX_UNKNOWN_TIME > seconds{1800})
762 Throw<std::runtime_error>("Invalid value 'max_unknown_time' in " SECTION_OVERLAY
763 ": the time must be between 300 and 1800 seconds, inclusive.");
764
765 try
766 {
767 if (auto val = sec.get("max_diverged_time"))
768 MAX_DIVERGED_TIME = seconds{beast::lexicalCastThrow<std::uint32_t>(*val)};
769 }
770 catch (...)
771 {
772 Throw<std::runtime_error>("Invalid value 'max_diverged_time' in " SECTION_OVERLAY
773 ": must be of the form '<number>' representing seconds.");
774 }
775
777 {
778 Throw<std::runtime_error>("Invalid value 'max_diverged_time' in " SECTION_OVERLAY
779 ": the time must be between 60 and 900 seconds, inclusive.");
780 }
781 }
782
783 if (getSingleSection(secConfig, SECTION_AMENDMENT_MAJORITY_TIME, strTemp, j_))
784 {
785 using namespace std::chrono;
786 boost::regex const re("^\\s*(\\d+)\\s*(minutes|hours|days|weeks)\\s*(\\s+.*)?$");
787 boost::smatch match;
788 if (!boost::regex_match(strTemp, match, re))
789 Throw<std::runtime_error>("Invalid " SECTION_AMENDMENT_MAJORITY_TIME
790 ", must be: [0-9]+ [minutes|hours|days|weeks]");
791
792 std::uint32_t duration = beast::lexicalCastThrow<std::uint32_t>(match[1].str());
793
794 if (boost::iequals(match[2], "minutes"))
796 else if (boost::iequals(match[2], "hours"))
798 else if (boost::iequals(match[2], "days"))
800 else if (boost::iequals(match[2], "weeks"))
802
804 Throw<std::runtime_error>("Invalid " SECTION_AMENDMENT_MAJORITY_TIME
805 ", the minimum amount of time an amendment must hold a "
806 "majority is 15 minutes");
807 }
808
809 if (getSingleSection(secConfig, SECTION_BETA_RPC_API, strTemp, j_))
810 BETA_RPC_API = beast::lexicalCastThrow<bool>(strTemp);
811
812 // Do not load trusted validator configuration for standalone mode
813 if (!RUN_STANDALONE)
814 {
815 // If a file was explicitly specified, then throw if the
816 // path is malformed or if the file does not exist or is
817 // not a file.
818 // If the specified file is not an absolute path, then look
819 // for it in the same directory as the config file.
820 // If no path was specified, then look for validators.txt
821 // in the same directory as the config file, but don't complain
822 // if we can't find it.
823 boost::filesystem::path validatorsFile;
824
825 if (getSingleSection(secConfig, SECTION_VALIDATORS_FILE, strTemp, j_))
826 {
827 validatorsFile = strTemp;
828
829 if (validatorsFile.empty())
830 Throw<std::runtime_error>("Invalid path specified in [" SECTION_VALIDATORS_FILE "]");
831
832 if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty())
833 validatorsFile = CONFIG_DIR / validatorsFile;
834
835 if (!boost::filesystem::exists(validatorsFile))
836 Throw<std::runtime_error>(
837 "The file specified in [" SECTION_VALIDATORS_FILE
838 "] "
839 "does not exist: " +
840 validatorsFile.string());
841
842 else if (
843 !boost::filesystem::is_regular_file(validatorsFile) && !boost::filesystem::is_symlink(validatorsFile))
844 Throw<std::runtime_error>(
845 "Invalid file specified in [" SECTION_VALIDATORS_FILE "]: " + validatorsFile.string());
846 }
847 else if (!CONFIG_DIR.empty())
848 {
849 validatorsFile = CONFIG_DIR / validatorsFileName;
850
851 if (!validatorsFile.empty())
852 {
853 if (!boost::filesystem::exists(validatorsFile))
854 validatorsFile.clear();
855 else if (
856 !boost::filesystem::is_regular_file(validatorsFile) &&
857 !boost::filesystem::is_symlink(validatorsFile))
858 validatorsFile.clear();
859 }
860 }
861
862 if (!validatorsFile.empty() && boost::filesystem::exists(validatorsFile) &&
863 (boost::filesystem::is_regular_file(validatorsFile) || boost::filesystem::is_symlink(validatorsFile)))
864 {
865 boost::system::error_code ec;
866 auto const data = getFileContents(ec, validatorsFile);
867 if (ec)
868 {
869 Throw<std::runtime_error>(
870 "Failed to read '" + validatorsFile.string() + "'." + std::to_string(ec.value()) + ": " +
871 ec.message());
872 }
873
874 auto iniFile = parseIniFile(data, true);
875
876 auto entries = getIniFileSection(iniFile, SECTION_VALIDATORS);
877
878 if (entries)
879 section(SECTION_VALIDATORS).append(*entries);
880
881 auto valKeyEntries = getIniFileSection(iniFile, SECTION_VALIDATOR_KEYS);
882
883 if (valKeyEntries)
884 section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries);
885
886 auto valSiteEntries = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_SITES);
887
888 if (valSiteEntries)
889 section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries);
890
891 auto valListKeys = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_KEYS);
892
893 if (valListKeys)
894 section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys);
895
896 auto valListThreshold = getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_THRESHOLD);
897
898 if (valListThreshold)
899 section(SECTION_VALIDATOR_LIST_THRESHOLD).append(*valListThreshold);
900
901 if (!entries && !valKeyEntries && !valListKeys)
902 Throw<std::runtime_error>(
903 "The file specified in [" SECTION_VALIDATORS_FILE
904 "] "
905 "does not contain a [" SECTION_VALIDATORS
906 "], "
907 "[" SECTION_VALIDATOR_KEYS
908 "] or "
909 "[" SECTION_VALIDATOR_LIST_KEYS
910 "]"
911 " section: " +
912 validatorsFile.string());
913 }
914
916 auto const& listThreshold = section(SECTION_VALIDATOR_LIST_THRESHOLD);
917 if (listThreshold.lines().empty())
918 return std::nullopt;
919 else if (listThreshold.values().size() == 1)
920 {
921 auto strTemp = listThreshold.values()[0];
922 auto const listThreshold = beast::lexicalCastThrow<std::size_t>(strTemp);
923 if (listThreshold == 0)
924 return std::nullopt; // NOTE: Explicitly ask for computed
925 else if (listThreshold > section(SECTION_VALIDATOR_LIST_KEYS).values().size())
926 {
927 Throw<std::runtime_error>(
928 "Value in config section "
929 "[" SECTION_VALIDATOR_LIST_THRESHOLD "] exceeds the number of configured list keys");
930 }
931 return listThreshold;
932 }
933 else
934 {
935 Throw<std::runtime_error>(
936 "Config section "
937 "[" SECTION_VALIDATOR_LIST_THRESHOLD "] should contain single value only");
938 }
939 }();
940
941 // Consolidate [validator_keys] and [validators]
942 section(SECTION_VALIDATORS).append(section(SECTION_VALIDATOR_KEYS).lines());
943
944 if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() &&
945 section(SECTION_VALIDATOR_LIST_KEYS).lines().empty())
946 {
947 Throw<std::runtime_error>("[" + std::string(SECTION_VALIDATOR_LIST_KEYS) + "] config section is missing");
948 }
949 }
950
951 {
952 auto const part = section("features");
953 for (auto const& s : part.values())
954 {
955 if (auto const f = getRegisteredFeature(s))
956 features.insert(*f);
957 else
958 Throw<std::runtime_error>("Unknown feature: " + s + " in config file.");
959 }
960 }
961
962 // This doesn't properly belong here, but check to make sure that the
963 // value specified for network_quorum is achievable:
964 {
965 auto pm = PEERS_MAX;
966
967 // FIXME this apparently magic value is actually defined as a constant
968 // elsewhere (see defaultMaxPeers) but we handle this check here.
969 if (pm == 0)
970 pm = 21;
971
972 if (NETWORK_QUORUM > pm)
973 {
974 Throw<std::runtime_error>(
975 "The minimum number of required peers (network_quorum) exceeds "
976 "the maximum number of allowed peers (peers_max)");
977 }
978 }
979}
980
981boost::filesystem::path
983{
984 auto log_file = DEBUG_LOGFILE;
985
986 if (!log_file.empty() && !log_file.is_absolute())
987 {
988 // Unless an absolute path for the log file is specified, the
989 // path is relative to the config file directory.
990 log_file = boost::filesystem::absolute(log_file, CONFIG_DIR);
991 }
992
993 if (!log_file.empty())
994 {
995 auto log_dir = log_file.parent_path();
996
997 if (!boost::filesystem::is_directory(log_dir))
998 {
999 boost::system::error_code ec;
1000 boost::filesystem::create_directories(log_dir, ec);
1001
1002 // If we fail, we warn but continue so that the calling code can
1003 // decide how to handle this situation.
1004 if (ec)
1005 {
1006 std::cerr << "Unable to create log file path " << log_dir << ": " << ec.message() << '\n';
1007 }
1008 }
1009 }
1010
1011 return log_file;
1012}
1013
1014int
1016{
1017 auto const index = static_cast<std::underlying_type_t<SizedItem>>(item);
1018 XRPL_ASSERT(index < sizedItems.size(), "xrpl::Config::getValueFor : valid index input");
1019 XRPL_ASSERT(!node || *node <= 4, "xrpl::Config::getValueFor : unset or valid node");
1020 return sizedItems.at(index).second.at(node.value_or(NODE_SIZE));
1021}
1022
1024setup_FeeVote(Section const& section)
1025{
1026 FeeSetup setup;
1027 {
1028 std::uint64_t temp;
1029 if (set(temp, "reference_fee", section) && temp <= std::numeric_limits<XRPAmount::value_type>::max())
1030 setup.reference_fee = temp;
1031 }
1032 {
1033 std::uint32_t temp;
1034 if (set(temp, "account_reserve", section))
1035 setup.account_reserve = temp;
1036 if (set(temp, "owner_reserve", section))
1037 setup.owner_reserve = temp;
1038 }
1039 return setup;
1040}
1041
1042} // 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:418
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:440
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:982
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:1015
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:1024
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:392
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)