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