Skip to content

Commit

Permalink
Parsing of [validator_list_threshold] section, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Nov 5, 2024
1 parent 460cc96 commit 5974b27
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
83 changes: 83 additions & 0 deletions src/test/core/Config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ moreripplevalidators.net
[validator_list_keys]
03E74EE14CB525AFBB9F1B7D86CD58ECC4B91452294B42AB4E78F260BD905C091D
030775A669685BD6ABCEBD80385921C7851783D991A8055FD21D2F3966C96F1B56
[validator_list_threshold]
2
)rippleConfig");
return configContents;
}
Expand Down Expand Up @@ -642,6 +645,34 @@ trustthesevalidators.gov
}
BEAST_EXPECT(error == expectedError);
}
{
// load should throw if [validator_list_threshold] is malformed
Config c;
std::string toLoad(R"rippleConfig(
[validator_list_sites]
ripplevalidators.com
trustthesevalidators.gov
[validator_list_keys]
021A99A537FDEBC34E4FCA03B39BEADD04299BB19E85097EC92B15A3518801E566
[validator_list_threshold]
value = 2
)rippleConfig");
std::string error;
auto const expectedError =
"Config section [validator_list_threshold] should contain "
"single value";
try
{
c.loadFromString(toLoad);
}
catch (std::runtime_error& e)
{
error = e.what();
}
BEAST_EXPECT(error == expectedError);
}
{
// load should throw if [validator_list_threshold] is negative
Config c;
Expand Down Expand Up @@ -703,6 +734,10 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}
{
// load from specified [validators_file] file name
Expand All @@ -721,6 +756,10 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}
{
// load from specified [validators_file] relative path
Expand All @@ -739,6 +778,10 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}
{
// load from validators file in default location
Expand All @@ -755,6 +798,10 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}
{
// load from specified [validators_file] instead
Expand All @@ -775,6 +822,10 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 2);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}

{
Expand Down Expand Up @@ -812,6 +863,38 @@ trustthesevalidators.gov
c.section(SECTION_VALIDATOR_LIST_SITES).values().size() == 4);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_KEYS).values().size() == 3);
BEAST_EXPECT(
c.section(SECTION_VALIDATOR_LIST_THRESHOLD).values().size() ==
1);
BEAST_EXPECT(c.VALIDATOR_LIST_THRESHOLD == 2);
}
{
// load should throw if [validator_list_threshold] is present both
// in rippled cfg and validators file
boost::format cc(R"rippleConfig(
[validators_file]
%1%
[validator_list_threshold]
1
)rippleConfig");
std::string error;
detail::ValidatorsTxtGuard const vtg(
*this, "test_cfg", "validators.cfg");
BEAST_EXPECT(vtg.validatorsFileExists());
auto const expectedError =
"Config section [validator_list_threshold] should contain "
"single value";
try
{
Config c;
c.loadFromString(boost::str(cc % vtg.validatorsFile()));
}
catch (std::runtime_error& e)
{
error = e.what();
}
BEAST_EXPECT(error == expectedError);
}
{
// load should throw if [validators], [validator_keys] and
Expand Down
17 changes: 13 additions & 4 deletions src/xrpld/core/detail/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,13 @@ Config::loadFromString(std::string const& fileContents)
}

VALIDATOR_LIST_THRESHOLD = [&]() -> std::optional<std::size_t> {
std::string strTemp;
if (getSingleSection(
secConfig, SECTION_VALIDATOR_LIST_THRESHOLD, strTemp, j_))
auto const& listThreshold =
section(SECTION_VALIDATOR_LIST_THRESHOLD);
if (listThreshold.lines().empty())
return std::nullopt;
else if (listThreshold.values().size() == 1)
{
auto strTemp = listThreshold.values()[0];
auto const listThreshold =
beast::lexicalCastThrow<std::size_t>(strTemp);
if (listThreshold == 0)
Expand All @@ -950,7 +953,13 @@ Config::loadFromString(std::string const& fileContents)
}
return listThreshold;
}
return std::nullopt;
else
{
Throw<std::runtime_error>(
"Config section "
"[" SECTION_VALIDATOR_LIST_THRESHOLD
"] should contain single value");
}
}();

// Consolidate [validator_keys] and [validators]
Expand Down

0 comments on commit 5974b27

Please sign in to comment.